docs.unity3d.com
    Show / Hide Table of Contents

    Access tensor data directly

    To avoid Sentis downloading a tensor to a cache when you access a tensor, or when you need to pass a tensor between multiple models, you can read from and write to the tensor data directly in memory instead.

    Refer to Tensor fundamentals in Sentis for more information about how Sentis stores tensor data.

    You can also use IOps methods to do complicated tensor operations with a Sentis back end type that uses compute shaders or Burst. For example, you can use IOps to do matrix multiplication. Refer to Do operations on tensors for more information.

    Check where the data for a tensor is stored

    Use the tensorOnDevice.deviceType property of a tensor to check where the tensor data is stored. The property is either CPU or GPU.

    For example:

    using UnityEngine;
    using Unity.Sentis;
    
    public class CheckTensorLocation : MonoBehaviour
    {
    
        public Texture2D inputTexture;
    
        void Start()
        {
            // Create input data as a tensor
            Tensor inputTensor = TextureConverter.ToTensor(inputTexture);
    
            // Check if the tensor is stored in CPU or GPU memory, and write to the Console window.
            Debug.Log(inputTensor.tensorOnDevice.deviceType);
        }
    }
    

    If you want to force a tensor to the other device, use the following:

    • ComputeTensorData.Pin to force a tensor into GPU compute shader memory in a ComputeBuffer.
    • BurstTensorData.Pin to force a tensor into CPU memory for a Burst job.
    • ArrayTensorData.Pin to force a tensor into CPU memory as a NativeArray.

    For example:

    // Create a tensor
    TensorFloat inputTensor = TensorFloat.Zeros(new TensorShape(1, 3, 2, 2));
    
    // Force the tensor into GPU memory
    ComputeTensorData gpuTensor = ComputeTensorData.Pin(inputTensor);
    

    If the tensor data is already on the device you force it to, the method is a passthrough.

    Access a tensor in GPU memory

    To access a tensor in GPU memory, first get the tensor data as a ComputeTensorData object. You can do either of the following to get a ComputeTensorData object:

    • Use the return value of ComputeTensorData.Pin.
    • Use myTensor.tensorOnDevice as ComputeTensorData.

      For example:

      // Create a tensor
      TensorFloat inputTensor = TensorFloat.Zeros(new TensorShape(1, 3, 2, 2));
      
      // Get the tensor data as a ComputeTensorData object
      ComputeTensorData gpuTensor = inputTensor.tensorOnDevice as ComputeTensorData;
      

    You can then use the buffer property of the ComputeTensorData object and a compute shader to access the tensor data in the compute buffer directly. Refer to ComputeBuffer in the Unity API reference for more information about how to access a compute buffer.

    Refer to the AsyncReadback/AsyncReadBackCompute example in the sample scripts for a working example.

    Access a tensor in CPU memory

    To access a tensor in CPU memory, first get the tensor data as a BurstTensorData or ArrayTensorData object.

    Get a BurstTensorData object

    You can do either of the following to get a BurstTensorData object:

    • Use the return value of BurstTensorData.Pin.
    • Use myTensor.tensorOnDevice as BurstTensorData.

    You can then use the object in a Burst function like IJobParallelFor to read from and write to the tensor data. You can also use the fence and reuse properties of the object to force your code to wait for a Burst job to finish.

    Refer to the following:

    • The CSharpJob example in the sample scripts for a working example.
    • The Burst documentation.

    Get an ArrayTensorData object

    You can do either of the following to get an ArrayTensorData object:

    • Use the return value of ArrayTensorData.Pin.
    • Use myTensor.tensorOnDevice as ArrayTensorData.

    You can then use methods in the NativeTensorArray class to read from and write to the tensor data as a native array.

    Refer to NativeArray in the Unity API reference for more information about how to use a native array.

    Additional resources

    • Tensor fundamentals in Sentis
    • Do operations on tensors

    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
    • Check where the data for a tensor is stored
    • Access a tensor in GPU memory
    • Access a tensor in CPU memory
      • Get a BurstTensorData object
      • Get an ArrayTensorData object
    • Additional resources
    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