From 0b5d5d0e9fb23df11f2f853e2b84867627189677 Mon Sep 17 00:00:00 2001 From: Reilly Grant Date: Fri, 10 Nov 2023 21:25:24 -0800 Subject: [PATCH] Finish the patch --- common/utils.js | 25 ++++++------------------- face_recognition/index.html | 6 +++--- face_recognition/main.js | 22 ++++++++++------------ facial_landmark_detection/index.html | 6 +++--- facial_landmark_detection/main.js | 22 ++++++++++------------ image_classification/index.html | 6 +++--- image_classification/main.js | 24 +++++++++++------------- lenet/index.html | 6 +++--- lenet/main.js | 4 ++-- nsnet2/index.html | 6 +++--- nsnet2/main.js | 4 ++-- object_detection/index.html | 6 +++--- object_detection/main.js | 22 ++++++++++------------ rnnoise/index.html | 6 +++--- rnnoise/main.js | 4 ++-- semantic_segmentation/index.html | 6 +++--- semantic_segmentation/main.js | 22 ++++++++++------------ style_transfer/index.html | 6 +++--- style_transfer/main.js | 21 +++++++++------------ 19 files changed, 99 insertions(+), 125 deletions(-) diff --git a/common/utils.js b/common/utils.js index 89fd13f2..a76f3ef0 100644 --- a/common/utils.js +++ b/common/utils.js @@ -199,17 +199,12 @@ export function getMedianValue(array) { (array[array.length / 2 - 1] + array[array.length / 2]) / 2; } -// Set tf.js backend based WebNN's 'MLDeviceType' option -export async function setPolyfillBackend(device) { - // Simulate WebNN's device selection using various tf.js backends. - // MLDeviceType: ['default', 'webgl', 'webgpu', 'cpu'] - // 'default' or 'webgl': tfjs-backend-webgl, 'webgpu': tfjs-backend-webgpu, - // 'cpu': tfjs-backend-wasm - if (!device) device = 'webgpu'; - // Use 'webgl' by default for better performance. +// Set tf.js backend +export async function setPolyfillBackend(backend) { + if (!backend) backend = 'webgpu'; + // Use 'webgpu' by default for better performance. // Note: 'wasm' backend may run failed on some samples since // some ops aren't supported on 'wasm' backend at present - const backend = device === 'cpu' ? 'wasm' : device; const context = await navigator.ml.createContext(); const tf = context.tf; if (tf) { @@ -240,13 +235,6 @@ export async function setPolyfillBackend(device) { `WebNN-polyfill with tf.js ${tf.version_core} ` + `${backendInfo} backend.`, 'info'); } - switch (device) { - case 'webgl': - case 'webgpu': - return 'gpu'; - default: - return 'cpu'; - } } // Get url params @@ -285,7 +273,7 @@ export function getUrlParams() { } // Set backend for using WebNN-polyfill or WebNN -export async function setBackend(backend, device) { +export async function setBackend(backend, device, polyfillBackend) { const webnnPolyfillId = 'webnn_polyfill'; const webnnNodeId = 'webnn_node'; const webnnPolyfillElem = document.getElementById(webnnPolyfillId); @@ -312,7 +300,7 @@ export async function setBackend(backend, device) { // Create WebNN-polyfill script await loadScript(webnnPolyfillUrl, webnnPolyfillId); } - return await setPolyfillBackend(device); + await setPolyfillBackend(polyfillBackend); } else if (backend === 'webnn') { // For Electron if (isElectron()) { @@ -334,7 +322,6 @@ export async function setBackend(backend, device) { addAlert(`WebNN is not supported!`, 'warning'); } } - return device; } else { addAlert(`Unknown backend: ${backend}`, 'warning'); } diff --git a/face_recognition/index.html b/face_recognition/index.html index 28cd18b5..e6b1f323 100644 --- a/face_recognition/index.html +++ b/face_recognition/index.html @@ -32,13 +32,13 @@