onnxruntime/js/web/lib/wasm/proxy-worker/main.ts
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

123 lines
4.3 KiB
TypeScript

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
/// <reference lib="webworker" />
//
// * type hack for "HTMLImageElement"
//
// in typescript, the type of "HTMLImageElement" is defined in lib.dom.d.ts, which is conflict with lib.webworker.d.ts.
// when we use webworker, the lib.webworker.d.ts will be used, which does not have HTMLImageElement defined.
//
// we will get the following errors complaining that HTMLImageElement is not defined:
//
// ====================================================================================================================
//
// ../common/dist/cjs/tensor-factory.d.ts:187:29 - error TS2552: Cannot find name 'HTMLImageElement'. Did you mean
// 'HTMLLIElement'?
//
// 187 fromImage(imageElement: HTMLImageElement, options?: TensorFromImageElementOptions):
// Promise<TypedTensor<'float32'> | TypedTensor<'uint8'>>;
// ~~~~~~~~~~~~~~~~
//
// node_modules/@webgpu/types/dist/index.d.ts:83:7 - error TS2552: Cannot find name 'HTMLImageElement'. Did you mean
// 'HTMLLIElement'?
//
// 83 | HTMLImageElement
// ~~~~~~~~~~~~~~~~
//
// ====================================================================================================================
//
// `HTMLImageElement` is only used in type declaration and not in real code. So we define it as `unknown` here to
// bypass the type check.
//
declare global {
type HTMLImageElement = unknown;
}
import {OrtWasmMessage, SerializableTensorMetadata} from '../proxy-messages';
import {createSession, copyFromExternalBuffer, endProfiling, extractTransferableBuffers, initEp, initRuntime, releaseSession, run} from '../wasm-core-impl';
import {initializeWebAssembly} from '../wasm-factory';
self.onmessage = (ev: MessageEvent<OrtWasmMessage>): void => {
const {type, in : message} = ev.data;
try {
switch (type) {
case 'init-wasm':
initializeWebAssembly(message!.wasm)
.then(
() => {
initRuntime(message!).then(
() => {
postMessage({type});
},
err => {
postMessage({type, err});
});
},
err => {
postMessage({type, err});
});
break;
case 'init-ep': {
const {epName, env} = message!;
initEp(env, epName)
.then(
() => {
postMessage({type});
},
err => {
postMessage({type, err});
});
break;
}
case 'copy-from': {
const {buffer} = message!;
const bufferData = copyFromExternalBuffer(buffer);
postMessage({type, out: bufferData} as OrtWasmMessage);
break;
}
case 'create': {
const {model, options} = message!;
createSession(model, options)
.then(
sessionMetadata => {
postMessage({type, out: sessionMetadata} as OrtWasmMessage);
},
err => {
postMessage({type, err});
});
break;
}
case 'release':
releaseSession(message!);
postMessage({type});
break;
case 'run': {
const {sessionId, inputIndices, inputs, outputIndices, options} = message!;
run(sessionId, inputIndices, inputs, outputIndices, new Array(outputIndices.length).fill(null), options)
.then(
outputs => {
if (outputs.some(o => o[3] !== 'cpu')) {
postMessage({type, err: 'Proxy does not support non-cpu tensor location.'});
} else {
postMessage(
{type, out: outputs} as OrtWasmMessage,
extractTransferableBuffers(outputs as SerializableTensorMetadata[]));
}
},
err => {
postMessage({type, err});
});
break;
}
case 'end-profiling':
endProfiling(message!);
postMessage({type});
break;
default:
}
} catch (err) {
postMessage({type, err} as OrtWasmMessage);
}
};