Transformers.js documentation

utils/tensor

You are viewing main version, which requires installation from source. If you'd like regular npm install, checkout the latest stable version (v3.8.1).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

utils/tensor

Tensors and tensor operations.

Tensor is the typed n-dimensional array used throughout the library for model inputs and outputs. This module also provides functions to create, transform, and combine tensors.

On this page

Classes β€” Tensor

Functions β€” permute Β· interpolate Β· interpolate_4d Β· matmul Β· rfft Β· topk Β· slice Β· mean_pooling Β· layer_norm Β· cat Β· stack Β· std_mean Β· mean Β· full Β· full_like Β· ones Β· ones_like Β· zeros Β· zeros_like Β· rand Β· randn Β· quantize_embeddings

Classes

Tensor

A typed multi-dimensional array.

import { Tensor } from '@huggingface/transformers';
const tensor = new Tensor('float32', [1, 2, 3, 4, 5, 6], [2, 3]);
tensor.dims;    // [2, 3]
tensor.tolist(); // [[1, 2, 3], [4, 5, 6]]

Tensor.dims : number[]

Dimensions of the tensor.

Tensor.type : DataType

Type of the tensor.

Tensor.data : DataArray

The data stored in the tensor.

Tensor.size : number

The number of elements in the tensor.

Tensor.location : string

The location of the tensor data.

Tensor.constructor(args)

Create a new Tensor, either from raw data or by wrapping an onnxruntime tensor:

  • new Tensor(dataType, data, dims), e.g. new Tensor('float32', new Float32Array([1, 2, 3]), [3]).
  • new Tensor(ortTensor).

Parameters

  • args ([DataType, DataArray, number[]] | [ONNXTensor])

Tensor.dispose()

Releases the underlying ONNX Runtime tensor (e.g., GPU buffers). Do not use the tensor afterwards.

Tensor.[Symbol.iterator]()

Returns an iterator object for iterating over the tensor data in row-major order. If the tensor has more than one dimension, the iterator will yield subarrays.

Returns: Iterator<any> β€” An iterator object for iterating over the tensor data in row-major order.

Tensor.item()

Returns the value of this tensor as a standard JavaScript Number. This only works for tensors with one element. For other cases, see Tensor.tolist().

Returns: number | bigint β€” The value of this tensor as a standard JavaScript Number.

Throws

  • Error β€” If the tensor has more than one element.

Tensor.tolist()

Convert tensor data to a n-dimensional JS list

Returns: any[]

Tensor.sigmoid()

Return a new Tensor with the sigmoid function applied to each element.

Returns: Tensor β€” The tensor with the sigmoid function applied.

Tensor.sigmoid_()

Applies the sigmoid function to the tensor in place.

Returns: Tensor β€” Returns this.

Tensor.map(callback)

Return a new Tensor with a callback function applied to each element.

Parameters

  • callback (Function) β€” The function to apply to each element. It should take three arguments: the current element, its index, and the tensor’s data array.

Returns: Tensor β€” A new Tensor with the callback function applied to each element.

Tensor.map_(callback)

Apply a callback function to each element of the tensor in place.

Parameters

  • callback (Function) β€” The function to apply to each element. It should take three arguments: the current element, its index, and the tensor’s data array.

Returns: Tensor β€” Returns this.

Tensor.mul(val)

Return a new Tensor with every element multiplied by a constant.

Parameters

  • val (number) β€” The value to multiply by.

Returns: Tensor β€” The new tensor.

Tensor.mul_(val)

Multiply the tensor by a constant in place.

Parameters

  • val (number) β€” The value to multiply by.

Returns: Tensor β€” Returns this.

Tensor.div(val)

Return a new Tensor with every element divided by a constant.

Parameters

  • val (number) β€” The value to divide by.

Returns: Tensor β€” The new tensor.

Tensor.div_(val)

Divide the tensor by a constant in place.

Parameters

  • val (number) β€” The value to divide by.

Returns: Tensor β€” Returns this.

Tensor.add(val)

Return a new Tensor with every element added by a constant.

Parameters

  • val (number) β€” The value to add by.

Returns: Tensor β€” The new tensor.

Tensor.add_(val)

Add the tensor by a constant in place.

Parameters

  • val (number) β€” The value to add by.

Returns: Tensor β€” Returns this.

Tensor.sub(val)

Return a new Tensor with every element subtracted by a constant.

Parameters

  • val (number) β€” The value to subtract by.

Returns: Tensor β€” The new tensor.

Tensor.sub_(val)

Subtract the tensor by a constant in place.

Parameters

  • val (number) β€” The value to subtract by.

Returns: Tensor β€” Returns this.

Tensor.remainder(val)

Return a new Tensor with the element-wise remainder of division by a constant. Uses Python-style modulo signs (e.g. -1 mod 2 = 1) while preserving the tensor’s dtype. Negative divisors are unsupported for unsigned and boolean tensors. This operation does not implement PyTorch’s dtype promotion or scalar casting rules.

Parameters

  • val (number | bigint) β€” The divisor.

Returns: Tensor β€” The new tensor.

Tensor.remainder_(val)

In-place version of Tensor.remainder

Parameters

  • val (number | bigint) β€” The divisor.

Returns: Tensor β€” Returns this.

Tensor.clone()

Creates a deep copy of the current Tensor.

Returns: Tensor β€” A new Tensor with the same type, data, and dimensions as the original.

Tensor.slice(slices)

Performs a slice operation on the Tensor along specified dimensions.

Consider a Tensor that has a dimension of [4, 7]:

[ 1,  2,  3,  4,  5,  6,  7]
[ 8,  9, 10, 11, 12, 13, 14]
[15, 16, 17, 18, 19, 20, 21]
[22, 23, 24, 25, 26, 27, 28]

We can slice against the two dims of row and column, for instance in this case we can start at the second element, and return to the second last, like this:

tensor.slice([1, -1], [1, -1]);

which would return:

[  9, 10, 11, 12, 13 ]
[ 16, 17, 18, 19, 20 ]

Parameters

  • slices (...(number|number[]|null)) β€” The slice specifications for each dimension.
    • If a number is given, then a single element is selected.
    • If an array of two numbers is given, then a range of elements [start, end (exclusive)] is selected.
    • If null is given, then the entire dimension is selected.

Returns: Tensor β€” A new Tensor containing the selected elements.

Throws

  • Error β€” If the slice input is invalid.

Tensor.permute(dims)

Return a permuted version of this Tensor, according to the provided dimensions.

Parameters

  • dims (...number) β€” Dimensions to permute.

Returns: Tensor β€” The permuted tensor.

Tensor.transpose(dims)

Return a permuted version of this Tensor, according to the provided dimensions.

Parameters

  • dims (...number) β€” Dimensions to permute.

Returns: Tensor β€” The permuted tensor.

Tensor.sum([dim], keepdim)

Returns the sum of each row of the input tensor in the given dimension dim.

Parameters

  • dim (number | null) optional β€” defaults to null β€” The dimension or dimensions to reduce. If null, all dimensions are reduced.
  • keepdim (boolean) β€” Whether the output tensor has dim retained or not.

Returns: The summed tensor

Tensor.norm([p], [dim], [keepdim])

Returns the matrix norm or vector norm of a given tensor.

Parameters

  • p (number | string) optional β€” defaults to 'fro' β€” The order of norm
  • dim (number | null) optional β€” defaults to null β€” Specifies which dimension of the tensor to calculate the norm across. If dim is None, the norm will be calculated across all dimensions of input.
  • keepdim (boolean) optional β€” defaults to false β€” Whether the output tensors have dim retained or not.

Returns: Tensor β€” The norm of the tensor.

Tensor.normalize_([p], [dim])

Performs L_p normalization of inputs over specified dimension. Operates in place.

Parameters

  • p (number) optional β€” defaults to 2 β€” The exponent value in the norm formulation
  • dim (number) optional β€” defaults to 1 β€” The dimension to reduce

Returns: Tensor β€” this for operation chaining.

Tensor.normalize([p], [dim])

Performs L_p normalization of inputs over specified dimension.

Parameters

  • p (number) optional β€” defaults to 2 β€” The exponent value in the norm formulation
  • dim (number) optional β€” defaults to 1 β€” The dimension to reduce

Returns: Tensor β€” The normalized tensor.

Tensor.stride()

Compute and return the stride of this tensor. Stride is the jump necessary to go from one element to the next one in the specified dimension dim.

Returns: number[] β€” The stride of this tensor.

Tensor.squeeze([dim])

Returns a tensor with all specified dimensions of input of size 1 removed.

NOTE: The returned tensor shares the storage with the input tensor, so changing the contents of one will change the contents of the other. If you would like a copy, use tensor.clone() before squeezing.

Parameters

  • dim (number | number[] | null) optional β€” defaults to null β€” If given, the input will be squeezed only in the specified dimensions.

Returns: Tensor β€” The squeezed tensor

Tensor.squeeze_([dim])

In-place version of Tensor.squeeze

Parameters

  • dim (number | number[] | null) optional β€” defaults to null β€” If given, the input will be squeezed only in the specified dimensions.

Returns: Tensor β€” this, with the specified dimensions of size 1 removed.

Tensor.unsqueeze(dim)

Returns a new tensor with a dimension of size one inserted at the specified position.

NOTE: The returned tensor shares the same underlying data with this tensor.

Parameters

  • dim (number) β€” The index at which to insert the singleton dimension

Returns: Tensor β€” The unsqueezed tensor

Tensor.unsqueeze_(dim)

In-place version of Tensor.unsqueeze

Parameters

  • dim (number) β€” The index at which to insert the singleton dimension

Returns: Tensor β€” The unsqueezed tensor

Tensor.flatten_([start_dim], [end_dim])

In-place version of Tensor.flatten

Parameters

  • start_dim (number) optional β€” defaults to 0 β€” the first dim to flatten
  • end_dim (number) optional β€” defaults to -1 β€” the last dim to flatten

Returns: Tensor β€” this, flattened along the specified dimensions.

Tensor.flatten(start_dim, end_dim)

Flattens input by reshaping it into a one-dimensional tensor. If start_dim or end_dim are passed, only dimensions starting with start_dim and ending with end_dim are flattened. The order of elements in input is unchanged.

Parameters

  • start_dim (number) β€” the first dim to flatten
  • end_dim (number) β€” the last dim to flatten

Returns: Tensor β€” The flattened tensor.

Tensor.view(dims)

Returns a new tensor with the same data as the self tensor but of a different shape.

Parameters

  • dims (...number) β€” the desired size

Returns: Tensor β€” The tensor with the same data but different shape

Tensor.neg_()

In-place version of Tensor.neg

Returns: Tensor β€” this, with every element negated.

Tensor.neg()

Returns a new tensor with the negative of the elements of this tensor.

Returns: Tensor β€” the output tensor.

Tensor.gt(val)

Computes input > val element-wise.

Parameters

  • val (number) β€” The value to compare with.

Returns: Tensor β€” A boolean tensor that is true where input is greater than other and false elsewhere.

Tensor.lt(val)

Computes input < val element-wise.

Parameters

  • val (number) β€” The value to compare with.

Returns: Tensor β€” A boolean tensor that is true where input is less than other and false elsewhere.

Tensor.clamp_(min, max)

In-place version of Tensor.clamp

Parameters

  • min (number) β€” lower-bound of the range to be clamped to
  • max (number) β€” upper-bound of the range to be clamped to

Returns: Tensor β€” the output tensor.

Tensor.clamp(min, max)

Clamps all elements in input into the range [ min, max ]

Parameters

  • min (number) β€” lower-bound of the range to be clamped to
  • max (number) β€” upper-bound of the range to be clamped to

Returns: Tensor β€” the output tensor.

Tensor.round_()

In-place version of Tensor.round

Tensor.round()

Rounds elements of input to the nearest integer.

Returns: Tensor β€” the output tensor.

Tensor.mean([dim], [keepdim])

Returns the mean value of each row of this tensor in the given dimension dim.

Parameters

  • dim (number | null) optional β€” defaults to null β€” the dimension to reduce. If null, the mean of all elements is computed.
  • keepdim (boolean) optional β€” defaults to false β€” whether the output tensor has dim retained or not.

Returns: Tensor β€” A new tensor with means taken along the specified dimension.

Tensor.min([dim], [keepdim])

Returns the minimum value of each row of this tensor in the given dimension dim.

Parameters

  • dim (number | null) optional β€” defaults to null β€” the dimension to reduce. If null, the minimum of all elements is computed.
  • keepdim (boolean) optional β€” defaults to false β€” whether the output tensor has dim retained or not.

Returns: Tensor β€” A new tensor with minimum values taken along the specified dimension.

Tensor.max([dim], [keepdim])

Returns the maximum value of each row of this tensor in the given dimension dim.

Parameters

  • dim (number | null) optional β€” defaults to null β€” the dimension to reduce. If null, the maximum of all elements is computed.
  • keepdim (boolean) optional β€” defaults to false β€” whether the output tensor has dim retained or not.

Returns: Tensor β€” A new tensor with maximum values taken along the specified dimension.

Tensor.argmin([dim], [keepdim])

Returns the index of the minimum value of all elements in this tensor.

Parameters

  • dim (number | null) optional β€” defaults to null β€” the dimension to reduce. Only null (reduce over all elements) is currently supported.
  • keepdim (boolean) optional β€” defaults to false β€” whether the output tensor has dim retained or not.

Returns: Tensor β€” An int64 scalar tensor containing the index of the minimum value.

Throws

  • Error β€” If dim is not null.

Tensor.argmax([dim], [keepdim])

Returns the index of the maximum value of all elements in this tensor.

Parameters

  • dim (number | null) optional β€” defaults to null β€” the dimension to reduce. Only null (reduce over all elements) is currently supported.
  • keepdim (boolean) optional β€” defaults to false β€” whether the output tensor has dim retained or not.

Returns: Tensor β€” An int64 scalar tensor containing the index of the maximum value.

Throws

  • Error β€” If dim is not null.

Tensor.repeat(repeats)

Repeats this tensor along the specified dimensions.

Parameters

  • repeats (...number) β€” The number of times to repeat this tensor along each dimension.

Returns: Tensor β€” The repeated tensor.

Throws

  • Error β€” If the number of repeats is less than the number of dimensions.

Tensor.tile(dims)

Constructs a tensor by repeating the elements of input. The dims argument specifies the number of repetitions in each dimension.

Parameters

  • dims (...number) β€” The number of repetitions per dimension.

Returns: Tensor β€” The tiled tensor.

Tensor.to(type)

Performs Tensor dtype conversion.

Parameters

  • type (DataType) β€” The desired data type.

Returns: Tensor β€” The converted tensor.

Functions

permute(tensor, axes)

Permutes a tensor according to the provided axes.

Parameters

  • tensor (any) β€” The input tensor to permute.
  • axes (number[]) β€” The axes to permute the tensor along.

Returns: Tensor β€” The permuted tensor.

interpolate(input, size, mode, align_corners)

Interpolates an Tensor to the given size.

Parameters

  • input (Tensor) β€” The input tensor to interpolate. Data must be channel-first (i.e., [c, h, w])
  • size (number[]) β€” The output size of the image
  • mode (string) β€” The interpolation mode
  • align_corners (boolean) β€” Whether to align corners.

Returns: Tensor β€” The interpolated tensor.

interpolate_4d(input, options)

Down/up samples the input. Inspired by https://pytorch.org/docs/stable/generated/torch.nn.functional.interpolate.html.

Parameters

  • input (Tensor) β€” the input tensor
  • options (Object) β€” the options for the interpolation
    • size ([number, number] | [number, number, number] | [number, number, number, number]) optional β€” defaults to null β€” output spatial size.
    • mode ('nearest' | 'bilinear' | 'bicubic') optional β€” defaults to 'bilinear' β€” algorithm used for upsampling

Returns: Promise<Tensor> β€” The interpolated tensor.

matmul(a, b)

Matrix product of two tensors. Inspired by https://pytorch.org/docs/stable/generated/torch.matmul.html

Parameters

  • a (Tensor) β€” the first tensor to be multiplied
  • b (Tensor) β€” the second tensor to be multiplied

Returns: Promise<Tensor> β€” The matrix product of the two tensors.

rfft(x, a)

Computes the one dimensional Fourier transform of real-valued input. Inspired by https://pytorch.org/docs/stable/generated/torch.fft.rfft.html

Parameters

  • x (Tensor) β€” the real input tensor
  • a (Tensor) β€” The dimension along which to take the one dimensional real FFT.

Returns: Promise<Tensor> β€” the output tensor.

topk(x, [k])

Returns the k largest elements of the given input tensor. Inspired by https://pytorch.org/docs/stable/generated/torch.topk.html

Parameters

  • x (Tensor) β€” the input tensor
  • k (number) optional β€” the k in β€œtop-k”

Returns: Promise<[Tensor, Tensor]> β€” the output tuple of (Tensor, LongTensor) of top-k elements and their indices.

slice(data, starts, ends, axes, [steps])

Slice a multidimensional float32 tensor.

Parameters

  • data (Tensor) β€” : Tensor of data to extract slices from
  • starts (number[]) β€” : 1-D array of starting indices of corresponding axis in axes
  • ends (number[]) β€” : 1-D array of ending indices (exclusive) of corresponding axis in axes
  • axes (number[]) β€” : 1-D array of axes that starts and ends apply to
  • steps (number[]) optional β€” : 1-D array of slice step of corresponding axis in axes.

Returns: Promise<Tensor> β€” Sliced data tensor.

mean_pooling(last_hidden_state, attention_mask)

Perform mean pooling of the last hidden state followed by a normalization step.

Parameters

  • last_hidden_state (Tensor) β€” Tensor of shape [batchSize, seqLength, embedDim]
  • attention_mask (Tensor) β€” Tensor of shape [batchSize, seqLength]

Returns: Tensor β€” Returns a new Tensor of shape [batchSize, embedDim].

layer_norm(input, normalized_shape, options)

Apply Layer Normalization for last certain number of dimensions.

Parameters

  • input (Tensor) β€” The input tensor
  • normalized_shape (number[]) β€” input shape from an expected input of size
  • options (Object) β€” The options for the layer normalization
    • eps (number) optional β€” defaults to 1e-5 β€” A value added to the denominator for numerical stability.

Returns: Tensor β€” The normalized tensor.

cat(tensors, dim)

Concatenates an array of tensors along a specified dimension.

Parameters

  • tensors (Tensor[]) β€” The array of tensors to concatenate.
  • dim (number) β€” The dimension to concatenate along.

Returns: Tensor β€” The concatenated tensor.

stack(tensors, dim)

Stack an array of tensors along a specified dimension.

Parameters

  • tensors (Tensor[]) β€” The array of tensors to stack.
  • dim (number) β€” The dimension to stack along.

Returns: Tensor β€” The stacked tensor.

std_mean(input, dim, correction, keepdim)

Calculates the standard deviation and mean over the dimensions specified by dim. dim can be a single dimension or null to reduce over all dimensions.

Parameters

  • input (Tensor) β€” the input tenso
  • dim (number | null) β€” the dimension to reduce. If None, all dimensions are reduced.
  • correction (number) β€” difference between the sample size and sample degrees of freedom. Defaults to Bessel’s correction, correction=1.
  • keepdim (boolean) β€” whether the output tensor has dim retained or not.

Returns: Tensor[] β€” A tuple of (std, mean) tensors.

mean(input, dim, keepdim)

Returns the mean value of each row of the input tensor in the given dimension dim.

Parameters

  • input (Tensor) β€” the input tensor.
  • dim (number | null) β€” the dimension to reduce.
  • keepdim (boolean) β€” whether the output tensor has dim retained or not.

Returns: Tensor β€” A new tensor with means taken along the specified dimension.

full(size, fill_value)

Creates a tensor of size size filled with fill_value. The tensor’s dtype is inferred from fill_value.

Parameters

  • size (number[]) β€” A sequence of integers defining the shape of the output tensor.
  • fill_value (number | bigint | boolean) β€” The value to fill the output tensor with.

Returns: Tensor β€” The filled tensor.

full_like(tensor, fill_value)

Creates a tensor with the same size as tensor, filled with fill_value. The tensor’s dtype is inferred from fill_value.

Parameters

  • tensor (Tensor) β€” The size of input will determine size of the output tensor.
  • fill_value (number | bigint | boolean) β€” The value to fill the output tensor with.

Returns: Tensor β€” The filled tensor.

ones(size)

Returns a tensor filled with the scalar value 1, with the shape defined by the variable argument size.

Parameters

  • size (number[]) β€” A sequence of integers defining the shape of the output tensor.

Returns: Tensor β€” The ones tensor.

ones_like(tensor)

Returns a tensor filled with the scalar value 1, with the same size as input.

Parameters

  • tensor (Tensor) β€” The size of input will determine size of the output tensor.

Returns: Tensor β€” The ones tensor.

zeros(size)

Returns a tensor filled with the scalar value 0, with the shape defined by the variable argument size.

Parameters

  • size (number[]) β€” A sequence of integers defining the shape of the output tensor.

Returns: Tensor β€” The zeros tensor.

zeros_like(tensor)

Returns a tensor filled with the scalar value 0, with the same size as input.

Parameters

  • tensor (Tensor) β€” The size of input will determine size of the output tensor.

Returns: Tensor β€” The zeros tensor.

rand(size)

Returns a tensor filled with random numbers from a uniform distribution on the interval [0, 1)

Parameters

  • size (number[]) β€” A sequence of integers defining the shape of the output tensor.

Returns: Tensor β€” The random tensor.

randn(size)

Returns a tensor filled with random numbers from a normal distribution with mean 0 and variance 1 (also called the standard normal distribution).

Parameters

  • size (number[]) β€” A sequence of integers defining the shape of the output tensor.

Returns: Tensor β€” The random tensor.

quantize_embeddings(tensor, precision)

Quantizes the embeddings tensor to binary or unsigned binary precision.

Parameters

  • tensor (Tensor) β€” The tensor to quantize.
  • precision ('binary' | 'ubinary') β€” The precision to use for quantization.

Returns: Tensor β€” The quantized tensor.

Type Definitions

DataType

Type: keyof typeof DataTypeMap

DataArray

Type: AnyTypedArray | any[]

NestArray

This creates a nested array of a given type and depth (see examples).

Update on GitHub