docs: add quixzoom-auth-core product to AAMOS

- Product documentation in docs/products/
- Updated MEMORY.md with product info
- quiXzoom Auth Core as AAMOS Identity product
This commit is contained in:
Bernt
2026-07-14 09:58:53 +00:00
parent 58ca4e68db
commit 48ea61cdcc
2730 changed files with 293827 additions and 7213 deletions
+13
View File
@@ -0,0 +1,13 @@
# ONNX Runtime JavaScript API
ONNX Runtime JavaScript API is a unified API for all JavaScript usages. It's dependency of the following NPM packages:
- onnxruntime-node
- onnxruntime-web
- onnxruntime-react-native
This package (onnxruntime-common) is not designed for using directly. Please consider to install one of the NPM packages above according to target platform.
## License
License information can be found [here](https://github.com/microsoft/onnxruntime/blob/main/README.md#license).
@@ -0,0 +1,24 @@
import { Backend } from './backend.js';
import { InferenceSession } from './inference-session.js';
/**
* Register a backend.
*
* @param name - the name as a key to lookup as an execution provider.
* @param backend - the backend object.
* @param priority - an integer indicating the priority of the backend. Higher number means higher priority. if priority
* < 0, it will be considered as a 'beta' version and will not be used as a fallback backend by default.
*
* @ignore
*/
export declare const registerBackend: (name: string, backend: Backend, priority: number) => void;
/**
* Resolve execution providers from the specific session options.
*
* @param options - the session options object.
* @returns a promise that resolves to a tuple of an initialized backend instance and a session options object with
* filtered EP list.
*
* @ignore
*/
export declare const resolveBackendAndExecutionProviders: (options: InferenceSession.SessionOptions) => Promise<[backend: Backend, options: InferenceSession.SessionOptions]>;
//# sourceMappingURL=backend-impl.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"backend-impl.d.ts","sourceRoot":"","sources":["../../lib/backend-impl.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAe1D;;;;;;;;;GASG;AACH,eAAO,MAAM,eAAe,GAAI,MAAM,MAAM,EAAE,SAAS,OAAO,EAAE,UAAU,MAAM,KAAG,IAgClF,CAAC;AAuCF;;;;;;;;GAQG;AACH,eAAO,MAAM,mCAAmC,GAC9C,SAAS,gBAAgB,CAAC,cAAc,KACvC,OAAO,CAAC,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,gBAAgB,CAAC,cAAc,CAAC,CAoDtE,CAAC"}
+147
View File
@@ -0,0 +1,147 @@
"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveBackendAndExecutionProviders = exports.registerBackend = void 0;
const backends = new Map();
const backendsSortedByPriority = [];
/**
* Register a backend.
*
* @param name - the name as a key to lookup as an execution provider.
* @param backend - the backend object.
* @param priority - an integer indicating the priority of the backend. Higher number means higher priority. if priority
* < 0, it will be considered as a 'beta' version and will not be used as a fallback backend by default.
*
* @ignore
*/
const registerBackend = (name, backend, priority) => {
if (backend && typeof backend.init === 'function' && typeof backend.createInferenceSessionHandler === 'function') {
const currentBackend = backends.get(name);
if (currentBackend === undefined) {
backends.set(name, { backend, priority });
}
else if (currentBackend.priority > priority) {
// same name is already registered with a higher priority. skip registeration.
return;
}
else if (currentBackend.priority === priority) {
if (currentBackend.backend !== backend) {
throw new Error(`cannot register backend "${name}" using priority ${priority}`);
}
}
if (priority >= 0) {
const i = backendsSortedByPriority.indexOf(name);
if (i !== -1) {
backendsSortedByPriority.splice(i, 1);
}
for (let i = 0; i < backendsSortedByPriority.length; i++) {
if (backends.get(backendsSortedByPriority[i]).priority <= priority) {
backendsSortedByPriority.splice(i, 0, name);
return;
}
}
backendsSortedByPriority.push(name);
}
return;
}
throw new TypeError('not a valid backend');
};
exports.registerBackend = registerBackend;
/**
* Try to resolve and initialize a backend.
*
* @param backendName - the name of the backend.
* @returns the backend instance if resolved and initialized successfully, or an error message if failed.
*/
const tryResolveAndInitializeBackend = async (backendName) => {
const backendInfo = backends.get(backendName);
if (!backendInfo) {
return 'backend not found.';
}
if (backendInfo.initialized) {
return backendInfo.backend;
}
else if (backendInfo.aborted) {
return backendInfo.error;
}
else {
const isInitializing = !!backendInfo.initPromise;
try {
if (!isInitializing) {
backendInfo.initPromise = backendInfo.backend.init(backendName);
}
await backendInfo.initPromise;
backendInfo.initialized = true;
return backendInfo.backend;
}
catch (e) {
if (!isInitializing) {
backendInfo.error = `${e}`;
backendInfo.aborted = true;
}
return backendInfo.error;
}
finally {
delete backendInfo.initPromise;
}
}
};
/**
* Resolve execution providers from the specific session options.
*
* @param options - the session options object.
* @returns a promise that resolves to a tuple of an initialized backend instance and a session options object with
* filtered EP list.
*
* @ignore
*/
const resolveBackendAndExecutionProviders = async (options) => {
// extract backend hints from session options
const eps = options.executionProviders || [];
const backendHints = eps.map((i) => (typeof i === 'string' ? i : i.name));
const backendNames = backendHints.length === 0 ? backendsSortedByPriority : backendHints;
// try to resolve and initialize all requested backends
let backend;
const errors = [];
const availableBackendNames = new Set();
for (const backendName of backendNames) {
const resolveResult = await tryResolveAndInitializeBackend(backendName);
if (typeof resolveResult === 'string') {
errors.push({ name: backendName, err: resolveResult });
}
else {
if (!backend) {
backend = resolveResult;
}
if (backend === resolveResult) {
availableBackendNames.add(backendName);
}
}
}
// if no backend is available, throw error.
if (!backend) {
throw new Error(`no available backend found. ERR: ${errors.map((e) => `[${e.name}] ${e.err}`).join(', ')}`);
}
// for each explicitly requested backend, if it's not available, output warning message.
for (const { name, err } of errors) {
if (backendHints.includes(name)) {
// eslint-disable-next-line no-console
console.warn(`removing requested execution provider "${name}" from session options because it is not available: ${err}`);
}
}
const filteredEps = eps.filter((i) => availableBackendNames.has(typeof i === 'string' ? i : i.name));
return [
backend,
new Proxy(options, {
get: (target, prop) => {
if (prop === 'executionProviders') {
return filteredEps;
}
return Reflect.get(target, prop);
},
}),
];
};
exports.resolveBackendAndExecutionProviders = resolveBackendAndExecutionProviders;
//# sourceMappingURL=backend-impl.js.map
@@ -0,0 +1 @@
{"version":3,"file":"backend-impl.js","sourceRoot":"","sources":["../../lib/backend-impl.ts"],"names":[],"mappings":";AAAA,4DAA4D;AAC5D,kCAAkC;;;AAelC,MAAM,QA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2C;IAC3C,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CAAC,oCAAoC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAC9G,CAAC;IAED,wFAAwF;IACxF,KAAK,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,MAAM,EAAE,CAAC;QACnC,IAAI,YAAY,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;YAChC,sCAAsC;YACtC,OAAO,CAAC,IAAI,CACV,0CAA0C,IAAI,uDAAuD,GAAG,EAAE,CAC3G,CAAC;QACJ,CAAC;IACH,CAAC;IAED,MAAM,WAAW,GAAG,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,qBAAqB,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;IAErG,OAAO;QACL,OAAO;QACP,IAAI,KAAK,CAAC,OAAO,EAAE;YACjB,GAAG,EAAE,CAAC,MAAM,EAAE,IAAI,EAAE,EAAE;gBACpB,IAAI,IAAI,KAAK,oBAAoB,EAAE,CAAC;oBAClC,OAAO,WAAW,CAAC;gBACrB,CAAC;gBACD,OAAO,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;YACnC,CAAC;SACF,CAAC;KACH,CAAC;AACJ,CAAC,CAAC;AAtDW,QAAA,mCAAmC,uCAsD9C"}
+52
View File
@@ -0,0 +1,52 @@
import { InferenceSession } from './inference-session.js';
import { OnnxValue } from './onnx-value.js';
/**
* @ignore
*/
export declare namespace SessionHandler {
type FeedsType = {
[name: string]: OnnxValue;
};
type FetchesType = {
[name: string]: OnnxValue | null;
};
type ReturnType = {
[name: string]: OnnxValue;
};
}
/**
* Represents shared SessionHandler functionality
*
* @ignore
*/
interface SessionHandler {
dispose(): Promise<void>;
readonly inputNames: readonly string[];
readonly outputNames: readonly string[];
readonly inputMetadata: readonly InferenceSession.ValueMetadata[];
readonly outputMetadata: readonly InferenceSession.ValueMetadata[];
}
/**
* Represent a handler instance of an inference session.
*
* @ignore
*/
export interface InferenceSessionHandler extends SessionHandler {
startProfiling(): void;
endProfiling(): void;
run(feeds: SessionHandler.FeedsType, fetches: SessionHandler.FetchesType, options: InferenceSession.RunOptions): Promise<SessionHandler.ReturnType>;
}
/**
* Represent a backend that provides implementation of model inferencing.
*
* @ignore
*/
export interface Backend {
/**
* Initialize the backend asynchronously. Should throw when failed.
*/
init(backendName: string): Promise<void>;
createInferenceSessionHandler(uriOrBuffer: string | Uint8Array, options?: InferenceSession.SessionOptions): Promise<InferenceSessionHandler>;
}
export { registerBackend } from './backend-impl.js';
//# sourceMappingURL=backend.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"backend.d.ts","sourceRoot":"","sources":["../../lib/backend.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C;;GAEG;AACH,MAAM,CAAC,OAAO,WAAW,cAAc,CAAC;IACtC,KAAK,SAAS,GAAG;QAAE,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,CAAA;KAAE,CAAC;IAC/C,KAAK,WAAW,GAAG;QAAE,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,CAAA;KAAE,CAAC;IACxD,KAAK,UAAU,GAAG;QAAE,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,CAAA;KAAE,CAAC;CACjD;AAED;;;;GAIG;AACH,UAAU,cAAc;IACtB,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAEzB,QAAQ,CAAC,UAAU,EAAE,SAAS,MAAM,EAAE,CAAC;IACvC,QAAQ,CAAC,WAAW,EAAE,SAAS,MAAM,EAAE,CAAC;IAExC,QAAQ,CAAC,aAAa,EAAE,SAAS,gBAAgB,CAAC,aAAa,EAAE,CAAC;IAClE,QAAQ,CAAC,cAAc,EAAE,SAAS,gBAAgB,CAAC,aAAa,EAAE,CAAC;CACpE;AAED;;;;GAIG;AACH,MAAM,WAAW,uBAAwB,SAAQ,cAAc;IAC7D,cAAc,IAAI,IAAI,CAAC;IACvB,YAAY,IAAI,IAAI,CAAC;IAErB,GAAG,CACD,KAAK,EAAE,cAAc,CAAC,SAAS,EAC/B,OAAO,EAAE,cAAc,CAAC,WAAW,EACnC,OAAO,EAAE,gBAAgB,CAAC,UAAU,GACnC,OAAO,CAAC,cAAc,CAAC,UAAU,CAAC,CAAC;CACvC;AAED;;;;GAIG;AACH,MAAM,WAAW,OAAO;IACtB;;OAEG;IACH,IAAI,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAEzC,6BAA6B,CAC3B,WAAW,EAAE,MAAM,GAAG,UAAU,EAChC,OAAO,CAAC,EAAE,gBAAgB,CAAC,cAAc,GACxC,OAAO,CAAC,uBAAuB,CAAC,CAAC;CACrC;AAED,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC"}
+8
View File
@@ -0,0 +1,8 @@
"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerBackend = void 0;
var backend_impl_js_1 = require("./backend-impl.js");
Object.defineProperty(exports, "registerBackend", { enumerable: true, get: function () { return backend_impl_js_1.registerBackend; } });
//# sourceMappingURL=backend.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"backend.js","sourceRoot":"","sources":["../../lib/backend.ts"],"names":[],"mappings":";AAAA,4DAA4D;AAC5D,kCAAkC;;;AA8DlC,qDAAoD;AAA3C,kHAAA,eAAe,OAAA"}
+3
View File
@@ -0,0 +1,3 @@
import { Env } from './env.js';
export declare const env: Env;
//# sourceMappingURL=env-impl.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"env-impl.d.ts","sourceRoot":"","sources":["../../lib/env-impl.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAO/B,eAAO,MAAM,GAAG,EAAE,GAkBjB,CAAC"}
+28
View File
@@ -0,0 +1,28 @@
"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.env = void 0;
const version_js_1 = require("./version.js");
let logLevelValue = 'warning';
exports.env = {
wasm: {},
webgl: {},
webgpu: {},
versions: { common: version_js_1.version },
set logLevel(value) {
if (value === undefined) {
return;
}
if (typeof value !== 'string' || ['verbose', 'info', 'warning', 'error', 'fatal'].indexOf(value) === -1) {
throw new Error(`Unsupported logging level: ${value}`);
}
logLevelValue = value;
},
get logLevel() {
return logLevelValue;
},
};
// set property 'logLevel' so that they can be correctly transferred to worker by `postMessage()`.
Object.defineProperty(exports.env, 'logLevel', { enumerable: true });
//# sourceMappingURL=env-impl.js.map
@@ -0,0 +1 @@
{"version":3,"file":"env-impl.js","sourceRoot":"","sources":["../../lib/env-impl.ts"],"names":[],"mappings":";AAAA,4DAA4D;AAC5D,kCAAkC;;;AAGlC,6CAAuC;AAIvC,IAAI,aAAa,GAA2B,SAAS,CAAC;AAEzC,QAAA,GAAG,GAAQ;IACtB,IAAI,EAAE,EAAE;IACR,KAAK,EAAE,EAAoB;IAC3B,MAAM,EAAE,EAAqB;IAC7B,QAAQ,EAAE,EAAE,MAAM,EAAE,oBAAO,EAAE;IAE7B,IAAI,QAAQ,CAAC,KAAmB;QAC9B,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;YACxB,OAAO;QACT,CAAC;QACD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC;YACxG,MAAM,IAAI,KAAK,CAAC,8BAA8B,KAAK,EAAE,CAAC,CAAC;QACzD,CAAC;QACD,aAAa,GAAG,KAAK,CAAC;IACxB,CAAC;IACD,IAAI,QAAQ;QACV,OAAO,aAAa,CAAC;IACvB,CAAC;CACF,CAAC;AAEF,kGAAkG;AAClG,MAAM,CAAC,cAAc,CAAC,WAAG,EAAE,UAAU,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC,CAAC"}
+277
View File
@@ -0,0 +1,277 @@
import { TryGetGlobalType } from './type-helper.js';
export declare namespace Env {
type WasmPathPrefix = string;
interface WasmFilePaths {
/**
* Specify the override path for the main .wasm file.
*
* This path should be an absolute path.
*
* If not modified, the filename of the .wasm file is:
* - `ort-wasm-simd-threaded.wasm` for default build
* - `ort-wasm-simd-threaded.jsep.wasm` for JSEP build (with WebGPU and WebNN)
* - `ort-wasm-simd-threaded.asyncify.wasm` for WebGPU build with Asyncify (with WebNN)
* - `ort-wasm-simd-threaded.jspi.wasm` for WebGPU build with JSPI support (with WebNN)
*/
wasm?: URL | string;
/**
* Specify the override path for the main .mjs file.
*
* This path should be an absolute path.
*
* If not modified, the filename of the .mjs file is:
* - `ort-wasm-simd-threaded.mjs` for default build
* - `ort-wasm-simd-threaded.jsep.mjs` for JSEP build (with WebGPU and WebNN)
* - `ort-wasm-simd-threaded.asyncify.mjs` for WebGPU build with Asyncify (with WebNN)
* - `ort-wasm-simd-threaded.jspi.mjs` for WebGPU build with JSPI support (with WebNN)
*/
mjs?: URL | string;
}
type WasmPrefixOrFilePaths = WasmPathPrefix | WasmFilePaths;
interface WebAssemblyFlags {
/**
* set or get number of thread(s). If omitted or set to 0, number of thread(s) will be determined by system. If set
* to 1, no worker thread will be spawned.
*
* This setting is available only when WebAssembly multithread feature is available in current context.
*
* @defaultValue `0`
*/
numThreads?: number;
/**
* set a value indicating whether to enable SIMD.
*
* ONNX Runtime will perform feature detection based on the value of this property. Specifically, when the value is
* set to:
* - `undefined`, `true` or `"fixed"`: will check availability of Fixed-width SIMD.
* - `"relaxed"`: will check availability of Relaxed SIMD.
* - `false`: will not perform SIMD feature checking.
*
* Setting this property does not make ONNX Runtime to switch to the corresponding runtime automatically. User need
* to set `wasmPaths` or `wasmBinary` property to load the corresponding runtime.
*
* This setting is available only when WebAssembly SIMD feature is available in current context.
*
* @defaultValue `true`
*/
simd?: boolean | 'fixed' | 'relaxed';
/**
* set or get a boolean value indicating whether to enable trace.
*
* @defaultValue `false`
*
* @deprecated Use `env.trace` instead. If `env.trace` is set, this property will be ignored.
*/
trace?: boolean;
/**
* Set or get a number specifying the timeout for initialization of WebAssembly backend, in milliseconds. A zero
* value indicates no timeout is set.
*
* @defaultValue `0`
*/
initTimeout?: number;
/**
* Set a custom URL prefix to the .wasm/.mjs files, or an object of overrides for both .wasm/.mjs file. The override
* path should be an absolute path.
*/
wasmPaths?: WasmPrefixOrFilePaths;
/**
* Set a custom buffer which contains the WebAssembly binary. If this property is set, the `wasmPaths` property will
* be ignored.
*/
wasmBinary?: ArrayBufferLike | Uint8Array;
/**
* Set or get a boolean value indicating whether to proxy the execution of main thread to a worker thread.
*
* @defaultValue `false`
*/
proxy?: boolean;
}
interface WebGLFlags {
/**
* Set or get the WebGL Context ID (webgl or webgl2).
*
* @defaultValue `'webgl2'`
*/
contextId?: 'webgl' | 'webgl2';
/**
* Get the WebGL rendering context.
*/
readonly context: WebGLRenderingContext;
/**
* Set or get the maximum batch size for matmul. 0 means to disable batching.
*
* @deprecated
*/
matmulMaxBatchSize?: number;
/**
* Set or get the texture cache mode.
*
* @defaultValue `'full'`
*/
textureCacheMode?: 'initializerOnly' | 'full';
/**
* Set or get the packed texture mode
*
* @defaultValue `false`
*/
pack?: boolean;
/**
* Set or get whether enable async download.
*
* @defaultValue `false`
*/
async?: boolean;
}
interface WebGpuProfilingDataV1TensorMetadata {
dims: readonly number[];
dataType: string;
}
interface WebGpuProfilingDataV1 {
version: 1;
inputsMetadata: readonly WebGpuProfilingDataV1TensorMetadata[];
outputsMetadata: readonly WebGpuProfilingDataV1TensorMetadata[];
kernelId: number;
kernelType: string;
kernelName: string;
programName: string;
startTime: number;
endTime: number;
}
type WebGpuProfilingData = WebGpuProfilingDataV1;
interface WebGpuFlags {
/**
* Set or get the profiling mode.
*
* @deprecated Use `env.webgpu.profiling.mode` instead. If `env.webgpu.profiling.mode` is set, this property will be
* ignored.
*/
profilingMode?: 'off' | 'default';
/**
* Set or get the profiling configuration.
*/
profiling: {
/**
* Set or get the profiling mode.
*
* @defaultValue `'off'`
*/
mode?: 'off' | 'default';
/**
* Set or get a callback function when a profiling data is received. If not set, the profiling data will be
* printed to console.
*/
ondata?: (data: WebGpuProfilingData) => void;
};
/**
* Set or get the power preference.
*
* Setting this property only has effect before the first WebGPU inference session is created. The value will be
* used as options for `navigator.gpu.requestAdapter()`.
*
* See {@link https://gpuweb.github.io/gpuweb/#dictdef-gpurequestadapteroptions} for more details.
*
* @defaultValue `undefined`
*
* @deprecated Create your own GPUAdapter, use it to create a GPUDevice instance and set {@link device} property if
* you want to use a specific power preference.
*/
powerPreference?: 'low-power' | 'high-performance';
/**
* Set or get the force fallback adapter flag.
*
* Setting this property only has effect before the first WebGPU inference session is created. The value will be
* used as options for `navigator.gpu.requestAdapter()`.
*
* See {@link https://gpuweb.github.io/gpuweb/#dictdef-gpurequestadapteroptions} for more details.
*
* @defaultValue `undefined`
*
* @deprecated Create your own GPUAdapter, use it to create a GPUDevice instance and set {@link device} property if
* you want to use a specific fallback option.
*/
forceFallbackAdapter?: boolean;
/**
* Set or get the adapter for WebGPU.
*
* Setting this property only has effect before the first WebGPU inference session is created. The value will be
* used as the GPU adapter for the underlying WebGPU backend to create GPU device.
*
* If this property is not set, it will be available to get after the first WebGPU inference session is created. The
* value will be the GPU adapter that created by the underlying WebGPU backend.
*
* When use with TypeScript, the type of this property is `GPUAdapter` defined in "@webgpu/types".
*
* @deprecated It is no longer recommended to use this property. The latest WebGPU spec adds `GPUDevice.adapterInfo`
* (https://www.w3.org/TR/webgpu/#dom-gpudevice-adapterinfo), which allows to get the adapter information from the
* device. When it's available, there is no need to set/get the {@link adapter} property.
*/
adapter: TryGetGlobalType<'GPUAdapter'>;
/**
* Set or get the GPU device for WebGPU.
*
* There are 3 valid scenarios of accessing this property:
* - Set a value before the first WebGPU inference session is created. The value will be used by the WebGPU backend
* to perform calculations. If the value is not a `GPUDevice` object, an error will be thrown.
* - Get the value before the first WebGPU inference session is created. This will try to create a new GPUDevice
* instance. Returns a `Promise` that resolves to a `GPUDevice` object.
* - Get the value after the first WebGPU inference session is created. Returns a resolved `Promise` to the
* `GPUDevice` object used by the WebGPU backend.
*/
get device(): Promise<TryGetGlobalType<'GPUDevice'>>;
set device(value: TryGetGlobalType<'GPUDevice'>);
/**
* Set or get whether validate input content.
*
* @defaultValue `false`
*/
validateInputContent?: boolean;
}
}
export interface Env {
/**
* set the severity level for logging.
*
* @defaultValue `'warning'`
*/
logLevel?: 'verbose' | 'info' | 'warning' | 'error' | 'fatal';
/**
* Indicate whether run in debug mode.
*
* @defaultValue `false`
*/
debug?: boolean;
/**
* set or get a boolean value indicating whether to enable trace.
*
* @defaultValue `false`
*/
trace?: boolean;
/**
* Get version of the current package.
*/
readonly versions: {
readonly common: string;
readonly web?: string;
readonly node?: string;
readonly 'react-native'?: string;
};
/**
* Represent a set of flags for WebAssembly
*/
readonly wasm: Env.WebAssemblyFlags;
/**
* Represent a set of flags for WebGL
*/
readonly webgl: Env.WebGLFlags;
/**
* Represent a set of flags for WebGPU
*/
readonly webgpu: Env.WebGpuFlags;
[name: string]: unknown;
}
/**
* Represent a set of flags as a global singleton.
*/
export declare const env: Env;
//# sourceMappingURL=env.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"env.d.ts","sourceRoot":"","sources":["../../lib/env.ts"],"names":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AAO,CAAC;IAE9D;;;;OAIG;IACH,KAAK,CAAC,EAAE,OAAO,CAAC;IAEhB;;;;OAIG;IACH,KAAK,CAAC,EAAE,OAAO,CAAC;IAEhB;;OAEG;IACH,QAAQ,CAAC,QAAQ,EAAE;QACjB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;QACxB,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC;QACtB,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;QAEvB,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,CAAC;KAClC,CAAC;IAEF;;OAEG;IACH,QAAQ,CAAC,IAAI,EAAE,GAAG,CAAC,gBAAgB,CAAC;IAEpC;;OAEG;IACH,QAAQ,CAAC,KAAK,EAAE,GAAG,CAAC,UAAU,CAAC;IAE/B;;OAEG;IACH,QAAQ,CAAC,MAAM,EAAE,GAAG,CAAC,WAAW,CAAC;IAEjC,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;CACzB;AAED;;GAEG;AACH,eAAO,MAAM,GAAG,EAAE,GAAa,CAAC"}
+11
View File
@@ -0,0 +1,11 @@
"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.env = void 0;
const env_impl_js_1 = require("./env-impl.js");
/**
* Represent a set of flags as a global singleton.
*/
exports.env = env_impl_js_1.env;
//# sourceMappingURL=env.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"env.js","sourceRoot":"","sources":["../../lib/env.ts"],"names":[],"mappings":";AAAA,4DAA4D;AAC5D,kCAAkC;;;AAElC,+CAA+C;AAuS/C;;GAEG;AACU,QAAA,GAAG,GAAQ,iBAAO,CAAC"}
+25
View File
@@ -0,0 +1,25 @@
/**
* # ONNX Runtime JavaScript API
*
* ONNX Runtime JavaScript API is a unified API for all JavaScript usages, including the following NPM packages:
*
* - [onnxruntime-node](https://www.npmjs.com/package/onnxruntime-node)
* - [onnxruntime-web](https://www.npmjs.com/package/onnxruntime-web)
* - [onnxruntime-react-native](https://www.npmjs.com/package/onnxruntime-react-native)
*
* See also:
* - [Get Started](https://onnxruntime.ai/docs/get-started/with-javascript/)
* - [Inference examples](https://github.com/microsoft/onnxruntime-inference-examples/tree/main/js)
*
* @packageDocumentation
*/
export * from './backend.js';
export * from './env.js';
export * from './inference-session.js';
export * from './tensor.js';
export * from './tensor-conversion.js';
export * from './tensor-factory.js';
export * from './trace.js';
export * from './onnx-model.js';
export * from './onnx-value.js';
//# sourceMappingURL=index.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/index.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;GAcG;AAEH,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC;AACzB,cAAc,wBAAwB,CAAC;AACvC,cAAc,aAAa,CAAC;AAC5B,cAAc,wBAAwB,CAAC;AACvC,cAAc,qBAAqB,CAAC;AACpC,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,cAAc,iBAAiB,CAAC"}
+43
View File
@@ -0,0 +1,43 @@
"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
* # ONNX Runtime JavaScript API
*
* ONNX Runtime JavaScript API is a unified API for all JavaScript usages, including the following NPM packages:
*
* - [onnxruntime-node](https://www.npmjs.com/package/onnxruntime-node)
* - [onnxruntime-web](https://www.npmjs.com/package/onnxruntime-web)
* - [onnxruntime-react-native](https://www.npmjs.com/package/onnxruntime-react-native)
*
* See also:
* - [Get Started](https://onnxruntime.ai/docs/get-started/with-javascript/)
* - [Inference examples](https://github.com/microsoft/onnxruntime-inference-examples/tree/main/js)
*
* @packageDocumentation
*/
__exportStar(require("./backend.js"), exports);
__exportStar(require("./env.js"), exports);
__exportStar(require("./inference-session.js"), exports);
__exportStar(require("./tensor.js"), exports);
__exportStar(require("./tensor-conversion.js"), exports);
__exportStar(require("./tensor-factory.js"), exports);
__exportStar(require("./trace.js"), exports);
__exportStar(require("./onnx-model.js"), exports);
__exportStar(require("./onnx-value.js"), exports);
//# sourceMappingURL=index.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/index.ts"],"names":[],"mappings":";AAAA,4DAA4D;AAC5D,kCAAkC;;;;;;;;;;;;;;;;AAElC;;;;;;;;;;;;;;GAcG;AAEH,+CAA6B;AAC7B,2CAAyB;AACzB,yDAAuC;AACvC,8CAA4B;AAC5B,yDAAuC;AACvC,sDAAoC;AACpC,6CAA2B;AAC3B,kDAAgC;AAChC,kDAAgC"}
@@ -0,0 +1,25 @@
import { InferenceSession as InferenceSessionInterface } from './inference-session.js';
type SessionOptions = InferenceSessionInterface.SessionOptions;
type RunOptions = InferenceSessionInterface.RunOptions;
type FeedsType = InferenceSessionInterface.FeedsType;
type FetchesType = InferenceSessionInterface.FetchesType;
type ReturnType = InferenceSessionInterface.ReturnType;
export declare class InferenceSession implements InferenceSessionInterface {
private constructor();
run(feeds: FeedsType, options?: RunOptions): Promise<ReturnType>;
run(feeds: FeedsType, fetches: FetchesType, options?: RunOptions): Promise<ReturnType>;
release(): Promise<void>;
static create(path: string, options?: SessionOptions): Promise<InferenceSessionInterface>;
static create(buffer: ArrayBufferLike, options?: SessionOptions): Promise<InferenceSessionInterface>;
static create(buffer: ArrayBufferLike, byteOffset: number, byteLength?: number, options?: SessionOptions): Promise<InferenceSessionInterface>;
static create(buffer: Uint8Array, options?: SessionOptions): Promise<InferenceSessionInterface>;
startProfiling(): void;
endProfiling(): void;
get inputNames(): readonly string[];
get outputNames(): readonly string[];
get inputMetadata(): readonly InferenceSessionInterface.ValueMetadata[];
get outputMetadata(): readonly InferenceSessionInterface.ValueMetadata[];
private handler;
}
export {};
//# sourceMappingURL=inference-session-impl.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"inference-session-impl.d.ts","sourceRoot":"","sources":["../../lib/inference-session-impl.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,gBAAgB,IAAI,yBAAyB,EAAE,MAAM,wBAAwB,CAAC;AAKvF,KAAK,cAAc,GAAG,yBAAyB,CAAC,cAAc,CAAC;AAC/D,KAAK,UAAU,GAAG,yBAAyB,CAAC,UAAU,CAAC;AACvD,KAAK,SAAS,GAAG,yBAAyB,CAAC,SAAS,CAAC;AACrD,KAAK,WAAW,GAAG,yBAAyB,CAAC,WAAW,CAAC;AACzD,KAAK,UAAU,GAAG,yBAAyB,CAAC,UAAU,CAAC;AAEvD,qBAAa,gBAAiB,YAAW,yBAAyB;IAChE,OAAO;IAGP,GAAG,CAAC,KAAK,EAAE,SAAS,EAAE,OAAO,CAAC,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;IAChE,GAAG,CAAC,KAAK,EAAE,SAAS,EAAE,OAAO,EAAE,WAAW,EAAE,OAAO,CAAC,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;IA2GhF,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAI9B,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,yBAAyB,CAAC;IACzF,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,eAAe,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,yBAAyB,CAAC;IACpG,MAAM,CAAC,MAAM,CACX,MAAM,EAAE,eAAe,EACvB,UAAU,EAAE,MAAM,EAClB,UAAU,CAAC,EAAE,MAAM,EACnB,OAAO,CAAC,EAAE,cAAc,GACvB,OAAO,CAAC,yBAAyB,CAAC;IACrC,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,UAAU,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,yBAAyB,CAAC;IA6E/F,cAAc,IAAI,IAAI;IAGtB,YAAY,IAAI,IAAI;IAIpB,IAAI,UAAU,IAAI,SAAS,MAAM,EAAE,CAElC;IACD,IAAI,WAAW,IAAI,SAAS,MAAM,EAAE,CAEnC;IAED,IAAI,aAAa,IAAI,SAAS,yBAAyB,CAAC,aAAa,EAAE,CAEtE;IAED,IAAI,cAAc,IAAI,SAAS,yBAAyB,CAAC,aAAa,EAAE,CAEvE;IAED,OAAO,CAAC,OAAO,CAA0B;CAC1C"}
@@ -0,0 +1,212 @@
"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.InferenceSession = void 0;
const backend_impl_js_1 = require("./backend-impl.js");
const tensor_js_1 = require("./tensor.js");
const trace_js_1 = require("./trace.js");
class InferenceSession {
constructor(handler) {
this.handler = handler;
}
async run(feeds, arg1, arg2) {
(0, trace_js_1.TRACE_FUNC_BEGIN)();
(0, trace_js_1.TRACE_EVENT_BEGIN)('InferenceSession.run');
const fetches = {};
let options = {};
// check inputs
if (typeof feeds !== 'object' || feeds === null || feeds instanceof tensor_js_1.Tensor || Array.isArray(feeds)) {
throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values.");
}
let isFetchesEmpty = true;
// determine which override is being used
if (typeof arg1 === 'object') {
if (arg1 === null) {
throw new TypeError('Unexpected argument[1]: cannot be null.');
}
if (arg1 instanceof tensor_js_1.Tensor) {
throw new TypeError("'fetches' cannot be a Tensor");
}
if (Array.isArray(arg1)) {
if (arg1.length === 0) {
throw new TypeError("'fetches' cannot be an empty array.");
}
isFetchesEmpty = false;
// output names
for (const name of arg1) {
if (typeof name !== 'string') {
throw new TypeError("'fetches' must be a string array or an object.");
}
if (this.outputNames.indexOf(name) === -1) {
throw new RangeError(`'fetches' contains invalid output name: ${name}.`);
}
fetches[name] = null;
}
if (typeof arg2 === 'object' && arg2 !== null) {
options = arg2;
}
else if (typeof arg2 !== 'undefined') {
throw new TypeError("'options' must be an object.");
}
}
else {
// decide whether arg1 is fetches or options
// if any output name is present and its value is valid OnnxValue, we consider it fetches
let isFetches = false;
const arg1Keys = Object.getOwnPropertyNames(arg1);
for (const name of this.outputNames) {
if (arg1Keys.indexOf(name) !== -1) {
const v = arg1[name];
if (v === null || v instanceof tensor_js_1.Tensor) {
isFetches = true;
isFetchesEmpty = false;
fetches[name] = v;
}
}
}
if (isFetches) {
if (typeof arg2 === 'object' && arg2 !== null) {
options = arg2;
}
else if (typeof arg2 !== 'undefined') {
throw new TypeError("'options' must be an object.");
}
}
else {
options = arg1;
}
}
}
else if (typeof arg1 !== 'undefined') {
throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'.");
}
// check if all inputs are in feed
for (const name of this.inputNames) {
if (typeof feeds[name] === 'undefined') {
throw new Error(`input '${name}' is missing in 'feeds'.`);
}
}
// if no fetches is specified, we use the full output names list
if (isFetchesEmpty) {
for (const name of this.outputNames) {
fetches[name] = null;
}
}
// feeds, fetches and options are prepared
const results = await this.handler.run(feeds, fetches, options);
const returnValue = {};
for (const key in results) {
if (Object.hasOwnProperty.call(results, key)) {
const result = results[key];
if (result instanceof tensor_js_1.Tensor) {
returnValue[key] = result;
}
else {
returnValue[key] = new tensor_js_1.Tensor(result.type, result.data, result.dims);
}
}
}
(0, trace_js_1.TRACE_EVENT_END)('InferenceSession.run');
(0, trace_js_1.TRACE_FUNC_END)();
return returnValue;
}
async release() {
return this.handler.dispose();
}
static async create(arg0, arg1, arg2, arg3) {
(0, trace_js_1.TRACE_FUNC_BEGIN)();
(0, trace_js_1.TRACE_EVENT_BEGIN)('InferenceSession.create');
// either load from a file or buffer
let filePathOrUint8Array;
let options = {};
if (typeof arg0 === 'string') {
filePathOrUint8Array = arg0;
if (typeof arg1 === 'object' && arg1 !== null) {
options = arg1;
}
else if (typeof arg1 !== 'undefined') {
throw new TypeError("'options' must be an object.");
}
}
else if (arg0 instanceof Uint8Array) {
filePathOrUint8Array = arg0;
if (typeof arg1 === 'object' && arg1 !== null) {
options = arg1;
}
else if (typeof arg1 !== 'undefined') {
throw new TypeError("'options' must be an object.");
}
}
else if (arg0 instanceof ArrayBuffer ||
(typeof SharedArrayBuffer !== 'undefined' && arg0 instanceof SharedArrayBuffer)) {
const buffer = arg0;
let byteOffset = 0;
let byteLength = arg0.byteLength;
if (typeof arg1 === 'object' && arg1 !== null) {
options = arg1;
}
else if (typeof arg1 === 'number') {
byteOffset = arg1;
if (!Number.isSafeInteger(byteOffset)) {
throw new RangeError("'byteOffset' must be an integer.");
}
if (byteOffset < 0 || byteOffset >= buffer.byteLength) {
throw new RangeError(`'byteOffset' is out of range [0, ${buffer.byteLength}).`);
}
byteLength = arg0.byteLength - byteOffset;
if (typeof arg2 === 'number') {
byteLength = arg2;
if (!Number.isSafeInteger(byteLength)) {
throw new RangeError("'byteLength' must be an integer.");
}
if (byteLength <= 0 || byteOffset + byteLength > buffer.byteLength) {
throw new RangeError(`'byteLength' is out of range (0, ${buffer.byteLength - byteOffset}].`);
}
if (typeof arg3 === 'object' && arg3 !== null) {
options = arg3;
}
else if (typeof arg3 !== 'undefined') {
throw new TypeError("'options' must be an object.");
}
}
else if (typeof arg2 !== 'undefined') {
throw new TypeError("'byteLength' must be a number.");
}
}
else if (typeof arg1 !== 'undefined') {
throw new TypeError("'options' must be an object.");
}
filePathOrUint8Array = new Uint8Array(buffer, byteOffset, byteLength);
}
else {
throw new TypeError("Unexpected argument[0]: must be 'path' or 'buffer'.");
}
// resolve backend, update session options with validated EPs, and create session handler
const [backend, optionsWithValidatedEPs] = await (0, backend_impl_js_1.resolveBackendAndExecutionProviders)(options);
const handler = await backend.createInferenceSessionHandler(filePathOrUint8Array, optionsWithValidatedEPs);
(0, trace_js_1.TRACE_EVENT_END)('InferenceSession.create');
(0, trace_js_1.TRACE_FUNC_END)();
return new InferenceSession(handler);
}
startProfiling() {
this.handler.startProfiling();
}
endProfiling() {
this.handler.endProfiling();
}
get inputNames() {
return this.handler.inputNames;
}
get outputNames() {
return this.handler.outputNames;
}
get inputMetadata() {
return this.handler.inputMetadata;
}
get outputMetadata() {
return this.handler.outputMetadata;
}
}
exports.InferenceSession = InferenceSession;
//# sourceMappingURL=inference-session-impl.js.map
File diff suppressed because one or more lines are too long
@@ -0,0 +1,526 @@
import { OnnxModelOptions } from './onnx-model.js';
import { OnnxValue, OnnxValueDataLocation } from './onnx-value.js';
import type { Tensor } from './tensor.js';
import { TryGetGlobalType } from './type-helper.js';
export declare namespace InferenceSession {
type OnnxValueMapType = {
readonly [name: string]: OnnxValue;
};
type NullableOnnxValueMapType = {
readonly [name: string]: OnnxValue | null;
};
/**
* A feeds (model inputs) is an object that uses input names as keys and OnnxValue as corresponding values.
*/
type FeedsType = OnnxValueMapType;
/**
* A fetches (model outputs) could be one of the following:
*
* - Omitted. Use model's output names definition.
* - An array of string indicating the output names.
* - An object that use output names as keys and OnnxValue or null as corresponding values.
*
* @remarks
* different from input argument, in output, OnnxValue is optional. If an OnnxValue is present it will be
* used as a pre-allocated value by the inference engine; if omitted, inference engine will allocate buffer
* internally.
*/
type FetchesType = readonly string[] | NullableOnnxValueMapType;
/**
* A inferencing return type is an object that uses output names as keys and OnnxValue as corresponding values.
*/
type ReturnType = OnnxValueMapType;
/**
* A set of configurations for session behavior.
*/
interface SessionOptions extends OnnxModelOptions {
/**
* An array of execution provider options.
*
* An execution provider option can be a string indicating the name of the execution provider,
* or an object of corresponding type.
*/
executionProviders?: readonly ExecutionProviderConfig[];
/**
* The intra OP threads number.
*
* This setting is available only in ONNXRuntime (Node.js binding and react-native).
*/
intraOpNumThreads?: number;
/**
* The inter OP threads number.
*
* This setting is available only in ONNXRuntime (Node.js binding and react-native).
*/
interOpNumThreads?: number;
/**
* The free dimension override.
*
* This setting is available only in ONNXRuntime (Node.js binding and react-native) or WebAssembly backend
*/
freeDimensionOverrides?: {
readonly [dimensionName: string]: number;
};
/**
* The optimization level.
*
* This setting is available only in ONNXRuntime (Node.js binding and react-native) or WebAssembly backend
*/
graphOptimizationLevel?: 'disabled' | 'basic' | 'extended' | 'layout' | 'all';
/**
* Whether enable CPU memory arena.
*
* This setting is available only in ONNXRuntime (Node.js binding and react-native) or WebAssembly backend
*/
enableCpuMemArena?: boolean;
/**
* Whether enable memory pattern.
*
* This setting is available only in ONNXRuntime (Node.js binding and react-native) or WebAssembly backend
*/
enableMemPattern?: boolean;
/**
* Execution mode.
*
* This setting is available only in ONNXRuntime (Node.js binding and react-native) or WebAssembly backend
*/
executionMode?: 'sequential' | 'parallel';
/**
* Optimized model file path.
*
* If this setting is specified, the optimized model will be dumped. In browser, a blob will be created
* with a pop-up window.
*/
optimizedModelFilePath?: string;
/**
* Whether enable profiling.
*
* This setting is a placeholder for a future use.
*/
enableProfiling?: boolean;
/**
* File prefix for profiling.
*
* This setting is a placeholder for a future use.
*/
profileFilePrefix?: string;
/**
* Log ID.
*
* This setting is available only in ONNXRuntime (Node.js binding and react-native) or WebAssembly backend
*/
logId?: string;
/**
* Log severity level. See
* https://github.com/microsoft/onnxruntime/blob/main/include/onnxruntime/core/common/logging/severity.h
*
* This setting is available only in ONNXRuntime (Node.js binding and react-native) or WebAssembly backend
*/
logSeverityLevel?: 0 | 1 | 2 | 3 | 4;
/**
* Log verbosity level.
*
* This setting is available only in WebAssembly backend. Will support Node.js binding and react-native later
*/
logVerbosityLevel?: number;
/**
* Specify string as a preferred data location for all outputs, or an object that use output names as keys and a
* preferred data location as corresponding values.
*
* This setting is available only in ONNXRuntime Web for WebGL and WebGPU EP.
*/
preferredOutputLocation?: OnnxValueDataLocation | {
readonly [outputName: string]: OnnxValueDataLocation;
};
/**
* Whether enable graph capture.
* This setting is available only in ONNXRuntime Web for WebGPU EP.
*/
enableGraphCapture?: boolean;
/**
* Store configurations for a session. See
* https://github.com/microsoft/onnxruntime/blob/main/include/onnxruntime/core/session/
* onnxruntime_session_options_config_keys.h
*
* This setting is available only in WebAssembly backend. Will support Node.js binding and react-native later
*
* @example
* ```js
* extra: {
* session: {
* set_denormal_as_zero: "1",
* disable_prepacking: "1"
* },
* optimization: {
* enable_gelu_approximation: "1"
* }
* }
* ```
*/
extra?: Record<string, unknown>;
}
interface ExecutionProviderOptionMap {
coreml: CoreMLExecutionProviderOption;
cpu: CpuExecutionProviderOption;
cuda: CudaExecutionProviderOption;
dml: DmlExecutionProviderOption;
nnapi: NnapiExecutionProviderOption;
tensorrt: TensorRtExecutionProviderOption;
wasm: WebAssemblyExecutionProviderOption;
webgl: WebGLExecutionProviderOption;
webgpu: WebGpuExecutionProviderOption;
webnn: WebNNExecutionProviderOption;
qnn: QnnExecutionProviderOption;
xnnpack: XnnpackExecutionProviderOption;
}
type ExecutionProviderName = keyof ExecutionProviderOptionMap;
type ExecutionProviderConfig = ExecutionProviderOptionMap[ExecutionProviderName] | ExecutionProviderOption | ExecutionProviderName | string;
interface ExecutionProviderOption {
readonly name: string;
}
interface CpuExecutionProviderOption extends ExecutionProviderOption {
readonly name: 'cpu';
useArena?: boolean;
}
interface CudaExecutionProviderOption extends ExecutionProviderOption {
readonly name: 'cuda';
deviceId?: number;
}
interface DmlExecutionProviderOption extends ExecutionProviderOption {
readonly name: 'dml';
deviceId?: number;
}
interface TensorRtExecutionProviderOption extends ExecutionProviderOption {
readonly name: 'tensorrt';
deviceId?: number;
}
interface WebAssemblyExecutionProviderOption extends ExecutionProviderOption {
readonly name: 'wasm';
}
interface WebGLExecutionProviderOption extends ExecutionProviderOption {
readonly name: 'webgl';
}
interface XnnpackExecutionProviderOption extends ExecutionProviderOption {
readonly name: 'xnnpack';
}
interface WebGpuExecutionProviderOption extends ExecutionProviderOption {
readonly name: 'webgpu';
/**
* Specify the preferred layout when running layout sensitive operators.
*
* @default 'NCHW'
*/
preferredLayout?: 'NCHW' | 'NHWC';
/**
* Specify a list of node names that should be executed on CPU even when WebGPU EP is used.
*/
forceCpuNodeNames?: readonly string[];
/**
* Specify the validation mode for WebGPU execution provider.
* - 'disabled': Disable all validation.
* When used in Node.js, disable validation may cause process crash if WebGPU errors occur. Be cautious when using
* this mode.
* When used in web, this mode is equivalent to 'wgpuOnly'.
* - 'wgpuOnly': Perform WebGPU internal validation only.
* - 'basic': Perform basic validation including WebGPU internal validation. This is the default mode.
* - 'full': Perform full validation. This mode may have performance impact. Use it for debugging purpose.
*
* @default 'basic'
*/
validationMode?: 'disabled' | 'wgpuOnly' | 'basic' | 'full';
/**
* Specify an optional WebGPU device to be used by the WebGPU execution provider.
*/
device?: TryGetGlobalType<'GPUDevice'>;
}
interface WebNNExecutionProviderName extends ExecutionProviderOption {
readonly name: 'webnn';
}
/**
* Represents a set of options for creating a WebNN MLContext.
*
* @see https://www.w3.org/TR/webnn/#dictdef-mlcontextoptions
*/
interface WebNNContextOptions {
deviceType?: 'cpu' | 'gpu' | 'npu';
numThreads?: number;
powerPreference?: 'default' | 'low-power' | 'high-performance';
}
/**
* Represents a set of options for WebNN execution provider without MLContext.
*/
interface WebNNOptionsWithoutMLContext extends WebNNExecutionProviderName, WebNNContextOptions {
context?: never;
}
/**
* Represents a set of options for WebNN execution provider with MLContext.
*
* When MLContext is provided, the deviceType is also required so that the WebNN EP can determine the preferred
* channel layout.
*
* @see https://www.w3.org/TR/webnn/#dom-ml-createcontext
*/
interface WebNNOptionsWithMLContext extends WebNNExecutionProviderName, Omit<WebNNContextOptions, 'deviceType'>, Required<Pick<WebNNContextOptions, 'deviceType'>> {
context: TryGetGlobalType<'MLContext'>;
}
/**
* Represents a set of options for WebNN execution provider with MLContext which is created from GPUDevice.
*
* @see https://www.w3.org/TR/webnn/#dom-ml-createcontext-gpudevice
*/
interface WebNNOptionsWebGpu extends WebNNExecutionProviderName {
context: TryGetGlobalType<'MLContext'>;
gpuDevice: TryGetGlobalType<'GPUDevice'>;
}
/**
* Options for WebNN execution provider.
*/
type WebNNExecutionProviderOption = WebNNOptionsWithoutMLContext | WebNNOptionsWithMLContext | WebNNOptionsWebGpu;
interface QnnExecutionProviderOption extends ExecutionProviderOption {
readonly name: 'qnn';
/**
* Specify the QNN backend type. E.g., 'cpu' or 'htp'.
* Mutually exclusive with `backendPath`.
*
* @default 'htp'
*/
backendType?: string;
/**
* Specify a path to the QNN backend library.
* Mutually exclusive with `backendType`.
*/
backendPath?: string;
/**
* Specify whether to enable HTP FP16 precision.
*
* @default true
*/
enableFp16Precision?: boolean;
}
interface CoreMLExecutionProviderOption extends ExecutionProviderOption {
readonly name: 'coreml';
/**
* The bit flags for CoreML execution provider.
*
* ```
* COREML_FLAG_USE_CPU_ONLY = 0x001
* COREML_FLAG_ENABLE_ON_SUBGRAPH = 0x002
* COREML_FLAG_ONLY_ENABLE_DEVICE_WITH_ANE = 0x004
* COREML_FLAG_ONLY_ALLOW_STATIC_INPUT_SHAPES = 0x008
* COREML_FLAG_CREATE_MLPROGRAM = 0x010
* COREML_FLAG_USE_CPU_AND_GPU = 0x020
* ```
*
* See include/onnxruntime/core/providers/coreml/coreml_provider_factory.h for more details.
*
* This flag is available only in ONNXRuntime (Node.js binding).
*/
coreMlFlags?: number;
/**
* Specify whether to use CPU only in CoreML EP.
*
* This setting is available only in ONNXRuntime (react-native).
*/
useCPUOnly?: boolean;
useCPUAndGPU?: boolean;
/**
* Specify whether to enable CoreML EP on subgraph.
*
* This setting is available only in ONNXRuntime (react-native).
*/
enableOnSubgraph?: boolean;
/**
* Specify whether to only enable CoreML EP for Apple devices with ANE (Apple Neural Engine).
*
* This setting is available only in ONNXRuntime (react-native).
*/
onlyEnableDeviceWithANE?: boolean;
}
interface NnapiExecutionProviderOption extends ExecutionProviderOption {
readonly name: 'nnapi';
useFP16?: boolean;
useNCHW?: boolean;
cpuDisabled?: boolean;
cpuOnly?: boolean;
}
/**
* A set of configurations for inference run behavior
*/
interface RunOptions {
/**
* Log severity level. See
* https://github.com/microsoft/onnxruntime/blob/main/include/onnxruntime/core/common/logging/severity.h
*
* This setting is available only in ONNXRuntime (Node.js binding and react-native) or WebAssembly backend
*/
logSeverityLevel?: 0 | 1 | 2 | 3 | 4;
/**
* Log verbosity level.
*
* This setting is available only in WebAssembly backend. Will support Node.js binding and react-native later
*/
logVerbosityLevel?: number;
/**
* Terminate all incomplete OrtRun calls as soon as possible if true
*
* This setting is available only in WebAssembly backend. Will support Node.js binding and react-native later
*/
terminate?: boolean;
/**
* A tag for the Run() calls using this
*
* This setting is available only in ONNXRuntime (Node.js binding and react-native) or WebAssembly backend
*/
tag?: string;
/**
* Set a single run configuration entry. See
* https://github.com/microsoft/onnxruntime/blob/main/include/onnxruntime/core/session/
* onnxruntime_run_options_config_keys.h
*
* This setting is available only in WebAssembly backend. Will support Node.js binding and react-native later
*
* @example
*
* ```js
* extra: {
* memory: {
* enable_memory_arena_shrinkage: "1",
* }
* }
* ```
*/
extra?: Record<string, unknown>;
}
/**
* The common part of the value metadata type for both tensor and non-tensor values.
*/
interface ValueMetadataBase {
/**
* The name of the specified input or output.
*/
readonly name: string;
}
/**
* Represents the metadata of a non-tensor value.
*/
interface NonTensorValueMetadata extends ValueMetadataBase {
/**
* Get a value indicating whether the value is a tensor.
*/
readonly isTensor: false;
}
/**
* Represents the metadata of a tensor value.
*/
interface TensorValueMetadata extends ValueMetadataBase {
/**
* Get a value indicating whether the value is a tensor.
*/
readonly isTensor: true;
/**
* Get the data type of the tensor.
*/
readonly type: Tensor.Type;
/**
* Get the shape of the tensor.
*
* If the shape is not defined, the value will an empty array. Otherwise, it will be an array representing the shape
* of the tensor. Each element in the array can be a number or a string. If the element is a number, it represents
* the corresponding dimension size. If the element is a string, it represents a symbolic dimension.
*/
readonly shape: ReadonlyArray<number | string>;
}
/**
* Represents the metadata of a value.
*/
type ValueMetadata = NonTensorValueMetadata | TensorValueMetadata;
}
/**
* Represent a runtime instance of an ONNX model.
*/
export interface InferenceSession {
/**
* Execute the model asynchronously with the given feeds and options.
*
* @param feeds - Representation of the model input. See type description of `InferenceSession.InputType` for detail.
* @param options - Optional. A set of options that controls the behavior of model inference.
* @returns A promise that resolves to a map, which uses output names as keys and OnnxValue as corresponding values.
*/
run(feeds: InferenceSession.FeedsType, options?: InferenceSession.RunOptions): Promise<InferenceSession.ReturnType>;
/**
* Execute the model asynchronously with the given feeds, fetches and options.
*
* @param feeds - Representation of the model input. See type description of `InferenceSession.InputType` for detail.
* @param fetches - Representation of the model output. See type description of `InferenceSession.OutputType` for
* detail.
* @param options - Optional. A set of options that controls the behavior of model inference.
* @returns A promise that resolves to a map, which uses output names as keys and OnnxValue as corresponding values.
*/
run(feeds: InferenceSession.FeedsType, fetches: InferenceSession.FetchesType, options?: InferenceSession.RunOptions): Promise<InferenceSession.ReturnType>;
/**
* Release the inference session and the underlying resources.
*/
release(): Promise<void>;
/**
* Start profiling.
*/
startProfiling(): void;
/**
* End profiling.
*/
endProfiling(): void;
/**
* Get input names of the loaded model.
*/
readonly inputNames: readonly string[];
/**
* Get output names of the loaded model.
*/
readonly outputNames: readonly string[];
/**
* Get input metadata of the loaded model.
*/
readonly inputMetadata: readonly InferenceSession.ValueMetadata[];
/**
* Get output metadata of the loaded model.
*/
readonly outputMetadata: readonly InferenceSession.ValueMetadata[];
}
export interface InferenceSessionFactory {
/**
* Create a new inference session and load model asynchronously from an ONNX model file.
*
* @param uri - The URI or file path of the model to load.
* @param options - specify configuration for creating a new inference session.
* @returns A promise that resolves to an InferenceSession object.
*/
create(uri: string, options?: InferenceSession.SessionOptions): Promise<InferenceSession>;
/**
* Create a new inference session and load model asynchronously from an array bufer.
*
* @param buffer - An ArrayBuffer representation of an ONNX model.
* @param options - specify configuration for creating a new inference session.
* @returns A promise that resolves to an InferenceSession object.
*/
create(buffer: ArrayBufferLike, options?: InferenceSession.SessionOptions): Promise<InferenceSession>;
/**
* Create a new inference session and load model asynchronously from segment of an array bufer.
*
* @param buffer - An ArrayBuffer representation of an ONNX model.
* @param byteOffset - The beginning of the specified portion of the array buffer.
* @param byteLength - The length in bytes of the array buffer.
* @param options - specify configuration for creating a new inference session.
* @returns A promise that resolves to an InferenceSession object.
*/
create(buffer: ArrayBufferLike, byteOffset: number, byteLength?: number, options?: InferenceSession.SessionOptions): Promise<InferenceSession>;
/**
* Create a new inference session and load model asynchronously from a Uint8Array.
*
* @param buffer - A Uint8Array representation of an ONNX model.
* @param options - specify configuration for creating a new inference session.
* @returns A promise that resolves to an InferenceSession object.
*/
create(buffer: Uint8Array, options?: InferenceSession.SessionOptions): Promise<InferenceSession>;
}
export declare const InferenceSession: InferenceSessionFactory;
//# sourceMappingURL=inference-session.d.ts.map
File diff suppressed because one or more lines are too long
@@ -0,0 +1,9 @@
"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.InferenceSession = void 0;
const inference_session_impl_js_1 = require("./inference-session-impl.js");
// eslint-disable-next-line @typescript-eslint/naming-convention
exports.InferenceSession = inference_session_impl_js_1.InferenceSession;
//# sourceMappingURL=inference-session.js.map
@@ -0,0 +1 @@
{"version":3,"file":"inference-session.js","sourceRoot":"","sources":["../../lib/inference-session.ts"],"names":[],"mappings":";AAAA,4DAA4D;AAC5D,kCAAkC;;;AAElC,2EAAuF;AAsoBvF,gEAAgE;AACnD,QAAA,gBAAgB,GAA4B,4CAAoB,CAAC"}
+49
View File
@@ -0,0 +1,49 @@
/**
* A string that represents a file's URL or path.
*
* Path is vailable only in onnxruntime-node or onnxruntime-web running in Node.js.
*/
export type FileUrlOrPath = string;
/**
* A Blob object that represents a file.
*/
export type FileBlob = Blob;
/**
* A Uint8Array, ArrayBuffer or SharedArrayBuffer object that represents a file content.
*
* When it is an ArrayBuffer or SharedArrayBuffer, the whole buffer is assumed to be the file content.
*/
export type FileData = Uint8Array | ArrayBufferLike;
/**
* Represents a file that can be loaded by the ONNX Runtime JavaScript API.
*/
export type FileType = FileUrlOrPath | FileBlob | FileData;
/**
* Represents an external data file.
*/
export interface ExternalDataFileDescription {
/**
* Specify the external data file.
*/
data: FileType;
/**
* Specify the file path.
*/
path: string;
}
/**
* Represents an external data file.
*
* When using a string, it should be a file URL or path that in the same directory as the model file.
*/
export type ExternalDataFileType = ExternalDataFileDescription | FileUrlOrPath;
/**
* Options for model loading.
*/
export interface OnnxModelOptions {
/**
* Specifying a list of files that represents the external data.
*/
externalData?: readonly ExternalDataFileType[];
}
//# sourceMappingURL=onnx-model.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"onnx-model.d.ts","sourceRoot":"","sources":["../../lib/onnx-model.ts"],"names":[],"mappings":"AAGA;;;;GAIG;AACH,MAAM,MAAM,aAAa,GAAG,MAAM,CAAC;AAEnC;;GAEG;AACH,MAAM,MAAM,QAAQ,GAAG,IAAI,CAAC;AAE5B;;;;GAIG;AACH,MAAM,MAAM,QAAQ,GAAG,UAAU,GAAG,eAAe,CAAC;AAEpD;;GAEG;AACH,MAAM,MAAM,QAAQ,GAAG,aAAa,GAAG,QAAQ,GAAG,QAAQ,CAAC;AAE3D;;GAEG;AACH,MAAM,WAAW,2BAA2B;IAC1C;;OAEG;IACH,IAAI,EAAE,QAAQ,CAAC;IACf;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;CACd;AAED;;;;GAIG;AACH,MAAM,MAAM,oBAAoB,GAAG,2BAA2B,GAAG,aAAa,CAAC;AAE/E;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;OAEG;IACH,YAAY,CAAC,EAAE,SAAS,oBAAoB,EAAE,CAAC;CAChD"}
+5
View File
@@ -0,0 +1,5 @@
"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
//# sourceMappingURL=onnx-model.js.map
@@ -0,0 +1 @@
{"version":3,"file":"onnx-model.js","sourceRoot":"","sources":["../../lib/onnx-model.ts"],"names":[],"mappings":";AAAA,4DAA4D;AAC5D,kCAAkC"}
+13
View File
@@ -0,0 +1,13 @@
import { Tensor } from './tensor.js';
export type NonTensorType = never;
/**
* Type OnnxValue Represents both tensors and non-tensors value for model's inputs/outputs.
*
* NOTE: currently not support non-tensor
*/
export type OnnxValue = Tensor | NonTensorType;
/**
* Type OnnxValueDataLocation represents the location of the data of an OnnxValue.
*/
export type OnnxValueDataLocation = Tensor.DataLocation;
//# sourceMappingURL=onnx-value.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"onnx-value.d.ts","sourceRoot":"","sources":["../../lib/onnx-value.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,MAAM,MAAM,aAAa,GAAG,KAAK,CAAC;AAElC;;;;GAIG;AACH,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,aAAa,CAAC;AAE/C;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAAG,MAAM,CAAC,YAAY,CAAC"}
+5
View File
@@ -0,0 +1,5 @@
"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
//# sourceMappingURL=onnx-value.js.map
@@ -0,0 +1 @@
{"version":3,"file":"onnx-value.js","sourceRoot":"","sources":["../../lib/onnx-value.ts"],"names":[],"mappings":";AAAA,4DAA4D;AAC5D,kCAAkC"}
+1
View File
@@ -0,0 +1 @@
{"type": "commonjs"}
@@ -0,0 +1,11 @@
import { TensorToDataUrlOptions, TensorToImageDataOptions } from './tensor-conversion.js';
import { Tensor } from './tensor.js';
/**
* implementation of Tensor.toDataURL()
*/
export declare const tensorToDataURL: (tensor: Tensor, options?: TensorToDataUrlOptions) => string;
/**
* implementation of Tensor.toImageData()
*/
export declare const tensorToImageData: (tensor: Tensor, options?: TensorToImageDataOptions) => ImageData;
//# sourceMappingURL=tensor-conversion-impl.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"tensor-conversion-impl.d.ts","sourceRoot":"","sources":["../../lib/tensor-conversion-impl.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,sBAAsB,EAAE,wBAAwB,EAAE,MAAM,wBAAwB,CAAC;AAC1F,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC;;GAEG;AACH,eAAO,MAAM,eAAe,GAAI,QAAQ,MAAM,EAAE,UAAU,sBAAsB,KAAG,MA8FlF,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,iBAAiB,GAAI,QAAQ,MAAM,EAAE,UAAU,wBAAwB,KAAG,SAyGtF,CAAC"}
@@ -0,0 +1,200 @@
"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.tensorToImageData = exports.tensorToDataURL = void 0;
/**
* implementation of Tensor.toDataURL()
*/
const tensorToDataURL = (tensor, options) => {
const canvas = typeof document !== 'undefined' ? document.createElement('canvas') : new OffscreenCanvas(1, 1);
canvas.width = tensor.dims[3];
canvas.height = tensor.dims[2];
const pixels2DContext = canvas.getContext('2d');
if (pixels2DContext != null) {
// Default values for height and width & format
let width;
let height;
if (options?.tensorLayout !== undefined && options.tensorLayout === 'NHWC') {
width = tensor.dims[2];
height = tensor.dims[3];
}
else {
// Default layout is NCWH
width = tensor.dims[3];
height = tensor.dims[2];
}
const inputformat = options?.format !== undefined ? options.format : 'RGB';
const norm = options?.norm;
let normMean;
let normBias;
if (norm === undefined || norm.mean === undefined) {
normMean = [255, 255, 255, 255];
}
else {
if (typeof norm.mean === 'number') {
normMean = [norm.mean, norm.mean, norm.mean, norm.mean];
}
else {
normMean = [norm.mean[0], norm.mean[1], norm.mean[2], 0];
if (norm.mean[3] !== undefined) {
normMean[3] = norm.mean[3];
}
}
}
if (norm === undefined || norm.bias === undefined) {
normBias = [0, 0, 0, 0];
}
else {
if (typeof norm.bias === 'number') {
normBias = [norm.bias, norm.bias, norm.bias, norm.bias];
}
else {
normBias = [norm.bias[0], norm.bias[1], norm.bias[2], 0];
if (norm.bias[3] !== undefined) {
normBias[3] = norm.bias[3];
}
}
}
const stride = height * width;
// Default pointer assignments
let rTensorPointer = 0, gTensorPointer = stride, bTensorPointer = stride * 2, aTensorPointer = -1;
// Updating the pointer assignments based on the input image format
if (inputformat === 'RGBA') {
rTensorPointer = 0;
gTensorPointer = stride;
bTensorPointer = stride * 2;
aTensorPointer = stride * 3;
}
else if (inputformat === 'RGB') {
rTensorPointer = 0;
gTensorPointer = stride;
bTensorPointer = stride * 2;
}
else if (inputformat === 'RBG') {
rTensorPointer = 0;
bTensorPointer = stride;
gTensorPointer = stride * 2;
}
for (let i = 0; i < height; i++) {
for (let j = 0; j < width; j++) {
const R = (tensor.data[rTensorPointer++] - normBias[0]) * normMean[0]; // R value
const G = (tensor.data[gTensorPointer++] - normBias[1]) * normMean[1]; // G value
const B = (tensor.data[bTensorPointer++] - normBias[2]) * normMean[2]; // B value
const A = aTensorPointer === -1 ? 255 : (tensor.data[aTensorPointer++] - normBias[3]) * normMean[3]; // A value
pixels2DContext.fillStyle = 'rgba(' + R + ',' + G + ',' + B + ',' + A + ')';
pixels2DContext.fillRect(j, i, 1, 1);
}
}
if ('toDataURL' in canvas) {
return canvas.toDataURL();
}
else {
throw new Error('toDataURL is not supported');
}
}
else {
throw new Error('Can not access image data');
}
};
exports.tensorToDataURL = tensorToDataURL;
/**
* implementation of Tensor.toImageData()
*/
const tensorToImageData = (tensor, options) => {
const pixels2DContext = typeof document !== 'undefined'
? document.createElement('canvas').getContext('2d')
: new OffscreenCanvas(1, 1).getContext('2d');
let image;
if (pixels2DContext != null) {
// Default values for height and width & format
let width;
let height;
let channels;
if (options?.tensorLayout !== undefined && options.tensorLayout === 'NHWC') {
width = tensor.dims[2];
height = tensor.dims[1];
channels = tensor.dims[3];
}
else {
// Default layout is NCWH
width = tensor.dims[3];
height = tensor.dims[2];
channels = tensor.dims[1];
}
const inputformat = options !== undefined ? (options.format !== undefined ? options.format : 'RGB') : 'RGB';
const norm = options?.norm;
let normMean;
let normBias;
if (norm === undefined || norm.mean === undefined) {
normMean = [255, 255, 255, 255];
}
else {
if (typeof norm.mean === 'number') {
normMean = [norm.mean, norm.mean, norm.mean, norm.mean];
}
else {
normMean = [norm.mean[0], norm.mean[1], norm.mean[2], 255];
if (norm.mean[3] !== undefined) {
normMean[3] = norm.mean[3];
}
}
}
if (norm === undefined || norm.bias === undefined) {
normBias = [0, 0, 0, 0];
}
else {
if (typeof norm.bias === 'number') {
normBias = [norm.bias, norm.bias, norm.bias, norm.bias];
}
else {
normBias = [norm.bias[0], norm.bias[1], norm.bias[2], 0];
if (norm.bias[3] !== undefined) {
normBias[3] = norm.bias[3];
}
}
}
const stride = height * width;
if (options !== undefined) {
if ((options.format !== undefined && channels === 4 && options.format !== 'RGBA') ||
(channels === 3 && options.format !== 'RGB' && options.format !== 'BGR')) {
throw new Error("Tensor format doesn't match input tensor dims");
}
}
// Default pointer assignments
const step = 4;
let rImagePointer = 0, gImagePointer = 1, bImagePointer = 2, aImagePointer = 3;
let rTensorPointer = 0, gTensorPointer = stride, bTensorPointer = stride * 2, aTensorPointer = -1;
// Updating the pointer assignments based on the input image format
if (inputformat === 'RGBA') {
rTensorPointer = 0;
gTensorPointer = stride;
bTensorPointer = stride * 2;
aTensorPointer = stride * 3;
}
else if (inputformat === 'RGB') {
rTensorPointer = 0;
gTensorPointer = stride;
bTensorPointer = stride * 2;
}
else if (inputformat === 'RBG') {
rTensorPointer = 0;
bTensorPointer = stride;
gTensorPointer = stride * 2;
}
image = pixels2DContext.createImageData(width, height);
for (let i = 0; i < height * width; rImagePointer += step, gImagePointer += step, bImagePointer += step, aImagePointer += step, i++) {
image.data[rImagePointer] = (tensor.data[rTensorPointer++] - normBias[0]) * normMean[0]; // R value
image.data[gImagePointer] = (tensor.data[gTensorPointer++] - normBias[1]) * normMean[1]; // G value
image.data[bImagePointer] = (tensor.data[bTensorPointer++] - normBias[2]) * normMean[2]; // B value
image.data[aImagePointer] =
aTensorPointer === -1 ? 255 : (tensor.data[aTensorPointer++] - normBias[3]) * normMean[3]; // A value
}
}
else {
throw new Error('Can not access image data');
}
return image;
};
exports.tensorToImageData = tensorToImageData;
//# sourceMappingURL=tensor-conversion-impl.js.map
File diff suppressed because one or more lines are too long
@@ -0,0 +1,30 @@
import { OptionsFormat, OptionsNormalizationParameters, OptionsTensorLayout } from './tensor-factory.js';
export interface TensorToDataUrlOptions extends OptionsTensorLayout, OptionsFormat, OptionsNormalizationParameters {
}
export interface TensorToImageDataOptions extends OptionsTensorLayout, OptionsFormat, OptionsNormalizationParameters {
}
export interface ConversionUtils {
/**
* creates a DataURL instance from tensor
*
* @param options - An optional object representing options for creating a DataURL instance from the tensor.
*
* The following default settings will be applied:
* - `format`: `'RGB'`
* - `tensorLayout`: `'NCHW'`
* @returns a DataURL string representing the image converted from tensor data
*/
toDataURL(options?: TensorToDataUrlOptions): string;
/**
* creates an ImageData instance from tensor
*
* @param options - An optional object representing options for creating an ImageData instance from the tensor.
*
* The following default settings will be applied:
* - `format`: `'RGB'`
* - `tensorLayout`: `'NCHW'`
* @returns an ImageData instance representing the image converted from tensor data
*/
toImageData(options?: TensorToImageDataOptions): ImageData;
}
//# sourceMappingURL=tensor-conversion.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"tensor-conversion.d.ts","sourceRoot":"","sources":["../../lib/tensor-conversion.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,aAAa,EAAE,8BAA8B,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;AAEzG,MAAM,WAAW,sBAAuB,SAAQ,mBAAmB,EAAE,aAAa,EAAE,8BAA8B;CAAG;AAErH,MAAM,WAAW,wBAAyB,SAAQ,mBAAmB,EAAE,aAAa,EAAE,8BAA8B;CAAG;AAEvH,MAAM,WAAW,eAAe;IAC9B;;;;;;;;;OASG;IACH,SAAS,CAAC,OAAO,CAAC,EAAE,sBAAsB,GAAG,MAAM,CAAC;IAEpD;;;;;;;;;OASG;IACH,WAAW,CAAC,OAAO,CAAC,EAAE,wBAAwB,GAAG,SAAS,CAAC;CAC5D"}
@@ -0,0 +1,5 @@
"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
//# sourceMappingURL=tensor-conversion.js.map
@@ -0,0 +1 @@
{"version":3,"file":"tensor-conversion.js","sourceRoot":"","sources":["../../lib/tensor-conversion.ts"],"names":[],"mappings":";AAAA,4DAA4D;AAC5D,kCAAkC"}
@@ -0,0 +1,35 @@
import { OptionsDimensions, OptionsFormat, OptionsNormalizationParameters, OptionsTensorFormat, OptionsTensorLayout, TensorFromGpuBufferOptions, TensorFromImageBitmapOptions, TensorFromImageDataOptions, TensorFromImageElementOptions, TensorFromMLTensorOptions, TensorFromTextureOptions, TensorFromUrlOptions } from './tensor-factory.js';
import { Tensor } from './tensor-impl.js';
import { Tensor as TensorInterface } from './tensor.js';
interface BufferToTensorOptions extends OptionsDimensions, OptionsTensorLayout, OptionsNormalizationParameters, OptionsFormat, OptionsTensorFormat {
}
/**
* Create a new tensor object from image object
*
* @param buffer - Extracted image buffer data - assuming RGBA format
* @param imageFormat - input image configuration - required configurations height, width, format
* @param tensorFormat - output tensor configuration - Default is RGB format
*/
export declare const bufferToTensor: (buffer: Uint8ClampedArray | undefined, options: BufferToTensorOptions) => Tensor;
/**
* implementation of Tensor.fromImage().
*/
export declare const tensorFromImage: (image: ImageData | HTMLImageElement | ImageBitmap | string, options?: TensorFromImageDataOptions | TensorFromImageElementOptions | TensorFromImageBitmapOptions | TensorFromUrlOptions) => Promise<Tensor>;
/**
* implementation of Tensor.fromTexture().
*/
export declare const tensorFromTexture: <T extends TensorInterface.TextureDataTypes>(texture: TensorInterface.TextureType, options: TensorFromTextureOptions<T>) => Tensor;
/**
* implementation of Tensor.fromGpuBuffer().
*/
export declare const tensorFromGpuBuffer: <T extends TensorInterface.GpuBufferDataTypes>(gpuBuffer: TensorInterface.GpuBufferType, options: TensorFromGpuBufferOptions<T>) => Tensor;
/**
* implementation of Tensor.fromMLTensor().
*/
export declare const tensorFromMLTensor: <T extends TensorInterface.MLTensorDataTypes>(mlTensor: TensorInterface.MLTensorType, options: TensorFromMLTensorOptions<T>) => Tensor;
/**
* implementation of Tensor.fromPinnedBuffer().
*/
export declare const tensorFromPinnedBuffer: <T extends TensorInterface.CpuPinnedDataTypes>(type: T, buffer: TensorInterface.DataTypeMap[T], dims?: readonly number[]) => Tensor;
export {};
//# sourceMappingURL=tensor-factory-impl.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"tensor-factory-impl.d.ts","sourceRoot":"","sources":["../../lib/tensor-factory-impl.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,iBAAiB,EACjB,aAAa,EACb,8BAA8B,EAC9B,mBAAmB,EACnB,mBAAmB,EACnB,0BAA0B,EAC1B,4BAA4B,EAC5B,0BAA0B,EAC1B,6BAA6B,EAC7B,yBAAyB,EACzB,wBAAwB,EACxB,oBAAoB,EACrB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EAAE,MAAM,IAAI,eAAe,EAAE,MAAM,aAAa,CAAC;AAExD,UAAU,qBACR,SAAQ,iBAAiB,EAAE,mBAAmB,EAAE,8BAA8B,EAAE,aAAa,EAAE,mBAAmB;CAAG;AAEvH;;;;;;GAMG;AACH,eAAO,MAAM,cAAc,GAAI,QAAQ,iBAAiB,GAAG,SAAS,EAAE,SAAS,qBAAqB,KAAG,MAyFtG,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,eAAe,GAC1B,OAAO,SAAS,GAAG,gBAAgB,GAAG,WAAW,GAAG,MAAM,EAC1D,UACI,0BAA0B,GAC1B,6BAA6B,GAC7B,4BAA4B,GAC5B,oBAAoB,KACvB,OAAO,CAAC,MAAM,CAwJhB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,iBAAiB,GAAI,CAAC,SAAS,eAAe,CAAC,gBAAgB,EAC1E,SAAS,eAAe,CAAC,WAAW,EACpC,SAAS,wBAAwB,CAAC,CAAC,CAAC,KACnC,MAKF,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,mBAAmB,GAAI,CAAC,SAAS,eAAe,CAAC,kBAAkB,EAC9E,WAAW,eAAe,CAAC,aAAa,EACxC,SAAS,0BAA0B,CAAC,CAAC,CAAC,KACrC,MAGF,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,kBAAkB,GAAI,CAAC,SAAS,eAAe,CAAC,iBAAiB,EAC5E,UAAU,eAAe,CAAC,YAAY,EACtC,SAAS,yBAAyB,CAAC,CAAC,CAAC,KACpC,MAGF,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,sBAAsB,GAAI,CAAC,SAAS,eAAe,CAAC,kBAAkB,EACjF,MAAM,CAAC,EACP,QAAQ,eAAe,CAAC,WAAW,CAAC,CAAC,CAAC,EACtC,OAAO,SAAS,MAAM,EAAE,KACvB,MAAmG,CAAC"}
@@ -0,0 +1,274 @@
"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.tensorFromPinnedBuffer = exports.tensorFromMLTensor = exports.tensorFromGpuBuffer = exports.tensorFromTexture = exports.tensorFromImage = exports.bufferToTensor = void 0;
const tensor_impl_js_1 = require("./tensor-impl.js");
/**
* Create a new tensor object from image object
*
* @param buffer - Extracted image buffer data - assuming RGBA format
* @param imageFormat - input image configuration - required configurations height, width, format
* @param tensorFormat - output tensor configuration - Default is RGB format
*/
const bufferToTensor = (buffer, options) => {
if (buffer === undefined) {
throw new Error('Image buffer must be defined');
}
if (options.height === undefined || options.width === undefined) {
throw new Error('Image height and width must be defined');
}
if (options.tensorLayout === 'NHWC') {
throw new Error('NHWC Tensor layout is not supported yet');
}
const { height, width } = options;
const norm = options.norm ?? { mean: 255, bias: 0 };
let normMean;
let normBias;
if (typeof norm.mean === 'number') {
normMean = [norm.mean, norm.mean, norm.mean, norm.mean];
}
else {
normMean = [norm.mean[0], norm.mean[1], norm.mean[2], norm.mean[3] ?? 255];
}
if (typeof norm.bias === 'number') {
normBias = [norm.bias, norm.bias, norm.bias, norm.bias];
}
else {
normBias = [norm.bias[0], norm.bias[1], norm.bias[2], norm.bias[3] ?? 0];
}
const inputformat = options.format !== undefined ? options.format : 'RGBA';
// default value is RGBA since imagedata and HTMLImageElement uses it
const outputformat = options.tensorFormat !== undefined ? (options.tensorFormat !== undefined ? options.tensorFormat : 'RGB') : 'RGB';
const stride = height * width;
const float32Data = outputformat === 'RGBA' ? new Float32Array(stride * 4) : new Float32Array(stride * 3);
// Default pointer assignments
let step = 4, rImagePointer = 0, gImagePointer = 1, bImagePointer = 2, aImagePointer = 3;
let rTensorPointer = 0, gTensorPointer = stride, bTensorPointer = stride * 2, aTensorPointer = -1;
// Updating the pointer assignments based on the input image format
if (inputformat === 'RGB') {
step = 3;
rImagePointer = 0;
gImagePointer = 1;
bImagePointer = 2;
aImagePointer = -1;
}
// Updating the pointer assignments based on the output tensor format
if (outputformat === 'RGBA') {
aTensorPointer = stride * 3;
}
else if (outputformat === 'RBG') {
rTensorPointer = 0;
bTensorPointer = stride;
gTensorPointer = stride * 2;
}
else if (outputformat === 'BGR') {
bTensorPointer = 0;
gTensorPointer = stride;
rTensorPointer = stride * 2;
}
for (let i = 0; i < stride; i++, rImagePointer += step, bImagePointer += step, gImagePointer += step, aImagePointer += step) {
float32Data[rTensorPointer++] = (buffer[rImagePointer] + normBias[0]) / normMean[0];
float32Data[gTensorPointer++] = (buffer[gImagePointer] + normBias[1]) / normMean[1];
float32Data[bTensorPointer++] = (buffer[bImagePointer] + normBias[2]) / normMean[2];
if (aTensorPointer !== -1 && aImagePointer !== -1) {
float32Data[aTensorPointer++] = (buffer[aImagePointer] + normBias[3]) / normMean[3];
}
}
// Float32Array -> ort.Tensor
const outputTensor = outputformat === 'RGBA'
? new tensor_impl_js_1.Tensor('float32', float32Data, [1, 4, height, width])
: new tensor_impl_js_1.Tensor('float32', float32Data, [1, 3, height, width]);
return outputTensor;
};
exports.bufferToTensor = bufferToTensor;
/**
* implementation of Tensor.fromImage().
*/
const tensorFromImage = async (image, options) => {
// checking the type of image object
const isHTMLImageEle = typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement;
const isImageDataEle = typeof ImageData !== 'undefined' && image instanceof ImageData;
const isImageBitmap = typeof ImageBitmap !== 'undefined' && image instanceof ImageBitmap;
const isString = typeof image === 'string';
let data;
let bufferToTensorOptions = options ?? {};
const createCanvas = () => {
if (typeof document !== 'undefined') {
return document.createElement('canvas');
}
else if (typeof OffscreenCanvas !== 'undefined') {
return new OffscreenCanvas(1, 1);
}
else {
throw new Error('Canvas is not supported');
}
};
const createCanvasContext = (canvas) => {
if (typeof HTMLCanvasElement !== 'undefined' && canvas instanceof HTMLCanvasElement) {
return canvas.getContext('2d');
}
else if (canvas instanceof OffscreenCanvas) {
return canvas.getContext('2d');
}
else {
return null;
}
};
// filling and checking image configuration options
if (isHTMLImageEle) {
// HTMLImageElement - image object - format is RGBA by default
const canvas = createCanvas();
canvas.width = image.width;
canvas.height = image.height;
const pixels2DContext = createCanvasContext(canvas);
if (pixels2DContext != null) {
let height = image.height;
let width = image.width;
if (options !== undefined && options.resizedHeight !== undefined && options.resizedWidth !== undefined) {
height = options.resizedHeight;
width = options.resizedWidth;
}
if (options !== undefined) {
bufferToTensorOptions = options;
if (options.tensorFormat !== undefined) {
throw new Error('Image input config format must be RGBA for HTMLImageElement');
}
else {
bufferToTensorOptions.tensorFormat = 'RGBA';
}
bufferToTensorOptions.height = height;
bufferToTensorOptions.width = width;
}
else {
bufferToTensorOptions.tensorFormat = 'RGBA';
bufferToTensorOptions.height = height;
bufferToTensorOptions.width = width;
}
pixels2DContext.drawImage(image, 0, 0);
data = pixels2DContext.getImageData(0, 0, width, height).data;
}
else {
throw new Error('Can not access image data');
}
}
else if (isImageDataEle) {
let height;
let width;
if (options !== undefined && options.resizedWidth !== undefined && options.resizedHeight !== undefined) {
height = options.resizedHeight;
width = options.resizedWidth;
}
else {
height = image.height;
width = image.width;
}
if (options !== undefined) {
bufferToTensorOptions = options;
}
bufferToTensorOptions.format = 'RGBA';
bufferToTensorOptions.height = height;
bufferToTensorOptions.width = width;
if (options !== undefined) {
const tempCanvas = createCanvas();
tempCanvas.width = width;
tempCanvas.height = height;
const pixels2DContext = createCanvasContext(tempCanvas);
if (pixels2DContext != null) {
pixels2DContext.putImageData(image, 0, 0);
data = pixels2DContext.getImageData(0, 0, width, height).data;
}
else {
throw new Error('Can not access image data');
}
}
else {
data = image.data;
}
}
else if (isImageBitmap) {
// ImageBitmap - image object - format must be provided by user
if (options === undefined) {
throw new Error('Please provide image config with format for Imagebitmap');
}
const canvas = createCanvas();
canvas.width = image.width;
canvas.height = image.height;
const pixels2DContext = createCanvasContext(canvas);
if (pixels2DContext != null) {
const height = image.height;
const width = image.width;
pixels2DContext.drawImage(image, 0, 0, width, height);
data = pixels2DContext.getImageData(0, 0, width, height).data;
bufferToTensorOptions.height = height;
bufferToTensorOptions.width = width;
return (0, exports.bufferToTensor)(data, bufferToTensorOptions);
}
else {
throw new Error('Can not access image data');
}
}
else if (isString) {
return new Promise((resolve, reject) => {
const canvas = createCanvas();
const context = createCanvasContext(canvas);
if (!image || !context) {
return reject();
}
const newImage = new Image();
newImage.crossOrigin = 'Anonymous';
newImage.src = image;
newImage.onload = () => {
canvas.width = newImage.width;
canvas.height = newImage.height;
context.drawImage(newImage, 0, 0, canvas.width, canvas.height);
const img = context.getImageData(0, 0, canvas.width, canvas.height);
bufferToTensorOptions.height = canvas.height;
bufferToTensorOptions.width = canvas.width;
resolve((0, exports.bufferToTensor)(img.data, bufferToTensorOptions));
};
});
}
else {
throw new Error('Input data provided is not supported - aborted tensor creation');
}
if (data !== undefined) {
return (0, exports.bufferToTensor)(data, bufferToTensorOptions);
}
else {
throw new Error('Input data provided is not supported - aborted tensor creation');
}
};
exports.tensorFromImage = tensorFromImage;
/**
* implementation of Tensor.fromTexture().
*/
const tensorFromTexture = (texture, options) => {
const { width, height, download, dispose } = options;
// Always assume RGBAF32. TODO: support different texture format
const dims = [1, height, width, 4];
return new tensor_impl_js_1.Tensor({ location: 'texture', type: 'float32', texture, dims, download, dispose });
};
exports.tensorFromTexture = tensorFromTexture;
/**
* implementation of Tensor.fromGpuBuffer().
*/
const tensorFromGpuBuffer = (gpuBuffer, options) => {
const { dataType, dims, download, dispose } = options;
return new tensor_impl_js_1.Tensor({ location: 'gpu-buffer', type: dataType ?? 'float32', gpuBuffer, dims, download, dispose });
};
exports.tensorFromGpuBuffer = tensorFromGpuBuffer;
/**
* implementation of Tensor.fromMLTensor().
*/
const tensorFromMLTensor = (mlTensor, options) => {
const { dataType, dims, download, dispose } = options;
return new tensor_impl_js_1.Tensor({ location: 'ml-tensor', type: dataType ?? 'float32', mlTensor, dims, download, dispose });
};
exports.tensorFromMLTensor = tensorFromMLTensor;
/**
* implementation of Tensor.fromPinnedBuffer().
*/
const tensorFromPinnedBuffer = (type, buffer, dims) => new tensor_impl_js_1.Tensor({ location: 'cpu-pinned', type, data: buffer, dims: dims ?? [buffer.length] });
exports.tensorFromPinnedBuffer = tensorFromPinnedBuffer;
//# sourceMappingURL=tensor-factory-impl.js.map
File diff suppressed because one or more lines are too long
@@ -0,0 +1,298 @@
import { Tensor, TypedTensor } from './tensor.js';
export type ImageFormat = 'RGB' | 'RGBA' | 'BGR' | 'RBG';
export type ImageTensorLayout = 'NHWC' | 'NCHW';
/**
* represent common properties of the parameter for constructing a tensor from a specific location.
*/
interface CommonConstructorParameters<T> extends Pick<Tensor, 'dims'> {
/**
* Specify the data type of the tensor.
*/
readonly type: T;
}
/**
* represent the parameter for constructing a tensor from a GPU resource.
*/
interface GpuResourceConstructorParameters<T extends Tensor.Type> {
/**
* an optional callback function to download data from GPU to CPU.
*
* If not provided, the tensor treat the GPU data as external resource.
*/
download?(): Promise<Tensor.DataTypeMap[T]>;
/**
* an optional callback function that will be called when the tensor is disposed.
*
* If not provided, the tensor treat the GPU data as external resource.
*/
dispose?(): void;
}
/**
* represent the parameter for constructing a tensor from a pinned CPU buffer
*/
export interface CpuPinnedConstructorParameters<T extends Tensor.CpuPinnedDataTypes = Tensor.CpuPinnedDataTypes> extends CommonConstructorParameters<T> {
/**
* Specify the location of the data to be 'cpu-pinned'.
*/
readonly location: 'cpu-pinned';
/**
* Specify the CPU pinned buffer that holds the tensor data.
*/
readonly data: Tensor.DataTypeMap[T];
}
/**
* represent the parameter for constructing a tensor from a WebGL texture
*/
export interface TextureConstructorParameters<T extends Tensor.TextureDataTypes = Tensor.TextureDataTypes> extends CommonConstructorParameters<T>, GpuResourceConstructorParameters<T> {
/**
* Specify the location of the data to be 'texture'.
*/
readonly location: 'texture';
/**
* Specify the WebGL texture that holds the tensor data.
*/
readonly texture: Tensor.TextureType;
}
/**
* represent the parameter for constructing a tensor from a WebGPU buffer
*/
export interface GpuBufferConstructorParameters<T extends Tensor.GpuBufferDataTypes = Tensor.GpuBufferDataTypes> extends CommonConstructorParameters<T>, GpuResourceConstructorParameters<T> {
/**
* Specify the location of the data to be 'gpu-buffer'.
*/
readonly location: 'gpu-buffer';
/**
* Specify the WebGPU buffer that holds the tensor data.
*/
readonly gpuBuffer: Tensor.GpuBufferType;
}
export interface MLTensorConstructorParameters<T extends Tensor.MLTensorDataTypes = Tensor.MLTensorDataTypes> extends CommonConstructorParameters<T>, GpuResourceConstructorParameters<T> {
/**
* Specify the location of the data to be 'ml-tensor'.
*/
readonly location: 'ml-tensor';
/**
* Specify the WebNN MLTensor that holds the tensor data.
*/
readonly mlTensor: Tensor.MLTensorType;
}
export interface OptionsFormat {
/**
* Describes the image format represented in RGBA color space.
*/
format?: ImageFormat;
}
export interface OptionsTensorFormat {
/**
* Describes the image format of the tensor.
*
* NOTE: this is different from option 'format'. While option 'format' represents the original image, 'tensorFormat'
* represents the target format of the tensor. A transpose will be performed if they are different.
*/
tensorFormat?: ImageFormat;
}
export interface OptionsTensorDataType {
/**
* Describes the data type of the tensor.
*/
dataType?: 'float32' | 'uint8';
}
export interface OptionsTensorLayout {
/**
* Describes the tensor layout when representing data of one or more image(s).
*/
tensorLayout?: ImageTensorLayout;
}
export interface OptionsDimensions {
/**
* Describes the image height in pixel
*/
height?: number;
/**
* Describes the image width in pixel
*/
width?: number;
}
export interface OptionResizedDimensions {
/**
* Describes the resized height. If omitted, original height will be used.
*/
resizedHeight?: number;
/**
* Describes resized width - can be accessed via tensor dimensions as well
*/
resizedWidth?: number;
}
export interface OptionsNormalizationParameters {
/**
* Describes normalization parameters when preprocessing the image as model input.
*
* Data element are ranged from 0 to 255.
*/
norm?: {
/**
* The 'bias' value for image normalization.
* - If omitted, use default value 0.
* - If it's a single number, apply to each channel
* - If it's an array of 3 or 4 numbers, apply element-wise. Number of elements need to match the number of channels
* for the corresponding image format
*/
bias?: number | [number, number, number] | [number, number, number, number];
/**
* The 'mean' value for image normalization.
* - If omitted, use default value 255.
* - If it's a single number, apply to each channel
* - If it's an array of 3 or 4 numbers, apply element-wise. Number of elements need to match the number of channels
* for the corresponding image format
*/
mean?: number | [number, number, number] | [number, number, number, number];
};
}
export interface TensorFromImageDataOptions extends OptionResizedDimensions, OptionsTensorFormat, OptionsTensorLayout, OptionsTensorDataType, OptionsNormalizationParameters {
}
export interface TensorFromImageElementOptions extends OptionResizedDimensions, OptionsTensorFormat, OptionsTensorLayout, OptionsTensorDataType, OptionsNormalizationParameters {
}
export interface TensorFromUrlOptions extends OptionsDimensions, OptionResizedDimensions, OptionsTensorFormat, OptionsTensorLayout, OptionsTensorDataType, OptionsNormalizationParameters {
}
export interface TensorFromImageBitmapOptions extends OptionResizedDimensions, OptionsTensorFormat, OptionsTensorLayout, OptionsTensorDataType, OptionsNormalizationParameters {
}
export interface TensorFromTextureOptions<T extends Tensor.TextureDataTypes> extends Required<OptionsDimensions>, OptionsFormat, GpuResourceConstructorParameters<T> {
}
export interface TensorFromGpuBufferOptions<T extends Tensor.GpuBufferDataTypes> extends Pick<Tensor, 'dims'>, GpuResourceConstructorParameters<T> {
/**
* Describes the data type of the tensor.
*/
dataType?: T;
}
export interface TensorFromMLTensorOptions<T extends Tensor.MLTensorDataTypes> extends Pick<Tensor, 'dims'>, GpuResourceConstructorParameters<T> {
/**
* Describes the data type of the tensor.
*/
dataType?: T;
}
/**
* type TensorFactory defines the factory functions of 'Tensor' to create tensor instances from existing data or
* resources.
*/
export interface TensorFactory {
/**
* create a tensor from an ImageData object
*
* @param imageData - the ImageData object to create tensor from
* @param options - An optional object representing options for creating tensor from ImageData.
*
* The following default settings will be applied:
* - `tensorFormat`: `'RGB'`
* - `tensorLayout`: `'NCHW'`
* - `dataType`: `'float32'`
* @returns A promise that resolves to a tensor object
*/
fromImage(imageData: ImageData, options?: TensorFromImageDataOptions): Promise<TypedTensor<'float32'> | TypedTensor<'uint8'>>;
/**
* create a tensor from a HTMLImageElement object
*
* @param imageElement - the HTMLImageElement object to create tensor from
* @param options - An optional object representing options for creating tensor from HTMLImageElement.
*
* The following default settings will be applied:
* - `tensorFormat`: `'RGB'`
* - `tensorLayout`: `'NCHW'`
* - `dataType`: `'float32'`
* @returns A promise that resolves to a tensor object
*/
fromImage(imageElement: HTMLImageElement, options?: TensorFromImageElementOptions): Promise<TypedTensor<'float32'> | TypedTensor<'uint8'>>;
/**
* create a tensor from URL
*
* @param urlSource - a string as a URL to the image or a data URL containing the image data.
* @param options - An optional object representing options for creating tensor from URL.
*
* The following default settings will be applied:
* - `tensorFormat`: `'RGB'`
* - `tensorLayout`: `'NCHW'`
* - `dataType`: `'float32'`
* @returns A promise that resolves to a tensor object
*/
fromImage(urlSource: string, options?: TensorFromUrlOptions): Promise<TypedTensor<'float32'> | TypedTensor<'uint8'>>;
/**
* create a tensor from an ImageBitmap object
*
* @param bitmap - the ImageBitmap object to create tensor from
* @param options - An optional object representing options for creating tensor from URL.
*
* The following default settings will be applied:
* - `tensorFormat`: `'RGB'`
* - `tensorLayout`: `'NCHW'`
* - `dataType`: `'float32'`
* @returns A promise that resolves to a tensor object
*/
fromImage(bitmap: ImageBitmap, options: TensorFromImageBitmapOptions): Promise<TypedTensor<'float32'> | TypedTensor<'uint8'>>;
/**
* create a tensor from a WebGL texture
*
* @param texture - the WebGLTexture object to create tensor from
* @param options - An optional object representing options for creating tensor from WebGL texture.
*
* The options include following properties:
* - `width`: the width of the texture. Required.
* - `height`: the height of the texture. Required.
* - `format`: the format of the texture. If omitted, assume 'RGBA'.
* - `download`: an optional function to download the tensor data from GPU to CPU. If omitted, the GPU data
* will not be able to download. Usually, this is provided by a GPU backend for the inference outputs. Users don't
* need to provide this function.
* - `dispose`: an optional function to dispose the tensor data on GPU. If omitted, the GPU data will not be disposed.
* Usually, this is provided by a GPU backend for the inference outputs. Users don't need to provide this function.
*
* @returns a tensor object
*/
fromTexture<T extends Tensor.TextureDataTypes = 'float32'>(texture: Tensor.TextureType, options: TensorFromTextureOptions<T>): TypedTensor<'float32'>;
/**
* create a tensor from a WebGPU buffer
*
* @param buffer - the GPUBuffer object to create tensor from
* @param options - An optional object representing options for creating tensor from WebGPU buffer.
*
* The options include following properties:
* - `dataType`: the data type of the tensor. If omitted, assume 'float32'.
* - `dims`: the dimension of the tensor. Required.
* - `download`: an optional function to download the tensor data from GPU to CPU. If omitted, the GPU data
* will not be able to download. Usually, this is provided by a GPU backend for the inference outputs. Users don't
* need to provide this function.
* - `dispose`: an optional function to dispose the tensor data on GPU. If omitted, the GPU data will not be disposed.
* Usually, this is provided by a GPU backend for the inference outputs. Users don't need to provide this function.
*
* @returns a tensor object
*/
fromGpuBuffer<T extends Tensor.GpuBufferDataTypes>(buffer: Tensor.GpuBufferType, options: TensorFromGpuBufferOptions<T>): TypedTensor<T>;
/**
* create a tensor from a WebNN MLTensor
*
* @param tensor - the MLTensor object to create tensor from
* @param options - An optional object representing options for creating tensor from a WebNN MLTensor.
*
* The options include following properties:
* - `dataType`: the data type of the tensor. If omitted, assume 'float32'.
* - `dims`: the dimension of the tensor. Required.
* - `download`: an optional function to download the tensor data from the MLTensor to CPU. If omitted, the MLTensor
* data will not be able to download. Usually, this is provided by the WebNN backend for the inference outputs.
* Users don't need to provide this function.
* - `dispose`: an optional function to dispose the tensor data on the WebNN MLTensor. If omitted, the MLTensor will
* not be disposed. Usually, this is provided by the WebNN backend for the inference outputs. Users don't need to
* provide this function.
*
* @returns a tensor object
*/
fromMLTensor<T extends Tensor.MLTensorDataTypes>(tensor: Tensor.MLTensorType, options: TensorFromMLTensorOptions<T>): TypedTensor<T>;
/**
* create a tensor from a pre-allocated buffer. The buffer will be used as a pinned buffer.
*
* @param type - the tensor element type.
* @param buffer - a TypedArray corresponding to the type.
* @param dims - specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*
* @returns a tensor object
*/
fromPinnedBuffer<T extends Exclude<Tensor.Type, 'string'>>(type: T, buffer: Tensor.DataTypeMap[T], dims?: readonly number[]): TypedTensor<T>;
}
export {};
//# sourceMappingURL=tensor-factory.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"tensor-factory.d.ts","sourceRoot":"","sources":["../../lib/tensor-factory.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAElD,MAAM,MAAM,WAAW,GAAG,KAAK,GAAG,MAAM,GAAG,KAAK,GAAG,KAAK,CAAC;AACzD,MAAM,MAAM,iBAAiB,GAAG,MAAM,GAAG,MAAM,CAAC;AAMhD;;GAEG;AACH,UAAU,2BAA2B,CAAC,CAAC,CAAE,SAAQ,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC;IACnE;;OAEG;IACH,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC;CAClB;AAED;;GAEG;AACH,UAAU,gCAAgC,CAAC,CAAC,SAAS,MAAM,CAAC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AAW,CAAC,CAAC,CAAC,CAAC;CACnB"}
@@ -0,0 +1,5 @@
"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
//# sourceMappingURL=tensor-factory.js.map
@@ -0,0 +1 @@
{"version":3,"file":"tensor-factory.js","sourceRoot":"","sources":["../../lib/tensor-factory.ts"],"names":[],"mappings":";AAAA,4DAA4D;AAC5D,kCAAkC"}
@@ -0,0 +1,7 @@
import { Tensor } from './tensor.js';
export type SupportedTypedArrayConstructors = Float32ArrayConstructor | Uint8ArrayConstructor | Int8ArrayConstructor | Uint16ArrayConstructor | Int16ArrayConstructor | Int32ArrayConstructor | BigInt64ArrayConstructor | Uint8ArrayConstructor | Float64ArrayConstructor | Uint32ArrayConstructor | BigUint64ArrayConstructor;
export type SupportedTypedArray = InstanceType<SupportedTypedArrayConstructors>;
export declare const NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP: Map<string, SupportedTypedArrayConstructors>;
export declare const NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP: Map<SupportedTypedArrayConstructors, keyof Tensor.DataTypeMap>;
export declare const checkTypedArray: () => void;
//# sourceMappingURL=tensor-impl-type-mapping.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"tensor-impl-type-mapping.d.ts","sourceRoot":"","sources":["../../lib/tensor-impl-type-mapping.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,MAAM,MAAM,+BAA+B,GACvC,uBAAuB,GACvB,qBAAqB,GACrB,oBAAoB,GACpB,sBAAsB,GACtB,qBAAqB,GACrB,qBAAqB,GACrB,wBAAwB,GACxB,qBAAqB,GACrB,uBAAuB,GACvB,sBAAsB,GACtB,yBAAyB,CAAC;AAC9B,MAAM,MAAM,mBAAmB,GAAG,YAAY,CAAC,+BAA+B,CAAC,CAAC;AAGhF,eAAO,MAAM,qCAAqC,8CAYhD,CAAC;AAGH,eAAO,MAAM,qCAAqC,gEAShD,CAAC;AAMH,eAAO,MAAM,eAAe,YA0B3B,CAAC"}
@@ -0,0 +1,62 @@
"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkTypedArray = exports.NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP = exports.NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP = void 0;
// a runtime map that maps type string to TypedArray constructor. Should match Tensor.DataTypeMap.
exports.NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP = new Map([
['float32', Float32Array],
['uint8', Uint8Array],
['int8', Int8Array],
['uint16', Uint16Array],
['int16', Int16Array],
['int32', Int32Array],
['bool', Uint8Array],
['float64', Float64Array],
['uint32', Uint32Array],
['int4', Uint8Array],
['uint4', Uint8Array],
]);
// a runtime map that maps type string to TypedArray constructor. Should match Tensor.DataTypeMap.
exports.NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP = new Map([
[Float32Array, 'float32'],
[Uint8Array, 'uint8'],
[Int8Array, 'int8'],
[Uint16Array, 'uint16'],
[Int16Array, 'int16'],
[Int32Array, 'int32'],
[Float64Array, 'float64'],
[Uint32Array, 'uint32'],
]);
// the following code allows delaying execution of BigInt/Float16Array checking. This allows lazy initialization for
// NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP and NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP, which allows BigInt/Float16Array
// polyfill if available.
let isTypedArrayChecked = false;
const checkTypedArray = () => {
if (!isTypedArrayChecked) {
isTypedArrayChecked = true;
const isBigInt64ArrayAvailable = typeof BigInt64Array !== 'undefined' && BigInt64Array.from;
const isBigUint64ArrayAvailable = typeof BigUint64Array !== 'undefined' && BigUint64Array.from;
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-explicit-any
const Float16Array = globalThis.Float16Array;
const isFloat16ArrayAvailable = typeof Float16Array !== 'undefined' && Float16Array.from;
if (isBigInt64ArrayAvailable) {
exports.NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set('int64', BigInt64Array);
exports.NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.set(BigInt64Array, 'int64');
}
if (isBigUint64ArrayAvailable) {
exports.NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set('uint64', BigUint64Array);
exports.NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.set(BigUint64Array, 'uint64');
}
if (isFloat16ArrayAvailable) {
exports.NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set('float16', Float16Array);
exports.NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.set(Float16Array, 'float16');
}
else {
// if Float16Array is not available, use 'Uint16Array' to store the data.
exports.NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set('float16', Uint16Array);
}
}
};
exports.checkTypedArray = checkTypedArray;
//# sourceMappingURL=tensor-impl-type-mapping.js.map
@@ -0,0 +1 @@
{"version":3,"file":"tensor-impl-type-mapping.js","sourceRoot":"","sources":["../../lib/tensor-impl-type-mapping.ts"],"names":[],"mappings":";AAAA,4DAA4D;AAC5D,kCAAkC;;;AAkBlC,kGAAkG;AACrF,QAAA,qCAAqC,GAAG,IAAI,GAAG,CAA0C;IACpG,CAAC,SAAS,EAAE,YAAY,CAAC;IACzB,CAAC,OAAO,EAAE,UAAU,CAAC;IACrB,CAAC,MAAM,EAAE,SAAS,CAAC;IACnB,CAAC,QAAQ,EAAE,WAAW,CAAC;IACvB,CAAC,OAAO,EAAE,UAAU,CAAC;IACrB,CAAC,OAAO,EAAE,UAAU,CAAC;IACrB,CAAC,MAAM,EAAE,UAAU,CAAC;IACpB,CAAC,SAAS,EAAE,YAAY,CAAC;IACzB,CAAC,QAAQ,EAAE,WAAW,CAAC;IACvB,CAAC,MAAM,EAAE,UAAU,CAAC;IACpB,CAAC,OAAO,EAAE,UAAU,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AC;QACD,IAAI,uBAAuB,EAAE,CAAC;YAC5B,6CAAqC,CAAC,GAAG,CAAC,SAAS,EAAE,YAAY,CAAC,CAAC;YACnE,6CAAqC,CAAC,GAAG,CAAC,YAAY,EAAE,SAAS,CAAC,CAAC;QACrE,CAAC;aAAM,CAAC;YACN,yEAAyE;YACzE,6CAAqC,CAAC,GAAG,CAAC,SAAS,EAAE,WAAW,CAAC,CAAC;QACpE,CAAC;IACH,CAAC;AACH,CAAC,CAAC;AA1BW,QAAA,eAAe,mBA0B1B"}
+109
View File
@@ -0,0 +1,109 @@
import { TensorToDataUrlOptions, TensorToImageDataOptions } from './tensor-conversion.js';
import { CpuPinnedConstructorParameters, GpuBufferConstructorParameters, MLTensorConstructorParameters, TensorFromGpuBufferOptions, TensorFromImageBitmapOptions, TensorFromImageDataOptions, TensorFromImageElementOptions, TensorFromMLTensorOptions, TensorFromTextureOptions, TensorFromUrlOptions, TextureConstructorParameters } from './tensor-factory.js';
import { Tensor as TensorInterface } from './tensor.js';
type TensorType = TensorInterface.Type;
type TensorDataType = TensorInterface.DataType;
type TensorDataLocation = TensorInterface.DataLocation;
type TensorTextureType = TensorInterface.TextureType;
type TensorGpuBufferType = TensorInterface.GpuBufferType;
type TensorMLTensorType = TensorInterface.MLTensorType;
/**
* the implementation of Tensor interface.
*
* @ignore
*/
export declare class Tensor implements TensorInterface {
/**
* Construct a new CPU tensor object from the given type, data and dims.
*/
constructor(type: TensorType, data: TensorDataType | Uint8ClampedArray | readonly string[] | readonly number[] | readonly boolean[], dims?: readonly number[]);
/**
* Construct a new CPU tensor object from the given data and dims. Type is inferred from data.
*/
constructor(data: TensorDataType | Uint8ClampedArray | readonly string[] | readonly boolean[], dims?: readonly number[]);
/**
* Construct a new tensor object from the pinned CPU data with the given type and dims.
*
* Tensor's location will be set to 'cpu-pinned'.
*
* @param params - Specify the parameters to construct the tensor.
*/
constructor(params: CpuPinnedConstructorParameters);
/**
* Construct a new tensor object from the WebGL texture with the given type and dims.
*
* Tensor's location will be set to 'texture'.
*
* @param params - Specify the parameters to construct the tensor.
*/
constructor(params: TextureConstructorParameters);
/**
* Construct a new tensor object from the WebGPU buffer with the given type and dims.
*
* Tensor's location will be set to 'gpu-buffer'.
*
* @param params - Specify the parameters to construct the tensor.
*/
constructor(params: GpuBufferConstructorParameters);
/**
* Construct a new tensor object from the WebNN MLTensor with the given type and dims.
*
* Tensor's location will be set to 'ml-tensor'.
*
* @param params - Specify the parameters to construct the tensor.
*/
constructor(params: MLTensorConstructorParameters);
static fromImage(image: ImageData | HTMLImageElement | ImageBitmap | string, options?: TensorFromImageDataOptions | TensorFromImageElementOptions | TensorFromImageBitmapOptions | TensorFromUrlOptions): Promise<TensorInterface>;
static fromTexture<T extends TensorInterface.TextureDataTypes>(texture: TensorTextureType, options: TensorFromTextureOptions<T>): TensorInterface;
static fromGpuBuffer<T extends TensorInterface.GpuBufferDataTypes>(gpuBuffer: TensorGpuBufferType, options: TensorFromGpuBufferOptions<T>): TensorInterface;
static fromMLTensor<T extends TensorInterface.MLTensorDataTypes>(mlTensor: TensorMLTensorType, options: TensorFromMLTensorOptions<T>): TensorInterface;
static fromPinnedBuffer<T extends TensorInterface.CpuPinnedDataTypes>(type: T, buffer: TensorInterface.DataTypeMap[T], dims?: readonly number[]): Tensor;
toDataURL(options?: TensorToDataUrlOptions): string;
toImageData(options?: TensorToImageDataOptions): ImageData;
readonly dims: readonly number[];
readonly type: TensorType;
readonly size: number;
/**
* stores the location of the data.
*/
private dataLocation;
/**
* stores the data on CPU, if location is 'cpu' or 'cpu-pinned'. otherwise empty.
*/
private cpuData?;
/**
* stores the underlying texture when location is 'texture'. otherwise empty.
*/
private gpuTextureData?;
/**
* stores the underlying GPU buffer when location is 'gpu-buffer'. otherwise empty.
*/
private gpuBufferData?;
/**
* stores the underlying WebNN MLTensor when location is 'ml-tensor'. otherwise empty.
*/
private mlTensorData?;
/**
* stores an optional downloader function to download data from GPU to CPU.
*/
private downloader;
/**
* a flag indicating whether the data is being downloaded from GPU to CPU.
*/
private isDownloading?;
/**
* stores an optional disposer function to dispose the underlying data.
*/
private disposer;
get data(): TensorDataType;
get location(): TensorDataLocation;
get texture(): TensorTextureType;
get gpuBuffer(): TensorGpuBufferType;
get mlTensor(): TensorMLTensorType;
getData(releaseData?: boolean): Promise<TensorDataType>;
dispose(): void;
private ensureValid;
reshape(dims: readonly number[]): TensorInterface;
}
export {};
//# sourceMappingURL=tensor-impl.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"tensor-impl.d.ts","sourceRoot":"","sources":["../../lib/tensor-impl.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,sBAAsB,EAAE,wBAAwB,EAAE,MAAM,wBAAwB,CAAC;AAQ1F,OAAO,EACL,8BAA8B,EAC9B,8BAA8B,EAC9B,6BAA6B,EAC7B,0BAA0B,EAC1B,4BAA4B,EAC5B,0BAA0B,EAC1B,6BAA6B,EAC7B,yBAAyB,EACzB,wBAAwB,EACxB,oBAAoB,EACpB,4BAA4B,EAC7B,MAAM,qBAAqB,CAAC;AAS7B,OAAO,EAAE,MAAM,IAAI,eAAe,EAAE,MAAM,aAAa,CAAC;AAIxD,KAAK,UAAU,GAAG,eAAe,CAAC,IAAI,CAAC;AACvC,KAAK,cAAc,GAAG,eAAe,CAAC,QAAQ,CAAC;AAC/C,KAAK,kBAAkB,GAAG,eAAe,CAAC,YAAY,CAAC;AACvD,KAAK,iBAAiB,GAAG,eAAe,CAAC,WAAW,CAAC;AACrD,KAAK,mBAAmB,GAAG,eAAe,CAAC,aAAa,CAAC;AACzD,KAAK,kBAAkB,GAAG,eAAe,CAAC,YAAY,CAAC;AAEvD;;;;GAIG;AACH,qBAAa,MAAO,YAAW,eAAe;IAG5C;;OAEG;gBAED,IAAI,EAAE,UAAU,EAChB,IAAI,EAAE,cAAc,GAAG,iBAAiB,GAAG,SAAS,MAAM,EAAE,GAAG,SAAS,MAAM,EAAE,GAAG,SAAS,OAAO,EAAE,EACrG,IAAI,CAAC,EAA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;IAInD,WAAW,CAAC,OAAO,CAAC,EAAE,wBAAwB,GAAG,SAAS;IAM1D,QAAQ,CAAC,IAAI,EAAE,SAAS,MAAM,EAAE,CAAC;IACjC,QAAQ,CAAC,IAAI,EAAE,UAAU,CAAC;IAC1B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAKtB;;OAEG;IACH,OAAO,CAAC,YAAY,CAAqB;IAEzC;;OAEG;IACH,OAAO,CAAC,OAAO,CAAC,CAAiB;IAEjC;;OAEG;IACH,OAAO,CAAC,cAAc,CAAC,CAAoB;IAE3C;;OAEG;IACH,OAAO,CAAC,aAAa,CAAC,CAAsB;IAE5C;;OAEG;IACH,OAAO,CAAC,YAAY,CAAC,CAAqB;IAE1C;;OAEG;IACH,OAAO,CAAC,UAAU;IAElB;;OAEG;IACH,OAAO,CAAC,aAAa,CAAC,CAAU;IAEhC;;OAEG;IACH,OAAO,CAAC,QAAQ;IAIhB,IAAI,IAAI,IAAI,cAAc,CASzB;IAED,IAAI,QAAQ,IAAI,kBAAkB,CAEjC;IAED,IAAI,OAAO,IAAI,iBAAiB,CAM/B;IAED,IAAI,SAAS,IAAI,mBAAmB,CAMnC;IAED,IAAI,QAAQ,IAAI,kBAAkB,CAMjC;IAKK,OAAO,CAAC,WAAW,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,cAAc,CAAC;IAqC7D,OAAO,IAAI,IAAI;IAsBf,OAAO,CAAC,WAAW;IAMnB,OAAO,CAAC,IAAI,EAAE,SAAS,MAAM,EAAE,GAAG,eAAe;CAQlD"}
+370
View File
@@ -0,0 +1,370 @@
"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.Tensor = void 0;
const tensor_conversion_impl_js_1 = require("./tensor-conversion-impl.js");
const tensor_factory_impl_js_1 = require("./tensor-factory-impl.js");
const tensor_impl_type_mapping_js_1 = require("./tensor-impl-type-mapping.js");
const tensor_utils_impl_js_1 = require("./tensor-utils-impl.js");
/**
* the implementation of Tensor interface.
*
* @ignore
*/
class Tensor {
/**
* implementation.
*/
constructor(arg0, arg1, arg2) {
// perform one-time check for BigInt/Float16Array support
(0, tensor_impl_type_mapping_js_1.checkTypedArray)();
let type;
let dims;
if (typeof arg0 === 'object' && 'location' in arg0) {
//
// constructing tensor from specific location
//
this.dataLocation = arg0.location;
type = arg0.type;
dims = arg0.dims;
switch (arg0.location) {
case 'cpu-pinned': {
const expectedTypedArrayConstructor = tensor_impl_type_mapping_js_1.NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.get(type);
if (!expectedTypedArrayConstructor) {
throw new TypeError(`unsupported type "${type}" to create tensor from pinned buffer`);
}
if (!(arg0.data instanceof expectedTypedArrayConstructor)) {
throw new TypeError(`buffer should be of type ${expectedTypedArrayConstructor.name}`);
}
this.cpuData = arg0.data;
break;
}
case 'texture': {
if (type !== 'float32') {
throw new TypeError(`unsupported type "${type}" to create tensor from texture`);
}
this.gpuTextureData = arg0.texture;
this.downloader = arg0.download;
this.disposer = arg0.dispose;
break;
}
case 'gpu-buffer': {
if (type !== 'float32' &&
type !== 'float16' &&
type !== 'int32' &&
type !== 'int64' &&
type !== 'uint32' &&
type !== 'uint8' &&
type !== 'bool' &&
type !== 'uint4' &&
type !== 'int4') {
throw new TypeError(`unsupported type "${type}" to create tensor from gpu buffer`);
}
this.gpuBufferData = arg0.gpuBuffer;
this.downloader = arg0.download;
this.disposer = arg0.dispose;
break;
}
case 'ml-tensor': {
if (type !== 'float32' &&
type !== 'float16' &&
type !== 'int32' &&
type !== 'int64' &&
type !== 'uint32' &&
type !== 'uint64' &&
type !== 'int8' &&
type !== 'uint8' &&
type !== 'bool' &&
type !== 'uint4' &&
type !== 'int4') {
throw new TypeError(`unsupported type "${type}" to create tensor from MLTensor`);
}
this.mlTensorData = arg0.mlTensor;
this.downloader = arg0.download;
this.disposer = arg0.dispose;
break;
}
default:
throw new Error(`Tensor constructor: unsupported location '${this.dataLocation}'`);
}
}
else {
//
// constructing tensor of location 'cpu'
//
let data;
let maybeDims;
// check whether arg0 is type or data
if (typeof arg0 === 'string') {
//
// Override: constructor(type, data, ...)
//
type = arg0;
maybeDims = arg2;
if (arg0 === 'string') {
// string tensor
if (!Array.isArray(arg1)) {
throw new TypeError("A string tensor's data must be a string array.");
}
// we don't check whether every element in the array is string; this is too slow. we assume it's correct and
// error will be populated at inference
data = arg1;
}
else {
// numeric tensor
const typedArrayConstructor = tensor_impl_type_mapping_js_1.NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.get(arg0);
if (typedArrayConstructor === undefined) {
throw new TypeError(`Unsupported tensor type: ${arg0}.`);
}
if (Array.isArray(arg1)) {
if ((arg0 === 'float16' && typedArrayConstructor === Uint16Array) || arg0 === 'uint4' || arg0 === 'int4') {
// - 'float16':
// When no Float16Array polyfill is used, we cannot create 'float16' tensor from number array.
//
// Throw error here because when user try to use number array as data,
// e.g. new Tensor('float16', [1, 2, 3, 4], dims)), it will actually call
// Uint16Array.from(arg1) which generates wrong data.
//
// - 'uint4' and 'int4':
// Uint8Array.from(arg1) will generate wrong data for 'uint4' and 'int4' tensor.
//
throw new TypeError(`Creating a ${arg0} tensor from number array is not supported. Please use ${typedArrayConstructor.name} as data.`);
}
else if (arg0 === 'uint64' || arg0 === 'int64') {
// use 'as any' here because:
// 1. TypeScript's check on type of 'Array.isArray()' does not work with readonly arrays.
// see https://github.com/microsoft/TypeScript/issues/17002
// 2. TypeScript's check on union type of '(BigInt64ArrayConstructor|BigUint64ArrayConstructor).from()'
// does not accept parameter mapFn.
// 3. parameters of 'SupportedTypedArrayConstructors.from()' does not match the requirement of the union
// type.
// assume 'arg1' is of type "readonly number[]|readonly bigint[]" here.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
data = typedArrayConstructor.from(arg1, BigInt);
}
else {
// assume 'arg1' is of type "readonly number[]" here.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
data = typedArrayConstructor.from(arg1);
}
}
else if (arg1 instanceof typedArrayConstructor) {
data = arg1;
}
else if (arg1 instanceof Uint8ClampedArray) {
if (arg0 === 'uint8') {
data = Uint8Array.from(arg1);
}
else {
throw new TypeError(`A Uint8ClampedArray tensor's data must be type of uint8`);
}
}
else if (arg0 === 'float16' && arg1 instanceof Uint16Array && typedArrayConstructor !== Uint16Array) {
// when Float16Array is available and data is of type Uint16Array.
// We allow Uint16Array to be passed in as data for 'float16' tensor until Float16Array is generally
// supported in JavaScript environment.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
data = new globalThis.Float16Array(arg1.buffer, arg1.byteOffset, arg1.length);
}
else {
throw new TypeError(`A ${type} tensor's data must be type of ${typedArrayConstructor}`);
}
}
}
else {
//
// Override: constructor(data, ...)
//
maybeDims = arg1;
if (Array.isArray(arg0)) {
// only boolean[] and string[] is supported
if (arg0.length === 0) {
throw new TypeError('Tensor type cannot be inferred from an empty array.');
}
const firstElementType = typeof arg0[0];
if (firstElementType === 'string') {
type = 'string';
data = arg0;
}
else if (firstElementType === 'boolean') {
type = 'bool';
// 'arg0' is of type 'boolean[]'. Uint8Array.from(boolean[]) actually works, but typescript thinks this is
// wrong type. We use 'as any' to make it happy.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
data = Uint8Array.from(arg0);
}
else {
throw new TypeError(`Invalid element type of data array: ${firstElementType}.`);
}
}
else if (arg0 instanceof Uint8ClampedArray) {
type = 'uint8';
data = Uint8Array.from(arg0);
}
else {
// get tensor type from TypedArray
const mappedType = tensor_impl_type_mapping_js_1.NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.get(arg0.constructor);
if (mappedType === undefined) {
throw new TypeError(`Unsupported type for tensor data: ${arg0.constructor}.`);
}
type = mappedType;
data = arg0;
}
}
// type and data is processed, now processing dims
if (maybeDims === undefined) {
// assume 1-D tensor if dims omitted
maybeDims = [data.length];
}
else if (!Array.isArray(maybeDims)) {
throw new TypeError("A tensor's dims must be a number array");
}
dims = maybeDims;
this.cpuData = data;
this.dataLocation = 'cpu';
}
// perform check on dims
const size = (0, tensor_utils_impl_js_1.calculateSize)(dims);
// if data is on CPU, check whether data length matches tensor size
if (this.cpuData && size !== this.cpuData.length) {
if ((type === 'uint4' || type === 'int4') && Math.ceil(size / 2) === this.cpuData.length) {
// for (u)int4, the data length is half of the tensor size. So we check this special case when size is odd.
}
else {
throw new Error(`Tensor's size(${size}) does not match data length(${this.cpuData.length}).`);
}
}
this.type = type;
this.dims = dims;
this.size = size;
}
// #endregion
// #region factory
static async fromImage(image, options) {
return (0, tensor_factory_impl_js_1.tensorFromImage)(image, options);
}
static fromTexture(texture, options) {
return (0, tensor_factory_impl_js_1.tensorFromTexture)(texture, options);
}
static fromGpuBuffer(gpuBuffer, options) {
return (0, tensor_factory_impl_js_1.tensorFromGpuBuffer)(gpuBuffer, options);
}
static fromMLTensor(mlTensor, options) {
return (0, tensor_factory_impl_js_1.tensorFromMLTensor)(mlTensor, options);
}
static fromPinnedBuffer(type, buffer, dims) {
return (0, tensor_factory_impl_js_1.tensorFromPinnedBuffer)(type, buffer, dims);
}
// #endregion
// #region conversions
toDataURL(options) {
return (0, tensor_conversion_impl_js_1.tensorToDataURL)(this, options);
}
toImageData(options) {
return (0, tensor_conversion_impl_js_1.tensorToImageData)(this, options);
}
// #endregion
// #region properties
get data() {
this.ensureValid();
if (!this.cpuData) {
throw new Error('The data is not on CPU. Use `getData()` to download GPU data to CPU, ' +
'or use `texture` or `gpuBuffer` property to access the GPU data directly.');
}
return this.cpuData;
}
get location() {
return this.dataLocation;
}
get texture() {
this.ensureValid();
if (!this.gpuTextureData) {
throw new Error('The data is not stored as a WebGL texture.');
}
return this.gpuTextureData;
}
get gpuBuffer() {
this.ensureValid();
if (!this.gpuBufferData) {
throw new Error('The data is not stored as a WebGPU buffer.');
}
return this.gpuBufferData;
}
get mlTensor() {
this.ensureValid();
if (!this.mlTensorData) {
throw new Error('The data is not stored as a WebNN MLTensor.');
}
return this.mlTensorData;
}
// #endregion
// #region methods
async getData(releaseData) {
this.ensureValid();
switch (this.dataLocation) {
case 'cpu':
case 'cpu-pinned':
return this.data;
case 'texture':
case 'gpu-buffer':
case 'ml-tensor': {
if (!this.downloader) {
throw new Error('The current tensor is not created with a specified data downloader.');
}
if (this.isDownloading) {
throw new Error('The current tensor is being downloaded.');
}
try {
this.isDownloading = true;
const data = await this.downloader();
this.downloader = undefined;
this.dataLocation = 'cpu';
this.cpuData = data;
if (releaseData && this.disposer) {
this.disposer();
this.disposer = undefined;
}
return data;
}
finally {
this.isDownloading = false;
}
}
default:
throw new Error(`cannot get data from location: ${this.dataLocation}`);
}
}
dispose() {
if (this.isDownloading) {
throw new Error('The current tensor is being downloaded.');
}
if (this.disposer) {
this.disposer();
this.disposer = undefined;
}
this.cpuData = undefined;
this.gpuTextureData = undefined;
this.gpuBufferData = undefined;
this.mlTensorData = undefined;
this.downloader = undefined;
this.isDownloading = undefined;
this.dataLocation = 'none';
}
// #endregion
// #region tensor utilities
ensureValid() {
if (this.dataLocation === 'none') {
throw new Error('The tensor is disposed.');
}
}
reshape(dims) {
this.ensureValid();
if (this.downloader || this.disposer) {
throw new Error('Cannot reshape a tensor that owns GPU resource.');
}
return (0, tensor_utils_impl_js_1.tensorReshape)(this, dims);
}
}
exports.Tensor = Tensor;
//# sourceMappingURL=tensor-impl.js.map
File diff suppressed because one or more lines are too long
@@ -0,0 +1,12 @@
import { Tensor } from './tensor-impl.js';
/**
* calculate size from dims.
*
* @param dims the dims array. May be an illegal input.
*/
export declare const calculateSize: (dims: readonly unknown[]) => number;
/**
* implementation of Tensor.reshape()
*/
export declare const tensorReshape: (tensor: Tensor, dims: readonly number[]) => Tensor;
//# sourceMappingURL=tensor-utils-impl.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"tensor-utils-impl.d.ts","sourceRoot":"","sources":["../../lib/tensor-utils-impl.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAE1C;;;;GAIG;AACH,eAAO,MAAM,aAAa,GAAI,MAAM,SAAS,OAAO,EAAE,KAAG,MAaxD,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,aAAa,GAAI,QAAQ,MAAM,EAAE,MAAM,SAAS,MAAM,EAAE,KAAG,MAmCvE,CAAC"}
@@ -0,0 +1,67 @@
"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.tensorReshape = exports.calculateSize = void 0;
const tensor_impl_js_1 = require("./tensor-impl.js");
/**
* calculate size from dims.
*
* @param dims the dims array. May be an illegal input.
*/
const calculateSize = (dims) => {
let size = 1;
for (let i = 0; i < dims.length; i++) {
const dim = dims[i];
if (typeof dim !== 'number' || !Number.isSafeInteger(dim)) {
throw new TypeError(`dims[${i}] must be an integer, got: ${dim}`);
}
if (dim < 0) {
throw new RangeError(`dims[${i}] must be a non-negative integer, got: ${dim}`);
}
size *= dim;
}
return size;
};
exports.calculateSize = calculateSize;
/**
* implementation of Tensor.reshape()
*/
const tensorReshape = (tensor, dims) => {
switch (tensor.location) {
case 'cpu':
return new tensor_impl_js_1.Tensor(tensor.type, tensor.data, dims);
case 'cpu-pinned':
return new tensor_impl_js_1.Tensor({
location: 'cpu-pinned',
data: tensor.data,
type: tensor.type,
dims,
});
case 'texture':
return new tensor_impl_js_1.Tensor({
location: 'texture',
texture: tensor.texture,
type: tensor.type,
dims,
});
case 'gpu-buffer':
return new tensor_impl_js_1.Tensor({
location: 'gpu-buffer',
gpuBuffer: tensor.gpuBuffer,
type: tensor.type,
dims,
});
case 'ml-tensor':
return new tensor_impl_js_1.Tensor({
location: 'ml-tensor',
mlTensor: tensor.mlTensor,
type: tensor.type,
dims,
});
default:
throw new Error(`tensorReshape: tensor location ${tensor.location} is not supported`);
}
};
exports.tensorReshape = tensorReshape;
//# sourceMappingURL=tensor-utils-impl.js.map
@@ -0,0 +1 @@
{"version":3,"file":"tensor-utils-impl.js","sourceRoot":"","sources":["../../lib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AQ,EAAE,SAAS;gBACnB,OAAO,EAAE,MAAM,CAAC,OAAO;gBACvB,IAAI,EAAE,MAAM,CAAC,IAA4C;gBACzD,IAAI;aACL,CAAC,CAAC;QACL,KAAK,YAAY;YACf,OAAO,IAAI,uBAAM,CAAC;gBAChB,QAAQ,EAAE,YAAY;gBACtB,SAAS,EAAE,MAAM,CAAC,SAAS;gBAC3B,IAAI,EAAE,MAAM,CAAC,IAA8C;gBAC3D,IAAI;aACL,CAAC,CAAC;QACL,KAAK,WAAW;YACd,OAAO,IAAI,uBAAM,CAAC;gBAChB,QAAQ,EAAE,WAAW;gBACrB,QAAQ,EAAE,MAAM,CAAC,QAAQ;gBACzB,IAAI,EAAE,MAAM,CAAC,IAA6C;gBAC1D,IAAI;aACL,CAAC,CAAC;QACL;YACE,MAAM,IAAI,KAAK,CAAC,kCAAkC,MAAM,CAAC,QAAQ,mBAAmB,CAAC,CAAC;IAC1F,CAAC;AACH,CAAC,CAAC;AAnCW,QAAA,aAAa,iBAmCxB"}
@@ -0,0 +1,28 @@
import { ConversionUtils } from './tensor-conversion.js';
import { Tensor, TypedTensor } from './tensor.js';
interface Properties {
/**
* Get the number of elements in the tensor.
*/
readonly size: number;
}
export interface TypedShapeUtils<T extends Tensor.Type> {
/**
* Create a new tensor with the same data buffer and specified dims.
*
* @param dims - New dimensions. Size should match the old one.
*/
reshape(dims: readonly number[]): TypedTensor<T>;
}
/**
* interface `TensorUtils` includes all utility members that does not use the type parameter from their signature.
*/
export interface TensorUtils extends Properties, ConversionUtils {
}
/**
* interface `TypedShapeUtils` includes all utility members that uses the type parameter from their signature.
*/
export interface TypedTensorUtils<T extends Tensor.Type> extends TensorUtils, TypedShapeUtils<T> {
}
export {};
//# sourceMappingURL=tensor-utils.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"tensor-utils.d.ts","sourceRoot":"","sources":["../../lib/tensor-utils.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAElD,UAAU,UAAU;IAClB;;OAEG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,eAAe,CAAC,CAAC,SAAS,MAAM,CAAC,IAAI;IACpD;;;;OAIG;IACH,OAAO,CAAC,IAAI,EAAE,SAAS,MAAM,EAAE,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC;CAClD;AAED;;GAEG;AACH,MAAM,WAAW,WAAY,SAAQ,UAAU,EAAE,eAAe;CAAG;AAEnE;;GAEG;AACH,MAAM,WAAW,gBAAgB,CAAC,CAAC,SAAS,MAAM,CAAC,IAAI,CAAE,SAAQ,WAAW,EAAE,eAAe,CAAC,CAAC,CAAC;CAAG"}
@@ -0,0 +1,5 @@
"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
//# sourceMappingURL=tensor-utils.js.map
@@ -0,0 +1 @@
{"version":3,"file":"tensor-utils.js","sourceRoot":"","sources":["../../lib/tensor-utils.ts"],"names":[],"mappings":";AAAA,4DAA4D;AAC5D,kCAAkC"}
+309
View File
@@ -0,0 +1,309 @@
import { TensorFactory } from './tensor-factory.js';
import { TypedTensorUtils } from './tensor-utils.js';
import { TryGetGlobalType } from './type-helper.js';
/**
* represent a basic tensor with specified dimensions and data type.
*/
interface TypedTensorBase<T extends Tensor.Type> {
/**
* Get the dimensions of the tensor.
*/
readonly dims: readonly number[];
/**
* Get the data type of the tensor.
*/
readonly type: T;
/**
* Get the buffer data of the tensor.
*
* If the data is not on CPU (eg. it's in the form of WebGL texture or WebGPU buffer), throw error.
*/
readonly data: Tensor.DataTypeMap[T];
/**
* Get the location of the data.
*/
readonly location: Tensor.DataLocation;
/**
* Get the WebGL texture that holds the tensor data.
*
* If the data is not on GPU as WebGL texture, throw error.
*/
readonly texture: Tensor.TextureType;
/**
* Get the WebGPU buffer that holds the tensor data.
*
* If the data is not on GPU as WebGPU buffer, throw error.
*/
readonly gpuBuffer: Tensor.GpuBufferType;
/**
* Get the WebNN MLTensor that holds the tensor data.
*
* If the data is not in a WebNN MLTensor, throw error.
*/
readonly mlTensor: Tensor.MLTensorType;
/**
* Get the buffer data of the tensor.
*
* If the data is on CPU, returns the data immediately.
* If the data is on GPU, downloads the data and returns the promise.
*
* @param releaseData - whether release the data on GPU. Ignore if data is already on CPU.
*/
getData(releaseData?: boolean): Promise<Tensor.DataTypeMap[T]>;
/**
* Dispose the tensor data.
*
* If the data is on CPU, remove its internal reference to the underlying data.
* If the data is on GPU, release the data on GPU.
*
* After calling this function, the tensor is considered no longer valid. Its location will be set to 'none'.
*/
dispose(): void;
}
export declare namespace Tensor {
interface DataTypeMap {
float32: Float32Array;
uint8: Uint8Array;
int8: Int8Array;
uint16: Uint16Array;
int16: Int16Array;
int32: Int32Array;
int64: BigInt64Array;
string: string[];
bool: Uint8Array;
float16: Uint16Array;
float64: Float64Array;
uint32: Uint32Array;
uint64: BigUint64Array;
uint4: Uint8Array;
int4: Int8Array;
}
interface ElementTypeMap {
float32: number;
uint8: number;
int8: number;
uint16: number;
int16: number;
int32: number;
int64: bigint;
string: string;
bool: boolean;
float16: number;
float64: number;
uint32: number;
uint64: bigint;
uint4: number;
int4: number;
}
type DataType = DataTypeMap[Type];
type ElementType = ElementTypeMap[Type];
/**
* supported data types for constructing a tensor from a pinned CPU buffer
*/
type CpuPinnedDataTypes = Exclude<Tensor.Type, 'string'>;
/**
* type alias for WebGL texture
*/
type TextureType = WebGLTexture;
/**
* supported data types for constructing a tensor from a WebGL texture
*/
type TextureDataTypes = 'float32';
type GpuBufferTypeFallback = {
size: number;
mapState: 'unmapped' | 'pending' | 'mapped';
};
/**
* type alias for WebGPU buffer
*/
type GpuBufferType = TryGetGlobalType<'GPUBuffer', GpuBufferTypeFallback>;
type MLTensorTypeFallback = {
destroy(): void;
};
/**
* type alias for WebNN MLTensor
*
* The specification for WebNN's MLTensor is currently in flux.
*/
type MLTensorType = TryGetGlobalType<'MLTensor', MLTensorTypeFallback>;
/**
* supported data types for constructing a tensor from a WebGPU buffer
*/
type GpuBufferDataTypes = 'float32' | 'float16' | 'int32' | 'int64' | 'uint32' | 'uint8' | 'bool';
/**
* supported data types for constructing a tensor from a WebNN MLTensor
*/
type MLTensorDataTypes = 'float32' | 'float16' | 'int8' | 'uint8' | 'int32' | 'uint32' | 'int64' | 'uint64' | 'bool' | 'uint4' | 'int4';
/**
* represent where the tensor data is stored
*/
type DataLocation = 'none' | 'cpu' | 'cpu-pinned' | 'texture' | 'gpu-buffer' | 'ml-tensor';
/**
* represent the data type of a tensor
*/
type Type = keyof DataTypeMap;
}
/**
* Represent multi-dimensional arrays to feed to or fetch from model inferencing.
*/
export interface TypedTensor<T extends Tensor.Type> extends TypedTensorBase<T>, TypedTensorUtils<T> {
}
/**
* Represent multi-dimensional arrays to feed to or fetch from model inferencing.
*/
export interface Tensor extends TypedTensorBase<Tensor.Type>, TypedTensorUtils<Tensor.Type> {
}
/**
* type TensorConstructor defines the constructors of 'Tensor' to create CPU tensor instances.
*/
export interface TensorConstructor extends TensorFactory {
/**
* Construct a new string tensor object from the given type, data and dims.
*
* @param type - Specify the element type.
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (type: 'string', data: Tensor.DataTypeMap['string'] | readonly string[], dims?: readonly number[]): TypedTensor<'string'>;
/**
* Construct a new bool tensor object from the given type, data and dims.
*
* @param type - Specify the element type.
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (type: 'bool', data: Tensor.DataTypeMap['bool'] | readonly boolean[], dims?: readonly number[]): TypedTensor<'bool'>;
/**
* Construct a new uint8 tensor object from a Uint8ClampedArray, data and dims.
*
* @param type - Specify the element type.
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (type: 'uint8', data: Uint8ClampedArray, dims?: readonly number[]): TypedTensor<'uint8'>;
/**
* Construct a new 64-bit integer typed tensor object from the given type, data and dims.
*
* @param type - Specify the element type.
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new <T extends 'uint64' | 'int64'>(type: T, data: Tensor.DataTypeMap[T] | readonly bigint[] | readonly number[], dims?: readonly number[]): TypedTensor<T>;
/**
* Construct a new numeric tensor object from the given type, data and dims.
*
* @param type - Specify the element type.
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new <T extends Exclude<Tensor.Type, 'string' | 'bool' | 'uint64' | 'int64'>>(type: T, data: Tensor.DataTypeMap[T] | readonly number[], dims?: readonly number[]): TypedTensor<T>;
/**
* Construct a new float32 tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: Float32Array, dims?: readonly number[]): TypedTensor<'float32'>;
/**
* Construct a new int8 tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: Int8Array, dims?: readonly number[]): TypedTensor<'int8'>;
/**
* Construct a new uint8 tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: Uint8Array, dims?: readonly number[]): TypedTensor<'uint8'>;
/**
* Construct a new uint8 tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: Uint8ClampedArray, dims?: readonly number[]): TypedTensor<'uint8'>;
/**
* Construct a new uint16 tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: Uint16Array, dims?: readonly number[]): TypedTensor<'uint16'>;
/**
* Construct a new int16 tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: Int16Array, dims?: readonly number[]): TypedTensor<'int16'>;
/**
* Construct a new int32 tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: Int32Array, dims?: readonly number[]): TypedTensor<'int32'>;
/**
* Construct a new int64 tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: BigInt64Array, dims?: readonly number[]): TypedTensor<'int64'>;
/**
* Construct a new string tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: readonly string[], dims?: readonly number[]): TypedTensor<'string'>;
/**
* Construct a new bool tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: readonly boolean[], dims?: readonly number[]): TypedTensor<'bool'>;
/**
* Construct a new float64 tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: Float64Array, dims?: readonly number[]): TypedTensor<'float64'>;
/**
* Construct a new uint32 tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: Uint32Array, dims?: readonly number[]): TypedTensor<'uint32'>;
/**
* Construct a new uint64 tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: BigUint64Array, dims?: readonly number[]): TypedTensor<'uint64'>;
/**
* Construct a new tensor object from the given type, data and dims.
*
* @param type - Specify the element type.
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (type: Tensor.Type, data: Tensor.DataType | readonly number[] | readonly string[] | readonly bigint[] | readonly boolean[], dims?: readonly number[]): Tensor;
/**
* Construct a new tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: Tensor.DataType, dims?: readonly number[]): Tensor;
}
export declare const Tensor: TensorConstructor;
export {};
//# sourceMappingURL=tensor.d.ts.map
File diff suppressed because one or more lines are too long
+9
View File
@@ -0,0 +1,9 @@
"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.Tensor = void 0;
const tensor_impl_js_1 = require("./tensor-impl.js");
// eslint-disable-next-line @typescript-eslint/naming-convention
exports.Tensor = tensor_impl_js_1.Tensor;
//# sourceMappingURL=tensor.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"tensor.js","sourceRoot":"","sources":["../../lib/tensor.ts"],"names":[],"mappings":";AAAA,4DAA4D;AAC5D,kCAAkC;;;AAGlC,qDAAwD;AAiYxD,gEAAgE;AACnD,QAAA,MAAM,GAAG,uBAA+B,CAAC"}
+21
View File
@@ -0,0 +1,21 @@
/**
* @ignore
*/
export declare const TRACE: (deviceType: string, label: string) => void;
/**
* @ignore
*/
export declare const TRACE_FUNC_BEGIN: (extraMsg?: string) => void;
/**
* @ignore
*/
export declare const TRACE_FUNC_END: (extraMsg?: string) => void;
/**
* @ignore
*/
export declare const TRACE_EVENT_BEGIN: (extraMsg?: string) => void;
/**
* @ignore
*/
export declare const TRACE_EVENT_END: (extraMsg?: string) => void;
//# sourceMappingURL=trace.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"trace.d.ts","sourceRoot":"","sources":["../../lib/trace.ts"],"names":[],"mappings":"AAKA;;GAEG;AACH,eAAO,MAAM,KAAK,GAAI,YAAY,MAAM,EAAE,OAAO,MAAM,SAMtD,CAAC;AAoBF;;GAEG;AACH,eAAO,MAAM,gBAAgB,GAAI,WAAW,MAAM,SAKjD,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,cAAc,GAAI,WAAW,MAAM,SAK/C,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,iBAAiB,GAAI,WAAW,MAAM,SAMlD,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,eAAe,GAAI,WAAW,MAAM,SAMhD,CAAC"}
+77
View File
@@ -0,0 +1,77 @@
"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.TRACE_EVENT_END = exports.TRACE_EVENT_BEGIN = exports.TRACE_FUNC_END = exports.TRACE_FUNC_BEGIN = exports.TRACE = void 0;
const env_impl_js_1 = require("./env-impl.js");
/**
* @ignore
*/
const TRACE = (deviceType, label) => {
if (typeof env_impl_js_1.env.trace === 'undefined' ? !env_impl_js_1.env.wasm.trace : !env_impl_js_1.env.trace) {
return;
}
// eslint-disable-next-line no-console
console.timeStamp(`${deviceType}::ORT::${label}`);
};
exports.TRACE = TRACE;
const TRACE_FUNC = (msg, extraMsg) => {
const stack = new Error().stack?.split(/\r\n|\r|\n/g) || [];
let hasTraceFunc = false;
for (let i = 0; i < stack.length; i++) {
if (hasTraceFunc && !stack[i].includes('TRACE_FUNC')) {
let label = `FUNC_${msg}::${stack[i].trim().split(' ')[1]}`;
if (extraMsg) {
label += `::${extraMsg}`;
}
(0, exports.TRACE)('CPU', label);
return;
}
if (stack[i].includes('TRACE_FUNC')) {
hasTraceFunc = true;
}
}
};
/**
* @ignore
*/
const TRACE_FUNC_BEGIN = (extraMsg) => {
if (typeof env_impl_js_1.env.trace === 'undefined' ? !env_impl_js_1.env.wasm.trace : !env_impl_js_1.env.trace) {
return;
}
TRACE_FUNC('BEGIN', extraMsg);
};
exports.TRACE_FUNC_BEGIN = TRACE_FUNC_BEGIN;
/**
* @ignore
*/
const TRACE_FUNC_END = (extraMsg) => {
if (typeof env_impl_js_1.env.trace === 'undefined' ? !env_impl_js_1.env.wasm.trace : !env_impl_js_1.env.trace) {
return;
}
TRACE_FUNC('END', extraMsg);
};
exports.TRACE_FUNC_END = TRACE_FUNC_END;
/**
* @ignore
*/
const TRACE_EVENT_BEGIN = (extraMsg) => {
if (typeof env_impl_js_1.env.trace === 'undefined' ? !env_impl_js_1.env.wasm.trace : !env_impl_js_1.env.trace) {
return;
}
// eslint-disable-next-line no-console
console.time(`ORT::${extraMsg}`);
};
exports.TRACE_EVENT_BEGIN = TRACE_EVENT_BEGIN;
/**
* @ignore
*/
const TRACE_EVENT_END = (extraMsg) => {
if (typeof env_impl_js_1.env.trace === 'undefined' ? !env_impl_js_1.env.wasm.trace : !env_impl_js_1.env.trace) {
return;
}
// eslint-disable-next-line no-console
console.timeEnd(`ORT::${extraMsg}`);
};
exports.TRACE_EVENT_END = TRACE_EVENT_END;
//# sourceMappingURL=trace.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"trace.js","sourceRoot":"","sources":["../../lib/trace.ts"],"names":[],"mappings":";AAAA,4DAA4D;AAC5D,kCAAkC;;;AAElC,+CAAoC;AAEpC;;GAEG;AACI,MAAM,KAAK,GAAG,CAAC,UAAkB,EAAE,KAAa,EAAE,EAAE;IACzD,IAAI,OAAO,iBAAG,CAAC,KAAK,KAAK,WAAW,CAAC,CAAC,CAAC,CAAC,iBAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,iBAAG,CAAC,KAAK,EAAE,CAAC;QACpE,OAAO;IACT,CAAC;IACD,sCAAsC;IACtC,OAAO,CAAC,SAAS,CAAC,GAAG,UAAU,UAAU,KAAK,EAAE,CAAC,CAAC;AACpD,CAAC,CAAC;AANW,QAAA,KAAK,SAMhB;AAEF,MAAM,UAAU,GAAG,CAAC,GAAW,E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AI,OAAO,iBAAG,CAAC,KAAK,KAAK,WAAW,CAAC,CAAC,CAAC,CAAC,iBAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,iBAAG,CAAC,KAAK,EAAE,CAAC;QACpE,OAAO;IACT,CAAC;IACD,UAAU,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;AAC9B,CAAC,CAAC;AALW,QAAA,cAAc,kBAKzB;AAEF;;GAEG;AACI,MAAM,iBAAiB,GAAG,CAAC,QAAiB,EAAE,EAAE;IACrD,IAAI,OAAO,iBAAG,CAAC,KAAK,KAAK,WAAW,CAAC,CAAC,CAAC,CAAC,iBAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,iBAAG,CAAC,KAAK,EAAE,CAAC;QACpE,OAAO;IACT,CAAC;IACD,sCAAsC;IACtC,OAAO,CAAC,IAAI,CAAC,QAAQ,QAAQ,EAAE,CAAC,CAAC;AACnC,CAAC,CAAC;AANW,QAAA,iBAAiB,qBAM5B;AAEF;;GAEG;AACI,MAAM,eAAe,GAAG,CAAC,QAAiB,EAAE,EAAE;IACnD,IAAI,OAAO,iBAAG,CAAC,KAAK,KAAK,WAAW,CAAC,CAAC,CAAC,CAAC,iBAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,iBAAG,CAAC,KAAK,EAAE,CAAC;QACpE,OAAO;IACT,CAAC;IACD,sCAAsC;IACtC,OAAO,CAAC,OAAO,CAAC,QAAQ,QAAQ,EAAE,CAAC,CAAC;AACtC,CAAC,CAAC;AANW,QAAA,eAAe,mBAM1B"}
+29
View File
@@ -0,0 +1,29 @@
/**
* A helper type to get certain types if they are declared in global scope.
*
* For example, if you installed "@webgpu/types" as a dev dependency, then `TryGetTypeIfDeclared<'GPUDevice'>` will
* be type `GPUDevice`, otherwise it will be type `unknown`.
*
*
* We don't want to introduce "@webgpu/types" as a dependency of this package because:
*
* (1) For JavaScript users, it's not needed. For TypeScript users, they can install it as dev dependency themselves.
*
* (2) because "@webgpu/types" requires "@types/dom-webcodecs" as peer dependency when using TypeScript < v5.1 and its
* version need to be chosen carefully according to the TypeScript version being used. This means so far there is not a
* way to keep every TypeScript version happy. It turns out that we will easily broke users on some TypeScript version.
*
* for more info see https://github.com/gpuweb/types/issues/127
*
* Update (2024-08-07): The reason (2) may be no longer valid. Most people should be using TypeScript >= 5.1 by now.
* However, we are still not sure whether introducing "@webgpu/types" as direct dependency is a good idea. We find this
* type helper is useful for TypeScript users.
*
* @ignore
*/
export type TryGetGlobalType<Name extends string, Fallback = unknown> = typeof globalThis extends {
[k in Name]: {
prototype: infer T;
};
} ? T : Fallback;
//# sourceMappingURL=type-helper.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"type-helper.d.ts","sourceRoot":"","sources":["../../lib/type-helper.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,MAAM,gBAAgB,CAAC,IAAI,SAAS,MAAM,EAAE,QAAQ,GAAG,OAAO,IAAI,OAAO,UAAU,SAAS;KAC/F,CAAC,IAAI,IAAI,GAAG;QAAE,SAAS,EAAE,MAAM,CAAC,CAAA;KAAE;CACpC,GACG,CAAC,GACD,QAAQ,CAAC"}
+5
View File
@@ -0,0 +1,5 @@
"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
//# sourceMappingURL=type-helper.js.map
@@ -0,0 +1 @@
{"version":3,"file":"type-helper.js","sourceRoot":"","sources":["../../lib/type-helper.ts"],"names":[],"mappings":";AAAA,4DAA4D;AAC5D,kCAAkC"}
+2
View File
@@ -0,0 +1,2 @@
export declare const version = "1.27.0";
//# sourceMappingURL=version.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../../lib/version.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,OAAO,WAAW,CAAC"}
+9
View File
@@ -0,0 +1,9 @@
"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.version = void 0;
// This file is generated by /js/scripts/update-version.ts
// Do not modify file content manually.
exports.version = '1.27.0';
//# sourceMappingURL=version.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"version.js","sourceRoot":"","sources":["../../lib/version.ts"],"names":[],"mappings":";AAAA,4DAA4D;AAC5D,kCAAkC;;;AAElC,0DAA0D;AAC1D,uCAAuC;AAE1B,QAAA,OAAO,GAAG,QAAQ,CAAC"}
@@ -0,0 +1,24 @@
import { Backend } from './backend.js';
import { InferenceSession } from './inference-session.js';
/**
* Register a backend.
*
* @param name - the name as a key to lookup as an execution provider.
* @param backend - the backend object.
* @param priority - an integer indicating the priority of the backend. Higher number means higher priority. if priority
* < 0, it will be considered as a 'beta' version and will not be used as a fallback backend by default.
*
* @ignore
*/
export declare const registerBackend: (name: string, backend: Backend, priority: number) => void;
/**
* Resolve execution providers from the specific session options.
*
* @param options - the session options object.
* @returns a promise that resolves to a tuple of an initialized backend instance and a session options object with
* filtered EP list.
*
* @ignore
*/
export declare const resolveBackendAndExecutionProviders: (options: InferenceSession.SessionOptions) => Promise<[backend: Backend, options: InferenceSession.SessionOptions]>;
//# sourceMappingURL=backend-impl.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"backend-impl.d.ts","sourceRoot":"","sources":["../../lib/backend-impl.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAe1D;;;;;;;;;GASG;AACH,eAAO,MAAM,eAAe,GAAI,MAAM,MAAM,EAAE,SAAS,OAAO,EAAE,UAAU,MAAM,KAAG,IAgClF,CAAC;AAuCF;;;;;;;;GAQG;AACH,eAAO,MAAM,mCAAmC,GAC9C,SAAS,gBAAgB,CAAC,cAAc,KACvC,OAAO,CAAC,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,gBAAgB,CAAC,cAAc,CAAC,CAoDtE,CAAC"}
+142
View File
@@ -0,0 +1,142 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
const backends = new Map();
const backendsSortedByPriority = [];
/**
* Register a backend.
*
* @param name - the name as a key to lookup as an execution provider.
* @param backend - the backend object.
* @param priority - an integer indicating the priority of the backend. Higher number means higher priority. if priority
* < 0, it will be considered as a 'beta' version and will not be used as a fallback backend by default.
*
* @ignore
*/
export const registerBackend = (name, backend, priority) => {
if (backend && typeof backend.init === 'function' && typeof backend.createInferenceSessionHandler === 'function') {
const currentBackend = backends.get(name);
if (currentBackend === undefined) {
backends.set(name, { backend, priority });
}
else if (currentBackend.priority > priority) {
// same name is already registered with a higher priority. skip registeration.
return;
}
else if (currentBackend.priority === priority) {
if (currentBackend.backend !== backend) {
throw new Error(`cannot register backend "${name}" using priority ${priority}`);
}
}
if (priority >= 0) {
const i = backendsSortedByPriority.indexOf(name);
if (i !== -1) {
backendsSortedByPriority.splice(i, 1);
}
for (let i = 0; i < backendsSortedByPriority.length; i++) {
if (backends.get(backendsSortedByPriority[i]).priority <= priority) {
backendsSortedByPriority.splice(i, 0, name);
return;
}
}
backendsSortedByPriority.push(name);
}
return;
}
throw new TypeError('not a valid backend');
};
/**
* Try to resolve and initialize a backend.
*
* @param backendName - the name of the backend.
* @returns the backend instance if resolved and initialized successfully, or an error message if failed.
*/
const tryResolveAndInitializeBackend = async (backendName) => {
const backendInfo = backends.get(backendName);
if (!backendInfo) {
return 'backend not found.';
}
if (backendInfo.initialized) {
return backendInfo.backend;
}
else if (backendInfo.aborted) {
return backendInfo.error;
}
else {
const isInitializing = !!backendInfo.initPromise;
try {
if (!isInitializing) {
backendInfo.initPromise = backendInfo.backend.init(backendName);
}
await backendInfo.initPromise;
backendInfo.initialized = true;
return backendInfo.backend;
}
catch (e) {
if (!isInitializing) {
backendInfo.error = `${e}`;
backendInfo.aborted = true;
}
return backendInfo.error;
}
finally {
delete backendInfo.initPromise;
}
}
};
/**
* Resolve execution providers from the specific session options.
*
* @param options - the session options object.
* @returns a promise that resolves to a tuple of an initialized backend instance and a session options object with
* filtered EP list.
*
* @ignore
*/
export const resolveBackendAndExecutionProviders = async (options) => {
// extract backend hints from session options
const eps = options.executionProviders || [];
const backendHints = eps.map((i) => (typeof i === 'string' ? i : i.name));
const backendNames = backendHints.length === 0 ? backendsSortedByPriority : backendHints;
// try to resolve and initialize all requested backends
let backend;
const errors = [];
const availableBackendNames = new Set();
for (const backendName of backendNames) {
const resolveResult = await tryResolveAndInitializeBackend(backendName);
if (typeof resolveResult === 'string') {
errors.push({ name: backendName, err: resolveResult });
}
else {
if (!backend) {
backend = resolveResult;
}
if (backend === resolveResult) {
availableBackendNames.add(backendName);
}
}
}
// if no backend is available, throw error.
if (!backend) {
throw new Error(`no available backend found. ERR: ${errors.map((e) => `[${e.name}] ${e.err}`).join(', ')}`);
}
// for each explicitly requested backend, if it's not available, output warning message.
for (const { name, err } of errors) {
if (backendHints.includes(name)) {
// eslint-disable-next-line no-console
console.warn(`removing requested execution provider "${name}" from session options because it is not available: ${err}`);
}
}
const filteredEps = eps.filter((i) => availableBackendNames.has(typeof i === 'string' ? i : i.name));
return [
backend,
new Proxy(options, {
get: (target, prop) => {
if (prop === 'executionProviders') {
return filteredEps;
}
return Reflect.get(target, prop);
},
}),
];
};
//# sourceMappingURL=backend-impl.js.map
@@ -0,0 +1 @@
{"version":3,"file":"backend-impl.js","sourceRoot":"","sources":["../../lib/backend-impl.ts"],"names":[],"mappings":"AAAA,4DAA4D;AAC5D,kCAAkC;AAelC,MAAM,QAAQ,GAA6B,IAAI,GAAG,EAAE,CAAC;AACrD,MAAM,wBAAwB,GAAa,EAAE,CAAC;AAE9C;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,IAAY,EAAE,OAAgB,EAAE,QAAgB,EAAQ,EAAE;IACxF,IAAI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IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAC9G,CAAC;IAED,wFAAwF;IACxF,KAAK,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,MAAM,EAAE,CAAC;QACnC,IAAI,YAAY,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;YAChC,sCAAsC;YACtC,OAAO,CAAC,IAAI,CACV,0CAA0C,IAAI,uDAAuD,GAAG,EAAE,CAC3G,CAAC;QACJ,CAAC;IACH,CAAC;IAED,MAAM,WAAW,GAAG,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,qBAAqB,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;IAErG,OAAO;QACL,OAAO;QACP,IAAI,KAAK,CAAC,OAAO,EAAE;YACjB,GAAG,EAAE,CAAC,MAAM,EAAE,IAAI,EAAE,EAAE;gBACpB,IAAI,IAAI,KAAK,oBAAoB,EAAE,CAAC;oBAClC,OAAO,WAAW,CAAC;gBACrB,CAAC;gBACD,OAAO,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;YACnC,CAAC;SACF,CAAC;KACH,CAAC;AACJ,CAAC,CAAC"}
+52
View File
@@ -0,0 +1,52 @@
import { InferenceSession } from './inference-session.js';
import { OnnxValue } from './onnx-value.js';
/**
* @ignore
*/
export declare namespace SessionHandler {
type FeedsType = {
[name: string]: OnnxValue;
};
type FetchesType = {
[name: string]: OnnxValue | null;
};
type ReturnType = {
[name: string]: OnnxValue;
};
}
/**
* Represents shared SessionHandler functionality
*
* @ignore
*/
interface SessionHandler {
dispose(): Promise<void>;
readonly inputNames: readonly string[];
readonly outputNames: readonly string[];
readonly inputMetadata: readonly InferenceSession.ValueMetadata[];
readonly outputMetadata: readonly InferenceSession.ValueMetadata[];
}
/**
* Represent a handler instance of an inference session.
*
* @ignore
*/
export interface InferenceSessionHandler extends SessionHandler {
startProfiling(): void;
endProfiling(): void;
run(feeds: SessionHandler.FeedsType, fetches: SessionHandler.FetchesType, options: InferenceSession.RunOptions): Promise<SessionHandler.ReturnType>;
}
/**
* Represent a backend that provides implementation of model inferencing.
*
* @ignore
*/
export interface Backend {
/**
* Initialize the backend asynchronously. Should throw when failed.
*/
init(backendName: string): Promise<void>;
createInferenceSessionHandler(uriOrBuffer: string | Uint8Array, options?: InferenceSession.SessionOptions): Promise<InferenceSessionHandler>;
}
export { registerBackend } from './backend-impl.js';
//# sourceMappingURL=backend.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"backend.d.ts","sourceRoot":"","sources":["../../lib/backend.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C;;GAEG;AACH,MAAM,CAAC,OAAO,WAAW,cAAc,CAAC;IACtC,KAAK,SAAS,GAAG;QAAE,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,CAAA;KAAE,CAAC;IAC/C,KAAK,WAAW,GAAG;QAAE,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,CAAA;KAAE,CAAC;IACxD,KAAK,UAAU,GAAG;QAAE,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,CAAA;KAAE,CAAC;CACjD;AAED;;;;GAIG;AACH,UAAU,cAAc;IACtB,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAEzB,QAAQ,CAAC,UAAU,EAAE,SAAS,MAAM,EAAE,CAAC;IACvC,QAAQ,CAAC,WAAW,EAAE,SAAS,MAAM,EAAE,CAAC;IAExC,QAAQ,CAAC,aAAa,EAAE,SAAS,gBAAgB,CAAC,aAAa,EAAE,CAAC;IAClE,QAAQ,CAAC,cAAc,EAAE,SAAS,gBAAgB,CAAC,aAAa,EAAE,CAAC;CACpE;AAED;;;;GAIG;AACH,MAAM,WAAW,uBAAwB,SAAQ,cAAc;IAC7D,cAAc,IAAI,IAAI,CAAC;IACvB,YAAY,IAAI,IAAI,CAAC;IAErB,GAAG,CACD,KAAK,EAAE,cAAc,CAAC,SAAS,EAC/B,OAAO,EAAE,cAAc,CAAC,WAAW,EACnC,OAAO,EAAE,gBAAgB,CAAC,UAAU,GACnC,OAAO,CAAC,cAAc,CAAC,UAAU,CAAC,CAAC;CACvC;AAED;;;;GAIG;AACH,MAAM,WAAW,OAAO;IACtB;;OAEG;IACH,IAAI,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAEzC,6BAA6B,CAC3B,WAAW,EAAE,MAAM,GAAG,UAAU,EAChC,OAAO,CAAC,EAAE,gBAAgB,CAAC,cAAc,GACxC,OAAO,CAAC,uBAAuB,CAAC,CAAC;CACrC;AAED,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC"}
+4
View File
@@ -0,0 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
export { registerBackend } from './backend-impl.js';
//# sourceMappingURL=backend.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"backend.js","sourceRoot":"","sources":["../../lib/backend.ts"],"names":[],"mappings":"AAAA,4DAA4D;AAC5D,kCAAkC;AA8DlC,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC"}
+3
View File
@@ -0,0 +1,3 @@
import { Env } from './env.js';
export declare const env: Env;
//# sourceMappingURL=env-impl.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"env-impl.d.ts","sourceRoot":"","sources":["../../lib/env-impl.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAO/B,eAAO,MAAM,GAAG,EAAE,GAkBjB,CAAC"}
+25
View File
@@ -0,0 +1,25 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { version } from './version.js';
let logLevelValue = 'warning';
export const env = {
wasm: {},
webgl: {},
webgpu: {},
versions: { common: version },
set logLevel(value) {
if (value === undefined) {
return;
}
if (typeof value !== 'string' || ['verbose', 'info', 'warning', 'error', 'fatal'].indexOf(value) === -1) {
throw new Error(`Unsupported logging level: ${value}`);
}
logLevelValue = value;
},
get logLevel() {
return logLevelValue;
},
};
// set property 'logLevel' so that they can be correctly transferred to worker by `postMessage()`.
Object.defineProperty(env, 'logLevel', { enumerable: true });
//# sourceMappingURL=env-impl.js.map
@@ -0,0 +1 @@
{"version":3,"file":"env-impl.js","sourceRoot":"","sources":["../../lib/env-impl.ts"],"names":[],"mappings":"AAAA,4DAA4D;AAC5D,kCAAkC;AAGlC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAIvC,IAAI,aAAa,GAA2B,SAAS,CAAC;AAEtD,MAAM,CAAC,MAAM,GAAG,GAAQ;IACtB,IAAI,EAAE,EAAE;IACR,KAAK,EAAE,EAAoB;IAC3B,MAAM,EAAE,EAAqB;IAC7B,QAAQ,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE;IAE7B,IAAI,QAAQ,CAAC,KAAmB;QAC9B,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;YACxB,OAAO;QACT,CAAC;QACD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC;YACxG,MAAM,IAAI,KAAK,CAAC,8BAA8B,KAAK,EAAE,CAAC,CAAC;QACzD,CAAC;QACD,aAAa,GAAG,KAAK,CAAC;IACxB,CAAC;IACD,IAAI,QAAQ;QACV,OAAO,aAAa,CAAC;IACvB,CAAC;CACF,CAAC;AAEF,kGAAkG;AAClG,MAAM,CAAC,cAAc,CAAC,GAAG,EAAE,UAAU,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC,CAAC"}
+277
View File
@@ -0,0 +1,277 @@
import { TryGetGlobalType } from './type-helper.js';
export declare namespace Env {
type WasmPathPrefix = string;
interface WasmFilePaths {
/**
* Specify the override path for the main .wasm file.
*
* This path should be an absolute path.
*
* If not modified, the filename of the .wasm file is:
* - `ort-wasm-simd-threaded.wasm` for default build
* - `ort-wasm-simd-threaded.jsep.wasm` for JSEP build (with WebGPU and WebNN)
* - `ort-wasm-simd-threaded.asyncify.wasm` for WebGPU build with Asyncify (with WebNN)
* - `ort-wasm-simd-threaded.jspi.wasm` for WebGPU build with JSPI support (with WebNN)
*/
wasm?: URL | string;
/**
* Specify the override path for the main .mjs file.
*
* This path should be an absolute path.
*
* If not modified, the filename of the .mjs file is:
* - `ort-wasm-simd-threaded.mjs` for default build
* - `ort-wasm-simd-threaded.jsep.mjs` for JSEP build (with WebGPU and WebNN)
* - `ort-wasm-simd-threaded.asyncify.mjs` for WebGPU build with Asyncify (with WebNN)
* - `ort-wasm-simd-threaded.jspi.mjs` for WebGPU build with JSPI support (with WebNN)
*/
mjs?: URL | string;
}
type WasmPrefixOrFilePaths = WasmPathPrefix | WasmFilePaths;
interface WebAssemblyFlags {
/**
* set or get number of thread(s). If omitted or set to 0, number of thread(s) will be determined by system. If set
* to 1, no worker thread will be spawned.
*
* This setting is available only when WebAssembly multithread feature is available in current context.
*
* @defaultValue `0`
*/
numThreads?: number;
/**
* set a value indicating whether to enable SIMD.
*
* ONNX Runtime will perform feature detection based on the value of this property. Specifically, when the value is
* set to:
* - `undefined`, `true` or `"fixed"`: will check availability of Fixed-width SIMD.
* - `"relaxed"`: will check availability of Relaxed SIMD.
* - `false`: will not perform SIMD feature checking.
*
* Setting this property does not make ONNX Runtime to switch to the corresponding runtime automatically. User need
* to set `wasmPaths` or `wasmBinary` property to load the corresponding runtime.
*
* This setting is available only when WebAssembly SIMD feature is available in current context.
*
* @defaultValue `true`
*/
simd?: boolean | 'fixed' | 'relaxed';
/**
* set or get a boolean value indicating whether to enable trace.
*
* @defaultValue `false`
*
* @deprecated Use `env.trace` instead. If `env.trace` is set, this property will be ignored.
*/
trace?: boolean;
/**
* Set or get a number specifying the timeout for initialization of WebAssembly backend, in milliseconds. A zero
* value indicates no timeout is set.
*
* @defaultValue `0`
*/
initTimeout?: number;
/**
* Set a custom URL prefix to the .wasm/.mjs files, or an object of overrides for both .wasm/.mjs file. The override
* path should be an absolute path.
*/
wasmPaths?: WasmPrefixOrFilePaths;
/**
* Set a custom buffer which contains the WebAssembly binary. If this property is set, the `wasmPaths` property will
* be ignored.
*/
wasmBinary?: ArrayBufferLike | Uint8Array;
/**
* Set or get a boolean value indicating whether to proxy the execution of main thread to a worker thread.
*
* @defaultValue `false`
*/
proxy?: boolean;
}
interface WebGLFlags {
/**
* Set or get the WebGL Context ID (webgl or webgl2).
*
* @defaultValue `'webgl2'`
*/
contextId?: 'webgl' | 'webgl2';
/**
* Get the WebGL rendering context.
*/
readonly context: WebGLRenderingContext;
/**
* Set or get the maximum batch size for matmul. 0 means to disable batching.
*
* @deprecated
*/
matmulMaxBatchSize?: number;
/**
* Set or get the texture cache mode.
*
* @defaultValue `'full'`
*/
textureCacheMode?: 'initializerOnly' | 'full';
/**
* Set or get the packed texture mode
*
* @defaultValue `false`
*/
pack?: boolean;
/**
* Set or get whether enable async download.
*
* @defaultValue `false`
*/
async?: boolean;
}
interface WebGpuProfilingDataV1TensorMetadata {
dims: readonly number[];
dataType: string;
}
interface WebGpuProfilingDataV1 {
version: 1;
inputsMetadata: readonly WebGpuProfilingDataV1TensorMetadata[];
outputsMetadata: readonly WebGpuProfilingDataV1TensorMetadata[];
kernelId: number;
kernelType: string;
kernelName: string;
programName: string;
startTime: number;
endTime: number;
}
type WebGpuProfilingData = WebGpuProfilingDataV1;
interface WebGpuFlags {
/**
* Set or get the profiling mode.
*
* @deprecated Use `env.webgpu.profiling.mode` instead. If `env.webgpu.profiling.mode` is set, this property will be
* ignored.
*/
profilingMode?: 'off' | 'default';
/**
* Set or get the profiling configuration.
*/
profiling: {
/**
* Set or get the profiling mode.
*
* @defaultValue `'off'`
*/
mode?: 'off' | 'default';
/**
* Set or get a callback function when a profiling data is received. If not set, the profiling data will be
* printed to console.
*/
ondata?: (data: WebGpuProfilingData) => void;
};
/**
* Set or get the power preference.
*
* Setting this property only has effect before the first WebGPU inference session is created. The value will be
* used as options for `navigator.gpu.requestAdapter()`.
*
* See {@link https://gpuweb.github.io/gpuweb/#dictdef-gpurequestadapteroptions} for more details.
*
* @defaultValue `undefined`
*
* @deprecated Create your own GPUAdapter, use it to create a GPUDevice instance and set {@link device} property if
* you want to use a specific power preference.
*/
powerPreference?: 'low-power' | 'high-performance';
/**
* Set or get the force fallback adapter flag.
*
* Setting this property only has effect before the first WebGPU inference session is created. The value will be
* used as options for `navigator.gpu.requestAdapter()`.
*
* See {@link https://gpuweb.github.io/gpuweb/#dictdef-gpurequestadapteroptions} for more details.
*
* @defaultValue `undefined`
*
* @deprecated Create your own GPUAdapter, use it to create a GPUDevice instance and set {@link device} property if
* you want to use a specific fallback option.
*/
forceFallbackAdapter?: boolean;
/**
* Set or get the adapter for WebGPU.
*
* Setting this property only has effect before the first WebGPU inference session is created. The value will be
* used as the GPU adapter for the underlying WebGPU backend to create GPU device.
*
* If this property is not set, it will be available to get after the first WebGPU inference session is created. The
* value will be the GPU adapter that created by the underlying WebGPU backend.
*
* When use with TypeScript, the type of this property is `GPUAdapter` defined in "@webgpu/types".
*
* @deprecated It is no longer recommended to use this property. The latest WebGPU spec adds `GPUDevice.adapterInfo`
* (https://www.w3.org/TR/webgpu/#dom-gpudevice-adapterinfo), which allows to get the adapter information from the
* device. When it's available, there is no need to set/get the {@link adapter} property.
*/
adapter: TryGetGlobalType<'GPUAdapter'>;
/**
* Set or get the GPU device for WebGPU.
*
* There are 3 valid scenarios of accessing this property:
* - Set a value before the first WebGPU inference session is created. The value will be used by the WebGPU backend
* to perform calculations. If the value is not a `GPUDevice` object, an error will be thrown.
* - Get the value before the first WebGPU inference session is created. This will try to create a new GPUDevice
* instance. Returns a `Promise` that resolves to a `GPUDevice` object.
* - Get the value after the first WebGPU inference session is created. Returns a resolved `Promise` to the
* `GPUDevice` object used by the WebGPU backend.
*/
get device(): Promise<TryGetGlobalType<'GPUDevice'>>;
set device(value: TryGetGlobalType<'GPUDevice'>);
/**
* Set or get whether validate input content.
*
* @defaultValue `false`
*/
validateInputContent?: boolean;
}
}
export interface Env {
/**
* set the severity level for logging.
*
* @defaultValue `'warning'`
*/
logLevel?: 'verbose' | 'info' | 'warning' | 'error' | 'fatal';
/**
* Indicate whether run in debug mode.
*
* @defaultValue `false`
*/
debug?: boolean;
/**
* set or get a boolean value indicating whether to enable trace.
*
* @defaultValue `false`
*/
trace?: boolean;
/**
* Get version of the current package.
*/
readonly versions: {
readonly common: string;
readonly web?: string;
readonly node?: string;
readonly 'react-native'?: string;
};
/**
* Represent a set of flags for WebAssembly
*/
readonly wasm: Env.WebAssemblyFlags;
/**
* Represent a set of flags for WebGL
*/
readonly webgl: Env.WebGLFlags;
/**
* Represent a set of flags for WebGPU
*/
readonly webgpu: Env.WebGpuFlags;
[name: string]: unknown;
}
/**
* Represent a set of flags as a global singleton.
*/
export declare const env: Env;
//# sourceMappingURL=env.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"env.d.ts","sourceRoot":"","sources":["../../lib/env.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAEpD,MAAM,CAAC,OAAO,WAAW,GAAG,CAAC;IAC3B,KAAY,cAAc,GAAG,MAAM,CAAC;IACpC,UAAiB,aAAa;QAC5B;;;;;;;;;;WAUG;QACH,IAAI,CAAC,EAAE,GAAG,GAAG,MAAM,CAAC;QACpB;;;;;;;;;;WAUG;QACH,GAAG,CAAC,EAAE,GAAG,GAAG,MAAM,CAAC;KACpB;IACD,KAAY,qBAAqB,GAAG,cAAc,GAAG,aAAa,CAAC;IACnE,UAA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F;;OAEG;IACH,QAAQ,CAAC,IAAI,EAAE,GAAG,CAAC,gBAAgB,CAAC;IAEpC;;OAEG;IACH,QAAQ,CAAC,KAAK,EAAE,GAAG,CAAC,UAAU,CAAC;IAE/B;;OAEG;IACH,QAAQ,CAAC,MAAM,EAAE,GAAG,CAAC,WAAW,CAAC;IAEjC,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;CACzB;AAED;;GAEG;AACH,eAAO,MAAM,GAAG,EAAE,GAAa,CAAC"}
+8
View File
@@ -0,0 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { env as envImpl } from './env-impl.js';
/**
* Represent a set of flags as a global singleton.
*/
export const env = envImpl;
//# sourceMappingURL=env.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"env.js","sourceRoot":"","sources":["../../lib/env.ts"],"names":[],"mappings":"AAAA,4DAA4D;AAC5D,kCAAkC;AAElC,OAAO,EAAE,GAAG,IAAI,OAAO,EAAE,MAAM,eAAe,CAAC;AAuS/C;;GAEG;AACH,MAAM,CAAC,MAAM,GAAG,GAAQ,OAAO,CAAC"}
+25
View File
@@ -0,0 +1,25 @@
/**
* # ONNX Runtime JavaScript API
*
* ONNX Runtime JavaScript API is a unified API for all JavaScript usages, including the following NPM packages:
*
* - [onnxruntime-node](https://www.npmjs.com/package/onnxruntime-node)
* - [onnxruntime-web](https://www.npmjs.com/package/onnxruntime-web)
* - [onnxruntime-react-native](https://www.npmjs.com/package/onnxruntime-react-native)
*
* See also:
* - [Get Started](https://onnxruntime.ai/docs/get-started/with-javascript/)
* - [Inference examples](https://github.com/microsoft/onnxruntime-inference-examples/tree/main/js)
*
* @packageDocumentation
*/
export * from './backend.js';
export * from './env.js';
export * from './inference-session.js';
export * from './tensor.js';
export * from './tensor-conversion.js';
export * from './tensor-factory.js';
export * from './trace.js';
export * from './onnx-model.js';
export * from './onnx-value.js';
//# sourceMappingURL=index.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/index.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;GAcG;AAEH,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC;AACzB,cAAc,wBAAwB,CAAC;AACvC,cAAc,aAAa,CAAC;AAC5B,cAAc,wBAAwB,CAAC;AACvC,cAAc,qBAAqB,CAAC;AACpC,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,cAAc,iBAAiB,CAAC"}
+27
View File
@@ -0,0 +1,27 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
/**
* # ONNX Runtime JavaScript API
*
* ONNX Runtime JavaScript API is a unified API for all JavaScript usages, including the following NPM packages:
*
* - [onnxruntime-node](https://www.npmjs.com/package/onnxruntime-node)
* - [onnxruntime-web](https://www.npmjs.com/package/onnxruntime-web)
* - [onnxruntime-react-native](https://www.npmjs.com/package/onnxruntime-react-native)
*
* See also:
* - [Get Started](https://onnxruntime.ai/docs/get-started/with-javascript/)
* - [Inference examples](https://github.com/microsoft/onnxruntime-inference-examples/tree/main/js)
*
* @packageDocumentation
*/
export * from './backend.js';
export * from './env.js';
export * from './inference-session.js';
export * from './tensor.js';
export * from './tensor-conversion.js';
export * from './tensor-factory.js';
export * from './trace.js';
export * from './onnx-model.js';
export * from './onnx-value.js';
//# sourceMappingURL=index.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../lib/index.ts"],"names":[],"mappings":"AAAA,4DAA4D;AAC5D,kCAAkC;AAElC;;;;;;;;;;;;;;GAcG;AAEH,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC;AACzB,cAAc,wBAAwB,CAAC;AACvC,cAAc,aAAa,CAAC;AAC5B,cAAc,wBAAwB,CAAC;AACvC,cAAc,qBAAqB,CAAC;AACpC,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,cAAc,iBAAiB,CAAC"}
@@ -0,0 +1,25 @@
import { InferenceSession as InferenceSessionInterface } from './inference-session.js';
type SessionOptions = InferenceSessionInterface.SessionOptions;
type RunOptions = InferenceSessionInterface.RunOptions;
type FeedsType = InferenceSessionInterface.FeedsType;
type FetchesType = InferenceSessionInterface.FetchesType;
type ReturnType = InferenceSessionInterface.ReturnType;
export declare class InferenceSession implements InferenceSessionInterface {
private constructor();
run(feeds: FeedsType, options?: RunOptions): Promise<ReturnType>;
run(feeds: FeedsType, fetches: FetchesType, options?: RunOptions): Promise<ReturnType>;
release(): Promise<void>;
static create(path: string, options?: SessionOptions): Promise<InferenceSessionInterface>;
static create(buffer: ArrayBufferLike, options?: SessionOptions): Promise<InferenceSessionInterface>;
static create(buffer: ArrayBufferLike, byteOffset: number, byteLength?: number, options?: SessionOptions): Promise<InferenceSessionInterface>;
static create(buffer: Uint8Array, options?: SessionOptions): Promise<InferenceSessionInterface>;
startProfiling(): void;
endProfiling(): void;
get inputNames(): readonly string[];
get outputNames(): readonly string[];
get inputMetadata(): readonly InferenceSessionInterface.ValueMetadata[];
get outputMetadata(): readonly InferenceSessionInterface.ValueMetadata[];
private handler;
}
export {};
//# sourceMappingURL=inference-session-impl.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"inference-session-impl.d.ts","sourceRoot":"","sources":["../../lib/inference-session-impl.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,gBAAgB,IAAI,yBAAyB,EAAE,MAAM,wBAAwB,CAAC;AAKvF,KAAK,cAAc,GAAG,yBAAyB,CAAC,cAAc,CAAC;AAC/D,KAAK,UAAU,GAAG,yBAAyB,CAAC,UAAU,CAAC;AACvD,KAAK,SAAS,GAAG,yBAAyB,CAAC,SAAS,CAAC;AACrD,KAAK,WAAW,GAAG,yBAAyB,CAAC,WAAW,CAAC;AACzD,KAAK,UAAU,GAAG,yBAAyB,CAAC,UAAU,CAAC;AAEvD,qBAAa,gBAAiB,YAAW,yBAAyB;IAChE,OAAO;IAGP,GAAG,CAAC,KAAK,EAAE,SAAS,EAAE,OAAO,CAAC,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;IAChE,GAAG,CAAC,KAAK,EAAE,SAAS,EAAE,OAAO,EAAE,WAAW,EAAE,OAAO,CAAC,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;IA2GhF,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAI9B,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,yBAAyB,CAAC;IACzF,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,eAAe,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,yBAAyB,CAAC;IACpG,MAAM,CAAC,MAAM,CACX,MAAM,EAAE,eAAe,EACvB,UAAU,EAAE,MAAM,EAClB,UAAU,CAAC,EAAE,MAAM,EACnB,OAAO,CAAC,EAAE,cAAc,GACvB,OAAO,CAAC,yBAAyB,CAAC;IACrC,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,UAAU,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,yBAAyB,CAAC;IA6E/F,cAAc,IAAI,IAAI;IAGtB,YAAY,IAAI,IAAI;IAIpB,IAAI,UAAU,IAAI,SAAS,MAAM,EAAE,CAElC;IACD,IAAI,WAAW,IAAI,SAAS,MAAM,EAAE,CAEnC;IAED,IAAI,aAAa,IAAI,SAAS,yBAAyB,CAAC,aAAa,EAAE,CAEtE;IAED,IAAI,cAAc,IAAI,SAAS,yBAAyB,CAAC,aAAa,EAAE,CAEvE;IAED,OAAO,CAAC,OAAO,CAA0B;CAC1C"}
@@ -0,0 +1,208 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { resolveBackendAndExecutionProviders } from './backend-impl.js';
import { Tensor } from './tensor.js';
import { TRACE_FUNC_BEGIN, TRACE_FUNC_END, TRACE_EVENT_BEGIN, TRACE_EVENT_END } from './trace.js';
export class InferenceSession {
constructor(handler) {
this.handler = handler;
}
async run(feeds, arg1, arg2) {
TRACE_FUNC_BEGIN();
TRACE_EVENT_BEGIN('InferenceSession.run');
const fetches = {};
let options = {};
// check inputs
if (typeof feeds !== 'object' || feeds === null || feeds instanceof Tensor || Array.isArray(feeds)) {
throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values.");
}
let isFetchesEmpty = true;
// determine which override is being used
if (typeof arg1 === 'object') {
if (arg1 === null) {
throw new TypeError('Unexpected argument[1]: cannot be null.');
}
if (arg1 instanceof Tensor) {
throw new TypeError("'fetches' cannot be a Tensor");
}
if (Array.isArray(arg1)) {
if (arg1.length === 0) {
throw new TypeError("'fetches' cannot be an empty array.");
}
isFetchesEmpty = false;
// output names
for (const name of arg1) {
if (typeof name !== 'string') {
throw new TypeError("'fetches' must be a string array or an object.");
}
if (this.outputNames.indexOf(name) === -1) {
throw new RangeError(`'fetches' contains invalid output name: ${name}.`);
}
fetches[name] = null;
}
if (typeof arg2 === 'object' && arg2 !== null) {
options = arg2;
}
else if (typeof arg2 !== 'undefined') {
throw new TypeError("'options' must be an object.");
}
}
else {
// decide whether arg1 is fetches or options
// if any output name is present and its value is valid OnnxValue, we consider it fetches
let isFetches = false;
const arg1Keys = Object.getOwnPropertyNames(arg1);
for (const name of this.outputNames) {
if (arg1Keys.indexOf(name) !== -1) {
const v = arg1[name];
if (v === null || v instanceof Tensor) {
isFetches = true;
isFetchesEmpty = false;
fetches[name] = v;
}
}
}
if (isFetches) {
if (typeof arg2 === 'object' && arg2 !== null) {
options = arg2;
}
else if (typeof arg2 !== 'undefined') {
throw new TypeError("'options' must be an object.");
}
}
else {
options = arg1;
}
}
}
else if (typeof arg1 !== 'undefined') {
throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'.");
}
// check if all inputs are in feed
for (const name of this.inputNames) {
if (typeof feeds[name] === 'undefined') {
throw new Error(`input '${name}' is missing in 'feeds'.`);
}
}
// if no fetches is specified, we use the full output names list
if (isFetchesEmpty) {
for (const name of this.outputNames) {
fetches[name] = null;
}
}
// feeds, fetches and options are prepared
const results = await this.handler.run(feeds, fetches, options);
const returnValue = {};
for (const key in results) {
if (Object.hasOwnProperty.call(results, key)) {
const result = results[key];
if (result instanceof Tensor) {
returnValue[key] = result;
}
else {
returnValue[key] = new Tensor(result.type, result.data, result.dims);
}
}
}
TRACE_EVENT_END('InferenceSession.run');
TRACE_FUNC_END();
return returnValue;
}
async release() {
return this.handler.dispose();
}
static async create(arg0, arg1, arg2, arg3) {
TRACE_FUNC_BEGIN();
TRACE_EVENT_BEGIN('InferenceSession.create');
// either load from a file or buffer
let filePathOrUint8Array;
let options = {};
if (typeof arg0 === 'string') {
filePathOrUint8Array = arg0;
if (typeof arg1 === 'object' && arg1 !== null) {
options = arg1;
}
else if (typeof arg1 !== 'undefined') {
throw new TypeError("'options' must be an object.");
}
}
else if (arg0 instanceof Uint8Array) {
filePathOrUint8Array = arg0;
if (typeof arg1 === 'object' && arg1 !== null) {
options = arg1;
}
else if (typeof arg1 !== 'undefined') {
throw new TypeError("'options' must be an object.");
}
}
else if (arg0 instanceof ArrayBuffer ||
(typeof SharedArrayBuffer !== 'undefined' && arg0 instanceof SharedArrayBuffer)) {
const buffer = arg0;
let byteOffset = 0;
let byteLength = arg0.byteLength;
if (typeof arg1 === 'object' && arg1 !== null) {
options = arg1;
}
else if (typeof arg1 === 'number') {
byteOffset = arg1;
if (!Number.isSafeInteger(byteOffset)) {
throw new RangeError("'byteOffset' must be an integer.");
}
if (byteOffset < 0 || byteOffset >= buffer.byteLength) {
throw new RangeError(`'byteOffset' is out of range [0, ${buffer.byteLength}).`);
}
byteLength = arg0.byteLength - byteOffset;
if (typeof arg2 === 'number') {
byteLength = arg2;
if (!Number.isSafeInteger(byteLength)) {
throw new RangeError("'byteLength' must be an integer.");
}
if (byteLength <= 0 || byteOffset + byteLength > buffer.byteLength) {
throw new RangeError(`'byteLength' is out of range (0, ${buffer.byteLength - byteOffset}].`);
}
if (typeof arg3 === 'object' && arg3 !== null) {
options = arg3;
}
else if (typeof arg3 !== 'undefined') {
throw new TypeError("'options' must be an object.");
}
}
else if (typeof arg2 !== 'undefined') {
throw new TypeError("'byteLength' must be a number.");
}
}
else if (typeof arg1 !== 'undefined') {
throw new TypeError("'options' must be an object.");
}
filePathOrUint8Array = new Uint8Array(buffer, byteOffset, byteLength);
}
else {
throw new TypeError("Unexpected argument[0]: must be 'path' or 'buffer'.");
}
// resolve backend, update session options with validated EPs, and create session handler
const [backend, optionsWithValidatedEPs] = await resolveBackendAndExecutionProviders(options);
const handler = await backend.createInferenceSessionHandler(filePathOrUint8Array, optionsWithValidatedEPs);
TRACE_EVENT_END('InferenceSession.create');
TRACE_FUNC_END();
return new InferenceSession(handler);
}
startProfiling() {
this.handler.startProfiling();
}
endProfiling() {
this.handler.endProfiling();
}
get inputNames() {
return this.handler.inputNames;
}
get outputNames() {
return this.handler.outputNames;
}
get inputMetadata() {
return this.handler.inputMetadata;
}
get outputMetadata() {
return this.handler.outputMetadata;
}
}
//# sourceMappingURL=inference-session-impl.js.map
File diff suppressed because one or more lines are too long
@@ -0,0 +1,526 @@
import { OnnxModelOptions } from './onnx-model.js';
import { OnnxValue, OnnxValueDataLocation } from './onnx-value.js';
import type { Tensor } from './tensor.js';
import { TryGetGlobalType } from './type-helper.js';
export declare namespace InferenceSession {
type OnnxValueMapType = {
readonly [name: string]: OnnxValue;
};
type NullableOnnxValueMapType = {
readonly [name: string]: OnnxValue | null;
};
/**
* A feeds (model inputs) is an object that uses input names as keys and OnnxValue as corresponding values.
*/
type FeedsType = OnnxValueMapType;
/**
* A fetches (model outputs) could be one of the following:
*
* - Omitted. Use model's output names definition.
* - An array of string indicating the output names.
* - An object that use output names as keys and OnnxValue or null as corresponding values.
*
* @remarks
* different from input argument, in output, OnnxValue is optional. If an OnnxValue is present it will be
* used as a pre-allocated value by the inference engine; if omitted, inference engine will allocate buffer
* internally.
*/
type FetchesType = readonly string[] | NullableOnnxValueMapType;
/**
* A inferencing return type is an object that uses output names as keys and OnnxValue as corresponding values.
*/
type ReturnType = OnnxValueMapType;
/**
* A set of configurations for session behavior.
*/
interface SessionOptions extends OnnxModelOptions {
/**
* An array of execution provider options.
*
* An execution provider option can be a string indicating the name of the execution provider,
* or an object of corresponding type.
*/
executionProviders?: readonly ExecutionProviderConfig[];
/**
* The intra OP threads number.
*
* This setting is available only in ONNXRuntime (Node.js binding and react-native).
*/
intraOpNumThreads?: number;
/**
* The inter OP threads number.
*
* This setting is available only in ONNXRuntime (Node.js binding and react-native).
*/
interOpNumThreads?: number;
/**
* The free dimension override.
*
* This setting is available only in ONNXRuntime (Node.js binding and react-native) or WebAssembly backend
*/
freeDimensionOverrides?: {
readonly [dimensionName: string]: number;
};
/**
* The optimization level.
*
* This setting is available only in ONNXRuntime (Node.js binding and react-native) or WebAssembly backend
*/
graphOptimizationLevel?: 'disabled' | 'basic' | 'extended' | 'layout' | 'all';
/**
* Whether enable CPU memory arena.
*
* This setting is available only in ONNXRuntime (Node.js binding and react-native) or WebAssembly backend
*/
enableCpuMemArena?: boolean;
/**
* Whether enable memory pattern.
*
* This setting is available only in ONNXRuntime (Node.js binding and react-native) or WebAssembly backend
*/
enableMemPattern?: boolean;
/**
* Execution mode.
*
* This setting is available only in ONNXRuntime (Node.js binding and react-native) or WebAssembly backend
*/
executionMode?: 'sequential' | 'parallel';
/**
* Optimized model file path.
*
* If this setting is specified, the optimized model will be dumped. In browser, a blob will be created
* with a pop-up window.
*/
optimizedModelFilePath?: string;
/**
* Whether enable profiling.
*
* This setting is a placeholder for a future use.
*/
enableProfiling?: boolean;
/**
* File prefix for profiling.
*
* This setting is a placeholder for a future use.
*/
profileFilePrefix?: string;
/**
* Log ID.
*
* This setting is available only in ONNXRuntime (Node.js binding and react-native) or WebAssembly backend
*/
logId?: string;
/**
* Log severity level. See
* https://github.com/microsoft/onnxruntime/blob/main/include/onnxruntime/core/common/logging/severity.h
*
* This setting is available only in ONNXRuntime (Node.js binding and react-native) or WebAssembly backend
*/
logSeverityLevel?: 0 | 1 | 2 | 3 | 4;
/**
* Log verbosity level.
*
* This setting is available only in WebAssembly backend. Will support Node.js binding and react-native later
*/
logVerbosityLevel?: number;
/**
* Specify string as a preferred data location for all outputs, or an object that use output names as keys and a
* preferred data location as corresponding values.
*
* This setting is available only in ONNXRuntime Web for WebGL and WebGPU EP.
*/
preferredOutputLocation?: OnnxValueDataLocation | {
readonly [outputName: string]: OnnxValueDataLocation;
};
/**
* Whether enable graph capture.
* This setting is available only in ONNXRuntime Web for WebGPU EP.
*/
enableGraphCapture?: boolean;
/**
* Store configurations for a session. See
* https://github.com/microsoft/onnxruntime/blob/main/include/onnxruntime/core/session/
* onnxruntime_session_options_config_keys.h
*
* This setting is available only in WebAssembly backend. Will support Node.js binding and react-native later
*
* @example
* ```js
* extra: {
* session: {
* set_denormal_as_zero: "1",
* disable_prepacking: "1"
* },
* optimization: {
* enable_gelu_approximation: "1"
* }
* }
* ```
*/
extra?: Record<string, unknown>;
}
interface ExecutionProviderOptionMap {
coreml: CoreMLExecutionProviderOption;
cpu: CpuExecutionProviderOption;
cuda: CudaExecutionProviderOption;
dml: DmlExecutionProviderOption;
nnapi: NnapiExecutionProviderOption;
tensorrt: TensorRtExecutionProviderOption;
wasm: WebAssemblyExecutionProviderOption;
webgl: WebGLExecutionProviderOption;
webgpu: WebGpuExecutionProviderOption;
webnn: WebNNExecutionProviderOption;
qnn: QnnExecutionProviderOption;
xnnpack: XnnpackExecutionProviderOption;
}
type ExecutionProviderName = keyof ExecutionProviderOptionMap;
type ExecutionProviderConfig = ExecutionProviderOptionMap[ExecutionProviderName] | ExecutionProviderOption | ExecutionProviderName | string;
interface ExecutionProviderOption {
readonly name: string;
}
interface CpuExecutionProviderOption extends ExecutionProviderOption {
readonly name: 'cpu';
useArena?: boolean;
}
interface CudaExecutionProviderOption extends ExecutionProviderOption {
readonly name: 'cuda';
deviceId?: number;
}
interface DmlExecutionProviderOption extends ExecutionProviderOption {
readonly name: 'dml';
deviceId?: number;
}
interface TensorRtExecutionProviderOption extends ExecutionProviderOption {
readonly name: 'tensorrt';
deviceId?: number;
}
interface WebAssemblyExecutionProviderOption extends ExecutionProviderOption {
readonly name: 'wasm';
}
interface WebGLExecutionProviderOption extends ExecutionProviderOption {
readonly name: 'webgl';
}
interface XnnpackExecutionProviderOption extends ExecutionProviderOption {
readonly name: 'xnnpack';
}
interface WebGpuExecutionProviderOption extends ExecutionProviderOption {
readonly name: 'webgpu';
/**
* Specify the preferred layout when running layout sensitive operators.
*
* @default 'NCHW'
*/
preferredLayout?: 'NCHW' | 'NHWC';
/**
* Specify a list of node names that should be executed on CPU even when WebGPU EP is used.
*/
forceCpuNodeNames?: readonly string[];
/**
* Specify the validation mode for WebGPU execution provider.
* - 'disabled': Disable all validation.
* When used in Node.js, disable validation may cause process crash if WebGPU errors occur. Be cautious when using
* this mode.
* When used in web, this mode is equivalent to 'wgpuOnly'.
* - 'wgpuOnly': Perform WebGPU internal validation only.
* - 'basic': Perform basic validation including WebGPU internal validation. This is the default mode.
* - 'full': Perform full validation. This mode may have performance impact. Use it for debugging purpose.
*
* @default 'basic'
*/
validationMode?: 'disabled' | 'wgpuOnly' | 'basic' | 'full';
/**
* Specify an optional WebGPU device to be used by the WebGPU execution provider.
*/
device?: TryGetGlobalType<'GPUDevice'>;
}
interface WebNNExecutionProviderName extends ExecutionProviderOption {
readonly name: 'webnn';
}
/**
* Represents a set of options for creating a WebNN MLContext.
*
* @see https://www.w3.org/TR/webnn/#dictdef-mlcontextoptions
*/
interface WebNNContextOptions {
deviceType?: 'cpu' | 'gpu' | 'npu';
numThreads?: number;
powerPreference?: 'default' | 'low-power' | 'high-performance';
}
/**
* Represents a set of options for WebNN execution provider without MLContext.
*/
interface WebNNOptionsWithoutMLContext extends WebNNExecutionProviderName, WebNNContextOptions {
context?: never;
}
/**
* Represents a set of options for WebNN execution provider with MLContext.
*
* When MLContext is provided, the deviceType is also required so that the WebNN EP can determine the preferred
* channel layout.
*
* @see https://www.w3.org/TR/webnn/#dom-ml-createcontext
*/
interface WebNNOptionsWithMLContext extends WebNNExecutionProviderName, Omit<WebNNContextOptions, 'deviceType'>, Required<Pick<WebNNContextOptions, 'deviceType'>> {
context: TryGetGlobalType<'MLContext'>;
}
/**
* Represents a set of options for WebNN execution provider with MLContext which is created from GPUDevice.
*
* @see https://www.w3.org/TR/webnn/#dom-ml-createcontext-gpudevice
*/
interface WebNNOptionsWebGpu extends WebNNExecutionProviderName {
context: TryGetGlobalType<'MLContext'>;
gpuDevice: TryGetGlobalType<'GPUDevice'>;
}
/**
* Options for WebNN execution provider.
*/
type WebNNExecutionProviderOption = WebNNOptionsWithoutMLContext | WebNNOptionsWithMLContext | WebNNOptionsWebGpu;
interface QnnExecutionProviderOption extends ExecutionProviderOption {
readonly name: 'qnn';
/**
* Specify the QNN backend type. E.g., 'cpu' or 'htp'.
* Mutually exclusive with `backendPath`.
*
* @default 'htp'
*/
backendType?: string;
/**
* Specify a path to the QNN backend library.
* Mutually exclusive with `backendType`.
*/
backendPath?: string;
/**
* Specify whether to enable HTP FP16 precision.
*
* @default true
*/
enableFp16Precision?: boolean;
}
interface CoreMLExecutionProviderOption extends ExecutionProviderOption {
readonly name: 'coreml';
/**
* The bit flags for CoreML execution provider.
*
* ```
* COREML_FLAG_USE_CPU_ONLY = 0x001
* COREML_FLAG_ENABLE_ON_SUBGRAPH = 0x002
* COREML_FLAG_ONLY_ENABLE_DEVICE_WITH_ANE = 0x004
* COREML_FLAG_ONLY_ALLOW_STATIC_INPUT_SHAPES = 0x008
* COREML_FLAG_CREATE_MLPROGRAM = 0x010
* COREML_FLAG_USE_CPU_AND_GPU = 0x020
* ```
*
* See include/onnxruntime/core/providers/coreml/coreml_provider_factory.h for more details.
*
* This flag is available only in ONNXRuntime (Node.js binding).
*/
coreMlFlags?: number;
/**
* Specify whether to use CPU only in CoreML EP.
*
* This setting is available only in ONNXRuntime (react-native).
*/
useCPUOnly?: boolean;
useCPUAndGPU?: boolean;
/**
* Specify whether to enable CoreML EP on subgraph.
*
* This setting is available only in ONNXRuntime (react-native).
*/
enableOnSubgraph?: boolean;
/**
* Specify whether to only enable CoreML EP for Apple devices with ANE (Apple Neural Engine).
*
* This setting is available only in ONNXRuntime (react-native).
*/
onlyEnableDeviceWithANE?: boolean;
}
interface NnapiExecutionProviderOption extends ExecutionProviderOption {
readonly name: 'nnapi';
useFP16?: boolean;
useNCHW?: boolean;
cpuDisabled?: boolean;
cpuOnly?: boolean;
}
/**
* A set of configurations for inference run behavior
*/
interface RunOptions {
/**
* Log severity level. See
* https://github.com/microsoft/onnxruntime/blob/main/include/onnxruntime/core/common/logging/severity.h
*
* This setting is available only in ONNXRuntime (Node.js binding and react-native) or WebAssembly backend
*/
logSeverityLevel?: 0 | 1 | 2 | 3 | 4;
/**
* Log verbosity level.
*
* This setting is available only in WebAssembly backend. Will support Node.js binding and react-native later
*/
logVerbosityLevel?: number;
/**
* Terminate all incomplete OrtRun calls as soon as possible if true
*
* This setting is available only in WebAssembly backend. Will support Node.js binding and react-native later
*/
terminate?: boolean;
/**
* A tag for the Run() calls using this
*
* This setting is available only in ONNXRuntime (Node.js binding and react-native) or WebAssembly backend
*/
tag?: string;
/**
* Set a single run configuration entry. See
* https://github.com/microsoft/onnxruntime/blob/main/include/onnxruntime/core/session/
* onnxruntime_run_options_config_keys.h
*
* This setting is available only in WebAssembly backend. Will support Node.js binding and react-native later
*
* @example
*
* ```js
* extra: {
* memory: {
* enable_memory_arena_shrinkage: "1",
* }
* }
* ```
*/
extra?: Record<string, unknown>;
}
/**
* The common part of the value metadata type for both tensor and non-tensor values.
*/
interface ValueMetadataBase {
/**
* The name of the specified input or output.
*/
readonly name: string;
}
/**
* Represents the metadata of a non-tensor value.
*/
interface NonTensorValueMetadata extends ValueMetadataBase {
/**
* Get a value indicating whether the value is a tensor.
*/
readonly isTensor: false;
}
/**
* Represents the metadata of a tensor value.
*/
interface TensorValueMetadata extends ValueMetadataBase {
/**
* Get a value indicating whether the value is a tensor.
*/
readonly isTensor: true;
/**
* Get the data type of the tensor.
*/
readonly type: Tensor.Type;
/**
* Get the shape of the tensor.
*
* If the shape is not defined, the value will an empty array. Otherwise, it will be an array representing the shape
* of the tensor. Each element in the array can be a number or a string. If the element is a number, it represents
* the corresponding dimension size. If the element is a string, it represents a symbolic dimension.
*/
readonly shape: ReadonlyArray<number | string>;
}
/**
* Represents the metadata of a value.
*/
type ValueMetadata = NonTensorValueMetadata | TensorValueMetadata;
}
/**
* Represent a runtime instance of an ONNX model.
*/
export interface InferenceSession {
/**
* Execute the model asynchronously with the given feeds and options.
*
* @param feeds - Representation of the model input. See type description of `InferenceSession.InputType` for detail.
* @param options - Optional. A set of options that controls the behavior of model inference.
* @returns A promise that resolves to a map, which uses output names as keys and OnnxValue as corresponding values.
*/
run(feeds: InferenceSession.FeedsType, options?: InferenceSession.RunOptions): Promise<InferenceSession.ReturnType>;
/**
* Execute the model asynchronously with the given feeds, fetches and options.
*
* @param feeds - Representation of the model input. See type description of `InferenceSession.InputType` for detail.
* @param fetches - Representation of the model output. See type description of `InferenceSession.OutputType` for
* detail.
* @param options - Optional. A set of options that controls the behavior of model inference.
* @returns A promise that resolves to a map, which uses output names as keys and OnnxValue as corresponding values.
*/
run(feeds: InferenceSession.FeedsType, fetches: InferenceSession.FetchesType, options?: InferenceSession.RunOptions): Promise<InferenceSession.ReturnType>;
/**
* Release the inference session and the underlying resources.
*/
release(): Promise<void>;
/**
* Start profiling.
*/
startProfiling(): void;
/**
* End profiling.
*/
endProfiling(): void;
/**
* Get input names of the loaded model.
*/
readonly inputNames: readonly string[];
/**
* Get output names of the loaded model.
*/
readonly outputNames: readonly string[];
/**
* Get input metadata of the loaded model.
*/
readonly inputMetadata: readonly InferenceSession.ValueMetadata[];
/**
* Get output metadata of the loaded model.
*/
readonly outputMetadata: readonly InferenceSession.ValueMetadata[];
}
export interface InferenceSessionFactory {
/**
* Create a new inference session and load model asynchronously from an ONNX model file.
*
* @param uri - The URI or file path of the model to load.
* @param options - specify configuration for creating a new inference session.
* @returns A promise that resolves to an InferenceSession object.
*/
create(uri: string, options?: InferenceSession.SessionOptions): Promise<InferenceSession>;
/**
* Create a new inference session and load model asynchronously from an array bufer.
*
* @param buffer - An ArrayBuffer representation of an ONNX model.
* @param options - specify configuration for creating a new inference session.
* @returns A promise that resolves to an InferenceSession object.
*/
create(buffer: ArrayBufferLike, options?: InferenceSession.SessionOptions): Promise<InferenceSession>;
/**
* Create a new inference session and load model asynchronously from segment of an array bufer.
*
* @param buffer - An ArrayBuffer representation of an ONNX model.
* @param byteOffset - The beginning of the specified portion of the array buffer.
* @param byteLength - The length in bytes of the array buffer.
* @param options - specify configuration for creating a new inference session.
* @returns A promise that resolves to an InferenceSession object.
*/
create(buffer: ArrayBufferLike, byteOffset: number, byteLength?: number, options?: InferenceSession.SessionOptions): Promise<InferenceSession>;
/**
* Create a new inference session and load model asynchronously from a Uint8Array.
*
* @param buffer - A Uint8Array representation of an ONNX model.
* @param options - specify configuration for creating a new inference session.
* @returns A promise that resolves to an InferenceSession object.
*/
create(buffer: Uint8Array, options?: InferenceSession.SessionOptions): Promise<InferenceSession>;
}
export declare const InferenceSession: InferenceSessionFactory;
//# sourceMappingURL=inference-session.d.ts.map
File diff suppressed because one or more lines are too long
@@ -0,0 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { InferenceSession as InferenceSessionImpl } from './inference-session-impl.js';
// eslint-disable-next-line @typescript-eslint/naming-convention
export const InferenceSession = InferenceSessionImpl;
//# sourceMappingURL=inference-session.js.map
@@ -0,0 +1 @@
{"version":3,"file":"inference-session.js","sourceRoot":"","sources":["../../lib/inference-session.ts"],"names":[],"mappings":"AAAA,4DAA4D;AAC5D,kCAAkC;AAElC,OAAO,EAAE,gBAAgB,IAAI,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AAsoBvF,gEAAgE;AAChE,MAAM,CAAC,MAAM,gBAAgB,GAA4B,oBAAoB,CAAC"}
+49
View File
@@ -0,0 +1,49 @@
/**
* A string that represents a file's URL or path.
*
* Path is vailable only in onnxruntime-node or onnxruntime-web running in Node.js.
*/
export type FileUrlOrPath = string;
/**
* A Blob object that represents a file.
*/
export type FileBlob = Blob;
/**
* A Uint8Array, ArrayBuffer or SharedArrayBuffer object that represents a file content.
*
* When it is an ArrayBuffer or SharedArrayBuffer, the whole buffer is assumed to be the file content.
*/
export type FileData = Uint8Array | ArrayBufferLike;
/**
* Represents a file that can be loaded by the ONNX Runtime JavaScript API.
*/
export type FileType = FileUrlOrPath | FileBlob | FileData;
/**
* Represents an external data file.
*/
export interface ExternalDataFileDescription {
/**
* Specify the external data file.
*/
data: FileType;
/**
* Specify the file path.
*/
path: string;
}
/**
* Represents an external data file.
*
* When using a string, it should be a file URL or path that in the same directory as the model file.
*/
export type ExternalDataFileType = ExternalDataFileDescription | FileUrlOrPath;
/**
* Options for model loading.
*/
export interface OnnxModelOptions {
/**
* Specifying a list of files that represents the external data.
*/
externalData?: readonly ExternalDataFileType[];
}
//# sourceMappingURL=onnx-model.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"onnx-model.d.ts","sourceRoot":"","sources":["../../lib/onnx-model.ts"],"names":[],"mappings":"AAGA;;;;GAIG;AACH,MAAM,MAAM,aAAa,GAAG,MAAM,CAAC;AAEnC;;GAEG;AACH,MAAM,MAAM,QAAQ,GAAG,IAAI,CAAC;AAE5B;;;;GAIG;AACH,MAAM,MAAM,QAAQ,GAAG,UAAU,GAAG,eAAe,CAAC;AAEpD;;GAEG;AACH,MAAM,MAAM,QAAQ,GAAG,aAAa,GAAG,QAAQ,GAAG,QAAQ,CAAC;AAE3D;;GAEG;AACH,MAAM,WAAW,2BAA2B;IAC1C;;OAEG;IACH,IAAI,EAAE,QAAQ,CAAC;IACf;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;CACd;AAED;;;;GAIG;AACH,MAAM,MAAM,oBAAoB,GAAG,2BAA2B,GAAG,aAAa,CAAC;AAE/E;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;OAEG;IACH,YAAY,CAAC,EAAE,SAAS,oBAAoB,EAAE,CAAC;CAChD"}
+4
View File
@@ -0,0 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
export {};
//# sourceMappingURL=onnx-model.js.map
@@ -0,0 +1 @@
{"version":3,"file":"onnx-model.js","sourceRoot":"","sources":["../../lib/onnx-model.ts"],"names":[],"mappings":"AAAA,4DAA4D;AAC5D,kCAAkC"}
+13
View File
@@ -0,0 +1,13 @@
import { Tensor } from './tensor.js';
export type NonTensorType = never;
/**
* Type OnnxValue Represents both tensors and non-tensors value for model's inputs/outputs.
*
* NOTE: currently not support non-tensor
*/
export type OnnxValue = Tensor | NonTensorType;
/**
* Type OnnxValueDataLocation represents the location of the data of an OnnxValue.
*/
export type OnnxValueDataLocation = Tensor.DataLocation;
//# sourceMappingURL=onnx-value.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"onnx-value.d.ts","sourceRoot":"","sources":["../../lib/onnx-value.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,MAAM,MAAM,aAAa,GAAG,KAAK,CAAC;AAElC;;;;GAIG;AACH,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,aAAa,CAAC;AAE/C;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAAG,MAAM,CAAC,YAAY,CAAC"}
+4
View File
@@ -0,0 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
export {};
//# sourceMappingURL=onnx-value.js.map
@@ -0,0 +1 @@
{"version":3,"file":"onnx-value.js","sourceRoot":"","sources":["../../lib/onnx-value.ts"],"names":[],"mappings":"AAAA,4DAA4D;AAC5D,kCAAkC"}
+1
View File
@@ -0,0 +1 @@
{"type": "module"}
@@ -0,0 +1,11 @@
import { TensorToDataUrlOptions, TensorToImageDataOptions } from './tensor-conversion.js';
import { Tensor } from './tensor.js';
/**
* implementation of Tensor.toDataURL()
*/
export declare const tensorToDataURL: (tensor: Tensor, options?: TensorToDataUrlOptions) => string;
/**
* implementation of Tensor.toImageData()
*/
export declare const tensorToImageData: (tensor: Tensor, options?: TensorToImageDataOptions) => ImageData;
//# sourceMappingURL=tensor-conversion-impl.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"tensor-conversion-impl.d.ts","sourceRoot":"","sources":["../../lib/tensor-conversion-impl.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,sBAAsB,EAAE,wBAAwB,EAAE,MAAM,wBAAwB,CAAC;AAC1F,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC;;GAEG;AACH,eAAO,MAAM,eAAe,GAAI,QAAQ,MAAM,EAAE,UAAU,sBAAsB,KAAG,MA8FlF,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,iBAAiB,GAAI,QAAQ,MAAM,EAAE,UAAU,wBAAwB,KAAG,SAyGtF,CAAC"}
@@ -0,0 +1,195 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
/**
* implementation of Tensor.toDataURL()
*/
export const tensorToDataURL = (tensor, options) => {
const canvas = typeof document !== 'undefined' ? document.createElement('canvas') : new OffscreenCanvas(1, 1);
canvas.width = tensor.dims[3];
canvas.height = tensor.dims[2];
const pixels2DContext = canvas.getContext('2d');
if (pixels2DContext != null) {
// Default values for height and width & format
let width;
let height;
if (options?.tensorLayout !== undefined && options.tensorLayout === 'NHWC') {
width = tensor.dims[2];
height = tensor.dims[3];
}
else {
// Default layout is NCWH
width = tensor.dims[3];
height = tensor.dims[2];
}
const inputformat = options?.format !== undefined ? options.format : 'RGB';
const norm = options?.norm;
let normMean;
let normBias;
if (norm === undefined || norm.mean === undefined) {
normMean = [255, 255, 255, 255];
}
else {
if (typeof norm.mean === 'number') {
normMean = [norm.mean, norm.mean, norm.mean, norm.mean];
}
else {
normMean = [norm.mean[0], norm.mean[1], norm.mean[2], 0];
if (norm.mean[3] !== undefined) {
normMean[3] = norm.mean[3];
}
}
}
if (norm === undefined || norm.bias === undefined) {
normBias = [0, 0, 0, 0];
}
else {
if (typeof norm.bias === 'number') {
normBias = [norm.bias, norm.bias, norm.bias, norm.bias];
}
else {
normBias = [norm.bias[0], norm.bias[1], norm.bias[2], 0];
if (norm.bias[3] !== undefined) {
normBias[3] = norm.bias[3];
}
}
}
const stride = height * width;
// Default pointer assignments
let rTensorPointer = 0, gTensorPointer = stride, bTensorPointer = stride * 2, aTensorPointer = -1;
// Updating the pointer assignments based on the input image format
if (inputformat === 'RGBA') {
rTensorPointer = 0;
gTensorPointer = stride;
bTensorPointer = stride * 2;
aTensorPointer = stride * 3;
}
else if (inputformat === 'RGB') {
rTensorPointer = 0;
gTensorPointer = stride;
bTensorPointer = stride * 2;
}
else if (inputformat === 'RBG') {
rTensorPointer = 0;
bTensorPointer = stride;
gTensorPointer = stride * 2;
}
for (let i = 0; i < height; i++) {
for (let j = 0; j < width; j++) {
const R = (tensor.data[rTensorPointer++] - normBias[0]) * normMean[0]; // R value
const G = (tensor.data[gTensorPointer++] - normBias[1]) * normMean[1]; // G value
const B = (tensor.data[bTensorPointer++] - normBias[2]) * normMean[2]; // B value
const A = aTensorPointer === -1 ? 255 : (tensor.data[aTensorPointer++] - normBias[3]) * normMean[3]; // A value
pixels2DContext.fillStyle = 'rgba(' + R + ',' + G + ',' + B + ',' + A + ')';
pixels2DContext.fillRect(j, i, 1, 1);
}
}
if ('toDataURL' in canvas) {
return canvas.toDataURL();
}
else {
throw new Error('toDataURL is not supported');
}
}
else {
throw new Error('Can not access image data');
}
};
/**
* implementation of Tensor.toImageData()
*/
export const tensorToImageData = (tensor, options) => {
const pixels2DContext = typeof document !== 'undefined'
? document.createElement('canvas').getContext('2d')
: new OffscreenCanvas(1, 1).getContext('2d');
let image;
if (pixels2DContext != null) {
// Default values for height and width & format
let width;
let height;
let channels;
if (options?.tensorLayout !== undefined && options.tensorLayout === 'NHWC') {
width = tensor.dims[2];
height = tensor.dims[1];
channels = tensor.dims[3];
}
else {
// Default layout is NCWH
width = tensor.dims[3];
height = tensor.dims[2];
channels = tensor.dims[1];
}
const inputformat = options !== undefined ? (options.format !== undefined ? options.format : 'RGB') : 'RGB';
const norm = options?.norm;
let normMean;
let normBias;
if (norm === undefined || norm.mean === undefined) {
normMean = [255, 255, 255, 255];
}
else {
if (typeof norm.mean === 'number') {
normMean = [norm.mean, norm.mean, norm.mean, norm.mean];
}
else {
normMean = [norm.mean[0], norm.mean[1], norm.mean[2], 255];
if (norm.mean[3] !== undefined) {
normMean[3] = norm.mean[3];
}
}
}
if (norm === undefined || norm.bias === undefined) {
normBias = [0, 0, 0, 0];
}
else {
if (typeof norm.bias === 'number') {
normBias = [norm.bias, norm.bias, norm.bias, norm.bias];
}
else {
normBias = [norm.bias[0], norm.bias[1], norm.bias[2], 0];
if (norm.bias[3] !== undefined) {
normBias[3] = norm.bias[3];
}
}
}
const stride = height * width;
if (options !== undefined) {
if ((options.format !== undefined && channels === 4 && options.format !== 'RGBA') ||
(channels === 3 && options.format !== 'RGB' && options.format !== 'BGR')) {
throw new Error("Tensor format doesn't match input tensor dims");
}
}
// Default pointer assignments
const step = 4;
let rImagePointer = 0, gImagePointer = 1, bImagePointer = 2, aImagePointer = 3;
let rTensorPointer = 0, gTensorPointer = stride, bTensorPointer = stride * 2, aTensorPointer = -1;
// Updating the pointer assignments based on the input image format
if (inputformat === 'RGBA') {
rTensorPointer = 0;
gTensorPointer = stride;
bTensorPointer = stride * 2;
aTensorPointer = stride * 3;
}
else if (inputformat === 'RGB') {
rTensorPointer = 0;
gTensorPointer = stride;
bTensorPointer = stride * 2;
}
else if (inputformat === 'RBG') {
rTensorPointer = 0;
bTensorPointer = stride;
gTensorPointer = stride * 2;
}
image = pixels2DContext.createImageData(width, height);
for (let i = 0; i < height * width; rImagePointer += step, gImagePointer += step, bImagePointer += step, aImagePointer += step, i++) {
image.data[rImagePointer] = (tensor.data[rTensorPointer++] - normBias[0]) * normMean[0]; // R value
image.data[gImagePointer] = (tensor.data[gTensorPointer++] - normBias[1]) * normMean[1]; // G value
image.data[bImagePointer] = (tensor.data[bTensorPointer++] - normBias[2]) * normMean[2]; // B value
image.data[aImagePointer] =
aTensorPointer === -1 ? 255 : (tensor.data[aTensorPointer++] - normBias[3]) * normMean[3]; // A value
}
}
else {
throw new Error('Can not access image data');
}
return image;
};
//# sourceMappingURL=tensor-conversion-impl.js.map
File diff suppressed because one or more lines are too long
@@ -0,0 +1,30 @@
import { OptionsFormat, OptionsNormalizationParameters, OptionsTensorLayout } from './tensor-factory.js';
export interface TensorToDataUrlOptions extends OptionsTensorLayout, OptionsFormat, OptionsNormalizationParameters {
}
export interface TensorToImageDataOptions extends OptionsTensorLayout, OptionsFormat, OptionsNormalizationParameters {
}
export interface ConversionUtils {
/**
* creates a DataURL instance from tensor
*
* @param options - An optional object representing options for creating a DataURL instance from the tensor.
*
* The following default settings will be applied:
* - `format`: `'RGB'`
* - `tensorLayout`: `'NCHW'`
* @returns a DataURL string representing the image converted from tensor data
*/
toDataURL(options?: TensorToDataUrlOptions): string;
/**
* creates an ImageData instance from tensor
*
* @param options - An optional object representing options for creating an ImageData instance from the tensor.
*
* The following default settings will be applied:
* - `format`: `'RGB'`
* - `tensorLayout`: `'NCHW'`
* @returns an ImageData instance representing the image converted from tensor data
*/
toImageData(options?: TensorToImageDataOptions): ImageData;
}
//# sourceMappingURL=tensor-conversion.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"tensor-conversion.d.ts","sourceRoot":"","sources":["../../lib/tensor-conversion.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,aAAa,EAAE,8BAA8B,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;AAEzG,MAAM,WAAW,sBAAuB,SAAQ,mBAAmB,EAAE,aAAa,EAAE,8BAA8B;CAAG;AAErH,MAAM,WAAW,wBAAyB,SAAQ,mBAAmB,EAAE,aAAa,EAAE,8BAA8B;CAAG;AAEvH,MAAM,WAAW,eAAe;IAC9B;;;;;;;;;OASG;IACH,SAAS,CAAC,OAAO,CAAC,EAAE,sBAAsB,GAAG,MAAM,CAAC;IAEpD;;;;;;;;;OASG;IACH,WAAW,CAAC,OAAO,CAAC,EAAE,wBAAwB,GAAG,SAAS,CAAC;CAC5D"}
@@ -0,0 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
export {};
//# sourceMappingURL=tensor-conversion.js.map
@@ -0,0 +1 @@
{"version":3,"file":"tensor-conversion.js","sourceRoot":"","sources":["../../lib/tensor-conversion.ts"],"names":[],"mappings":"AAAA,4DAA4D;AAC5D,kCAAkC"}
@@ -0,0 +1,35 @@
import { OptionsDimensions, OptionsFormat, OptionsNormalizationParameters, OptionsTensorFormat, OptionsTensorLayout, TensorFromGpuBufferOptions, TensorFromImageBitmapOptions, TensorFromImageDataOptions, TensorFromImageElementOptions, TensorFromMLTensorOptions, TensorFromTextureOptions, TensorFromUrlOptions } from './tensor-factory.js';
import { Tensor } from './tensor-impl.js';
import { Tensor as TensorInterface } from './tensor.js';
interface BufferToTensorOptions extends OptionsDimensions, OptionsTensorLayout, OptionsNormalizationParameters, OptionsFormat, OptionsTensorFormat {
}
/**
* Create a new tensor object from image object
*
* @param buffer - Extracted image buffer data - assuming RGBA format
* @param imageFormat - input image configuration - required configurations height, width, format
* @param tensorFormat - output tensor configuration - Default is RGB format
*/
export declare const bufferToTensor: (buffer: Uint8ClampedArray | undefined, options: BufferToTensorOptions) => Tensor;
/**
* implementation of Tensor.fromImage().
*/
export declare const tensorFromImage: (image: ImageData | HTMLImageElement | ImageBitmap | string, options?: TensorFromImageDataOptions | TensorFromImageElementOptions | TensorFromImageBitmapOptions | TensorFromUrlOptions) => Promise<Tensor>;
/**
* implementation of Tensor.fromTexture().
*/
export declare const tensorFromTexture: <T extends TensorInterface.TextureDataTypes>(texture: TensorInterface.TextureType, options: TensorFromTextureOptions<T>) => Tensor;
/**
* implementation of Tensor.fromGpuBuffer().
*/
export declare const tensorFromGpuBuffer: <T extends TensorInterface.GpuBufferDataTypes>(gpuBuffer: TensorInterface.GpuBufferType, options: TensorFromGpuBufferOptions<T>) => Tensor;
/**
* implementation of Tensor.fromMLTensor().
*/
export declare const tensorFromMLTensor: <T extends TensorInterface.MLTensorDataTypes>(mlTensor: TensorInterface.MLTensorType, options: TensorFromMLTensorOptions<T>) => Tensor;
/**
* implementation of Tensor.fromPinnedBuffer().
*/
export declare const tensorFromPinnedBuffer: <T extends TensorInterface.CpuPinnedDataTypes>(type: T, buffer: TensorInterface.DataTypeMap[T], dims?: readonly number[]) => Tensor;
export {};
//# sourceMappingURL=tensor-factory-impl.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"tensor-factory-impl.d.ts","sourceRoot":"","sources":["../../lib/tensor-factory-impl.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,iBAAiB,EACjB,aAAa,EACb,8BAA8B,EAC9B,mBAAmB,EACnB,mBAAmB,EACnB,0BAA0B,EAC1B,4BAA4B,EAC5B,0BAA0B,EAC1B,6BAA6B,EAC7B,yBAAyB,EACzB,wBAAwB,EACxB,oBAAoB,EACrB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EAAE,MAAM,IAAI,eAAe,EAAE,MAAM,aAAa,CAAC;AAExD,UAAU,qBACR,SAAQ,iBAAiB,EAAE,mBAAmB,EAAE,8BAA8B,EAAE,aAAa,EAAE,mBAAmB;CAAG;AAEvH;;;;;;GAMG;AACH,eAAO,MAAM,cAAc,GAAI,QAAQ,iBAAiB,GAAG,SAAS,EAAE,SAAS,qBAAqB,KAAG,MAyFtG,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,eAAe,GAC1B,OAAO,SAAS,GAAG,gBAAgB,GAAG,WAAW,GAAG,MAAM,EAC1D,UACI,0BAA0B,GAC1B,6BAA6B,GAC7B,4BAA4B,GAC5B,oBAAoB,KACvB,OAAO,CAAC,MAAM,CAwJhB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,iBAAiB,GAAI,CAAC,SAAS,eAAe,CAAC,gBAAgB,EAC1E,SAAS,eAAe,CAAC,WAAW,EACpC,SAAS,wBAAwB,CAAC,CAAC,CAAC,KACnC,MAKF,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,mBAAmB,GAAI,CAAC,SAAS,eAAe,CAAC,kBAAkB,EAC9E,WAAW,eAAe,CAAC,aAAa,EACxC,SAAS,0BAA0B,CAAC,CAAC,CAAC,KACrC,MAGF,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,kBAAkB,GAAI,CAAC,SAAS,eAAe,CAAC,iBAAiB,EAC5E,UAAU,eAAe,CAAC,YAAY,EACtC,SAAS,yBAAyB,CAAC,CAAC,CAAC,KACpC,MAGF,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,sBAAsB,GAAI,CAAC,SAAS,eAAe,CAAC,kBAAkB,EACjF,MAAM,CAAC,EACP,QAAQ,eAAe,CAAC,WAAW,CAAC,CAAC,CAAC,EACtC,OAAO,SAAS,MAAM,EAAE,KACvB,MAAmG,CAAC"}
@@ -0,0 +1,265 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { Tensor } from './tensor-impl.js';
/**
* Create a new tensor object from image object
*
* @param buffer - Extracted image buffer data - assuming RGBA format
* @param imageFormat - input image configuration - required configurations height, width, format
* @param tensorFormat - output tensor configuration - Default is RGB format
*/
export const bufferToTensor = (buffer, options) => {
if (buffer === undefined) {
throw new Error('Image buffer must be defined');
}
if (options.height === undefined || options.width === undefined) {
throw new Error('Image height and width must be defined');
}
if (options.tensorLayout === 'NHWC') {
throw new Error('NHWC Tensor layout is not supported yet');
}
const { height, width } = options;
const norm = options.norm ?? { mean: 255, bias: 0 };
let normMean;
let normBias;
if (typeof norm.mean === 'number') {
normMean = [norm.mean, norm.mean, norm.mean, norm.mean];
}
else {
normMean = [norm.mean[0], norm.mean[1], norm.mean[2], norm.mean[3] ?? 255];
}
if (typeof norm.bias === 'number') {
normBias = [norm.bias, norm.bias, norm.bias, norm.bias];
}
else {
normBias = [norm.bias[0], norm.bias[1], norm.bias[2], norm.bias[3] ?? 0];
}
const inputformat = options.format !== undefined ? options.format : 'RGBA';
// default value is RGBA since imagedata and HTMLImageElement uses it
const outputformat = options.tensorFormat !== undefined ? (options.tensorFormat !== undefined ? options.tensorFormat : 'RGB') : 'RGB';
const stride = height * width;
const float32Data = outputformat === 'RGBA' ? new Float32Array(stride * 4) : new Float32Array(stride * 3);
// Default pointer assignments
let step = 4, rImagePointer = 0, gImagePointer = 1, bImagePointer = 2, aImagePointer = 3;
let rTensorPointer = 0, gTensorPointer = stride, bTensorPointer = stride * 2, aTensorPointer = -1;
// Updating the pointer assignments based on the input image format
if (inputformat === 'RGB') {
step = 3;
rImagePointer = 0;
gImagePointer = 1;
bImagePointer = 2;
aImagePointer = -1;
}
// Updating the pointer assignments based on the output tensor format
if (outputformat === 'RGBA') {
aTensorPointer = stride * 3;
}
else if (outputformat === 'RBG') {
rTensorPointer = 0;
bTensorPointer = stride;
gTensorPointer = stride * 2;
}
else if (outputformat === 'BGR') {
bTensorPointer = 0;
gTensorPointer = stride;
rTensorPointer = stride * 2;
}
for (let i = 0; i < stride; i++, rImagePointer += step, bImagePointer += step, gImagePointer += step, aImagePointer += step) {
float32Data[rTensorPointer++] = (buffer[rImagePointer] + normBias[0]) / normMean[0];
float32Data[gTensorPointer++] = (buffer[gImagePointer] + normBias[1]) / normMean[1];
float32Data[bTensorPointer++] = (buffer[bImagePointer] + normBias[2]) / normMean[2];
if (aTensorPointer !== -1 && aImagePointer !== -1) {
float32Data[aTensorPointer++] = (buffer[aImagePointer] + normBias[3]) / normMean[3];
}
}
// Float32Array -> ort.Tensor
const outputTensor = outputformat === 'RGBA'
? new Tensor('float32', float32Data, [1, 4, height, width])
: new Tensor('float32', float32Data, [1, 3, height, width]);
return outputTensor;
};
/**
* implementation of Tensor.fromImage().
*/
export const tensorFromImage = async (image, options) => {
// checking the type of image object
const isHTMLImageEle = typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement;
const isImageDataEle = typeof ImageData !== 'undefined' && image instanceof ImageData;
const isImageBitmap = typeof ImageBitmap !== 'undefined' && image instanceof ImageBitmap;
const isString = typeof image === 'string';
let data;
let bufferToTensorOptions = options ?? {};
const createCanvas = () => {
if (typeof document !== 'undefined') {
return document.createElement('canvas');
}
else if (typeof OffscreenCanvas !== 'undefined') {
return new OffscreenCanvas(1, 1);
}
else {
throw new Error('Canvas is not supported');
}
};
const createCanvasContext = (canvas) => {
if (typeof HTMLCanvasElement !== 'undefined' && canvas instanceof HTMLCanvasElement) {
return canvas.getContext('2d');
}
else if (canvas instanceof OffscreenCanvas) {
return canvas.getContext('2d');
}
else {
return null;
}
};
// filling and checking image configuration options
if (isHTMLImageEle) {
// HTMLImageElement - image object - format is RGBA by default
const canvas = createCanvas();
canvas.width = image.width;
canvas.height = image.height;
const pixels2DContext = createCanvasContext(canvas);
if (pixels2DContext != null) {
let height = image.height;
let width = image.width;
if (options !== undefined && options.resizedHeight !== undefined && options.resizedWidth !== undefined) {
height = options.resizedHeight;
width = options.resizedWidth;
}
if (options !== undefined) {
bufferToTensorOptions = options;
if (options.tensorFormat !== undefined) {
throw new Error('Image input config format must be RGBA for HTMLImageElement');
}
else {
bufferToTensorOptions.tensorFormat = 'RGBA';
}
bufferToTensorOptions.height = height;
bufferToTensorOptions.width = width;
}
else {
bufferToTensorOptions.tensorFormat = 'RGBA';
bufferToTensorOptions.height = height;
bufferToTensorOptions.width = width;
}
pixels2DContext.drawImage(image, 0, 0);
data = pixels2DContext.getImageData(0, 0, width, height).data;
}
else {
throw new Error('Can not access image data');
}
}
else if (isImageDataEle) {
let height;
let width;
if (options !== undefined && options.resizedWidth !== undefined && options.resizedHeight !== undefined) {
height = options.resizedHeight;
width = options.resizedWidth;
}
else {
height = image.height;
width = image.width;
}
if (options !== undefined) {
bufferToTensorOptions = options;
}
bufferToTensorOptions.format = 'RGBA';
bufferToTensorOptions.height = height;
bufferToTensorOptions.width = width;
if (options !== undefined) {
const tempCanvas = createCanvas();
tempCanvas.width = width;
tempCanvas.height = height;
const pixels2DContext = createCanvasContext(tempCanvas);
if (pixels2DContext != null) {
pixels2DContext.putImageData(image, 0, 0);
data = pixels2DContext.getImageData(0, 0, width, height).data;
}
else {
throw new Error('Can not access image data');
}
}
else {
data = image.data;
}
}
else if (isImageBitmap) {
// ImageBitmap - image object - format must be provided by user
if (options === undefined) {
throw new Error('Please provide image config with format for Imagebitmap');
}
const canvas = createCanvas();
canvas.width = image.width;
canvas.height = image.height;
const pixels2DContext = createCanvasContext(canvas);
if (pixels2DContext != null) {
const height = image.height;
const width = image.width;
pixels2DContext.drawImage(image, 0, 0, width, height);
data = pixels2DContext.getImageData(0, 0, width, height).data;
bufferToTensorOptions.height = height;
bufferToTensorOptions.width = width;
return bufferToTensor(data, bufferToTensorOptions);
}
else {
throw new Error('Can not access image data');
}
}
else if (isString) {
return new Promise((resolve, reject) => {
const canvas = createCanvas();
const context = createCanvasContext(canvas);
if (!image || !context) {
return reject();
}
const newImage = new Image();
newImage.crossOrigin = 'Anonymous';
newImage.src = image;
newImage.onload = () => {
canvas.width = newImage.width;
canvas.height = newImage.height;
context.drawImage(newImage, 0, 0, canvas.width, canvas.height);
const img = context.getImageData(0, 0, canvas.width, canvas.height);
bufferToTensorOptions.height = canvas.height;
bufferToTensorOptions.width = canvas.width;
resolve(bufferToTensor(img.data, bufferToTensorOptions));
};
});
}
else {
throw new Error('Input data provided is not supported - aborted tensor creation');
}
if (data !== undefined) {
return bufferToTensor(data, bufferToTensorOptions);
}
else {
throw new Error('Input data provided is not supported - aborted tensor creation');
}
};
/**
* implementation of Tensor.fromTexture().
*/
export const tensorFromTexture = (texture, options) => {
const { width, height, download, dispose } = options;
// Always assume RGBAF32. TODO: support different texture format
const dims = [1, height, width, 4];
return new Tensor({ location: 'texture', type: 'float32', texture, dims, download, dispose });
};
/**
* implementation of Tensor.fromGpuBuffer().
*/
export const tensorFromGpuBuffer = (gpuBuffer, options) => {
const { dataType, dims, download, dispose } = options;
return new Tensor({ location: 'gpu-buffer', type: dataType ?? 'float32', gpuBuffer, dims, download, dispose });
};
/**
* implementation of Tensor.fromMLTensor().
*/
export const tensorFromMLTensor = (mlTensor, options) => {
const { dataType, dims, download, dispose } = options;
return new Tensor({ location: 'ml-tensor', type: dataType ?? 'float32', mlTensor, dims, download, dispose });
};
/**
* implementation of Tensor.fromPinnedBuffer().
*/
export const tensorFromPinnedBuffer = (type, buffer, dims) => new Tensor({ location: 'cpu-pinned', type, data: buffer, dims: dims ?? [buffer.length] });
//# sourceMappingURL=tensor-factory-impl.js.map
File diff suppressed because one or more lines are too long
@@ -0,0 +1,298 @@
import { Tensor, TypedTensor } from './tensor.js';
export type ImageFormat = 'RGB' | 'RGBA' | 'BGR' | 'RBG';
export type ImageTensorLayout = 'NHWC' | 'NCHW';
/**
* represent common properties of the parameter for constructing a tensor from a specific location.
*/
interface CommonConstructorParameters<T> extends Pick<Tensor, 'dims'> {
/**
* Specify the data type of the tensor.
*/
readonly type: T;
}
/**
* represent the parameter for constructing a tensor from a GPU resource.
*/
interface GpuResourceConstructorParameters<T extends Tensor.Type> {
/**
* an optional callback function to download data from GPU to CPU.
*
* If not provided, the tensor treat the GPU data as external resource.
*/
download?(): Promise<Tensor.DataTypeMap[T]>;
/**
* an optional callback function that will be called when the tensor is disposed.
*
* If not provided, the tensor treat the GPU data as external resource.
*/
dispose?(): void;
}
/**
* represent the parameter for constructing a tensor from a pinned CPU buffer
*/
export interface CpuPinnedConstructorParameters<T extends Tensor.CpuPinnedDataTypes = Tensor.CpuPinnedDataTypes> extends CommonConstructorParameters<T> {
/**
* Specify the location of the data to be 'cpu-pinned'.
*/
readonly location: 'cpu-pinned';
/**
* Specify the CPU pinned buffer that holds the tensor data.
*/
readonly data: Tensor.DataTypeMap[T];
}
/**
* represent the parameter for constructing a tensor from a WebGL texture
*/
export interface TextureConstructorParameters<T extends Tensor.TextureDataTypes = Tensor.TextureDataTypes> extends CommonConstructorParameters<T>, GpuResourceConstructorParameters<T> {
/**
* Specify the location of the data to be 'texture'.
*/
readonly location: 'texture';
/**
* Specify the WebGL texture that holds the tensor data.
*/
readonly texture: Tensor.TextureType;
}
/**
* represent the parameter for constructing a tensor from a WebGPU buffer
*/
export interface GpuBufferConstructorParameters<T extends Tensor.GpuBufferDataTypes = Tensor.GpuBufferDataTypes> extends CommonConstructorParameters<T>, GpuResourceConstructorParameters<T> {
/**
* Specify the location of the data to be 'gpu-buffer'.
*/
readonly location: 'gpu-buffer';
/**
* Specify the WebGPU buffer that holds the tensor data.
*/
readonly gpuBuffer: Tensor.GpuBufferType;
}
export interface MLTensorConstructorParameters<T extends Tensor.MLTensorDataTypes = Tensor.MLTensorDataTypes> extends CommonConstructorParameters<T>, GpuResourceConstructorParameters<T> {
/**
* Specify the location of the data to be 'ml-tensor'.
*/
readonly location: 'ml-tensor';
/**
* Specify the WebNN MLTensor that holds the tensor data.
*/
readonly mlTensor: Tensor.MLTensorType;
}
export interface OptionsFormat {
/**
* Describes the image format represented in RGBA color space.
*/
format?: ImageFormat;
}
export interface OptionsTensorFormat {
/**
* Describes the image format of the tensor.
*
* NOTE: this is different from option 'format'. While option 'format' represents the original image, 'tensorFormat'
* represents the target format of the tensor. A transpose will be performed if they are different.
*/
tensorFormat?: ImageFormat;
}
export interface OptionsTensorDataType {
/**
* Describes the data type of the tensor.
*/
dataType?: 'float32' | 'uint8';
}
export interface OptionsTensorLayout {
/**
* Describes the tensor layout when representing data of one or more image(s).
*/
tensorLayout?: ImageTensorLayout;
}
export interface OptionsDimensions {
/**
* Describes the image height in pixel
*/
height?: number;
/**
* Describes the image width in pixel
*/
width?: number;
}
export interface OptionResizedDimensions {
/**
* Describes the resized height. If omitted, original height will be used.
*/
resizedHeight?: number;
/**
* Describes resized width - can be accessed via tensor dimensions as well
*/
resizedWidth?: number;
}
export interface OptionsNormalizationParameters {
/**
* Describes normalization parameters when preprocessing the image as model input.
*
* Data element are ranged from 0 to 255.
*/
norm?: {
/**
* The 'bias' value for image normalization.
* - If omitted, use default value 0.
* - If it's a single number, apply to each channel
* - If it's an array of 3 or 4 numbers, apply element-wise. Number of elements need to match the number of channels
* for the corresponding image format
*/
bias?: number | [number, number, number] | [number, number, number, number];
/**
* The 'mean' value for image normalization.
* - If omitted, use default value 255.
* - If it's a single number, apply to each channel
* - If it's an array of 3 or 4 numbers, apply element-wise. Number of elements need to match the number of channels
* for the corresponding image format
*/
mean?: number | [number, number, number] | [number, number, number, number];
};
}
export interface TensorFromImageDataOptions extends OptionResizedDimensions, OptionsTensorFormat, OptionsTensorLayout, OptionsTensorDataType, OptionsNormalizationParameters {
}
export interface TensorFromImageElementOptions extends OptionResizedDimensions, OptionsTensorFormat, OptionsTensorLayout, OptionsTensorDataType, OptionsNormalizationParameters {
}
export interface TensorFromUrlOptions extends OptionsDimensions, OptionResizedDimensions, OptionsTensorFormat, OptionsTensorLayout, OptionsTensorDataType, OptionsNormalizationParameters {
}
export interface TensorFromImageBitmapOptions extends OptionResizedDimensions, OptionsTensorFormat, OptionsTensorLayout, OptionsTensorDataType, OptionsNormalizationParameters {
}
export interface TensorFromTextureOptions<T extends Tensor.TextureDataTypes> extends Required<OptionsDimensions>, OptionsFormat, GpuResourceConstructorParameters<T> {
}
export interface TensorFromGpuBufferOptions<T extends Tensor.GpuBufferDataTypes> extends Pick<Tensor, 'dims'>, GpuResourceConstructorParameters<T> {
/**
* Describes the data type of the tensor.
*/
dataType?: T;
}
export interface TensorFromMLTensorOptions<T extends Tensor.MLTensorDataTypes> extends Pick<Tensor, 'dims'>, GpuResourceConstructorParameters<T> {
/**
* Describes the data type of the tensor.
*/
dataType?: T;
}
/**
* type TensorFactory defines the factory functions of 'Tensor' to create tensor instances from existing data or
* resources.
*/
export interface TensorFactory {
/**
* create a tensor from an ImageData object
*
* @param imageData - the ImageData object to create tensor from
* @param options - An optional object representing options for creating tensor from ImageData.
*
* The following default settings will be applied:
* - `tensorFormat`: `'RGB'`
* - `tensorLayout`: `'NCHW'`
* - `dataType`: `'float32'`
* @returns A promise that resolves to a tensor object
*/
fromImage(imageData: ImageData, options?: TensorFromImageDataOptions): Promise<TypedTensor<'float32'> | TypedTensor<'uint8'>>;
/**
* create a tensor from a HTMLImageElement object
*
* @param imageElement - the HTMLImageElement object to create tensor from
* @param options - An optional object representing options for creating tensor from HTMLImageElement.
*
* The following default settings will be applied:
* - `tensorFormat`: `'RGB'`
* - `tensorLayout`: `'NCHW'`
* - `dataType`: `'float32'`
* @returns A promise that resolves to a tensor object
*/
fromImage(imageElement: HTMLImageElement, options?: TensorFromImageElementOptions): Promise<TypedTensor<'float32'> | TypedTensor<'uint8'>>;
/**
* create a tensor from URL
*
* @param urlSource - a string as a URL to the image or a data URL containing the image data.
* @param options - An optional object representing options for creating tensor from URL.
*
* The following default settings will be applied:
* - `tensorFormat`: `'RGB'`
* - `tensorLayout`: `'NCHW'`
* - `dataType`: `'float32'`
* @returns A promise that resolves to a tensor object
*/
fromImage(urlSource: string, options?: TensorFromUrlOptions): Promise<TypedTensor<'float32'> | TypedTensor<'uint8'>>;
/**
* create a tensor from an ImageBitmap object
*
* @param bitmap - the ImageBitmap object to create tensor from
* @param options - An optional object representing options for creating tensor from URL.
*
* The following default settings will be applied:
* - `tensorFormat`: `'RGB'`
* - `tensorLayout`: `'NCHW'`
* - `dataType`: `'float32'`
* @returns A promise that resolves to a tensor object
*/
fromImage(bitmap: ImageBitmap, options: TensorFromImageBitmapOptions): Promise<TypedTensor<'float32'> | TypedTensor<'uint8'>>;
/**
* create a tensor from a WebGL texture
*
* @param texture - the WebGLTexture object to create tensor from
* @param options - An optional object representing options for creating tensor from WebGL texture.
*
* The options include following properties:
* - `width`: the width of the texture. Required.
* - `height`: the height of the texture. Required.
* - `format`: the format of the texture. If omitted, assume 'RGBA'.
* - `download`: an optional function to download the tensor data from GPU to CPU. If omitted, the GPU data
* will not be able to download. Usually, this is provided by a GPU backend for the inference outputs. Users don't
* need to provide this function.
* - `dispose`: an optional function to dispose the tensor data on GPU. If omitted, the GPU data will not be disposed.
* Usually, this is provided by a GPU backend for the inference outputs. Users don't need to provide this function.
*
* @returns a tensor object
*/
fromTexture<T extends Tensor.TextureDataTypes = 'float32'>(texture: Tensor.TextureType, options: TensorFromTextureOptions<T>): TypedTensor<'float32'>;
/**
* create a tensor from a WebGPU buffer
*
* @param buffer - the GPUBuffer object to create tensor from
* @param options - An optional object representing options for creating tensor from WebGPU buffer.
*
* The options include following properties:
* - `dataType`: the data type of the tensor. If omitted, assume 'float32'.
* - `dims`: the dimension of the tensor. Required.
* - `download`: an optional function to download the tensor data from GPU to CPU. If omitted, the GPU data
* will not be able to download. Usually, this is provided by a GPU backend for the inference outputs. Users don't
* need to provide this function.
* - `dispose`: an optional function to dispose the tensor data on GPU. If omitted, the GPU data will not be disposed.
* Usually, this is provided by a GPU backend for the inference outputs. Users don't need to provide this function.
*
* @returns a tensor object
*/
fromGpuBuffer<T extends Tensor.GpuBufferDataTypes>(buffer: Tensor.GpuBufferType, options: TensorFromGpuBufferOptions<T>): TypedTensor<T>;
/**
* create a tensor from a WebNN MLTensor
*
* @param tensor - the MLTensor object to create tensor from
* @param options - An optional object representing options for creating tensor from a WebNN MLTensor.
*
* The options include following properties:
* - `dataType`: the data type of the tensor. If omitted, assume 'float32'.
* - `dims`: the dimension of the tensor. Required.
* - `download`: an optional function to download the tensor data from the MLTensor to CPU. If omitted, the MLTensor
* data will not be able to download. Usually, this is provided by the WebNN backend for the inference outputs.
* Users don't need to provide this function.
* - `dispose`: an optional function to dispose the tensor data on the WebNN MLTensor. If omitted, the MLTensor will
* not be disposed. Usually, this is provided by the WebNN backend for the inference outputs. Users don't need to
* provide this function.
*
* @returns a tensor object
*/
fromMLTensor<T extends Tensor.MLTensorDataTypes>(tensor: Tensor.MLTensorType, options: TensorFromMLTensorOptions<T>): TypedTensor<T>;
/**
* create a tensor from a pre-allocated buffer. The buffer will be used as a pinned buffer.
*
* @param type - the tensor element type.
* @param buffer - a TypedArray corresponding to the type.
* @param dims - specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*
* @returns a tensor object
*/
fromPinnedBuffer<T extends Exclude<Tensor.Type, 'string'>>(type: T, buffer: Tensor.DataTypeMap[T], dims?: readonly number[]): TypedTensor<T>;
}
export {};
//# sourceMappingURL=tensor-factory.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"tensor-factory.d.ts","sourceRoot":"","sources":["../../lib/tensor-factory.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAElD,MAAM,MAAM,WAAW,GAAG,KAAK,GAAG,MAAM,GAAG,KAAK,GAAG,KAAK,CAAC;AACzD,MAAM,MAAM,iBAAiB,GAAG,MAAM,GAAG,MAAM,CAAC;AAMhD;;GAEG;AACH,UAAU,2BAA2B,CAAC,CAAC,CAAE,SAAQ,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC;IACnE;;OAEG;IACH,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC;CAClB;AAED;;GAEG;AACH,UAAU,gCAAgC,CAAC,CAAC,SAAS,MAAM,CAAC,IAAI;IAC9D;;;;OAIG;IACH,QAAQ,CAAC,IAAI,OAAO,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC;IAE5C;;;;OAIG;IACH,OAAO,CAAC,IAAI,IAAI,CAAC;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,8BAA8B,CAC7C,CAAC,SAAS,MAAM,CAAC,kBAAkB,GAAG,MAAM,CAAC,kBAAkB,CAC/D,SAAQ,2BAA2B,CAAC,CAAC,CAAC;IACtC;;OAEG;IACH,QAAQ,CAAC,QAAQ,EAAE,YAAY,CAAC;IAChC;;OAEG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AC,CAAC;IAE1D;;;;;;;;;;;;;;;;;OAiBG;IACH,WAAW,CAAC,CAAC,SAAS,MAAM,CAAC,gBAAgB,GAAG,SAAS,EACvD,OAAO,EAAE,MAAM,CAAC,WAAW,EAC3B,OAAO,EAAE,wBAAwB,CAAC,CAAC,CAAC,GACnC,WAAW,CAAC,SAAS,CAAC,CAAC;IAE1B;;;;;;;;;;;;;;;;OAgBG;IACH,aAAa,CAAC,CAAC,SAAS,MAAM,CAAC,kBAAkB,EAC/C,MAAM,EAAE,MAAM,CAAC,aAAa,EAC5B,OAAO,EAAE,0BAA0B,CAAC,CAAC,CAAC,GACrC,WAAW,CAAC,CAAC,CAAC,CAAC;IAElB;;;;;;;;;;;;;;;;;OAiBG;IACH,YAAY,CAAC,CAAC,SAAS,MAAM,CAAC,iBAAiB,EAC7C,MAAM,EAAE,MAAM,CAAC,YAAY,EAC3B,OAAO,EAAE,yBAAyB,CAAC,CAAC,CAAC,GACpC,WAAW,CAAC,CAAC,CAAC,CAAC;IAElB;;;;;;;;OAQG;IACH,gBAAgB,CAAC,CAAC,SAAS,OAAO,CAAC,MAAM,CAAC,IAAI,EAAE,QAAQ,CAAC,EACvD,IAAI,EAAE,CAAC,EACP,MAAM,EAAE,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC,EAC7B,IAAI,CAAC,EAAE,SAAS,MAAM,EAAE,GACvB,WAAW,CAAC,CAAC,CAAC,CAAC;CACnB"}
@@ -0,0 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
export {};
//# sourceMappingURL=tensor-factory.js.map
@@ -0,0 +1 @@
{"version":3,"file":"tensor-factory.js","sourceRoot":"","sources":["../../lib/tensor-factory.ts"],"names":[],"mappings":"AAAA,4DAA4D;AAC5D,kCAAkC"}
@@ -0,0 +1,7 @@
import { Tensor } from './tensor.js';
export type SupportedTypedArrayConstructors = Float32ArrayConstructor | Uint8ArrayConstructor | Int8ArrayConstructor | Uint16ArrayConstructor | Int16ArrayConstructor | Int32ArrayConstructor | BigInt64ArrayConstructor | Uint8ArrayConstructor | Float64ArrayConstructor | Uint32ArrayConstructor | BigUint64ArrayConstructor;
export type SupportedTypedArray = InstanceType<SupportedTypedArrayConstructors>;
export declare const NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP: Map<string, SupportedTypedArrayConstructors>;
export declare const NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP: Map<SupportedTypedArrayConstructors, keyof Tensor.DataTypeMap>;
export declare const checkTypedArray: () => void;
//# sourceMappingURL=tensor-impl-type-mapping.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"tensor-impl-type-mapping.d.ts","sourceRoot":"","sources":["../../lib/tensor-impl-type-mapping.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,MAAM,MAAM,+BAA+B,GACvC,uBAAuB,GACvB,qBAAqB,GACrB,oBAAoB,GACpB,sBAAsB,GACtB,qBAAqB,GACrB,qBAAqB,GACrB,wBAAwB,GACxB,qBAAqB,GACrB,uBAAuB,GACvB,sBAAsB,GACtB,yBAAyB,CAAC;AAC9B,MAAM,MAAM,mBAAmB,GAAG,YAAY,CAAC,+BAA+B,CAAC,CAAC;AAGhF,eAAO,MAAM,qCAAqC,8CAYhD,CAAC;AAGH,eAAO,MAAM,qCAAqC,gEAShD,CAAC;AAMH,eAAO,MAAM,eAAe,YA0B3B,CAAC"}
@@ -0,0 +1,58 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// a runtime map that maps type string to TypedArray constructor. Should match Tensor.DataTypeMap.
export const NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP = new Map([
['float32', Float32Array],
['uint8', Uint8Array],
['int8', Int8Array],
['uint16', Uint16Array],
['int16', Int16Array],
['int32', Int32Array],
['bool', Uint8Array],
['float64', Float64Array],
['uint32', Uint32Array],
['int4', Uint8Array],
['uint4', Uint8Array],
]);
// a runtime map that maps type string to TypedArray constructor. Should match Tensor.DataTypeMap.
export const NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP = new Map([
[Float32Array, 'float32'],
[Uint8Array, 'uint8'],
[Int8Array, 'int8'],
[Uint16Array, 'uint16'],
[Int16Array, 'int16'],
[Int32Array, 'int32'],
[Float64Array, 'float64'],
[Uint32Array, 'uint32'],
]);
// the following code allows delaying execution of BigInt/Float16Array checking. This allows lazy initialization for
// NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP and NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP, which allows BigInt/Float16Array
// polyfill if available.
let isTypedArrayChecked = false;
export const checkTypedArray = () => {
if (!isTypedArrayChecked) {
isTypedArrayChecked = true;
const isBigInt64ArrayAvailable = typeof BigInt64Array !== 'undefined' && BigInt64Array.from;
const isBigUint64ArrayAvailable = typeof BigUint64Array !== 'undefined' && BigUint64Array.from;
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-explicit-any
const Float16Array = globalThis.Float16Array;
const isFloat16ArrayAvailable = typeof Float16Array !== 'undefined' && Float16Array.from;
if (isBigInt64ArrayAvailable) {
NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set('int64', BigInt64Array);
NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.set(BigInt64Array, 'int64');
}
if (isBigUint64ArrayAvailable) {
NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set('uint64', BigUint64Array);
NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.set(BigUint64Array, 'uint64');
}
if (isFloat16ArrayAvailable) {
NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set('float16', Float16Array);
NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.set(Float16Array, 'float16');
}
else {
// if Float16Array is not available, use 'Uint16Array' to store the data.
NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set('float16', Uint16Array);
}
}
};
//# sourceMappingURL=tensor-impl-type-mapping.js.map
@@ -0,0 +1 @@
{"version":3,"file":"tensor-impl-type-mapping.js","sourceRoot":"","sources":["../../lib/tensor-impl-type-mapping.ts"],"names":[],"mappings":"AAAA,4DAA4D;AAC5D,kCAAkC;AAkBlC,kGAAkG;AAClG,MAAM,CAAC,MAAM,qCAAqC,GAAG,IAAI,GAAG,CAA0C;IACpG,CAAC,SAAS,EAAE,YAAY,CAAC;IACzB,CAAC,OAAO,EAAE,UAAU,CAAC;IACrB,CAAC,MAAM,EAAE,SAAS,CAAC;IACnB,CAAC,QAAQ,EAAE,WAAW,CAAC;IACvB,CAAC,OAAO,EAAE,UAAU,CAAC;IACrB,CAAC,OAAO,EAAE,UAAU,CAAC;IACrB,CAAC,MAAM,EAAE,UAAU,CAAC;IACpB,CAAC,SAAS,EAAE,YAAY,CAAC;IACzB,CAAC,QAAQ,EAAE,WAAW,CAAC;IACvB,CAAC,MAAM,EAAE,UAAU,CAAC;IACpB,CAAC,OAAO,EAAE,UAAU,CAAC;CACtB,CAAC,CAAC;AAEH,kGAAkG;AAClG,MAAM,CAAC,MAAM,qCAAqC,GAAG,IAAI,GAAG,CAA+C;IACzG,CAAC,YAAY,EAAE,SAAS,CAAC;IACzB,CAAC,UAAU,EAAE,OAAO,CAAC;IACrB,CAAC,SAAS,EAAE,MAAM,CAAC;IACnB,CAAC,WAAW,EAAE,QAAQ,CAAC;IACvB,CAAC,UAAU,EAAE,OAAO,CAAC;IACrB,CAAC,UAAU,EAAE,OAAO,CAAC;IACrB,CAAC,YAAY,EAAE,SAAS,CAAC;IACzB,CAAC,WAAW,EAAE,QAAQ,CAAC;CACxB,CAAC,CAAC;AAEH,oHAAoH;AACpH,oHAAoH;AACpH,yBAAyB;AACzB,IAAI,mBAAmB,GAAG,KAAK,CAAC;AAChC,MAAM,CAAC,MAAM,eAAe,GAAG,GAAG,EAAE;IAClC,IAAI,CAAC,mBAAmB,EAAE,CAAC;QACzB,mBAAmB,GAAG,IAAI,CAAC;QAC3B,MAAM,wBAAwB,GAAG,OAAO,aAAa,KAAK,WAAW,IAAI,aAAa,CAAC,IAAI,CAAC;QAC5F,MAAM,yBAAyB,GAAG,OAAO,cAAc,KAAK,WAAW,IAAI,cAAc,CAAC,IAAI,CAAC;QAE/F,oGAAoG;QACpG,MAAM,YAAY,GAAI,UAAkB,CAAC,YAAY,CAAC;QACtD,MAAM,uBAAuB,GAAG,OAAO,YAAY,KAAK,WAAW,IAAI,YAAY,CAAC,IAAI,CAAC;QAEzF,IAAI,wBAAwB,EAAE,CAAC;YAC7B,qCAAqC,CAAC,GAAG,CAAC,OAAO,EAAE,aAAa,CAAC,CAAC;YAClE,qCAAqC,CAAC,GAAG,CAAC,aAAa,EAAE,OAAO,CAAC,CAAC;QACpE,CAAC;QACD,IAAI,yBAAyB,EAAE,CAAC;YAC9B,qCAAqC,CAAC,GAAG,CAAC,QAAQ,EAAE,cAAc,CAAC,CAAC;YACpE,qCAAqC,CAAC,GAAG,CAAC,cAAc,EAAE,QAAQ,CAAC,CAAC;QACtE,CAAC;QACD,IAAI,uBAAuB,EAAE,CAAC;YAC5B,qCAAqC,CAAC,GAAG,CAAC,SAAS,EAAE,YAAY,CAAC,CAAC;YACnE,qCAAqC,CAAC,GAAG,CAAC,YAAY,EAAE,SAAS,CAAC,CAAC;QACrE,CAAC;aAAM,CAAC;YACN,yEAAyE;YACzE,qCAAqC,CAAC,GAAG,CAAC,SAAS,EAAE,WAAW,CAAC,CAAC;QACpE,CAAC;IACH,CAAC;AACH,CAAC,CAAC"}
+109
View File
@@ -0,0 +1,109 @@
import { TensorToDataUrlOptions, TensorToImageDataOptions } from './tensor-conversion.js';
import { CpuPinnedConstructorParameters, GpuBufferConstructorParameters, MLTensorConstructorParameters, TensorFromGpuBufferOptions, TensorFromImageBitmapOptions, TensorFromImageDataOptions, TensorFromImageElementOptions, TensorFromMLTensorOptions, TensorFromTextureOptions, TensorFromUrlOptions, TextureConstructorParameters } from './tensor-factory.js';
import { Tensor as TensorInterface } from './tensor.js';
type TensorType = TensorInterface.Type;
type TensorDataType = TensorInterface.DataType;
type TensorDataLocation = TensorInterface.DataLocation;
type TensorTextureType = TensorInterface.TextureType;
type TensorGpuBufferType = TensorInterface.GpuBufferType;
type TensorMLTensorType = TensorInterface.MLTensorType;
/**
* the implementation of Tensor interface.
*
* @ignore
*/
export declare class Tensor implements TensorInterface {
/**
* Construct a new CPU tensor object from the given type, data and dims.
*/
constructor(type: TensorType, data: TensorDataType | Uint8ClampedArray | readonly string[] | readonly number[] | readonly boolean[], dims?: readonly number[]);
/**
* Construct a new CPU tensor object from the given data and dims. Type is inferred from data.
*/
constructor(data: TensorDataType | Uint8ClampedArray | readonly string[] | readonly boolean[], dims?: readonly number[]);
/**
* Construct a new tensor object from the pinned CPU data with the given type and dims.
*
* Tensor's location will be set to 'cpu-pinned'.
*
* @param params - Specify the parameters to construct the tensor.
*/
constructor(params: CpuPinnedConstructorParameters);
/**
* Construct a new tensor object from the WebGL texture with the given type and dims.
*
* Tensor's location will be set to 'texture'.
*
* @param params - Specify the parameters to construct the tensor.
*/
constructor(params: TextureConstructorParameters);
/**
* Construct a new tensor object from the WebGPU buffer with the given type and dims.
*
* Tensor's location will be set to 'gpu-buffer'.
*
* @param params - Specify the parameters to construct the tensor.
*/
constructor(params: GpuBufferConstructorParameters);
/**
* Construct a new tensor object from the WebNN MLTensor with the given type and dims.
*
* Tensor's location will be set to 'ml-tensor'.
*
* @param params - Specify the parameters to construct the tensor.
*/
constructor(params: MLTensorConstructorParameters);
static fromImage(image: ImageData | HTMLImageElement | ImageBitmap | string, options?: TensorFromImageDataOptions | TensorFromImageElementOptions | TensorFromImageBitmapOptions | TensorFromUrlOptions): Promise<TensorInterface>;
static fromTexture<T extends TensorInterface.TextureDataTypes>(texture: TensorTextureType, options: TensorFromTextureOptions<T>): TensorInterface;
static fromGpuBuffer<T extends TensorInterface.GpuBufferDataTypes>(gpuBuffer: TensorGpuBufferType, options: TensorFromGpuBufferOptions<T>): TensorInterface;
static fromMLTensor<T extends TensorInterface.MLTensorDataTypes>(mlTensor: TensorMLTensorType, options: TensorFromMLTensorOptions<T>): TensorInterface;
static fromPinnedBuffer<T extends TensorInterface.CpuPinnedDataTypes>(type: T, buffer: TensorInterface.DataTypeMap[T], dims?: readonly number[]): Tensor;
toDataURL(options?: TensorToDataUrlOptions): string;
toImageData(options?: TensorToImageDataOptions): ImageData;
readonly dims: readonly number[];
readonly type: TensorType;
readonly size: number;
/**
* stores the location of the data.
*/
private dataLocation;
/**
* stores the data on CPU, if location is 'cpu' or 'cpu-pinned'. otherwise empty.
*/
private cpuData?;
/**
* stores the underlying texture when location is 'texture'. otherwise empty.
*/
private gpuTextureData?;
/**
* stores the underlying GPU buffer when location is 'gpu-buffer'. otherwise empty.
*/
private gpuBufferData?;
/**
* stores the underlying WebNN MLTensor when location is 'ml-tensor'. otherwise empty.
*/
private mlTensorData?;
/**
* stores an optional downloader function to download data from GPU to CPU.
*/
private downloader;
/**
* a flag indicating whether the data is being downloaded from GPU to CPU.
*/
private isDownloading?;
/**
* stores an optional disposer function to dispose the underlying data.
*/
private disposer;
get data(): TensorDataType;
get location(): TensorDataLocation;
get texture(): TensorTextureType;
get gpuBuffer(): TensorGpuBufferType;
get mlTensor(): TensorMLTensorType;
getData(releaseData?: boolean): Promise<TensorDataType>;
dispose(): void;
private ensureValid;
reshape(dims: readonly number[]): TensorInterface;
}
export {};
//# sourceMappingURL=tensor-impl.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"tensor-impl.d.ts","sourceRoot":"","sources":["../../lib/tensor-impl.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,sBAAsB,EAAE,wBAAwB,EAAE,MAAM,wBAAwB,CAAC;AAQ1F,OAAO,EACL,8BAA8B,EAC9B,8BAA8B,EAC9B,6BAA6B,EAC7B,0BAA0B,EAC1B,4BAA4B,EAC5B,0BAA0B,EAC1B,6BAA6B,EAC7B,yBAAyB,EACzB,wBAAwB,EACxB,oB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,CASzB;IAED,IAAI,QAAQ,IAAI,kBAAkB,CAEjC;IAED,IAAI,OAAO,IAAI,iBAAiB,CAM/B;IAED,IAAI,SAAS,IAAI,mBAAmB,CAMnC;IAED,IAAI,QAAQ,IAAI,kBAAkB,CAMjC;IAKK,OAAO,CAAC,WAAW,CAAC,EAAE,OAAO,GAAG,OAAO,CAAC,cAAc,CAAC;IAqC7D,OAAO,IAAI,IAAI;IAsBf,OAAO,CAAC,WAAW;IAMnB,OAAO,CAAC,IAAI,EAAE,SAAS,MAAM,EAAE,GAAG,eAAe;CAQlD"}
+366
View File
@@ -0,0 +1,366 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { tensorToDataURL, tensorToImageData } from './tensor-conversion-impl.js';
import { tensorFromGpuBuffer, tensorFromImage, tensorFromMLTensor, tensorFromPinnedBuffer, tensorFromTexture, } from './tensor-factory-impl.js';
import { checkTypedArray, NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP, NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP, } from './tensor-impl-type-mapping.js';
import { calculateSize, tensorReshape } from './tensor-utils-impl.js';
/**
* the implementation of Tensor interface.
*
* @ignore
*/
export class Tensor {
/**
* implementation.
*/
constructor(arg0, arg1, arg2) {
// perform one-time check for BigInt/Float16Array support
checkTypedArray();
let type;
let dims;
if (typeof arg0 === 'object' && 'location' in arg0) {
//
// constructing tensor from specific location
//
this.dataLocation = arg0.location;
type = arg0.type;
dims = arg0.dims;
switch (arg0.location) {
case 'cpu-pinned': {
const expectedTypedArrayConstructor = NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.get(type);
if (!expectedTypedArrayConstructor) {
throw new TypeError(`unsupported type "${type}" to create tensor from pinned buffer`);
}
if (!(arg0.data instanceof expectedTypedArrayConstructor)) {
throw new TypeError(`buffer should be of type ${expectedTypedArrayConstructor.name}`);
}
this.cpuData = arg0.data;
break;
}
case 'texture': {
if (type !== 'float32') {
throw new TypeError(`unsupported type "${type}" to create tensor from texture`);
}
this.gpuTextureData = arg0.texture;
this.downloader = arg0.download;
this.disposer = arg0.dispose;
break;
}
case 'gpu-buffer': {
if (type !== 'float32' &&
type !== 'float16' &&
type !== 'int32' &&
type !== 'int64' &&
type !== 'uint32' &&
type !== 'uint8' &&
type !== 'bool' &&
type !== 'uint4' &&
type !== 'int4') {
throw new TypeError(`unsupported type "${type}" to create tensor from gpu buffer`);
}
this.gpuBufferData = arg0.gpuBuffer;
this.downloader = arg0.download;
this.disposer = arg0.dispose;
break;
}
case 'ml-tensor': {
if (type !== 'float32' &&
type !== 'float16' &&
type !== 'int32' &&
type !== 'int64' &&
type !== 'uint32' &&
type !== 'uint64' &&
type !== 'int8' &&
type !== 'uint8' &&
type !== 'bool' &&
type !== 'uint4' &&
type !== 'int4') {
throw new TypeError(`unsupported type "${type}" to create tensor from MLTensor`);
}
this.mlTensorData = arg0.mlTensor;
this.downloader = arg0.download;
this.disposer = arg0.dispose;
break;
}
default:
throw new Error(`Tensor constructor: unsupported location '${this.dataLocation}'`);
}
}
else {
//
// constructing tensor of location 'cpu'
//
let data;
let maybeDims;
// check whether arg0 is type or data
if (typeof arg0 === 'string') {
//
// Override: constructor(type, data, ...)
//
type = arg0;
maybeDims = arg2;
if (arg0 === 'string') {
// string tensor
if (!Array.isArray(arg1)) {
throw new TypeError("A string tensor's data must be a string array.");
}
// we don't check whether every element in the array is string; this is too slow. we assume it's correct and
// error will be populated at inference
data = arg1;
}
else {
// numeric tensor
const typedArrayConstructor = NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.get(arg0);
if (typedArrayConstructor === undefined) {
throw new TypeError(`Unsupported tensor type: ${arg0}.`);
}
if (Array.isArray(arg1)) {
if ((arg0 === 'float16' && typedArrayConstructor === Uint16Array) || arg0 === 'uint4' || arg0 === 'int4') {
// - 'float16':
// When no Float16Array polyfill is used, we cannot create 'float16' tensor from number array.
//
// Throw error here because when user try to use number array as data,
// e.g. new Tensor('float16', [1, 2, 3, 4], dims)), it will actually call
// Uint16Array.from(arg1) which generates wrong data.
//
// - 'uint4' and 'int4':
// Uint8Array.from(arg1) will generate wrong data for 'uint4' and 'int4' tensor.
//
throw new TypeError(`Creating a ${arg0} tensor from number array is not supported. Please use ${typedArrayConstructor.name} as data.`);
}
else if (arg0 === 'uint64' || arg0 === 'int64') {
// use 'as any' here because:
// 1. TypeScript's check on type of 'Array.isArray()' does not work with readonly arrays.
// see https://github.com/microsoft/TypeScript/issues/17002
// 2. TypeScript's check on union type of '(BigInt64ArrayConstructor|BigUint64ArrayConstructor).from()'
// does not accept parameter mapFn.
// 3. parameters of 'SupportedTypedArrayConstructors.from()' does not match the requirement of the union
// type.
// assume 'arg1' is of type "readonly number[]|readonly bigint[]" here.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
data = typedArrayConstructor.from(arg1, BigInt);
}
else {
// assume 'arg1' is of type "readonly number[]" here.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
data = typedArrayConstructor.from(arg1);
}
}
else if (arg1 instanceof typedArrayConstructor) {
data = arg1;
}
else if (arg1 instanceof Uint8ClampedArray) {
if (arg0 === 'uint8') {
data = Uint8Array.from(arg1);
}
else {
throw new TypeError(`A Uint8ClampedArray tensor's data must be type of uint8`);
}
}
else if (arg0 === 'float16' && arg1 instanceof Uint16Array && typedArrayConstructor !== Uint16Array) {
// when Float16Array is available and data is of type Uint16Array.
// We allow Uint16Array to be passed in as data for 'float16' tensor until Float16Array is generally
// supported in JavaScript environment.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
data = new globalThis.Float16Array(arg1.buffer, arg1.byteOffset, arg1.length);
}
else {
throw new TypeError(`A ${type} tensor's data must be type of ${typedArrayConstructor}`);
}
}
}
else {
//
// Override: constructor(data, ...)
//
maybeDims = arg1;
if (Array.isArray(arg0)) {
// only boolean[] and string[] is supported
if (arg0.length === 0) {
throw new TypeError('Tensor type cannot be inferred from an empty array.');
}
const firstElementType = typeof arg0[0];
if (firstElementType === 'string') {
type = 'string';
data = arg0;
}
else if (firstElementType === 'boolean') {
type = 'bool';
// 'arg0' is of type 'boolean[]'. Uint8Array.from(boolean[]) actually works, but typescript thinks this is
// wrong type. We use 'as any' to make it happy.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
data = Uint8Array.from(arg0);
}
else {
throw new TypeError(`Invalid element type of data array: ${firstElementType}.`);
}
}
else if (arg0 instanceof Uint8ClampedArray) {
type = 'uint8';
data = Uint8Array.from(arg0);
}
else {
// get tensor type from TypedArray
const mappedType = NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.get(arg0.constructor);
if (mappedType === undefined) {
throw new TypeError(`Unsupported type for tensor data: ${arg0.constructor}.`);
}
type = mappedType;
data = arg0;
}
}
// type and data is processed, now processing dims
if (maybeDims === undefined) {
// assume 1-D tensor if dims omitted
maybeDims = [data.length];
}
else if (!Array.isArray(maybeDims)) {
throw new TypeError("A tensor's dims must be a number array");
}
dims = maybeDims;
this.cpuData = data;
this.dataLocation = 'cpu';
}
// perform check on dims
const size = calculateSize(dims);
// if data is on CPU, check whether data length matches tensor size
if (this.cpuData && size !== this.cpuData.length) {
if ((type === 'uint4' || type === 'int4') && Math.ceil(size / 2) === this.cpuData.length) {
// for (u)int4, the data length is half of the tensor size. So we check this special case when size is odd.
}
else {
throw new Error(`Tensor's size(${size}) does not match data length(${this.cpuData.length}).`);
}
}
this.type = type;
this.dims = dims;
this.size = size;
}
// #endregion
// #region factory
static async fromImage(image, options) {
return tensorFromImage(image, options);
}
static fromTexture(texture, options) {
return tensorFromTexture(texture, options);
}
static fromGpuBuffer(gpuBuffer, options) {
return tensorFromGpuBuffer(gpuBuffer, options);
}
static fromMLTensor(mlTensor, options) {
return tensorFromMLTensor(mlTensor, options);
}
static fromPinnedBuffer(type, buffer, dims) {
return tensorFromPinnedBuffer(type, buffer, dims);
}
// #endregion
// #region conversions
toDataURL(options) {
return tensorToDataURL(this, options);
}
toImageData(options) {
return tensorToImageData(this, options);
}
// #endregion
// #region properties
get data() {
this.ensureValid();
if (!this.cpuData) {
throw new Error('The data is not on CPU. Use `getData()` to download GPU data to CPU, ' +
'or use `texture` or `gpuBuffer` property to access the GPU data directly.');
}
return this.cpuData;
}
get location() {
return this.dataLocation;
}
get texture() {
this.ensureValid();
if (!this.gpuTextureData) {
throw new Error('The data is not stored as a WebGL texture.');
}
return this.gpuTextureData;
}
get gpuBuffer() {
this.ensureValid();
if (!this.gpuBufferData) {
throw new Error('The data is not stored as a WebGPU buffer.');
}
return this.gpuBufferData;
}
get mlTensor() {
this.ensureValid();
if (!this.mlTensorData) {
throw new Error('The data is not stored as a WebNN MLTensor.');
}
return this.mlTensorData;
}
// #endregion
// #region methods
async getData(releaseData) {
this.ensureValid();
switch (this.dataLocation) {
case 'cpu':
case 'cpu-pinned':
return this.data;
case 'texture':
case 'gpu-buffer':
case 'ml-tensor': {
if (!this.downloader) {
throw new Error('The current tensor is not created with a specified data downloader.');
}
if (this.isDownloading) {
throw new Error('The current tensor is being downloaded.');
}
try {
this.isDownloading = true;
const data = await this.downloader();
this.downloader = undefined;
this.dataLocation = 'cpu';
this.cpuData = data;
if (releaseData && this.disposer) {
this.disposer();
this.disposer = undefined;
}
return data;
}
finally {
this.isDownloading = false;
}
}
default:
throw new Error(`cannot get data from location: ${this.dataLocation}`);
}
}
dispose() {
if (this.isDownloading) {
throw new Error('The current tensor is being downloaded.');
}
if (this.disposer) {
this.disposer();
this.disposer = undefined;
}
this.cpuData = undefined;
this.gpuTextureData = undefined;
this.gpuBufferData = undefined;
this.mlTensorData = undefined;
this.downloader = undefined;
this.isDownloading = undefined;
this.dataLocation = 'none';
}
// #endregion
// #region tensor utilities
ensureValid() {
if (this.dataLocation === 'none') {
throw new Error('The tensor is disposed.');
}
}
reshape(dims) {
this.ensureValid();
if (this.downloader || this.disposer) {
throw new Error('Cannot reshape a tensor that owns GPU resource.');
}
return tensorReshape(this, dims);
}
}
//# sourceMappingURL=tensor-impl.js.map
File diff suppressed because one or more lines are too long
@@ -0,0 +1,12 @@
import { Tensor } from './tensor-impl.js';
/**
* calculate size from dims.
*
* @param dims the dims array. May be an illegal input.
*/
export declare const calculateSize: (dims: readonly unknown[]) => number;
/**
* implementation of Tensor.reshape()
*/
export declare const tensorReshape: (tensor: Tensor, dims: readonly number[]) => Tensor;
//# sourceMappingURL=tensor-utils-impl.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"tensor-utils-impl.d.ts","sourceRoot":"","sources":["../../lib/tensor-utils-impl.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAE1C;;;;GAIG;AACH,eAAO,MAAM,aAAa,GAAI,MAAM,SAAS,OAAO,EAAE,KAAG,MAaxD,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,aAAa,GAAI,QAAQ,MAAM,EAAE,MAAM,SAAS,MAAM,EAAE,KAAG,MAmCvE,CAAC"}
@@ -0,0 +1,62 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { Tensor } from './tensor-impl.js';
/**
* calculate size from dims.
*
* @param dims the dims array. May be an illegal input.
*/
export const calculateSize = (dims) => {
let size = 1;
for (let i = 0; i < dims.length; i++) {
const dim = dims[i];
if (typeof dim !== 'number' || !Number.isSafeInteger(dim)) {
throw new TypeError(`dims[${i}] must be an integer, got: ${dim}`);
}
if (dim < 0) {
throw new RangeError(`dims[${i}] must be a non-negative integer, got: ${dim}`);
}
size *= dim;
}
return size;
};
/**
* implementation of Tensor.reshape()
*/
export const tensorReshape = (tensor, dims) => {
switch (tensor.location) {
case 'cpu':
return new Tensor(tensor.type, tensor.data, dims);
case 'cpu-pinned':
return new Tensor({
location: 'cpu-pinned',
data: tensor.data,
type: tensor.type,
dims,
});
case 'texture':
return new Tensor({
location: 'texture',
texture: tensor.texture,
type: tensor.type,
dims,
});
case 'gpu-buffer':
return new Tensor({
location: 'gpu-buffer',
gpuBuffer: tensor.gpuBuffer,
type: tensor.type,
dims,
});
case 'ml-tensor':
return new Tensor({
location: 'ml-tensor',
mlTensor: tensor.mlTensor,
type: tensor.type,
dims,
});
default:
throw new Error(`tensorReshape: tensor location ${tensor.location} is not supported`);
}
};
//# sourceMappingURL=tensor-utils-impl.js.map
@@ -0,0 +1 @@
{"version":3,"file":"tensor-utils-impl.js","sourceRoot":"","sources":["../../lib/tensor-utils-impl.ts"],"names":[],"mappings":"AAAA,4DAA4D;AAC5D,kCAAkC;AAQlC,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAE1C;;;;GAIG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,IAAwB,EAAU,EAAE;IAChE,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACrC,MAAM,GAAG,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;QACpB,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,GAAG,CAAC,EAAE,CAAC;YAC1D,MAAM,IAAI,SAAS,CAAC,QAAQ,CAAC,8BAA8B,GAAG,EAAE,CAAC,CAAC;QACpE,CAAC;QACD,IAAI,GAAG,GAAG,CAAC,EAAE,CAAC;YACZ,MAAM,IAAI,UAAU,CAAC,QAAQ,CAAC,0CAA0C,GAAG,EAAE,CAAC,CAAC;QACjF,CAAC;QACD,IAAI,IAAI,GAAG,CAAC;IACd,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,MAAc,EAAE,IAAuB,EAAU,EAAE;IAC/E,QAAQ,MAAM,CAAC,QAAQ,EAAE,CAAC;QACxB,KAAK,KAAK;YACR,OAAO,IAAI,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACpD,KAAK,YAAY;YACf,OAAO,IAAI,MAAM,CAAC;gBAChB,QAAQ,EAAE,YAAY;gBACtB,IAAI,EAAE,MAAM,CAAC,IAA8C;gBAC3D,IAAI,EAAE,MAAM,CAAC,IAA8C;gBAC3D,IAAI;aACL,CAAC,CAAC;QACL,KAAK,SAAS;YACZ,OAAO,IAAI,MAAM,CAAC;gBAChB,QAAQ,EAAE,SAAS;gBACnB,OAAO,EAAE,MAAM,CAAC,OAAO;gBACvB,IAAI,EAAE,MAAM,CAAC,IAA4C;gBACzD,IAAI;aACL,CAAC,CAAC;QACL,KAAK,YAAY;YACf,OAAO,IAAI,MAAM,CAAC;gBAChB,QAAQ,EAAE,YAAY;gBACtB,SAAS,EAAE,MAAM,CAAC,SAAS;gBAC3B,IAAI,EAAE,MAAM,CAAC,IAA8C;gBAC3D,IAAI;aACL,CAAC,CAAC;QACL,KAAK,WAAW;YACd,OAAO,IAAI,MAAM,CAAC;gBAChB,QAAQ,EAAE,WAAW;gBACrB,QAAQ,EAAE,MAAM,CAAC,QAAQ;gBACzB,IAAI,EAAE,MAAM,CAAC,IAA6C;gBAC1D,IAAI;aACL,CAAC,CAAC;QACL;YACE,MAAM,IAAI,KAAK,CAAC,kCAAkC,MAAM,CAAC,QAAQ,mBAAmB,CAAC,CAAC;IAC1F,CAAC;AACH,CAAC,CAAC"}
@@ -0,0 +1,28 @@
import { ConversionUtils } from './tensor-conversion.js';
import { Tensor, TypedTensor } from './tensor.js';
interface Properties {
/**
* Get the number of elements in the tensor.
*/
readonly size: number;
}
export interface TypedShapeUtils<T extends Tensor.Type> {
/**
* Create a new tensor with the same data buffer and specified dims.
*
* @param dims - New dimensions. Size should match the old one.
*/
reshape(dims: readonly number[]): TypedTensor<T>;
}
/**
* interface `TensorUtils` includes all utility members that does not use the type parameter from their signature.
*/
export interface TensorUtils extends Properties, ConversionUtils {
}
/**
* interface `TypedShapeUtils` includes all utility members that uses the type parameter from their signature.
*/
export interface TypedTensorUtils<T extends Tensor.Type> extends TensorUtils, TypedShapeUtils<T> {
}
export {};
//# sourceMappingURL=tensor-utils.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"tensor-utils.d.ts","sourceRoot":"","sources":["../../lib/tensor-utils.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAElD,UAAU,UAAU;IAClB;;OAEG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,eAAe,CAAC,CAAC,SAAS,MAAM,CAAC,IAAI;IACpD;;;;OAIG;IACH,OAAO,CAAC,IAAI,EAAE,SAAS,MAAM,EAAE,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC;CAClD;AAED;;GAEG;AACH,MAAM,WAAW,WAAY,SAAQ,UAAU,EAAE,eAAe;CAAG;AAEnE;;GAEG;AACH,MAAM,WAAW,gBAAgB,CAAC,CAAC,SAAS,MAAM,CAAC,IAAI,CAAE,SAAQ,WAAW,EAAE,eAAe,CAAC,CAAC,CAAC;CAAG"}
@@ -0,0 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
export {};
//# sourceMappingURL=tensor-utils.js.map
@@ -0,0 +1 @@
{"version":3,"file":"tensor-utils.js","sourceRoot":"","sources":["../../lib/tensor-utils.ts"],"names":[],"mappings":"AAAA,4DAA4D;AAC5D,kCAAkC"}
+309
View File
@@ -0,0 +1,309 @@
import { TensorFactory } from './tensor-factory.js';
import { TypedTensorUtils } from './tensor-utils.js';
import { TryGetGlobalType } from './type-helper.js';
/**
* represent a basic tensor with specified dimensions and data type.
*/
interface TypedTensorBase<T extends Tensor.Type> {
/**
* Get the dimensions of the tensor.
*/
readonly dims: readonly number[];
/**
* Get the data type of the tensor.
*/
readonly type: T;
/**
* Get the buffer data of the tensor.
*
* If the data is not on CPU (eg. it's in the form of WebGL texture or WebGPU buffer), throw error.
*/
readonly data: Tensor.DataTypeMap[T];
/**
* Get the location of the data.
*/
readonly location: Tensor.DataLocation;
/**
* Get the WebGL texture that holds the tensor data.
*
* If the data is not on GPU as WebGL texture, throw error.
*/
readonly texture: Tensor.TextureType;
/**
* Get the WebGPU buffer that holds the tensor data.
*
* If the data is not on GPU as WebGPU buffer, throw error.
*/
readonly gpuBuffer: Tensor.GpuBufferType;
/**
* Get the WebNN MLTensor that holds the tensor data.
*
* If the data is not in a WebNN MLTensor, throw error.
*/
readonly mlTensor: Tensor.MLTensorType;
/**
* Get the buffer data of the tensor.
*
* If the data is on CPU, returns the data immediately.
* If the data is on GPU, downloads the data and returns the promise.
*
* @param releaseData - whether release the data on GPU. Ignore if data is already on CPU.
*/
getData(releaseData?: boolean): Promise<Tensor.DataTypeMap[T]>;
/**
* Dispose the tensor data.
*
* If the data is on CPU, remove its internal reference to the underlying data.
* If the data is on GPU, release the data on GPU.
*
* After calling this function, the tensor is considered no longer valid. Its location will be set to 'none'.
*/
dispose(): void;
}
export declare namespace Tensor {
interface DataTypeMap {
float32: Float32Array;
uint8: Uint8Array;
int8: Int8Array;
uint16: Uint16Array;
int16: Int16Array;
int32: Int32Array;
int64: BigInt64Array;
string: string[];
bool: Uint8Array;
float16: Uint16Array;
float64: Float64Array;
uint32: Uint32Array;
uint64: BigUint64Array;
uint4: Uint8Array;
int4: Int8Array;
}
interface ElementTypeMap {
float32: number;
uint8: number;
int8: number;
uint16: number;
int16: number;
int32: number;
int64: bigint;
string: string;
bool: boolean;
float16: number;
float64: number;
uint32: number;
uint64: bigint;
uint4: number;
int4: number;
}
type DataType = DataTypeMap[Type];
type ElementType = ElementTypeMap[Type];
/**
* supported data types for constructing a tensor from a pinned CPU buffer
*/
type CpuPinnedDataTypes = Exclude<Tensor.Type, 'string'>;
/**
* type alias for WebGL texture
*/
type TextureType = WebGLTexture;
/**
* supported data types for constructing a tensor from a WebGL texture
*/
type TextureDataTypes = 'float32';
type GpuBufferTypeFallback = {
size: number;
mapState: 'unmapped' | 'pending' | 'mapped';
};
/**
* type alias for WebGPU buffer
*/
type GpuBufferType = TryGetGlobalType<'GPUBuffer', GpuBufferTypeFallback>;
type MLTensorTypeFallback = {
destroy(): void;
};
/**
* type alias for WebNN MLTensor
*
* The specification for WebNN's MLTensor is currently in flux.
*/
type MLTensorType = TryGetGlobalType<'MLTensor', MLTensorTypeFallback>;
/**
* supported data types for constructing a tensor from a WebGPU buffer
*/
type GpuBufferDataTypes = 'float32' | 'float16' | 'int32' | 'int64' | 'uint32' | 'uint8' | 'bool';
/**
* supported data types for constructing a tensor from a WebNN MLTensor
*/
type MLTensorDataTypes = 'float32' | 'float16' | 'int8' | 'uint8' | 'int32' | 'uint32' | 'int64' | 'uint64' | 'bool' | 'uint4' | 'int4';
/**
* represent where the tensor data is stored
*/
type DataLocation = 'none' | 'cpu' | 'cpu-pinned' | 'texture' | 'gpu-buffer' | 'ml-tensor';
/**
* represent the data type of a tensor
*/
type Type = keyof DataTypeMap;
}
/**
* Represent multi-dimensional arrays to feed to or fetch from model inferencing.
*/
export interface TypedTensor<T extends Tensor.Type> extends TypedTensorBase<T>, TypedTensorUtils<T> {
}
/**
* Represent multi-dimensional arrays to feed to or fetch from model inferencing.
*/
export interface Tensor extends TypedTensorBase<Tensor.Type>, TypedTensorUtils<Tensor.Type> {
}
/**
* type TensorConstructor defines the constructors of 'Tensor' to create CPU tensor instances.
*/
export interface TensorConstructor extends TensorFactory {
/**
* Construct a new string tensor object from the given type, data and dims.
*
* @param type - Specify the element type.
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (type: 'string', data: Tensor.DataTypeMap['string'] | readonly string[], dims?: readonly number[]): TypedTensor<'string'>;
/**
* Construct a new bool tensor object from the given type, data and dims.
*
* @param type - Specify the element type.
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (type: 'bool', data: Tensor.DataTypeMap['bool'] | readonly boolean[], dims?: readonly number[]): TypedTensor<'bool'>;
/**
* Construct a new uint8 tensor object from a Uint8ClampedArray, data and dims.
*
* @param type - Specify the element type.
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (type: 'uint8', data: Uint8ClampedArray, dims?: readonly number[]): TypedTensor<'uint8'>;
/**
* Construct a new 64-bit integer typed tensor object from the given type, data and dims.
*
* @param type - Specify the element type.
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new <T extends 'uint64' | 'int64'>(type: T, data: Tensor.DataTypeMap[T] | readonly bigint[] | readonly number[], dims?: readonly number[]): TypedTensor<T>;
/**
* Construct a new numeric tensor object from the given type, data and dims.
*
* @param type - Specify the element type.
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new <T extends Exclude<Tensor.Type, 'string' | 'bool' | 'uint64' | 'int64'>>(type: T, data: Tensor.DataTypeMap[T] | readonly number[], dims?: readonly number[]): TypedTensor<T>;
/**
* Construct a new float32 tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: Float32Array, dims?: readonly number[]): TypedTensor<'float32'>;
/**
* Construct a new int8 tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: Int8Array, dims?: readonly number[]): TypedTensor<'int8'>;
/**
* Construct a new uint8 tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: Uint8Array, dims?: readonly number[]): TypedTensor<'uint8'>;
/**
* Construct a new uint8 tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: Uint8ClampedArray, dims?: readonly number[]): TypedTensor<'uint8'>;
/**
* Construct a new uint16 tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: Uint16Array, dims?: readonly number[]): TypedTensor<'uint16'>;
/**
* Construct a new int16 tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: Int16Array, dims?: readonly number[]): TypedTensor<'int16'>;
/**
* Construct a new int32 tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: Int32Array, dims?: readonly number[]): TypedTensor<'int32'>;
/**
* Construct a new int64 tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: BigInt64Array, dims?: readonly number[]): TypedTensor<'int64'>;
/**
* Construct a new string tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: readonly string[], dims?: readonly number[]): TypedTensor<'string'>;
/**
* Construct a new bool tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: readonly boolean[], dims?: readonly number[]): TypedTensor<'bool'>;
/**
* Construct a new float64 tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: Float64Array, dims?: readonly number[]): TypedTensor<'float64'>;
/**
* Construct a new uint32 tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: Uint32Array, dims?: readonly number[]): TypedTensor<'uint32'>;
/**
* Construct a new uint64 tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: BigUint64Array, dims?: readonly number[]): TypedTensor<'uint64'>;
/**
* Construct a new tensor object from the given type, data and dims.
*
* @param type - Specify the element type.
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (type: Tensor.Type, data: Tensor.DataType | readonly number[] | readonly string[] | readonly bigint[] | readonly boolean[], dims?: readonly number[]): Tensor;
/**
* Construct a new tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: Tensor.DataType, dims?: readonly number[]): Tensor;
}
export declare const Tensor: TensorConstructor;
export {};
//# sourceMappingURL=tensor.d.ts.map
File diff suppressed because one or more lines are too long
+6
View File
@@ -0,0 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { Tensor as TensorImpl } from './tensor-impl.js';
// eslint-disable-next-line @typescript-eslint/naming-convention
export const Tensor = TensorImpl;
//# sourceMappingURL=tensor.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"tensor.js","sourceRoot":"","sources":["../../lib/tensor.ts"],"names":[],"mappings":"AAAA,4DAA4D;AAC5D,kCAAkC;AAGlC,OAAO,EAAE,MAAM,IAAI,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAiYxD,gEAAgE;AAChE,MAAM,CAAC,MAAM,MAAM,GAAG,UAA+B,CAAC"}
+21
View File
@@ -0,0 +1,21 @@
/**
* @ignore
*/
export declare const TRACE: (deviceType: string, label: string) => void;
/**
* @ignore
*/
export declare const TRACE_FUNC_BEGIN: (extraMsg?: string) => void;
/**
* @ignore
*/
export declare const TRACE_FUNC_END: (extraMsg?: string) => void;
/**
* @ignore
*/
export declare const TRACE_EVENT_BEGIN: (extraMsg?: string) => void;
/**
* @ignore
*/
export declare const TRACE_EVENT_END: (extraMsg?: string) => void;
//# sourceMappingURL=trace.d.ts.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"trace.d.ts","sourceRoot":"","sources":["../../lib/trace.ts"],"names":[],"mappings":"AAKA;;GAEG;AACH,eAAO,MAAM,KAAK,GAAI,YAAY,MAAM,EAAE,OAAO,MAAM,SAMtD,CAAC;AAoBF;;GAEG;AACH,eAAO,MAAM,gBAAgB,GAAI,WAAW,MAAM,SAKjD,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,cAAc,GAAI,WAAW,MAAM,SAK/C,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,iBAAiB,GAAI,WAAW,MAAM,SAMlD,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,eAAe,GAAI,WAAW,MAAM,SAMhD,CAAC"}
+69
View File
@@ -0,0 +1,69 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { env } from './env-impl.js';
/**
* @ignore
*/
export const TRACE = (deviceType, label) => {
if (typeof env.trace === 'undefined' ? !env.wasm.trace : !env.trace) {
return;
}
// eslint-disable-next-line no-console
console.timeStamp(`${deviceType}::ORT::${label}`);
};
const TRACE_FUNC = (msg, extraMsg) => {
const stack = new Error().stack?.split(/\r\n|\r|\n/g) || [];
let hasTraceFunc = false;
for (let i = 0; i < stack.length; i++) {
if (hasTraceFunc && !stack[i].includes('TRACE_FUNC')) {
let label = `FUNC_${msg}::${stack[i].trim().split(' ')[1]}`;
if (extraMsg) {
label += `::${extraMsg}`;
}
TRACE('CPU', label);
return;
}
if (stack[i].includes('TRACE_FUNC')) {
hasTraceFunc = true;
}
}
};
/**
* @ignore
*/
export const TRACE_FUNC_BEGIN = (extraMsg) => {
if (typeof env.trace === 'undefined' ? !env.wasm.trace : !env.trace) {
return;
}
TRACE_FUNC('BEGIN', extraMsg);
};
/**
* @ignore
*/
export const TRACE_FUNC_END = (extraMsg) => {
if (typeof env.trace === 'undefined' ? !env.wasm.trace : !env.trace) {
return;
}
TRACE_FUNC('END', extraMsg);
};
/**
* @ignore
*/
export const TRACE_EVENT_BEGIN = (extraMsg) => {
if (typeof env.trace === 'undefined' ? !env.wasm.trace : !env.trace) {
return;
}
// eslint-disable-next-line no-console
console.time(`ORT::${extraMsg}`);
};
/**
* @ignore
*/
export const TRACE_EVENT_END = (extraMsg) => {
if (typeof env.trace === 'undefined' ? !env.wasm.trace : !env.trace) {
return;
}
// eslint-disable-next-line no-console
console.timeEnd(`ORT::${extraMsg}`);
};
//# sourceMappingURL=trace.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"trace.js","sourceRoot":"","sources":["../../lib/trace.ts"],"names":[],"mappings":"AAAA,4DAA4D;AAC5D,kCAAkC;AAElC,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAEpC;;GAEG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG,CAAC,UAAkB,EAAE,KAAa,EAAE,EAAE;IACzD,IAAI,OAAO,GAAG,CAAC,KAAK,KAAK,WAAW,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC;QACpE,OAAO;IACT,CAAC;IACD,sCAAsC;IACtC,OAAO,CAAC,SAAS,CAAC,GAAG,UAAU,UAAU,KAAK,EAAE,CAAC,CAAC;AACpD,CAAC,CAAC;AAEF,MAAM,UAAU,GAAG,CAAC,GAAW,EAAE,QAAiB,EAAE,EAAE;IACpD,MAAM,KAAK,GAAG,IAAI,KAAK,EAAE,CAAC,KAAK,EAAE,KAAK,CAAC,aAAa,CAAC,IAAI,EAAE,CAAC;IAC5D,IAAI,YAAY,GAAG,KAAK,CAAC;IACzB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACtC,IAAI,YAAY,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AiB,EAAE,EAAE;IACrD,IAAI,OAAO,GAAG,CAAC,KAAK,KAAK,WAAW,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC;QACpE,OAAO;IACT,CAAC;IACD,sCAAsC;IACtC,OAAO,CAAC,IAAI,CAAC,QAAQ,QAAQ,EAAE,CAAC,CAAC;AACnC,CAAC,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,QAAiB,EAAE,EAAE;IACnD,IAAI,OAAO,GAAG,CAAC,KAAK,KAAK,WAAW,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC;QACpE,OAAO;IACT,CAAC;IACD,sCAAsC;IACtC,OAAO,CAAC,OAAO,CAAC,QAAQ,QAAQ,EAAE,CAAC,CAAC;AACtC,CAAC,CAAC"}
+29
View File
@@ -0,0 +1,29 @@
/**
* A helper type to get certain types if they are declared in global scope.
*
* For example, if you installed "@webgpu/types" as a dev dependency, then `TryGetTypeIfDeclared<'GPUDevice'>` will
* be type `GPUDevice`, otherwise it will be type `unknown`.
*
*
* We don't want to introduce "@webgpu/types" as a dependency of this package because:
*
* (1) For JavaScript users, it's not needed. For TypeScript users, they can install it as dev dependency themselves.
*
* (2) because "@webgpu/types" requires "@types/dom-webcodecs" as peer dependency when using TypeScript < v5.1 and its
* version need to be chosen carefully according to the TypeScript version being used. This means so far there is not a
* way to keep every TypeScript version happy. It turns out that we will easily broke users on some TypeScript version.
*
* for more info see https://github.com/gpuweb/types/issues/127
*
* Update (2024-08-07): The reason (2) may be no longer valid. Most people should be using TypeScript >= 5.1 by now.
* However, we are still not sure whether introducing "@webgpu/types" as direct dependency is a good idea. We find this
* type helper is useful for TypeScript users.
*
* @ignore
*/
export type TryGetGlobalType<Name extends string, Fallback = unknown> = typeof globalThis extends {
[k in Name]: {
prototype: infer T;
};
} ? T : Fallback;
//# sourceMappingURL=type-helper.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"type-helper.d.ts","sourceRoot":"","sources":["../../lib/type-helper.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,MAAM,gBAAgB,CAAC,IAAI,SAAS,MAAM,EAAE,QAAQ,GAAG,OAAO,IAAI,OAAO,UAAU,SAAS;KAC/F,CAAC,IAAI,IAAI,GAAG;QAAE,SAAS,EAAE,MAAM,CAAC,CAAA;KAAE;CACpC,GACG,CAAC,GACD,QAAQ,CAAC"}
+4
View File
@@ -0,0 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
export {};
//# sourceMappingURL=type-helper.js.map
@@ -0,0 +1 @@
{"version":3,"file":"type-helper.js","sourceRoot":"","sources":["../../lib/type-helper.ts"],"names":[],"mappings":"AAAA,4DAA4D;AAC5D,kCAAkC"}
+2
View File
@@ -0,0 +1,2 @@
export declare const version = "1.27.0";
//# sourceMappingURL=version.d.ts.map
@@ -0,0 +1 @@
{"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../../lib/version.ts"],"names":[],"mappings":"AAMA,eAAO,MAAM,OAAO,WAAW,CAAC"}
+6
View File
@@ -0,0 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// This file is generated by /js/scripts/update-version.ts
// Do not modify file content manually.
export const version = '1.27.0';
//# sourceMappingURL=version.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"version.js","sourceRoot":"","sources":["../../lib/version.ts"],"names":[],"mappings":"AAAA,4DAA4D;AAC5D,kCAAkC;AAElC,0DAA0D;AAC1D,uCAAuC;AAEvC,MAAM,CAAC,MAAM,OAAO,GAAG,QAAQ,CAAC"}
+164
View File
@@ -0,0 +1,164 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { Backend } from './backend.js';
import { InferenceSession } from './inference-session.js';
interface BackendInfo {
backend: Backend;
priority: number;
initPromise?: Promise<void>;
initialized?: boolean;
aborted?: boolean;
error?: string;
}
const backends: Map<string, BackendInfo> = new Map();
const backendsSortedByPriority: string[] = [];
/**
* Register a backend.
*
* @param name - the name as a key to lookup as an execution provider.
* @param backend - the backend object.
* @param priority - an integer indicating the priority of the backend. Higher number means higher priority. if priority
* < 0, it will be considered as a 'beta' version and will not be used as a fallback backend by default.
*
* @ignore
*/
export const registerBackend = (name: string, backend: Backend, priority: number): void => {
if (backend && typeof backend.init === 'function' && typeof backend.createInferenceSessionHandler === 'function') {
const currentBackend = backends.get(name);
if (currentBackend === undefined) {
backends.set(name, { backend, priority });
} else if (currentBackend.priority > priority) {
// same name is already registered with a higher priority. skip registeration.
return;
} else if (currentBackend.priority === priority) {
if (currentBackend.backend !== backend) {
throw new Error(`cannot register backend "${name}" using priority ${priority}`);
}
}
if (priority >= 0) {
const i = backendsSortedByPriority.indexOf(name);
if (i !== -1) {
backendsSortedByPriority.splice(i, 1);
}
for (let i = 0; i < backendsSortedByPriority.length; i++) {
if (backends.get(backendsSortedByPriority[i])!.priority <= priority) {
backendsSortedByPriority.splice(i, 0, name);
return;
}
}
backendsSortedByPriority.push(name);
}
return;
}
throw new TypeError('not a valid backend');
};
/**
* Try to resolve and initialize a backend.
*
* @param backendName - the name of the backend.
* @returns the backend instance if resolved and initialized successfully, or an error message if failed.
*/
const tryResolveAndInitializeBackend = async (backendName: string): Promise<Backend | string> => {
const backendInfo = backends.get(backendName);
if (!backendInfo) {
return 'backend not found.';
}
if (backendInfo.initialized) {
return backendInfo.backend;
} else if (backendInfo.aborted) {
return backendInfo.error!;
} else {
const isInitializing = !!backendInfo.initPromise;
try {
if (!isInitializing) {
backendInfo.initPromise = backendInfo.backend.init(backendName);
}
await backendInfo.initPromise;
backendInfo.initialized = true;
return backendInfo.backend;
} catch (e) {
if (!isInitializing) {
backendInfo.error = `${e}`;
backendInfo.aborted = true;
}
return backendInfo.error!;
} finally {
delete backendInfo.initPromise;
}
}
};
/**
* Resolve execution providers from the specific session options.
*
* @param options - the session options object.
* @returns a promise that resolves to a tuple of an initialized backend instance and a session options object with
* filtered EP list.
*
* @ignore
*/
export const resolveBackendAndExecutionProviders = async (
options: InferenceSession.SessionOptions,
): Promise<[backend: Backend, options: InferenceSession.SessionOptions]> => {
// extract backend hints from session options
const eps = options.executionProviders || [];
const backendHints = eps.map((i) => (typeof i === 'string' ? i : i.name));
const backendNames = backendHints.length === 0 ? backendsSortedByPriority : backendHints;
// try to resolve and initialize all requested backends
let backend: Backend | undefined;
const errors = [];
const availableBackendNames = new Set<string>();
for (const backendName of backendNames) {
const resolveResult = await tryResolveAndInitializeBackend(backendName);
if (typeof resolveResult === 'string') {
errors.push({ name: backendName, err: resolveResult });
} else {
if (!backend) {
backend = resolveResult;
}
if (backend === resolveResult) {
availableBackendNames.add(backendName);
}
}
}
// if no backend is available, throw error.
if (!backend) {
throw new Error(`no available backend found. ERR: ${errors.map((e) => `[${e.name}] ${e.err}`).join(', ')}`);
}
// for each explicitly requested backend, if it's not available, output warning message.
for (const { name, err } of errors) {
if (backendHints.includes(name)) {
// eslint-disable-next-line no-console
console.warn(
`removing requested execution provider "${name}" from session options because it is not available: ${err}`,
);
}
}
const filteredEps = eps.filter((i) => availableBackendNames.has(typeof i === 'string' ? i : i.name));
return [
backend,
new Proxy(options, {
get: (target, prop) => {
if (prop === 'executionProviders') {
return filteredEps;
}
return Reflect.get(target, prop);
},
}),
];
};
+64
View File
@@ -0,0 +1,64 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { InferenceSession } from './inference-session.js';
import { OnnxValue } from './onnx-value.js';
/**
* @ignore
*/
export declare namespace SessionHandler {
type FeedsType = { [name: string]: OnnxValue };
type FetchesType = { [name: string]: OnnxValue | null };
type ReturnType = { [name: string]: OnnxValue };
}
/**
* Represents shared SessionHandler functionality
*
* @ignore
*/
interface SessionHandler {
dispose(): Promise<void>;
readonly inputNames: readonly string[];
readonly outputNames: readonly string[];
readonly inputMetadata: readonly InferenceSession.ValueMetadata[];
readonly outputMetadata: readonly InferenceSession.ValueMetadata[];
}
/**
* Represent a handler instance of an inference session.
*
* @ignore
*/
export interface InferenceSessionHandler extends SessionHandler {
startProfiling(): void;
endProfiling(): void;
run(
feeds: SessionHandler.FeedsType,
fetches: SessionHandler.FetchesType,
options: InferenceSession.RunOptions,
): Promise<SessionHandler.ReturnType>;
}
/**
* Represent a backend that provides implementation of model inferencing.
*
* @ignore
*/
export interface Backend {
/**
* Initialize the backend asynchronously. Should throw when failed.
*/
init(backendName: string): Promise<void>;
createInferenceSessionHandler(
uriOrBuffer: string | Uint8Array,
options?: InferenceSession.SessionOptions,
): Promise<InferenceSessionHandler>;
}
export { registerBackend } from './backend-impl.js';
+32
View File
@@ -0,0 +1,32 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { Env } from './env.js';
import { version } from './version.js';
type LogLevelType = Env['logLevel'];
let logLevelValue: Required<LogLevelType> = 'warning';
export const env: Env = {
wasm: {},
webgl: {} as Env.WebGLFlags,
webgpu: {} as Env.WebGpuFlags,
versions: { common: version },
set logLevel(value: LogLevelType) {
if (value === undefined) {
return;
}
if (typeof value !== 'string' || ['verbose', 'info', 'warning', 'error', 'fatal'].indexOf(value) === -1) {
throw new Error(`Unsupported logging level: ${value}`);
}
logLevelValue = value;
},
get logLevel(): Required<LogLevelType> {
return logLevelValue;
},
};
// set property 'logLevel' so that they can be correctly transferred to worker by `postMessage()`.
Object.defineProperty(env, 'logLevel', { enumerable: true });
+302
View File
@@ -0,0 +1,302 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { env as envImpl } from './env-impl.js';
import { TryGetGlobalType } from './type-helper.js';
export declare namespace Env {
export type WasmPathPrefix = string;
export interface WasmFilePaths {
/**
* Specify the override path for the main .wasm file.
*
* This path should be an absolute path.
*
* If not modified, the filename of the .wasm file is:
* - `ort-wasm-simd-threaded.wasm` for default build
* - `ort-wasm-simd-threaded.jsep.wasm` for JSEP build (with WebGPU and WebNN)
* - `ort-wasm-simd-threaded.asyncify.wasm` for WebGPU build with Asyncify (with WebNN)
* - `ort-wasm-simd-threaded.jspi.wasm` for WebGPU build with JSPI support (with WebNN)
*/
wasm?: URL | string;
/**
* Specify the override path for the main .mjs file.
*
* This path should be an absolute path.
*
* If not modified, the filename of the .mjs file is:
* - `ort-wasm-simd-threaded.mjs` for default build
* - `ort-wasm-simd-threaded.jsep.mjs` for JSEP build (with WebGPU and WebNN)
* - `ort-wasm-simd-threaded.asyncify.mjs` for WebGPU build with Asyncify (with WebNN)
* - `ort-wasm-simd-threaded.jspi.mjs` for WebGPU build with JSPI support (with WebNN)
*/
mjs?: URL | string;
}
export type WasmPrefixOrFilePaths = WasmPathPrefix | WasmFilePaths;
export interface WebAssemblyFlags {
/**
* set or get number of thread(s). If omitted or set to 0, number of thread(s) will be determined by system. If set
* to 1, no worker thread will be spawned.
*
* This setting is available only when WebAssembly multithread feature is available in current context.
*
* @defaultValue `0`
*/
numThreads?: number;
/**
* set a value indicating whether to enable SIMD.
*
* ONNX Runtime will perform feature detection based on the value of this property. Specifically, when the value is
* set to:
* - `undefined`, `true` or `"fixed"`: will check availability of Fixed-width SIMD.
* - `"relaxed"`: will check availability of Relaxed SIMD.
* - `false`: will not perform SIMD feature checking.
*
* Setting this property does not make ONNX Runtime to switch to the corresponding runtime automatically. User need
* to set `wasmPaths` or `wasmBinary` property to load the corresponding runtime.
*
* This setting is available only when WebAssembly SIMD feature is available in current context.
*
* @defaultValue `true`
*/
simd?: boolean | 'fixed' | 'relaxed';
/**
* set or get a boolean value indicating whether to enable trace.
*
* @defaultValue `false`
*
* @deprecated Use `env.trace` instead. If `env.trace` is set, this property will be ignored.
*/
trace?: boolean;
/**
* Set or get a number specifying the timeout for initialization of WebAssembly backend, in milliseconds. A zero
* value indicates no timeout is set.
*
* @defaultValue `0`
*/
initTimeout?: number;
/**
* Set a custom URL prefix to the .wasm/.mjs files, or an object of overrides for both .wasm/.mjs file. The override
* path should be an absolute path.
*/
wasmPaths?: WasmPrefixOrFilePaths;
/**
* Set a custom buffer which contains the WebAssembly binary. If this property is set, the `wasmPaths` property will
* be ignored.
*/
wasmBinary?: ArrayBufferLike | Uint8Array;
/**
* Set or get a boolean value indicating whether to proxy the execution of main thread to a worker thread.
*
* @defaultValue `false`
*/
proxy?: boolean;
}
export interface WebGLFlags {
/**
* Set or get the WebGL Context ID (webgl or webgl2).
*
* @defaultValue `'webgl2'`
*/
contextId?: 'webgl' | 'webgl2';
/**
* Get the WebGL rendering context.
*/
readonly context: WebGLRenderingContext;
/**
* Set or get the maximum batch size for matmul. 0 means to disable batching.
*
* @deprecated
*/
matmulMaxBatchSize?: number;
/**
* Set or get the texture cache mode.
*
* @defaultValue `'full'`
*/
textureCacheMode?: 'initializerOnly' | 'full';
/**
* Set or get the packed texture mode
*
* @defaultValue `false`
*/
pack?: boolean;
/**
* Set or get whether enable async download.
*
* @defaultValue `false`
*/
async?: boolean;
}
export interface WebGpuProfilingDataV1TensorMetadata {
dims: readonly number[];
dataType: string;
}
export interface WebGpuProfilingDataV1 {
version: 1;
inputsMetadata: readonly WebGpuProfilingDataV1TensorMetadata[];
outputsMetadata: readonly WebGpuProfilingDataV1TensorMetadata[];
kernelId: number;
kernelType: string;
kernelName: string;
programName: string;
startTime: number;
endTime: number;
}
export type WebGpuProfilingData = WebGpuProfilingDataV1;
export interface WebGpuFlags {
/**
* Set or get the profiling mode.
*
* @deprecated Use `env.webgpu.profiling.mode` instead. If `env.webgpu.profiling.mode` is set, this property will be
* ignored.
*/
profilingMode?: 'off' | 'default';
/**
* Set or get the profiling configuration.
*/
profiling: {
/**
* Set or get the profiling mode.
*
* @defaultValue `'off'`
*/
mode?: 'off' | 'default';
/**
* Set or get a callback function when a profiling data is received. If not set, the profiling data will be
* printed to console.
*/
ondata?: (data: WebGpuProfilingData) => void;
};
/**
* Set or get the power preference.
*
* Setting this property only has effect before the first WebGPU inference session is created. The value will be
* used as options for `navigator.gpu.requestAdapter()`.
*
* See {@link https://gpuweb.github.io/gpuweb/#dictdef-gpurequestadapteroptions} for more details.
*
* @defaultValue `undefined`
*
* @deprecated Create your own GPUAdapter, use it to create a GPUDevice instance and set {@link device} property if
* you want to use a specific power preference.
*/
powerPreference?: 'low-power' | 'high-performance';
/**
* Set or get the force fallback adapter flag.
*
* Setting this property only has effect before the first WebGPU inference session is created. The value will be
* used as options for `navigator.gpu.requestAdapter()`.
*
* See {@link https://gpuweb.github.io/gpuweb/#dictdef-gpurequestadapteroptions} for more details.
*
* @defaultValue `undefined`
*
* @deprecated Create your own GPUAdapter, use it to create a GPUDevice instance and set {@link device} property if
* you want to use a specific fallback option.
*/
forceFallbackAdapter?: boolean;
/**
* Set or get the adapter for WebGPU.
*
* Setting this property only has effect before the first WebGPU inference session is created. The value will be
* used as the GPU adapter for the underlying WebGPU backend to create GPU device.
*
* If this property is not set, it will be available to get after the first WebGPU inference session is created. The
* value will be the GPU adapter that created by the underlying WebGPU backend.
*
* When use with TypeScript, the type of this property is `GPUAdapter` defined in "@webgpu/types".
*
* @deprecated It is no longer recommended to use this property. The latest WebGPU spec adds `GPUDevice.adapterInfo`
* (https://www.w3.org/TR/webgpu/#dom-gpudevice-adapterinfo), which allows to get the adapter information from the
* device. When it's available, there is no need to set/get the {@link adapter} property.
*/
adapter: TryGetGlobalType<'GPUAdapter'>;
/**
* Set or get the GPU device for WebGPU.
*
* There are 3 valid scenarios of accessing this property:
* - Set a value before the first WebGPU inference session is created. The value will be used by the WebGPU backend
* to perform calculations. If the value is not a `GPUDevice` object, an error will be thrown.
* - Get the value before the first WebGPU inference session is created. This will try to create a new GPUDevice
* instance. Returns a `Promise` that resolves to a `GPUDevice` object.
* - Get the value after the first WebGPU inference session is created. Returns a resolved `Promise` to the
* `GPUDevice` object used by the WebGPU backend.
*/
get device(): Promise<TryGetGlobalType<'GPUDevice'>>;
set device(value: TryGetGlobalType<'GPUDevice'>);
/**
* Set or get whether validate input content.
*
* @defaultValue `false`
*/
validateInputContent?: boolean;
}
}
export interface Env {
/**
* set the severity level for logging.
*
* @defaultValue `'warning'`
*/
logLevel?: 'verbose' | 'info' | 'warning' | 'error' | 'fatal';
/**
* Indicate whether run in debug mode.
*
* @defaultValue `false`
*/
debug?: boolean;
/**
* set or get a boolean value indicating whether to enable trace.
*
* @defaultValue `false`
*/
trace?: boolean;
/**
* Get version of the current package.
*/
readonly versions: {
readonly common: string;
readonly web?: string;
readonly node?: string;
// eslint-disable-next-line @typescript-eslint/naming-convention
readonly 'react-native'?: string;
};
/**
* Represent a set of flags for WebAssembly
*/
readonly wasm: Env.WebAssemblyFlags;
/**
* Represent a set of flags for WebGL
*/
readonly webgl: Env.WebGLFlags;
/**
* Represent a set of flags for WebGPU
*/
readonly webgpu: Env.WebGpuFlags;
[name: string]: unknown;
}
/**
* Represent a set of flags as a global singleton.
*/
export const env: Env = envImpl;
+28
View File
@@ -0,0 +1,28 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
/**
* # ONNX Runtime JavaScript API
*
* ONNX Runtime JavaScript API is a unified API for all JavaScript usages, including the following NPM packages:
*
* - [onnxruntime-node](https://www.npmjs.com/package/onnxruntime-node)
* - [onnxruntime-web](https://www.npmjs.com/package/onnxruntime-web)
* - [onnxruntime-react-native](https://www.npmjs.com/package/onnxruntime-react-native)
*
* See also:
* - [Get Started](https://onnxruntime.ai/docs/get-started/with-javascript/)
* - [Inference examples](https://github.com/microsoft/onnxruntime-inference-examples/tree/main/js)
*
* @packageDocumentation
*/
export * from './backend.js';
export * from './env.js';
export * from './inference-session.js';
export * from './tensor.js';
export * from './tensor-conversion.js';
export * from './tensor-factory.js';
export * from './trace.js';
export * from './onnx-model.js';
export * from './onnx-value.js';
@@ -0,0 +1,241 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { resolveBackendAndExecutionProviders } from './backend-impl.js';
import { InferenceSessionHandler } from './backend.js';
import { InferenceSession as InferenceSessionInterface } from './inference-session.js';
import { OnnxValue } from './onnx-value.js';
import { Tensor } from './tensor.js';
import { TRACE_FUNC_BEGIN, TRACE_FUNC_END, TRACE_EVENT_BEGIN, TRACE_EVENT_END } from './trace.js';
type SessionOptions = InferenceSessionInterface.SessionOptions;
type RunOptions = InferenceSessionInterface.RunOptions;
type FeedsType = InferenceSessionInterface.FeedsType;
type FetchesType = InferenceSessionInterface.FetchesType;
type ReturnType = InferenceSessionInterface.ReturnType;
export class InferenceSession implements InferenceSessionInterface {
private constructor(handler: InferenceSessionHandler) {
this.handler = handler;
}
run(feeds: FeedsType, options?: RunOptions): Promise<ReturnType>;
run(feeds: FeedsType, fetches: FetchesType, options?: RunOptions): Promise<ReturnType>;
async run(feeds: FeedsType, arg1?: FetchesType | RunOptions, arg2?: RunOptions): Promise<ReturnType> {
TRACE_FUNC_BEGIN();
TRACE_EVENT_BEGIN('InferenceSession.run');
const fetches: { [name: string]: OnnxValue | null } = {};
let options: RunOptions = {};
// check inputs
if (typeof feeds !== 'object' || feeds === null || feeds instanceof Tensor || Array.isArray(feeds)) {
throw new TypeError(
"'feeds' must be an object that use input names as keys and OnnxValue as corresponding values.",
);
}
let isFetchesEmpty = true;
// determine which override is being used
if (typeof arg1 === 'object') {
if (arg1 === null) {
throw new TypeError('Unexpected argument[1]: cannot be null.');
}
if (arg1 instanceof Tensor) {
throw new TypeError("'fetches' cannot be a Tensor");
}
if (Array.isArray(arg1)) {
if (arg1.length === 0) {
throw new TypeError("'fetches' cannot be an empty array.");
}
isFetchesEmpty = false;
// output names
for (const name of arg1) {
if (typeof name !== 'string') {
throw new TypeError("'fetches' must be a string array or an object.");
}
if (this.outputNames.indexOf(name) === -1) {
throw new RangeError(`'fetches' contains invalid output name: ${name}.`);
}
fetches[name] = null;
}
if (typeof arg2 === 'object' && arg2 !== null) {
options = arg2;
} else if (typeof arg2 !== 'undefined') {
throw new TypeError("'options' must be an object.");
}
} else {
// decide whether arg1 is fetches or options
// if any output name is present and its value is valid OnnxValue, we consider it fetches
let isFetches = false;
const arg1Keys = Object.getOwnPropertyNames(arg1);
for (const name of this.outputNames) {
if (arg1Keys.indexOf(name) !== -1) {
const v = (arg1 as InferenceSessionInterface.NullableOnnxValueMapType)[name];
if (v === null || v instanceof Tensor) {
isFetches = true;
isFetchesEmpty = false;
fetches[name] = v;
}
}
}
if (isFetches) {
if (typeof arg2 === 'object' && arg2 !== null) {
options = arg2;
} else if (typeof arg2 !== 'undefined') {
throw new TypeError("'options' must be an object.");
}
} else {
options = arg1 as RunOptions;
}
}
} else if (typeof arg1 !== 'undefined') {
throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'.");
}
// check if all inputs are in feed
for (const name of this.inputNames) {
if (typeof feeds[name] === 'undefined') {
throw new Error(`input '${name}' is missing in 'feeds'.`);
}
}
// if no fetches is specified, we use the full output names list
if (isFetchesEmpty) {
for (const name of this.outputNames) {
fetches[name] = null;
}
}
// feeds, fetches and options are prepared
const results = await this.handler.run(feeds, fetches, options);
const returnValue: { [name: string]: OnnxValue } = {};
for (const key in results) {
if (Object.hasOwnProperty.call(results, key)) {
const result = results[key];
if (result instanceof Tensor) {
returnValue[key] = result;
} else {
returnValue[key] = new Tensor(result.type, result.data, result.dims);
}
}
}
TRACE_EVENT_END('InferenceSession.run');
TRACE_FUNC_END();
return returnValue;
}
async release(): Promise<void> {
return this.handler.dispose();
}
static create(path: string, options?: SessionOptions): Promise<InferenceSessionInterface>;
static create(buffer: ArrayBufferLike, options?: SessionOptions): Promise<InferenceSessionInterface>;
static create(
buffer: ArrayBufferLike,
byteOffset: number,
byteLength?: number,
options?: SessionOptions,
): Promise<InferenceSessionInterface>;
static create(buffer: Uint8Array, options?: SessionOptions): Promise<InferenceSessionInterface>;
static async create(
arg0: string | ArrayBufferLike | Uint8Array,
arg1?: SessionOptions | number,
arg2?: number,
arg3?: SessionOptions,
): Promise<InferenceSessionInterface> {
TRACE_FUNC_BEGIN();
TRACE_EVENT_BEGIN('InferenceSession.create');
// either load from a file or buffer
let filePathOrUint8Array: string | Uint8Array;
let options: SessionOptions = {};
if (typeof arg0 === 'string') {
filePathOrUint8Array = arg0;
if (typeof arg1 === 'object' && arg1 !== null) {
options = arg1;
} else if (typeof arg1 !== 'undefined') {
throw new TypeError("'options' must be an object.");
}
} else if (arg0 instanceof Uint8Array) {
filePathOrUint8Array = arg0;
if (typeof arg1 === 'object' && arg1 !== null) {
options = arg1;
} else if (typeof arg1 !== 'undefined') {
throw new TypeError("'options' must be an object.");
}
} else if (
arg0 instanceof ArrayBuffer ||
(typeof SharedArrayBuffer !== 'undefined' && arg0 instanceof SharedArrayBuffer)
) {
const buffer = arg0;
let byteOffset = 0;
let byteLength = arg0.byteLength;
if (typeof arg1 === 'object' && arg1 !== null) {
options = arg1;
} else if (typeof arg1 === 'number') {
byteOffset = arg1;
if (!Number.isSafeInteger(byteOffset)) {
throw new RangeError("'byteOffset' must be an integer.");
}
if (byteOffset < 0 || byteOffset >= buffer.byteLength) {
throw new RangeError(`'byteOffset' is out of range [0, ${buffer.byteLength}).`);
}
byteLength = arg0.byteLength - byteOffset;
if (typeof arg2 === 'number') {
byteLength = arg2;
if (!Number.isSafeInteger(byteLength)) {
throw new RangeError("'byteLength' must be an integer.");
}
if (byteLength <= 0 || byteOffset + byteLength > buffer.byteLength) {
throw new RangeError(`'byteLength' is out of range (0, ${buffer.byteLength - byteOffset}].`);
}
if (typeof arg3 === 'object' && arg3 !== null) {
options = arg3;
} else if (typeof arg3 !== 'undefined') {
throw new TypeError("'options' must be an object.");
}
} else if (typeof arg2 !== 'undefined') {
throw new TypeError("'byteLength' must be a number.");
}
} else if (typeof arg1 !== 'undefined') {
throw new TypeError("'options' must be an object.");
}
filePathOrUint8Array = new Uint8Array(buffer, byteOffset, byteLength);
} else {
throw new TypeError("Unexpected argument[0]: must be 'path' or 'buffer'.");
}
// resolve backend, update session options with validated EPs, and create session handler
const [backend, optionsWithValidatedEPs] = await resolveBackendAndExecutionProviders(options);
const handler = await backend.createInferenceSessionHandler(filePathOrUint8Array, optionsWithValidatedEPs);
TRACE_EVENT_END('InferenceSession.create');
TRACE_FUNC_END();
return new InferenceSession(handler);
}
startProfiling(): void {
this.handler.startProfiling();
}
endProfiling(): void {
this.handler.endProfiling();
}
get inputNames(): readonly string[] {
return this.handler.inputNames;
}
get outputNames(): readonly string[] {
return this.handler.outputNames;
}
get inputMetadata(): readonly InferenceSessionInterface.ValueMetadata[] {
return this.handler.inputMetadata;
}
get outputMetadata(): readonly InferenceSessionInterface.ValueMetadata[] {
return this.handler.outputMetadata;
}
private handler: InferenceSessionHandler;
}
+651
View File
@@ -0,0 +1,651 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { InferenceSession as InferenceSessionImpl } from './inference-session-impl.js';
import { OnnxModelOptions } from './onnx-model.js';
import { OnnxValue, OnnxValueDataLocation } from './onnx-value.js';
import type { Tensor } from './tensor.js';
import { TryGetGlobalType } from './type-helper.js';
/* eslint-disable @typescript-eslint/no-redeclare */
export declare namespace InferenceSession {
// #region input/output types
type OnnxValueMapType = { readonly [name: string]: OnnxValue };
type NullableOnnxValueMapType = { readonly [name: string]: OnnxValue | null };
/**
* A feeds (model inputs) is an object that uses input names as keys and OnnxValue as corresponding values.
*/
type FeedsType = OnnxValueMapType;
/**
* A fetches (model outputs) could be one of the following:
*
* - Omitted. Use model's output names definition.
* - An array of string indicating the output names.
* - An object that use output names as keys and OnnxValue or null as corresponding values.
*
* @remarks
* different from input argument, in output, OnnxValue is optional. If an OnnxValue is present it will be
* used as a pre-allocated value by the inference engine; if omitted, inference engine will allocate buffer
* internally.
*/
type FetchesType = readonly string[] | NullableOnnxValueMapType;
/**
* A inferencing return type is an object that uses output names as keys and OnnxValue as corresponding values.
*/
type ReturnType = OnnxValueMapType;
// #endregion
// #region session options
/**
* A set of configurations for session behavior.
*/
export interface SessionOptions extends OnnxModelOptions {
/**
* An array of execution provider options.
*
* An execution provider option can be a string indicating the name of the execution provider,
* or an object of corresponding type.
*/
executionProviders?: readonly ExecutionProviderConfig[];
/**
* The intra OP threads number.
*
* This setting is available only in ONNXRuntime (Node.js binding and react-native).
*/
intraOpNumThreads?: number;
/**
* The inter OP threads number.
*
* This setting is available only in ONNXRuntime (Node.js binding and react-native).
*/
interOpNumThreads?: number;
/**
* The free dimension override.
*
* This setting is available only in ONNXRuntime (Node.js binding and react-native) or WebAssembly backend
*/
freeDimensionOverrides?: { readonly [dimensionName: string]: number };
/**
* The optimization level.
*
* This setting is available only in ONNXRuntime (Node.js binding and react-native) or WebAssembly backend
*/
graphOptimizationLevel?: 'disabled' | 'basic' | 'extended' | 'layout' | 'all';
/**
* Whether enable CPU memory arena.
*
* This setting is available only in ONNXRuntime (Node.js binding and react-native) or WebAssembly backend
*/
enableCpuMemArena?: boolean;
/**
* Whether enable memory pattern.
*
* This setting is available only in ONNXRuntime (Node.js binding and react-native) or WebAssembly backend
*/
enableMemPattern?: boolean;
/**
* Execution mode.
*
* This setting is available only in ONNXRuntime (Node.js binding and react-native) or WebAssembly backend
*/
executionMode?: 'sequential' | 'parallel';
/**
* Optimized model file path.
*
* If this setting is specified, the optimized model will be dumped. In browser, a blob will be created
* with a pop-up window.
*/
optimizedModelFilePath?: string;
/**
* Whether enable profiling.
*
* This setting is a placeholder for a future use.
*/
enableProfiling?: boolean;
/**
* File prefix for profiling.
*
* This setting is a placeholder for a future use.
*/
profileFilePrefix?: string;
/**
* Log ID.
*
* This setting is available only in ONNXRuntime (Node.js binding and react-native) or WebAssembly backend
*/
logId?: string;
/**
* Log severity level. See
* https://github.com/microsoft/onnxruntime/blob/main/include/onnxruntime/core/common/logging/severity.h
*
* This setting is available only in ONNXRuntime (Node.js binding and react-native) or WebAssembly backend
*/
logSeverityLevel?: 0 | 1 | 2 | 3 | 4;
/**
* Log verbosity level.
*
* This setting is available only in WebAssembly backend. Will support Node.js binding and react-native later
*/
logVerbosityLevel?: number;
/**
* Specify string as a preferred data location for all outputs, or an object that use output names as keys and a
* preferred data location as corresponding values.
*
* This setting is available only in ONNXRuntime Web for WebGL and WebGPU EP.
*/
preferredOutputLocation?: OnnxValueDataLocation | { readonly [outputName: string]: OnnxValueDataLocation };
/**
* Whether enable graph capture.
* This setting is available only in ONNXRuntime Web for WebGPU EP.
*/
enableGraphCapture?: boolean;
/**
* Store configurations for a session. See
* https://github.com/microsoft/onnxruntime/blob/main/include/onnxruntime/core/session/
* onnxruntime_session_options_config_keys.h
*
* This setting is available only in WebAssembly backend. Will support Node.js binding and react-native later
*
* @example
* ```js
* extra: {
* session: {
* set_denormal_as_zero: "1",
* disable_prepacking: "1"
* },
* optimization: {
* enable_gelu_approximation: "1"
* }
* }
* ```
*/
extra?: Record<string, unknown>;
}
// #region execution providers
// Currently, we have the following backends to support execution providers:
// Backend Node.js binding: supports 'cpu', 'dml' (win32), 'coreml' (macOS) and 'cuda' (linux).
// Backend WebAssembly: supports 'cpu', 'wasm', 'webgpu' and 'webnn'.
// Backend ONNX.js: supports 'webgl'.
// Backend React Native: supports 'cpu', 'xnnpack', 'coreml' (iOS), 'nnapi' (Android).
interface ExecutionProviderOptionMap {
coreml: CoreMLExecutionProviderOption;
cpu: CpuExecutionProviderOption;
cuda: CudaExecutionProviderOption;
dml: DmlExecutionProviderOption;
nnapi: NnapiExecutionProviderOption;
tensorrt: TensorRtExecutionProviderOption;
wasm: WebAssemblyExecutionProviderOption;
webgl: WebGLExecutionProviderOption;
webgpu: WebGpuExecutionProviderOption;
webnn: WebNNExecutionProviderOption;
qnn: QnnExecutionProviderOption;
xnnpack: XnnpackExecutionProviderOption;
}
type ExecutionProviderName = keyof ExecutionProviderOptionMap;
type ExecutionProviderConfig =
| ExecutionProviderOptionMap[ExecutionProviderName]
| ExecutionProviderOption
| ExecutionProviderName
| string;
export interface ExecutionProviderOption {
readonly name: string;
}
export interface CpuExecutionProviderOption extends ExecutionProviderOption {
readonly name: 'cpu';
useArena?: boolean;
}
export interface CudaExecutionProviderOption extends ExecutionProviderOption {
readonly name: 'cuda';
deviceId?: number;
}
export interface DmlExecutionProviderOption extends ExecutionProviderOption {
readonly name: 'dml';
deviceId?: number;
}
export interface TensorRtExecutionProviderOption extends ExecutionProviderOption {
readonly name: 'tensorrt';
deviceId?: number;
}
export interface WebAssemblyExecutionProviderOption extends ExecutionProviderOption {
readonly name: 'wasm';
}
export interface WebGLExecutionProviderOption extends ExecutionProviderOption {
readonly name: 'webgl';
// TODO: add flags
}
export interface XnnpackExecutionProviderOption extends ExecutionProviderOption {
readonly name: 'xnnpack';
}
export interface WebGpuExecutionProviderOption extends ExecutionProviderOption {
readonly name: 'webgpu';
/**
* Specify the preferred layout when running layout sensitive operators.
*
* @default 'NCHW'
*/
preferredLayout?: 'NCHW' | 'NHWC';
/**
* Specify a list of node names that should be executed on CPU even when WebGPU EP is used.
*/
forceCpuNodeNames?: readonly string[];
/**
* Specify the validation mode for WebGPU execution provider.
* - 'disabled': Disable all validation.
* When used in Node.js, disable validation may cause process crash if WebGPU errors occur. Be cautious when using
* this mode.
* When used in web, this mode is equivalent to 'wgpuOnly'.
* - 'wgpuOnly': Perform WebGPU internal validation only.
* - 'basic': Perform basic validation including WebGPU internal validation. This is the default mode.
* - 'full': Perform full validation. This mode may have performance impact. Use it for debugging purpose.
*
* @default 'basic'
*/
validationMode?: 'disabled' | 'wgpuOnly' | 'basic' | 'full';
/**
* Specify an optional WebGPU device to be used by the WebGPU execution provider.
*/
device?: TryGetGlobalType<'GPUDevice'>;
}
// #region WebNN options
interface WebNNExecutionProviderName extends ExecutionProviderOption {
readonly name: 'webnn';
}
/**
* Represents a set of options for creating a WebNN MLContext.
*
* @see https://www.w3.org/TR/webnn/#dictdef-mlcontextoptions
*/
export interface WebNNContextOptions {
deviceType?: 'cpu' | 'gpu' | 'npu';
numThreads?: number;
powerPreference?: 'default' | 'low-power' | 'high-performance';
}
/**
* Represents a set of options for WebNN execution provider without MLContext.
*/
export interface WebNNOptionsWithoutMLContext extends WebNNExecutionProviderName, WebNNContextOptions {
context?: never;
}
/**
* Represents a set of options for WebNN execution provider with MLContext.
*
* When MLContext is provided, the deviceType is also required so that the WebNN EP can determine the preferred
* channel layout.
*
* @see https://www.w3.org/TR/webnn/#dom-ml-createcontext
*/
export interface WebNNOptionsWithMLContext
extends
WebNNExecutionProviderName,
Omit<WebNNContextOptions, 'deviceType'>,
Required<Pick<WebNNContextOptions, 'deviceType'>> {
context: TryGetGlobalType<'MLContext'>;
}
/**
* Represents a set of options for WebNN execution provider with MLContext which is created from GPUDevice.
*
* @see https://www.w3.org/TR/webnn/#dom-ml-createcontext-gpudevice
*/
export interface WebNNOptionsWebGpu extends WebNNExecutionProviderName {
context: TryGetGlobalType<'MLContext'>;
gpuDevice: TryGetGlobalType<'GPUDevice'>;
}
/**
* Options for WebNN execution provider.
*/
export type WebNNExecutionProviderOption =
| WebNNOptionsWithoutMLContext
| WebNNOptionsWithMLContext
| WebNNOptionsWebGpu;
// #endregion
export interface QnnExecutionProviderOption extends ExecutionProviderOption {
readonly name: 'qnn';
/**
* Specify the QNN backend type. E.g., 'cpu' or 'htp'.
* Mutually exclusive with `backendPath`.
*
* @default 'htp'
*/
backendType?: string;
/**
* Specify a path to the QNN backend library.
* Mutually exclusive with `backendType`.
*/
backendPath?: string;
/**
* Specify whether to enable HTP FP16 precision.
*
* @default true
*/
enableFp16Precision?: boolean;
}
export interface CoreMLExecutionProviderOption extends ExecutionProviderOption {
readonly name: 'coreml';
/**
* The bit flags for CoreML execution provider.
*
* ```
* COREML_FLAG_USE_CPU_ONLY = 0x001
* COREML_FLAG_ENABLE_ON_SUBGRAPH = 0x002
* COREML_FLAG_ONLY_ENABLE_DEVICE_WITH_ANE = 0x004
* COREML_FLAG_ONLY_ALLOW_STATIC_INPUT_SHAPES = 0x008
* COREML_FLAG_CREATE_MLPROGRAM = 0x010
* COREML_FLAG_USE_CPU_AND_GPU = 0x020
* ```
*
* See include/onnxruntime/core/providers/coreml/coreml_provider_factory.h for more details.
*
* This flag is available only in ONNXRuntime (Node.js binding).
*/
coreMlFlags?: number;
/**
* Specify whether to use CPU only in CoreML EP.
*
* This setting is available only in ONNXRuntime (react-native).
*/
useCPUOnly?: boolean;
useCPUAndGPU?: boolean;
/**
* Specify whether to enable CoreML EP on subgraph.
*
* This setting is available only in ONNXRuntime (react-native).
*/
enableOnSubgraph?: boolean;
/**
* Specify whether to only enable CoreML EP for Apple devices with ANE (Apple Neural Engine).
*
* This setting is available only in ONNXRuntime (react-native).
*/
onlyEnableDeviceWithANE?: boolean;
}
export interface NnapiExecutionProviderOption extends ExecutionProviderOption {
readonly name: 'nnapi';
useFP16?: boolean;
useNCHW?: boolean;
cpuDisabled?: boolean;
cpuOnly?: boolean;
}
// #endregion
// #endregion
// #region run options
/**
* A set of configurations for inference run behavior
*/
export interface RunOptions {
/**
* Log severity level. See
* https://github.com/microsoft/onnxruntime/blob/main/include/onnxruntime/core/common/logging/severity.h
*
* This setting is available only in ONNXRuntime (Node.js binding and react-native) or WebAssembly backend
*/
logSeverityLevel?: 0 | 1 | 2 | 3 | 4;
/**
* Log verbosity level.
*
* This setting is available only in WebAssembly backend. Will support Node.js binding and react-native later
*/
logVerbosityLevel?: number;
/**
* Terminate all incomplete OrtRun calls as soon as possible if true
*
* This setting is available only in WebAssembly backend. Will support Node.js binding and react-native later
*/
terminate?: boolean;
/**
* A tag for the Run() calls using this
*
* This setting is available only in ONNXRuntime (Node.js binding and react-native) or WebAssembly backend
*/
tag?: string;
/**
* Set a single run configuration entry. See
* https://github.com/microsoft/onnxruntime/blob/main/include/onnxruntime/core/session/
* onnxruntime_run_options_config_keys.h
*
* This setting is available only in WebAssembly backend. Will support Node.js binding and react-native later
*
* @example
*
* ```js
* extra: {
* memory: {
* enable_memory_arena_shrinkage: "1",
* }
* }
* ```
*/
extra?: Record<string, unknown>;
}
// #endregion
// #region value metadata
/**
* The common part of the value metadata type for both tensor and non-tensor values.
*/
export interface ValueMetadataBase {
/**
* The name of the specified input or output.
*/
readonly name: string;
}
/**
* Represents the metadata of a non-tensor value.
*/
export interface NonTensorValueMetadata extends ValueMetadataBase {
/**
* Get a value indicating whether the value is a tensor.
*/
readonly isTensor: false;
}
/**
* Represents the metadata of a tensor value.
*/
export interface TensorValueMetadata extends ValueMetadataBase {
/**
* Get a value indicating whether the value is a tensor.
*/
readonly isTensor: true;
/**
* Get the data type of the tensor.
*/
readonly type: Tensor.Type;
/**
* Get the shape of the tensor.
*
* If the shape is not defined, the value will an empty array. Otherwise, it will be an array representing the shape
* of the tensor. Each element in the array can be a number or a string. If the element is a number, it represents
* the corresponding dimension size. If the element is a string, it represents a symbolic dimension.
*/
readonly shape: ReadonlyArray<number | string>;
}
/**
* Represents the metadata of a value.
*/
export type ValueMetadata = NonTensorValueMetadata | TensorValueMetadata;
// #endregion
}
/**
* Represent a runtime instance of an ONNX model.
*/
export interface InferenceSession {
// #region run()
/**
* Execute the model asynchronously with the given feeds and options.
*
* @param feeds - Representation of the model input. See type description of `InferenceSession.InputType` for detail.
* @param options - Optional. A set of options that controls the behavior of model inference.
* @returns A promise that resolves to a map, which uses output names as keys and OnnxValue as corresponding values.
*/
run(feeds: InferenceSession.FeedsType, options?: InferenceSession.RunOptions): Promise<InferenceSession.ReturnType>;
/**
* Execute the model asynchronously with the given feeds, fetches and options.
*
* @param feeds - Representation of the model input. See type description of `InferenceSession.InputType` for detail.
* @param fetches - Representation of the model output. See type description of `InferenceSession.OutputType` for
* detail.
* @param options - Optional. A set of options that controls the behavior of model inference.
* @returns A promise that resolves to a map, which uses output names as keys and OnnxValue as corresponding values.
*/
run(
feeds: InferenceSession.FeedsType,
fetches: InferenceSession.FetchesType,
options?: InferenceSession.RunOptions,
): Promise<InferenceSession.ReturnType>;
// #endregion
// #region release()
/**
* Release the inference session and the underlying resources.
*/
release(): Promise<void>;
// #endregion
// #region profiling
/**
* Start profiling.
*/
startProfiling(): void;
/**
* End profiling.
*/
endProfiling(): void;
// #endregion
// #region metadata
/**
* Get input names of the loaded model.
*/
readonly inputNames: readonly string[];
/**
* Get output names of the loaded model.
*/
readonly outputNames: readonly string[];
/**
* Get input metadata of the loaded model.
*/
readonly inputMetadata: readonly InferenceSession.ValueMetadata[];
/**
* Get output metadata of the loaded model.
*/
readonly outputMetadata: readonly InferenceSession.ValueMetadata[];
// #endregion
}
export interface InferenceSessionFactory {
// #region create()
/**
* Create a new inference session and load model asynchronously from an ONNX model file.
*
* @param uri - The URI or file path of the model to load.
* @param options - specify configuration for creating a new inference session.
* @returns A promise that resolves to an InferenceSession object.
*/
create(uri: string, options?: InferenceSession.SessionOptions): Promise<InferenceSession>;
/**
* Create a new inference session and load model asynchronously from an array bufer.
*
* @param buffer - An ArrayBuffer representation of an ONNX model.
* @param options - specify configuration for creating a new inference session.
* @returns A promise that resolves to an InferenceSession object.
*/
create(buffer: ArrayBufferLike, options?: InferenceSession.SessionOptions): Promise<InferenceSession>;
/**
* Create a new inference session and load model asynchronously from segment of an array bufer.
*
* @param buffer - An ArrayBuffer representation of an ONNX model.
* @param byteOffset - The beginning of the specified portion of the array buffer.
* @param byteLength - The length in bytes of the array buffer.
* @param options - specify configuration for creating a new inference session.
* @returns A promise that resolves to an InferenceSession object.
*/
create(
buffer: ArrayBufferLike,
byteOffset: number,
byteLength?: number,
options?: InferenceSession.SessionOptions,
): Promise<InferenceSession>;
/**
* Create a new inference session and load model asynchronously from a Uint8Array.
*
* @param buffer - A Uint8Array representation of an ONNX model.
* @param options - specify configuration for creating a new inference session.
* @returns A promise that resolves to an InferenceSession object.
*/
create(buffer: Uint8Array, options?: InferenceSession.SessionOptions): Promise<InferenceSession>;
// #endregion
}
// eslint-disable-next-line @typescript-eslint/naming-convention
export const InferenceSession: InferenceSessionFactory = InferenceSessionImpl;
+57
View File
@@ -0,0 +1,57 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
/**
* A string that represents a file's URL or path.
*
* Path is vailable only in onnxruntime-node or onnxruntime-web running in Node.js.
*/
export type FileUrlOrPath = string;
/**
* A Blob object that represents a file.
*/
export type FileBlob = Blob;
/**
* A Uint8Array, ArrayBuffer or SharedArrayBuffer object that represents a file content.
*
* When it is an ArrayBuffer or SharedArrayBuffer, the whole buffer is assumed to be the file content.
*/
export type FileData = Uint8Array | ArrayBufferLike;
/**
* Represents a file that can be loaded by the ONNX Runtime JavaScript API.
*/
export type FileType = FileUrlOrPath | FileBlob | FileData;
/**
* Represents an external data file.
*/
export interface ExternalDataFileDescription {
/**
* Specify the external data file.
*/
data: FileType;
/**
* Specify the file path.
*/
path: string;
}
/**
* Represents an external data file.
*
* When using a string, it should be a file URL or path that in the same directory as the model file.
*/
export type ExternalDataFileType = ExternalDataFileDescription | FileUrlOrPath;
/**
* Options for model loading.
*/
export interface OnnxModelOptions {
/**
* Specifying a list of files that represents the external data.
*/
externalData?: readonly ExternalDataFileType[];
}
+18
View File
@@ -0,0 +1,18 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { Tensor } from './tensor.js';
export type NonTensorType = never;
/**
* Type OnnxValue Represents both tensors and non-tensors value for model's inputs/outputs.
*
* NOTE: currently not support non-tensor
*/
export type OnnxValue = Tensor | NonTensorType;
/**
* Type OnnxValueDataLocation represents the location of the data of an OnnxValue.
*/
export type OnnxValueDataLocation = Tensor.DataLocation;
@@ -0,0 +1,214 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { TensorToDataUrlOptions, TensorToImageDataOptions } from './tensor-conversion.js';
import { Tensor } from './tensor.js';
/**
* implementation of Tensor.toDataURL()
*/
export const tensorToDataURL = (tensor: Tensor, options?: TensorToDataUrlOptions): string => {
const canvas = typeof document !== 'undefined' ? document.createElement('canvas') : new OffscreenCanvas(1, 1);
canvas.width = tensor.dims[3];
canvas.height = tensor.dims[2];
const pixels2DContext = canvas.getContext('2d') as
| CanvasRenderingContext2D
| OffscreenCanvasRenderingContext2D
| null;
if (pixels2DContext != null) {
// Default values for height and width & format
let width: number;
let height: number;
if (options?.tensorLayout !== undefined && options.tensorLayout === 'NHWC') {
width = tensor.dims[2];
height = tensor.dims[3];
} else {
// Default layout is NCWH
width = tensor.dims[3];
height = tensor.dims[2];
}
const inputformat = options?.format !== undefined ? options.format : 'RGB';
const norm = options?.norm;
let normMean: [number, number, number, number];
let normBias: [number, number, number, number];
if (norm === undefined || norm.mean === undefined) {
normMean = [255, 255, 255, 255];
} else {
if (typeof norm.mean === 'number') {
normMean = [norm.mean, norm.mean, norm.mean, norm.mean];
} else {
normMean = [norm.mean[0], norm.mean[1], norm.mean[2], 0];
if (norm.mean[3] !== undefined) {
normMean[3] = norm.mean[3];
}
}
}
if (norm === undefined || norm.bias === undefined) {
normBias = [0, 0, 0, 0];
} else {
if (typeof norm.bias === 'number') {
normBias = [norm.bias, norm.bias, norm.bias, norm.bias];
} else {
normBias = [norm.bias[0], norm.bias[1], norm.bias[2], 0];
if (norm.bias[3] !== undefined) {
normBias[3] = norm.bias[3];
}
}
}
const stride = height * width;
// Default pointer assignments
let rTensorPointer = 0,
gTensorPointer = stride,
bTensorPointer = stride * 2,
aTensorPointer = -1;
// Updating the pointer assignments based on the input image format
if (inputformat === 'RGBA') {
rTensorPointer = 0;
gTensorPointer = stride;
bTensorPointer = stride * 2;
aTensorPointer = stride * 3;
} else if (inputformat === 'RGB') {
rTensorPointer = 0;
gTensorPointer = stride;
bTensorPointer = stride * 2;
} else if (inputformat === 'RBG') {
rTensorPointer = 0;
bTensorPointer = stride;
gTensorPointer = stride * 2;
}
for (let i = 0; i < height; i++) {
for (let j = 0; j < width; j++) {
const R = ((tensor.data[rTensorPointer++] as number) - normBias[0]) * normMean[0]; // R value
const G = ((tensor.data[gTensorPointer++] as number) - normBias[1]) * normMean[1]; // G value
const B = ((tensor.data[bTensorPointer++] as number) - normBias[2]) * normMean[2]; // B value
const A = aTensorPointer === -1 ? 255 : ((tensor.data[aTensorPointer++] as number) - normBias[3]) * normMean[3]; // A value
pixels2DContext.fillStyle = 'rgba(' + R + ',' + G + ',' + B + ',' + A + ')';
pixels2DContext.fillRect(j, i, 1, 1);
}
}
if ('toDataURL' in canvas) {
return canvas.toDataURL();
} else {
throw new Error('toDataURL is not supported');
}
} else {
throw new Error('Can not access image data');
}
};
/**
* implementation of Tensor.toImageData()
*/
export const tensorToImageData = (tensor: Tensor, options?: TensorToImageDataOptions): ImageData => {
const pixels2DContext =
typeof document !== 'undefined'
? document.createElement('canvas').getContext('2d')
: (new OffscreenCanvas(1, 1).getContext('2d') as OffscreenCanvasRenderingContext2D);
let image: ImageData;
if (pixels2DContext != null) {
// Default values for height and width & format
let width: number;
let height: number;
let channels: number;
if (options?.tensorLayout !== undefined && options.tensorLayout === 'NHWC') {
width = tensor.dims[2];
height = tensor.dims[1];
channels = tensor.dims[3];
} else {
// Default layout is NCWH
width = tensor.dims[3];
height = tensor.dims[2];
channels = tensor.dims[1];
}
const inputformat = options !== undefined ? (options.format !== undefined ? options.format : 'RGB') : 'RGB';
const norm = options?.norm;
let normMean: [number, number, number, number];
let normBias: [number, number, number, number];
if (norm === undefined || norm.mean === undefined) {
normMean = [255, 255, 255, 255];
} else {
if (typeof norm.mean === 'number') {
normMean = [norm.mean, norm.mean, norm.mean, norm.mean];
} else {
normMean = [norm.mean[0], norm.mean[1], norm.mean[2], 255];
if (norm.mean[3] !== undefined) {
normMean[3] = norm.mean[3];
}
}
}
if (norm === undefined || norm.bias === undefined) {
normBias = [0, 0, 0, 0];
} else {
if (typeof norm.bias === 'number') {
normBias = [norm.bias, norm.bias, norm.bias, norm.bias];
} else {
normBias = [norm.bias[0], norm.bias[1], norm.bias[2], 0];
if (norm.bias[3] !== undefined) {
normBias[3] = norm.bias[3];
}
}
}
const stride = height * width;
if (options !== undefined) {
if (
(options.format !== undefined && channels === 4 && options.format !== 'RGBA') ||
(channels === 3 && options.format !== 'RGB' && options.format !== 'BGR')
) {
throw new Error("Tensor format doesn't match input tensor dims");
}
}
// Default pointer assignments
const step = 4;
let rImagePointer = 0,
gImagePointer = 1,
bImagePointer = 2,
aImagePointer = 3;
let rTensorPointer = 0,
gTensorPointer = stride,
bTensorPointer = stride * 2,
aTensorPointer = -1;
// Updating the pointer assignments based on the input image format
if (inputformat === 'RGBA') {
rTensorPointer = 0;
gTensorPointer = stride;
bTensorPointer = stride * 2;
aTensorPointer = stride * 3;
} else if (inputformat === 'RGB') {
rTensorPointer = 0;
gTensorPointer = stride;
bTensorPointer = stride * 2;
} else if (inputformat === 'RBG') {
rTensorPointer = 0;
bTensorPointer = stride;
gTensorPointer = stride * 2;
}
image = pixels2DContext.createImageData(width, height);
for (
let i = 0;
i < height * width;
rImagePointer += step, gImagePointer += step, bImagePointer += step, aImagePointer += step, i++
) {
image.data[rImagePointer] = ((tensor.data[rTensorPointer++] as number) - normBias[0]) * normMean[0]; // R value
image.data[gImagePointer] = ((tensor.data[gTensorPointer++] as number) - normBias[1]) * normMean[1]; // G value
image.data[bImagePointer] = ((tensor.data[bTensorPointer++] as number) - normBias[2]) * normMean[2]; // B value
image.data[aImagePointer] =
aTensorPointer === -1 ? 255 : ((tensor.data[aTensorPointer++] as number) - normBias[3]) * normMean[3]; // A value
}
} else {
throw new Error('Can not access image data');
}
return image;
};
+34
View File
@@ -0,0 +1,34 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { OptionsFormat, OptionsNormalizationParameters, OptionsTensorLayout } from './tensor-factory.js';
export interface TensorToDataUrlOptions extends OptionsTensorLayout, OptionsFormat, OptionsNormalizationParameters {}
export interface TensorToImageDataOptions extends OptionsTensorLayout, OptionsFormat, OptionsNormalizationParameters {}
export interface ConversionUtils {
/**
* creates a DataURL instance from tensor
*
* @param options - An optional object representing options for creating a DataURL instance from the tensor.
*
* The following default settings will be applied:
* - `format`: `'RGB'`
* - `tensorLayout`: `'NCHW'`
* @returns a DataURL string representing the image converted from tensor data
*/
toDataURL(options?: TensorToDataUrlOptions): string;
/**
* creates an ImageData instance from tensor
*
* @param options - An optional object representing options for creating an ImageData instance from the tensor.
*
* The following default settings will be applied:
* - `format`: `'RGB'`
* - `tensorLayout`: `'NCHW'`
* @returns an ImageData instance representing the image converted from tensor data
*/
toImageData(options?: TensorToImageDataOptions): ImageData;
}
+328
View File
@@ -0,0 +1,328 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import {
OptionsDimensions,
OptionsFormat,
OptionsNormalizationParameters,
OptionsTensorFormat,
OptionsTensorLayout,
TensorFromGpuBufferOptions,
TensorFromImageBitmapOptions,
TensorFromImageDataOptions,
TensorFromImageElementOptions,
TensorFromMLTensorOptions,
TensorFromTextureOptions,
TensorFromUrlOptions,
} from './tensor-factory.js';
import { Tensor } from './tensor-impl.js';
import { Tensor as TensorInterface } from './tensor.js';
interface BufferToTensorOptions
extends OptionsDimensions, OptionsTensorLayout, OptionsNormalizationParameters, OptionsFormat, OptionsTensorFormat {}
/**
* Create a new tensor object from image object
*
* @param buffer - Extracted image buffer data - assuming RGBA format
* @param imageFormat - input image configuration - required configurations height, width, format
* @param tensorFormat - output tensor configuration - Default is RGB format
*/
export const bufferToTensor = (buffer: Uint8ClampedArray | undefined, options: BufferToTensorOptions): Tensor => {
if (buffer === undefined) {
throw new Error('Image buffer must be defined');
}
if (options.height === undefined || options.width === undefined) {
throw new Error('Image height and width must be defined');
}
if (options.tensorLayout === 'NHWC') {
throw new Error('NHWC Tensor layout is not supported yet');
}
const { height, width } = options;
const norm = options.norm ?? { mean: 255, bias: 0 };
let normMean: [number, number, number, number];
let normBias: [number, number, number, number];
if (typeof norm.mean === 'number') {
normMean = [norm.mean, norm.mean, norm.mean, norm.mean];
} else {
normMean = [norm.mean![0], norm.mean![1], norm.mean![2], norm.mean![3] ?? 255];
}
if (typeof norm.bias === 'number') {
normBias = [norm.bias, norm.bias, norm.bias, norm.bias];
} else {
normBias = [norm.bias![0], norm.bias![1], norm.bias![2], norm.bias![3] ?? 0];
}
const inputformat = options.format !== undefined ? options.format : 'RGBA';
// default value is RGBA since imagedata and HTMLImageElement uses it
const outputformat =
options.tensorFormat !== undefined ? (options.tensorFormat !== undefined ? options.tensorFormat : 'RGB') : 'RGB';
const stride = height * width;
const float32Data = outputformat === 'RGBA' ? new Float32Array(stride * 4) : new Float32Array(stride * 3);
// Default pointer assignments
let step = 4,
rImagePointer = 0,
gImagePointer = 1,
bImagePointer = 2,
aImagePointer = 3;
let rTensorPointer = 0,
gTensorPointer = stride,
bTensorPointer = stride * 2,
aTensorPointer = -1;
// Updating the pointer assignments based on the input image format
if (inputformat === 'RGB') {
step = 3;
rImagePointer = 0;
gImagePointer = 1;
bImagePointer = 2;
aImagePointer = -1;
}
// Updating the pointer assignments based on the output tensor format
if (outputformat === 'RGBA') {
aTensorPointer = stride * 3;
} else if (outputformat === 'RBG') {
rTensorPointer = 0;
bTensorPointer = stride;
gTensorPointer = stride * 2;
} else if (outputformat === 'BGR') {
bTensorPointer = 0;
gTensorPointer = stride;
rTensorPointer = stride * 2;
}
for (
let i = 0;
i < stride;
i++, rImagePointer += step, bImagePointer += step, gImagePointer += step, aImagePointer += step
) {
float32Data[rTensorPointer++] = (buffer[rImagePointer] + normBias[0]) / normMean[0];
float32Data[gTensorPointer++] = (buffer[gImagePointer] + normBias[1]) / normMean[1];
float32Data[bTensorPointer++] = (buffer[bImagePointer] + normBias[2]) / normMean[2];
if (aTensorPointer !== -1 && aImagePointer !== -1) {
float32Data[aTensorPointer++] = (buffer[aImagePointer] + normBias[3]) / normMean[3];
}
}
// Float32Array -> ort.Tensor
const outputTensor =
outputformat === 'RGBA'
? new Tensor('float32', float32Data, [1, 4, height, width])
: new Tensor('float32', float32Data, [1, 3, height, width]);
return outputTensor;
};
/**
* implementation of Tensor.fromImage().
*/
export const tensorFromImage = async (
image: ImageData | HTMLImageElement | ImageBitmap | string,
options?:
| TensorFromImageDataOptions
| TensorFromImageElementOptions
| TensorFromImageBitmapOptions
| TensorFromUrlOptions,
): Promise<Tensor> => {
// checking the type of image object
const isHTMLImageEle = typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement;
const isImageDataEle = typeof ImageData !== 'undefined' && image instanceof ImageData;
const isImageBitmap = typeof ImageBitmap !== 'undefined' && image instanceof ImageBitmap;
const isString = typeof image === 'string';
let data: Uint8ClampedArray | undefined;
let bufferToTensorOptions: BufferToTensorOptions = options ?? {};
const createCanvas = () => {
if (typeof document !== 'undefined') {
return document.createElement('canvas');
} else if (typeof OffscreenCanvas !== 'undefined') {
return new OffscreenCanvas(1, 1);
} else {
throw new Error('Canvas is not supported');
}
};
const createCanvasContext = (canvas: HTMLCanvasElement | OffscreenCanvas) => {
if (typeof HTMLCanvasElement !== 'undefined' && canvas instanceof HTMLCanvasElement) {
return canvas.getContext('2d');
} else if (canvas instanceof OffscreenCanvas) {
return canvas.getContext('2d') as OffscreenCanvasRenderingContext2D;
} else {
return null;
}
};
// filling and checking image configuration options
if (isHTMLImageEle) {
// HTMLImageElement - image object - format is RGBA by default
const canvas = createCanvas();
canvas.width = image.width;
canvas.height = image.height;
const pixels2DContext = createCanvasContext(canvas);
if (pixels2DContext != null) {
let height = image.height;
let width = image.width;
if (options !== undefined && options.resizedHeight !== undefined && options.resizedWidth !== undefined) {
height = options.resizedHeight;
width = options.resizedWidth;
}
if (options !== undefined) {
bufferToTensorOptions = options;
if (options.tensorFormat !== undefined) {
throw new Error('Image input config format must be RGBA for HTMLImageElement');
} else {
bufferToTensorOptions.tensorFormat = 'RGBA';
}
bufferToTensorOptions.height = height;
bufferToTensorOptions.width = width;
} else {
bufferToTensorOptions.tensorFormat = 'RGBA';
bufferToTensorOptions.height = height;
bufferToTensorOptions.width = width;
}
pixels2DContext.drawImage(image, 0, 0);
data = pixels2DContext.getImageData(0, 0, width, height).data;
} else {
throw new Error('Can not access image data');
}
} else if (isImageDataEle) {
let height: number;
let width: number;
if (options !== undefined && options.resizedWidth !== undefined && options.resizedHeight !== undefined) {
height = options.resizedHeight;
width = options.resizedWidth;
} else {
height = image.height;
width = image.width;
}
if (options !== undefined) {
bufferToTensorOptions = options;
}
bufferToTensorOptions.format = 'RGBA';
bufferToTensorOptions.height = height;
bufferToTensorOptions.width = width;
if (options !== undefined) {
const tempCanvas = createCanvas();
tempCanvas.width = width;
tempCanvas.height = height;
const pixels2DContext = createCanvasContext(tempCanvas);
if (pixels2DContext != null) {
pixels2DContext.putImageData(image, 0, 0);
data = pixels2DContext.getImageData(0, 0, width, height).data;
} else {
throw new Error('Can not access image data');
}
} else {
data = image.data;
}
} else if (isImageBitmap) {
// ImageBitmap - image object - format must be provided by user
if (options === undefined) {
throw new Error('Please provide image config with format for Imagebitmap');
}
const canvas = createCanvas();
canvas.width = image.width;
canvas.height = image.height;
const pixels2DContext = createCanvasContext(canvas);
if (pixels2DContext != null) {
const height = image.height;
const width = image.width;
pixels2DContext.drawImage(image, 0, 0, width, height);
data = pixels2DContext.getImageData(0, 0, width, height).data;
bufferToTensorOptions.height = height;
bufferToTensorOptions.width = width;
return bufferToTensor(data, bufferToTensorOptions);
} else {
throw new Error('Can not access image data');
}
} else if (isString) {
return new Promise((resolve, reject) => {
const canvas = createCanvas();
const context = createCanvasContext(canvas);
if (!image || !context) {
return reject();
}
const newImage = new Image();
newImage.crossOrigin = 'Anonymous';
newImage.src = image;
newImage.onload = () => {
canvas.width = newImage.width;
canvas.height = newImage.height;
context.drawImage(newImage, 0, 0, canvas.width, canvas.height);
const img = context.getImageData(0, 0, canvas.width, canvas.height);
bufferToTensorOptions.height = canvas.height;
bufferToTensorOptions.width = canvas.width;
resolve(bufferToTensor(img.data, bufferToTensorOptions));
};
});
} else {
throw new Error('Input data provided is not supported - aborted tensor creation');
}
if (data !== undefined) {
return bufferToTensor(data, bufferToTensorOptions);
} else {
throw new Error('Input data provided is not supported - aborted tensor creation');
}
};
/**
* implementation of Tensor.fromTexture().
*/
export const tensorFromTexture = <T extends TensorInterface.TextureDataTypes>(
texture: TensorInterface.TextureType,
options: TensorFromTextureOptions<T>,
): Tensor => {
const { width, height, download, dispose } = options;
// Always assume RGBAF32. TODO: support different texture format
const dims = [1, height, width, 4];
return new Tensor({ location: 'texture', type: 'float32', texture, dims, download, dispose });
};
/**
* implementation of Tensor.fromGpuBuffer().
*/
export const tensorFromGpuBuffer = <T extends TensorInterface.GpuBufferDataTypes>(
gpuBuffer: TensorInterface.GpuBufferType,
options: TensorFromGpuBufferOptions<T>,
): Tensor => {
const { dataType, dims, download, dispose } = options;
return new Tensor({ location: 'gpu-buffer', type: dataType ?? 'float32', gpuBuffer, dims, download, dispose });
};
/**
* implementation of Tensor.fromMLTensor().
*/
export const tensorFromMLTensor = <T extends TensorInterface.MLTensorDataTypes>(
mlTensor: TensorInterface.MLTensorType,
options: TensorFromMLTensorOptions<T>,
): Tensor => {
const { dataType, dims, download, dispose } = options;
return new Tensor({ location: 'ml-tensor', type: dataType ?? 'float32', mlTensor, dims, download, dispose });
};
/**
* implementation of Tensor.fromPinnedBuffer().
*/
export const tensorFromPinnedBuffer = <T extends TensorInterface.CpuPinnedDataTypes>(
type: T,
buffer: TensorInterface.DataTypeMap[T],
dims?: readonly number[],
): Tensor => new Tensor({ location: 'cpu-pinned', type, data: buffer, dims: dims ?? [buffer.length] });
+397
View File
@@ -0,0 +1,397 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { Tensor, TypedTensor } from './tensor.js';
export type ImageFormat = 'RGB' | 'RGBA' | 'BGR' | 'RBG';
export type ImageTensorLayout = 'NHWC' | 'NCHW';
// the following region contains type definitions for constructing tensor from a specific location.
// #region types for constructing a tensor from a specific location
/**
* represent common properties of the parameter for constructing a tensor from a specific location.
*/
interface CommonConstructorParameters<T> extends Pick<Tensor, 'dims'> {
/**
* Specify the data type of the tensor.
*/
readonly type: T;
}
/**
* represent the parameter for constructing a tensor from a GPU resource.
*/
interface GpuResourceConstructorParameters<T extends Tensor.Type> {
/**
* an optional callback function to download data from GPU to CPU.
*
* If not provided, the tensor treat the GPU data as external resource.
*/
download?(): Promise<Tensor.DataTypeMap[T]>;
/**
* an optional callback function that will be called when the tensor is disposed.
*
* If not provided, the tensor treat the GPU data as external resource.
*/
dispose?(): void;
}
/**
* represent the parameter for constructing a tensor from a pinned CPU buffer
*/
export interface CpuPinnedConstructorParameters<
T extends Tensor.CpuPinnedDataTypes = Tensor.CpuPinnedDataTypes,
> extends CommonConstructorParameters<T> {
/**
* Specify the location of the data to be 'cpu-pinned'.
*/
readonly location: 'cpu-pinned';
/**
* Specify the CPU pinned buffer that holds the tensor data.
*/
readonly data: Tensor.DataTypeMap[T];
}
/**
* represent the parameter for constructing a tensor from a WebGL texture
*/
export interface TextureConstructorParameters<T extends Tensor.TextureDataTypes = Tensor.TextureDataTypes>
extends CommonConstructorParameters<T>, GpuResourceConstructorParameters<T> {
/**
* Specify the location of the data to be 'texture'.
*/
readonly location: 'texture';
/**
* Specify the WebGL texture that holds the tensor data.
*/
readonly texture: Tensor.TextureType;
}
/**
* represent the parameter for constructing a tensor from a WebGPU buffer
*/
export interface GpuBufferConstructorParameters<T extends Tensor.GpuBufferDataTypes = Tensor.GpuBufferDataTypes>
extends CommonConstructorParameters<T>, GpuResourceConstructorParameters<T> {
/**
* Specify the location of the data to be 'gpu-buffer'.
*/
readonly location: 'gpu-buffer';
/**
* Specify the WebGPU buffer that holds the tensor data.
*/
readonly gpuBuffer: Tensor.GpuBufferType;
}
export interface MLTensorConstructorParameters<T extends Tensor.MLTensorDataTypes = Tensor.MLTensorDataTypes>
extends CommonConstructorParameters<T>, GpuResourceConstructorParameters<T> {
/**
* Specify the location of the data to be 'ml-tensor'.
*/
readonly location: 'ml-tensor';
/**
* Specify the WebNN MLTensor that holds the tensor data.
*/
readonly mlTensor: Tensor.MLTensorType;
}
// #endregion
// the following region contains type definitions of each individual options.
// the tensor factory functions use a composition of those options as the parameter type.
// #region Options fields
export interface OptionsFormat {
/**
* Describes the image format represented in RGBA color space.
*/
format?: ImageFormat;
}
export interface OptionsTensorFormat {
/**
* Describes the image format of the tensor.
*
* NOTE: this is different from option 'format'. While option 'format' represents the original image, 'tensorFormat'
* represents the target format of the tensor. A transpose will be performed if they are different.
*/
tensorFormat?: ImageFormat;
}
export interface OptionsTensorDataType {
/**
* Describes the data type of the tensor.
*/
dataType?: 'float32' | 'uint8';
}
export interface OptionsTensorLayout {
/**
* Describes the tensor layout when representing data of one or more image(s).
*/
tensorLayout?: ImageTensorLayout;
}
export interface OptionsDimensions {
/**
* Describes the image height in pixel
*/
height?: number;
/**
* Describes the image width in pixel
*/
width?: number;
}
export interface OptionResizedDimensions {
/**
* Describes the resized height. If omitted, original height will be used.
*/
resizedHeight?: number;
/**
* Describes resized width - can be accessed via tensor dimensions as well
*/
resizedWidth?: number;
}
export interface OptionsNormalizationParameters {
/**
* Describes normalization parameters when preprocessing the image as model input.
*
* Data element are ranged from 0 to 255.
*/
norm?: {
/**
* The 'bias' value for image normalization.
* - If omitted, use default value 0.
* - If it's a single number, apply to each channel
* - If it's an array of 3 or 4 numbers, apply element-wise. Number of elements need to match the number of channels
* for the corresponding image format
*/
bias?: number | [number, number, number] | [number, number, number, number];
/**
* The 'mean' value for image normalization.
* - If omitted, use default value 255.
* - If it's a single number, apply to each channel
* - If it's an array of 3 or 4 numbers, apply element-wise. Number of elements need to match the number of channels
* for the corresponding image format
*/
mean?: number | [number, number, number] | [number, number, number, number];
};
}
// #endregion
// #region Options composition
export interface TensorFromImageDataOptions
extends
OptionResizedDimensions,
OptionsTensorFormat,
OptionsTensorLayout,
OptionsTensorDataType,
OptionsNormalizationParameters {}
export interface TensorFromImageElementOptions
extends
OptionResizedDimensions,
OptionsTensorFormat,
OptionsTensorLayout,
OptionsTensorDataType,
OptionsNormalizationParameters {}
export interface TensorFromUrlOptions
extends
OptionsDimensions,
OptionResizedDimensions,
OptionsTensorFormat,
OptionsTensorLayout,
OptionsTensorDataType,
OptionsNormalizationParameters {}
export interface TensorFromImageBitmapOptions
extends
OptionResizedDimensions,
OptionsTensorFormat,
OptionsTensorLayout,
OptionsTensorDataType,
OptionsNormalizationParameters {}
export interface TensorFromTextureOptions<T extends Tensor.TextureDataTypes>
extends Required<OptionsDimensions>, OptionsFormat, GpuResourceConstructorParameters<T> /* TODO: add more */ {}
export interface TensorFromGpuBufferOptions<T extends Tensor.GpuBufferDataTypes>
extends Pick<Tensor, 'dims'>, GpuResourceConstructorParameters<T> {
/**
* Describes the data type of the tensor.
*/
dataType?: T;
}
export interface TensorFromMLTensorOptions<T extends Tensor.MLTensorDataTypes>
extends Pick<Tensor, 'dims'>, GpuResourceConstructorParameters<T> {
/**
* Describes the data type of the tensor.
*/
dataType?: T;
}
// #endregion
/**
* type TensorFactory defines the factory functions of 'Tensor' to create tensor instances from existing data or
* resources.
*/
export interface TensorFactory {
/**
* create a tensor from an ImageData object
*
* @param imageData - the ImageData object to create tensor from
* @param options - An optional object representing options for creating tensor from ImageData.
*
* The following default settings will be applied:
* - `tensorFormat`: `'RGB'`
* - `tensorLayout`: `'NCHW'`
* - `dataType`: `'float32'`
* @returns A promise that resolves to a tensor object
*/
fromImage(
imageData: ImageData,
options?: TensorFromImageDataOptions,
): Promise<TypedTensor<'float32'> | TypedTensor<'uint8'>>;
/**
* create a tensor from a HTMLImageElement object
*
* @param imageElement - the HTMLImageElement object to create tensor from
* @param options - An optional object representing options for creating tensor from HTMLImageElement.
*
* The following default settings will be applied:
* - `tensorFormat`: `'RGB'`
* - `tensorLayout`: `'NCHW'`
* - `dataType`: `'float32'`
* @returns A promise that resolves to a tensor object
*/
fromImage(
imageElement: HTMLImageElement,
options?: TensorFromImageElementOptions,
): Promise<TypedTensor<'float32'> | TypedTensor<'uint8'>>;
/**
* create a tensor from URL
*
* @param urlSource - a string as a URL to the image or a data URL containing the image data.
* @param options - An optional object representing options for creating tensor from URL.
*
* The following default settings will be applied:
* - `tensorFormat`: `'RGB'`
* - `tensorLayout`: `'NCHW'`
* - `dataType`: `'float32'`
* @returns A promise that resolves to a tensor object
*/
fromImage(urlSource: string, options?: TensorFromUrlOptions): Promise<TypedTensor<'float32'> | TypedTensor<'uint8'>>;
/**
* create a tensor from an ImageBitmap object
*
* @param bitmap - the ImageBitmap object to create tensor from
* @param options - An optional object representing options for creating tensor from URL.
*
* The following default settings will be applied:
* - `tensorFormat`: `'RGB'`
* - `tensorLayout`: `'NCHW'`
* - `dataType`: `'float32'`
* @returns A promise that resolves to a tensor object
*/
fromImage(
bitmap: ImageBitmap,
options: TensorFromImageBitmapOptions,
): Promise<TypedTensor<'float32'> | TypedTensor<'uint8'>>;
/**
* create a tensor from a WebGL texture
*
* @param texture - the WebGLTexture object to create tensor from
* @param options - An optional object representing options for creating tensor from WebGL texture.
*
* The options include following properties:
* - `width`: the width of the texture. Required.
* - `height`: the height of the texture. Required.
* - `format`: the format of the texture. If omitted, assume 'RGBA'.
* - `download`: an optional function to download the tensor data from GPU to CPU. If omitted, the GPU data
* will not be able to download. Usually, this is provided by a GPU backend for the inference outputs. Users don't
* need to provide this function.
* - `dispose`: an optional function to dispose the tensor data on GPU. If omitted, the GPU data will not be disposed.
* Usually, this is provided by a GPU backend for the inference outputs. Users don't need to provide this function.
*
* @returns a tensor object
*/
fromTexture<T extends Tensor.TextureDataTypes = 'float32'>(
texture: Tensor.TextureType,
options: TensorFromTextureOptions<T>,
): TypedTensor<'float32'>;
/**
* create a tensor from a WebGPU buffer
*
* @param buffer - the GPUBuffer object to create tensor from
* @param options - An optional object representing options for creating tensor from WebGPU buffer.
*
* The options include following properties:
* - `dataType`: the data type of the tensor. If omitted, assume 'float32'.
* - `dims`: the dimension of the tensor. Required.
* - `download`: an optional function to download the tensor data from GPU to CPU. If omitted, the GPU data
* will not be able to download. Usually, this is provided by a GPU backend for the inference outputs. Users don't
* need to provide this function.
* - `dispose`: an optional function to dispose the tensor data on GPU. If omitted, the GPU data will not be disposed.
* Usually, this is provided by a GPU backend for the inference outputs. Users don't need to provide this function.
*
* @returns a tensor object
*/
fromGpuBuffer<T extends Tensor.GpuBufferDataTypes>(
buffer: Tensor.GpuBufferType,
options: TensorFromGpuBufferOptions<T>,
): TypedTensor<T>;
/**
* create a tensor from a WebNN MLTensor
*
* @param tensor - the MLTensor object to create tensor from
* @param options - An optional object representing options for creating tensor from a WebNN MLTensor.
*
* The options include following properties:
* - `dataType`: the data type of the tensor. If omitted, assume 'float32'.
* - `dims`: the dimension of the tensor. Required.
* - `download`: an optional function to download the tensor data from the MLTensor to CPU. If omitted, the MLTensor
* data will not be able to download. Usually, this is provided by the WebNN backend for the inference outputs.
* Users don't need to provide this function.
* - `dispose`: an optional function to dispose the tensor data on the WebNN MLTensor. If omitted, the MLTensor will
* not be disposed. Usually, this is provided by the WebNN backend for the inference outputs. Users don't need to
* provide this function.
*
* @returns a tensor object
*/
fromMLTensor<T extends Tensor.MLTensorDataTypes>(
tensor: Tensor.MLTensorType,
options: TensorFromMLTensorOptions<T>,
): TypedTensor<T>;
/**
* create a tensor from a pre-allocated buffer. The buffer will be used as a pinned buffer.
*
* @param type - the tensor element type.
* @param buffer - a TypedArray corresponding to the type.
* @param dims - specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*
* @returns a tensor object
*/
fromPinnedBuffer<T extends Exclude<Tensor.Type, 'string'>>(
type: T,
buffer: Tensor.DataTypeMap[T],
dims?: readonly number[],
): TypedTensor<T>;
}
@@ -0,0 +1,77 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { Tensor } from './tensor.js';
export type SupportedTypedArrayConstructors =
| Float32ArrayConstructor
| Uint8ArrayConstructor
| Int8ArrayConstructor
| Uint16ArrayConstructor
| Int16ArrayConstructor
| Int32ArrayConstructor
| BigInt64ArrayConstructor
| Uint8ArrayConstructor
| Float64ArrayConstructor
| Uint32ArrayConstructor
| BigUint64ArrayConstructor;
export type SupportedTypedArray = InstanceType<SupportedTypedArrayConstructors>;
// a runtime map that maps type string to TypedArray constructor. Should match Tensor.DataTypeMap.
export const NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP = new Map<string, SupportedTypedArrayConstructors>([
['float32', Float32Array],
['uint8', Uint8Array],
['int8', Int8Array],
['uint16', Uint16Array],
['int16', Int16Array],
['int32', Int32Array],
['bool', Uint8Array],
['float64', Float64Array],
['uint32', Uint32Array],
['int4', Uint8Array],
['uint4', Uint8Array],
]);
// a runtime map that maps type string to TypedArray constructor. Should match Tensor.DataTypeMap.
export const NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP = new Map<SupportedTypedArrayConstructors, Tensor.Type>([
[Float32Array, 'float32'],
[Uint8Array, 'uint8'],
[Int8Array, 'int8'],
[Uint16Array, 'uint16'],
[Int16Array, 'int16'],
[Int32Array, 'int32'],
[Float64Array, 'float64'],
[Uint32Array, 'uint32'],
]);
// the following code allows delaying execution of BigInt/Float16Array checking. This allows lazy initialization for
// NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP and NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP, which allows BigInt/Float16Array
// polyfill if available.
let isTypedArrayChecked = false;
export const checkTypedArray = () => {
if (!isTypedArrayChecked) {
isTypedArrayChecked = true;
const isBigInt64ArrayAvailable = typeof BigInt64Array !== 'undefined' && BigInt64Array.from;
const isBigUint64ArrayAvailable = typeof BigUint64Array !== 'undefined' && BigUint64Array.from;
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-explicit-any
const Float16Array = (globalThis as any).Float16Array;
const isFloat16ArrayAvailable = typeof Float16Array !== 'undefined' && Float16Array.from;
if (isBigInt64ArrayAvailable) {
NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set('int64', BigInt64Array);
NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.set(BigInt64Array, 'int64');
}
if (isBigUint64ArrayAvailable) {
NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set('uint64', BigUint64Array);
NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.set(BigUint64Array, 'uint64');
}
if (isFloat16ArrayAvailable) {
NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set('float16', Float16Array);
NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.set(Float16Array, 'float16');
} else {
// if Float16Array is not available, use 'Uint16Array' to store the data.
NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.set('float16', Uint16Array);
}
}
};
+562
View File
@@ -0,0 +1,562 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { tensorToDataURL, tensorToImageData } from './tensor-conversion-impl.js';
import { TensorToDataUrlOptions, TensorToImageDataOptions } from './tensor-conversion.js';
import {
tensorFromGpuBuffer,
tensorFromImage,
tensorFromMLTensor,
tensorFromPinnedBuffer,
tensorFromTexture,
} from './tensor-factory-impl.js';
import {
CpuPinnedConstructorParameters,
GpuBufferConstructorParameters,
MLTensorConstructorParameters,
TensorFromGpuBufferOptions,
TensorFromImageBitmapOptions,
TensorFromImageDataOptions,
TensorFromImageElementOptions,
TensorFromMLTensorOptions,
TensorFromTextureOptions,
TensorFromUrlOptions,
TextureConstructorParameters,
} from './tensor-factory.js';
import {
checkTypedArray,
NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP,
NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP,
SupportedTypedArray,
SupportedTypedArrayConstructors,
} from './tensor-impl-type-mapping.js';
import { calculateSize, tensorReshape } from './tensor-utils-impl.js';
import { Tensor as TensorInterface } from './tensor.js';
// type aliases for those exported from Tensor interface
type TensorType = TensorInterface.Type;
type TensorDataType = TensorInterface.DataType;
type TensorDataLocation = TensorInterface.DataLocation;
type TensorTextureType = TensorInterface.TextureType;
type TensorGpuBufferType = TensorInterface.GpuBufferType;
type TensorMLTensorType = TensorInterface.MLTensorType;
/**
* the implementation of Tensor interface.
*
* @ignore
*/
export class Tensor implements TensorInterface {
// #region constructors
/**
* Construct a new CPU tensor object from the given type, data and dims.
*/
constructor(
type: TensorType,
data: TensorDataType | Uint8ClampedArray | readonly string[] | readonly number[] | readonly boolean[],
dims?: readonly number[],
);
/**
* Construct a new CPU tensor object from the given data and dims. Type is inferred from data.
*/
constructor(
data: TensorDataType | Uint8ClampedArray | readonly string[] | readonly boolean[],
dims?: readonly number[],
);
/**
* Construct a new tensor object from the pinned CPU data with the given type and dims.
*
* Tensor's location will be set to 'cpu-pinned'.
*
* @param params - Specify the parameters to construct the tensor.
*/
constructor(params: CpuPinnedConstructorParameters);
/**
* Construct a new tensor object from the WebGL texture with the given type and dims.
*
* Tensor's location will be set to 'texture'.
*
* @param params - Specify the parameters to construct the tensor.
*/
constructor(params: TextureConstructorParameters);
/**
* Construct a new tensor object from the WebGPU buffer with the given type and dims.
*
* Tensor's location will be set to 'gpu-buffer'.
*
* @param params - Specify the parameters to construct the tensor.
*/
constructor(params: GpuBufferConstructorParameters);
/**
* Construct a new tensor object from the WebNN MLTensor with the given type and dims.
*
* Tensor's location will be set to 'ml-tensor'.
*
* @param params - Specify the parameters to construct the tensor.
*/
constructor(params: MLTensorConstructorParameters);
/**
* implementation.
*/
constructor(
arg0:
| TensorType
| TensorDataType
| Uint8ClampedArray
| readonly string[]
| readonly boolean[]
| CpuPinnedConstructorParameters
| TextureConstructorParameters
| GpuBufferConstructorParameters
| MLTensorConstructorParameters,
arg1?: TensorDataType | Uint8ClampedArray | readonly number[] | readonly string[] | readonly boolean[],
arg2?: readonly number[],
) {
// perform one-time check for BigInt/Float16Array support
checkTypedArray();
let type: TensorType;
let dims: readonly number[];
if (typeof arg0 === 'object' && 'location' in arg0) {
//
// constructing tensor from specific location
//
this.dataLocation = arg0.location;
type = arg0.type;
dims = arg0.dims;
switch (arg0.location) {
case 'cpu-pinned': {
const expectedTypedArrayConstructor = NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.get(type);
if (!expectedTypedArrayConstructor) {
throw new TypeError(`unsupported type "${type}" to create tensor from pinned buffer`);
}
if (!(arg0.data instanceof expectedTypedArrayConstructor)) {
throw new TypeError(`buffer should be of type ${expectedTypedArrayConstructor.name}`);
}
this.cpuData = arg0.data;
break;
}
case 'texture': {
if (type !== 'float32') {
throw new TypeError(`unsupported type "${type}" to create tensor from texture`);
}
this.gpuTextureData = arg0.texture;
this.downloader = arg0.download;
this.disposer = arg0.dispose;
break;
}
case 'gpu-buffer': {
if (
type !== 'float32' &&
type !== 'float16' &&
type !== 'int32' &&
type !== 'int64' &&
type !== 'uint32' &&
type !== 'uint8' &&
type !== 'bool' &&
type !== 'uint4' &&
type !== 'int4'
) {
throw new TypeError(`unsupported type "${type}" to create tensor from gpu buffer`);
}
this.gpuBufferData = arg0.gpuBuffer;
this.downloader = arg0.download;
this.disposer = arg0.dispose;
break;
}
case 'ml-tensor': {
if (
type !== 'float32' &&
type !== 'float16' &&
type !== 'int32' &&
type !== 'int64' &&
type !== 'uint32' &&
type !== 'uint64' &&
type !== 'int8' &&
type !== 'uint8' &&
type !== 'bool' &&
type !== 'uint4' &&
type !== 'int4'
) {
throw new TypeError(`unsupported type "${type}" to create tensor from MLTensor`);
}
this.mlTensorData = arg0.mlTensor;
this.downloader = arg0.download;
this.disposer = arg0.dispose;
break;
}
default:
throw new Error(`Tensor constructor: unsupported location '${this.dataLocation}'`);
}
} else {
//
// constructing tensor of location 'cpu'
//
let data: TensorDataType;
let maybeDims: typeof arg1 | typeof arg2;
// check whether arg0 is type or data
if (typeof arg0 === 'string') {
//
// Override: constructor(type, data, ...)
//
type = arg0;
maybeDims = arg2;
if (arg0 === 'string') {
// string tensor
if (!Array.isArray(arg1)) {
throw new TypeError("A string tensor's data must be a string array.");
}
// we don't check whether every element in the array is string; this is too slow. we assume it's correct and
// error will be populated at inference
data = arg1;
} else {
// numeric tensor
const typedArrayConstructor = NUMERIC_TENSOR_TYPE_TO_TYPEDARRAY_MAP.get(arg0);
if (typedArrayConstructor === undefined) {
throw new TypeError(`Unsupported tensor type: ${arg0}.`);
}
if (Array.isArray(arg1)) {
if ((arg0 === 'float16' && typedArrayConstructor === Uint16Array) || arg0 === 'uint4' || arg0 === 'int4') {
// - 'float16':
// When no Float16Array polyfill is used, we cannot create 'float16' tensor from number array.
//
// Throw error here because when user try to use number array as data,
// e.g. new Tensor('float16', [1, 2, 3, 4], dims)), it will actually call
// Uint16Array.from(arg1) which generates wrong data.
//
// - 'uint4' and 'int4':
// Uint8Array.from(arg1) will generate wrong data for 'uint4' and 'int4' tensor.
//
throw new TypeError(
`Creating a ${arg0} tensor from number array is not supported. Please use ${typedArrayConstructor.name} as data.`,
);
} else if (arg0 === 'uint64' || arg0 === 'int64') {
// use 'as any' here because:
// 1. TypeScript's check on type of 'Array.isArray()' does not work with readonly arrays.
// see https://github.com/microsoft/TypeScript/issues/17002
// 2. TypeScript's check on union type of '(BigInt64ArrayConstructor|BigUint64ArrayConstructor).from()'
// does not accept parameter mapFn.
// 3. parameters of 'SupportedTypedArrayConstructors.from()' does not match the requirement of the union
// type.
// assume 'arg1' is of type "readonly number[]|readonly bigint[]" here.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
data = (typedArrayConstructor as any).from(arg1, BigInt);
} else {
// assume 'arg1' is of type "readonly number[]" here.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
data = (typedArrayConstructor as any).from(arg1);
}
} else if (arg1 instanceof typedArrayConstructor) {
data = arg1;
} else if (arg1 instanceof Uint8ClampedArray) {
if (arg0 === 'uint8') {
data = Uint8Array.from(arg1);
} else {
throw new TypeError(`A Uint8ClampedArray tensor's data must be type of uint8`);
}
} else if (arg0 === 'float16' && arg1 instanceof Uint16Array && typedArrayConstructor !== Uint16Array) {
// when Float16Array is available and data is of type Uint16Array.
// We allow Uint16Array to be passed in as data for 'float16' tensor until Float16Array is generally
// supported in JavaScript environment.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
data = new (globalThis as any).Float16Array(arg1.buffer, arg1.byteOffset, arg1.length);
} else {
throw new TypeError(`A ${type} tensor's data must be type of ${typedArrayConstructor}`);
}
}
} else {
//
// Override: constructor(data, ...)
//
maybeDims = arg1;
if (Array.isArray(arg0)) {
// only boolean[] and string[] is supported
if (arg0.length === 0) {
throw new TypeError('Tensor type cannot be inferred from an empty array.');
}
const firstElementType = typeof arg0[0];
if (firstElementType === 'string') {
type = 'string';
data = arg0;
} else if (firstElementType === 'boolean') {
type = 'bool';
// 'arg0' is of type 'boolean[]'. Uint8Array.from(boolean[]) actually works, but typescript thinks this is
// wrong type. We use 'as any' to make it happy.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
data = Uint8Array.from(arg0 as any[]);
} else {
throw new TypeError(`Invalid element type of data array: ${firstElementType}.`);
}
} else if (arg0 instanceof Uint8ClampedArray) {
type = 'uint8';
data = Uint8Array.from(arg0);
} else {
// get tensor type from TypedArray
const mappedType = NUMERIC_TENSOR_TYPEDARRAY_TO_TYPE_MAP.get(
arg0.constructor as SupportedTypedArrayConstructors,
);
if (mappedType === undefined) {
throw new TypeError(`Unsupported type for tensor data: ${arg0.constructor}.`);
}
type = mappedType;
data = arg0 as SupportedTypedArray;
}
}
// type and data is processed, now processing dims
if (maybeDims === undefined) {
// assume 1-D tensor if dims omitted
maybeDims = [data.length];
} else if (!Array.isArray(maybeDims)) {
throw new TypeError("A tensor's dims must be a number array");
}
dims = maybeDims as readonly number[];
this.cpuData = data;
this.dataLocation = 'cpu';
}
// perform check on dims
const size = calculateSize(dims);
// if data is on CPU, check whether data length matches tensor size
if (this.cpuData && size !== this.cpuData.length) {
if ((type === 'uint4' || type === 'int4') && Math.ceil(size / 2) === this.cpuData.length) {
// for (u)int4, the data length is half of the tensor size. So we check this special case when size is odd.
} else {
throw new Error(`Tensor's size(${size}) does not match data length(${this.cpuData.length}).`);
}
}
this.type = type;
this.dims = dims;
this.size = size;
}
// #endregion
// #region factory
static async fromImage(
image: ImageData | HTMLImageElement | ImageBitmap | string,
options?:
| TensorFromImageDataOptions
| TensorFromImageElementOptions
| TensorFromImageBitmapOptions
| TensorFromUrlOptions,
): Promise<TensorInterface> {
return tensorFromImage(image, options);
}
static fromTexture<T extends TensorInterface.TextureDataTypes>(
texture: TensorTextureType,
options: TensorFromTextureOptions<T>,
): TensorInterface {
return tensorFromTexture(texture, options);
}
static fromGpuBuffer<T extends TensorInterface.GpuBufferDataTypes>(
gpuBuffer: TensorGpuBufferType,
options: TensorFromGpuBufferOptions<T>,
): TensorInterface {
return tensorFromGpuBuffer(gpuBuffer, options);
}
static fromMLTensor<T extends TensorInterface.MLTensorDataTypes>(
mlTensor: TensorMLTensorType,
options: TensorFromMLTensorOptions<T>,
): TensorInterface {
return tensorFromMLTensor(mlTensor, options);
}
static fromPinnedBuffer<T extends TensorInterface.CpuPinnedDataTypes>(
type: T,
buffer: TensorInterface.DataTypeMap[T],
dims?: readonly number[],
): Tensor {
return tensorFromPinnedBuffer(type, buffer, dims);
}
// #endregion
// #region conversions
toDataURL(options?: TensorToDataUrlOptions): string {
return tensorToDataURL(this, options);
}
toImageData(options?: TensorToImageDataOptions): ImageData {
return tensorToImageData(this, options);
}
// #endregion
// #region public fields
readonly dims: readonly number[];
readonly type: TensorType;
readonly size: number;
// #endregion
// #region private fields
/**
* stores the location of the data.
*/
private dataLocation: TensorDataLocation;
/**
* stores the data on CPU, if location is 'cpu' or 'cpu-pinned'. otherwise empty.
*/
private cpuData?: TensorDataType;
/**
* stores the underlying texture when location is 'texture'. otherwise empty.
*/
private gpuTextureData?: TensorTextureType;
/**
* stores the underlying GPU buffer when location is 'gpu-buffer'. otherwise empty.
*/
private gpuBufferData?: TensorGpuBufferType;
/**
* stores the underlying WebNN MLTensor when location is 'ml-tensor'. otherwise empty.
*/
private mlTensorData?: TensorMLTensorType;
/**
* stores an optional downloader function to download data from GPU to CPU.
*/
private downloader?(): Promise<TensorDataType>;
/**
* a flag indicating whether the data is being downloaded from GPU to CPU.
*/
private isDownloading?: boolean;
/**
* stores an optional disposer function to dispose the underlying data.
*/
private disposer?(): void;
// #endregion
// #region properties
get data(): TensorDataType {
this.ensureValid();
if (!this.cpuData) {
throw new Error(
'The data is not on CPU. Use `getData()` to download GPU data to CPU, ' +
'or use `texture` or `gpuBuffer` property to access the GPU data directly.',
);
}
return this.cpuData;
}
get location(): TensorDataLocation {
return this.dataLocation;
}
get texture(): TensorTextureType {
this.ensureValid();
if (!this.gpuTextureData) {
throw new Error('The data is not stored as a WebGL texture.');
}
return this.gpuTextureData;
}
get gpuBuffer(): TensorGpuBufferType {
this.ensureValid();
if (!this.gpuBufferData) {
throw new Error('The data is not stored as a WebGPU buffer.');
}
return this.gpuBufferData;
}
get mlTensor(): TensorMLTensorType {
this.ensureValid();
if (!this.mlTensorData) {
throw new Error('The data is not stored as a WebNN MLTensor.');
}
return this.mlTensorData;
}
// #endregion
// #region methods
async getData(releaseData?: boolean): Promise<TensorDataType> {
this.ensureValid();
switch (this.dataLocation) {
case 'cpu':
case 'cpu-pinned':
return this.data;
case 'texture':
case 'gpu-buffer':
case 'ml-tensor': {
if (!this.downloader) {
throw new Error('The current tensor is not created with a specified data downloader.');
}
if (this.isDownloading) {
throw new Error('The current tensor is being downloaded.');
}
try {
this.isDownloading = true;
const data = await this.downloader();
this.downloader = undefined;
this.dataLocation = 'cpu';
this.cpuData = data;
if (releaseData && this.disposer) {
this.disposer();
this.disposer = undefined;
}
return data;
} finally {
this.isDownloading = false;
}
}
default:
throw new Error(`cannot get data from location: ${this.dataLocation}`);
}
}
dispose(): void {
if (this.isDownloading) {
throw new Error('The current tensor is being downloaded.');
}
if (this.disposer) {
this.disposer();
this.disposer = undefined;
}
this.cpuData = undefined;
this.gpuTextureData = undefined;
this.gpuBufferData = undefined;
this.mlTensorData = undefined;
this.downloader = undefined;
this.isDownloading = undefined;
this.dataLocation = 'none';
}
// #endregion
// #region tensor utilities
private ensureValid(): void {
if (this.dataLocation === 'none') {
throw new Error('The tensor is disposed.');
}
}
reshape(dims: readonly number[]): TensorInterface {
this.ensureValid();
if (this.downloader || this.disposer) {
throw new Error('Cannot reshape a tensor that owns GPU resource.');
}
return tensorReshape(this, dims);
}
// #endregion
}
+70
View File
@@ -0,0 +1,70 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import {
CpuPinnedConstructorParameters,
GpuBufferConstructorParameters,
MLTensorConstructorParameters,
TextureConstructorParameters,
} from './tensor-factory.js';
import { Tensor } from './tensor-impl.js';
/**
* calculate size from dims.
*
* @param dims the dims array. May be an illegal input.
*/
export const calculateSize = (dims: readonly unknown[]): number => {
let size = 1;
for (let i = 0; i < dims.length; i++) {
const dim = dims[i];
if (typeof dim !== 'number' || !Number.isSafeInteger(dim)) {
throw new TypeError(`dims[${i}] must be an integer, got: ${dim}`);
}
if (dim < 0) {
throw new RangeError(`dims[${i}] must be a non-negative integer, got: ${dim}`);
}
size *= dim;
}
return size;
};
/**
* implementation of Tensor.reshape()
*/
export const tensorReshape = (tensor: Tensor, dims: readonly number[]): Tensor => {
switch (tensor.location) {
case 'cpu':
return new Tensor(tensor.type, tensor.data, dims);
case 'cpu-pinned':
return new Tensor({
location: 'cpu-pinned',
data: tensor.data as CpuPinnedConstructorParameters['data'],
type: tensor.type as CpuPinnedConstructorParameters['type'],
dims,
});
case 'texture':
return new Tensor({
location: 'texture',
texture: tensor.texture,
type: tensor.type as TextureConstructorParameters['type'],
dims,
});
case 'gpu-buffer':
return new Tensor({
location: 'gpu-buffer',
gpuBuffer: tensor.gpuBuffer,
type: tensor.type as GpuBufferConstructorParameters['type'],
dims,
});
case 'ml-tensor':
return new Tensor({
location: 'ml-tensor',
mlTensor: tensor.mlTensor,
type: tensor.type as MLTensorConstructorParameters['type'],
dims,
});
default:
throw new Error(`tensorReshape: tensor location ${tensor.location} is not supported`);
}
};
+31
View File
@@ -0,0 +1,31 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { ConversionUtils } from './tensor-conversion.js';
import { Tensor, TypedTensor } from './tensor.js';
interface Properties {
/**
* Get the number of elements in the tensor.
*/
readonly size: number;
}
export interface TypedShapeUtils<T extends Tensor.Type> {
/**
* Create a new tensor with the same data buffer and specified dims.
*
* @param dims - New dimensions. Size should match the old one.
*/
reshape(dims: readonly number[]): TypedTensor<T>;
}
/**
* interface `TensorUtils` includes all utility members that does not use the type parameter from their signature.
*/
export interface TensorUtils extends Properties, ConversionUtils {}
/**
* interface `TypedShapeUtils` includes all utility members that uses the type parameter from their signature.
*/
export interface TypedTensorUtils<T extends Tensor.Type> extends TensorUtils, TypedShapeUtils<T> {}
+391
View File
@@ -0,0 +1,391 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { TensorFactory } from './tensor-factory.js';
import { Tensor as TensorImpl } from './tensor-impl.js';
import { TypedTensorUtils } from './tensor-utils.js';
import { TryGetGlobalType } from './type-helper.js';
/* eslint-disable @typescript-eslint/no-redeclare */
/**
* represent a basic tensor with specified dimensions and data type.
*/
interface TypedTensorBase<T extends Tensor.Type> {
/**
* Get the dimensions of the tensor.
*/
readonly dims: readonly number[];
/**
* Get the data type of the tensor.
*/
readonly type: T;
/**
* Get the buffer data of the tensor.
*
* If the data is not on CPU (eg. it's in the form of WebGL texture or WebGPU buffer), throw error.
*/
readonly data: Tensor.DataTypeMap[T];
/**
* Get the location of the data.
*/
readonly location: Tensor.DataLocation;
/**
* Get the WebGL texture that holds the tensor data.
*
* If the data is not on GPU as WebGL texture, throw error.
*/
readonly texture: Tensor.TextureType;
/**
* Get the WebGPU buffer that holds the tensor data.
*
* If the data is not on GPU as WebGPU buffer, throw error.
*/
readonly gpuBuffer: Tensor.GpuBufferType;
/**
* Get the WebNN MLTensor that holds the tensor data.
*
* If the data is not in a WebNN MLTensor, throw error.
*/
readonly mlTensor: Tensor.MLTensorType;
/**
* Get the buffer data of the tensor.
*
* If the data is on CPU, returns the data immediately.
* If the data is on GPU, downloads the data and returns the promise.
*
* @param releaseData - whether release the data on GPU. Ignore if data is already on CPU.
*/
getData(releaseData?: boolean): Promise<Tensor.DataTypeMap[T]>;
/**
* Dispose the tensor data.
*
* If the data is on CPU, remove its internal reference to the underlying data.
* If the data is on GPU, release the data on GPU.
*
* After calling this function, the tensor is considered no longer valid. Its location will be set to 'none'.
*/
dispose(): void;
}
export declare namespace Tensor {
interface DataTypeMap {
float32: Float32Array;
uint8: Uint8Array;
int8: Int8Array;
uint16: Uint16Array;
int16: Int16Array;
int32: Int32Array;
int64: BigInt64Array;
string: string[];
bool: Uint8Array;
float16: Uint16Array; // Keep using Uint16Array until we have a concrete solution for float 16.
float64: Float64Array;
uint32: Uint32Array;
uint64: BigUint64Array;
// complex64: never;
// complex128: never;
// bfloat16: never;
uint4: Uint8Array;
int4: Int8Array;
}
interface ElementTypeMap {
float32: number;
uint8: number;
int8: number;
uint16: number;
int16: number;
int32: number;
int64: bigint;
string: string;
bool: boolean;
float16: number; // Keep using Uint16Array until we have a concrete solution for float 16.
float64: number;
uint32: number;
uint64: bigint;
// complex64: never;
// complex128: never;
// bfloat16: never;
uint4: number;
int4: number;
}
type DataType = DataTypeMap[Type];
type ElementType = ElementTypeMap[Type];
/**
* supported data types for constructing a tensor from a pinned CPU buffer
*/
export type CpuPinnedDataTypes = Exclude<Tensor.Type, 'string'>;
/**
* type alias for WebGL texture
*/
export type TextureType = WebGLTexture;
/**
* supported data types for constructing a tensor from a WebGL texture
*/
export type TextureDataTypes = 'float32';
type GpuBufferTypeFallback = { size: number; mapState: 'unmapped' | 'pending' | 'mapped' };
/**
* type alias for WebGPU buffer
*/
export type GpuBufferType = TryGetGlobalType<'GPUBuffer', GpuBufferTypeFallback>;
type MLTensorTypeFallback = { destroy(): void };
/**
* type alias for WebNN MLTensor
*
* The specification for WebNN's MLTensor is currently in flux.
*/
export type MLTensorType = TryGetGlobalType<'MLTensor', MLTensorTypeFallback>;
/**
* supported data types for constructing a tensor from a WebGPU buffer
*/
export type GpuBufferDataTypes = 'float32' | 'float16' | 'int32' | 'int64' | 'uint32' | 'uint8' | 'bool';
/**
* supported data types for constructing a tensor from a WebNN MLTensor
*/
export type MLTensorDataTypes =
| 'float32'
| 'float16'
| 'int8'
| 'uint8'
| 'int32'
| 'uint32'
| 'int64'
| 'uint64'
| 'bool'
| 'uint4'
| 'int4';
/**
* represent where the tensor data is stored
*/
export type DataLocation = 'none' | 'cpu' | 'cpu-pinned' | 'texture' | 'gpu-buffer' | 'ml-tensor';
/**
* represent the data type of a tensor
*/
export type Type = keyof DataTypeMap;
}
/**
* Represent multi-dimensional arrays to feed to or fetch from model inferencing.
*/
export interface TypedTensor<T extends Tensor.Type> extends TypedTensorBase<T>, TypedTensorUtils<T> {}
/**
* Represent multi-dimensional arrays to feed to or fetch from model inferencing.
*/
export interface Tensor extends TypedTensorBase<Tensor.Type>, TypedTensorUtils<Tensor.Type> {}
/**
* type TensorConstructor defines the constructors of 'Tensor' to create CPU tensor instances.
*/
export interface TensorConstructor extends TensorFactory {
// #region CPU tensor - specify element type
/**
* Construct a new string tensor object from the given type, data and dims.
*
* @param type - Specify the element type.
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (
type: 'string',
data: Tensor.DataTypeMap['string'] | readonly string[],
dims?: readonly number[],
): TypedTensor<'string'>;
/**
* Construct a new bool tensor object from the given type, data and dims.
*
* @param type - Specify the element type.
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (
type: 'bool',
data: Tensor.DataTypeMap['bool'] | readonly boolean[],
dims?: readonly number[],
): TypedTensor<'bool'>;
/**
* Construct a new uint8 tensor object from a Uint8ClampedArray, data and dims.
*
* @param type - Specify the element type.
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (type: 'uint8', data: Uint8ClampedArray, dims?: readonly number[]): TypedTensor<'uint8'>;
/**
* Construct a new 64-bit integer typed tensor object from the given type, data and dims.
*
* @param type - Specify the element type.
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new <T extends 'uint64' | 'int64'>(
type: T,
data: Tensor.DataTypeMap[T] | readonly bigint[] | readonly number[],
dims?: readonly number[],
): TypedTensor<T>;
/**
* Construct a new numeric tensor object from the given type, data and dims.
*
* @param type - Specify the element type.
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new <T extends Exclude<Tensor.Type, 'string' | 'bool' | 'uint64' | 'int64'>>(
type: T,
data: Tensor.DataTypeMap[T] | readonly number[],
dims?: readonly number[],
): TypedTensor<T>;
// #endregion
// #region CPU tensor - infer element types
/**
* Construct a new float32 tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: Float32Array, dims?: readonly number[]): TypedTensor<'float32'>;
/**
* Construct a new int8 tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: Int8Array, dims?: readonly number[]): TypedTensor<'int8'>;
/**
* Construct a new uint8 tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: Uint8Array, dims?: readonly number[]): TypedTensor<'uint8'>;
/**
* Construct a new uint8 tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: Uint8ClampedArray, dims?: readonly number[]): TypedTensor<'uint8'>;
/**
* Construct a new uint16 tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: Uint16Array, dims?: readonly number[]): TypedTensor<'uint16'>;
/**
* Construct a new int16 tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: Int16Array, dims?: readonly number[]): TypedTensor<'int16'>;
/**
* Construct a new int32 tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: Int32Array, dims?: readonly number[]): TypedTensor<'int32'>;
/**
* Construct a new int64 tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: BigInt64Array, dims?: readonly number[]): TypedTensor<'int64'>;
/**
* Construct a new string tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: readonly string[], dims?: readonly number[]): TypedTensor<'string'>;
/**
* Construct a new bool tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: readonly boolean[], dims?: readonly number[]): TypedTensor<'bool'>;
/**
* Construct a new float64 tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: Float64Array, dims?: readonly number[]): TypedTensor<'float64'>;
/**
* Construct a new uint32 tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: Uint32Array, dims?: readonly number[]): TypedTensor<'uint32'>;
/**
* Construct a new uint64 tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: BigUint64Array, dims?: readonly number[]): TypedTensor<'uint64'>;
// #endregion
// #region CPU tensor - fall back to non-generic tensor type declaration
/**
* Construct a new tensor object from the given type, data and dims.
*
* @param type - Specify the element type.
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (
type: Tensor.Type,
data: Tensor.DataType | readonly number[] | readonly string[] | readonly bigint[] | readonly boolean[],
dims?: readonly number[],
): Tensor;
/**
* Construct a new tensor object from the given data and dims.
*
* @param data - Specify the CPU tensor data.
* @param dims - Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.
*/
new (data: Tensor.DataType, dims?: readonly number[]): Tensor;
// #endregion
}
// eslint-disable-next-line @typescript-eslint/naming-convention
export const Tensor = TensorImpl as TensorConstructor;
+75
View File
@@ -0,0 +1,75 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { env } from './env-impl.js';
/**
* @ignore
*/
export const TRACE = (deviceType: string, label: string) => {
if (typeof env.trace === 'undefined' ? !env.wasm.trace : !env.trace) {
return;
}
// eslint-disable-next-line no-console
console.timeStamp(`${deviceType}::ORT::${label}`);
};
const TRACE_FUNC = (msg: string, extraMsg?: string) => {
const stack = new Error().stack?.split(/\r\n|\r|\n/g) || [];
let hasTraceFunc = false;
for (let i = 0; i < stack.length; i++) {
if (hasTraceFunc && !stack[i].includes('TRACE_FUNC')) {
let label = `FUNC_${msg}::${stack[i].trim().split(' ')[1]}`;
if (extraMsg) {
label += `::${extraMsg}`;
}
TRACE('CPU', label);
return;
}
if (stack[i].includes('TRACE_FUNC')) {
hasTraceFunc = true;
}
}
};
/**
* @ignore
*/
export const TRACE_FUNC_BEGIN = (extraMsg?: string) => {
if (typeof env.trace === 'undefined' ? !env.wasm.trace : !env.trace) {
return;
}
TRACE_FUNC('BEGIN', extraMsg);
};
/**
* @ignore
*/
export const TRACE_FUNC_END = (extraMsg?: string) => {
if (typeof env.trace === 'undefined' ? !env.wasm.trace : !env.trace) {
return;
}
TRACE_FUNC('END', extraMsg);
};
/**
* @ignore
*/
export const TRACE_EVENT_BEGIN = (extraMsg?: string) => {
if (typeof env.trace === 'undefined' ? !env.wasm.trace : !env.trace) {
return;
}
// eslint-disable-next-line no-console
console.time(`ORT::${extraMsg}`);
};
/**
* @ignore
*/
export const TRACE_EVENT_END = (extraMsg?: string) => {
if (typeof env.trace === 'undefined' ? !env.wasm.trace : !env.trace) {
return;
}
// eslint-disable-next-line no-console
console.timeEnd(`ORT::${extraMsg}`);
};
+31
View File
@@ -0,0 +1,31 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
/**
* A helper type to get certain types if they are declared in global scope.
*
* For example, if you installed "@webgpu/types" as a dev dependency, then `TryGetTypeIfDeclared<'GPUDevice'>` will
* be type `GPUDevice`, otherwise it will be type `unknown`.
*
*
* We don't want to introduce "@webgpu/types" as a dependency of this package because:
*
* (1) For JavaScript users, it's not needed. For TypeScript users, they can install it as dev dependency themselves.
*
* (2) because "@webgpu/types" requires "@types/dom-webcodecs" as peer dependency when using TypeScript < v5.1 and its
* version need to be chosen carefully according to the TypeScript version being used. This means so far there is not a
* way to keep every TypeScript version happy. It turns out that we will easily broke users on some TypeScript version.
*
* for more info see https://github.com/gpuweb/types/issues/127
*
* Update (2024-08-07): The reason (2) may be no longer valid. Most people should be using TypeScript >= 5.1 by now.
* However, we are still not sure whether introducing "@webgpu/types" as direct dependency is a good idea. We find this
* type helper is useful for TypeScript users.
*
* @ignore
*/
export type TryGetGlobalType<Name extends string, Fallback = unknown> = typeof globalThis extends {
[k in Name]: { prototype: infer T };
}
? T
: Fallback;
+7
View File
@@ -0,0 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// This file is generated by /js/scripts/update-version.ts
// Do not modify file content manually.
export const version = '1.27.0';
+37
View File
@@ -0,0 +1,37 @@
{
"license": "MIT",
"type": "module",
"name": "onnxruntime-common",
"version": "1.27.0",
"repository": {
"url": "https://github.com/Microsoft/onnxruntime.git",
"type": "git"
},
"author": "fs-eire",
"scripts": {
"build:cjs": "tsc --module commonjs --moduleResolution node10 --outDir ./dist/cjs",
"build:esm": "tsc",
"build:bundles": "webpack",
"build": "node ./build.js",
"prepare": "npm run build",
"pretest": "tsc --build ./test",
"test": "mocha \"./test/**/*.js\" --timeout 30000",
"test:f16": "mocha -n js-float16array \"./test/**/*.js\" --timeout 30000"
},
"devDependencies": {
"globby": "^15.0.0",
"typedoc": "^0.28.0",
"typescript": "^5.6.2"
},
"main": "dist/cjs/index.js",
"exports": {
"require": "./dist/cjs/index.js",
"import": "./dist/esm/index.js"
},
"keywords": [
"ONNX",
"ONNXRuntime",
"ONNX Runtime"
],
"description": "ONNXRuntime JavaScript API library"
}