onnxruntime/js/web/lib/wasm/wasm-utils-load-file.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

77 lines
2.8 KiB
TypeScript

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as fs from 'fs';
import {readFile} from 'node:fs/promises';
/**
* Load a file into a Uint8Array.
*
* @param file - the file to load. Can be a URL/path, a Blob, an ArrayBuffer, or a Uint8Array.
* @returns a Uint8Array containing the file data.
*/
export const loadFile = async(file: string|Blob|ArrayBufferLike|Uint8Array): Promise<Uint8Array> => {
if (typeof file === 'string') {
if (typeof process !== 'undefined' && process.versions && process.versions.node) {
// load file into ArrayBuffer in Node.js
try {
return new Uint8Array(await readFile(file));
} catch (e) {
if (e.code === 'ERR_FS_FILE_TOO_LARGE') {
// file is too large, use fs.createReadStream instead
const stream = fs.createReadStream(file);
const chunks: Uint8Array[] = [];
for await (const chunk of stream) {
chunks.push(chunk);
}
return new Uint8Array(Buffer.concat(chunks));
}
throw e;
}
} else {
// load file into ArrayBuffer in browsers
const response = await fetch(file);
if (!response.ok) {
throw new Error(`failed to load external data file: ${file}`);
}
const contentLengthHeader = response.headers.get('Content-Length');
const fileSize = contentLengthHeader ? parseInt(contentLengthHeader, 10) : 0;
if (fileSize < 1073741824 /* 1GB */) {
// when Content-Length header is not set, we cannot determine the file size. We assume it is small enough to
// load into memory.
return new Uint8Array(await response.arrayBuffer());
} else {
// file is too large, use stream instead
if (!response.body) {
throw new Error(`failed to load external data file: ${file}, no response body.`);
}
const reader = response.body.getReader();
// use WebAssembly Memory to allocate larger ArrayBuffer
const pages = Math.ceil(fileSize / 65536);
const buffer = new WebAssembly.Memory({initial: pages, maximum: pages}).buffer;
let offset = 0;
// eslint-disable-next-line no-constant-condition
while (true) {
const {done, value} = await reader.read();
if (done) {
break;
}
const chunkSize = value.byteLength;
const chunk = new Uint8Array(buffer, offset, chunkSize);
chunk.set(value);
offset += chunkSize;
}
return new Uint8Array(buffer, 0, fileSize);
}
}
} else if (file instanceof Blob) {
return new Uint8Array(await file.arrayBuffer());
} else if (file instanceof Uint8Array) {
return file;
} else {
return new Uint8Array(file);
}
};