Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamically load ort-wasm*.js according to the EP name #19130

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion js/web/lib/backend-wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class OnnxruntimeWebAssemblyBackend implements Backend {
initializeFlags();

// init wasm
await initializeWebAssemblyAndOrtRuntime();
await initializeWebAssemblyAndOrtRuntime(backendName);

// performe EP specific initialization
await initializeOrtEp(backendName);
Expand Down
2 changes: 1 addition & 1 deletion js/web/lib/wasm/proxy-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ interface MessageError {

interface MessageInitWasm extends MessageError {
type: 'init-wasm';
in ?: Env;
in ?: {env: Env; epName: string};
out?: never;
}

Expand Down
8 changes: 5 additions & 3 deletions js/web/lib/wasm/proxy-worker/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ self.onmessage = (ev: MessageEvent<OrtWasmMessage>): void => {
const {type, in : message} = ev.data;
try {
switch (type) {
case 'init-wasm':
initializeWebAssembly(message!.wasm)
case 'init-wasm': {
const {env, epName} = message!;
initializeWebAssembly(env!.wasm, epName)
.then(
() => {
initRuntime(message!).then(
initRuntime(env!).then(
() => {
postMessage({type});
},
Expand All @@ -59,6 +60,7 @@ self.onmessage = (ev: MessageEvent<OrtWasmMessage>): void => {
postMessage({type, err});
});
break;
}
case 'init-ep': {
const {epName, env} = message!;
initEp(env, epName)
Expand Down
6 changes: 3 additions & 3 deletions js/web/lib/wasm/proxy-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const onProxyWorkerMessage = (ev: MessageEvent<OrtWasmMessage>): void => {

const scriptSrc = typeof document !== 'undefined' ? (document?.currentScript as HTMLScriptElement)?.src : undefined;

export const initializeWebAssemblyAndOrtRuntime = async(): Promise<void> => {
export const initializeWebAssemblyAndOrtRuntime = async(epName: string): Promise<void> => {
if (initialized) {
return;
}
Expand Down Expand Up @@ -100,13 +100,13 @@ export const initializeWebAssemblyAndOrtRuntime = async(): Promise<void> => {
proxyWorker.onmessage = onProxyWorkerMessage;
URL.revokeObjectURL(workerUrl);
initWasmCallbacks = [resolve, reject];
const message: OrtWasmMessage = {type: 'init-wasm', in : env};
const message: OrtWasmMessage = {type: 'init-wasm', in : {env, epName}};
proxyWorker.postMessage(message);
});

} else {
try {
await initializeWebAssembly(env.wasm);
await initializeWebAssembly(env.wasm, epName);
await core.initRuntime(env);
initialized = true;
} catch (e) {
Expand Down
17 changes: 14 additions & 3 deletions js/web/lib/wasm/wasm-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ if (!BUILD_DEFS.DISABLE_TRAINING) {
BUILD_DEFS.DISABLE_WEBGPU ? require('./binding/ort-wasm.js') : require('./binding/ort-wasm-simd.jsep.js');
}

const ortWasmFactoryThreaded: EmscriptenModuleFactory<OrtWasmModule> = !BUILD_DEFS.DISABLE_WASM_THREAD ?
let ortWasmFactoryThreaded: EmscriptenModuleFactory<OrtWasmModule> = !BUILD_DEFS.DISABLE_WASM_THREAD ?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fs-eire, I was trying to move the code of ortWasmFactory and ortWasmFactory initialization to the initializeWebAssembly function, but failed.

Looks like it takes some other ort-web initialization work in somewhere. Is that possible to optimize them?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The initialization logic is kind of complicated. You can check #18715 for details if you are interested.

(BUILD_DEFS.DISABLE_WEBGPU ? require('./binding/ort-wasm-threaded.js') :
require('./binding/ort-wasm-simd-threaded.jsep.js')) :
ortWasmFactory;
Expand Down Expand Up @@ -88,7 +88,7 @@ const getWasmFileName = (useSimd: boolean, useThreads: boolean) => {
}
};

export const initializeWebAssembly = async(flags: Env.WebAssemblyFlags): Promise<void> => {
export const initializeWebAssembly = async(flags: Env.WebAssemblyFlags, epName: string): Promise<void> => {
if (initialized) {
return Promise.resolve();
}
Expand All @@ -109,6 +109,17 @@ export const initializeWebAssembly = async(flags: Env.WebAssemblyFlags): Promise
const useThreads = numThreads > 1 && isMultiThreadSupported();
const useSimd = simd && isSimdSupported();

// If the BUILD_DEFS.DISABLE_WEBGPU is false, the ort-web will laod ort-wasm*.jsep.js by default,
// this would cause other ep (e.g. webnn ep) fail in npm test if it is not compiled in the ort-wasm*.jsep.js.
// Overrides ortWasmFactory and ortWasmFactoryThreaded to dynamically load ort-wasm*.js according to the epName.
if (BUILD_DEFS.DISABLE_TRAINING && epName != 'webgpu') {
ortWasmFactory = useSimd ? require('./binding/ort-wasm-simd.js') : require('./binding/ort-wasm.js');
}
if (!BUILD_DEFS.DISABLE_WASM_THREAD && useThreads && epName != 'webgpu') {
ortWasmFactoryThreaded = useSimd ? require('./binding/ort-wasm-simd-threaded.js') :
require('./binding/ort-wasm-threaded.js');
}

const wasmPaths = flags.wasmPaths;
const wasmPrefixOverride = typeof wasmPaths === 'string' ? wasmPaths : undefined;
const wasmFileName = getWasmFileName(useSimd, useThreads);
Expand Down Expand Up @@ -151,7 +162,7 @@ export const initializeWebAssembly = async(flags: Env.WebAssemblyFlags): Promise

const prefix = wasmPrefixOverride ?? scriptDirectory;

if (!BUILD_DEFS.DISABLE_WEBGPU) {
if (epName == 'webgpu') {
if (wasmFileName === 'ort-wasm-simd.wasm') {
return prefix + 'ort-wasm-simd.jsep.wasm';
} else if (wasmFileName === 'ort-wasm-simd-threaded.wasm') {
Expand Down