Anonymous View
ONNX Runtime JavaScript API
    Preparing search index...

    Interface Tensor

    Represent multi-dimensional arrays to feed to or fetch from model inferencing.

    interface Tensor {
        data:
            | string[]
            | Int8Array<ArrayBufferLike>
            | Uint8Array<ArrayBufferLike>
            | Int16Array<ArrayBufferLike>
            | Uint16Array<ArrayBufferLike>
            | Int32Array<ArrayBufferLike>
            | Uint32Array<ArrayBufferLike>
            | Float32Array<ArrayBufferLike>
            | Float64Array<ArrayBufferLike>
            | BigInt64Array<ArrayBufferLike>
            | BigUint64Array<ArrayBufferLike>;
        dims: readonly number[];
        gpuBuffer: GpuBufferTypeFallback;
        location: DataLocation;
        mlTensor: MLTensorTypeFallback;
        size: number;
        texture: WebGLTexture;
        type: keyof DataTypeMap;
        dispose(): void;
        getData(
            releaseData?: boolean,
        ): Promise<
            | string[]
            | Int8Array<ArrayBufferLike>
            | Uint8Array<ArrayBufferLike>
            | Int16Array<ArrayBufferLike>
            | Uint16Array<ArrayBufferLike>
            | Int32Array<ArrayBufferLike>
            | Uint32Array<ArrayBufferLike>
            | Float32Array<ArrayBufferLike>
            | Float64Array<ArrayBufferLike>
            | BigInt64Array<ArrayBufferLike>
            | BigUint64Array<ArrayBufferLike>,
        >;
        reshape(dims: readonly number[]): TypedTensor<keyof DataTypeMap>;
        toDataURL(options?: TensorToDataUrlOptions): string;
        toImageData(options?: TensorToImageDataOptions): ImageData;
    }

    Hierarchy

    • TypedTensorBase<Type>
    • TypedTensorUtils<Type>
      • Tensor
    Index

    Properties

    data:
        | string[]
        | Int8Array<ArrayBufferLike>
        | Uint8Array<ArrayBufferLike>
        | Int16Array<ArrayBufferLike>
        | Uint16Array<ArrayBufferLike>
        | Int32Array<ArrayBufferLike>
        | Uint32Array<ArrayBufferLike>
        | Float32Array<ArrayBufferLike>
        | Float64Array<ArrayBufferLike>
        | BigInt64Array<ArrayBufferLike>
        | BigUint64Array<ArrayBufferLike>

    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.

    dims: readonly number[]

    Get the dimensions of the tensor.

    Get the WebGPU buffer that holds the tensor data.

    If the data is not on GPU as WebGPU buffer, throw error.

    location: DataLocation

    Get the location of the data.

    Get the WebNN MLTensor that holds the tensor data.

    If the data is not in a WebNN MLTensor, throw error.

    size: number

    Get the number of elements in the tensor.

    texture: WebGLTexture

    Get the WebGL texture that holds the tensor data.

    If the data is not on GPU as WebGL texture, throw error.

    type: keyof DataTypeMap

    Get the data type of the tensor.

    Methods

    • 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'.

      Returns void

    • 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.

      Parameters

      • OptionalreleaseData: boolean

        whether release the data on GPU. Ignore if data is already on CPU.

      Returns Promise<
          | string[]
          | Int8Array<ArrayBufferLike>
          | Uint8Array<ArrayBufferLike>
          | Int16Array<ArrayBufferLike>
          | Uint16Array<ArrayBufferLike>
          | Int32Array<ArrayBufferLike>
          | Uint32Array<ArrayBufferLike>
          | Float32Array<ArrayBufferLike>
          | Float64Array<ArrayBufferLike>
          | BigInt64Array<ArrayBufferLike>
          | BigUint64Array<ArrayBufferLike>,
      >

    • Create a new tensor with the same data buffer and specified dims.

      Parameters

      • dims: readonly number[]

        New dimensions. Size should match the old one.

      Returns TypedTensor<keyof DataTypeMap>

    • creates a DataURL instance from tensor

      Parameters

      • Optionaloptions: TensorToDataUrlOptions

        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 string

      a DataURL string representing the image converted from tensor data

    • creates an ImageData instance from tensor

      Parameters

      • Optionaloptions: TensorToImageDataOptions

        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 ImageData

      an ImageData instance representing the image converted from tensor data