-
Notifications
You must be signed in to change notification settings - Fork 7
/
runtime.go
270 lines (223 loc) · 7.54 KB
/
runtime.go
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package rknnlite
/*
#include "rknn_api.h"
#include <stdlib.h>
*/
import "C"
import (
"fmt"
"os"
"unsafe"
)
// CoreMask wraps C.rknn_core_mask
type CoreMask int
// rknn_core_mask values used to target which cores on the NPU the model is run
// on. The rk3588 has three cores, auto will pick an idle core to run the model
// on, whilst the others specify the specific core or combined number of cores
// to run. For multi-core modes the following ops have better acceleration: Conv,
// DepthwiseConvolution, Add, Concat, Relu, Clip, Relu6, ThresholdedRelu, Prelu,
// and LeakyRelu. Other type of ops will fallback to Core0 to continue running
const (
NPUCoreAuto CoreMask = C.RKNN_NPU_CORE_AUTO
NPUCore0 CoreMask = C.RKNN_NPU_CORE_0
NPUCore1 CoreMask = C.RKNN_NPU_CORE_1
NPUCore2 CoreMask = C.RKNN_NPU_CORE_2
NPUCore01 CoreMask = C.RKNN_NPU_CORE_0_1
NPUCore012 CoreMask = C.RKNN_NPU_CORE_0_1_2
NPUSkipSetCore CoreMask = 9999
)
// ErrorCodes
type ErrorCodes int
// error code values returned by the C API
const (
Success ErrorCodes = C.RKNN_SUCC
ErrFail ErrorCodes = C.RKNN_ERR_FAIL
ErrTimeout ErrorCodes = C.RKNN_ERR_TIMEOUT
ErrDeviceUnavailable ErrorCodes = C.RKNN_ERR_DEVICE_UNAVAILABLE
ErrMallocFail ErrorCodes = C.RKNN_ERR_MALLOC_FAIL
ErrParamInvalid ErrorCodes = C.RKNN_ERR_PARAM_INVALID
ErrModelInvalid ErrorCodes = C.RKNN_ERR_MODEL_INVALID
ErrCtxInvalid ErrorCodes = C.RKNN_ERR_CTX_INVALID
ErrInputInvalid ErrorCodes = C.RKNN_ERR_INPUT_INVALID
ErrOutputInvalid ErrorCodes = C.RKNN_ERR_OUTPUT_INVALID
ErrDeviceMismatch ErrorCodes = C.RKNN_ERR_DEVICE_UNMATCH
ErrPreCompiledModel ErrorCodes = C.RKNN_ERR_INCOMPATILE_PRE_COMPILE_MODEL
ErrOptimizationVersion ErrorCodes = C.RKNN_ERR_INCOMPATILE_OPTIMIZATION_LEVEL_VERSION
ErrPlatformMismatch ErrorCodes = C.RKNN_ERR_TARGET_PLATFORM_UNMATCH
)
// String returns a readable description of the error code
func (e ErrorCodes) String() string {
switch e {
case Success:
return "execution successful"
case ErrFail:
return "execution failed"
case ErrTimeout:
return "execution timed out"
case ErrDeviceUnavailable:
return "device is unavailable"
case ErrMallocFail:
return "C memory allocation failed"
case ErrParamInvalid:
return "parameter is invalid"
case ErrModelInvalid:
return "model file is invalid"
case ErrCtxInvalid:
return "context is invalid"
case ErrInputInvalid:
return "input is invalid"
case ErrOutputInvalid:
return "output is invalid"
case ErrDeviceMismatch:
return "device mismatch, please update rknn sdk and npu driver/firmware"
case ErrPreCompiledModel:
return "the RKNN model uses pre_compile mode, but is not compatible with current driver"
case ErrOptimizationVersion:
return "the RKNN model optimization level is not compatible with current driver"
case ErrPlatformMismatch:
return "the RKNN model target platform is not compatible with the current platform"
default:
return fmt.Sprintf("unknown error code %d", e)
}
}
// Runtime defines the RKNN run time instance
type Runtime struct {
// ctx is the C runtime context
ctx C.rknn_context
// ioNum caches the IONumber of Model Input/Output tensors
ioNum IONumber
// inputAttrs caches the Input Tensor Attributes of the Model
inputAttrs []TensorAttr
// inputAttrs caches the Output Tensor Attributes of the Model
outputAttrs []TensorAttr
// wantFloat indicates if Outputs are converted to float32 or left as int8.
// default option is True
wantFloat bool
// inputTypeFloat32 indicates if we pass the input gocv.Mat's data as float32
// to the RKNN backend
inputTypeFloat32 bool
}
// NewRuntime returns a RKNN run time instance. Provide the full path and
// filename of the RKNN compiled model file to run.
func NewRuntime(modelFile string, core CoreMask) (*Runtime, error) {
r := &Runtime{
wantFloat: true,
}
err := r.init(modelFile)
if err != nil {
return nil, err
}
// setCoreMask is only supported on RK3588, allow skipping for other Rockchip models
// like RK3566
if core != NPUSkipSetCore {
err = r.setCoreMask(core)
if err != nil {
return nil, err
}
}
// cache IONumber
r.ioNum, err = r.QueryModelIONumber()
if err != nil {
return nil, err
}
// query Input tensors
r.inputAttrs, err = r.QueryInputTensors()
if err != nil {
return nil, err
}
// query Output tensors
r.outputAttrs, err = r.QueryOutputTensors()
if err != nil {
return nil, err
}
return r, nil
}
// init wraps C.rknn_init which initializes the RKNN context with the given
// model. The modelFile is the full path and filename of the RKNN compiled
// model file to run.
func (r *Runtime) init(modelFile string) error {
// check file exists in Go, before passing to C
info, err := os.Stat(modelFile)
if err != nil {
return fmt.Errorf("model file does not exist at %s, error: %w",
modelFile, err)
}
if info.IsDir() {
return fmt.Errorf("model file is a directory")
}
// convert the Go string to a C string
cModelFile := C.CString(modelFile)
defer C.free(unsafe.Pointer(cModelFile))
// call the C function.
ret := C.rknn_init(&r.ctx, unsafe.Pointer(cModelFile), 0, 0, nil)
if ret != C.RKNN_SUCC {
return fmt.Errorf("C.rknn_init call failed with code %d, error: %s",
ret, ErrorCodes(ret).String())
}
return nil
}
// setCoreMark wraps C.rknn_set_core_mask and specifies the NPU core configuration
// to run the model on
func (r *Runtime) setCoreMask(mask CoreMask) error {
ret := C.rknn_set_core_mask(r.ctx, C.rknn_core_mask(mask))
if ret != C.RKNN_SUCC {
return fmt.Errorf("C.rknn_set_core_mask failed with code %d, error: %s",
ret, ErrorCodes(ret).String())
}
return nil
}
// Close wraps C.rknn_destroy which unloads the RKNN model from the runtime and
// destroys the context releasing all C resources
func (r *Runtime) Close() error {
ret := C.rknn_destroy(r.ctx)
if ret != C.RKNN_SUCC {
return fmt.Errorf("C.rknn_destroy failed with code %d, error: %s",
ret, ErrorCodes(ret).String())
}
return nil
}
// SetWantFloat defines if the Model load requires Output tensors to be converted
// to float32 for post processing, or left as quantitized int8
func (r *Runtime) SetWantFloat(val bool) {
r.wantFloat = val
}
// SetInputTypeFloat32 defines if the Model requires the Inference() function to
// pass the gocv.Mat's as float32 data to RKNN backend. Setting this overrides
// the default behaviour to pass gocv.Mat data as Uint8
func (r *Runtime) SetInputTypeFloat32(val bool) {
r.inputTypeFloat32 = val
}
// SDKVersion represents the C.rknn_sdk_version struct
type SDKVersion struct {
DriverVersion string
APIVersion string
}
// SDKVersion returns the RKNN API and Driver versions
func (r *Runtime) SDKVersion() (SDKVersion, error) {
// prepare the structure to receive the SDK version info
var cSdkVer C.rknn_sdk_version
// call the C function
ret := C.rknn_query(
r.ctx,
C.RKNN_QUERY_SDK_VERSION,
unsafe.Pointer(&cSdkVer),
C.uint(C.sizeof_rknn_sdk_version),
)
if ret != C.RKNN_SUCC {
return SDKVersion{}, fmt.Errorf("rknn_query failed with return code %d", int(ret))
}
// convert the C rknn_sdk_version to Go rknn_sdk_version
version := SDKVersion{
DriverVersion: C.GoString(&(cSdkVer.drv_version[0])),
APIVersion: C.GoString(&(cSdkVer.api_version[0])),
}
return version, nil
}
// InputAttrs returns the loaded model's input tensor attributes
func (r *Runtime) InputAttrs() []TensorAttr {
return r.inputAttrs
}
// OutputAttrs returns the loaded model's output tensor attributes
func (r *Runtime) OutputAttrs() []TensorAttr {
return r.outputAttrs
}