Skip to content

Commit 4fe0f93

Browse files
committed
Use more widely-supported method to get GPU name
1 parent 29cd2cb commit 4fe0f93

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed

examples/piston-train-toy/src/lib/components/controls/Controls.svelte

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
controlSectionsOpen,
1818
getGpuName,
1919
gpuPowerPreference,
20+
hasWebGPU,
2021
setGpuName,
2122
toggleControlSection
2223
} from '$lib/workspace/ui.svelte';
@@ -78,12 +79,34 @@
7879
JSON.stringify(config.model);
7980
JSON.stringify(config.data);
8081
JSON.stringify(gpuPowerPreference.current);
81-
setGpuName(null);
8282
8383
// Trigger parameter count when config changes, but triggerParameterCount itself needs to be untracked
8484
untrack(() => triggerModelInspection());
8585
});
8686
87+
$effect(() => {
88+
// Recompute GPU name whenever WebGPU availability or power preference changes
89+
JSON.stringify(gpuPowerPreference.current);
90+
const has = hasWebGPU.current;
91+
if (!has || typeof navigator === 'undefined' || !('gpu' in navigator)) {
92+
setGpuName(null);
93+
return;
94+
}
95+
void (async () => {
96+
try {
97+
const adapter = await navigator.gpu.requestAdapter({
98+
powerPreference: gpuPowerPreference.current
99+
});
100+
// Use info.description if available
101+
const name = adapter?.info?.description;
102+
setGpuName(typeof name === 'string' && name.length > 0 ? name : null);
103+
} catch (error) {
104+
console.error('Error requesting WebGPU adapter:', error);
105+
setGpuName(null);
106+
}
107+
})();
108+
});
109+
87110
// Preset selection wiring: local selection drives setPreset, stays in sync with config
88111
let lastAppliedPreset = $state<string | null>(null);
89112
let selectedPresetId = $state<string>('');

examples/piston-train-toy/src/lib/train/moduleWorker.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,8 @@ self.addEventListener('message', async (event) => {
312312

313313
// Apply GPU power preference before any GPU usage
314314
if (gpuPowerPreference) {
315-
const name =
316-
(await piston.applyGpuPowerPreference(gpuPowerPreference))?.adapterInfo?.description ??
317-
null;
318-
self.postMessage({ type: 'gpu.info', name: name ?? null });
315+
await piston.applyGpuPowerPreference(gpuPowerPreference);
316+
(globalThis as unknown as { piston: typeof piston }).piston = piston;
319317
}
320318

321319
console.debug('Inspecting model...');

examples/piston-train-toy/src/lib/workspace/ui.svelte.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ export const gpuPowerPreference = new LocalStorage<'high-performance' | 'low-pow
4242
'gpuPowerPreference',
4343
'high-performance'
4444
);
45-
let gpuName = $state<string | null>(null);
45+
const gpuName = $state<{ current: string | null }>({ current: null });
4646
export function setGpuName(name: string | null) {
47-
gpuName = name;
47+
gpuName.current = name;
4848
}
4949
export function getGpuName() {
50-
return gpuName;
50+
return gpuName.current;
5151
}
5252

5353
export const setupUI = () => {

examples/piston-train-toy/src/lib/workspace/workers.svelte.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { lastSessionStore } from './lastSessionStore';
99
import { currentRun, log, runsMap } from './runs.svelte';
1010
import {
1111
gpuPowerPreference,
12-
setGpuName,
1312
triggerLowDiversityDatasetError,
1413
triggerVramLimitFlash
1514
} from './ui.svelte';
@@ -451,11 +450,6 @@ export async function initializeModelInspectionWorker() {
451450
case 'error':
452451
console.error('[Main] Model inspection worker error:', data.message);
453452
break;
454-
case 'gpu.info': {
455-
const name = (data as { name?: string | null }).name ?? null;
456-
setGpuName(name ?? null);
457-
break;
458-
}
459453
}
460454
};
461455

0 commit comments

Comments
 (0)