ONNX Runtime JavaScript API
    Preparing search index...

    Interface TensorConstructor

    type TensorConstructor defines the constructors of 'Tensor' to create CPU tensor instances.

    interface TensorConstructor {
        new TensorConstructor(
            type: "string",
            data: string[] | readonly string[],
            dims?: readonly number[],
        ): TypedTensor<"string">;
        new TensorConstructor(
            type: "bool",
            data: Uint8Array<ArrayBufferLike> | readonly boolean[],
            dims?: readonly number[],
        ): TypedTensor<"bool">;
        new TensorConstructor(
            type: "uint8",
            data: Uint8ClampedArray,
            dims?: readonly number[],
        ): TypedTensor<"uint8">;
        new TensorConstructor<T extends "int64" | "uint64">(
            type: T,
            data: readonly number[] | DataTypeMap[T] | readonly bigint[],
            dims?: readonly number[],
        ): TypedTensor<T>;
        new TensorConstructor<
            T extends
                | "float32"
                | "uint8"
                | "int8"
                | "uint16"
                | "int16"
                | "int32"
                | "float16"
                | "float64"
                | "uint32"
                | "uint4"
                | "int4",
        >(
            type: T,
            data: readonly number[] | DataTypeMap[T],
            dims?: readonly number[],
        ): TypedTensor<T>;
        new TensorConstructor(
            data: Float32Array,
            dims?: readonly number[],
        ): TypedTensor<"float32">;
        new TensorConstructor(
            data: Int8Array,
            dims?: readonly number[],
        ): TypedTensor<"int8">;
        new TensorConstructor(
            data: Uint8Array,
            dims?: readonly number[],
        ): TypedTensor<"uint8">;
        new TensorConstructor(
            data: Uint8ClampedArray,
            dims?: readonly number[],
        ): TypedTensor<"uint8">;
        new TensorConstructor(
            data: Uint16Array,
            dims?: readonly number[],
        ): TypedTensor<"uint16">;
        new TensorConstructor(
            data: Int16Array,
            dims?: readonly number[],
        ): TypedTensor<"int16">;
        new TensorConstructor(
            data: Int32Array,
            dims?: readonly number[],
        ): TypedTensor<"int32">;
        new TensorConstructor(
            data: BigInt64Array,
            dims?: readonly number[],
        ): TypedTensor<"int64">;
        new TensorConstructor(
            data: readonly string[],
            dims?: readonly number[],
        ): TypedTensor<"string">;
        new TensorConstructor(
            data: readonly boolean[],
            dims?: readonly number[],
        ): TypedTensor<"bool">;
        new TensorConstructor(
            data: Float64Array,
            dims?: readonly number[],
        ): TypedTensor<"float64">;
        new TensorConstructor(
            data: Uint32Array,
            dims?: readonly number[],
        ): TypedTensor<"uint32">;
        new TensorConstructor(
            data: BigUint64Array,
            dims?: readonly number[],
        ): TypedTensor<"uint64">;
        new TensorConstructor(
            type: keyof DataTypeMap,
            data:
                | readonly string[]
                | readonly number[]
                | DataType
                | readonly boolean[]
                | readonly bigint[],
            dims?: readonly number[],
        ): Tensor;
        new TensorConstructor(data: DataType, dims?: readonly number[]): Tensor;
        fromGpuBuffer<T extends GpuBufferDataTypes>(
            buffer: GpuBufferTypeFallback,
            options: TensorFromGpuBufferOptions<T>,
        ): TypedTensor<T>;
        fromImage(
            imageData: ImageData,
            options?: TensorFromImageDataOptions,
        ): Promise<TypedTensor<"float32"> | TypedTensor<"uint8">>;
        fromImage(
            imageElement: HTMLImageElement,
            options?: TensorFromImageElementOptions,
        ): Promise<TypedTensor<"float32"> | TypedTensor<"uint8">>;
        fromImage(
            urlSource: string,
            options?: TensorFromUrlOptions,
        ): Promise<TypedTensor<"float32"> | TypedTensor<"uint8">>;
        fromImage(
            bitmap: ImageBitmap,
            options: TensorFromImageBitmapOptions,
        ): Promise<TypedTensor<"float32"> | TypedTensor<"uint8">>;
        fromMLTensor<T extends MLTensorDataTypes>(
            tensor: MLTensorTypeFallback,
            options: TensorFromMLTensorOptions<T>,
        ): TypedTensor<T>;
        fromPinnedBuffer<
            T extends
                | "float32"
                | "uint8"
                | "int8"
                | "uint16"
                | "int16"
                | "int32"
                | "int64"
                | "bool"
                | "float16"
                | "float64"
                | "uint32"
                | "uint64"
                | "uint4"
                | "int4",
        >(
            type: T,
            buffer: DataTypeMap[T],
            dims?: readonly number[],
        ): TypedTensor<T>;
        fromTexture<T extends "float32" = "float32">(
            texture: WebGLTexture,
            options: TensorFromTextureOptions<T>,
        ): TypedTensor<"float32">;
    }

    Hierarchy (View Summary)

    Index

    Constructors

    • Construct a new string tensor object from the given type, data and dims.

      Parameters

      • type: "string"

        Specify the element type.

      • data: string[] | readonly string[]

        Specify the CPU tensor data.

      • Optionaldims: readonly number[]

        Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.

      Returns TypedTensor<"string">

    • Construct a new bool tensor object from the given type, data and dims.

      Parameters

      • type: "bool"

        Specify the element type.

      • data: Uint8Array<ArrayBufferLike> | readonly boolean[]

        Specify the CPU tensor data.

      • Optionaldims: readonly number[]

        Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.

      Returns TypedTensor<"bool">

    • Construct a new uint8 tensor object from a Uint8ClampedArray, data and dims.

      Parameters

      • type: "uint8"

        Specify the element type.

      • data: Uint8ClampedArray

        Specify the CPU tensor data.

      • Optionaldims: readonly number[]

        Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.

      Returns TypedTensor<"uint8">

    • Construct a new 64-bit integer typed tensor object from the given type, data and dims.

      Type Parameters

      • T extends "int64" | "uint64"

      Parameters

      • type: T

        Specify the element type.

      • data: readonly number[] | DataTypeMap[T] | readonly bigint[]

        Specify the CPU tensor data.

      • Optionaldims: readonly number[]

        Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.

      Returns TypedTensor<T>

    • Construct a new numeric tensor object from the given type, data and dims.

      Type Parameters

      • T extends
            | "float32"
            | "uint8"
            | "int8"
            | "uint16"
            | "int16"
            | "int32"
            | "float16"
            | "float64"
            | "uint32"
            | "uint4"
            | "int4"

      Parameters

      • type: T

        Specify the element type.

      • data: readonly number[] | DataTypeMap[T]

        Specify the CPU tensor data.

      • Optionaldims: readonly number[]

        Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.

      Returns TypedTensor<T>

    • Construct a new float32 tensor object from the given data and dims.

      Parameters

      • data: Float32Array

        Specify the CPU tensor data.

      • Optionaldims: readonly number[]

        Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.

      Returns TypedTensor<"float32">

    • Construct a new int8 tensor object from the given data and dims.

      Parameters

      • data: Int8Array

        Specify the CPU tensor data.

      • Optionaldims: readonly number[]

        Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.

      Returns TypedTensor<"int8">

    • Construct a new uint8 tensor object from the given data and dims.

      Parameters

      • data: Uint8Array

        Specify the CPU tensor data.

      • Optionaldims: readonly number[]

        Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.

      Returns TypedTensor<"uint8">

    • Construct a new uint8 tensor object from the given data and dims.

      Parameters

      • data: Uint8ClampedArray

        Specify the CPU tensor data.

      • Optionaldims: readonly number[]

        Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.

      Returns TypedTensor<"uint8">

    • Construct a new uint16 tensor object from the given data and dims.

      Parameters

      • data: Uint16Array

        Specify the CPU tensor data.

      • Optionaldims: readonly number[]

        Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.

      Returns TypedTensor<"uint16">

    • Construct a new int16 tensor object from the given data and dims.

      Parameters

      • data: Int16Array

        Specify the CPU tensor data.

      • Optionaldims: readonly number[]

        Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.

      Returns TypedTensor<"int16">

    • Construct a new int32 tensor object from the given data and dims.

      Parameters

      • data: Int32Array

        Specify the CPU tensor data.

      • Optionaldims: readonly number[]

        Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.

      Returns TypedTensor<"int32">

    • Construct a new int64 tensor object from the given data and dims.

      Parameters

      • data: BigInt64Array

        Specify the CPU tensor data.

      • Optionaldims: readonly number[]

        Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.

      Returns TypedTensor<"int64">

    • Construct a new string tensor object from the given data and dims.

      Parameters

      • data: readonly string[]

        Specify the CPU tensor data.

      • Optionaldims: readonly number[]

        Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.

      Returns TypedTensor<"string">

    • Construct a new bool tensor object from the given data and dims.

      Parameters

      • data: readonly boolean[]

        Specify the CPU tensor data.

      • Optionaldims: readonly number[]

        Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.

      Returns TypedTensor<"bool">

    • Construct a new float64 tensor object from the given data and dims.

      Parameters

      • data: Float64Array

        Specify the CPU tensor data.

      • Optionaldims: readonly number[]

        Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.

      Returns TypedTensor<"float64">

    • Construct a new uint32 tensor object from the given data and dims.

      Parameters

      • data: Uint32Array

        Specify the CPU tensor data.

      • Optionaldims: readonly number[]

        Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.

      Returns TypedTensor<"uint32">

    • Construct a new uint64 tensor object from the given data and dims.

      Parameters

      • data: BigUint64Array

        Specify the CPU tensor data.

      • Optionaldims: readonly number[]

        Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.

      Returns TypedTensor<"uint64">

    • Construct a new tensor object from the given type, data and dims.

      Parameters

      • type: keyof DataTypeMap

        Specify the element type.

      • data:
            | readonly string[]
            | readonly number[]
            | DataType
            | readonly boolean[]
            | readonly bigint[]

        Specify the CPU tensor data.

      • Optionaldims: readonly number[]

        Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.

      Returns Tensor

    • Construct a new tensor object from the given data and dims.

      Parameters

      • data: DataType

        Specify the CPU tensor data.

      • Optionaldims: readonly number[]

        Specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.

      Returns Tensor

    Methods

    • create a tensor from a WebGPU buffer

      Type Parameters

      Parameters

      • buffer: GpuBufferTypeFallback

        the GPUBuffer object to create tensor from

      • options: TensorFromGpuBufferOptions<T>

        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 TypedTensor<T>

      a tensor object

    • create a tensor from a WebNN MLTensor

      Type Parameters

      Parameters

      • tensor: MLTensorTypeFallback

        the MLTensor object to create tensor from

      • options: TensorFromMLTensorOptions<T>

        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 TypedTensor<T>

      a tensor object

    • create a tensor from a pre-allocated buffer. The buffer will be used as a pinned buffer.

      Type Parameters

      • T extends
            | "float32"
            | "uint8"
            | "int8"
            | "uint16"
            | "int16"
            | "int32"
            | "int64"
            | "bool"
            | "float16"
            | "float64"
            | "uint32"
            | "uint64"
            | "uint4"
            | "int4"

      Parameters

      • type: T

        the tensor element type.

      • buffer: DataTypeMap[T]

        a TypedArray corresponding to the type.

      • Optionaldims: readonly number[]

        specify the dimension of the tensor. If omitted, a 1-D tensor is assumed.

      Returns TypedTensor<T>

      a tensor object

    • create a tensor from a WebGL texture

      Type Parameters

      • T extends "float32" = "float32"

      Parameters

      • texture: WebGLTexture

        the WebGLTexture object to create tensor from

      • options: TensorFromTextureOptions<T>

        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 TypedTensor<"float32">

      a tensor object