Skip to content

Commit

Permalink
[js/web] allow load WebAssembly binary from buffer (#21534)
Browse files Browse the repository at this point in the history
### Description

This PR adds a new option `ort.env.wasm.wasmBinary`, which allows user
to set to a buffer containing preload .wasm file content.

This PR should resolve the problem from latest discussion in #20876.
  • Loading branch information
fs-eire authored Jul 29, 2024
1 parent 0d7cf30 commit b03c949
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cmake/onnxruntime_webassembly.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ else()
"SHELL:-s EXPORT_ALL=0"
"SHELL:-s VERBOSE=0"
"SHELL:-s FILESYSTEM=0"
"SHELL:-s INCOMING_MODULE_JS_API=[preRun,locateFile,arguments,onExit,wasmMemory,buffer,instantiateWasm,mainScriptUrlOrBlob]"
"SHELL:-s INCOMING_MODULE_JS_API=[locateFile,instantiateWasm,wasmBinary]"
"SHELL:-s WASM_BIGINT=1"
${WASM_API_EXCEPTION_CATCHING}
--no-entry
Expand Down
6 changes: 6 additions & 0 deletions js/common/lib/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ export declare namespace Env {
*/
wasmPaths?: WasmPrefixOrFilePaths;

/**
* Set a custom buffer which contains the WebAssembly binary. If this property is set, the `wasmPaths` property will
* be ignored.
*/
wasmBinary?: ArrayBufferLike|Uint8Array;

/**
* Set or get a boolean value indicating whether to proxy the execution of main thread to a worker thread.
*
Expand Down
8 changes: 7 additions & 1 deletion js/web/lib/wasm/wasm-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export const initializeWebAssembly = async(flags: Env.WebAssemblyFlags): Promise
const mjsPathOverride = (mjsPathOverrideFlag as URL)?.href ?? mjsPathOverrideFlag;
const wasmPathOverrideFlag = (wasmPaths as Env.WasmFilePaths)?.wasm;
const wasmPathOverride = (wasmPathOverrideFlag as URL)?.href ?? wasmPathOverrideFlag;
const wasmBinaryOverride = flags.wasmBinary;

const [objectUrl, ortWasmFactory] = (await importWasmModule(mjsPathOverride, wasmPrefixOverride, numThreads > 1));

Expand Down Expand Up @@ -135,7 +136,12 @@ export const initializeWebAssembly = async(flags: Env.WebAssemblyFlags): Promise
numThreads,
};

if (wasmPathOverride || wasmPrefixOverride) {
if (wasmBinaryOverride) {
/**
* Set a custom buffer which contains the WebAssembly binary. This will skip the wasm file fetching.
*/
config.wasmBinary = wasmBinaryOverride;
} else if (wasmPathOverride || wasmPrefixOverride) {
/**
* A callback function to locate the WebAssembly file. The function should return the full path of the file.
*
Expand Down
22 changes: 22 additions & 0 deletions js/web/test/e2e/browser-test-wasm-binary-override.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

'use strict';

const documentUrl = document.currentScript.src;

it('Browser E2E testing - WebAssembly backend', async function() {
// preload .wasm file binary
const wasmUrl = new URL('./node_modules/onnxruntime-web/dist/ort-wasm-simd-threaded.wasm', documentUrl).href;
const response = await fetch(wasmUrl);

// make sure the .wasm file is loaded successfully
assert(response.ok);
assert(response.headers.get('Content-Type') === 'application/wasm');

// override wasm binary
const binary = await response.arrayBuffer();
ort.env.wasm.wasmBinary = binary;

await testFunction(ort, {executionProviders: ['wasm']});
});
3 changes: 3 additions & 0 deletions js/web/test/e2e/run-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ const BROWSER_TEST_CASES = [
[true, false, './browser-test-wasm.js', 'ort.bundle.min.mjs', ['num_threads=2', 'proxy=1']], // 2 threads, proxy
[true, false, './browser-test-wasm.js', 'ort.bundle.min.mjs', ['num_threads=1', 'proxy=1']], // 1 thread, proxy

// wasm binary override:
[true, false, './browser-test-wasm-binary-override.js', 'ort.min.js'],

// path override:
// wasm, path override filenames for both mjs and wasm, same origin
[true, false, './browser-test-wasm-path-override-filename.js', 'ort.min.js', ['port=9876', 'files=mjs,wasm']],
Expand Down

0 comments on commit b03c949

Please sign in to comment.