webmachinelearning / webnn-polyfill

🧠⚙️ Web Neural Network API polyfill based on TensorFlow.js
https://www.npmjs.com/package/@webmachinelearning/webnn-polyfill
Apache License 2.0
102 stars 18 forks source link

"The context paramter [sic] is invalid" error if Experimental Web Platform features flag is on #186

Open anssiko opened 2 years ago

anssiko commented 2 years ago

All samples at https://webmachinelearning.github.io/webnn-samples/ fail with:

The context paramter is invalid

If chrome://flags/#enable-experimental-web-platform-features is enabled.

Tested with Chrome Stable 107 and Canary 109.

huningxin commented 2 years ago

The model loader API exposes the MLContext interface and it will be enabled by this flag. The webnn-polyfill should adapt to this and only polyfill the MLGraphBuilder and MLGraph interfaces.

@BruceDai , could you please take a look at this issue?

BruceDai commented 2 years ago

Yes, when testing by Chrome with enabled enable-experimental-web-platform-features flag, the following code would invoke createContext() function of ML interface from Model Load API.

const context = navigator.ml.createContext(contextOptions);

The createContext() function of Model Load API is an async function, it returns a Promise\<MLContext> object, while WebNN API MLGraphBuilder constructor requires a context parameter of MLContext type, so the error was alerted.

this.builder_ = new MLGraphBuilder(context);

To fix this issue need modify code using await, @Honry PTAL, thanks.

const context = await navigator.ml.createContext(contextOptions);

while there's an issue https://github.com/webmachinelearning/model-loader/issues/41 of Model Load API.

And current WebNN Polyfill API checks whether Navigator object has ML interface, if not, WebNN Polyfill API will provide it.

if (navigator.ml == null) {
  navigator.ml = new ML();
}
Honry commented 2 years ago

We are enabling latest async and sync APIs (PRs: https://github.com/webmachinelearning/webnn-polyfill/pull/172, https://github.com/webmachinelearning/webnn-samples/pull/150), which should fix this issue.