@@ -269,48 +269,6 @@ export function getMedianValue(array) {
269
269
( array [ array . length / 2 - 1 ] + array [ array . length / 2 ] ) / 2 ;
270
270
}
271
271
272
- // Set tf.js backend based WebNN's 'MLDeviceType' option
273
- export async function setPolyfillBackend ( device ) {
274
- // Simulate WebNN's device selection using various tf.js backends.
275
- // MLDeviceType: ['default', 'gpu', 'cpu']
276
- // 'default' or 'gpu': tfjs-backend-webgl, 'cpu': tfjs-backend-wasm
277
- if ( ! device ) device = 'gpu' ;
278
- // Use 'webgl' by default for better performance.
279
- // Note: 'wasm' backend may run failed on some samples since
280
- // some ops aren't supported on 'wasm' backend at present
281
- const backend = device === 'cpu' ? 'wasm' : 'webgl' ;
282
- const context = await navigator . ml . createContext ( ) ;
283
- const tf = context . tf ;
284
- if ( tf ) {
285
- if ( backend == 'wasm' ) {
286
- const wasm = context . wasm ;
287
- // Force to use Wasm SIMD only
288
- wasm . setWasmPath ( `https://unpkg.com/@tensorflow/tfjs-backend-wasm@${ tf . version_core } /dist/tfjs-backend-wasm-simd.wasm` ) ;
289
- }
290
- if ( ! ( await tf . setBackend ( backend ) ) ) {
291
- throw new Error ( `Failed to set tf.js backend ${ backend } .` ) ;
292
- }
293
- await tf . ready ( ) ;
294
- let backendInfo = backend == 'wasm' ? 'WASM' : 'WebGL' ;
295
- if ( backendInfo == 'WASM' ) {
296
- const hasSimd = tf . env ( ) . features [ 'WASM_HAS_SIMD_SUPPORT' ] ;
297
- const hasThreads = tf . env ( ) . features [ 'WASM_HAS_MULTITHREAD_SUPPORT' ] ;
298
- if ( hasThreads && hasSimd ) {
299
- backendInfo += ' (SIMD + threads)' ;
300
- } else if ( hasThreads && ! hasSimd ) {
301
- backendInfo += ' (threads)' ;
302
- } else if ( ! hasThreads && hasSimd ) {
303
- backendInfo += ' (SIMD)' ;
304
- }
305
- }
306
- addAlert (
307
- `This sample is running on ` +
308
- `<a href='https://github.com/webmachinelearning/webnn-polyfill'>` +
309
- `WebNN-polyfill</a> with tf.js ${ tf . version_core } ` +
310
- `<b>${ backendInfo } </b> backend.` , 'info' ) ;
311
- }
312
- }
313
-
314
272
// Get url params
315
273
export function getUrlParams ( ) {
316
274
const params = new URLSearchParams ( location . search ) ;
@@ -346,59 +304,6 @@ export function getUrlParams() {
346
304
return [ numRuns , powerPreference , numThreads ] ;
347
305
}
348
306
349
- // Set backend for using WebNN-polyfill or WebNN
350
- export async function setBackend ( backend , device ) {
351
- const webnnPolyfillId = 'webnn_polyfill' ;
352
- const webnnNodeId = 'webnn_node' ;
353
- const webnnPolyfillElem = document . getElementById ( webnnPolyfillId ) ;
354
- const webnnNodeElem = document . getElementById ( webnnNodeId ) ;
355
-
356
- if ( backend === 'polyfill' ) {
357
- if ( webnnNodeElem ) {
358
- document . body . removeChild ( webnnNodeElem ) ;
359
- // Unset global objects defined in node_setup.js
360
- global . navigator . ml = undefined ;
361
- global . MLContext = undefined ;
362
- global . MLGraphBuilder = undefined ;
363
- global . MLGraph = undefined ;
364
- global . MLOperand = undefined ;
365
- }
366
- if ( ! webnnPolyfillElem ) {
367
- const webnnPolyfillUrl =
368
- 'https://webmachinelearning.github.io/webnn-polyfill/dist/webnn-polyfill.js' ;
369
- if ( typeof ( tf ) != 'undefined' ) {
370
- // Reset tf.ENV to avoid environments from tf.min.js
371
- // affect webnn-polyfill.js
372
- tf . engine ( ) . reset ( ) ;
373
- }
374
- // Create WebNN-polyfill script
375
- await loadScript ( webnnPolyfillUrl , webnnPolyfillId ) ;
376
- }
377
- await setPolyfillBackend ( device ) ;
378
- } else if ( backend === 'webnn' ) {
379
- if ( ! await isWebNN ( ) ) {
380
- addAlert ( `WebNN is not supported!` , 'warning' ) ;
381
- }
382
- } else {
383
- addAlert ( `Unknow backend: ${ backend } ` , 'warning' ) ;
384
- }
385
- }
386
-
387
- // Promise to load script with url and id
388
- async function loadScript ( url , id ) {
389
- return new Promise ( ( resolve , reject ) => {
390
- const script = document . createElement ( 'script' ) ;
391
- script . onload = resolve ;
392
- script . onerror = reject ;
393
- script . src = url ;
394
- script . id = id ;
395
- if ( url . startsWith ( 'http' ) ) {
396
- script . crossOrigin = 'anonymous' ;
397
- }
398
- document . body . appendChild ( script ) ;
399
- } ) ;
400
- }
401
-
402
307
export async function isWebNN ( ) {
403
308
if ( typeof MLGraphBuilder !== 'undefined' ) {
404
309
const context = await navigator . ml . createContext ( ) ;
@@ -408,6 +313,14 @@ export async function isWebNN() {
408
313
}
409
314
}
410
315
316
+ export function webNNNotSupportMessage ( ) {
317
+ return 'Your browser does not support WebNN.' ;
318
+ }
319
+
320
+ export function webNNNotSupportMessageHTML ( ) {
321
+ return 'Your browser does not support WebNN. Please refer to <a href="https://github.com/webmachinelearning/webnn-samples/#webnn-installation-guides">WebNN Installation Guides</a> for more details.' ;
322
+ }
323
+
411
324
// Derive from
412
325
// https://github.com/webmachinelearning/webnn-baseline/blob/main/src/lib/compute-padding.js
413
326
/**
0 commit comments