From 77c337ea5d5c4d76580c2fa8f65aa6f92165d7ea Mon Sep 17 00:00:00 2001 From: Jason Date: Sat, 21 Mar 2026 23:55:43 +0100 Subject: [PATCH 1/2] fix(onnx): pass external data to session_options when using customCache in Node.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a model uses an external data file (e.g. `model.onnx_data`) and the user supplies a `customCache` (which returns a `Uint8Array` / `Response` rather than a filesystem path), the ONNX runtime in Node.js fails to locate the external data file with an error like: file_size: system cannot find the specified path: "model.onnx_data" Root cause: `session.js` only sets `session_options.externalData` when `\!IS_NODE_ENV`. In Node.js with `useFSCache` this is correct — the ONNX runtime finds the `.onnx_data` files automatically relative to the model path on disk. But when `customCache` is used the data arrives as an in-memory `Uint8Array` (not a file path), so the runtime has no way to discover the sidecar files. Fix: also set `session_options.externalData` in Node.js whenever any of the resolved external-data entries is not a plain string (i.e. it is a `{path, data}` object carrying in-memory bytes from a custom cache). Fixes #1408 --- packages/transformers/src/models/session.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/transformers/src/models/session.js b/packages/transformers/src/models/session.js index b3897892e..ddd8a9ca2 100644 --- a/packages/transformers/src/models/session.js +++ b/packages/transformers/src/models/session.js @@ -115,8 +115,16 @@ async function getSession( session_options, ); - if (externalData.length > 0 && !apis.IS_NODE_ENV) { - session_options.externalData = externalData; + if (externalData.length > 0) { + // In non-Node environments, always pass external data via session options. + // In Node.js with filesystem cache, the ONNX runtime locates external data + // files automatically relative to the model path, so we skip this. + // However, when using a custom cache (which returns Uint8Array instead of a + // file path), the ONNX runtime cannot find the files on disk, so we must + // supply the data in-memory via session_options.externalData. + if (!apis.IS_NODE_ENV || externalData.some(item => typeof item !== 'string')) { + session_options.externalData = externalData; + } } if (cache_config && selectedDevice === 'webgpu' && kv_cache_dtype_config !== false) { From 32371fe6ceac01cfef09e2747705158a94eeb386 Mon Sep 17 00:00:00 2001 From: Jason Date: Wed, 25 Mar 2026 23:40:09 +0100 Subject: [PATCH 2/2] refactor: use instanceof Uint8Array instead of typeof !== 'string' More specific type check as suggested by maintainer. Since custom cache returns Uint8Array (binary data) vs string (file paths), checking for the exact type is clearer and more correct. --- packages/transformers/src/models/session.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/transformers/src/models/session.js b/packages/transformers/src/models/session.js index ddd8a9ca2..5ba11be68 100644 --- a/packages/transformers/src/models/session.js +++ b/packages/transformers/src/models/session.js @@ -122,7 +122,7 @@ async function getSession( // However, when using a custom cache (which returns Uint8Array instead of a // file path), the ONNX runtime cannot find the files on disk, so we must // supply the data in-memory via session_options.externalData. - if (!apis.IS_NODE_ENV || externalData.some(item => typeof item !== 'string')) { + if (!apis.IS_NODE_ENV || externalData.some(item => item instanceof Uint8Array)) { session_options.externalData = externalData; } }