Skip to content

Latest commit

 

History

History
192 lines (168 loc) · 15.3 KB

README.md

File metadata and controls

192 lines (168 loc) · 15.3 KB

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 type dtype
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.

function description
tensor.new Returns a new tensor with the given shape and data.
tensor.reshape Returns a new tensor with the specified target shape and the same data as the input tensor.
tensor.flatten Flattens the input tensor into a 2D tensor.
tensor.constant_of_shape Generate a tensor with given value and shape.
tensor.transpose Returns a new tensor with the axes rearranged according to the given permutation.
tensor.at Retrieves the value at the specified indices of a Tensor.
tensor.ravel_index Converts a multi-dimensional index to a one-dimensional index.
tensor.unravel_index Converts a one-dimensional index to a multi-dimensional index.
tensor.equal Check if two tensors are equal element-wise.
tensor.greater Check if each element of the first tensor is greater than the corresponding element of the second tensor.
tensor.greater_equal Check if each element of the first tensor is greater than or equal to the corresponding element of the second tensor.
tensor.less Check if each element of the first tensor is less than the corresponding element of the second tensor.
tensor.less_equal Check if each element of the first tensor is less than or equal to the corresponding element of the second tensor.
tensor.or Computes the logical OR of two tensors element-wise.
tensor.xor Computes the logical XOR of two tensors element-wise.
tensor.stride Computes the stride of each dimension in the tensor.
tensor.onehot Produces one-hot tensor based on input.
tensor.max_in_tensor Returns the maximum value in the tensor.
tensor.min_in_tensor Returns the minimum value in the tensor.
tensor.min Returns the minimum value in the tensor.
tensor.max Returns the maximum value in the tensor.
tensor.reduce_sum Reduces a tensor by summing its elements along a specified axis.
tensor.reduce_prod Reduces a tensor to its products along specified axis.
tensor.argmax Returns the index of the maximum value along the specified axis.
tensor.argmin Returns the index of the minimum value along the specified axis.
tensor.cumsum Performs cumulative sum of the input elements along the given axis.
tensor.matmul Performs matrix product of two tensors.
tensor.exp Computes the exponential of all elements of the input tensor.
tensor.log Computes the natural log of all elements of the input tensor.
tensor.abs Computes the absolute value of all elements in the input tensor.
tensor.neg Computes the negation of all elements in the input tensor.
tensor.ceil Rounds up the value of each element in the input tensor.
tensor.sqrt Computes the square root of all elements of the input tensor.
tensor.sin Computes the sine of all elements of the input tensor.
tensor.cos Computes the cosine of all elements of the input tensor.
tensor.atan Computes the arctangent (inverse of tangent) of all elements of the input tensor.
tensor.asin Computes the arcsine (inverse of sine) of all elements of the input tensor.
tensor.acos Computes the arccosine (inverse of cosine) of all elements of the input tensor.
tensor.sinh Computes the hyperbolic sine of all elements of the input tensor.
tensor.tanh Computes the hyperbolic tangent of all elements of the input tensor.
tensor.cosh Computes the hyperbolic cosine of all elements of the input tensor.
tensor.asinh Computes the inverse hyperbolic sine of all elements of the input tensor.
tensor.acosh Computes the inverse hyperbolic cosine of all elements of the input tensor.
tensor.slice Produces a slice of the input tensor along multiple axes.
tensor.concat Concatenate a list of tensors into a single tensor.
tensor.quantize_linear Quantizes a Tensor to i8 using linear quantization.
tensor.dequantize_linear Dequantizes an i8 Tensor using linear dequantization.
tensor.qlinear_add Performs the sum of two quantized i8 Tensors.
tensor.qlinear_mul Performs the element-wise multiplication of quantized Tensors.
tensor.qlinear_matmul Performs the product of two quantized i8 Tensors.
tensor.qlinear_concat Concatenate a list of tensors after dequantizing them with their respective scales and zero_points and returns the quantized result.
tensor.qlinear_leakyrelu Applies the Leaky Relu operator to a quantized Tensor
tensor.qlinear_conv Performs convolution on quantized Tensors
tensor.gather Gather entries of the axis dimension of data.
tensor.nonzero Produces indices of the elements that are non-zero (in row-major order - by dimension).
tensor.squeeze Removes dimensions of size 1 from the shape of a tensor.
tensor.unsqueeze Inserts single-dimensional entries to the shape of an input tensor.
tensor.sign Calculates the sign of the given input tensor element-wise.
tensor.clip Clip operator limits the given input within an interval.
tensor.and Computes the logical AND of two tensors element-wise.
tensor.identity Return a Tensor with the same shape and contents as input.
tensor.where Return elements chosen from x or y depending on condition.
tensor.bitwise_and Computes the bitwise AND of two tensors element-wise.
tensor.bitwise_xor Computes the bitwise XOR of two tensors element-wise.
tensor.bitwise_or Computes the bitwise OR of two tensors element-wise.
tensor.resize Resizes the input tensor.
tensor.round Computes the round value of all elements in the input tensor.
tensor.reduce_l1 Computes the L1 norm of the input tensor's elements along the provided axes.
tensor.trilu Returns the upper or lower triangular part of a tensor or a batch of 2D matrices.
tensor.scatter Produces a copy of input data, and updates value to values specified by updates at specific index positions specified by indices.
tensor.reduce_sum_square Computes the sum square of the input tensor's elements along the provided axes.
tensor.reduce_l2 Computes the L2 norm of the input tensor's elements along the provided axes.
tensor.gather_elements 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.
tensor.reduce_min Computes the min of the input tensor's elements along the provided axes.
tensor.reduce_mean Computes the mean of the input tensor's elements along the provided axes.
tensor.pow 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.
tensor.binarizer Maps the values of a tensor element-wise to 0 or 1 based on the comparison against a threshold value.
tensor.array_feature_extractor Selects elements of the input tensor based on the indices passed applied to the last tensor axis.
tensor.reduce_min Computes the min of the input tensor's elements along the provided axes.
tensor.is_nan Returns which elements of the input are NaN.
tensor.is_inf Maps infinity to true and other values to false.
tensor.not Computes the logical negation of all elements in the input tensor.
tensor.gather_nd 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.
tensor.reduce_log_sum Computes the log sum of the input tensor's elements along the provided axes.
tensor.erf Computes the error function of the given input tensor element-wise.
tensor.reduce_log_sum_exp Computes the log sum of the exponentials of the input tensor's elements along the provided axes.
tensor.layer_normalization computes the layer normalization of the input tensor.
tensor.split Split a tensor into a list of tensors, along the specified ‘axis’.
tensor.random_uniform_like RandomUniformLike generates a tensor with random values using a uniform distribution, matching the shape of the input tensor.
tensor.split_to_sequence Split a tensor into a sequence of tensors, along the specified ‘axis’.
tensor.range Generate a tensor containing a sequence of numbers that begin at start and extends by increments of delta up to limit (exclusive).
tensor.hann_window Generates a Hann window as described in the paper https://ieeexplore.ieee.org/document/1455106.
tensor.hamming_window Generates a Hamming window as described in the paper https://ieeexplore.ieee.org/document/1455106.
tensor.blackman_window Generates a Blackman window as described in the paper https://ieeexplore.ieee.org/document/1455106.
tensor.reverse_sequence Reverse batch of sequences having different lengths specified by sequence_lens.
tensor.optional 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.
tensor.dynamic_quantize_linear Computes the Scale, Zero Point and FP32->8Bit conversion of FP32 Input data.
tensor.scatter_nd 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
tensor.label_encoder Maps each element in the input tensor to another value.
tensor.bit_shift 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.
tensor.eye_like 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]]]