Skip to content

Commit

Permalink
patch compatible with threads
Browse files Browse the repository at this point in the history
  • Loading branch information
tarekziade committed Jan 13, 2025
1 parent 5cdceec commit 4e87794
Showing 1 changed file with 37 additions and 18 deletions.
55 changes: 37 additions & 18 deletions onnxruntime/wasm/pre.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,28 +54,47 @@ var SharedArrayBuffer = globalThis.SharedArrayBuffer ??

/**
* Custom call to instantiate WebAssembly module. so we can use custom imports
*/ Module["instantiateWasm"] = async (info, receiveInstance) => {
const wasmBinaryFile = findWasmBinary();
const bytes = await getBinaryPromise(wasmBinaryFile);
const module = await WebAssembly.compile(bytes);
let imports = getWasmImports();
*/

const OPTIMIZED_GEMM = "mozIntGemm";
const optimizedGemmModule = WebAssembly[OPTIMIZED_GEMM];
const optimizedGemmModuleExports = new WebAssembly.Instance(optimizedGemmModule(), {
/**
* Patches the original one so we can inject mozIntGemm and do a single compilation
*
* getWasmImports() gets called in the main thread, then twice in each em-thread
* The first call is done in createWasm(), and the second via Module["instantiateWasm"]
* On the first call, the thread's wasmMemory variable is not initialized yet,
* on the second call we can hook the import
*/
getWasmImports = function() {
assignWasmImports();

var optimizedGemmModuleExports;

if (wasmMemory) {
const gemmModule = WebAssembly["mozIntGemm"];
gemmModuleExports = new WebAssembly.Instance(gemmModule(), {
"": {
memory: wasmMemory
memory: wasmMemory
}
}).exports;
}).exports;
}

imports.wasm_gemm = optimizedGemmModuleExports;
return {
"env": wasmImports,
"wasi_snapshot_preview1": wasmImports,
"wasm_gemm": gemmModuleExports
};
}

try {
var instance = new WebAssembly.Instance(module, imports);
receiveInstance(instance);
} catch (error) {
console.error("Error creating WebAssembly instance:", error);
throw error;
}
Module["instantiateWasm"] = async (info, receiveInstance) => {
const wasmBinaryFile = findWasmBinary();
const bytes = await getBinaryPromise(wasmBinaryFile);
const module = await WebAssembly.compile(bytes);
try {
var instance = new WebAssembly.Instance(module, getWasmImports());
receiveInstance(instance, module); // passing the module so threads can reuse it
} catch (error) {
console.error("Error creating WebAssembly instance:", error);
throw error;
}
};

0 comments on commit 4e87794

Please sign in to comment.