-
Notifications
You must be signed in to change notification settings - Fork 0
/
mlharness.go
268 lines (225 loc) · 6.63 KB
/
mlharness.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
package mlharness
import (
"context"
"fmt"
"runtime"
"strconv"
"github.com/c3sr/dldataset"
"github.com/c3sr/dlframework/steps"
"github.com/c3sr/go-python3"
"github.com/c3sr/mlharness/sut"
"github.com/c3sr/pipeline"
"github.com/c3sr/tracer"
opentracing "github.com/opentracing/opentracing-go"
)
var (
mlharnessSUT *sut.SUT
mlharnessDataset *dldataset.Dataset
rootSpan opentracing.Span
ctx context.Context
dataCount int
supportedTraceLevel = map[string]int{
"NO_TRACE": 0,
"APPLICATION_TRACE": 1,
"MODEL_TRACE": 2,
"FRAMEWORK_TRACE": 3,
"ML_LIBRARY_TRACE": 4,
"SYSTEM_LIBRARY_TRACE": 5,
"HARDWARE_TRACE": 6,
"FULL_TRACE": 7,
}
defaultChannelBuffer = 100000
)
// This needs to be call once from the python side in the beginning
func Initialize(backendName string, modelPath string, datasetPath string, count int,
useGPU bool, GPUID int, traceLevel string, batchSize int) (int, error) {
// Not from shared library
if !python3.Py_IsInitialized() {
python3.Py_Initialize()
if !python3.Py_IsInitialized() {
return 0, fmt.Errorf("Error initializing the python interpreter")
}
python3.PyEval_SaveThread()
}
if _, ok := supportedTraceLevel[traceLevel]; !ok {
return 0, fmt.Errorf("%s is not a supported trace level", traceLevel)
}
var err error
rootSpan, ctx = tracer.StartSpanFromContext(
context.Background(),
tracer.APPLICATION_TRACE,
"MLHarness",
)
if rootSpan == nil {
panic("invalid span")
}
fmt.Println("Start initializing SUT...")
mlharnessSUT, err = sut.NewSUT(ctx, backendName, modelPath, useGPU, GPUID, traceLevel, batchSize)
if err != nil {
return 0, err
}
fmt.Println("Finish initializing SUT...")
fmt.Println("Start initializing QSL...")
mlharnessDataset, err = dldataset.NewDataset(datasetPath, count)
if err != nil {
return 0, err
}
fmt.Println("Finish initializing QSL...")
tracer.SetLevel(tracer.NO_TRACE)
modelManifest, err := mlharnessSUT.GetModelManifest()
if err != nil {
return 0, err
}
if modelManifest.GetBeforePreprocess() != "" {
runtime.LockOSThread()
pyState := python3.PyGILState_Ensure()
python3.PyRun_SimpleString(modelManifest.GetBeforePreprocess())
pyMain := python3.PyImport_AddModule("__main__")
pyDict := python3.PyModule_GetDict(pyMain)
pyBeforePreprocess := python3.PyDict_GetItemString(pyDict, "before_preprocess")
pyCnt := pyBeforePreprocess.CallFunctionObjArgs()
if python3.PyLong_Check(pyCnt) {
dataCount = python3.PyLong_AsLong(pyCnt)
}
pyCnt.DecRef()
python3.PyGILState_Release(pyState)
runtime.UnlockOSThread()
}
if modelManifest.GetBeforePostprocess() != "" {
runtime.LockOSThread()
pyState := python3.PyGILState_Ensure()
python3.PyRun_SimpleString(modelManifest.GetBeforePostprocess())
pyMain := python3.PyImport_AddModule("__main__")
pyDict := python3.PyModule_GetDict(pyMain)
pyBeforePostprocess := python3.PyDict_GetItemString(pyDict, "before_postprocess")
pyBeforePostprocess.CallFunctionObjArgs().DecRef()
python3.PyGILState_Release(pyState)
runtime.UnlockOSThread()
}
if err := warmup(); err != nil {
return 0, err
}
tracer.SetLevel(tracer.LevelFromName(traceLevel))
return mlharnessDataset.Count(), nil
}
func warmup() error {
warmupSpan, issueCtx := tracer.StartSpanFromContext(
ctx,
tracer.APPLICATION_TRACE,
"Warmup Span",
)
if warmupSpan == nil {
panic("invalid issue query span")
}
defer warmupSpan.Finish()
fmt.Println("Start warmup...")
if err := mlharnessDataset.Load([]int{0}); err != nil {
return err
}
input := make(chan interface{}, defaultChannelBuffer)
opts := []pipeline.Option{pipeline.ChannelBuffer(defaultChannelBuffer)}
preProcessMethod, err := mlharnessSUT.GetPreprocessMethod()
if err != nil {
return err
}
output := pipeline.New(opts...).
Then(steps.NewPreprocessGeneral(preProcessMethod)).
Run(input)
tensors := make(map[int]interface{})
input <- strconv.Itoa(0)
close(input)
for out := range output {
if err, ok := out.(error); ok {
return err
}
tensors[0] = out
}
for i := 0; i < 5; i++ {
mlharnessSUT.ProcessQuery(issueCtx, tensors, []int{0})
}
if err := mlharnessDataset.Unload([]int{}); err != nil {
return err
}
fmt.Println("Finish warmup...")
return nil
}
func IssueQuery(sampleList []int) string {
issueSpan, issueCtx := tracer.StartSpanFromContext(
ctx,
tracer.APPLICATION_TRACE,
"IssueQuery Span",
)
if issueSpan == nil {
panic("invalid issue query span")
}
defer issueSpan.Finish()
data, err := mlharnessDataset.GetAll()
if err != nil {
return "[[]]"
}
return mlharnessSUT.ProcessQuery(issueCtx, data, sampleList)
}
func LoadQuerySamples(sampleList []int) error {
if err := mlharnessDataset.Load(sampleList); err != nil {
return err
}
input := make(chan interface{}, defaultChannelBuffer)
opts := []pipeline.Option{pipeline.ChannelBuffer(defaultChannelBuffer)}
preProcessMethod, err := mlharnessSUT.GetPreprocessMethod()
if err != nil {
return err
}
output := pipeline.New(opts...).
Then(steps.NewPreprocessGeneral(preProcessMethod)).
Run(input)
tensors := make(map[int]interface{})
for _, sample := range sampleList {
input <- strconv.Itoa(sample)
}
close(input)
idx := 0
for out := range output {
if err, ok := out.(error); ok {
return err
}
tensors[sampleList[idx]] = out
idx++
}
return mlharnessDataset.Set(tensors)
}
func UnloadQuerySamples(sampleList []int) error {
return mlharnessDataset.Unload(sampleList)
}
// This needs to be called once from the python side in the end
func Finalize() error {
modelManifest, err := mlharnessSUT.GetModelManifest()
if err != nil {
return err
}
if modelManifest.GetAfterPreprocess() != "" {
runtime.LockOSThread()
pyState := python3.PyGILState_Ensure()
python3.PyRun_SimpleString(modelManifest.GetAfterPreprocess())
pyMain := python3.PyImport_AddModule("__main__")
pyDict := python3.PyModule_GetDict(pyMain)
pyAfterPreprocess := python3.PyDict_GetItemString(pyDict, "after_preprocess")
pyAfterPreprocess.CallFunctionObjArgs().DecRef()
python3.PyGILState_Release(pyState)
runtime.UnlockOSThread()
}
if modelManifest.GetAfterPostprocess() != "" {
runtime.LockOSThread()
pyState := python3.PyGILState_Ensure()
python3.PyRun_SimpleString(modelManifest.GetAfterPostprocess())
pyMain := python3.PyImport_AddModule("__main__")
pyDict := python3.PyModule_GetDict(pyMain)
pyAfterPostprocess := python3.PyDict_GetItemString(pyDict, "after_postprocess")
pyAfterPostprocess.CallFunctionObjArgs().DecRef()
python3.PyGILState_Release(pyState)
runtime.UnlockOSThread()
}
mlharnessSUT.Close()
rootSpan.Finish()
tracer.Close()
return nil
}