Create an engine to run a model
To run a model, you need to create a worker. A worker is the engine that breaks the model down into executable tasks and schedules the tasks to run on a back end, usually 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
Sentis provides CPU and GPU back end types. To understand how Sentis executes operations using the different back ends, refer to How Sentis runs a model.
If a back end type doesn't support a Sentis layer in a model, the worker will assert. Refer to Supported ONNX operators for more information.
Among the backend types, BackendType.GPUCompute
, BackendType.GPUCommandBuffer
, and BackendType.CPU
are the fastest. Only resort to using BackendType.GPUPixel
if the platform does not support compute shaders. To check if your runtime platform supports compute shaders, use SystemInfo.supportsComputeShaders.
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.
The speed of model execution depends on the platform's support for multithreading in Burst or its full support for compute shaders. You can profile a model to understand the performance of a model.