/
progress.go
224 lines (199 loc) 路 5.93 KB
/
progress.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
package progress
import (
"errors"
"fmt"
"os"
"os/signal"
"regexp"
"strconv"
"strings"
"syscall"
"time"
"github.com/briandowns/spinner"
"github.com/cheggaaa/pb"
"github.com/cloud-annotations/training/cacli/cmd/login"
"github.com/cloud-annotations/training/cacli/e"
"github.com/jedib0t/go-pretty/text"
"github.com/spf13/cobra"
"nhooyr.io/websocket"
)
var s = spinner.New(spinner.CharSets[14], 60*time.Millisecond)
func Run(_ *cobra.Command, args []string) {
if len(args) < 1 {
e.Exit(errors.New("no `Model ID` provided\nUsage: cacli progress <model_id>"))
}
modelID := args[0]
session := login.AssertLoggedIn()
s.Suffix = " Checking training status..."
s.Start()
model, err := session.GetTrainingRun(modelID)
if err != nil {
e.Exit(err)
}
switch model.Entity.Status.State {
case "completed":
s.Stop()
fmt.Println(text.Colors{text.FgGreen}.Sprintf("success") + " training complete")
os.Exit(0)
case "error", "failed", "canceled":
s.Stop()
e.Exit(errors.New("training failed or canceled"))
case "pending", "running":
// do nothing
default:
s.Stop()
e.Exit(errors.New("invalid training run state"))
}
totalStepsRegex, err := regexp.Compile(`\.\/start\.sh (\d*)$`)
if err != nil {
e.Exit(err)
}
trainingCommand := model.Entity.ModelDefinition.Execution.Command
totalStepCount := 0
stepRegRes := totalStepsRegex.FindStringSubmatch(trainingCommand)
if len(stepRegRes) >= 2 {
totalStepCount, err = strconv.Atoi(stepRegRes[1])
if err != nil {
e.Exit(err)
}
}
// GROSSSSSS, but okay...
var theRate float64
pointerToTheRate := &theRate
var ElementRemainingTime pb.ElementFunc = func(state *pb.State, args ...string) string {
baseString := "| ETA: "
sp := *pointerToTheRate
if !state.IsFinished() {
if sp > 0 {
remain := float64(state.Total() - state.Value())
remainDur := time.Duration(remain/sp) * time.Second
rts := remainDur
hours := int(rts.Hours())
minutes := int(rts.Minutes())
seconds := int(rts.Seconds())
if hours > 0 {
return baseString + strconv.Itoa(hours) + " hrs"
}
if minutes > 0 {
return baseString + strconv.Itoa(minutes) + " mins"
}
return baseString + strconv.Itoa(seconds) + " sec"
}
}
return baseString + "???"
}
pb.RegisterElement("rtime", ElementRemainingTime, false)
bar := pb.New(totalStepCount)
template := `{{ bar . "[" "#" "-" "-" "]"}} {{counters . "%s/%s"}} {{rtime . "| ETA: %s"}}`
bar.SetTemplateString(template)
// const classificationStepRegex = /Step (\d*): Train accuracy/gm
objectDetectionStepRegex, err := regexp.Compile(`tensorflow:loss = [\d.]*, step = (\d*)`)
successRegex, err := regexp.Compile(`CACLI-TRAINING-SUCCESS`)
trainingFailedRegex, err := regexp.Compile(`CACLI-TRAINING-FAILED`)
conversionFailedRegex, err := regexp.Compile(`CACLI-CONVERSION-FAILED`)
trainingRegex, err := regexp.Compile(`^training-[^:]*:(.*)`)
if err != nil {
e.Exit(err)
}
trainingSteps := 0
hasStartedPreparing := model.Entity.Status.State == "running"
hasStartedTraining := false
timeWeStartedToMonitor := time.Now()
err = session.MonitorRun(modelID, func(message string) {
if !hasStartedPreparing {
// If we haven't gotten any training messages yet, check to see if we do now.
regRes := trainingRegex.FindStringSubmatch(message)
if len(regRes) >= 2 && strings.TrimSpace(regRes[1]) != "" {
hasStartedPreparing = true
s.Stop()
// move on to check for training steps.
} else {
// If it's been 10 seconds of nothing, show waiting for GPU message.
nowTime := time.Now()
if nowTime.Sub(timeWeStartedToMonitor).Seconds() > 10 {
s.Suffix = " Waiting for an available GPU (this may take a while)..."
s.Start()
}
return
}
}
// check for steps.
regRes := objectDetectionStepRegex.FindStringSubmatch(message)
if len(regRes) >= 2 {
tmpTrainingSteps, err := strconv.Atoi(regRes[1])
if err != nil {
return // ignore, not fatal.
}
// disregard output until we know there wasn't an error.
trainingSteps = tmpTrainingSteps + 1 // indexed by zero.
if !hasStartedTraining {
hasStartedTraining = true
s.Stop()
fmt.Print("\033[?25l")
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
fmt.Print("\033[?25h")
os.Exit(1)
}()
bar.Start()
}
}
isSuccess := len(successRegex.FindStringSubmatch(message)) > 2
if isSuccess {
bar.Finish()
fmt.Print("\033[?25h")
fmt.Println(text.Colors{text.FgGreen}.Sprintf("success") + " training complete")
s.Suffix = " Generating model files..."
s.Start()
return
}
isTrainingFail := len(trainingFailedRegex.FindStringSubmatch(message)) > 2
if isTrainingFail {
bar.Finish()
fmt.Print("\033[?25h")
e.Exit(errors.New("training failed"))
}
isConversionFail := len(conversionFailedRegex.FindStringSubmatch(message)) > 2
if isConversionFail {
bar.Finish()
fmt.Print("\033[?25h")
e.Exit(errors.New("conversion failed"))
}
// if there are training steps, update progress.
if hasStartedTraining {
// avoids to many state calculations.
if bar.Current() < int64(trainingSteps) {
bar.SetCurrent(int64(trainingSteps))
}
// We have old information, update it.
if model.Entity.Status.State != "running" {
model, err = session.GetTrainingRun(modelID)
if err != nil {
e.Exit(err)
}
}
startedAt := model.Entity.Status.RunningAt
now := time.Now().UTC()
elapsedSeconds := float64(now.Sub(startedAt).Seconds())
if elapsedSeconds > 0 {
theRate = float64(trainingSteps) / elapsedSeconds
}
return
}
// otherwise show preparing to train message.
s.Suffix = " Training has started, waiting for a progress update..."
s.Start()
return
})
if websocket.CloseStatus(err) == websocket.StatusNormalClosure {
fmt.Print("\033[?25h")
s.Stop()
fmt.Println(text.Colors{text.FgGreen}.Sprintf("success") + " model files saved to bucket")
return
}
if err != nil {
e.Exit(err)
}
}