mirror of
https://github.com/saymrwulf/onnxruntime.git
synced 2026-05-16 21:00:14 +00:00
### 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.
63 lines
2.5 KiB
JavaScript
63 lines
2.5 KiB
JavaScript
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
// Licensed under the MIT License.
|
|
|
|
'use strict';
|
|
|
|
// this is a simple HTTP server that enables CORS.
|
|
// following code is based on https://developer.mozilla.org/en-US/docs/Learn/Server-side/Node_server_without_framework
|
|
|
|
const http = require('http');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const validRequests = {
|
|
// .wasm files
|
|
'/dist/ort-wasm.wasm': ['dist/ort-wasm.wasm', 'application/wasm'],
|
|
'/dist/ort-wasm-simd.wasm': ['dist/ort-wasm-simd.wasm', 'application/wasm'],
|
|
'/dist/ort-wasm-threaded.wasm': ['dist/ort-wasm-threaded.wasm', 'application/wasm'],
|
|
'/dist/ort-wasm-simd-threaded.wasm': ['dist/ort-wasm-simd-threaded.wasm', 'application/wasm'],
|
|
'/dist/ort-wasm-simd.jsep.wasm': ['dist/ort-wasm-simd.jsep.wasm', 'application/wasm'],
|
|
|
|
// proxied .wasm files:
|
|
'/test-wasm-path-override/ort-wasm.wasm': ['dist/ort-wasm.wasm', 'application/wasm'],
|
|
//'/test-wasm-path-override/renamed.wasm': ['dist/ort-wasm.wasm', 'application/wasm'],
|
|
|
|
// .js files
|
|
'/dist/ort.min.js': ['dist/ort.min.js', 'text/javascript'],
|
|
'/dist/ort.js': ['dist/ort.js', 'text/javascript'],
|
|
'/dist/ort.webgl.min.js': ['dist/ort.webgl.min.js', 'text/javascript'],
|
|
'/dist/ort.webgpu.min.js': ['dist/ort.webgpu.min.js', 'text/javascript'],
|
|
'/dist/ort.wasm.min.js': ['dist/ort.wasm.min.js', 'text/javascript'],
|
|
'/dist/ort.wasm-core.min.js': ['dist/ort.wasm-core.min.js', 'text/javascript'],
|
|
};
|
|
|
|
module.exports = function(dir) {
|
|
http.createServer(function(request, response) {
|
|
console.log(`request ${request.url.replace(/\n|\r/g, '')}`);
|
|
|
|
const requestData = validRequests[request.url];
|
|
if (!request) {
|
|
response.writeHead(404);
|
|
response.end('404');
|
|
} else {
|
|
const [filePath, contentType] = requestData;
|
|
fs.readFile(path.resolve(dir, filePath), function(error, content) {
|
|
if (error) {
|
|
if (error.code == 'ENOENT') {
|
|
response.writeHead(404);
|
|
response.end('404');
|
|
} else {
|
|
response.writeHead(500);
|
|
response.end('500');
|
|
}
|
|
} else {
|
|
response.setHeader('access-control-allow-origin', '*');
|
|
response.writeHead(200, {'Content-Type': contentType});
|
|
response.end(content, 'utf-8');
|
|
}
|
|
});
|
|
}
|
|
})
|
|
.listen(8081);
|
|
console.log('Server running at http://127.0.0.1:8081/');
|
|
};
|