Class Tensor | Barracuda | 0.7.1-preview
docs.unity3d.com
    Show / Hide Table of Contents

    Class Tensor

    Inheritance
    Object
    Tensor
    Inherited Members
    Object.Equals(Object)
    Object.Equals(Object, Object)
    Object.ReferenceEquals(Object, Object)
    Object.GetHashCode()
    Object.GetType()
    Object.MemberwiseClone()
    Namespace: Unity.Barracuda
    Syntax
    public class Tensor : IDisposable

    Constructors

    Tensor(Int32, Int32, Int32, Int32, Single[], String)

    Create a Tensor of shape [N,H,W,C], an array of data srcData and an optional name n srcData should be of size bhw*ch

    Declaration
    public Tensor(int b, int h, int w, int ch, float[] srcData, string n = "")
    Parameters
    Type Name Description
    Int32 b
    Int32 h
    Int32 w
    Int32 ch
    Single[] srcData
    String n

    Tensor(Int32, Int32, Int32, Int32, Single[][], String)

    Create a Tensor of shape [N,H,W,C], an array of data srcData and an optional name n srcData should be of size bhw*ch

    Declaration
    public Tensor(int b, int h, int w, int ch, float[][] srcData, string n = "")
    Parameters
    Type Name Description
    Int32 b
    Int32 h
    Int32 w
    Int32 ch
    Single[][] srcData
    String n

    Tensor(Int32, Int32, Int32, Int32, String)

    Create an uninitialized Tensor of shape [N,H,W,C].

    Declaration
    public Tensor(int b, int h, int w, int ch, string n = "")
    Parameters
    Type Name Description
    Int32 b
    Int32 h
    Int32 w
    Int32 ch
    String n

    Tensor(Int32, Int32, Int32, Int32, ITensorAllocator)

    Create an uninitialized Tensor of shape [N,H,W,C] and ITensorAllocator a.

    Declaration
    public Tensor(int b, int h, int w, int ch, ITensorAllocator a)
    Parameters
    Type Name Description
    Int32 b
    Int32 h
    Int32 w
    Int32 ch
    ITensorAllocator a

    Tensor(Int32, Int32, Int32, Int32, ITensorData, String)

    Create a Tensor of shape [N,H,W,C], a ITensorData d and an optional name n srcData should be of size bhw*ch

    Declaration
    public Tensor(int b, int h, int w, int ch, ITensorData d, string n = "")
    Parameters
    Type Name Description
    Int32 b
    Int32 h
    Int32 w
    Int32 ch
    ITensorData d
    String n

    Tensor(Int32, Int32, Int32, Int32, ITensorData, ITensorAllocator)

    Create a Tensor of shape [N,H,W,C], a ITensorData d and a ITensorAllocator a

    Declaration
    public Tensor(int b, int h, int w, int ch, ITensorData d, ITensorAllocator a)
    Parameters
    Type Name Description
    Int32 b
    Int32 h
    Int32 w
    Int32 ch
    ITensorData d
    ITensorAllocator a

    Tensor(Int32, Int32, Int32, Int32, UnityEngine.ComputeBuffer, String)

    Create a Tensor of shape [N,H,W,C], associated ComputeBuffer srcBuffer filled with tensor values, and an optional name n srcBuffer should be larger than bhw*ch

    Declaration
    public Tensor(int b, int h, int w, int ch, UnityEngine.ComputeBuffer srcBuffer, string n = "")
    Parameters
    Type Name Description
    Int32 b
    Int32 h
    Int32 w
    Int32 ch
    UnityEngine.ComputeBuffer srcBuffer
    String n

    Tensor(Int32, Int32, Single[], String)

    Create a Tensor of shape [N,1,1,C], an array of data srcData and an optional name n srcData should be of size b*ch

    Declaration
    public Tensor(int b, int ch, float[] srcData, string n = "")
    Parameters
    Type Name Description
    Int32 b
    Int32 ch
    Single[] srcData
    String n

    Tensor(Int32, Int32, Single[][], String)

    Create a Tensor of shape [N,1,1,C], an array of data srcData and an optional name n srcData should be of size b*ch

    Declaration
    public Tensor(int b, int ch, float[][] srcData, string n = "")
    Parameters
    Type Name Description
    Int32 b
    Int32 ch
    Single[][] srcData
    String n

    Tensor(Int32, Int32, String)

    Create an uninitialized Tensor of shape [N,1,1,C].

    Declaration
    public Tensor(int b, int ch, string n = "")
    Parameters
    Type Name Description
    Int32 b
    Int32 ch
    String n

    Tensor(Int32, Int32, ITensorAllocator)

    Create an uninitialized Tensor of shape [N,1,1,C] and ITensorAllocator a.

    Declaration
    public Tensor(int b, int ch, ITensorAllocator a)
    Parameters
    Type Name Description
    Int32 b
    Int32 ch
    ITensorAllocator a

    Tensor(Int32, Int32, ITensorData, String)

    Create a Tensor of shape [N,1,1,C], a ITensorData d and an optional name n srcData should be of size b*ch

    Declaration
    public Tensor(int b, int ch, ITensorData d, string n = "")
    Parameters
    Type Name Description
    Int32 b
    Int32 ch
    ITensorData d
    String n

    Tensor(Int32, Int32, ITensorData, ITensorAllocator)

    Create a Tensor of shape [N,1,1,C], a ITensorData d and a ITensorAllocator a srcData should be of size b*ch

    Declaration
    public Tensor(int b, int ch, ITensorData d, ITensorAllocator a)
    Parameters
    Type Name Description
    Int32 b
    Int32 ch
    ITensorData d
    ITensorAllocator a

    Tensor(Int32, Int32, UnityEngine.ComputeBuffer, String)

    Create a Tensor of shape [N,1,1,C], associated ComputeBuffer srcBuffer filled with tensor values, and an optional name n srcBuffer should be larger than b*ch

    Declaration
    public Tensor(int b, int ch, UnityEngine.ComputeBuffer srcBuffer, string n = "")
    Parameters
    Type Name Description
    Int32 b
    Int32 ch
    UnityEngine.ComputeBuffer srcBuffer
    String n

    Tensor(Int32[], Single[], String)

    Create a Tensor from a shape s, an array of data srcData and an optional name n s should be of size 4, order is [N,H,W,C]. srcData should be of size s[0]s[1]s[2]*s[3].

    Declaration
    public Tensor(int[] s, float[] srcData, string n = "")
    Parameters
    Type Name Description
    Int32[] s
    Single[] srcData
    String n

    Tensor(Int32[], Single[][], String)

    Create a Tensor from a shape s, an array of data srcData and an optional name n s should be of size 4, order is [N,H,W,C]. srcData should be of size s[0]s[1]s[2]*s[3].

    Declaration
    public Tensor(int[] s, float[][] srcData, string n = "")
    Parameters
    Type Name Description
    Int32[] s
    Single[][] srcData
    String n

    Tensor(Int32[], String)

    Create an uninitialized Tensor from a shape s. s should be of size 4, order is [N,H,W,C]

    Declaration
    public Tensor(int[] s, string n = "")
    Parameters
    Type Name Description
    Int32[] s
    String n

    Tensor(Int32[], ITensorAllocator)

    Create an uninitialized Tensor from a shape s and ITensorAllocator a s should be of size 4, order is [N,H,W,C].

    Declaration
    public Tensor(int[] s, ITensorAllocator a)
    Parameters
    Type Name Description
    Int32[] s
    ITensorAllocator a

    Tensor(Int32[], ITensorData, String)

    Create a Tensor from a shape s, a ITensorData d and an optional name n s should be of size 4, order is [N,H,W,C].

    Declaration
    public Tensor(int[] s, ITensorData d, string n = "")
    Parameters
    Type Name Description
    Int32[] s
    ITensorData d
    String n

    Tensor(Int32[], ITensorData, ITensorAllocator)

    Create a Tensor from a shape s, a ITensorData d and a ITensorAllocator a s should be of size 4, order is [N,H,W,C].

    Declaration
    public Tensor(int[] s, ITensorData d, ITensorAllocator a)
    Parameters
    Type Name Description
    Int32[] s
    ITensorData d
    ITensorAllocator a

    Tensor(Int32[], UnityEngine.ComputeBuffer, String)

    Create a Tensor from a shape s, associated ComputeBuffer srcBuffer filled with tensor values, and an optional name n s should be of size 4, order is [N,H,W,C]. srcBuffer should be larger than s[0]s[1]s[2]*s[3].

    Declaration
    public Tensor(int[] s, UnityEngine.ComputeBuffer srcBuffer, string n = "")
    Parameters
    Type Name Description
    Int32[] s
    UnityEngine.ComputeBuffer srcBuffer
    String n

    Tensor(String)

    Create an uninitialized Tensor with a shape of [1,1,1,1].

    Declaration
    public Tensor(string n = "")
    Parameters
    Type Name Description
    String n

    Tensor(ITensorAllocator)

    Create an uninitialized Tensor with a shape of [1,1,1,1] and ITensorAllocator a

    Declaration
    public Tensor(ITensorAllocator a)
    Parameters
    Type Name Description
    ITensorAllocator a

    Tensor(TensorShape, Single[], String)

    Create a Tensor of shape s, an array of data srcData and an optional name n srcData should be of size s.length.

    Declaration
    public Tensor(TensorShape s, float[] srcData, string n = "")
    Parameters
    Type Name Description
    TensorShape s
    Single[] srcData
    String n

    Tensor(TensorShape, Single[][], String)

    Create a Tensor of shape s, an array of data srcData and an optional name n srcData should be of size s.length.

    Declaration
    public Tensor(TensorShape s, float[][] srcData, string n = "")
    Parameters
    Type Name Description
    TensorShape s
    Single[][] srcData
    String n

    Tensor(TensorShape, String)

    Create an uninitialized Tensor of shape s.

    Declaration
    public Tensor(TensorShape s, string n = "")
    Parameters
    Type Name Description
    TensorShape s
    String n

    Tensor(TensorShape, ITensorAllocator)

    Create an uninitialized Tensor of shape s and ITensorAllocator a.

    Declaration
    public Tensor(TensorShape s, ITensorAllocator a)
    Parameters
    Type Name Description
    TensorShape s
    ITensorAllocator a

    Tensor(TensorShape, ITensorData, String)

    Create a Tensor of shape s, a ITensorData d and an optional name n

    Declaration
    public Tensor(TensorShape s, ITensorData d, string n = "")
    Parameters
    Type Name Description
    TensorShape s
    ITensorData d
    String n

    Tensor(TensorShape, ITensorData, ITensorAllocator)

    Create a Tensor of shape s, a ITensorData d and a ITensorAllocator a

    Declaration
    public Tensor(TensorShape s, ITensorData d, ITensorAllocator a)
    Parameters
    Type Name Description
    TensorShape s
    ITensorData d
    ITensorAllocator a

    Tensor(TensorShape, UnityEngine.ComputeBuffer, String)

    Create a Tensor of shape s, associated ComputeBuffer srcBuffer filled with tensor values, and an optional name n srcBuffer should be larger than s.length.

    Declaration
    public Tensor(TensorShape s, UnityEngine.ComputeBuffer srcBuffer, string n = "")
    Parameters
    Type Name Description
    TensorShape s
    UnityEngine.ComputeBuffer srcBuffer
    String n

    Tensor(UnityEngine.Texture, Int32, String)

    Create a Tensor from a texture, shape is [1, texture.height, texture.width, channels=3]

    Declaration
    public Tensor(UnityEngine.Texture srcTexture, int channels = 3, string n = "")
    Parameters
    Type Name Description
    UnityEngine.Texture srcTexture
    Int32 channels
    String n

    Tensor(UnityEngine.Texture[], Int32, String)

    Create a Tensor from multiple texture, shape is [srcTextures.length, texture.height, texture.width, channels=3] All textures must be of the same size and dimension.

    Declaration
    public Tensor(UnityEngine.Texture[] srcTextures, int channels = 3, string n = "")
    Parameters
    Type Name Description
    UnityEngine.Texture[] srcTextures
    Int32 channels
    String n

    Fields

    name

    Return this tensor name.

    Declaration
    public string name
    Field Value
    Type Description
    String

    shape

    Return this tensor shape as [N,H,W,C].

    Declaration
    public readonly TensorShape shape
    Field Value
    Type Description
    TensorShape

    Properties

    allocator

    Return this tensor allocator, see interface ITensorAllocator.

    Declaration
    public ITensorAllocator allocator { get; }
    Property Value
    Type Description
    ITensorAllocator

    batch

    Return the number of batch.

    Declaration
    public int batch { get; }
    Property Value
    Type Description
    Int32

    channels

    Return the number of channels.

    Declaration
    public int channels { get; }
    Property Value
    Type Description
    Int32

    data

    Declaration
    public ITensorData data { get; }
    Property Value
    Type Description
    ITensorData

    dimensions

    Return the count of non-unit dimension of this tensor shape. For example [N,1,1,C] dimensions is 2.

    Declaration
    public int dimensions { get; }
    Property Value
    Type Description
    Int32

    flatHeight

    Return the number of batch.

    Declaration
    public int flatHeight { get; }
    Property Value
    Type Description
    Int32

    flatWidth

    Return the HWC.

    Declaration
    public int flatWidth { get; }
    Property Value
    Type Description
    Int32

    height

    Return the spatial height.

    Declaration
    public int height { get; }
    Property Value
    Type Description
    Int32

    Item[Int32]

    Access element at offset index in this Tensor. This will create a blocking read if cache is dirty.

    Declaration
    public float this[int index] { get; set; }
    Parameters
    Type Name Description
    Int32 index
    Property Value
    Type Description
    Single

    Item[Int32, Int32]

    Access element at index [N,0,0,C] in this Tensor. This will create a blocking read if cache is dirty.

    Declaration
    public float this[int b, int ch] { get; set; }
    Parameters
    Type Name Description
    Int32 b
    Int32 ch
    Property Value
    Type Description
    Single

    Item[Int32, Int32, Int32, Int32]

    Access element at index [N,H,W,C] in this Tensor. This will create a blocking read if cache is dirty.

    Declaration
    public float this[int b, int h, int w, int ch] { get; set; }
    Parameters
    Type Name Description
    Int32 b
    Int32 h
    Int32 w
    Int32 ch
    Property Value
    Type Description
    Single

    kernelCount

    Kernel dimension ordering is [H,W,C,K] for efficiency purpose. Return kernel count (aka the number of output channels of the associated operator).

    Declaration
    public int kernelCount { get; }
    Property Value
    Type Description
    Int32

    kernelDepth

    Kernel dimension ordering is [H,W,C,K] for efficiency purpose. Return kernel depth (aka the number of input channels of the associated operator).

    Declaration
    public int kernelDepth { get; }
    Property Value
    Type Description
    Int32

    kernelHeight

    Kernel dimension ordering is [H,W,C,K] for efficiency purpose. Return kernel height.

    Declaration
    public int kernelHeight { get; }
    Property Value
    Type Description
    Int32

    kernelWidth

    Kernel dimension ordering is [H,W,C,K] for efficiency purpose. Return kernel width.

    Declaration
    public int kernelWidth { get; }
    Property Value
    Type Description
    Int32

    length

    Return the total number of elements in this tensor.

    Declaration
    public int length { get; }
    Property Value
    Type Description
    Int32

    readonlyArray

    Return the cached linear memory representation of this tensor data. This will create a blocking read if cache is dirty. see also readonlyArrayOffset. IMPORTANT: This data should not be modified.

    Declaration
    public float[] readonlyArray { get; }
    Property Value
    Type Description
    Single[]

    readonlyArrayOffset

    Return the offset to use when accessing readonlyArray Always 0 at the moment.

    Declaration
    public int readonlyArrayOffset { get; }
    Property Value
    Type Description
    Int32

    tensorOnDevice

    Declaration
    public ITensorData tensorOnDevice { get; }
    Property Value
    Type Description
    ITensorData

    width

    Return the spatial width.

    Declaration
    public int width { get; }
    Property Value
    Type Description
    Int32

    Methods

    Axis(Int32)

    Allow to use negative axis to access tensorShape backward. axis should be from -rank to rank (exclusive).

    Declaration
    public int Axis(int axis)
    Parameters
    Type Name Description
    Int32 axis
    Returns
    Type Description
    Int32

    CastOnDevice(ITensorData)

    Cast a tensorData to this tensor, transferring ownership of on tensorData device memory to this tensor.

    Declaration
    public void CastOnDevice(ITensorData onDevice)
    Parameters
    Type Name Description
    ITensorData onDevice

    DeepCopy()

    Create a copy of the current Tensor, actively syncing there data in a blocking way.

    Declaration
    public Tensor DeepCopy()
    Returns
    Type Description
    Tensor

    Dispose()

    Dispose Tensor and associated memories.

    Declaration
    public virtual void Dispose()
    Implements
    IDisposable.Dispose()

    Finalize()

    Destructor will also dispose associated memories.

    Declaration
    protected void Finalize()

    Flatten(String)

    Create a flattened copy of the current Tensor ie of shape [N,1,1,HWC]

    Declaration
    public Tensor Flatten(string newName = null)
    Parameters
    Type Name Description
    String newName
    Returns
    Type Description
    Tensor

    FlushCache()

    Upload cache to device memory and delete it.

    Declaration
    public void FlushCache()

    Index(Int32, Int32)

    Given an element dimensions indices [N,0,0,C] return this element offset in memory.

    Declaration
    public int Index(int y, int x)
    Parameters
    Type Name Description
    Int32 y
    Int32 x
    Returns
    Type Description
    Int32

    Index(Int32, Int32, Int32, Int32)

    Given an element dimensions indices [N,H,W,C] return this element offset in memory.

    Declaration
    public int Index(int b, int h, int w, int ch)
    Parameters
    Type Name Description
    Int32 b
    Int32 h
    Int32 w
    Int32 ch
    Returns
    Type Description
    Int32

    IndexWithBroadcast(Int32, Int32, Int32, Int32)

    Given an element dimensions indices [N,H,W,C] with broadcast support, return this element offset in memory.

    Declaration
    public int IndexWithBroadcast(int b, int h, int w, int ch)
    Parameters
    Type Name Description
    Int32 b
    Int32 h
    Int32 w
    Int32 ch
    Returns
    Type Description
    Int32

    IndexWithClamp(Int32, Int32, Int32, Int32)

    Given an element dimensions indices [N,H,W,C] return this element offset in memory, clamping indices to tensor dimensions.

    Declaration
    public int IndexWithClamp(int b, int h, int w, int ch)
    Parameters
    Type Name Description
    Int32 b
    Int32 h
    Int32 w
    Int32 ch
    Returns
    Type Description
    Int32

    PinToDeviceAndDownloadFromIt(ITensorData)

    Allocate tensor on device if needed and download data to cache. See also PrepareCacheForAccess().

    Declaration
    public void PinToDeviceAndDownloadFromIt(ITensorData onDevice)
    Parameters
    Type Name Description
    ITensorData onDevice

    PinToDeviceAndUploadToIt(ITensorData, Boolean)

    Allocate tensor on device if needed and update data. By default cached copy of the data will be discarded when doing so, set forceInvalidateCache to false to keep the cache.

    Declaration
    public void PinToDeviceAndUploadToIt(ITensorData onDevice, bool forceInvalidateCache = true)
    Parameters
    Type Name Description
    ITensorData onDevice
    Boolean forceInvalidateCache

    PrepareCacheForAccess(Boolean)

    Populate the cache with on device data. Blocking read if blocking is true (default)

    Declaration
    public bool PrepareCacheForAccess(bool blocking = true)
    Parameters
    Type Name Description
    Boolean blocking
    Returns
    Type Description
    Boolean

    Reshape(TensorShape, String)

    Create a reshaped copy of the current Tensor. newShape.length must be equal to this.shape.length.

    Declaration
    public Tensor Reshape(TensorShape newShape, string newName = null)
    Parameters
    Type Name Description
    TensorShape newShape
    String newName
    Returns
    Type Description
    Tensor

    ShallowCopy(String)

    Create a copy of the current Tensor, sharing data storage with original tensor.

    Declaration
    public Tensor ShallowCopy(string newName = null)
    Parameters
    Type Name Description
    String newName
    Returns
    Type Description
    Tensor

    TakeOwnership()

    Remove system reference to this tensor, caller assume ownership.

    Declaration
    public void TakeOwnership()

    ToRenderTexture(Int32, Int32, Single, Single, Texture3D)

    Create a new RenderTexture from a slice/batch of a tensor.

    Declaration
    public UnityEngine.RenderTexture ToRenderTexture(int batch = 0, int fromChannel = 0, float scale = 1F, float bias = 0F, Texture3D lut = null)
    Parameters
    Type Name Description
    Int32 batch
    Int32 fromChannel
    Single scale
    Single bias
    Texture3D lut
    Returns
    Type Description
    UnityEngine.RenderTexture

    ToRenderTexture(UnityEngine.RenderTexture, Int32, Int32, Single, Single, Texture3D)

    Fill a RenderTexture with a slice/batch of a tensor.

    Declaration
    public void ToRenderTexture(UnityEngine.RenderTexture target, int batch = 0, int fromChannel = 0, float scale = 1F, float bias = 0F, Texture3D lut = null)
    Parameters
    Type Name Description
    UnityEngine.RenderTexture target
    Int32 batch
    Int32 fromChannel
    Single scale
    Single bias
    Texture3D lut

    ToRenderTexture(UnityEngine.RenderTexture, Int32, Int32, Vector4, Vector4, Texture3D)

    Fill a RenderTexture with a slice/batch of a tensor.

    Declaration
    public void ToRenderTexture(UnityEngine.RenderTexture target, int batch, int fromChannel, Vector4 scale, Vector4 bias, Texture3D lut = null)
    Parameters
    Type Name Description
    UnityEngine.RenderTexture target
    Int32 batch
    Int32 fromChannel
    Vector4 scale
    Vector4 bias
    Texture3D lut

    ToString()

    Declaration
    public override string ToString()
    Returns
    Type Description
    String
    Overrides
    Object.ToString()

    Unpin(Boolean)

    Remove tensor from device, will first sync the cache with device data.

    Declaration
    public ITensorData Unpin(bool disposeUnpinned = true)
    Parameters
    Type Name Description
    Boolean disposeUnpinned
    Returns
    Type Description
    ITensorData

    UnpinAndDisposeTensor()

    Remove tensor from device, and dispose it.

    Declaration
    public ITensorData UnpinAndDisposeTensor()
    Returns
    Type Description
    ITensorData

    Extension Methods

    TensorExtensions.TestInit(Tensor, Int32)
    TensorExtensions.TestInitCos(Tensor, Int32, Single)
    TensorExtensions.TestInitValue(Tensor, Single, Int32)
    TensorExtensions.AsFloats(Tensor)
    TensorExtensions.AsInts(Tensor)
    TensorExtensions.AsLongs(Tensor)
    TensorExtensions.DataToString(Tensor, Int32)
    TensorExtensions.Print(Tensor, String)
    TensorExtensions.PrintDataPart(Tensor, Int32, String)
    TensorExtensions.Equals(Tensor, Tensor)
    TensorExtensions.Approximately(Tensor, Tensor, Single, Int32)
    TensorExtensions.MaxDifference(Tensor, Tensor)
    TensorExtensions.ArgMax(Tensor)
    TensorExtensions.Reshape(Tensor, Int32[])
    TensorExtensions.ArgSort(Tensor)
    TensorExtensions.AdjustPadToKernel(Tensor, Tensor, Int32[], Int32[])
    TensorExtensions.AdjustPadToPool(Tensor, Int32[], Int32[], Int32[])
    TensorExtensions.AdjustPadToPool(Tensor, (Int32, Int32), Int32[], Int32[])
    Back to top
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023