// Latency + parity of the SAME onnxruntime-web build the browser uses (1.31.0-dev, WASM/CPU) running in Node. // transformers.js' Node build can't hand external-data buffers to onnxruntime-web, so sessions are created directly // with ORT-web; preprocessing/post-processing still use the transformers.js processor. Compares the chosen mask with // the onnxruntime-node path (transformers.js) for the 3 examples. // Usage: node scripts/evidence-ortweb.mjs [threads] [outDir] import * as ort from 'onnxruntime-web'; import { AutoProcessor, RawImage, Tensor, Sam3TrackerModel } from '@huggingface/transformers'; import { readFileSync, writeFileSync, mkdirSync } from 'node:fs'; import { MODEL_ID, MODEL_REVISION, VARIANTS, EXAMPLES, ORT_WEB_VERSION } from '../src/config.js'; import { buildPrompt, bestMaskIndex, sliceMask, maskIoU, describeMask } from '../src/lib.js'; import { encodeImage, decodePrompt } from '../src/sam.js'; import { hardware } from './hw.mjs'; const variant = process.argv[2] || 'q4f16', threads = +(process.argv[3] || 4); const out = process.argv[4] || '/workspace/evidence/rotoscopia-lab'; mkdirSync(out, { recursive: true }); ort.env.wasm.numThreads = threads; ort.env.wasm.wasmPaths = new URL('../node_modules/onnxruntime-web/dist/', import.meta.url).href; const root = new URL('../', import.meta.url).pathname; const cache = root + `node_modules/@huggingface/transformers/.cache/${MODEL_ID}/${MODEL_REVISION}/`; const base = `https://huggingface.co/${MODEL_ID}/resolve/${MODEL_REVISION}/`; async function file(rel) { // use the transformers.js cache if present, else download (pinned revision) try { return readFileSync(cache + rel); } catch { const b = Buffer.from(await (await fetch(base + rel)).arrayBuffer()); mkdirSync((cache + rel).replace(/\/[^/]+$/, ''), { recursive: true }); writeFileSync(cache + rel, b); return b; } } const files = Object.keys(VARIANTS[variant].files); const pick = (n) => files.filter((f) => f.includes(n)); async function session(name) { const [onnx, data] = pick(name).sort(); const t = performance.now(); const s = await ort.InferenceSession.create(await file(onnx), { externalData: [{ path: data.split('/').pop(), data: await file(data) }] }); return [s, performance.now() - t]; } const [enc, encLoad] = await session('vision_encoder'); const [dec, decLoad] = await session('prompt_encoder_mask_decoder'); const processor = await AutoProcessor.from_pretrained(MODEL_ID, { revision: MODEL_REVISION }); const toOrt = (t) => new ort.Tensor(t.type, t.data, t.dims); const report = { createdAt: new Date().toISOString(), kind: 'node-ortweb-wasm', onnxruntimeWeb: ort.env.versions.web, expected: ORT_WEB_VERSION, variant, requestedThreads: threads, effectiveThreads: ort.env.wasm.numThreads, sessionLoadMs: { encoder: Math.round(encLoad), decoder: Math.round(decLoad) }, hardware: hardware(), examples: [] }; // reference path (onnxruntime-node via transformers.js) for parity const ref = await Sam3TrackerModel.from_pretrained(MODEL_ID, { revision: MODEL_REVISION, dtype: VARIANTS[variant].dtype, device: 'cpu' }); for (const ex of EXAMPLES) { const image = await RawImage.read(root + 'public/examples/' + ex.file); const inputs = await processor(image, buildPrompt(ex.points, ex.box)); let t = performance.now(); const e = await enc.run({ pixel_values: toOrt(inputs.pixel_values) }); const encodeMs = performance.now() - t; const feeds = { 'image_embeddings.0': e['image_embeddings.0'], 'image_embeddings.1': e['image_embeddings.1'], 'image_embeddings.2': e['image_embeddings.2'] }; if (inputs.input_points) { feeds.input_points = toOrt(inputs.input_points); feeds.input_labels = toOrt(inputs.input_labels); } else { feeds.input_points = new ort.Tensor('float32', new Float32Array(0), [1, 1, 0, 2]); feeds.input_labels = new ort.Tensor('int64', new BigInt64Array(0), [1, 1, 0]); } feeds.input_boxes = inputs.input_boxes ? toOrt(inputs.input_boxes) : new ort.Tensor('float32', new Float32Array(0), [1, 0, 4]); t = performance.now(); const o = await dec.run(feeds); const decodeMs = performance.now() - t; const pm = new Tensor('float32', o.pred_masks.data, o.pred_masks.dims); const masks = await processor.post_process_masks(pm, inputs.original_sizes, inputs.reshaped_input_sizes); const [, K, H, W] = masks[0].dims; const scores = Array.from(o.iou_scores.data).slice(0, K), k = bestMaskIndex(scores); const mWeb = sliceMask(masks[0].data, k, H, W); const encR = await encodeImage(ref, processor, image); const rNode = await decodePrompt(ref, processor, image, encR, ex.points, ex.box); const row = { id: ex.id, encodeMs: Math.round(encodeMs), decodeMs: Math.round(decodeMs), scores: scores.map((s) => +s.toFixed(4)), chosen: k, area: describeMask(mWeb, W, H).area, bbox: describeMask(mWeb, W, H).bbox, maskIoUvsOrtNode: +maskIoU(mWeb, rNode.mask).toFixed(4) }; console.log(JSON.stringify(row)); report.examples.push(row); } writeFileSync(`${out}/ortweb-wasm-${variant}-${threads}t.json`, JSON.stringify(report, null, 2)); console.log('effective threads', ort.env.wasm.numThreads);