docs.unity3d.com
    Show / Hide Table of Contents

    Create an engine to run a model

    To run a model, create a worker. A worker is the engine that breaks the model down into executable tasks and schedules the tasks to run on the GPU or CPU.

    A worker is an instance of an IWorker object.

    Create a Worker

    Use WorkerFactory.CreateWorker to create a worker. You must specify a back end type, which tells Sentis where to run the worker, and a runtime model.

    For example, the following code creates a worker that runs on the GPU using Sentis compute shaders.

    using UnityEngine;
    using Unity.Sentis;
    
    public class CreateWorker : MonoBehaviour
    {
    
        ModelAsset modelAsset;
        Model runtimeModel;
        IWorker worker;
    
        void Start()
        {
            runtimeModel = ModelLoader.Load(modelAsset);
            worker = WorkerFactory.CreateWorker(BackendType.GPUCompute, runtimeModel);
        }
    }
    

    Back end types

    Back end type Runs on Compatible platforms Compatible tensor data types
    BackendType.CPU CPU, using Burst All float and int
    BackendType.GPUCompute GPU, using Sentis compute shaders Platforms that support compute shaders. Use SystemInfo.supportsComputeShaders to check if a platform supports compute shaders. float and int
    BackendType.GPUPixel GPU, using Sentis pixel shaders All float

    If a back end type doesn't support a Sentis layer in a model, the worker falls back to running on the CPU for that layer. Refer to Supported ONNX operators for more information.

    BackendType.GPUCompute and BackendType.CPU are the fastest back end types, so use BackendType.GPUPixel only if a platform doesn't support compute shaders.

    If you use BackendType.CPU with WebGL, Burst compiles to WebAssembly code which might be slow. Refer to Getting started with WebGL development for more information.

    How fast a model runs depends on how well a platform supports multithreading for Burst, or how fully it supports compute shaders. You can profile a model to understand the performance of a model.

    Additional resources

    • Create a runtime model
    • Supported ONNX operators
    • Run a model
    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