chore: update node_modules with new binary files and dependencies

- Add new binary files for nodemon, onnxruntime-web, and xenova/transformers
- Update various JavaScript and TypeScript files in node_modules
- Remove unused files and dependencies
- Add new test fixtures and documentation files
This commit is contained in:
你的名字
2025-07-18 08:41:48 +08:00
parent db8c0adc79
commit 4c2f5c69a1
4655 changed files with 811329 additions and 35112 deletions

View File

@@ -0,0 +1,21 @@
import { Backend } from './backend';
/**
* 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.
*
* @internal
*/
export declare const registerBackend: (name: string, backend: Backend, priority: number) => void;
/**
* Resolve backend by specified hints.
*
* @param backendHints - a list of execution provider names to lookup. If omitted use registered backends as list.
* @returns a promise that resolves to the backend.
*
* @internal
*/
export declare const resolveBackend: (backendHints: readonly string[]) => Promise<Backend>;

View File

@@ -0,0 +1,89 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
const backends = {};
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.
*
* @internal
*/
export const registerBackend = (name, backend, priority) => {
if (backend && typeof backend.init === 'function' && typeof backend.createSessionHandler === 'function') {
const currentBackend = backends[name];
if (currentBackend === undefined) {
backends[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[backendsSortedByPriority[i]].priority <= priority) {
backendsSortedByPriority.splice(i, 0, name);
return;
}
}
backendsSortedByPriority.push(name);
}
return;
}
throw new TypeError('not a valid backend');
};
/**
* Resolve backend by specified hints.
*
* @param backendHints - a list of execution provider names to lookup. If omitted use registered backends as list.
* @returns a promise that resolves to the backend.
*
* @internal
*/
export const resolveBackend = async (backendHints) => {
const backendNames = backendHints.length === 0 ? backendsSortedByPriority : backendHints;
const errors = [];
for (const backendName of backendNames) {
const backendInfo = backends[backendName];
if (backendInfo) {
if (backendInfo.initialized) {
return backendInfo.backend;
}
else if (backendInfo.aborted) {
continue; // current backend is unavailable; try next
}
const isInitializing = !!backendInfo.initPromise;
try {
if (!isInitializing) {
backendInfo.initPromise = backendInfo.backend.init();
}
await backendInfo.initPromise;
backendInfo.initialized = true;
return backendInfo.backend;
}
catch (e) {
if (!isInitializing) {
errors.push({ name: backendName, err: e });
}
backendInfo.aborted = true;
}
finally {
delete backendInfo.initPromise;
}
}
}
throw new Error(`no available backend found. ERR: ${errors.map(e => `[${e.name}] ${e.err}`).join(', ')}`);
};
//# sourceMappingURL=backend-impl.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"backend-impl.js","sourceRoot":"","sources":["../../lib/backend-impl.ts"],"names":[],"mappings":"AAAA,4DAA4D;AAC5D,kCAAkC;AAalC,MAAM,QAAQ,GAAkC,EAAE,CAAC;AACnD,MAAM,wBAAwB,GAAa,EAAE,CAAC;AAE9C;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,IAAY,EAAE,OAAgB,EAAE,QAAgB,EAAQ,EAAE;IACxF,IAAI,OAAO,IAAI,OAAO,OAAO,CAAC,IAAI,KAAK,UAAU,IAAI,OAAO,OAAO,CAAC,oBAAoB,KAAK,UAAU,EAAE;QACvG,MAAM,cAAc,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC;QACtC,IAAI,cAAc,KAAK,SAAS,EAAE;YAChC,QAAQ,CAAC,IAAI,CAAC,GAAG,EAAC,OAAO,EAAE,QAAQ,EAAC,CAAC;SACtC;aAAM,IAAI,cAAc,CAAC,QAAQ,GAAG,QAAQ,EAAE;YAC7C,8EAA8E;YAC9E,OAAO;SACR;aAAM,IAAI,cAAc,CAAC,QAAQ,KAAK,QAAQ,EAAE;YAC/C,IAAI,cAAc,CAAC,OAAO,KAAK,OAAO,EAAE;gBACtC,MAAM,IAAI,KAAK,CAAC,4BAA4B,IAAI,oBAAoB,QAAQ,EAAE,CAAC,CAAC;aACjF;SACF;QAED,IAAI,QAAQ,IAAI,CAAC,EAAE;YACjB,MAAM,CAAC,GAAG,wBAAwB,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;YACjD,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE;gBACZ,wBAAwB,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;aACvC;YAED,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,wBAAwB,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;gBACxD,IAAI,QAAQ,CAAC,wBAAwB,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,IAAI,QAAQ,EAAE;oBAC9D,wBAAwB,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,EAAE,IAAI,CAAC,CAAC;oBAC5C,OAAO;iBACR;aACF;YACD,wBAAwB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;SACrC;QACD,OAAO;KACR;IAED,MAAM,IAAI,SAAS,CAAC,qBAAqB,CAAC,CAAC;AAC7C,CAAC,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,KAAK,EAAC,YAA+B,EAAoB,EAAE;IACvF,MAAM,YAAY,GAAG,YAAY,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,wBAAwB,CAAC,CAAC,CAAC,YAAY,CAAC;IACzF,MAAM,MAAM,GAAG,EAAE,CAAC;IAClB,KAAK,MAAM,WAAW,IAAI,YAAY,EAAE;QACtC,MAAM,WAAW,GAAG,QAAQ,CAAC,WAAW,CAAC,CAAC;QAC1C,IAAI,WAAW,EAAE;YACf,IAAI,WAAW,CAAC,WAAW,EAAE;gBAC3B,OAAO,WAAW,CAAC,OAAO,CAAC;aAC5B;iBAAM,IAAI,WAAW,CAAC,OAAO,EAAE;gBAC9B,SAAS,CAAE,2CAA2C;aACvD;YAED,MAAM,cAAc,GAAG,CAAC,CAAC,WAAW,CAAC,WAAW,CAAC;YACjD,IAAI;gBACF,IAAI,CAAC,cAAc,EAAE;oBACnB,WAAW,CAAC,WAAW,GAAG,WAAW,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC;iBACtD;gBACD,MAAM,WAAW,CAAC,WAAW,CAAC;gBAC9B,WAAW,CAAC,WAAW,GAAG,IAAI,CAAC;gBAC/B,OAAO,WAAW,CAAC,OAAO,CAAC;aAC5B;YAAC,OAAO,CAAC,EAAE;gBACV,IAAI,CAAC,cAAc,EAAE;oBACnB,MAAM,CAAC,IAAI,CAAC,EAAC,IAAI,EAAE,WAAW,EAAE,GAAG,EAAE,CAAC,EAAC,CAAC,CAAC;iBAC1C;gBACD,WAAW,CAAC,OAAO,GAAG,IAAI,CAAC;aAC5B;oBAAS;gBACR,OAAO,WAAW,CAAC,WAAW,CAAC;aAChC;SACF;KACF;IAED,MAAM,IAAI,KAAK,CAAC,oCAAoC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AAC5G,CAAC,CAAC"}

42
node_modules/onnxruntime-common/dist/lib/backend.d.ts generated vendored Normal file
View File

@@ -0,0 +1,42 @@
import { InferenceSession } from './inference-session';
import { OnnxValue } from './onnx-value';
/**
* @internal
*/
export declare namespace SessionHandler {
type FeedsType = {
[name: string]: OnnxValue;
};
type FetchesType = {
[name: string]: OnnxValue | null;
};
type ReturnType = {
[name: string]: OnnxValue;
};
}
/**
* Represent a handler instance of an inference session.
*
* @internal
*/
export interface SessionHandler {
dispose(): Promise<void>;
readonly inputNames: readonly string[];
readonly outputNames: readonly string[];
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.
*
* @internal
*/
export interface Backend {
/**
* Initialize the backend asynchronously. Should throw when failed.
*/
init(): Promise<void>;
createSessionHandler(uriOrBuffer: string | Uint8Array, options?: InferenceSession.SessionOptions): Promise<SessionHandler>;
}
export { registerBackend } from './backend-impl';

4
node_modules/onnxruntime-common/dist/lib/backend.js generated vendored Normal file
View File

@@ -0,0 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
export { registerBackend } from './backend-impl';
//# sourceMappingURL=backend.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"backend.js","sourceRoot":"","sources":["../../lib/backend.ts"],"names":[],"mappings":"AAAA,4DAA4D;AAC5D,kCAAkC;AA+ClC,OAAO,EAAC,eAAe,EAAC,MAAM,gBAAgB,CAAC"}

13
node_modules/onnxruntime-common/dist/lib/env-impl.d.ts generated vendored Normal file
View File

@@ -0,0 +1,13 @@
import { Env } from './env';
type LogLevelType = Env['logLevel'];
export declare class EnvImpl implements Env {
constructor();
set logLevel(value: LogLevelType);
get logLevel(): LogLevelType;
debug?: boolean;
wasm: Env.WebAssemblyFlags;
webgl: Env.WebGLFlags;
[name: string]: unknown;
private logLevelInternal;
}
export {};

23
node_modules/onnxruntime-common/dist/lib/env-impl.js generated vendored Normal file
View File

@@ -0,0 +1,23 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
export class EnvImpl {
constructor() {
this.wasm = {};
this.webgl = {};
this.logLevelInternal = 'warning';
}
// TODO standadize the getter and setter convention in env for other fields.
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}`);
}
this.logLevelInternal = value;
}
get logLevel() {
return this.logLevelInternal;
}
}
//# sourceMappingURL=env-impl.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"env-impl.js","sourceRoot":"","sources":["../../lib/env-impl.ts"],"names":[],"mappings":"AAAA,4DAA4D;AAC5D,kCAAkC;AAKlC,MAAM,OAAO,OAAO;IAClB;QACE,IAAI,CAAC,IAAI,GAAG,EAAE,CAAC;QACf,IAAI,CAAC,KAAK,GAAG,EAAE,CAAC;QAChB,IAAI,CAAC,gBAAgB,GAAG,SAAS,CAAC;IACpC,CAAC;IAED,4EAA4E;IAC5E,IAAI,QAAQ,CAAC,KAAmB;QAC9B,IAAI,KAAK,KAAK,SAAS,EAAE;YACvB,OAAO;SACR;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;YACvG,MAAM,IAAI,KAAK,CAAC,8BAA8B,KAAK,EAAE,CAAC,CAAC;SACxD;QACD,IAAI,CAAC,gBAAgB,GAAG,KAAK,CAAC;IAChC,CAAC;IACD,IAAI,QAAQ;QACV,OAAO,IAAI,CAAC,gBAAgB,CAAC;IAC/B,CAAC;CAWF"}

104
node_modules/onnxruntime-common/dist/lib/env.d.ts generated vendored Normal file
View File

@@ -0,0 +1,104 @@
export declare namespace Env {
type WasmPrefixOrFilePaths = string | {
'ort-wasm.wasm'?: string;
'ort-wasm-threaded.wasm'?: string;
'ort-wasm-simd.wasm'?: string;
'ort-wasm-simd-threaded.wasm'?: string;
};
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 or get a boolean value indicating whether to enable SIMD. If set to false, SIMD will be forcely disabled.
*
* This setting is available only when WebAssembly SIMD feature is available in current context.
*
* @defaultValue `true`
*/
simd?: 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 files or a set of overrides for each .wasm file. The override path should be
* an absolute path.
*/
wasmPaths?: WasmPrefixOrFilePaths;
/**
* 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';
/**
* 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 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;
/**
* Represent a set of flags for WebAssembly
*/
wasm: Env.WebAssemblyFlags;
/**
* Represent a set of flags for WebGL
*/
webgl: Env.WebGLFlags;
[name: string]: unknown;
}
/**
* Represent a set of flags as a global singleton.
*/
export declare const env: Env;

8
node_modules/onnxruntime-common/dist/lib/env.js generated vendored Normal file
View File

@@ -0,0 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { EnvImpl } from './env-impl';
/**
* Represent a set of flags as a global singleton.
*/
export const env = new EnvImpl();
//# sourceMappingURL=env.js.map

1
node_modules/onnxruntime-common/dist/lib/env.js.map generated vendored Normal file
View File

@@ -0,0 +1 @@
{"version":3,"file":"env.js","sourceRoot":"","sources":["../../lib/env.ts"],"names":[],"mappings":"AAAA,4DAA4D;AAC5D,kCAAkC;AAElC,OAAO,EAAC,OAAO,EAAC,MAAM,YAAY,CAAC;AA+GnC;;GAEG;AACH,MAAM,CAAC,MAAM,GAAG,GAAQ,IAAI,OAAO,EAAE,CAAC"}

20
node_modules/onnxruntime-common/dist/lib/index.d.ts generated vendored Normal file
View File

@@ -0,0 +1,20 @@
/**
* # 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.html)
* - [Inference examples](https://github.com/microsoft/onnxruntime-inference-examples/tree/main/js)
*
* @packageDocumentation
*/
export * from './backend';
export * from './env';
export * from './inference-session';
export * from './tensor';
export * from './onnx-value';

23
node_modules/onnxruntime-common/dist/lib/index.js generated vendored Normal file
View File

@@ -0,0 +1,23 @@
// 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.html)
* - [Inference examples](https://github.com/microsoft/onnxruntime-inference-examples/tree/main/js)
*
* @packageDocumentation
*/
export * from './backend';
export * from './env';
export * from './inference-session';
export * from './tensor';
export * from './onnx-value';
//# sourceMappingURL=index.js.map

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,WAAW,CAAC;AAC1B,cAAc,OAAO,CAAC;AACtB,cAAc,qBAAqB,CAAC;AACpC,cAAc,UAAU,CAAC;AACzB,cAAc,cAAc,CAAC"}

View File

@@ -0,0 +1,21 @@
import { InferenceSession as InferenceSessionInterface } from './inference-session';
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>;
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[];
private handler;
}
export {};

View File

@@ -0,0 +1,186 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { resolveBackend } from './backend-impl';
import { Tensor } from './tensor';
export class InferenceSession {
constructor(handler) {
this.handler = handler;
}
async run(feeds, arg1, arg2) {
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)) {
returnValue[key] = new Tensor(results[key].type, results[key].data, results[key].dims);
}
}
return returnValue;
}
static async create(arg0, arg1, arg2, arg3) {
// 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\'.');
}
// get backend hints
const eps = options.executionProviders || [];
const backendHints = eps.map(i => typeof i === 'string' ? i : i.name);
const backend = await resolveBackend(backendHints);
const handler = await backend.createSessionHandler(filePathOrUint8Array, options);
return new InferenceSession(handler);
}
startProfiling() {
this.handler.startProfiling();
}
endProfiling() {
this.handler.endProfiling();
}
get inputNames() {
return this.handler.inputNames;
}
get outputNames() {
return this.handler.outputNames;
}
}
//# sourceMappingURL=inference-session-impl.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,285 @@
import { OnnxValue } from './onnx-value';
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.
*
* @remark
* 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 {
/**
* 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 optimization level.
*
* This setting is available only in ONNXRuntime (Node.js binding and react-native) or WebAssembly backend
*/
graphOptimizationLevel?: 'disabled' | 'basic' | 'extended' | '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';
/**
* Wether 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;
/**
* 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 {
cpu: CpuExecutionProviderOption;
cuda: CudaExecutionProviderOption;
wasm: WebAssemblyExecutionProviderOption;
webgl: WebGLExecutionProviderOption;
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 WebAssemblyExecutionProviderOption extends ExecutionProviderOption {
readonly name: 'wasm';
}
interface WebGLExecutionProviderOption extends ExecutionProviderOption {
readonly name: 'webgl';
}
interface XnnpackExecutionProviderOption extends ExecutionProviderOption {
readonly name: 'xnnpack';
}
/**
* 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>;
}
interface ValueMetadata {
}
}
/**
* 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>;
/**
* 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[];
}
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;

View File

@@ -0,0 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { InferenceSession as InferenceSessionImpl } from './inference-session-impl';
// eslint-disable-next-line @typescript-eslint/naming-convention
export const InferenceSession = InferenceSessionImpl;
//# sourceMappingURL=inference-session.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"inference-session.js","sourceRoot":"","sources":["../../lib/inference-session.ts"],"names":[],"mappings":"AAAA,4DAA4D;AAC5D,kCAAkC;AAElC,OAAO,EAAC,gBAAgB,IAAI,oBAAoB,EAAC,MAAM,0BAA0B,CAAC;AAuXlF,gEAAgE;AAChE,MAAM,CAAC,MAAM,gBAAgB,GAA4B,oBAAoB,CAAC"}

View File

@@ -0,0 +1,9 @@
import { Tensor } from './tensor';
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;
export {};

View File

@@ -0,0 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
export {};
//# sourceMappingURL=onnx-value.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"onnx-value.js","sourceRoot":"","sources":["../../lib/onnx-value.ts"],"names":[],"mappings":"AAAA,4DAA4D;AAC5D,kCAAkC"}

View File

@@ -0,0 +1,26 @@
import { Tensor as TensorInterface, TensorFromImageOptions, TensorToImageDataOptions } from './tensor';
type TensorType = TensorInterface.Type;
type TensorDataType = TensorInterface.DataType;
export declare class Tensor implements TensorInterface {
constructor(type: TensorType, data: TensorDataType | readonly number[] | readonly boolean[], dims?: readonly number[]);
constructor(data: TensorDataType | readonly boolean[], dims?: readonly number[]);
/**
* 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
*/
private static bufferToTensor;
static fromImage(imageData: ImageData, options?: TensorFromImageOptions): Promise<Tensor>;
static fromImage(imageElement: HTMLImageElement, options?: TensorFromImageOptions): Promise<Tensor>;
static fromImage(bitmap: ImageBitmap, options: TensorFromImageOptions): Promise<Tensor>;
static fromImage(url: string, options?: TensorFromImageOptions): Promise<Tensor>;
toImageData(options?: TensorToImageDataOptions): ImageData;
readonly dims: readonly number[];
readonly type: TensorType;
readonly data: TensorDataType;
readonly size: number;
reshape(dims: readonly number[]): Tensor;
}
export {};

481
node_modules/onnxruntime-common/dist/lib/tensor-impl.js generated vendored Normal file
View File

@@ -0,0 +1,481 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
const isBigInt64ArrayAvailable = typeof BigInt64Array !== 'undefined' && typeof BigInt64Array.from === 'function';
const isBigUint64ArrayAvailable = typeof BigUint64Array !== 'undefined' && typeof BigUint64Array.from === 'function';
// a runtime map that maps type string to TypedArray constructor. Should match Tensor.DataTypeMap.
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],
]);
// a runtime map that maps type string to TypedArray constructor. Should match Tensor.DataTypeMap.
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'],
]);
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');
}
/**
* 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;
};
export class Tensor {
constructor(arg0, arg1, arg2) {
let type;
let data;
let dims;
// check whether arg0 is type or data
if (typeof arg0 === 'string') {
//
// Override: constructor(type, data, ...)
//
type = arg0;
dims = 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)) {
// use 'as any' here because TypeScript's check on type of 'SupportedTypedArrayConstructors.from()' produces
// incorrect results.
// 'typedArrayConstructor' should be one of the typed array prototype objects.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
data = typedArrayConstructor.from(arg1);
}
else if (arg1 instanceof typedArrayConstructor) {
data = arg1;
}
else {
throw new TypeError(`A ${type} tensor's data must be type of ${typedArrayConstructor}`);
}
}
}
else {
//
// Override: constructor(data, ...)
//
dims = 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 {
// 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 (dims === undefined) {
// assume 1-D tensor if dims omitted
dims = [data.length];
}
else if (!Array.isArray(dims)) {
throw new TypeError('A tensor\'s dims must be a number array');
}
// perform check
const size = calculateSize(dims);
if (size !== data.length) {
throw new Error(`Tensor's size(${size}) does not match data length(${data.length}).`);
}
this.dims = dims;
this.type = type;
this.data = data;
this.size = size;
}
// #endregion
/**
* 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
*/
static 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');
}
const { height, width } = options;
const norm = options.norm;
let normMean;
let normBias;
if (norm === undefined || norm.mean === undefined) {
normMean = 255;
}
else {
normMean = norm.mean;
}
if (norm === undefined || norm.bias === undefined) {
normBias = 0;
}
else {
normBias = norm.bias;
}
const inputformat = options.bitmapFormat !== undefined ? options.bitmapFormat : 'RGBA';
// default value is RGBA since imagedata and HTMLImageElement uses it
const outputformat = options.tensorFormat !== undefined ?
(options.tensorFormat !== undefined ? options.tensorFormat : 'RGB') :
'RGB';
const offset = height * width;
const float32Data = outputformat === 'RGBA' ? new Float32Array(offset * 4) : new Float32Array(offset * 3);
// Default pointer assignments
let step = 4, rImagePointer = 0, gImagePointer = 1, bImagePointer = 2, aImagePointer = 3;
let rTensorPointer = 0, gTensorPointer = offset, bTensorPointer = offset * 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 = offset * 3;
}
else if (outputformat === 'RBG') {
rTensorPointer = 0;
bTensorPointer = offset;
gTensorPointer = offset * 2;
}
else if (outputformat === 'BGR') {
bTensorPointer = 0;
gTensorPointer = offset;
rTensorPointer = offset * 2;
}
for (let i = 0; i < offset; i++, rImagePointer += step, bImagePointer += step, gImagePointer += step, aImagePointer += step) {
float32Data[rTensorPointer++] = (buffer[rImagePointer] + normBias) / normMean;
float32Data[gTensorPointer++] = (buffer[gImagePointer] + normBias) / normMean;
float32Data[bTensorPointer++] = (buffer[bImagePointer] + normBias) / normMean;
if (aTensorPointer !== -1 && aImagePointer !== -1) {
float32Data[aTensorPointer++] = (buffer[aImagePointer] + normBias) / normMean;
}
}
// 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;
}
static async fromImage(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 isURL = typeof (String) !== 'undefined' && (image instanceof String || typeof image === 'string');
let data;
let tensorConfig = {};
// filling and checking image configuration options
if (isHTMLImageEle) {
// HTMLImageElement - image object - format is RGBA by default
const canvas = document.createElement('canvas');
const pixels2DContext = canvas.getContext('2d');
if (pixels2DContext != null) {
let height = image.naturalHeight;
let width = image.naturalWidth;
if (options !== undefined && options.resizedHeight !== undefined && options.resizedWidth !== undefined) {
height = options.resizedHeight;
width = options.resizedWidth;
}
if (options !== undefined) {
tensorConfig = options;
if (options.tensorFormat !== undefined) {
throw new Error('Image input config format must be RGBA for HTMLImageElement');
}
else {
tensorConfig.tensorFormat = 'RGBA';
}
if (options.height !== undefined && options.height !== height) {
throw new Error('Image input config height doesn\'t match HTMLImageElement height');
}
else {
tensorConfig.height = height;
}
if (options.width !== undefined && options.width !== width) {
throw new Error('Image input config width doesn\'t match HTMLImageElement width');
}
else {
tensorConfig.width = width;
}
}
else {
tensorConfig.tensorFormat = 'RGBA';
tensorConfig.height = height;
tensorConfig.width = width;
}
canvas.width = width;
canvas.height = height;
pixels2DContext.drawImage(image, 0, 0, width, height);
data = pixels2DContext.getImageData(0, 0, width, height).data;
}
else {
throw new Error('Can not access image data');
}
}
else if (isImageDataEle) {
// ImageData - image object - format is RGBA by default
const format = 'RGBA';
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) {
tensorConfig = options;
if (options.bitmapFormat !== undefined && options.bitmapFormat !== format) {
throw new Error('Image input config format must be RGBA for ImageData');
}
else {
tensorConfig.bitmapFormat = 'RGBA';
}
}
else {
tensorConfig.bitmapFormat = 'RGBA';
}
tensorConfig.height = height;
tensorConfig.width = width;
if (options !== undefined) {
const tempCanvas = document.createElement('canvas');
tempCanvas.width = width;
tempCanvas.height = height;
const pixels2DContext = tempCanvas.getContext('2d');
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');
}
if (options.bitmapFormat !== undefined) {
throw new Error('Image input config format must be defined for ImageBitmap');
}
const pixels2DContext = document.createElement('canvas').getContext('2d');
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;
if (options !== undefined) {
// using square brackets to avoid TS error - type 'never'
if (options.height !== undefined && options.height !== height) {
throw new Error('Image input config height doesn\'t match ImageBitmap height');
}
else {
tensorConfig.height = height;
}
// using square brackets to avoid TS error - type 'never'
if (options.width !== undefined && options.width !== width) {
throw new Error('Image input config width doesn\'t match ImageBitmap width');
}
else {
tensorConfig.width = width;
}
}
else {
tensorConfig.height = height;
tensorConfig.width = width;
}
return Tensor.bufferToTensor(data, tensorConfig);
}
else {
throw new Error('Can not access image data');
}
}
else if (isURL) {
return new Promise((resolve, reject) => {
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
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);
if (options !== undefined) {
// using square brackets to avoid TS error - type 'never'
if (options.height !== undefined && options.height !== canvas.height) {
throw new Error('Image input config height doesn\'t match ImageBitmap height');
}
else {
tensorConfig.height = canvas.height;
}
// using square brackets to avoid TS error - type 'never'
if (options.width !== undefined && options.width !== canvas.width) {
throw new Error('Image input config width doesn\'t match ImageBitmap width');
}
else {
tensorConfig.width = canvas.width;
}
}
else {
tensorConfig.height = canvas.height;
tensorConfig.width = canvas.width;
}
resolve(Tensor.bufferToTensor(img.data, tensorConfig));
};
});
}
else {
throw new Error('Input data provided is not supported - aborted tensor creation');
}
if (data !== undefined) {
return Tensor.bufferToTensor(data, tensorConfig);
}
else {
throw new Error('Input data provided is not supported - aborted tensor creation');
}
}
toImageData(options) {
var _a, _b;
const pixels2DContext = document.createElement('canvas').getContext('2d');
let image;
if (pixels2DContext != null) {
// Default values for height and width & format
const width = this.dims[3];
const height = this.dims[2];
const channels = this.dims[1];
const inputformat = options !== undefined ? (options.format !== undefined ? options.format : 'RGB') : 'RGB';
const normMean = options !== undefined ? (((_a = options.norm) === null || _a === void 0 ? void 0 : _a.mean) !== undefined ? options.norm.mean : 255) : 255;
const normBias = options !== undefined ? (((_b = options.norm) === null || _b === void 0 ? void 0 : _b.bias) !== undefined ? options.norm.bias : 0) : 0;
const offset = height * width;
if (options !== undefined) {
if (options.height !== undefined && options.height !== height) {
throw new Error('Image output config height doesn\'t match tensor height');
}
if (options.width !== undefined && options.width !== width) {
throw new Error('Image output config width doesn\'t match tensor width');
}
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 = offset, bTensorPointer = offset * 2, aTensorPointer = -1;
// Updating the pointer assignments based on the input image format
if (inputformat === 'RGBA') {
rTensorPointer = 0;
gTensorPointer = offset;
bTensorPointer = offset * 2;
aTensorPointer = offset * 3;
}
else if (inputformat === 'RGB') {
rTensorPointer = 0;
gTensorPointer = offset;
bTensorPointer = offset * 2;
}
else if (inputformat === 'RBG') {
rTensorPointer = 0;
bTensorPointer = offset;
gTensorPointer = offset * 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] = (this.data[rTensorPointer++] - normBias) * normMean; // R value
image.data[gImagePointer] = (this.data[gTensorPointer++] - normBias) * normMean; // G value
image.data[bImagePointer] = (this.data[bTensorPointer++] - normBias) * normMean; // B value
image.data[aImagePointer] =
aTensorPointer === -1 ? 255 : (this.data[aTensorPointer++] - normBias) * normMean; // A value
}
}
else {
throw new Error('Can not access image data');
}
return image;
}
// #endregion
// #region tensor utilities
reshape(dims) {
return new Tensor(this.type, this.data, dims);
}
}
//# sourceMappingURL=tensor-impl.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,26 @@
import { Tensor, TensorToImageDataOptions, TypedTensor } from './tensor';
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>;
}
export interface TypedTensorUtils<T extends Tensor.Type> extends Properties, TypedShapeUtils<T> {
/**
* creates an ImageData instance from tensor
*
* @param tensorFormat - Interface describing tensor instance - Defaults: RGB, 3 channels, 0-255, NHWC
* 0-255, NHWC
* @returns An ImageData instance which can be used to draw on canvas
*/
toImageData(options?: TensorToImageDataOptions): ImageData;
}
export {};

View File

@@ -0,0 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
export {};
//# sourceMappingURL=tensor-utils.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"tensor-utils.js","sourceRoot":"","sources":["../../lib/tensor-utils.ts"],"names":[],"mappings":"AAAA,4DAA4D;AAC5D,kCAAkC"}

307
node_modules/onnxruntime-common/dist/lib/tensor.d.ts generated vendored Normal file
View File

@@ -0,0 +1,307 @@
import { TypedTensorUtils } from './tensor-utils';
/**
* 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.
*/
readonly data: Tensor.DataTypeMap[T];
}
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: never;
float64: Float64Array;
uint32: Uint32Array;
uint64: BigUint64Array;
}
interface ElementTypeMap {
float32: number;
uint8: number;
int8: number;
uint16: number;
int16: number;
int32: number;
int64: bigint;
string: string;
bool: boolean;
float16: never;
float64: number;
uint32: number;
uint64: bigint;
}
type DataType = DataTypeMap[Type];
type ElementType = ElementTypeMap[Type];
/**
* 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> {
}
export interface TensorConstructor {
/**
* Construct a new string tensor object from the given type, data and dims.
*
* @param type - Specify the element type.
* @param data - Specify the 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 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 numeric tensor object from the given type, data and dims.
*
* @param type - Specify the element type.
* @param data - Specify the 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'>>(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 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 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 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 uint16 tensor object from the given data and dims.
*
* @param data - Specify the 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 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 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 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 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 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 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 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 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 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 boolean[], dims?: readonly number[]): Tensor;
/**
* Construct a new tensor object from the given data and dims.
*
* @param data - Specify the 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;
}
/**
* Specify the image format. Assume 'RGBA' if omitted.
*/
export type ImageFormat = 'RGB' | 'RGBA' | 'BGR' | 'RBG';
/**
* Describes Tensor configuration to an image data.
*/
export interface TensorToImageDataOptions {
/**
* Describes Tensor channels order.
*/
format?: ImageFormat;
/**
* Tensor channel layout - default is 'NHWC'
*/
tensorLayout?: 'NHWC' | 'NCHW';
/**
* Describes Tensor Height - can be accessed via tensor dimensions as well
*/
height?: number;
/**
* Describes Tensor Width - can be accessed via tensor dimensions as well
*/
width?: number;
/**
* Describes normalization parameters to ImageData conversion from tensor - default values - Bias: 0, Mean: 255
*/
norm?: {
bias?: number;
mean?: number;
};
}
/**
* Describes Tensor and Image configuration to an image data.
*/
export interface TensorFromImageOptions {
/**
* Describes image data format - will be used only in the case of ImageBitMap
*/
bitmapFormat?: ImageFormat;
/**
* Describes Tensor channels order - can differ from original image
*/
tensorFormat?: ImageFormat;
/**
* Tensor data type - default is 'float32'
*/
dataType?: 'float32' | 'uint8';
/**
* Tensor channel layout - default is 'NHWC'
*/
tensorLayout?: 'NHWC' | 'NCHW';
/**
* Describes Image Height - Required only in the case of ImageBitMap
*/
height?: number;
/**
* Describes Image Width - Required only in the case of ImageBitMap
*/
width?: number;
/**
* Describes resized height - can be accessed via tensor dimensions as well
*/
resizedHeight?: number;
/**
* Describes resized width - can be accessed via tensor dimensions as well
*/
resizedWidth?: number;
/**
* Describes normalization parameters to tensor conversion from image data - default values - Bias: 0, Mean: 255
*/
norm?: {
bias?: number;
mean?: number;
};
}
export interface TensorFactory {
/**
* create a tensor from image object - HTMLImageElement, ImageData, ImageBitmap, URL
*
* @param imageData - {ImageData} - composed of: Uint8ClampedArray, width. height - uses known pixel format RGBA
* @param options - Optional - Interface describing input image & output tensor -
* Input Defaults: RGBA, 3 channels, 0-255, NHWC - Output Defaults: same as input parameters
* @returns A promise that resolves to a tensor object
*/
fromImage(imageData: ImageData, options?: TensorFromImageOptions): Promise<Tensor>;
/**
* create a tensor from image object - HTMLImageElement, ImageData, ImageBitmap, URL
*
* @param imageElement - {HTMLImageElement} - since the data is stored as ImageData no need for format parameter
* @param options - Optional - Interface describing input image & output tensor -
* Input Defaults: RGBA, 3 channels, 0-255, NHWC - Output Defaults: same as input parameters
* @returns A promise that resolves to a tensor object
*/
fromImage(imageElement: HTMLImageElement, options?: TensorFromImageOptions): Promise<Tensor>;
/**
* create a tensor from image object - HTMLImageElement, ImageData, ImageBitmap, URL
*
* @param url - {string} - Assuming the string is a URL to an image
* @param options - Optional - Interface describing input image & output tensor -
* Input Defaults: RGBA, 3 channels, 0-255, NHWC - Output Defaults: same as input parameters
* @returns A promise that resolves to a tensor object
*/
fromImage(url: string, options?: TensorFromImageOptions): Promise<Tensor>;
/**
* create a tensor from image object - HTMLImageElement, ImageData, ImageBitmap, URL
*
* @param bitMap - {ImageBitmap} - since the data is stored as ImageData no need for format parameter
* @param options - NOT Optional - Interface describing input image & output tensor -
* Output Defaults: same as input parameters
* @returns A promise that resolves to a tensor object
*/
fromImage(bitmap: ImageBitmap, options: TensorFromImageOptions): Promise<Tensor>;
}
export declare const Tensor: TensorConstructor;
export {};

6
node_modules/onnxruntime-common/dist/lib/tensor.js generated vendored Normal file
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';
// eslint-disable-next-line @typescript-eslint/naming-convention
export const Tensor = TensorImpl;
//# sourceMappingURL=tensor.js.map

View File

@@ -0,0 +1 @@
{"version":3,"file":"tensor.js","sourceRoot":"","sources":["../../lib/tensor.ts"],"names":[],"mappings":"AAAA,4DAA4D;AAC5D,kCAAkC;AAElC,OAAO,EAAC,MAAM,IAAI,UAAU,EAAC,MAAM,eAAe,CAAC;AAgWnD,gEAAgE;AAChE,MAAM,CAAC,MAAM,MAAM,GAAG,UAA+B,CAAC"}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

1051
node_modules/onnxruntime-common/dist/ort-common.js generated vendored Normal file

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long