From 06e90e4c2222437cb1a77605c03a4b5e6168282d Mon Sep 17 00:00:00 2001 From: Wanming Lin Date: Thu, 14 Nov 2024 13:34:34 +0800 Subject: [PATCH 1/2] Fix MLTensorUsage is undefined issue Polyfill MLTensorUsage to make it compatible with old version of Chrome --- common/utils.js | 7 +++++-- nnotepad/js/nnotepad.js | 3 ++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/common/utils.js b/common/utils.js index a9c089e2..4eed4084 100644 --- a/common/utils.js +++ b/common/utils.js @@ -306,8 +306,11 @@ export function getUrlParams() { export async function isWebNN() { if (typeof MLGraphBuilder !== 'undefined') { - const context = await navigator.ml.createContext(); - return !context.tf; + if (typeof MLTensorUsage == 'undefined') { + // Polyfill MLTensorUsage to make it compatible with old version of Chrome. + window.MLTensorUsage = {WEBGPU_INTEROP: 1, READ: 2, WRITE: 4}; + } + return true; } else { return false; } diff --git a/nnotepad/js/nnotepad.js b/nnotepad/js/nnotepad.js index 315b9f5a..21906a63 100644 --- a/nnotepad/js/nnotepad.js +++ b/nnotepad/js/nnotepad.js @@ -52,7 +52,8 @@ class WebNNUtil { dataType: isShapeMethod ? operand.dataType() : operand.dataType, dimensions: isShapeMethod ? operand.shape() : operand.shape, shape: isShapeMethod ? operand.shape() : operand.shape, - usage: MLTensorUsage.READ, + usage: typeof MLTensorUsage == 'undefined' ? + undefined : MLTensorUsage.READ, readable: true, }; const tensor = await context.createTensor(desc); From f45c7537d2cdfb98db0abfec8587eeaad6bdbdf7 Mon Sep 17 00:00:00 2001 From: Wanming Lin Date: Thu, 14 Nov 2024 13:36:36 +0800 Subject: [PATCH 2/2] Fix lint issue --- common/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/utils.js b/common/utils.js index 4eed4084..f7c61cec 100644 --- a/common/utils.js +++ b/common/utils.js @@ -306,8 +306,8 @@ export function getUrlParams() { export async function isWebNN() { if (typeof MLGraphBuilder !== 'undefined') { + // Polyfill MLTensorUsage to make it compatible with old version of Chrome. if (typeof MLTensorUsage == 'undefined') { - // Polyfill MLTensorUsage to make it compatible with old version of Chrome. window.MLTensorUsage = {WEBGPU_INTEROP: 1, READ: 2, WRITE: 4}; } return true;