-
Notifications
You must be signed in to change notification settings - Fork 19
/
training_job.go
509 lines (385 loc) · 13.6 KB
/
training_job.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
package elasticthought
import (
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"reflect"
"sync"
"github.com/couchbaselabs/logg"
"github.com/tleyden/go-couch"
)
// A training job represents a "training session" of a solver against training/test data
type TrainingJob struct {
ElasticThoughtDoc
ProcessingState ProcessingState `json:"processing-state"`
ProcessingLog string `json:"processing-log"`
UserID string `json:"user-id"`
SolverId string `json:"solver-id" binding:"required"`
StdOutUrl string `json:"std-out-url"`
StdErrUrl string `json:"std-err-url"`
TrainedModelUrl string `json:"trained-model-url"`
Labels []string `json:"labels"`
// had to make exported, due to https://github.com/gin-gonic/gin/pull/123
// waiting for this to get merged into master branch, since go get
// pulls from master branch.
Configuration Configuration
}
// Create a new training job. If you don't use this, you must set the
// embedded ElasticThoughtDoc Type field.
func NewTrainingJob(c Configuration) *TrainingJob {
return &TrainingJob{
ElasticThoughtDoc: ElasticThoughtDoc{Type: DOC_TYPE_TRAINING_JOB},
Configuration: c,
}
}
// Run this job
func (j *TrainingJob) Run(wg *sync.WaitGroup) {
defer wg.Done()
logg.LogTo("TRAINING_JOB", "Run() called!")
updatedState, err := j.UpdateProcessingState(Processing)
if err != nil {
j.recordProcessingError(err)
return
}
if !updatedState {
logg.LogTo("TRAINING_JOB", "%+v already processed. Ignoring.", j)
return
}
j.StdOutUrl = j.getStdOutCbfsUrl()
j.StdErrUrl = j.getStdErrCbfsUrl()
if err := j.extractData(); err != nil {
j.recordProcessingError(err)
return
}
if err := j.runCaffe(); err != nil {
j.recordProcessingError(err)
return
}
j.FinishedSuccessfully(j.Configuration.DbConnection(), "")
}
// Update the processing state to new state.
func (j *TrainingJob) UpdateProcessingState(newState ProcessingState) (bool, error) {
updater := func(trainingJob *TrainingJob) {
trainingJob.ProcessingState = newState
}
doneMetric := func(trainingJob TrainingJob) bool {
return trainingJob.ProcessingState == newState
}
return j.casUpdate(updater, doneMetric)
}
func (j *TrainingJob) UpdateProcessingLog(val string) (bool, error) {
updater := func(trainingJob *TrainingJob) {
trainingJob.ProcessingLog = val
}
doneMetric := func(trainingJob TrainingJob) bool {
return trainingJob.ProcessingLog == val
}
return j.casUpdate(updater, doneMetric)
}
func (j *TrainingJob) UpdateLabels(labels []string) (bool, error) {
updater := func(trainingJob *TrainingJob) {
trainingJob.Labels = labels
}
doneMetric := func(trainingJob TrainingJob) bool {
return reflect.DeepEqual(labels, trainingJob.Labels)
}
return j.casUpdate(updater, doneMetric)
}
func (j *TrainingJob) GetProcessingState() ProcessingState {
return j.ProcessingState
}
func (j *TrainingJob) SetProcessingState(newState ProcessingState) {
j.ProcessingState = newState
}
func (j *TrainingJob) RefreshFromDB(db couch.Database) error {
trainingJob := TrainingJob{}
err := db.Retrieve(j.Id, &trainingJob)
if err != nil {
logg.LogTo("TRAINING_JOB", "Error getting latest: %v", err)
return err
}
*j = trainingJob
return nil
}
// call caffe train --solver=<work-dir>/spec.prototxt
func (j TrainingJob) runCaffe() error {
logg.LogTo("TRAINING_JOB", "runCaffe()")
// get the solver associated with this training job
solver, err := j.getSolver()
if err != nil {
return fmt.Errorf("Error getting solver: %+v. Err: %v", j, err)
}
// filename of solver prototxt, (ie, "solver.prototxt")
_, solverFilename := filepath.Split(solver.SpecificationUrl)
logg.LogTo("TRAINING_JOB", "solverFilename: %v", solverFilename)
// build command args
cmdArgs := []string{"train", fmt.Sprintf("--solver=%v", solverFilename)}
caffePath := "caffe"
// debugging
logg.LogTo("TRAINING_JOB", "Running %v with args %v", caffePath, cmdArgs)
logg.LogTo("TRAINING_JOB", "Path %v", os.Getenv("PATH"))
out, _ := exec.Command("ls", "-alh", "/usr/local/bin").Output()
logg.LogTo("TRAINING_JOB", "ls -alh /usr/local/bin: %v", string(out))
// explicitly check if caffe binary found on the PATH
lookPathResult, err := exec.LookPath("caffe")
if err != nil {
logg.LogError(fmt.Errorf("caffe not found on path: %v", err))
}
logg.LogTo("TRAINING_JOB", "caffe found on path: %v", lookPathResult)
// Create Caffe command, but don't actually run it yet
cmd := exec.Command(caffePath, cmdArgs...)
// set the directory where the command will be run in (important
// because we depend on relative file paths to work)
cmd.Dir = j.getWorkDirectory()
// run the command and save stdio to files and tee to stdio streams
if err := runCmdTeeStdio(cmd, j.getStdOutPath(), j.getStdErrPath()); err != nil {
return err
}
// read from temp files and write to cbfs.
// initially I tried to write the stdout/stderr streams directly
// to cbfs, but ran into an error related to the io.Seeker interface.
if err := j.saveCmdOutputToCbfs(j.getStdOutPath()); err != nil {
return fmt.Errorf("Error running caffe: could not save output to cbfs. Err: %v", err)
}
if err := j.saveCmdOutputToCbfs(j.getStdErrPath()); err != nil {
return fmt.Errorf("Error running caffe: could not save output to cbfs. Err: %v", err)
}
// find out the name of the final model, eg snapshot_iter_200.caffemodel
caffeModelFilename, err := j.getCaffeModelFilename()
if err != nil {
return fmt.Errorf("Error finding the caffe model file. Err: %v", err)
}
// get the full path to the caffe model file
caffeModelFilepath := path.Join(j.getWorkDirectory(), caffeModelFilename)
logg.LogTo("TRAINING_JOB", "caffeModelFilepath: %v", caffeModelFilepath)
// upload caffemodel to cbfs as <training-job-id>/trained.caffemodel
logg.LogTo("TRAINING_JOB", "upload caffe model to cbfs")
if err := j.uploadCaffeModelToCbfs(caffeModelFilepath); err != nil {
return fmt.Errorf("Error uploading caffe model to cbfs. Err: %v", err)
}
logg.LogTo("TRAINING_JOB", "uploaded caffe model to cbfs")
// update the training job to have the caffe model URL
// set the url to the model, could be:
// relative (do this for now, convert to absolute later)
// - maybe cbfs/243224lkjlkj/caffe.model which a user can paste at end of API url
// absolute
// - http://host:8080/cbfs/243224lkjlkj/caffe.model
// - will need to be given public ip in config
logg.LogTo("TRAINING_JOB", "updating caffe model url")
if err := j.updateCaffeModelUrl(); err != nil {
return fmt.Errorf("Error updating caffe model url. Err: %v", err)
}
logg.LogTo("TRAINING_JOB", "updated caffe model url")
// TODO: add cbfs proxy so that we can get to this file
// via http://host:8080/cbfs/243224lkjlkj/caffe.model
return nil
}
func (j TrainingJob) uploadCaffeModelToCbfs(caffeModelFilename string) error {
destPath := path.Join(j.Id, "trained.caffemodel")
cbfs, err := j.Configuration.NewBlobStoreClient()
if err != nil {
return err
}
if err := saveFileToBlobStore(caffeModelFilename, destPath, "application/octet-stream", cbfs); err != nil {
return err
}
return nil
}
func (j *TrainingJob) updateCaffeModelUrl() error {
// update to cbfs/<training job id>/trained.caffemodel
newTrainedModelUrl := fmt.Sprintf("%v%v", CBFS_URI_PREFIX, path.Join(j.Id, "trained.caffemodel"))
updater := func(job *TrainingJob) {
job.TrainedModelUrl = newTrainedModelUrl
}
doneMetric := func(job TrainingJob) bool {
return job.TrainedModelUrl == newTrainedModelUrl
}
if _, err := j.casUpdate(updater, doneMetric); err != nil {
return err
}
return nil
}
func (j *TrainingJob) casUpdate(updater func(*TrainingJob), doneMetric func(TrainingJob) bool) (bool, error) {
db := j.Configuration.DbConnection()
genUpdater := func(trainingJobPtr interface{}) {
cjp := trainingJobPtr.(*TrainingJob)
updater(cjp)
}
genDoneMetric := func(trainingJobPtr interface{}) bool {
cjp := trainingJobPtr.(*TrainingJob)
return doneMetric(*cjp)
}
refresh := func(trainingJobPtr interface{}) error {
cjp := trainingJobPtr.(*TrainingJob)
return cjp.RefreshFromDB(db)
}
return casUpdate(db, j, genUpdater, genDoneMetric, refresh)
}
func (j TrainingJob) getCaffeModelFilename() (string, error) {
// get the solver associated with this training job
solver, err := j.getSolver()
if err != nil {
return "", fmt.Errorf("Error getting solver: %+v. Err: %v", j, err)
}
// read into object with protobuf (must have already generated go protobuf code)
solverParam, err := solver.getSolverParameter()
if err != nil {
return "", fmt.Errorf("Error getting solverParam. Err: %v", err)
}
maxIter := *solverParam.MaxIter
snapshotPrefix := *solverParam.SnapshotPrefix
// eg, snapshot_iter_200.caffemodel
caffeModelFilename := fmt.Sprintf("%v_iter_%v.caffemodel", snapshotPrefix, maxIter)
logg.LogTo("TRAINING_JOB", "caffeModelFilename: %v", caffeModelFilename)
return caffeModelFilename, nil
}
func (j TrainingJob) getStdOutPath() string {
return path.Join(j.getWorkDirectory(), "stdout")
}
func (j TrainingJob) getStdErrPath() string {
return path.Join(j.getWorkDirectory(), "stderr")
}
func (j TrainingJob) getStdOutCbfsUrl() string {
return fmt.Sprintf("%v/%v/%v", CBFS_URI_PREFIX, j.Id, path.Base(j.getStdOutPath()))
}
func (j TrainingJob) getStdErrCbfsUrl() string {
return fmt.Sprintf("%v/%v/%v", CBFS_URI_PREFIX, j.Id, path.Base(j.getStdErrPath()))
}
func (j TrainingJob) saveCmdOutputToCbfs(sourcePath string) error {
base := path.Base(sourcePath)
destPath := fmt.Sprintf("%v/%v", j.Id, base)
cbfsclient, err := j.Configuration.NewBlobStoreClient()
if err != nil {
return err
}
if err := saveFileToBlobStore(sourcePath, destPath, "text/plain", cbfsclient); err != nil {
return err
}
return nil
}
func (j TrainingJob) extractData() error {
// get the solver associated with this training job
solver, err := j.getSolver()
if err != nil {
return fmt.Errorf("Error getting solver: %+v. Err: %v", j, err)
}
// create a work directory based on config, eg, /usr/lib/elasticthought/<job-id>
if err := j.createWorkDirectory(); err != nil {
return fmt.Errorf("Error creating work dir: %+v. Err: %v", j, err)
}
// read prototext from cbfs, write to work dir
if err := j.writeSpecToFile(*solver); err != nil {
return fmt.Errorf("Error saving specifcation: %+v. Err: %v", j, err)
}
// download and untar the training and test .tar.gz files associated w/ solver
if err := j.saveTrainTestData(*solver); err != nil {
return fmt.Errorf("Error saving train/test data: %+v. Err: %v", j, err)
}
return nil
}
func (j TrainingJob) saveTrainTestData(s Solver) error {
labels, err := s.SaveTrainTestData(j.Configuration, j.getWorkDirectory())
if err != nil {
return err
}
logg.LogTo("TRAINING_JOB", "labels: %v", labels)
if _, err := j.UpdateLabels(labels); err != nil {
return err
}
return nil
}
// Codereview: de-dupe
func (j TrainingJob) recordProcessingError(err error) {
logg.LogError(err)
db := j.Configuration.DbConnection()
if err := j.Failed(db, err); err != nil {
errMsg := fmt.Errorf("Error setting training job as failed: %v", err)
logg.LogError(errMsg)
}
}
func (j TrainingJob) getWorkDirectory() string {
return filepath.Join(j.Configuration.WorkDirectory, j.Id)
}
func (j TrainingJob) createWorkDirectory() error {
workDir := j.getWorkDirectory()
logg.LogTo("TRAINING_JOB", "Creating dir: %v", workDir)
return Mkdir(workDir)
}
func (j TrainingJob) getSolver() (*Solver, error) {
db := j.Configuration.DbConnection()
solver := &Solver{}
err := db.Retrieve(j.SolverId, solver)
if err != nil {
errMsg := fmt.Errorf("Didn't retrieve: %v - %v", j.SolverId, err)
logg.LogError(errMsg)
return nil, errMsg
}
solver.Configuration = j.Configuration
return solver, nil
}
func (j TrainingJob) writeSpecToFile(s Solver) error {
if err := s.writeSpecToFile(j.Configuration, j.getWorkDirectory()); err != nil {
return err
}
logg.LogTo("TRAINING_JOB", "Saved specification: %v", j.getWorkDirectory())
return nil
}
// Insert into database (only call this if you know it doesn't arleady exist,
// or else you'll end up w/ unwanted dupes)
// TODO: use same approach as Classifier#Insert()
// Codereview: de-dupe
func (j TrainingJob) Insert(db couch.Database) (*TrainingJob, error) {
id, _, err := db.Insert(j)
if err != nil {
err := fmt.Errorf("Error inserting training job: %+v. Err: %v", j, err)
return nil, err
}
// load dataset object from db (so we have id/rev fields)
trainingJob := &TrainingJob{}
err = db.Retrieve(id, trainingJob)
if err != nil {
err := fmt.Errorf("Error fetching training job: %v. Err: %v", id, err)
return nil, err
}
return trainingJob, nil
}
// Update the state to record that it failed
// Codereview: de-dupe
func (j TrainingJob) Failed(db couch.Database, processingErr error) error {
_, err := j.UpdateProcessingState(Failed)
if err != nil {
return err
}
logg.LogTo("TRAINING_JOB", "updating processing log")
logValue := fmt.Sprintf("%v", processingErr)
_, err = j.UpdateProcessingLog(logValue)
if err != nil {
return err
}
return nil
}
// Update the state to record that it succeeded
// Codereview: de-dupe
func (j TrainingJob) FinishedSuccessfully(db couch.Database, logPath string) error {
_, err := j.UpdateProcessingState(FinishedSuccessfully)
if err != nil {
return err
}
_, err = j.UpdateProcessingLog(logPath)
if err != nil {
return err
}
return nil
}
// Find a training job in the db with the given id,
// or return an error if not found
func (j *TrainingJob) Find(id string) error {
db := j.Configuration.DbConnection()
j.Id = id
if err := j.RefreshFromDB(db); err != nil {
return err
}
return nil
}