onnxruntime: [Web] `executionProviders` chain for `webnn` fallback does not work on init error
Describe the issue
In case of any error during init with webnn the execution provider blocks and does not fallback to the next provider in chain. For example, DirectML API init in windows 10 with GPU initialization will not fallback to others.
This only occurs if the WebNN API is enabled in the browser flags.
To reproduce
Try in Windows 10 with a browser with the API enabled with following code fragment:
const mySession = await ort.InferenceSession.create("./model.onnx", {
executionProviders: [
{
name: "webnn",
deviceType: "gpu",
},
{
name: "webnn",
deviceType: "cpu",
},
"wasm",
"cpu",
],
});
Results in error:
Failed to execute 'createContext' on 'ML': DirectML: The DirectML feature level on this platform is lower than the minimum required one.
The fallback works if the webnn api is not available.
Urgency
NA. Just doing some PoCs.
ONNX Runtime Installation
Released Package
ONNX Runtime Version or Commit ID
1.18.0
Execution Provider
Other / Unknown
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 2
- Comments: 30 (29 by maintainers)
That’s true. However I suppose the concern is this is a breaking change.
Could we make MLContext as an option for advanced usages (I/O binding, WebGPU interop etc.,) while keep the the existing options? If MLContext option is present, other options are ignored.
Considering recent issues/PRs about WebNN, here is a summary of requirements:
MLContextusing different combinations of options when trying to initialize WebNN.MLContextin order to use IO bindingIt is not straightforward to implement those features. The major reason is because the existing design was based on some old assumptions that are no longer true for the new scenarios:
How about we let ORT just accept an instance of
MLContextand leave the creation ofMLContextto users? This is also what we did for WebGPU when integrated with Transformerjs. We allow users to setadaptorwhich is created by themselves (https://onnxruntime.ai/docs/api/js/interfaces/Env.WebGpuFlags.html#adapter). This should solve recent issues and the only concern is the backward compatibility. @Honry@sansmoraxz
At the current stage, you can try Edge browser dev channel that supports WebNN on Windows 10.
Agreed, I think MLContext should expose
preferredLayout, I add this input to corresponding spec issue for WG discussion: https://github.com/webmachinelearning/webnn/issues/463#issuecomment-2132473790Yes, I think this is a good idea. This allows to preserve backward compatibility.
With all the discussions above, I created #20816, which tries to make an API update for
executionProvidersfor WebNN. Please help to review the change.I don’t think this is a clean solution. A better solution would be exposing properties for getting those metadata from the MLContext object, which may require a spec review process.
The reason is not only because the options are redundant info (because the process of creating MLContext implicitly include those info), but also may cause inconsistency. User may use ‘cpu’ to create the MLContext but passing to session options with that MLContext and
deviceType: 'gpu'. We can only assume that users may not make this kind of errors.For now, we can ask users to pass those info in session options and eventually deprecate those options and just let passing MLContext only, which may take several versions after the spec allows it (if this ever happens).
I think before WebNN allows getting the options from a
MLContextobject, it seems that we need users to pass bothdeviceTypeandpowerPreferencein session options.If MLContext is not specified, they are used to create the MLContext (or fail if not available). If MLContext is specified, they are still need in C++ for setting preferred layout, etc.
WebNN spec supports both layouts. This workaround is for a limitation of previous Chromium WebNN XNNPACK implementation for CPU. Now Chromium is using TFLite implementation for CPU and supporting NCHW. This workaround will not be necessary anymore once that is fixed.
There is an ongoing discussion in Working Group about Allow checking whether operators/types are supported for a backend before creating a graph. I think querying the preferred layout is a good use case. Feel free to chime in and share your inputs.
That sounds correct to me. I can only think of one easier way to do that: if we restrict that at the same time only one session is being initialized, we can just use the “currentContext” instead of a map. Then all other parts should be similar (using the
Moduleobject and embind)@fdwr, following’s the code snippet where it checks the availability of WebNN API. I drafted a PR to attempt to check the creation of WebNN
MLContextat the same place, while it requires an additionalWebNNExecutionProviderOptionoption parameter, which makes this parameter special in its parent methods (all related to EPs initialization).@fs-eire, could you pls. take a look at the PR? Do you have any other good idea? Thanks!