Skip to content

Commit b125e82

Browse files
authored
Hot-fix Float16Array usage in Node>=24 (#1482)
Fixes `TypeError: Tensor.data must be a typed array (4) for float16 tensors, but got typed array (0).`
1 parent 2ec882e commit b125e82

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/models.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,19 @@ async function sessionRun(session, inputs) {
435435
const checkedInputs = validateInputs(session, inputs);
436436
try {
437437
// pass the original ort tensor
438-
const ortFeed = Object.fromEntries(Object.entries(checkedInputs).map(([k, v]) => [k, v.ort_tensor]));
438+
const ortFeed = Object.fromEntries(Object.entries(checkedInputs).map(([k, v]) => {
439+
const tensor = /** @type {any} */(v.ort_tensor);
440+
if (apis.IS_NODE_ENV) {
441+
// In recent versions of Node.js, which support Float16Array, we need to convert
442+
// the Float16Array to Uint16Array for ONNX Runtime to accept it.
443+
// NOTE: This will be removed in Transformers.js v4.0.
444+
if (typeof Float16Array !== "undefined" && tensor.cpuData instanceof Float16Array) {
445+
tensor.cpuData = new Uint16Array(tensor.cpuData.buffer); // reinterpret as Uint16Array
446+
}
447+
}
448+
return [k, tensor];
449+
}));
450+
439451
const output = await runInferenceSession(session, ortFeed);
440452
return replaceTensors(output);
441453
} catch (e) {

0 commit comments

Comments
 (0)