From 617da897dd8461a21afe750321bf58befb37043d Mon Sep 17 00:00:00 2001 From: Ronald Tse Date: Thu, 12 Nov 2026 16:47:48 +0800 Subject: [PATCH] fix(ml): reject float16 graph-I/O feeds with the artifact-contract error Issue #111's "not enough space: expected N, got 0" from onnxruntime-node's session.run is float16 graph I/O meeting the binding, not a windowing or size bug: - The issue-era fp16 exports shipped float16 graph boundaries (last_hidden_state / encoder_hidden_states / logits). Python ORT consumes fp16 I/O natively, so the reference decoder and the frozen vectors never saw it (the models-repo WO79 finding). - On runtimes with native Float16Array (Node >= 24), ORT's JS Tensor converts every float16 feed into a Float16Array view whose bytes the N-API binding reads as zero, and run() fails with the opaque error. The issue's numbers decode exactly as seq x d_model x 2 bytes of feed with a 0-byte buffer: fas 288512 = 98 x 1472 (byt5-small) x 2, urd 73728 = 24 x 1536 (byt5-base) x 2. Size-independent because "got" is always the buffer length, not the artifact. - Node 20/22 (no Float16Array) consume the same feed as Uint16Array and decode fine: verified 20.20.2/22.20.0 ok, 24.14.0/26.5.0 broken. The shipped artifacts have been IO-normalized since 2026-10-21 (models-repo WO79 #327 replaced both release zips), so both conformance legs pass today; the runtime had no defense against the next fp16-IO export and CI cannot see it (the tiny fixtures are fp32-I/O). Fix: reject float16 feeds at the onnx-node binding boundary with the artifact-contract error (input name + normalize_fp16_io.py pointer) instead of the opaque binding failure, and type Tensor honestly for what already flows (float16 / Uint16Array). fp32-I/O artifacts never construct a float16 feed, so the guard is a no-op for them; the browser (WASM) backend handles fp16 feeds itself and is untouched. Regression: test/fixtures/tiny-fp16io.zip (~1KB, the pre-WO79 export shape Cast-wrapped from tiny-imf.zip with recomputed member digests) plus test/imf-fp16io.test.ts, which reproduced the opaque binding error before the fix and locks both runtime branches (decode on Node 20/22, explicit rejection on Node 24+). --- src/ml/session/onnx-node.ts | 23 ++++++++++++++ src/ml/types.ts | 12 ++++++-- test/fixtures/tiny-fp16io.zip | Bin 0 -> 1035 bytes test/imf-fp16io.test.ts | 56 ++++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/tiny-fp16io.zip create mode 100644 test/imf-fp16io.test.ts diff --git a/src/ml/session/onnx-node.ts b/src/ml/session/onnx-node.ts index 44f6870..a33b577 100644 --- a/src/ml/session/onnx-node.ts +++ b/src/ml/session/onnx-node.ts @@ -44,6 +44,20 @@ async function loadOrt(): Promise { } } +/** + * float16 feeds cannot cross the onnxruntime-node binding on runtimes + * with native Float16Array (Node >= 24): ORT's JS Tensor converts every + * float16 feed into a Float16Array view, and the N-API binding reads + * that buffer as zero bytes, so session.run fails with the opaque + * "not enough space: expected N, got 0" (issue #111). On pre-Float16Array + * runtimes the same feed stays Uint16Array-backed and is consumed fine + * (verified Node 20/22 ok, Node 24/26 broken). Artifacts with float16 + * graph I/O (the pre-WO79 fp16 exports) must be IO-normalized instead: + * interscript-models tools/normalize_fp16_io.py. + */ +const FLOAT16_FEEDS_CONSUMABLE = + typeof (globalThis as { Float16Array?: unknown }).Float16Array === "undefined" + class NodeInferenceSession implements InferenceSession { private readonly session: OnnxNodeSession private readonly ort: OnnxNodeModule @@ -76,6 +90,15 @@ class NodeInferenceSession implements InferenceSession { async run(inputs: InferenceInputs): Promise { const feeds: Record = {} for (const [name, tensor] of Object.entries(inputs)) { + if (tensor.type === "float16" && !FLOAT16_FEEDS_CONSUMABLE) { + throw new Error( + `input '${name}' is float16: float16 graph I/O cannot cross the onnxruntime-node ` + + `binding on this runtime (the feed is converted to a Float16Array the binding reads ` + + `as zero bytes, surfacing as "not enough space: expected N, got 0"). The artifact's ` + + `graphs must expose fp32 graph I/O — re-export with interscript-models ` + + `tools/normalize_fp16_io.py.`, + ) + } feeds[name] = new this.ort.Tensor(tensor.type, tensor.data, tensor.dims) } const outputs = await this.session.run(feeds) diff --git a/src/ml/types.ts b/src/ml/types.ts index 19459d2..78785df 100644 --- a/src/ml/types.ts +++ b/src/ml/types.ts @@ -27,13 +27,21 @@ export interface ModelRef { * Record of named tensors. */ export type TensorData = - Int8Array | Uint8Array | Int16Array | Int32Array | Float32Array | Float64Array | BigInt64Array + | Int8Array + | Uint8Array + | Int16Array + | Uint16Array + | Int32Array + | Float32Array + | Float64Array + | BigInt64Array export interface Tensor { readonly name: string readonly data: TensorData readonly dims: readonly number[] - readonly type: "int8" | "uint8" | "int16" | "int32" | "int64" | "float32" | "float64" + readonly type: + "int8" | "uint8" | "int16" | "uint16" | "int32" | "int64" | "float32" | "float64" | "float16" } export interface InferenceInputs { diff --git a/test/fixtures/tiny-fp16io.zip b/test/fixtures/tiny-fp16io.zip new file mode 100644 index 0000000000000000000000000000000000000000..d3d7c7a702c942a11d03cb2eda5348bd1d854250 GIT binary patch literal 1035 zcmWIWW@Zs#U|`^2Xl~AleO@Xm%gM;VPyocdK+)XPlEjq6l0?1A#M~U;2^;f{C40b&||6W{MX2rcbkrSUTPCDN?Q4Bewv}CpBcKE@9(7= zu>;wyLjC_vug>l?4Zru%|H^qgkHVFC8#hj~H$3|Oc-6mq8#cW=((=$t_5HNz##?tN zzxR7AlK)ocL>)r_N|*&?`uNWRhLtQZ%y@uwYF=`FN@|f_eqLV1)X^JfN~IXY>|%m^KoX_F&jgyv72Icd@?@%WfY5tF3kC5UH;*2m zSgc1wcWj5L=0+9E=I+profcaSPBJVPE_iw2*`X4d2t)B@N$O&HY>akjet5s?%-_90 zx5q;Lkdlhxhh0aGJUyg3Yu5BR^Cv}#AiFwBN2N8?!p`pO4Uf*FmK%4tbWiSHsS@8* z$fztl-AFt=EzU91vnI{UP~1BxEoq}bxnEsX8fSoWVA4*Z&BWru;Rz6E&6zntVmb%=?gG$}~T@(6M*mE|j*EV5(4yJM*ngt^6=8RXHt>2j*0h!f~db|41PL9UK2 zzOH(?DZybHC(o>Y!UR;$$RxsyJ3|09fk6W>pwO}hx>odb0ny07(786 n214f#pibntLDz}yL4?lxOi11h@MdKLNwEOoGa${+4B`O*F2iP& literal 0 HcmV?d00001 diff --git a/test/imf-fp16io.test.ts b/test/imf-fp16io.test.ts new file mode 100644 index 0000000..a6ed7ac --- /dev/null +++ b/test/imf-fp16io.test.ts @@ -0,0 +1,56 @@ +/** + * The fp16 graph-I/O contract guard (issue #111). The original fp16 + * exports shipped float16 graph I/O (last_hidden_state / + * encoder_hidden_states / logits all float16). Python ORT consumes + * fp16 I/O natively, so the reference decoder and the frozen vectors + * never saw it — but onnxruntime-node cannot: on runtimes with native + * Float16Array (Node >= 24) every float16 feed is converted to a + * Float16Array view whose bytes the N-API binding reads as zero, and + * session.run fails with the opaque "not enough space: expected N, + * got 0". On pre-Float16Array runtimes the same feed stays + * Uint16Array-backed and decodes fine (verified: Node 20/22 ok, Node + * 24/26 broken). + * + * The runtime therefore rejects the unconsumable condition explicitly, + * naming the artifact contract: graphs must expose fp32 I/O + * (interscript-models tools/normalize_fp16_io.py, WO79 — the shipped + * fp16 artifacts have been IO-normalized since 2026-10-21). + * + * The fixture is tiny-imf.zip with float16 wrapped across both graph + * boundaries (Cast nodes, recomputed member digests) — the pre-WO79 + * export shape in ~1KB instead of 700MB. + */ + +import { describe, expect, it } from "vitest" +import { IMFModel } from "../src/ml/imf/index.js" + +const fixtureZip = "test/fixtures/tiny-fp16io.zip" + +/** Node >= 24 ships native Float16Array; ORT converts fp16 feeds to + * Float16Array views the onnxruntime-node binding cannot consume. */ +const float16FeedsUnconsumable = + typeof (globalThis as { Float16Array?: unknown }).Float16Array !== "undefined" + +describe("fp16 graph-I/O artifacts (issue #111)", () => { + it( + float16FeedsUnconsumable + ? "rejects with the artifact-contract error, not the opaque binding failure" + : "decodes on runtimes whose binding consumes Uint16Array-backed fp16 feeds", + async () => { + const model = await IMFModel.load(fixtureZip) + try { + if (float16FeedsUnconsumable) { + await expect(model.translate("rok", 64)).rejects.toThrow( + /float16 graph I\/O.*fp32 graph I\/O.*normalize_fp16_io/s, + ) + } else { + const out = await model.translate("rok", 64) + expect(typeof out).toBe("string") + } + } finally { + await model.dispose() + } + }, + 60_000, + ) +})