WebNN support initial

This commit is contained in:
Lucas Winther
2026-07-10 11:18:03 +02:00
parent 71cb61cd90
commit db7b8a22c4
5 changed files with 41 additions and 17 deletions
+12 -3
View File
@@ -86,14 +86,22 @@ function forwardProgress(info: ProgressInfo): void {
// Device selection (D-004): WebGPU when available, automatic WASM fallback
// ---------------------------------------------------------------------------
interface NavigatorWithGpu {
interface NavigatorWithHardware {
gpu?: { requestAdapter(): Promise<unknown | null> };
ml?: unknown;
}
async function detectDevice(preference: DevicePreference): Promise<InferenceDevice> {
if (preference === "wasm") return "wasm";
if (preference === "webnn") {
try {
return (navigator as NavigatorWithHardware).ml ? "webnn" : "wasm";
} catch {
return "wasm";
}
}
try {
const adapter = await (navigator as NavigatorWithGpu).gpu?.requestAdapter();
const adapter = await (navigator as NavigatorWithHardware).gpu?.requestAdapter();
return adapter ? "webgpu" : "wasm";
} catch {
return "wasm";
@@ -127,7 +135,8 @@ async function createPipeline(
// If WebGPU was selected but pipeline creation throws (driver quirks,
// unsupported ops, out-of-memory), fall back to WASM automatically (D-004).
const attempts: InferenceDevice[] =
detected === "webgpu" ? ["webgpu", "wasm"] : ["wasm"];
detected === "webgpu" ? ["webgpu", "wasm"] :
detected === "webnn" ? ["webnn", "wasm"] : ["wasm"];
let lastError: unknown;
for (const device of attempts) {