WebNN support initial
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user