MCPcopy
hub / github.com/huggingface/transformers.js / init

Function init

packages/transformers/tests/init.js:15–59  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

13 * For more information, see: https://github.com/jestjs/jest/issues/11864#issuecomment-1261468011
14 */
15export function init() {
16 // In rare cases (specifically when running unit tests with GitHub actions), possibly due to
17 // a large number of concurrent executions, onnxruntime might fallback to use the WASM backend.
18 // In this case, we set the number of threads to 1 to avoid errors like:
19 // - `TypeError: The worker script or module filename must be an absolute path or a relative path starting with './' or '../'. Received "blob:nodedata:..."`
20 ONNX_COMMON.env.wasm.numThreads = 1;
21
22 let registerBackend = ONNX_COMMON.registerBackend;
23
24 // Define the constructors to monkey-patch
25 const TYPED_ARRAYS_CONSTRUCTOR_NAMES = ["Int8Array", "Int16Array", "Int32Array", "BigInt64Array", "Uint8Array", "Uint8ClampedArray", "Uint16Array", "Uint32Array", "BigUint64Array", "Float16Array", "Float32Array", "Float64Array"];
26
27 // Keep a reference to the original initialization method
28 const originalMethod = onnxruntimeBackend.init;
29
30 // Monkey-patch the initialization function
31 onnxruntimeBackend.init = function (...args) {
32 // There is probably a better way to do this
33 Array.isArray = (x) => typeof x === "object" && x !== null && typeof x.length === "number" && x?.constructor.toString() === Array.toString();
34
35 // For each typed array constructor
36 for (const ctorName of TYPED_ARRAYS_CONSTRUCTOR_NAMES) {
37 // Get the constructor from the current context
38 const ctor = globalThis[ctorName];
39 if (ctor === undefined) continue; // If unavailable, skip the patching
40
41 // Get the corresponding test function from the `util` module
42 const value = types[`is${ctorName}`].bind(types);
43
44 // Monkey-patch the constructor so "x instanceof ctor" returns "types[`is${ctorName}`](x)"
45 Object.defineProperty(ctor, Symbol.hasInstance, {
46 value,
47 writable: true, // writable=true is necessary to overwrite the default implementation (and allow subsequent overwrites)
48 configurable: false,
49 enumerable: false,
50 });
51 }
52
53 // Call the original method
54 return originalMethod.apply(this, args);
55 };
56
57 // Register the backend with the highest priority, so it is used instead of the default one
58 registerBackend("test", onnxruntimeBackend, Number.POSITIVE_INFINITY);
59}
60
61export const MAX_TOKENIZER_LOAD_TIME = 32_000; // 32 seconds
62export const MAX_FEATURE_EXTRACTOR_LOAD_TIME = 10_000; // 10 seconds

Callers 14

pipelines.test.jsFile · 0.90
models.test.jsFile · 0.90
processors.test.jsFile · 0.90
audio.test.jsFile · 0.90
tensor.test.jsFile · 0.90
maths.test.jsFile · 0.90
tensor_ops.test.jsFile · 0.90
generation.test.jsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected