/
predict_workload.go
284 lines (247 loc) · 7.44 KB
/
predict_workload.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
package server
// import (
// "bytes"
// "context"
// "fmt"
// "io"
// "io/ioutil"
// "path/filepath"
// "strconv"
// "sync"
// "time"
// sourcepath "github.com/GeertJohan/go-sourcepath"
// "github.com/Unknwon/com"
// "github.com/pkg/errors"
// "github.com/rai-project/batching"
// dl "github.com/rai-project/dlframework"
// "github.com/rai-project/dlframework/framework/agent"
// "github.com/rai-project/dlframework/framework/options"
// common "github.com/rai-project/dlframework/framework/predictor"
// "github.com/rai-project/dlframework/steps"
// nvidiasmi "github.com/rai-project/nvidia-smi"
// "github.com/rai-project/pipeline"
// "github.com/rai-project/synthetic_load"
// "github.com/rai-project/tracer"
// "github.com/schollz/progressbar"
// "github.com/spf13/cobra"
// "github.com/ulule/deepcopier"
// )
// var (
// qps float64
// latencyBound int64
// latencyBoundPercentile float64
// minDuration int64
// minQueries int
// maxQpsSearchIterations int
// imagePath string
// )
// func computeLatency(qps float64) (trace synthetic_load.Trace, latency time.Duration, err error) {
// span, ctx := tracer.StartSpanFromContext(context.Background(), traceLevel, "workload")
// defer func() {
// if span != nil {
// span.Finish()
// }
// }()
// predictorFramework, err := agent.GetPredictor(framework)
// if err != nil {
// err = errors.Wrapf(err,
// "⚠️ failed to get predictor for %s. make sure you have "+
// "imported the framework's predictor package",
// framework.MustCanonicalName(),
// )
// return
// }
// model, err := framework.FindModel(modelName + ":" + modelVersion)
// if err != nil {
// return
// }
// var dc map[string]int32
// if useGPU {
// if !nvidiasmi.HasGPU {
// err = errors.New("not gpu found")
// return
// }
// dc = map[string]int32{"GPU": 0}
// } else {
// dc = map[string]int32{"CPU": 0}
// }
// execOpts := &dl.ExecutionOptions{
// TraceLevel: dl.ExecutionOptions_TraceLevel(
// dl.ExecutionOptions_TraceLevel_value[traceLevel.String()],
// ),
// DeviceCount: dc,
// }
// predOpts := &dl.PredictionOptions{
// FeatureLimit: 10,
// BatchSize: int32(batchSize),
// ExecutionOptions: execOpts,
// }
// predictor, err := predictorFramework.Load(
// ctx,
// *model,
// // options.Context(ctx),
// options.PredictorOptions(predOpts),
// // options.DisableFrameworkAutoTuning(disableFrameworkAutoTuning),
// )
// if err != nil {
// return
// }
// preprocessOptions, err := predictor.GetPreprocessOptions()
// if err != nil {
// return
// }
// var imagePredictor common.ImagePredictor
// err = deepcopier.Copy(predictor).To(&imagePredictor)
// if err != nil {
// err = errors.Errorf("failed to copy to an image predictor for %v", model.MustCanonicalName())
// return
// }
// var bar *progressbar.ProgressBar
// useBar := false
// println("Starting inference workload generation process")
// batchQueue := make(chan steps.IDer)
// outputQueue := new(sync.Map)
// go func() {
// defer close(batchQueue)
// input, err := ioutil.ReadFile(imagePath)
// if err != nil {
// panic(err)
// }
// opts := []synthetic_load.Option{
// synthetic_load.Context(ctx),
// synthetic_load.QPS(qps),
// synthetic_load.LatencyBoundPercentile(latencyBoundPercentile),
// synthetic_load.MinQueries(minQueries),
// synthetic_load.MinDuration(time.Duration(minDuration * int64(time.Millisecond))),
// synthetic_load.InputGenerator(func(idx int) ([]byte, error) {
// return input, nil
// }),
// synthetic_load.InputRunner(batchingRunner{
// inputQueue: batchQueue,
// outputQueue: outputQueue,
// batchSize: batchSize,
// }),
// }
// trace = synthetic_load.NewTrace(opts...)
// if useBar {
// bar = progressbar.NewOptions(len(trace), progressbar.OptionSetRenderBlankState(true))
// }
// latency, err = trace.Replay(opts...)
// if err != nil {
// return
// }
// // qps := trace.QPS()
// // fmt.Printf("qps = %v latency = %v \n", qps, latency)
// }()
// btch, err := batching.NewNaive(
// func(data []steps.IDer) {
// if useBar {
// defer bar.Add(len(data))
// }
// input := make(chan interface{}, DefaultChannelBuffer)
// go func() {
// defer close(input)
// for _, elem := range data {
// input <- elem
// }
// }()
// output := pipeline.New(pipeline.Context(ctx), pipeline.ChannelBuffer(DefaultChannelBuffer)).
// Then(steps.NewReadImage(preprocessOptions)).
// Then(steps.NewPreprocessImage(preprocessOptions)).
// Run(input)
// var images []interface{}
// for out := range output {
// images = append(images, out)
// }
// input = make(chan interface{})
// go func() {
// defer close(input)
// input <- images
// }()
// output = pipeline.New(pipeline.Context(ctx), pipeline.ChannelBuffer(DefaultChannelBuffer)).
// Then(steps.NewPredict(predictor)).
// Run(input)
// for out0 := range output {
// if err, ok := out0.(error); ok {
// panic(err)
// }
// out := out0.(steps.IDer)
// qu0, ok := outputQueue.Load(out.GetID())
// if !ok {
// panic("cannot find " + out.GetID() + " input output queue")
// }
// qu := qu0.(chan struct{})
// qu <- struct{}{}
// }
// },
// batchQueue,
// batching.BatchSize(batchSize),
// )
// if err != nil {
// panic(err)
// }
// btch.Wait()
// return
// }
// var predictWorkloadCmd = &cobra.Command{
// Use: "workload",
// Short: "Evaluate the workload using the specified model and framework",
// Aliases: []string{"work-load"},
// RunE: func(c *cobra.Command, args []string) error {
// tr, latency, err := computeLatency(qps)
// if err != nil {
// return err
// }
// fmt.Printf("qps = %v, latency = %v\n",
// tr.QPS(),
// latency,
// )
// return nil
// },
// }
// type workloadInput struct {
// id string
// data io.Reader
// }
// func (w workloadInput) GetID() string {
// return w.id
// }
// func (w workloadInput) GetData() interface{} {
// return w.data
// }
// type batchingRunner struct {
// inputQueue chan steps.IDer
// outputQueue *sync.Map
// batchSize int
// }
// func (s batchingRunner) Run(tr synthetic_load.TraceEntry, bts []byte, onFinish func()) error {
// id := strconv.Itoa(tr.Index)
// s.inputQueue <- workloadInput{
// id: id,
// data: bytes.NewBuffer(bts),
// }
// ch := make(chan struct{})
// s.outputQueue.Store(id, ch)
// go func() {
// for {
// select {
// case <-ch:
// onFinish()
// return
// }
// }
// }()
// return nil
// }
// func init() {
// sourcePath := sourcepath.MustAbsoluteDir()
// defaultImagePath := filepath.Join(sourcePath, "..", "_fixtures", "chicken.jpg")
// if !com.IsFile(defaultImagePath) {
// defaultImagePath = ""
// }
// predictWorkloadCmd.PersistentFlags().Float64Var(&qps, "initial_qps", 16, "the initial QPS")
// predictWorkloadCmd.PersistentFlags().Float64Var(&latencyBoundPercentile, "percentile", 95, "the minimum percent of queries meeting the latency bound")
// predictWorkloadCmd.PersistentFlags().Int64Var(&minDuration, "min_duration", 100, "the minimum duration of the trace in ms")
// predictWorkloadCmd.PersistentFlags().IntVar(&minQueries, "min_queries", 512, "the minimum number of queries")
// predictUrlsCmd.PersistentFlags().StringVar(&imagePath, "image_path", defaultImagePath, "the path to the image to perform the evaluations on.")
// }