Skip to content
Closed
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
2 changes: 1 addition & 1 deletion jest.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export default {
// runner: "jest-runner",

// The paths to modules that run some code to configure or set up the testing environment before each test
// setupFiles: [],
setupFiles: ["./tests/setup.js"],

// A list of paths to modules that run some code to configure or set up the testing framework before each test
// setupFilesAfterEnv: [],
Expand Down
7 changes: 4 additions & 3 deletions src/utils/tensor.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import { TensorOpRegistry } from '../ops/registry.js';

export const DataTypeMap = Object.freeze({
float32: Float32Array,
// @ts-ignore ts(2552) Limited availability of Float16Array across browsers:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float16Array
float16: typeof Float16Array !== "undefined" ? Float16Array: Uint16Array,
// NOTE: onnxruntime-node's native binding expects Uint16Array for float16 tensors.
// Float16Array is not yet supported by onnxruntime-node (see microsoft/onnxruntime#26742).
// Once onnxruntime adds Float16Array support, this can be updated.
float16: Uint16Array,
float64: Float64Array,
string: Array, // string[]
int8: Int8Array,
Expand Down
17 changes: 17 additions & 0 deletions tests/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// WORKAROUND: onnxruntime-node's native binding expects Uint16Array for float16 tensors,
// but onnxruntime-common uses Float16Array when available (Node 20+).
// Hide Float16Array before any onnxruntime imports to force Uint16Array usage.
//
// This file runs before any test imports, ensuring Float16Array is hidden
// when onnxruntime-common's checkTypedArray() runs.
//
// TODO: Remove this workaround once onnxruntime-node adds Float16Array support.
// Track the upstream PR: https://github.com/microsoft/onnxruntime/pull/26742
// When that PR is merged and released, this file can be deleted and the
// setupFiles entry in jest.config.mjs can be removed.

if (typeof globalThis.Float16Array !== 'undefined') {
// Save reference in case other code needs it
globalThis._Float16Array = globalThis.Float16Array;
delete globalThis.Float16Array;
}