onnxruntime/nodejs/lib/tensor.ts
Yulong Wang 5dfc91db51
Node.js binding for ONNX Runtime (#3613)
* initial commit for Node.js binding

* add c++ code

* add inference session impl

* e2e working

* add settings.json

* add test data

* adjust binding declaration

* refine tensor constructor declaration

* update tests

* enable onnx tests

* simply refine readme

* refine cpp impl

* refine tests

* formatting

* add linting

* move bin folder

* fix linux build

* manually update test filter list

* update C++ API headers: fix crash in release build

* make (manually) prebuild work

* add test into prepack script

* specify prebuild runtime type (N-API)

* build.ts: update rebuild and include regex

* fix lazy load on electron.js

* update dev version, git link and binary host

* support session options and run options

* bump dev version

* update README

* add 1 example

* move folder

* adjust path

* update document for examples

* rename example 01

* add example 02

* add session option: log severity level

* add example 04

* resolve comments

* fix typo

* remove double guard in header files

* add copyright banner

* move BUILD outside from README

* consume test filter list from onnxruntime
2020-05-05 11:45:12 -07:00

230 lines
7.3 KiB
TypeScript

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import {Tensor as impl} from './tensor-impl';
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; // hold on using Uint16Array before we have a concrete solution for float 16
float64: Float64Array;
uint32: Uint32Array;
uint64: BigUint64Array;
// complex64: never;
// complex128: never;
// bfloat16: never;
}
interface ElementTypeMap {
float32: number;
uint8: number;
int8: number;
uint16: number;
int16: number;
int32: number;
int64: number; // may lose precision
string: string;
bool: boolean;
float16: never; // hold on before we have a concret solution for float 16
float64: number;
uint32: number;
uint64: number; // may lose precision
// complex64: never;
// complex128: never;
// bfloat16: never;
}
type DataType = DataTypeMap[Type];
type ElementType = ElementTypeMap[Type];
/**
* represent the data type of a tensor
*/
export type Type = keyof DataTypeMap;
}
export interface TypedTensor<T extends Tensor.Type> extends TypedTensorBase<T>, TypedTensorUtils<T> {}
export type Tensor = TypedTensor<Tensor.Type>;
export interface TensorConstructor {
//#region specify element type
/**
* Construct a new string tensor object from the given type, data and dims.
*
* @type Specify the element type.
* @data Specify the tensor data
* @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.
*
* @type Specify the element type.
* @data Specify the tensor data
* @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.
*
* @type Specify the element type.
* @data Specify the tensor data
* @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>;
//#endregion
//#region infer element types
/**
* Construct a new float32 tensor object from the given data and dims.
*
* @data Specify the tensor data
* @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.
*
* @data Specify the tensor data
* @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.
*
* @data Specify the tensor data
* @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.
*
* @data Specify the tensor data
* @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.
*
* @data Specify the tensor data
* @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.
*
* @data Specify the tensor data
* @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.
*
* @data Specify the tensor data
* @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.
*
* @data Specify the tensor data
* @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.
*
* @data Specify the tensor data
* @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.
*
* @data Specify the tensor data
* @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.
*
* @data Specify the tensor data
* @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.
*
* @data Specify the tensor data
* @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 fall back to non-generic tensor type declaration
/**
* Construct a new tensor object from the given type, data and dims.
*
* @type Specify the element type.
* @data Specify the tensor data
* @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.
*
* @data Specify the tensor data
* @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 no-redeclare
export const Tensor = impl as TensorConstructor;