Skip to content
Open
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
32 changes: 32 additions & 0 deletions workloads/transformersjs/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,35 @@ class URL {
}
globalThis.URL = URL;

class Headers {
constructor(init) {
this._headers = new Map();
if (init instanceof Headers) {
for (const [k, v] of init._headers) {
this._headers.set(k.toLowerCase(), v);
}
} else if (init) {
for (const [k, v] of Object.entries(init)) {
this._headers.set(k.toLowerCase(), String(v));
}
}
}
get(key) { return this._headers.get(key.toLowerCase()) ?? null; }
set(key, value) { this._headers.set(key.toLowerCase(), String(value)); }
has(key) { return this._headers.has(key.toLowerCase()); }
delete(key) { this._headers.delete(key.toLowerCase()); }
[Symbol.iterator]() { return this._headers.entries(); }
}
if (typeof globalThis.Headers === 'undefined') {
globalThis.Headers = Headers;
}

// We run ONNX Runtime single-threaded (numThreads = 1), so no pthread workers
// are spawned. Delete Atomics.waitAsync so Emscripten's pthread mailbox init
// in ort-wasm-simd-threaded.mjs does not register a never-resolved async waiter
// that keeps the JSC shell alive after the benchmark completes.
delete Atomics.waitAsync;

// Polyfill fetch for shell-compatibility and to cache / preload model weights etc.
let preload = { /* Initialized in init() below due to async. */ };

Expand All @@ -29,6 +58,7 @@ async function redirectingFetch(url) {
return {
ok: true,
status: 200,
headers: new Headers(),
arrayBuffer() { return preload[url]; },
async blob() {
return {
Expand All @@ -41,6 +71,7 @@ async function redirectingFetch(url) {

throw new Error(`Unexpected resource requested in benchmark: ${url}`);
};
globalThis.fetch ??= redirectingFetch;

// JetStream benchmark harness. Reuse for two different Transformers.js tasks.
// Assumes `preloadFiles(module)`, `initPipeline(pipelineFromTransformersJs)`,
Expand Down Expand Up @@ -80,6 +111,7 @@ class Benchmark {
env.allowRemoteModels = false;
env.allowLocalModels = true;
env.localModelPath = './workloads/transformersjs/build/models/';
env.fetch = redirectingFetch;

// Always select the Wasm backend, nothing else.
delete env.backends.onnx.webgl;
Expand Down
4 changes: 3 additions & 1 deletion workloads/transformersjs/build.log
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Built on 2025-08-20T13:30:51Z
Built on 2026-10-28T16:40:00Z
Installing Node dependencies...
Transformers.js version: 4.3.2
ONNX Runtime Web version: 1.31.0-dev.20260914-8d85527a0
Download and convert audio input(s)...
Converted 4.25s of audio
from 'jfk.wav', 2 channel(s), 44100 Hz, 16 bit, 176000 samples
Expand Down
6 changes: 5 additions & 1 deletion workloads/transformersjs/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ pushd util/
npm install
popd

TRANSFORMERS_VERSION="$(grep -m1 '"version":' util/node_modules/@huggingface/transformers/package.json | cut -d '"' -f 4)"
ORT_VERSION="$(grep -m1 '"version":' util/node_modules/onnxruntime-web/package.json | cut -d '"' -f 4)"
echo "Transformers.js version: ${TRANSFORMERS_VERSION}" | tee -a "$BUILD_LOG"
echo "ONNX Runtime Web version: ${ORT_VERSION}" | tee -a "$BUILD_LOG"

echo "Download and convert audio input(s)..." | tee -a "$BUILD_LOG"
wget https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/jfk.wav | tee -a "$BUILD_LOG"
# Shorten the audio file to one sentence in the middle, to speed up a single iteration.
Expand All @@ -30,7 +35,6 @@ node util/test-models.mjs
echo "Copy library files into build/..." | tee -a "$BUILD_LOG"

cp util/node_modules/@huggingface/transformers/dist/transformers.js build/
git apply transformers.js.patch

# Transformers.js packages the ONNX runtime JSEP build by default, even when
# only using the Wasm backend, which would be fine with the non-JSEP build.
Expand Down

Large diffs are not rendered by default.

Binary file not shown.
Loading
Loading