-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpredictor.go
295 lines (244 loc) · 6.53 KB
/
predictor.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// +build linux
// +build !ppc64le
// +build !nogpu
// +build cgo
package tensorrt
// #include <stdlib.h>
// #include "cbits/predictor.hpp"
import "C"
import (
"context"
"fmt"
"runtime"
"strings"
"unsafe"
"github.com/Unknwon/com"
"github.com/k0kubun/pp"
"github.com/pkg/errors"
"github.com/rai-project/dlframework/framework/options"
cupti "github.com/rai-project/go-cupti"
"github.com/rai-project/tracer"
"github.com/rai-project/tracer/ctimer"
)
type Predictor struct {
handle C.PredictorHandle
inputNodes []options.Node
outputNodes []options.Node
options *options.Options
cu *cupti.CUPTI
}
func New(ctx context.Context, opts ...options.Option) (*Predictor, error) {
span, _ := tracer.StartSpanFromContext(ctx, tracer.MODEL_TRACE, "c_new")
defer span.Finish()
options := options.New(opts...)
var modelFiles []*C.char
modelFile := string(options.Graph())
if !com.IsFile(modelFile) {
return nil, errors.Errorf("file %s not found", modelFile)
}
modelFileString := C.CString(modelFile)
defer C.free(unsafe.Pointer(modelFileString))
modelFiles = append(modelFiles, modelFileString)
format := ClassifyModelFormat(modelFile)
if format == ModelFormatCaffe {
weightsFile := string(options.Weights())
if !com.IsFile(weightsFile) {
return nil, errors.Errorf("file %s not found", weightsFile)
}
weightsFileString := C.CString(weightsFile)
defer C.free(unsafe.Pointer(weightsFileString))
modelFiles = append(modelFiles, weightsFileString)
}
if len(options.InputNodes()) == 0 {
return nil, errors.Errorf("input nodes not found")
}
if len(options.OutputNodes()) == 0 {
return nil, errors.Errorf("output nodes not found")
}
inputNodes := options.InputNodes() // take the first input node
for _, n := range inputNodes {
if n.Key == "" {
return nil, errors.New("expecting a valid (non-empty) output layer name")
}
}
cInputNodes := makeCStringArray(inputNodes)
defer deleteCStringArray(cInputNodes)
outputNodes := options.OutputNodes()
for _, n := range outputNodes {
if n.Key == "" {
return nil, errors.New("expecting a valid (non-empty) output layer name")
}
}
cOutputNodes := makeCStringArray(outputNodes)
defer deleteCStringArray(cOutputNodes)
handle := C.NewTensorRTPredictor(
C.TensorRT_ModelFormat(ModelFormatCaffe),
(**C.char)(&modelFiles[0]),
C.TensorRT_DType(Float),
(**C.char)(&cInputNodes[0]),
C.int32_t(len(inputNodes)),
(**C.char)(&cOutputNodes[0]),
C.int32_t(len(outputNodes)),
C.int32_t(options.BatchSize()),
)
pred := &Predictor{
handle: handle,
inputNodes: inputNodes,
outputNodes: outputNodes,
options: options,
}
runtime.SetFinalizer(pred, func(p *Predictor) {
p.Close()
})
return pred, nil
}
func makeCStringArray(nds []options.Node) []*C.char {
res := make([]*C.char, len(nds))
for ii, nd := range nds {
res[ii] = C.CString(nd.Key)
}
return res
}
func deleteCStringArray(strs []*C.char) {
for ii := range strs {
C.free(unsafe.Pointer(strs[ii]))
}
}
func (p *Predictor) GetOptions() *options.Options {
return p.options
}
func (p *Predictor) Predict(ctx context.Context, data []float32) error {
if data == nil || len(data) < 1 {
return fmt.Errorf("intput data nil or empty")
}
cnamei := C.CString(p.inputNodes[0].Key)
defer C.free(unsafe.Pointer(cnamei))
cnameo := C.CString(p.outputNodes[0].Key)
defer C.free(unsafe.Pointer(cnameo))
span, _ := tracer.StartSpanFromContext(ctx, tracer.MODEL_TRACE, "c_predict")
defer span.Finish()
if p.GetOptions().TraceLevel() >= tracer.FRAMEWORK_TRACE {
p.StartProfiling("predict", "")
defer func() {
p.EndProfiling()
profBuffer, err := p.ReadProfile()
if err != nil {
panic(err)
}
t, err := ctimer.New(profBuffer)
if err != nil {
panic(err)
}
t.Publish(ctx, tracer.FRAMEWORK_TRACE)
}()
}
err := p.cuptiStart(ctx)
if err != nil {
return err
}
C.TenorRTPredictor_AddInput(
p.handle,
cnamei,
C.TensorRT_DType(Float),
unsafe.Pointer(&data[0]),
C.size_t(len(data)),
)
C.TenorRTPredictor_AddOutput(
p.handle,
cnameo,
C.TensorRT_DType(Float),
)
C.TenorRTPredictor_Run(p.handle)
C.TenorRTPredictor_Synchronize(p.handle)
p.cuptiClose()
return nil
}
func (p *Predictor) ReadPredictionOutputs(ctx context.Context) ([][]float32, error) {
span, _ := tracer.StartSpanFromContext(ctx, tracer.MODEL_TRACE, "c_read_prediction_output")
defer span.Finish()
numOutputs := int(C.TenorRTPredictor_GetNumOutputs(p.handle))
outputs := make([][]float32, numOutputs)
for ii := 0; ii < numOutputs; ii++ {
outputs[ii] = p.ReadPredictionOutput(p.outputNodes[ii].Key)
}
return outputs, nil
}
func prod(sz []int) int {
res := 1
for _, a := range sz {
res *= a
}
return res
}
// ReadPredictionOutput ...
func (p *Predictor) ReadPredictionOutput(name string) []float32 {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
var ndims int32
cdims := new(C.int32_t)
data := C.TenorRTPredictor_GetOutput(p.handle, cname, (*C.int32_t)(&ndims), (**C.int32_t)(&cdims))
dims := (*[1 << 30]C.int32_t)(unsafe.Pointer(cdims))[:ndims:ndims]
sz := 1
for ii := 0; ii < int(ndims); ii++ {
sz *= int(dims[ii])
}
return (*[1 << 30]float32)(unsafe.Pointer(data))[:sz:sz]
}
func (p *Predictor) Close() {
var nilPredictorHandle C.PredictorHandle
if p == nil || p.handle == nilPredictorHandle {
return
}
C.TenorRTPredictor_Delete(p.handle)
p.handle = nilPredictorHandle
}
func (p *Predictor) StartProfiling(name, metadata string) error {
cname := C.CString(name)
cmetadata := C.CString(metadata)
defer C.free(unsafe.Pointer(cname))
defer C.free(unsafe.Pointer(cmetadata))
C.TenorRTPredictor_StartProfiling(p.handle, cname, cmetadata)
return nil
}
func (p *Predictor) EndProfiling() error {
C.TenorRTPredictor_EndProfiling(p.handle)
return nil
}
func (p *Predictor) ReadProfile() (string, error) {
cstr := C.TenorRTPredictor_ReadProfiling(p.handle)
if cstr == nil {
return "", errors.New("failed to read nil profile")
}
defer C.free(unsafe.Pointer(cstr))
return C.GoString(cstr), nil
}
func dummyPP() {
pp.Println("dummy")
}
func (p *Predictor) cuptiStart(ctx context.Context) error {
opts := p.GetOptions()
if !opts.UsesGPU() || opts.TraceLevel() < tracer.SYSTEM_LIBRARY_TRACE {
return nil
}
metrics := []string{}
if opts.GPUMetrics() != "" {
metrics = strings.Split(opts.GPUMetrics(), ",")
}
cu, err := cupti.New(cupti.Context(ctx),
cupti.SamplingPeriod(0),
cupti.Metrics(metrics),
)
if err != nil {
return err
}
p.cu = cu
return nil
}
func (p *Predictor) cuptiClose() {
if p.cu == nil {
return
}
p.cu.Wait()
p.cu.Close()
p.cu = nil
}