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) {
+2 -2
View File
@@ -7,7 +7,7 @@
*/
/** Devices the worker can actually run on (D-004). */
export type InferenceDevice = "webgpu" | "wasm";
export type InferenceDevice = "webgpu" | "wasm" | "webnn";
/**
* What the UI may request. "auto" (the default) lets the worker probe
@@ -147,7 +147,7 @@ function isOptionalString(value: unknown): value is string | undefined {
return value === undefined || typeof value === "string";
}
const DEVICES: readonly string[] = ["webgpu", "wasm"];
const DEVICES: readonly string[] = ["webgpu", "wasm", "webnn"];
const DTYPES: readonly string[] = ["fp32", "fp16", "q8", "q4"];
export function isInferenceDevice(value: unknown): value is InferenceDevice {