onnxruntime-web
Version:
A Javascript library for running ONNX models on browsers
4 lines • 1.82 MB
Source Map (JSON)
{
"version": 3,
"sources": ["../../common/lib/backend-impl.ts", "../../common/lib/backend.ts", "../../common/lib/version.ts", "../../common/lib/env-impl.ts", "../../common/lib/env.ts", "../../common/lib/tensor-conversion-impl.ts", "../../common/lib/tensor-factory-impl.ts", "../../common/lib/tensor-impl-type-mapping.ts", "../../common/lib/tensor-utils-impl.ts", "../../common/lib/tensor-impl.ts", "../../common/lib/tensor.ts", "../../common/lib/trace.ts", "../../common/lib/inference-session-impl.ts", "../../common/lib/inference-session.ts", "../../common/lib/tensor-conversion.ts", "../../common/lib/tensor-factory.ts", "../../common/lib/onnx-model.ts", "../../common/lib/onnx-value.ts", "../../common/lib/index.ts", "../lib/onnxjs/instrument.ts", "../lib/onnxjs/opset.ts", "../node_modules/guid-typescript/dist/guid.js", "../node_modules/long/index.js", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/arg-type.ts", "../node_modules/flatbuffers/js/constants.js", "../node_modules/flatbuffers/js/utils.js", "../node_modules/flatbuffers/js/encoding.js", "../node_modules/flatbuffers/js/byte-buffer.js", "../node_modules/flatbuffers/js/builder.js", "../node_modules/flatbuffers/js/flatbuffers.js", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/arg-type-and-index.ts", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/attribute-type.ts", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/node-type.ts", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/node.ts", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/edge-end.ts", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/node-edge.ts", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/nodes-to-optimize-indices.ts", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/runtime-optimization-record.ts", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/runtime-optimization-record-container-entry.ts", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/runtime-optimizations.ts", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/tensor-data-type.ts", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/tensor.ts", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/sparse-tensor.ts", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/map-type.ts", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/sequence-type.ts", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/dimension-value-type.ts", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/dimension-value.ts", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/dimension.ts", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/shape.ts", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/tensor-type-and-shape.ts", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/type-info-value.ts", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/type-info.ts", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/value-info.ts", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/graph.ts", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/attribute.ts", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/deprecated-kernel-create-infos.ts", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/deprecated-node-index-and-kernel-def-hash.ts", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/deprecated-sub-graph-session-state.ts", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/deprecated-session-state.ts", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/kernel-type-str-args-entry.ts", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/op-id-kernel-type-str-args-entry.ts", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/kernel-type-str-resolver.ts", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/operator-set-id.ts", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/string-string-entry.ts", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/model.ts", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs/inference-session.ts", "../lib/onnxjs/ort-schema/flatbuffers/onnxruntime/fbs.ts", "../lib/onnxjs/ort-schema/flatbuffers/ort-generated.ts", "../node_modules/@protobufjs/aspromise/index.js", "../node_modules/@protobufjs/base64/index.js", "../node_modules/@protobufjs/eventemitter/index.js", "../node_modules/@protobufjs/float/index.js", "../node_modules/@protobufjs/inquire/index.js", "../node_modules/@protobufjs/utf8/index.js", "../node_modules/@protobufjs/pool/index.js", "../node_modules/protobufjs/src/util/longbits.js", "../node_modules/protobufjs/src/util/minimal.js", "../node_modules/protobufjs/src/writer.js", "../node_modules/protobufjs/src/writer_buffer.js", "../node_modules/protobufjs/src/reader.js", "../node_modules/protobufjs/src/reader_buffer.js", "../node_modules/protobufjs/src/rpc/service.js", "../node_modules/protobufjs/src/rpc.js", "../node_modules/protobufjs/src/roots.js", "../node_modules/protobufjs/src/index-minimal.js", "../node_modules/protobufjs/minimal.js", "../lib/onnxjs/ort-schema/protobuf/onnx.js", "../lib/onnxjs/util.ts", "../lib/onnxjs/tensor.ts", "../lib/onnxjs/backends/webgl/glsl-source.ts", "../lib/onnxjs/backends/webgl/types.ts", "../lib/onnxjs/backends/webgl/utils.ts", "../lib/onnxjs/backends/webgl/ops/packing-utils.ts", "../lib/onnxjs/backends/webgl/ops/pack.ts", "../lib/onnxjs/backends/webgl/ops/reshape-packed.ts", "../lib/onnxjs/backends/webgl/ops/uint8-encode.ts", "../lib/onnxjs/backends/webgl/ops/unpack.ts", "../lib/onnxjs/backends/webgl/texture-data-encoder.ts", "../lib/onnxjs/backends/webgl/texture-layout.ts", "../lib/onnxjs/backends/webgl/inference-handler.ts", "../lib/onnxjs/attribute-with-cache-key.ts", "../lib/onnxjs/backends/webgl/ops/batch-normalization.ts", "../lib/onnxjs/backends/webgl/glsl-definitions.ts", "../lib/onnxjs/backends/webgl/ops/binary-op.ts", "../lib/onnxjs/backends/webgl/ops/cast.ts", "../lib/onnxjs/backends/webgl/ops/concat-packed.ts", "../lib/onnxjs/backends/webgl/ops/concat.ts", "../lib/onnxjs/backends/webgl/ops/unary-op.ts", "../lib/onnxjs/backends/webgl/ops/fuse-utils.ts", "../lib/onnxjs/backends/webgl/ops/conv-grouped.ts", "../lib/onnxjs/backends/webgl/ops/im2col-pack.ts", "../lib/onnxjs/backends/webgl/ops/matmul.ts", "../lib/onnxjs/backends/webgl/ops/matmul-pack.ts", "../lib/onnxjs/backends/webgl/ops/conv-pack.ts", "../lib/onnxjs/backends/webgl/ops/im2col.ts", "../lib/onnxjs/backends/webgl/ops/dot-product.ts", "../lib/onnxjs/backends/webgl/ops/conv.ts", "../lib/onnxjs/backends/webgl/ops/conv-transpose.ts", "../lib/onnxjs/backends/webgl/ops/transpose.ts", "../lib/onnxjs/backends/webgl/ops/depth-to-space.ts", "../lib/onnxjs/backends/webgl/ops/flatten.ts", "../lib/onnxjs/operators.ts", "../lib/onnxjs/backends/webgl/ops/gather.ts", "../lib/onnxjs/backends/webgl/ops/gemm.ts", "../lib/onnxjs/backends/webgl/ops/image-scaler.ts", "../lib/onnxjs/backends/webgl/ops/instance-normalization.ts", "../lib/onnxjs/backends/webgl/ops/lrn.ts", "../lib/onnxjs/backends/webgl/ops/pad.ts", "../lib/onnxjs/backends/webgl/ops/pool.ts", "../lib/onnxjs/backends/webgl/ops/reduce.ts", "../lib/onnxjs/backends/webgl/ops/reshape.ts", "../lib/onnxjs/backends/webgl/ops/upsample.ts", "../lib/onnxjs/backends/webgl/ops/resize-packed.ts", "../lib/onnxjs/backends/webgl/ops/shape.ts", "../lib/onnxjs/backends/webgl/ops/slice.ts", "../lib/onnxjs/backends/webgl/ops/softmax.ts", "../lib/onnxjs/backends/webgl/ops/split.ts", "../lib/onnxjs/backends/webgl/ops/squeeze.ts", "../lib/onnxjs/backends/webgl/ops/sum.ts", "../lib/onnxjs/backends/webgl/ops/tile.ts", "../lib/onnxjs/backends/webgl/ops/unsqueeze.ts", "../lib/onnxjs/backends/webgl/op-resolve-rules.ts", "../lib/onnxjs/backends/webgl/glsl-function-inliner.ts", "../lib/onnxjs/backends/webgl/texture-layout-strategy.ts", "../lib/onnxjs/backends/webgl/glsl-coordinate-lib.ts", "../lib/onnxjs/backends/webgl/glsl-encoding-lib.ts", "../lib/onnxjs/backends/webgl/glsl-fragcolor-lib.ts", "../lib/onnxjs/backends/webgl/glsl-shape-utils-lib.ts", "../lib/onnxjs/backends/webgl/glsl-vec-lib.ts", "../lib/onnxjs/backends/webgl/glsl-registered-libs.ts", "../lib/onnxjs/backends/webgl/glsl-preprocessor.ts", "../lib/onnxjs/backends/webgl/program-manager.ts", "../lib/onnxjs/backends/webgl/texture-manager.ts", "../lib/onnxjs/backends/webgl/session-handler.ts", "../lib/onnxjs/backends/webgl/webgl-context.ts", "../lib/onnxjs/backends/webgl/webgl-context-factory.ts", "../lib/onnxjs/backends/backend-webgl.ts", "../lib/onnxjs/backend.ts", "../lib/onnxjs/execution-plan.ts", "../lib/onnxjs/attribute.ts", "../lib/onnxjs/graph.ts", "../lib/onnxjs/model.ts", "../lib/onnxjs/session.ts", "../lib/onnxjs/session-handler-inference.ts", "../lib/backend-onnxjs.ts", "../lib/index.ts", "../lib/version.ts"],
"sourcesContent": ["// Copyright (c) Microsoft Corporation. All rights reserved.\n// Licensed under the MIT License.\n\nimport { Backend } from './backend.js';\nimport { InferenceSession } from './inference-session.js';\n\ninterface BackendInfo {\n backend: Backend;\n priority: number;\n\n initPromise?: Promise<void>;\n initialized?: boolean;\n aborted?: boolean;\n error?: string;\n}\n\nconst backends: Map<string, BackendInfo> = new Map();\nconst backendsSortedByPriority: string[] = [];\n\n/**\n * Register a backend.\n *\n * @param name - the name as a key to lookup as an execution provider.\n * @param backend - the backend object.\n * @param priority - an integer indicating the priority of the backend. Higher number means higher priority. if priority\n * < 0, it will be considered as a 'beta' version and will not be used as a fallback backend by default.\n *\n * @ignore\n */\nexport const registerBackend = (name: string, backend: Backend, priority: number): void => {\n if (backend && typeof backend.init === 'function' && typeof backend.createInferenceSessionHandler === 'function') {\n const currentBackend = backends.get(name);\n if (currentBackend === undefined) {\n backends.set(name, { backend, priority });\n } else if (currentBackend.priority > priority) {\n // same name is already registered with a higher priority. skip registeration.\n return;\n } else if (currentBackend.priority === priority) {\n if (currentBackend.backend !== backend) {\n throw new Error(`cannot register backend \"${name}\" using priority ${priority}`);\n }\n }\n\n if (priority >= 0) {\n const i = backendsSortedByPriority.indexOf(name);\n if (i !== -1) {\n backendsSortedByPriority.splice(i, 1);\n }\n\n for (let i = 0; i < backendsSortedByPriority.length; i++) {\n if (backends.get(backendsSortedByPriority[i])!.priority <= priority) {\n backendsSortedByPriority.splice(i, 0, name);\n return;\n }\n }\n backendsSortedByPriority.push(name);\n }\n return;\n }\n\n throw new TypeError('not a valid backend');\n};\n\n/**\n * Try to resolve and initialize a backend.\n *\n * @param backendName - the name of the backend.\n * @returns the backend instance if resolved and initialized successfully, or an error message if failed.\n */\nconst tryResolveAndInitializeBackend = async (backendName: string): Promise<Backend | string> => {\n const backendInfo = backends.get(backendName);\n if (!backendInfo) {\n return 'backend not found.';\n }\n\n if (backendInfo.initialized) {\n return backendInfo.backend;\n } else if (backendInfo.aborted) {\n return backendInfo.error!;\n } else {\n const isInitializing = !!backendInfo.initPromise;\n try {\n if (!isInitializing) {\n backendInfo.initPromise = backendInfo.backend.init(backendName);\n }\n await backendInfo.initPromise;\n backendInfo.initialized = true;\n return backendInfo.backend;\n } catch (e) {\n if (!isInitializing) {\n backendInfo.error = `${e}`;\n backendInfo.aborted = true;\n }\n return backendInfo.error!;\n } finally {\n delete backendInfo.initPromise;\n }\n }\n};\n\n/**\n * Resolve execution providers from the specific session options.\n *\n * @param options - the session options object.\n * @returns a promise that resolves to a tuple of an initialized backend instance and a session options object with\n * filtered EP list.\n *\n * @ignore\n */\nexport const resolveBackendAndExecutionProviders = async (\n options: InferenceSession.SessionOptions,\n): Promise<[backend: Backend, options: InferenceSession.SessionOptions]> => {\n // extract backend hints from session options\n const eps = options.executionProviders || [];\n const backendHints = eps.map((i) => (typeof i === 'string' ? i : i.name));\n const backendNames = backendHints.length === 0 ? backendsSortedByPriority : backendHints;\n\n // try to resolve and initialize all requested backends\n let backend: Backend | undefined;\n const errors = [];\n const availableBackendNames = new Set<string>();\n for (const backendName of backendNames) {\n const resolveResult = await tryResolveAndInitializeBackend(backendName);\n if (typeof resolveResult === 'string') {\n errors.push({ name: backendName, err: resolveResult });\n } else {\n if (!backend) {\n backend = resolveResult;\n }\n if (backend === resolveResult) {\n availableBackendNames.add(backendName);\n }\n }\n }\n\n // if no backend is available, throw error.\n if (!backend) {\n throw new Error(`no available backend found. ERR: ${errors.map((e) => `[${e.name}] ${e.err}`).join(', ')}`);\n }\n\n // for each explicitly requested backend, if it's not available, output warning message.\n for (const { name, err } of errors) {\n if (backendHints.includes(name)) {\n // eslint-disable-next-line no-console\n console.warn(\n `removing requested execution provider \"${name}\" from session options because it is not available: ${err}`,\n );\n }\n }\n\n const filteredEps = eps.filter((i) => availableBackendNames.has(typeof i === 'string' ? i : i.name));\n\n return [\n backend,\n new Proxy(options, {\n get: (target, prop) => {\n if (prop === 'executionProviders') {\n return filteredEps;\n }\n return Reflect.get(target, prop);\n },\n }),\n ];\n};\n", "// Copyright (c) Microsoft Corporation. All rights reserved.\n// Licensed under the MIT License.\n\nimport { InferenceSession } from './inference-session.js';\nimport { OnnxValue } from './onnx-value.js';\n\n/**\n * @ignore\n */\nexport declare namespace SessionHandler {\n type FeedsType = { [name: string]: OnnxValue };\n type FetchesType = { [name: string]: OnnxValue | null };\n type ReturnType = { [name: string]: OnnxValue };\n}\n\n/**\n * Represents shared SessionHandler functionality\n *\n * @ignore\n */\ninterface SessionHandler {\n dispose(): Promise<void>;\n\n readonly inputNames: readonly string[];\n readonly outputNames: readonly string[];\n\n readonly inputMetadata: readonly InferenceSession.ValueMetadata[];\n readonly outputMetadata: readonly InferenceSession.ValueMetadata[];\n}\n\n/**\n * Represent a handler instance of an inference session.\n *\n * @ignore\n */\nexport interface InferenceSessionHandler extends SessionHandler {\n startProfiling(): void;\n endProfiling(): void;\n\n run(\n feeds: SessionHandler.FeedsType,\n fetches: SessionHandler.FetchesType,\n options: InferenceSession.RunOptions,\n ): Promise<SessionHandler.ReturnType>;\n}\n\n/**\n * Represent a backend that provides implementation of model inferencing.\n *\n * @ignore\n */\nexport interface Backend {\n /**\n * Initialize the backend asynchronously. Should throw when failed.\n */\n init(backendName: string): Promise<void>;\n\n createInferenceSessionHandler(\n uriOrBuffer: string | Uint8Array,\n options?: InferenceSession.SessionOptions,\n ): Promise<InferenceSessionHandler>;\n}\n\nexport { registerBackend } from './backend-impl.js';\n", "// Copyright (c) Microsoft Corporation. All rights reserved.\n// Licensed under the MIT License.\n\n// This file is generated by /js/scripts/update-version.ts\n// Do not modify file content manually.\n\nexport const version = '1.23.2';\n", "// Copyright (c) Microsoft Corporation. All rights reserved.\n// Licensed under the MIT License.\n\nimport { Env } from './env.js';\nimport { version } from './version.js';\n\ntype LogLevelType = Env['logLevel'];\n\nlet logLevelValue: Required<LogLevelType> = 'warning';\n\nexport const env: Env = {\n wasm: {} as Env.WebAssemblyFlags,\n webgl: {} as Env.WebGLFlags,\n webgpu: {} as Env.WebGpuFlags,\n versions: { common: version },\n\n set logLevel(value: LogLevelType) {\n if (value === undefined) {\n return;\n }\n if (typeof value !== 'string' || ['verbose', 'info', 'warning', 'error', 'fatal'].indexOf(value) === -1) {\n throw new Error(`Unsupported logging level: ${value}`);\n }\n logLevelValue = value;\n },\n get logLevel(): Required<LogLevelType> {\n return logLevelValue;\n },\n};\n\n// set property 'logLevel' so that they can be correctly transferred to worker by `postMessage()`.\nObject.defineProperty(env, 'logLevel', { enumerable: true });\n", "// Copyright (c) Microsoft Corporation. All rights reserved.\n// Licensed under the MIT License.\n\nimport { env as envImpl } from './env-impl.js';\nimport { TryGetGlobalType } from './type-helper.js';\n\nexport declare namespace Env {\n export type WasmPathPrefix = string;\n export interface WasmFilePaths {\n /**\n * Specify the override path for the main .wasm file.\n *\n * This path should be an absolute path.\n *\n * If not modified, the filename of the .wasm file is:\n * - `ort-wasm-simd-threaded.wasm` for default build\n * - `ort-wasm-simd-threaded.jsep.wasm` for JSEP build (with WebGPU and WebNN)\n * - `ort-wasm-simd-threaded.asyncify.wasm` for WebGPU build with Asyncify (with WebNN)\n */\n wasm?: URL | string;\n /**\n * Specify the override path for the main .mjs file.\n *\n * This path should be an absolute path.\n *\n * If not modified, the filename of the .mjs file is:\n * - `ort-wasm-simd-threaded.mjs` for default build\n * - `ort-wasm-simd-threaded.jsep.mjs` for JSEP build (with WebGPU and WebNN)\n * - `ort-wasm-simd-threaded.asyncify.mjs` for WebGPU build with Asyncify (with WebNN)\n */\n mjs?: URL | string;\n }\n export type WasmPrefixOrFilePaths = WasmPathPrefix | WasmFilePaths;\n export interface WebAssemblyFlags {\n /**\n * set or get number of thread(s). If omitted or set to 0, number of thread(s) will be determined by system. If set\n * to 1, no worker thread will be spawned.\n *\n * This setting is available only when WebAssembly multithread feature is available in current context.\n *\n * @defaultValue `0`\n */\n numThreads?: number;\n\n /**\n * set a value indicating whether to enable SIMD.\n *\n * ONNX Runtime will perform feature detection based on the value of this property. Specifically, when the value is\n * set to:\n * - `undefined`, `true` or `\"fixed\"`: will check availability of Fixed-width SIMD.\n * - `\"relaxed\"`: will check availability of Relaxed SIMD.\n * - `false`: will not perform SIMD feature checking.\n *\n * Setting this property does not make ONNX Runtime to switch to the corresponding runtime automatically. User need\n * to set `wasmPaths` or `wasmBinary` property to load the corresponding runtime.\n *\n * This setting is available only when WebAssembly SIMD feature is available in current context.\n *\n * @defaultValue `true`\n */\n simd?: boolean | 'fixed' | 'relaxed';\n\n /**\n * set or get a boolean value indicating whether to enable trace.\n *\n * @defaultValue `false`\n *\n * @deprecated Use `env.trace` instead. If `env.trace` is set, this property will be ignored.\n */\n trace?: boolean;\n\n /**\n * Set or get a number specifying the timeout for initialization of WebAssembly backend, in milliseconds. A zero\n * value indicates no timeout is set.\n *\n * @defaultValue `0`\n */\n initTimeout?: number;\n\n /**\n * Set a custom URL prefix to the .wasm/.mjs files, or an object of overrides for both .wasm/.mjs file. The override\n * path should be an absolute path.\n */\n wasmPaths?: WasmPrefixOrFilePaths;\n\n /**\n * Set a custom buffer which contains the WebAssembly binary. If this property is set, the `wasmPaths` property will\n * be ignored.\n */\n wasmBinary?: ArrayBufferLike | Uint8Array;\n\n /**\n * Set or get a boolean value indicating whether to proxy the execution of main thread to a worker thread.\n *\n * @defaultValue `false`\n */\n proxy?: boolean;\n }\n\n export interface WebGLFlags {\n /**\n * Set or get the WebGL Context ID (webgl or webgl2).\n *\n * @defaultValue `'webgl2'`\n */\n contextId?: 'webgl' | 'webgl2';\n /**\n * Get the WebGL rendering context.\n */\n readonly context: WebGLRenderingContext;\n /**\n * Set or get the maximum batch size for matmul. 0 means to disable batching.\n *\n * @deprecated\n */\n matmulMaxBatchSize?: number;\n /**\n * Set or get the texture cache mode.\n *\n * @defaultValue `'full'`\n */\n textureCacheMode?: 'initializerOnly' | 'full';\n /**\n * Set or get the packed texture mode\n *\n * @defaultValue `false`\n */\n pack?: boolean;\n /**\n * Set or get whether enable async download.\n *\n * @defaultValue `false`\n */\n async?: boolean;\n }\n\n export interface WebGpuProfilingDataV1TensorMetadata {\n dims: readonly number[];\n dataType: string;\n }\n export interface WebGpuProfilingDataV1 {\n version: 1;\n inputsMetadata: readonly WebGpuProfilingDataV1TensorMetadata[];\n outputsMetadata: readonly WebGpuProfilingDataV1TensorMetadata[];\n kernelId: number;\n kernelType: string;\n kernelName: string;\n programName: string;\n startTime: number;\n endTime: number;\n }\n\n export type WebGpuProfilingData = WebGpuProfilingDataV1;\n\n export interface WebGpuFlags {\n /**\n * Set or get the profiling mode.\n *\n * @deprecated Use `env.webgpu.profiling.mode` instead. If `env.webgpu.profiling.mode` is set, this property will be\n * ignored.\n */\n profilingMode?: 'off' | 'default';\n /**\n * Set or get the profiling configuration.\n */\n profiling: {\n /**\n * Set or get the profiling mode.\n *\n * @defaultValue `'off'`\n */\n mode?: 'off' | 'default';\n\n /**\n * Set or get a callback function when a profiling data is received. If not set, the profiling data will be\n * printed to console.\n */\n ondata?: (data: WebGpuProfilingData) => void;\n };\n /**\n * Set or get the power preference.\n *\n * Setting this property only has effect before the first WebGPU inference session is created. The value will be\n * used as options for `navigator.gpu.requestAdapter()`.\n *\n * See {@link https://gpuweb.github.io/gpuweb/#dictdef-gpurequestadapteroptions} for more details.\n *\n * @defaultValue `undefined`\n *\n * @deprecated Create your own GPUAdapter, use it to create a GPUDevice instance and set {@link device} property if\n * you want to use a specific power preference.\n */\n powerPreference?: 'low-power' | 'high-performance';\n /**\n * Set or get the force fallback adapter flag.\n *\n * Setting this property only has effect before the first WebGPU inference session is created. The value will be\n * used as options for `navigator.gpu.requestAdapter()`.\n *\n * See {@link https://gpuweb.github.io/gpuweb/#dictdef-gpurequestadapteroptions} for more details.\n *\n * @defaultValue `undefined`\n *\n * @deprecated Create your own GPUAdapter, use it to create a GPUDevice instance and set {@link device} property if\n * you want to use a specific fallback option.\n */\n forceFallbackAdapter?: boolean;\n /**\n * Set or get the adapter for WebGPU.\n *\n * Setting this property only has effect before the first WebGPU inference session is created. The value will be\n * used as the GPU adapter for the underlying WebGPU backend to create GPU device.\n *\n * If this property is not set, it will be available to get after the first WebGPU inference session is created. The\n * value will be the GPU adapter that created by the underlying WebGPU backend.\n *\n * When use with TypeScript, the type of this property is `GPUAdapter` defined in \"@webgpu/types\".\n *\n * @deprecated It is no longer recommended to use this property. The latest WebGPU spec adds `GPUDevice.adapterInfo`\n * (https://www.w3.org/TR/webgpu/#dom-gpudevice-adapterinfo), which allows to get the adapter information from the\n * device. When it's available, there is no need to set/get the {@link adapter} property.\n */\n adapter: TryGetGlobalType<'GPUAdapter'>;\n /**\n * Set or get the GPU device for WebGPU.\n *\n * There are 3 valid scenarios of accessing this property:\n * - Set a value before the first WebGPU inference session is created. The value will be used by the WebGPU backend\n * to perform calculations. If the value is not a `GPUDevice` object, an error will be thrown.\n * - Get the value before the first WebGPU inference session is created. This will try to create a new GPUDevice\n * instance. Returns a `Promise` that resolves to a `GPUDevice` object.\n * - Get the value after the first WebGPU inference session is created. Returns a resolved `Promise` to the\n * `GPUDevice` object used by the WebGPU backend.\n */\n get device(): Promise<TryGetGlobalType<'GPUDevice'>>;\n set device(value: TryGetGlobalType<'GPUDevice'>);\n /**\n * Set or get whether validate input content.\n *\n * @defaultValue `false`\n */\n validateInputContent?: boolean;\n }\n}\n\nexport interface Env {\n /**\n * set the severity level for logging.\n *\n * @defaultValue `'warning'`\n */\n logLevel?: 'verbose' | 'info' | 'warning' | 'error' | 'fatal';\n\n /**\n * Indicate whether run in debug mode.\n *\n * @defaultValue `false`\n */\n debug?: boolean;\n\n /**\n * set or get a boolean value indicating whether to enable trace.\n *\n * @defaultValue `false`\n */\n trace?: boolean;\n\n /**\n * Get version of the current package.\n */\n readonly versions: {\n readonly common: string;\n readonly web?: string;\n readonly node?: string;\n // eslint-disable-next-line @typescript-eslint/naming-convention\n readonly 'react-native'?: string;\n };\n\n /**\n * Represent a set of flags for WebAssembly\n */\n readonly wasm: Env.WebAssemblyFlags;\n\n /**\n * Represent a set of flags for WebGL\n */\n readonly webgl: Env.WebGLFlags;\n\n /**\n * Represent a set of flags for WebGPU\n */\n readonly webgpu: Env.WebGpuFlags;\n\n [name: string]: unknown;\n}\n\n/**\n * Represent a set of flags as a global singleton.\n */\nexport const env: Env = envImpl;\n", "// Copyright (c) Microsoft Corporation. All rights reserved.\n// Licensed under the MIT License.\n\nimport { TensorToDataUrlOptions, TensorToImageDataOptions } from './tensor-conversion.js';\nimport { Tensor } from './tensor.js';\n\n/**\n * implementation of Tensor.toDataURL()\n */\nexport const tensorToDataURL = (tensor: Tensor, options?: TensorToDataUrlOptions): string => {\n const canvas = typeof document !== 'undefined' ? document.createElement('canvas') : new OffscreenCanvas(1, 1);\n canvas.width = tensor.dims[3];\n canvas.height = tensor.dims[2];\n const pixels2DContext = canvas.getContext('2d') as\n | CanvasRenderingContext2D\n | OffscreenCanvasRenderingContext2D\n | null;\n\n if (pixels2DContext != null) {\n // Default values for height and width & format\n let width: number;\n let height: number;\n if (options?.tensorLayout !== undefined && options.tensorLayout === 'NHWC') {\n width = tensor.dims[2];\n height = tensor.dims[3];\n } else {\n // Default layout is NCWH\n width = tensor.dims[3];\n height = tensor.dims[2];\n }\n\n const inputformat = options?.format !== undefined ? options.format : 'RGB';\n\n const norm = options?.norm;\n let normMean: [number, number, number, number];\n let normBias: [number, number, number, number];\n if (norm === undefined || norm.mean === undefined) {\n normMean = [255, 255, 255, 255];\n } else {\n if (typeof norm.mean === 'number') {\n normMean = [norm.mean, norm.mean, norm.mean, norm.mean];\n } else {\n normMean = [norm.mean[0], norm.mean[1], norm.mean[2], 0];\n if (norm.mean[3] !== undefined) {\n normMean[3] = norm.mean[3];\n }\n }\n }\n if (norm === undefined || norm.bias === undefined) {\n normBias = [0, 0, 0, 0];\n } else {\n if (typeof norm.bias === 'number') {\n normBias = [norm.bias, norm.bias, norm.bias, norm.bias];\n } else {\n normBias = [norm.bias[0], norm.bias[1], norm.bias[2], 0];\n if (norm.bias[3] !== undefined) {\n normBias[3] = norm.bias[3];\n }\n }\n }\n\n const stride = height * width;\n // Default pointer assignments\n let rTensorPointer = 0,\n gTensorPointer = stride,\n bTensorPointer = stride * 2,\n aTensorPointer = -1;\n\n // Updating the pointer assignments based on the input image format\n if (inputformat === 'RGBA') {\n rTensorPointer = 0;\n gTensorPointer = stride;\n bTensorPointer = stride * 2;\n aTensorPointer = stride * 3;\n } else if (inputformat === 'RGB') {\n rTensorPointer = 0;\n gTensorPointer = stride;\n bTensorPointer = stride * 2;\n } else if (inputformat === 'RBG') {\n rTensorPointer = 0;\n bTensorPointer = stride;\n gTensorPointer = stride * 2;\n }\n\n for (let i = 0; i < height; i++) {\n for (let j = 0; j < width; j++) {\n const R = ((tensor.data[rTensorPointer++] as number) - normBias[0]) * normMean[0]; // R value\n const G = ((tensor.data[gTensorPointer++] as number) - normBias[1]) * normMean[1]; // G value\n const B = ((tensor.data[bTensorPointer++] as number) - normBias[2]) * normMean[2]; // B value\n const A = aTensorPointer === -1 ? 255 : ((tensor.data[aTensorPointer++] as number) - normBias[3]) * normMean[3]; // A value\n // eslint-disable-next-line @typescript-eslint/restrict-plus-operands\n pixels2DContext.fillStyle = 'rgba(' + R + ',' + G + ',' + B + ',' + A + ')';\n pixels2DContext.fillRect(j, i, 1, 1);\n }\n }\n if ('toDataURL' in canvas) {\n return canvas.toDataURL();\n } else {\n throw new Error('toDataURL is not supported');\n }\n } else {\n throw new Error('Can not access image data');\n }\n};\n\n/**\n * implementation of Tensor.toImageData()\n */\nexport const tensorToImageData = (tensor: Tensor, options?: TensorToImageDataOptions): ImageData => {\n const pixels2DContext =\n typeof document !== 'undefined'\n ? document.createElement('canvas').getContext('2d')\n : (new OffscreenCanvas(1, 1).getContext('2d') as OffscreenCanvasRenderingContext2D);\n let image: ImageData;\n if (pixels2DContext != null) {\n // Default values for height and width & format\n let width: number;\n let height: number;\n let channels: number;\n if (options?.tensorLayout !== undefined && options.tensorLayout === 'NHWC') {\n width = tensor.dims[2];\n height = tensor.dims[1];\n channels = tensor.dims[3];\n } else {\n // Default layout is NCWH\n width = tensor.dims[3];\n height = tensor.dims[2];\n channels = tensor.dims[1];\n }\n const inputformat = options !== undefined ? (options.format !== undefined ? options.format : 'RGB') : 'RGB';\n\n const norm = options?.norm;\n let normMean: [number, number, number, number];\n let normBias: [number, number, number, number];\n if (norm === undefined || norm.mean === undefined) {\n normMean = [255, 255, 255, 255];\n } else {\n if (typeof norm.mean === 'number') {\n normMean = [norm.mean, norm.mean, norm.mean, norm.mean];\n } else {\n normMean = [norm.mean[0], norm.mean[1], norm.mean[2], 255];\n if (norm.mean[3] !== undefined) {\n normMean[3] = norm.mean[3];\n }\n }\n }\n if (norm === undefined || norm.bias === undefined) {\n normBias = [0, 0, 0, 0];\n } else {\n if (typeof norm.bias === 'number') {\n normBias = [norm.bias, norm.bias, norm.bias, norm.bias];\n } else {\n normBias = [norm.bias[0], norm.bias[1], norm.bias[2], 0];\n if (norm.bias[3] !== undefined) {\n normBias[3] = norm.bias[3];\n }\n }\n }\n\n const stride = height * width;\n if (options !== undefined) {\n if (\n (options.format !== undefined && channels === 4 && options.format !== 'RGBA') ||\n (channels === 3 && options.format !== 'RGB' && options.format !== 'BGR')\n ) {\n throw new Error(\"Tensor format doesn't match input tensor dims\");\n }\n }\n\n // Default pointer assignments\n const step = 4;\n let rImagePointer = 0,\n gImagePointer = 1,\n bImagePointer = 2,\n aImagePointer = 3;\n let rTensorPointer = 0,\n gTensorPointer = stride,\n bTensorPointer = stride * 2,\n aTensorPointer = -1;\n\n // Updating the pointer assignments based on the input image format\n if (inputformat === 'RGBA') {\n rTensorPointer = 0;\n gTensorPointer = stride;\n bTensorPointer = stride * 2;\n aTensorPointer = stride * 3;\n } else if (inputformat === 'RGB') {\n rTensorPointer = 0;\n gTensorPointer = stride;\n bTensorPointer = stride * 2;\n } else if (inputformat === 'RBG') {\n rTensorPointer = 0;\n bTensorPointer = stride;\n gTensorPointer = stride * 2;\n }\n\n image = pixels2DContext.createImageData(width, height);\n\n for (\n let i = 0;\n i < height * width;\n rImagePointer += step, gImagePointer += step, bImagePointer += step, aImagePointer += step, i++\n ) {\n image.data[rImagePointer] = ((tensor.data[rTensorPointer++] as number) - normBias[0]) * normMean[0]; // R value\n image.data[gImagePointer] = ((tensor.data[gTensorPointer++] as number) - normBias[1]) * normMean[1]; // G value\n image.data[bImagePointer] = ((tensor.data[bTensorPointer++] as number) - normBias[2]) * normMean[2]; // B value\n image.data[aImagePointer] =\n aTensorPointer === -1 ? 255 : ((tensor.data[aTensorPointer++] as number) - normBias[3]) * normMean[3]; // A value\n }\n } else {\n throw new Error('Can not access image data');\n }\n return image;\n};\n", "// Copyright (c) Microsoft Corporation. All rights reserved.\n// Licensed under the MIT License.\n\nimport {\n OptionsDimensions,\n OptionsFormat,\n OptionsNormalizationParameters,\n OptionsTensorFormat,\n OptionsTensorLayout,\n TensorFromGpuBufferOptions,\n TensorFromImageBitmapOptions,\n TensorFromImageDataOptions,\n TensorFromImageElementOptions,\n TensorFromMLTensorOptions,\n TensorFromTextureOptions,\n TensorFromUrlOptions,\n} from './tensor-factory.js';\nimport { Tensor } from './tensor-impl.js';\nimport { Tensor as TensorInterface } from './tensor.js';\n\ninterface BufferToTensorOptions\n extends OptionsDimensions,\n OptionsTensorLayout,\n OptionsNormalizationParameters,\n OptionsFormat,\n OptionsTensorFormat {}\n\n/**\n * Create a new tensor object from image object\n *\n * @param buffer - Extracted image buffer data - assuming RGBA format\n * @param imageFormat - input image configuration - required configurations height, width, format\n * @param tensorFormat - output tensor configuration - Default is RGB format\n */\nexport const bufferToTensor = (buffer: Uint8ClampedArray | undefined, options: BufferToTensorOptions): Tensor => {\n if (buffer === undefined) {\n throw new Error('Image buffer must be defined');\n }\n if (options.height === undefined || options.width === undefined) {\n throw new Error('Image height and width must be defined');\n }\n if (options.tensorLayout === 'NHWC') {\n throw new Error('NHWC Tensor layout is not supported yet');\n }\n\n const { height, width } = options;\n\n const norm = options.norm ?? { mean: 255, bias: 0 };\n let normMean: [number, number, number, number];\n let normBias: [number, number, number, number];\n\n if (typeof norm.mean === 'number') {\n normMean = [norm.mean, norm.mean, norm.mean, norm.mean];\n } else {\n normMean = [norm.mean![0], norm.mean![1], norm.mean![2], norm.mean![3] ?? 255];\n }\n\n if (typeof norm.bias === 'number') {\n normBias = [norm.bias, norm.bias, norm.bias, norm.bias];\n } else {\n normBias = [norm.bias![0], norm.bias![1], norm.bias![2], norm.bias![3] ?? 0];\n }\n\n const inputformat = options.format !== undefined ? options.format : 'RGBA';\n // default value is RGBA since imagedata and HTMLImageElement uses it\n\n const outputformat =\n options.tensorFormat !== undefined ? (options.tensorFormat !== undefined ? options.tensorFormat : 'RGB') : 'RGB';\n const stride = height * width;\n const float32Data = outputformat === 'RGBA' ? new Float32Array(stride * 4) : new Float32Array(stride * 3);\n\n // Default pointer assignments\n let step = 4,\n rImagePointer = 0,\n gImagePointer = 1,\n bImagePointer = 2,\n aImagePointer = 3;\n let rTensorPointer = 0,\n gTensorPointer = stride,\n bTensorPointer = stride * 2,\n aTensorPointer = -1;\n\n // Updating the pointer assignments based on the input image format\n if (inputformat === 'RGB') {\n step = 3;\n rImagePointer = 0;\n gImagePointer = 1;\n bImagePointer = 2;\n aImagePointer = -1;\n }\n\n // Updating the pointer assignments based on the output tensor format\n if (outputformat === 'RGBA') {\n aTensorPointer = stride * 3;\n } else if (outputformat === 'RBG') {\n rTensorPointer = 0;\n bTensorPointer = stride;\n gTensorPointer = stride * 2;\n } else if (outputformat === 'BGR') {\n bTensorPointer = 0;\n gTensorPointer = stride;\n rTensorPointer = stride * 2;\n }\n\n for (\n let i = 0;\n i < stride;\n i++, rImagePointer += step, bImagePointer += step, gImagePointer += step, aImagePointer += step\n ) {\n float32Data[rTensorPointer++] = (buffer[rImagePointer] + normBias[0]) / normMean[0];\n float32Data[gTensorPointer++] = (buffer[gImagePointer] + normBias[1]) / normMean[1];\n float32Data[bTensorPointer++] = (buffer[bImagePointer] + normBias[2]) / normMean[2];\n if (aTensorPointer !== -1 && aImagePointer !== -1) {\n float32Data[aTensorPointer++] = (buffer[aImagePointer] + normBias[3]) / normMean[3];\n }\n }\n\n // Float32Array -> ort.Tensor\n const outputTensor =\n outputformat === 'RGBA'\n ? new Tensor('float32', float32Data, [1, 4, height, width])\n : new Tensor('float32', float32Data, [1, 3, height, width]);\n return outputTensor;\n};\n\n/**\n * implementation of Tensor.fromImage().\n */\nexport const tensorFromImage = async (\n image: ImageData | HTMLImageElement | ImageBitmap | string,\n options?:\n | TensorFromImageDataOptions\n | TensorFromImageElementOptions\n | TensorFromImageBitmapOptions\n | TensorFromUrlOptions,\n): Promise<Tensor> => {\n // checking the type of image object\n const isHTMLImageEle = typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement;\n const isImageDataEle = typeof ImageData !== 'undefined' && image instanceof ImageData;\n const isImageBitmap = typeof ImageBitmap !== 'undefined' && image instanceof ImageBitmap;\n const isString = typeof image === 'string';\n\n let data: Uint8ClampedArray | undefined;\n let bufferToTensorOptions: BufferToTensorOptions = options ?? {};\n\n const createCanvas = () => {\n if (typeof document !== 'undefined') {\n return document.createElement('canvas');\n } else if (typeof OffscreenCanvas !== 'undefined') {\n return new OffscreenCanvas(1, 1);\n } else {\n throw new Error('Canvas is not supported');\n }\n };\n const createCanvasContext = (canvas: HTMLCanvasElement | OffscreenCanvas) => {\n if (typeof HTMLCanvasElement !== 'undefined' && canvas instanceof HTMLCanvasElement) {\n return canvas.getContext('2d');\n } else if (canvas instanceof OffscreenCanvas) {\n return canvas.getContext('2d') as OffscreenCanvasRenderingContext2D;\n } else {\n return null;\n }\n };\n // filling and checking image configuration options\n if (isHTMLImageEle) {\n // HTMLImageElement - image object - format is RGBA by default\n const canvas = createCanvas();\n canvas.width = image.width;\n canvas.height = image.height;\n const pixels2DContext = createCanvasContext(canvas);\n\n if (pixels2DContext != null) {\n let height = image.height;\n let width = image.width;\n if (options !== undefined && options.resizedHeight !== undefined && options.resizedWidth !== undefined) {\n height = options.resizedHeight;\n width = options.resizedWidth;\n }\n\n if (options !== undefined) {\n bufferToTensorOptions = options;\n if (options.tensorFormat !== undefined) {\n throw new Error('Image input config format must be RGBA for HTMLImageElement');\n } else {\n bufferToTensorOptions.tensorFormat = 'RGBA';\n }\n bufferToTensorOptions.height = height;\n bufferToTensorOptions.width = width;\n } else {\n bufferToTensorOptions.tensorFormat = 'RGBA';\n bufferToTensorOptions.height = height;\n bufferToTensorOptions.width = width;\n }\n\n pixels2DContext.drawImage(image, 0, 0);\n data = pixels2DContext.getImageData(0, 0, width, height).data;\n } else {\n throw new Error('Can not access image data');\n }\n } else if (isImageDataEle) {\n let height: number;\n let width: number;\n\n if (options !== undefined && options.resizedWidth !== undefined && options.resizedHeight !== undefined) {\n height = options.resizedHeight;\n width = options.resizedWidth;\n } else {\n height = image.height;\n width = image.width;\n }\n\n if (options !== undefined) {\n bufferToTensorOptions = options;\n }\n bufferToTensorOptions.format = 'RGBA';\n bufferToTensorOptions.height = height;\n bufferToTensorOptions.width = width;\n\n if (options !== undefined) {\n const tempCanvas = createCanvas();\n\n tempCanvas.width = width;\n tempCanvas.height = height;\n\n const pixels2DContext = createCanvasContext(tempCanvas);\n\n if (pixels2DContext != null) {\n pixels2DContext.putImageData(image, 0, 0);\n data = pixels2DContext.getImageData(0, 0, width, height).data;\n } else {\n throw new Error('Can not access image data');\n }\n } else {\n data = image.data;\n }\n } else if (isImageBitmap) {\n // ImageBitmap - image object - format must be provided by user\n if (options === undefined) {\n throw new Error('Please provide image config with format for Imagebitmap');\n }\n\n const canvas = createCanvas();\n canvas.width = image.width;\n canvas.height = image.height;\n const pixels2DContext = createCanvasContext(canvas);\n\n if (pixels2DContext != null) {\n const height = image.height;\n const width = image.width;\n pixels2DContext.drawImage(image, 0, 0, width, height);\n data = pixels2DContext.getImageData(0, 0, width, height).data;\n bufferToTensorOptions.height = height;\n bufferToTensorOptions.width = width;\n return bufferToTensor(data, bufferToTensorOptions);\n } else {\n throw new Error('Can not access image data');\n }\n } else if (isString) {\n return new Promise((resolve, reject) => {\n const canvas = createCanvas();\n const context = createCanvasContext(canvas);\n if (!image || !context) {\n return reject();\n }\n const newImage = new Image();\n newImage.crossOrigin = 'Anonymous';\n newImage.src = image;\n newImage.onload = () => {\n canvas.width = newImage.width;\n canvas.height = newImage.height;\n context.drawImage(newImage, 0, 0, canvas.width, canvas.height);\n const img = context.getImageData(0, 0, canvas.width, canvas.height);\n\n bufferToTensorOptions.height = canvas.height;\n bufferToTensorOptions.width = canvas.width;\n resolve(bufferToTensor(img.data, bufferToTensorOptions));\n };\n });\n } else {\n throw new Error('Input data provided is not supported - aborted tensor creation');\n }\n\n if (data !== undefined) {\n return bufferToTensor(data, bufferToTensorOptions);\n } else {\n throw new Error('Input data provided is not supported - aborted tensor creation');\n }\n};\n\n/**\n * implementation of Tensor.fromTexture().\n */\nexport const tensorFromTexture = <T extends TensorInterface.TextureDataTypes>(\n texture: TensorInterface.TextureType,\n options: TensorFromTextureOptions<T>,\n): Tensor => {\n const { width, height, download, dispose } = options;\n // Always assume RGBAF32. TODO: support different texture format\n const dims = [1, height, width, 4];\n return new Tensor({ location: 'texture', type: 'float32', texture, dims, download, dispose });\n};\n\n/**\n * implementation of Tensor.fromGpuBuffer().\n */\nexport const tensorFromGpuBuffer = <T extends TensorInterface.GpuBufferDataTypes>(\n gpuBuffer: TensorInterface.GpuBufferType,\n options: TensorFromGpuBufferOptions<T>,\n): Tensor => {\n const { dataType, dims, download, dispose } = options;\n return new Tensor({ location: 'gpu-buffer', type: dataType ?? 'float32', gpuBuffer, dims, download, dispose });\n};\n\n/**\n * implementation of Tensor.fromMLTensor().\n */\nexport const tensorFromMLTensor = <T extends TensorInterface.MLTensorDataTypes>(\n mlTensor: TensorInterface.MLTensorType,\n options: TensorFromMLTensorOptions<T>,\n): Tensor => {\n const { dataType, dims, download, dispose } = options;\n return new Tensor({ location: 'ml-tensor', type: dataType ?? 'float32', mlTensor, dims, download, dispose });\n};\n\n/**\n * implementation of Tensor.fromPinnedBuffer().\n */\nexport const tensorFromPinnedBuffer = <T extends TensorInterface.CpuPinnedDataTypes>(\n type: T,\n buffer: TensorInterface.DataTypeMap[T],\n dims?: readonly number[],\n): Tensor => new Tensor({ location: 'cpu-pinned', type, data: buffer, dims: dims ?? [buffer.length] });\n", "// Copyright (c) Microsoft Corporation. All rights reserved.\n// Licensed under the MIT License.\n\nimport { Tensor } from './tensor.js';\n\nexport type SupportedTypedArrayConstructors =\n | Float32ArrayConstructor\n | Uint8ArrayConstructor\n | Int8ArrayConstructor\n | Uint16ArrayConstructor\n | Int16ArrayConstructor\n | Int32ArrayConstructor\n | BigInt64ArrayConstructor\n | Uint8ArrayConstructor\n | Float64ArrayConstructor\n | Uint32ArrayConstructor\n | BigUint64ArrayConstructor;\nexport type SupportedTypedArray = InstanceType<SupportedTypedArrayConstructors>;\n\n// a runtime map that maps type string to TypedArray constructor. Should match Tensor.DataTypeMap.\nexport const NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP = new Map<string, SupportedTypedArrayConstructors>([\n ['float32', Float32Array],\n ['uint8', Uint8Array],\n ['int8', Int8Array],\n ['uint16', Uint16Array],\n ['int16', Int16Array],\n ['int32', Int32Array],\n ['bool', Uint8Array],\n ['float64', Float64Array],\n ['uint32', Uint32Array],\n ['int4', Uint8Array],\n ['uint4', Uint8Array],\n]);\n\n// a runtime map that maps type string to TypedArray constructor. Should match Tensor.DataTypeMap.\nexport const NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP = new Map<SupportedTypedArrayConstructors, Tensor.Type>([\n [Float32Array, 'float32'],\n [Uint8Array, 'uint8'],\n [Int8Array, 'int8'],\n [Uint16Array, 'uint16'],\n [Int16Array, 'int16'],\n [Int32Array, 'int32'],\n [Float64Array, 'float64'],\n [Uint32Array, 'uint32'],\n]);\n\n// the following code allows delaying execution of BigInt/Float16Array checking. This allows lazy initialization for\n// NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP and NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP, which allows BigInt/Float16Array\n// polyfill if available.\nlet isTypedArrayChecked = false;\nexport const checkTypedArray = () => {\n if (!isTypedArrayChecked) {\n isTypedArrayChecked = true;\n const isBigInt64ArrayAvailable = typeof BigInt64Array !== 'undefined' && BigInt64Array.from;\n const isBigUint64ArrayAvailable = typeof BigUint64Array !== 'undefined' && BigUint64Array.from;\n\n // eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-explicit-any\n const Float16Array = (globalThis as any).Float16Array;\n const isFloat16ArrayAvailable = typeof Float16Array !== 'undefined' && Float16Array.from;\n\n if (isBigInt64ArrayAvailable) {\n NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set('int64', BigInt64Array);\n NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.set(BigInt64Array, 'int64');\n }\n if (isBigUint64ArrayAvailable) {\n NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set('uint64', BigUint64Array);\n NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.set(BigUint64Array, 'uint64');\n }\n if (isFloat16ArrayAvailable) {\n NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set('float16', Float16Array);\n NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.set(Float16Array, 'f