onnxruntime/js/web/test/e2e/browser-test-webgpu-external-data.js
Yulong Wang 07cfc56538
[js] enable external data loading for ort-web (#19087)
### Description
enable external data loading for ort-web.

### Why
The ORT external data design is highly depending on the file system,
especially synchronous file I/O APIs. Those are not available in web
platforms. We need to have extra code to make external data working on
web.

### How
Considering there is no file system in web, an implementation for web to
support external data is to use pre-loaded data. Assume model file
a.onnx includes initializers that linked to ./b.bin, we require users to
pass a full data file list when creating the session. The user code will
be look like:
```js
const mySess = await ort.InferenceSession.create('./path/model/a.onnx', {
  // session options
  externalData: [
    {
      // relative or absolute path/URL of the file,
      // or a pre-loaded Uint8Array containing the data of the external data file
      data: './path/data/b.bin', 

      // the relative path of the external data. Should match initializers' "location" value defined in the model file
      path: './b.bin'
    },
    // { } if multiple external data file
  ]
});
```

Currently, this feature only works with JSEP build enabled.
2024-01-12 19:24:24 -08:00

24 lines
804 B
JavaScript

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
it('Browser E2E testing - WebGPU backend with external data', async function() {
const session = await ort.InferenceSession.create('./model_with_orig_ext_data.onnx', {
executionProviders: ['webgpu'],
externalData: [{data: './model_with_orig_ext_data.bin', path: 'model_with_orig_ext_data.bin'}]
});
const fetches = await session.run({X: new ort.Tensor('float32', [1, 1], [1, 2])});
const Y = fetches.Y;
assert(Y instanceof ort.Tensor);
assert(Y.dims.length === 2 && Y.dims[0] === 2 && Y.dims[1] === 3);
assert(Y.data[0] === 1);
assert(Y.data[1] === 1);
assert(Y.data[2] === 0);
assert(Y.data[3] === 0);
assert(Y.data[4] === 0);
assert(Y.data[5] === 0);
});