Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions api/routers/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def registry_tool_lines(registry: MeshOpsRegistry) -> str:
- **install_extension(github_url)** / **uninstall_extension(extension_id)** / **reload_extensions** — Manage extensions.
- **download_model_weights(extension_id, variant?)** / **delete_model_weights(extension_id, variant?)** — Manage model weights.
- **open_workflow(workflow_id)** / **delete_workflow(workflow_id)** — Open a workflow in the editor, or delete it.
- **set_setting(key, value)** — Change `uiScale` (one of `small`, `medium`, `large`, `very-large`), `useAtkinsonFont` or `showRamIndicator` (booleans).
- **set_setting(key, value)** — Change `uiScale` (one of `small`, `medium`, `large`, `very-large`), `useAtkinsonFont`, `showRamIndicator` or `showVramIndicator` (booleans).
- **set_storage_folder(kind, path)** — Move the `models`, `workspace` or `workflows` folder.
- **undo_mesh** / **redo_mesh** — Undo or redo the last change to the current mesh.
- **navigate(page)** — Switch the app to another page.
Expand Down Expand Up @@ -378,9 +378,9 @@ def _function(name: str, description: str, properties: dict | None = None, requi
),
_function(
"set_setting",
"Change a UI setting. uiScale is one of small, medium, large, very-large; useAtkinsonFont and showRamIndicator are booleans.",
"Change a UI setting. uiScale is one of small, medium, large, very-large; useAtkinsonFont, showRamIndicator and showVramIndicator are booleans.",
{
"key": {"type": "string", "enum": ["uiScale", "useAtkinsonFont", "showRamIndicator"]},
"key": {"type": "string", "enum": ["uiScale", "useAtkinsonFont", "showRamIndicator", "showVramIndicator"]},
"value": {"type": ["string", "boolean"], "description": "For uiScale one of: small, medium, large, very-large. A boolean otherwise."},
},
["key", "value"],
Expand Down
8 changes: 4 additions & 4 deletions api/tests/test_agent_client_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ def test_every_client_tool_has_a_schema(self) -> None:

def test_enums(self) -> None:
props = self._tool("set_setting")["parameters"]["properties"]
self.assertEqual(props["key"]["enum"], ["uiScale", "useAtkinsonFont", "showRamIndicator"])
self.assertEqual(props["key"]["enum"], ["uiScale", "useAtkinsonFont", "showRamIndicator", "showVramIndicator"])
# The app's scale names, not a number: the frontend accepts only these.
self.assertEqual(props["value"]["type"], ["string", "boolean"])
self.assertIn("small, medium, large, very-large", props["value"]["description"])
Expand Down Expand Up @@ -372,7 +372,7 @@ def test_system_prompt_lists_the_new_tools(self) -> None:
"modelsDir": "D:/models", "workspaceDir": "D:/ws",
"workflowsDir": "D:/wf", "extensionsDir": "D:/ext",
},
"ui": {"uiScale": "large", "useAtkinsonFont": False, "showRamIndicator": True},
"ui": {"uiScale": "large", "useAtkinsonFont": False, "showRamIndicator": True, "showVramIndicator": False},
}


Expand All @@ -389,7 +389,7 @@ def test_list_extensions(self) -> None:

def test_get_settings(self) -> None:
text, payload = _run("get_settings")
for expected in ("D:/models", "D:/ws", "D:/wf", "D:/ext", "uiScale: large", "useAtkinsonFont: false", "showRamIndicator: true"):
for expected in ("D:/models", "D:/ws", "D:/wf", "D:/ext", "uiScale: large", "useAtkinsonFont: false", "showRamIndicator: true", "showVramIndicator: false"):
self.assertIn(expected, text)
self.assertIsNone(payload)
self.assertIn("No settings", _run("get_settings", context={})[0])
Expand Down Expand Up @@ -452,7 +452,7 @@ def test_context_is_rendered_into_the_single_system_message(self) -> None:
"- sf3d: SF3D",
"- wf1: My Workflow",
"D:/models", "D:/ws", "D:/wf", "D:/ext",
"uiScale: large", "useAtkinsonFont: false", "showRamIndicator: true",
"uiScale: large", "useAtkinsonFont: false", "showRamIndicator: true", "showVramIndicator: false",
"Current mesh path: Default/mesh.glb",
"- trellis/gen (image→mesh): Trellis",
):
Expand Down
148 changes: 148 additions & 0 deletions electron/main/gpu-memory.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import assert from 'node:assert/strict'
import test from 'node:test'

async function load() {
return import(new URL('./gpu-memory.ts', import.meta.url).href)
}

const MIB = 1024 * 1024

test('parses a single GPU and converts MiB to bytes', async () => {
const { parseNvidiaSmiGpus, pickGpu, toGpuMemory } = await load()
const gpus = parseNvidiaSmiGpus('0, NVIDIA GeForce RTX 4090, 24564, 1234\n')
assert.deepEqual(gpus, [{ index: 0, name: 'NVIDIA GeForce RTX 4090', totalMiB: 24564, usedMiB: 1234 }])
assert.deepEqual(toGpuMemory(pickGpu(gpus)), {
total: 24564 * MIB,
used: 1234 * MIB,
available: (24564 - 1234) * MIB,
name: 'NVIDIA GeForce RTX 4090',
})
})

test('picks the GPU with the most memory on a multi-GPU machine', async () => {
const { parseNvidiaSmiGpus, pickGpu } = await load()
const stdout = [
'0, NVIDIA GeForce GTX 1650, 4096, 300',
'1, NVIDIA GeForce RTX 3090, 24576, 9000',
'2, NVIDIA GeForce RTX 3060, 12288, 100',
].join('\r\n')
const gpus = parseNvidiaSmiGpus(stdout)
assert.equal(gpus.length, 3)
assert.equal(pickGpu(gpus).index, 1)
assert.equal(pickGpu(gpus).name, 'NVIDIA GeForce RTX 3090')
})

test('keeps the first GPU on a tie', async () => {
const { parseNvidiaSmiGpus, pickGpu } = await load()
const gpus = parseNvidiaSmiGpus('0, A, 8192, 1\n1, B, 8192, 2\n')
assert.equal(pickGpu(gpus).index, 0)
})

test('rebuilds names containing commas and collapses extra spaces', async () => {
const { parseNvidiaSmiGpus } = await load()
const [gpu] = parseNvidiaSmiGpus(' 3 , Quadro RTX, 8000 Rev,B , 49152 , 0 ')
assert.deepEqual(gpu, { index: 3, name: 'Quadro RTX, 8000 Rev, B', totalMiB: 49152, usedMiB: 0 })
})

test('skips lines with [N/A] or garbage and keeps the valid ones', async () => {
const { parseNvidiaSmiGpus, pickGpu } = await load()
const gpus = parseNvidiaSmiGpus('0, Tesla, [N/A], [N/A]\nnot a gpu\n1, RTX 4070, 12282, 512\n')
assert.equal(gpus.length, 1)
assert.equal(pickGpu(gpus).name, 'RTX 4070')
})

test('empty or error output yields no GPU', async () => {
const { parseNvidiaSmiGpus, pickGpu } = await load()
for (const stdout of ['', '\n\n', 'NVIDIA-SMI has failed because it couldn\'t communicate with the NVIDIA driver.', ', , ,', '0, X, 0, 0']) {
assert.equal(pickGpu(parseNvidiaSmiGpus(stdout)), null, JSON.stringify(stdout))
}
})

test('sampler returns the parsed GPU and passes the timeout', async () => {
const { createGpuMemorySampler, NVIDIA_SMI_ARGS } = await load()
const calls: Array<{ args: string[]; timeoutMs: number }> = []
const sample = createGpuMemorySampler({
platform: 'win32',
timeoutMs: 3000,
run: async (args: string[], timeoutMs: number) => {
calls.push({ args, timeoutMs })
return { kind: 'ok', stdout: '0, RTX, 8192, 2048\n' }
},
})
const mem = await sample()
assert.deepEqual(mem, { total: 8192 * MIB, used: 2048 * MIB, available: 6144 * MIB, name: 'RTX' })
assert.deepEqual(calls, [{ args: NVIDIA_SMI_ARGS, timeoutMs: 3000 }])
})

test('sampler remembers a missing nvidia-smi and stops spawning', async () => {
const { createGpuMemorySampler } = await load()
let runs = 0
const sample = createGpuMemorySampler({
platform: 'linux',
run: async () => { runs++; return { kind: 'missing' } },
})
assert.equal(await sample(), null)
assert.equal(await sample(), null)
assert.equal(await sample(), null)
assert.equal(runs, 1)
})

test('sampler retries after a transient failure (timeout / non-zero exit)', async () => {
const { createGpuMemorySampler } = await load()
const results = [{ kind: 'failed' }, { kind: 'ok', stdout: '0, RTX, 4096, 0' }]
let runs = 0
const sample = createGpuMemorySampler({
platform: 'linux',
run: async () => results[runs++],
})
assert.equal(await sample(), null)
assert.equal((await sample())?.name, 'RTX')
assert.equal(runs, 2)
})

test('sampler shares one in-flight run between concurrent calls', async () => {
const { createGpuMemorySampler } = await load()
let runs = 0
let release: (value: unknown) => void = () => {}
const sample = createGpuMemorySampler({
platform: 'win32',
run: () => {
runs++
return new Promise((resolve) => { release = resolve })
},
})
const a = sample()
const b = sample()
assert.equal(a, b)
release({ kind: 'ok', stdout: '0, RTX, 4096, 1024' })
const [ra, rb] = await Promise.all([a, b])
assert.equal(runs, 1)
assert.deepEqual(ra, rb)

// Once settled, the next call starts a fresh run.
const c = sample()
release({ kind: 'failed' })
assert.equal(await c, null)
assert.equal(runs, 2)
})

test('sampler never spawns on macOS', async () => {
const { createGpuMemorySampler } = await load()
let runs = 0
const sample = createGpuMemorySampler({
platform: 'darwin',
run: async () => { runs++; return { kind: 'ok', stdout: '0, X, 1024, 0' } },
})
assert.equal(await sample(), null)
assert.equal(runs, 0)
})

test('sampler swallows a throwing runner', async () => {
const { createGpuMemorySampler } = await load()
const sample = createGpuMemorySampler({
platform: 'linux',
run: async () => { throw new Error('boom') },
})
assert.equal(await sample(), null)
})

157 changes: 157 additions & 0 deletions electron/main/gpu-memory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/**
* VRAM sampling for the status-bar indicator (NVIDIA only, via nvidia-smi).
*
* The renderer polls this every couple of seconds, so the sampler:
* - never runs two nvidia-smi processes at once (concurrent calls share one),
* - kills a stalled nvidia-smi after a timeout (the driver can hang while a
* model is generating) and reports null for that sample,
* - stops spawning for the rest of the session once nvidia-smi turns out not
* to exist (AMD / Intel / no GPU), and
* - returns null on macOS without spawning anything.
*/

import { spawn } from 'child_process'

export interface NvidiaGpu {
index: number
name: string
totalMiB: number
usedMiB: number
}

/** Bytes. Mirrors the `system:gpuMemory` IPC contract. */
export interface GpuMemory {
total: number
used: number
available: number
name: string
}

const MIB = 1024 * 1024

export const NVIDIA_SMI_ARGS = [
'--query-gpu=index,name,memory.total,memory.used',
'--format=csv,noheader,nounits',
]

// ─── Pure helpers ─────────────────────────────────────────────────────────────

/**
* Parses `nvidia-smi --query-gpu=index,name,memory.total,memory.used
* --format=csv,noheader,nounits`, e.g. "0, NVIDIA GeForce RTX 4090, 24564, 1234".
* The name sits between the index and the two numbers, so a name that itself
* contains commas is rebuilt from the middle fields. Lines that do not parse
* (blank, "[N/A]", error text) are skipped.
*/
export function parseNvidiaSmiGpus(stdout: string): NvidiaGpu[] {
const gpus: NvidiaGpu[] = []
for (const raw of stdout.split(/\r?\n/)) {
const parts = raw.split(',').map((s) => s.trim())
if (parts.length < 4) continue
const index = Number(parts[0])
const totalMiB = Number(parts[parts.length - 2])
const usedMiB = Number(parts[parts.length - 1])
const name = parts.slice(1, -2).join(', ').replace(/\s+/g, ' ').trim()
if (parts[0] === '' || !Number.isInteger(index)) continue
if (!Number.isFinite(totalMiB) || !Number.isFinite(usedMiB)) continue
if (totalMiB <= 0 || usedMiB < 0) continue
gpus.push({ index, name, totalMiB, usedMiB })
}
return gpus
}

/** The GPU with the most memory (where heavy models end up); first one wins ties. */
export function pickGpu(gpus: NvidiaGpu[]): NvidiaGpu | null {
let best: NvidiaGpu | null = null
for (const gpu of gpus) {
if (!best || gpu.totalMiB > best.totalMiB) best = gpu
}
return best
}

export function toGpuMemory(gpu: NvidiaGpu): GpuMemory {
const total = gpu.totalMiB * MIB
const used = Math.min(gpu.usedMiB * MIB, total)
return { total, used, available: total - used, name: gpu.name || `GPU ${gpu.index}` }
}

// ─── Sampler ──────────────────────────────────────────────────────────────────

/**
* Outcome of one nvidia-smi run. `missing` means the binary is not there and
* never will be this session; `failed` covers timeouts and non-zero exits,
* which may clear up on the next sample.
*/
export type NvidiaSmiResult =
| { kind: 'ok'; stdout: string }
| { kind: 'missing' }
| { kind: 'failed' }

export type NvidiaSmiRunner = (args: string[], timeoutMs: number) => Promise<NvidiaSmiResult>

export const runNvidiaSmi: NvidiaSmiRunner = (args, timeoutMs) =>
new Promise((resolve) => {
let settled = false
const finish = (result: NvidiaSmiResult): void => {
if (settled) return
settled = true
clearTimeout(timer)
resolve(result)
}

const proc = spawn('nvidia-smi', args, {
stdio: ['ignore', 'pipe', 'ignore'],
windowsHide: true,
})
const timer = setTimeout(() => {
proc.kill()
finish({ kind: 'failed' })
}, timeoutMs)

let out = ''
proc.stdout?.on('data', (d: Buffer) => { out += d.toString() })
proc.on('close', (code) => finish(code === 0 ? { kind: 'ok', stdout: out } : { kind: 'failed' }))
proc.on('error', (err: NodeJS.ErrnoException) =>
finish(err.code === 'ENOENT' ? { kind: 'missing' } : { kind: 'failed' }))
})

export interface GpuMemorySamplerOptions {
run?: NvidiaSmiRunner
platform?: NodeJS.Platform
timeoutMs?: number
}

export function createGpuMemorySampler(options: GpuMemorySamplerOptions = {}): () => Promise<GpuMemory | null> {
const run = options.run ?? runNvidiaSmi
const platform = options.platform ?? process.platform
const timeoutMs = options.timeoutMs ?? 3000

let unavailable = platform === 'darwin'
let inFlight: Promise<GpuMemory | null> | null = null

const sampleOnce = async (): Promise<GpuMemory | null> => {
try {
const result = await run(NVIDIA_SMI_ARGS, timeoutMs)
if (result.kind === 'missing') {
unavailable = true
return null
}
if (result.kind !== 'ok') return null
const gpu = pickGpu(parseNvidiaSmiGpus(result.stdout))
return gpu ? toGpuMemory(gpu) : null
} catch {
return null
}
}

return () => {
if (unavailable) return Promise.resolve(null)
if (!inFlight) {
inFlight = sampleOnce().finally(() => { inFlight = null })
}
return inFlight
}
}

/** Session-wide sampler used by the `system:gpuMemory` IPC handler. */
export const sampleGpuMemory = createGpuMemorySampler()
5 changes: 5 additions & 0 deletions electron/main/ipc-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import {
resolveExtensionPathWithinRoot,
} from './extension-path-guard'
import { detectGpuInfo, describeGpuInfo, torchFlavorFor, type GpuInfo } from './gpu-detect'
import { sampleGpuMemory } from './gpu-memory'
import { SETUP_LAUNCHER_SOURCE } from './setup-launcher'
import {
assertCompatibleExtensionUpdateType,
Expand Down Expand Up @@ -914,6 +915,10 @@ export function setupIpcHandlers(pythonBridge: PythonBridge, getWindow: WindowGe
return { total, used: total - free, available: free }
})

// GPU memory (VRAM) — NVIDIA only via nvidia-smi
// (null on macOS, without NVIDIA, or when a sample times out).
ipcMain.handle('system:gpuMemory', () => sampleGpuMemory())

ipcMain.handle('app:info', () => ({
version: app.getVersion(),
userData: app.getPath('userData'),
Expand Down
3 changes: 3 additions & 0 deletions electron/preload/electron-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ export function createElectronApi(ipcRenderer: IpcRendererLike, webFrame: WebFra
system: {
memory: (): Promise<{ total: number; used: number; available: number }> =>
ipcRenderer.invoke('system:memory') as Promise<{ total: number; used: number; available: number }>,
/** VRAM in bytes of the largest NVIDIA GPU; null without NVIDIA, on macOS, or when a sample timed out. */
gpuMemory: (): Promise<{ total: number; used: number; available: number; name: string } | null> =>
ipcRenderer.invoke('system:gpuMemory') as Promise<{ total: number; used: number; available: number; name: string } | null>,
},

// Python / FastAPI bridge
Expand Down
Loading
Loading