Tensor

A Tensor represents a multi-dimensional array of elements.

A Tensor represents a multi-dimensional array of elements and is depicted as a struct containing both the tensor's shape and a flattened array of its data. The generic Tensor is defined as follows:

struct Tensor<T> {
    shape: Span<usize>,
    data: Span<T>
}

Data types

Orion supports currently these tensor types.

Data typedtype

32-bit integer (signed)

Tensor<i32>

8-bit integer (signed)

Tensor<i8>

32-bit integer (unsigned)

Tensor<u32>

Fixed point (signed)

Tensor<FP8x23 | FP16x16 | FP64x64 | FP32x32>


TensorTrait

use orion::operators::tensor::TensorTrait;

TensorTrait defines the operations that can be performed on a Tensor.

functiondescription

Returns a new tensor with the given shape and data.

Returns a new tensor with the specified target shape and the same data as the input tensor.

Flattens the input tensor into a 2D tensor.

Generate a tensor with given value and shape.

Returns a new tensor with the axes rearranged according to the given permutation.

Retrieves the value at the specified indices of a Tensor.

Converts a multi-dimensional index to a one-dimensional index.

Converts a one-dimensional index to a multi-dimensional index.

Check if two tensors are equal element-wise.

Check if each element of the first tensor is greater than the corresponding element of the second tensor.

Check if each element of the first tensor is greater than or equal to the corresponding element of the second tensor.

Check if each element of the first tensor is less than the corresponding element of the second tensor.

Check if each element of the first tensor is less than or equal to the corresponding element of the second tensor.

Computes the logical OR of two tensors element-wise.

Computes the logical XOR of two tensors element-wise.

Computes the stride of each dimension in the tensor.

Produces one-hot tensor based on input.

Returns the maximum value in the tensor.

Returns the minimum value in the tensor.

Returns the minimum value in the tensor.

Returns the maximum value in the tensor.

Reduces a tensor by summing its elements along a specified axis.

Reduces a tensor to its products along specified axis.

Returns the index of the maximum value along the specified axis.

Returns the index of the minimum value along the specified axis.

Performs cumulative sum of the input elements along the given axis.

Performs matrix product of two tensors.

Computes the exponential of all elements of the input tensor.

Computes the natural log of all elements of the input tensor.

Computes the absolute value of all elements in the input tensor.

Computes the negation of all elements in the input tensor.

Rounds up the value of each element in the input tensor.

Computes the square root of all elements of the input tensor.

Computes the sine of all elements of the input tensor.

Computes the cosine of all elements of the input tensor.

Computes the arctangent (inverse of tangent) of all elements of the input tensor.

Computes the arcsine (inverse of sine) of all elements of the input tensor.

Computes the arccosine (inverse of cosine) of all elements of the input tensor.

Computes the hyperbolic sine of all elements of the input tensor.

Computes the hyperbolic tangent of all elements of the input tensor.

Computes the hyperbolic cosine of all elements of the input tensor.

Computes the inverse hyperbolic sine of all elements of the input tensor.

Computes the inverse hyperbolic cosine of all elements of the input tensor.

Produces a slice of the input tensor along multiple axes.

Concatenate a list of tensors into a single tensor.

Quantizes a Tensor to i8 using linear quantization.

Dequantizes an i8 Tensor using linear dequantization.

Performs the sum of two quantized i8 Tensors.

Performs the element-wise multiplication of quantized Tensors.

Performs the product of two quantized i8 Tensors.

Concatenate a list of tensors after dequantizing them with their respective scales and zero_points and returns the quantized result.

Applies the Leaky Relu operator to a quantized Tensor

Performs convolution on quantized Tensors

Gather entries of the axis dimension of data.

Produces indices of the elements that are non-zero (in row-major order - by dimension).

Removes dimensions of size 1 from the shape of a tensor.

Inserts single-dimensional entries to the shape of an input tensor.

Calculates the sign of the given input tensor element-wise.

Clip operator limits the given input within an interval.

Computes the logical AND of two tensors element-wise.

Return a Tensor with the same shape and contents as input.

Return elements chosen from x or y depending on condition.

Computes the bitwise AND of two tensors element-wise.

Computes the bitwise XOR of two tensors element-wise.

Computes the bitwise OR of two tensors element-wise.

Resizes the input tensor.

Computes the round value of all elements in the input tensor.

Computes the L1 norm of the input tensor's elements along the provided axes.

Returns the upper or lower triangular part of a tensor or a batch of 2D matrices.

Produces a copy of input data, and updates value to values specified by updates at specific index positions specified by indices.

Computes the sum square of the input tensor's elements along the provided axes.

Computes the L2 norm of the input tensor's elements along the provided axes.

GatherElements is an indexing operation that produces its output by indexing into the input data tensor at index positions determined by elements of the indices tensor.

Computes the min of the input tensor's elements along the provided axes.

Computes the mean of the input tensor's elements along the provided axes.

Pow takes input data (Tensor) and exponent Tensor, and produces one output data (Tensor) where the function f(x) = x^exponent, is applied to the data tensor elementwise.

Maps the values of a tensor element-wise to 0 or 1 based on the comparison against a threshold value.

Selects elements of the input tensor based on the indices passed applied to the last tensor axis.

Computes the min of the input tensor's elements along the provided axes.

Returns which elements of the input are NaN.

Maps infinity to true and other values to false.

Computes the logical negation of all elements in the input tensor.

Given data tensor of rank r >= 1, indices tensor of rank q >= 1, and batch_dims integer b, this operator gathers slices of data into an output tensor of rank q + r - indices_shape[-1] - 1 - b.

Computes the log sum of the input tensor's elements along the provided axes.

Computes the error function of the given input tensor element-wise.

Computes the log sum of the exponentials of the input tensor's elements along the provided axes.

computes the layer normalization of the input tensor.

Split a tensor into a list of tensors, along the specified ‘axis’.

RandomUniformLike generates a tensor with random values using a uniform distribution, matching the shape of the input tensor.

Split a tensor into a sequence of tensors, along the specified ‘axis’.

Generate a tensor containing a sequence of numbers that begin at start and extends by increments of delta up to limit (exclusive).

Generates a Hann window as described in the paper https://ieeexplore.ieee.org/document/1455106.

Generates a Hamming window as described in the paper https://ieeexplore.ieee.org/document/1455106.

Generates a Blackman window as described in the paper https://ieeexplore.ieee.org/document/1455106.

Reverse batch of sequences having different lengths specified by sequence_lens.

Constructs an optional-type value containing either an empty optional of a certain type specified by the attribute, or a non-empty value containing the input element.

Computes the Scale, Zero Point and FP32->8Bit conversion of FP32 Input data.

The output of the operation is produced by creating a copy of the input data, and then updating its value to values specified by updates at specific index positions specified by indices. Its output shape is the same as the shape of data

Maps each element in the input tensor to another value.

Bitwise shift operator performs element-wise operation. For each input element, if the attribute "direction" is "RIGHT", this operator moves its binary representation toward the right side so that the input value is effectively decreased. If the attribute "direction" is "LEFT", bits of binary representation moves toward the left side, which results the increase of its actual value.

Generate a 2D tensor (matrix) with ones on the diagonal and zeros everywhere else. Only 2D tensors are supported, i.e. input T1 must be of rank 2. The shape of the output tensor is the same as the input tensor.

Arithmetic Operations

Tensor implements arithmetic traits. This allows you to perform basic arithmetic operations using the associated operators. (+, -, *, / ). Tensors arithmetic operations supports broadcasting.

Two tensors are “broadcastable” if the following rules hold:

  • Each tensor has at least one dimension.

  • When iterating over the dimension sizes, starting at the trailing dimension, the dimension sizes must either be equal, one of them is 1, or one of them does not exist.

Examples

Element-wise add.

use core::array::{ArrayTrait, SpanTrait};
use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor, U32TensorAdd};

fn element_wise_add_example() -> Tensor<u32> {
    // We instantiate two 3D Tensors here.
    let tensor_1 = TensorTrait::new(
        shape: array![2, 2, 2].span(), data: array![0, 1, 2, 3, 4, 5, 6, 7].span(),
    );
    let tensor_2 = TensorTrait::new(
        shape: array![2, 2, 2].span(), data: array![0, 1, 2, 3, 4, 5, 6, 7].span(),
    );

    // We can add two tensors as follows.
    return tensor_1 + tensor_2;
}
>>> [[[0,2],[4,6]],[[8,10],[12,14]]]

Add two tensors of different shapes but compatible in broadcasting.

use core::array::{ArrayTrait, SpanTrait};
use orion::operators::tensor::{TensorTrait, Tensor, U32Tensor, U32TensorAdd};

fn broadcasting_add_example() -> Tensor<u32> {
    // We instantiate two 3D Tensors here.
    let tensor_1 = TensorTrait::new(
        shape: array![2, 2, 2].span(),
        data: array![0, 1, 2, 3, 4, 5, 6, 7].span(),
    );
    let tensor_2 = TensorTrait::new(
        shape: array![1, 2, 1].span(),
        data: array![10, 100].span(),
    );

    // We can add two tensors as follows.
    return tensor_1 + tensor_2;
}
>>> [[[10,11],[102,103]],[[14,15],[106,107]]]

Last updated