docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    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
    Inheritance
    object
    Tensor
    Tensor<T>
    Implements
    IDisposable
    Inherited Members
    Tensor.dataType
    Tensor.count
    Tensor.shape
    Tensor.dataOnBackend
    Tensor.backendType
    Tensor.AdoptTensorData(ITensorData, bool)
    Tensor.ReleaseTensorData()
    Tensor.IsReadbackRequestDone()
    Tensor.ReadbackRequest()
    Tensor.CompleteAllPendingOperations()
    Tensor.Dispose()
    Tensor.ToString()
    object.Equals(object)
    object.Equals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    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
    Tensor.Reshape(TensorShape)

    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.

    Implements

    IDisposable

    Did you find this page useful? Please give it a rating:

    Thanks for rating this page!

    Report a problem on this page

    What kind of problem would you like to report?

    • This page needs code samples
    • Code samples do not work
    • Information is missing
    • Information is incorrect
    • Information is unclear or confusing
    • There is a spelling/grammar error on this page
    • Something else

    Thanks for letting us know! This page has been marked for review based on your feedback.

    If you have time, you can provide more information to help us fix the problem faster.

    Provide more information

    You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:

    You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:

    You've told us there is information missing from this page. Please tell us more about what's missing:

    You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:

    You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:

    You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:

    You've told us this page has a problem. Please tell us more about what's wrong:

    Thank you for helping to make the Unity documentation better!

    Your feedback has been submitted as a ticket for our documentation team to review.

    We are not able to reply to every ticket submitted.

    In This Article
    • Constructors
      • Tensor(TensorShape, bool)
      • Tensor(TensorShape, NativeArray<T>, int)
      • Tensor(TensorShape, ITensorData)
      • Tensor(TensorShape, T[], int)
    • Properties
      • this[int]
      • this[int, int]
      • this[int, int, int]
      • this[int, int, int, int]
      • this[int, int, int, int, int]
      • this[int, int, int, int, int, int]
      • this[int, int, int, int, int, int, int]
      • this[int, int, int, int, int, int, int, int]
    • Methods
      • AsReadOnlyNativeArray()
      • AsReadOnlySpan()
      • DownloadToArray()
      • DownloadToNativeArray()
      • ReadbackAndClone()
      • Reshape(TensorShape)
      • Upload(NativeArray<T>)
      • Upload(T[])
    • Implements
    Back to top
    Copyright © 2025 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)