-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpredictor.go
140 lines (113 loc) · 3 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
package pytorch
// #include <stdio.h>
// #include <stdlib.h>
// #include "cbits/predictor.hpp"
//
// size_t size_of_tensor_ctx = sizeof(Torch_TensorContext);
import "C"
import (
"context"
"fmt"
"runtime"
"unsafe"
"github.com/Unknwon/com"
"github.com/pkg/errors"
"github.com/rai-project/dlframework/framework/options"
nvidiasmi "github.com/rai-project/nvidia-smi"
"github.com/rai-project/tracer"
"gorgonia.org/tensor"
)
type Predictor struct {
ctx C.Torch_PredictorContext
inputs []C.Torch_TensorContext
options *options.Options
}
func New(ctx context.Context, opts ...options.Option) (*Predictor, error) {
defer PanicOnError()
span, _ := tracer.StartSpanFromContext(ctx, tracer.MODEL_TRACE, "c_new")
defer span.Finish()
options := options.New(opts...)
modelFile := string(options.Graph())
if !com.IsFile(modelFile) {
return nil, errors.Errorf("file %s not found", modelFile)
}
device := fromDevice(options)
if device == UnknownDeviceKind {
return nil, errors.New("invalid device")
}
cModelFile := C.CString(modelFile)
defer C.free(unsafe.Pointer(cModelFile))
pred := &Predictor{
ctx: C.Torch_NewPredictor(
cModelFile,
C.Torch_DeviceKind(device),
),
options: options,
}
runtime.SetFinalizer(pred, (*Predictor).finalize)
return pred, GetError()
}
func fromDevice(opts *options.Options) DeviceKind {
device := CPUDeviceKind
if opts.UsesGPU() {
if !nvidiasmi.HasGPU {
return UnknownDeviceKind
}
device = CUDADeviceKind
}
return device
}
func (p *Predictor) Predict(ctx context.Context, inputs []tensor.Tensor) error {
if len(inputs) < 1 {
return fmt.Errorf("input nil or empty")
}
inputsLength := len(inputs)
inputSlice := make([]C.Torch_TensorContext, inputsLength)
for ii, input := range inputs {
dense, ok := input.(*tensor.Dense)
if !ok {
return errors.New("expecting a dense tensor")
}
inputSlice[ii] = toTensorCtx(dense, fromDevice(p.options))
}
predictSpan, _ := tracer.StartSpanFromContext(ctx, tracer.MODEL_TRACE, "c_predict")
defer predictSpan.Finish()
C.Torch_PredictorRun(p.ctx, &inputSlice[0], C.int(inputsLength))
return GetError()
}
func (p *Predictor) ReadPredictionOutput(ctx context.Context) ([]tensor.Tensor, error) {
span, _ := tracer.StartSpanFromContext(ctx, tracer.MODEL_TRACE, "c_read_predicted_output")
defer span.Finish()
cNumOutputs := int(C.Torch_PredictorNumOutputs(p.ctx))
if cNumOutputs == 0 {
return nil, errors.New("zero number of tensors")
}
cPredictions := C.Torch_PredictorGetOutput(p.ctx)
defer C.Torch_IValueDelete(cPredictions)
if cPredictions.itype == C.Torch_IValueTypeUnknown {
return nil, errors.New("empty predictions")
}
res := ivalueToTensor(cPredictions)
if err := GetError(); err != nil {
return nil, err
}
return res, nil
}
func (p *Predictor) finalize() {
if p == nil {
return
}
for _, input := range p.inputs {
C.Torch_DeleteTensor(input)
}
if p.ctx != nil {
C.Torch_PredictorDelete(p.ctx)
}
p.ctx = nil
}
func (p *Predictor) Close() {
p.finalize()
}
func init() {
C.InitPytorch()
}