File size: 1,238 Bytes
6ac63e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using UnityEngine;
using Unity.InferenceEngine;

public class RunQuantizedModel : MonoBehaviour
{
    // 1. Download a .sentis or .onnx model you want to quantize and bring into your Unity Project.
    // 2. Open the Editor window 'Sentis > Sample > Quantize and Save Model' and reference your model as the source model.
    // 3. Select the desired quantization type and click 'Quantize and Save'.

    // Reference your quantized tiny stories here in the RunQuantizedModel scene.
    [SerializeField]
    ModelAsset modelAsset;
    Worker m_Worker;
    Tensor m_Input;

    const int maxTokens = 100;

    void OnEnable()
    {
        // Load the quantized model as any other Sentis model.
        var model = ModelLoader.Load(modelAsset);
        m_Worker = new Worker(model, BackendType.GPUCompute);

        // Initialize input.
        m_Input = new Tensor<int>(new TensorShape(1, maxTokens));
    }

    void Update()
    {
        // Execute worker and peek output as with any other Sentis model.
        m_Worker.Schedule(m_Input);
        var output = m_Worker.PeekOutput() as Tensor<float>;
    }

    void OnDisable()
    {
        // Clean up Sentis resources.
        m_Worker.Dispose();
        m_Input.Dispose();
    }
}