Class Tensor<T>
Represents data in a multidimensional array-like structure.
Ownership and lifetime:
- Disposed needs to be called on the main thread.
- Ownership is always to the owner of the object.
Data Representation:
- TensorShape represents the data layout of the tensor
- Data is held by a tensorData (ITensorData) which can be on a given backend
- Data is stored in a flattened row major format
- Data can be pending (ie computation is being done in parallel)
- call CompleteAllPendingOperations for a blocking call to finish computing the tensor's data Data can be in a non readable type (GPU/NPU)
- Call CompleteAllPendingOperations to finish computing the tensor's data
- Call ReadbackAndClone or ReadBackAndCloneAsync to allow reading the tensor's data
Data manipulation
- ToReadOnlyArray returns a copy of the tensor's data
- dataOnBackend can be manipulated directly to avoid a unnecessary copy see ComputeTensorData/CPUTensorData for info
Implements
Inherited Members
Namespace: Unity.Sentis
Assembly: Unity.Sentis.dll
Syntax
public class Tensor<T> : Tensor, IDisposable where T : unmanaged
Type Parameters
Name | Description |
---|---|
T |
Constructors
Tensor(TensorShape, bool)
Initializes and returns a tensor with the specified shape
.
Declaration
public Tensor(TensorShape shape, bool clearOnInit = true)
Parameters
Type | Name | Description |
---|---|---|
TensorShape | shape | The shape of the tensor. |
bool | clearOnInit | Whether to clear the tensor data to zeros. |
Tensor(TensorShape, NativeArray<T>, int)
Initializes and returns a tensor with specified shape
and a native T array of srcData
data.
Declaration
public Tensor(TensorShape shape, NativeArray<T> srcData, int dataStartIndex = 0)
Parameters
Type | Name | Description |
---|---|---|
TensorShape | shape | The shape of the tensor. |
NativeArray<T> | srcData | The data elements of the tensor. |
int | dataStartIndex | The index of the first tensor element in the srcData array. |
Tensor(TensorShape, ITensorData)
Initializes and returns a Tensor with the specified shape
, an ITensorData
data
.
Declaration
public Tensor(TensorShape shape, ITensorData data)
Parameters
Type | Name | Description |
---|---|---|
TensorShape | shape | The shape of the tensor. |
ITensorData | data | The optional tensor data. |
Tensor(TensorShape, T[], int)
Initializes and returns a tensor with specified shape
and a T[] array of srcData
data.
Declaration
public Tensor(TensorShape shape, T[] srcData, int dataStartIndex = 0)
Parameters
Type | Name | Description |
---|---|---|
TensorShape | shape | The shape of the tensor. |
T[] | srcData | The data elements of the tensor. |
int | dataStartIndex | The index of the first tensor element in the srcData array. |
Properties
this[int]
Returns the tensor element at offset d0
.
Declaration
public T this[int d0] { get; set; }
Parameters
Type | Name | Description |
---|---|---|
int | d0 | Axis 0. |
Property Value
Type | Description |
---|---|
T |
this[int, int]
Returns the tensor element at offset (d1, d0)
, which is position d1 * stride0 + d0
.
Declaration
public T this[int d1, int d0] { get; set; }
Parameters
Type | Name | Description |
---|---|---|
int | d1 | Axis 1. |
int | d0 | Axis 0. |
Property Value
Type | Description |
---|---|
T |
this[int, int, int]
Returns the tensor element at offset (d2, d1, d0)
, which is position d2 * stride1 + d1 * stride0 + d0
.
Declaration
public T this[int d2, int d1, int d0] { get; set; }
Parameters
Type | Name | Description |
---|---|---|
int | d2 | Axis 2. |
int | d1 | Axis 1. |
int | d0 | Axis 0. |
Property Value
Type | Description |
---|---|
T |
this[int, int, int, int]
Returns the tensor element at offset (d3, d2, d1, d0)
, which is position d3 * stride2 + d2 * stride1 + d1 * stride0 + d0
in this tensor.
Declaration
public T this[int d3, int d2, int d1, int d0] { get; set; }
Parameters
Type | Name | Description |
---|---|---|
int | d3 | Axis 3. |
int | d2 | Axis 2. |
int | d1 | Axis 1. |
int | d0 | Axis 0. |
Property Value
Type | Description |
---|---|
T |
this[int, int, int, int, int]
Returns the tensor element at offset (d4, d3, d2, d1, d0)
, which is position d4 * stride3 + d3 * stride2 + d2 * stride1 + d1 * stride0 + d0
.
Declaration
public T this[int d4, int d3, int d2, int d1, int d0] { get; set; }
Parameters
Type | Name | Description |
---|---|---|
int | d4 | Axis 4. |
int | d3 | Axis 3. |
int | d2 | Axis 2. |
int | d1 | Axis 1. |
int | d0 | Axis 0. |
Property Value
Type | Description |
---|---|
T |
this[int, int, int, int, int, int]
Returns the tensor element at offset (d5, d4, d3, d2, d1, d0)
, which is position d5 * stride4 + d4 * stride3 + d3 * stride2 + d2 * stride1 + d1 * stride0 + d0
.
Declaration
public T this[int d5, int d4, int d3, int d2, int d1, int d0] { get; set; }
Parameters
Type | Name | Description |
---|---|---|
int | d5 | Axis 5. |
int | d4 | Axis 4. |
int | d3 | Axis 3. |
int | d2 | Axis 2. |
int | d1 | Axis 1. |
int | d0 | Axis 0. |
Property Value
Type | Description |
---|---|
T |
this[int, int, int, int, int, int, int]
Returns the tensor element at offset (d6, d5, d4, d3, d2, d1, d0)
, which is position d6 * stride5 + d5 * stride4 + d4 * stride3 + d3 * stride2 + d2 * stride1 + d1 * stride0 + d0
.
Declaration
public T this[int d6, int d5, int d4, int d3, int d2, int d1, int d0] { get; set; }
Parameters
Type | Name | Description |
---|---|---|
int | d6 | Axis 6. |
int | d5 | Axis 5. |
int | d4 | Axis 4. |
int | d3 | Axis 3. |
int | d2 | Axis 2. |
int | d1 | Axis 1. |
int | d0 | Axis 0. |
Property Value
Type | Description |
---|---|
T |
this[int, int, int, int, int, int, int, int]
Returns the tensor element at offset (d7, d6, d5, d4, d3, d2, d1, d0)
, which is position d7 * stride6 + d6 * stride5 + d5 * stride4 + d4 * stride3 + d3 * stride2 + d2 * stride1 + d1 * stride0 + d0
.
Declaration
public T this[int d7, int d6, int d5, int d4, int d3, int d2, int d1, int d0] { get; set; }
Parameters
Type | Name | Description |
---|---|---|
int | d7 | Axis 7. |
int | d6 | Axis 6. |
int | d5 | Axis 5. |
int | d4 | Axis 4. |
int | d3 | Axis 3. |
int | d2 | Axis 2. |
int | d1 | Axis 1. |
int | d0 | Axis 0. |
Property Value
Type | Description |
---|---|
T |
Methods
AsReadOnlyNativeArray()
Exposes the linear memory data representation of this tensor as a readonly NativeArray.
Declaration
public NativeArray<T>.ReadOnly AsReadOnlyNativeArray()
Returns
Type | Description |
---|---|
NativeArray<T>.ReadOnly | NativeArray of tensor data. |
AsReadOnlySpan()
Exposes the linear memory data representation of this tensor as a ReadOnlySpan.
Declaration
public ReadOnlySpan<T> AsReadOnlySpan()
Returns
Type | Description |
---|---|
ReadOnlySpan<T> | Span of tensor data. |
DownloadToArray()
Blocking Call to return a copy of linear memory representation of the data in this tensor.
the returned array is a deepcopy of the tensor, the caller of this methods is now responsible for it. If you modify the contents of the returned array, it will not modify the underlying tensor
Declaration
public T[] DownloadToArray()
Returns
Type | Description |
---|---|
T[] | T array copy of tensor data. |
DownloadToNativeArray()
Blocking Call to return a copy of linear memory representation of the data in this tensor.
the returned native array is a deepcopy of the tensor, the caller of this methods is now responsible for it. If you modify the contents of the returned native array, it will not modify the underlying tensor
Declaration
public NativeArray<T> DownloadToNativeArray()
Returns
Type | Description |
---|---|
NativeArray<T> | T native array copy of tensor data. |
ReadbackAndClone()
Blocking download task of the internal data.
Declaration
public Tensor<T> ReadbackAndClone()
Returns
Type | Description |
---|---|
Tensor<T> | returns cpu copy of the tensor. |
Reshape(TensorShape)
Changes the shape of a tensor without changing the backing data.
The new shape must fit in the allocated backend tensor data, and the data cannot be on the GPUPixel backend.
Declaration
public override void Reshape(TensorShape shape)
Parameters
Type | Name | Description |
---|---|---|
TensorShape | shape | The new shape for the tensor. |
Overrides
Upload(NativeArray<T>)
Uploads a contiguous block of tensor data to internal storage.
Declaration
public void Upload(NativeArray<T> srcData)
Parameters
Type | Name | Description |
---|---|---|
NativeArray<T> | srcData | The data to upload. |
Upload(T[])
Uploads a contiguous block of tensor data to internal storage.
Declaration
public void Upload(T[] srcData)
Parameters
Type | Name | Description |
---|---|---|
T[] | srcData | The data to upload. |