-
Notifications
You must be signed in to change notification settings - Fork 175
/
training_client.go
214 lines (194 loc) · 7.34 KB
/
training_client.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
package arenaclient
import (
"fmt"
"time"
"github.com/kubeflow/arena/pkg/apis/config"
apistraining "github.com/kubeflow/arena/pkg/apis/training"
"github.com/kubeflow/arena/pkg/apis/types"
"github.com/kubeflow/arena/pkg/apis/utils"
"github.com/kubeflow/arena/pkg/podexec"
"github.com/kubeflow/arena/pkg/training"
)
var (
errJobNotFoundMessage = "Not found training job %s in namespace %s,please use 'arena submit' to create it."
)
// TrainingJobClient provides some operators for managing training jobs.
type TrainingJobClient struct {
// namespace store the namespace
namespace string
arenaSystemNamespace string
configer *config.ArenaConfiger
}
// NewTrainingJobClient creates a TrainingJobClient
func NewTrainingJobClient(namespace, arenaSystemNamespace string, configer *config.ArenaConfiger) *TrainingJobClient {
return &TrainingJobClient{
namespace: namespace,
arenaSystemNamespace: arenaSystemNamespace,
configer: configer,
}
}
// Namespace sets the namespace
func (t *TrainingJobClient) Namespace(namespace string) *TrainingJobClient {
copyTrainingJobClient := &TrainingJobClient{
namespace: namespace,
arenaSystemNamespace: t.arenaSystemNamespace,
configer: t.configer,
}
return copyTrainingJobClient
}
// Submit submits a training job
func (t *TrainingJobClient) Submit(job *apistraining.Job) error {
switch job.Type() {
case types.TFTrainingJob:
args := job.Args().(*types.SubmitTFJobArgs)
return training.SubmitTFJob(t.namespace, args)
case types.PytorchTrainingJob:
args := job.Args().(*types.SubmitPyTorchJobArgs)
return training.SubmitPytorchJob(t.namespace, args)
case types.MPITrainingJob:
args := job.Args().(*types.SubmitMPIJobArgs)
return training.SubmitMPIJob(t.namespace, args)
case types.HorovodTrainingJob:
args := job.Args().(*types.SubmitHorovodJobArgs)
return training.SubmitHorovodJob(t.namespace, args)
case types.VolcanoTrainingJob:
args := job.Args().(*types.SubmitVolcanoJobArgs)
return training.SubmitVolcanoJob(t.namespace, args)
case types.ETTrainingJob:
args := job.Args().(*types.SubmitETJobArgs)
return training.SubmitETJob(t.namespace, args)
case types.SparkTrainingJob:
args := job.Args().(*types.SubmitSparkJobArgs)
return training.SubmitSparkJob(t.namespace, args)
case types.DeepSpeedTrainingJob:
args := job.Args().(*types.SubmitDeepSpeedJobArgs)
return training.SubmitDeepSpeedJob(t.namespace, args)
}
return nil
}
// ScaleIn scales in job
func (t *TrainingJobClient) ScaleIn(job *apistraining.Job) error {
switch job.Type() {
case types.ETTrainingJob:
args := job.Args().(*types.ScaleInETJobArgs)
return training.SubmitScaleInETJob(t.namespace, args)
}
return nil
}
// ScaleOut scales out job
func (t *TrainingJobClient) ScaleOut(job *apistraining.Job) error {
switch job.Type() {
case types.ETTrainingJob:
args := job.Args().(*types.ScaleOutETJobArgs)
return training.SubmitScaleOutETJob(t.namespace, args)
}
return nil
}
// Get returns a training job information
func (t *TrainingJobClient) Get(jobName string, jobType types.TrainingJobType, showPrometheusMetric bool) (*types.TrainingJobInfo, error) {
job, err := training.SearchTrainingJob(jobName, t.namespace, jobType)
if err != nil {
return nil, err
}
services, nodes := training.PrepareServicesAndNodesForTensorboard([]training.TrainingJob{job}, false)
jobInfo := training.BuildJobInfo(job, showPrometheusMetric, services, nodes)
return jobInfo, nil
}
// GetAndPrint print training job information
func (t *TrainingJobClient) GetAndPrint(jobName string, jobType types.TrainingJobType, format string, showEvent bool, showGPU bool) error {
if utils.TransferPrintFormat(format) == types.UnknownFormat {
return fmt.Errorf("Unknown output format,only support:[wide|json|yaml]")
}
job, err := training.SearchTrainingJob(jobName, t.namespace, jobType)
if err != nil {
if err == types.ErrTrainingJobNotFound {
return fmt.Errorf(errJobNotFoundMessage, jobName, t.namespace)
}
return err
}
// Search model version associated with the job
jobLabels := job.GetLabels()
mv := searchModelVersionByJobLabels(t.namespace, t.configer, jobLabels)
training.PrintTrainingJob(job, mv, format, showEvent, showGPU)
return nil
}
// List returns all training jobs
func (t *TrainingJobClient) List(allNamespaces bool, trainingType types.TrainingJobType, showPrometheusMetric bool) ([]*types.TrainingJobInfo, error) {
jobs, err := training.ListTrainingJobs(t.namespace, allNamespaces, trainingType)
if err != nil {
return nil, err
}
jobInfos := []*types.TrainingJobInfo{}
services, nodes := training.PrepareServicesAndNodesForTensorboard(jobs, allNamespaces)
for _, job := range jobs {
jobInfos = append(jobInfos, training.BuildJobInfo(job, showPrometheusMetric, services, nodes))
}
return jobInfos, nil
}
// ListAndPrint lists and prints the job informations
func (t *TrainingJobClient) ListAndPrint(allNamespaces bool, format string, trainingType types.TrainingJobType) error {
if utils.TransferPrintFormat(format) == types.UnknownFormat {
return fmt.Errorf("Unknown output format,only support:[wide|json|yaml]")
}
jobs, err := training.ListTrainingJobs(t.namespace, allNamespaces, trainingType)
if err != nil {
return err
}
training.DisplayTrainingJobList(jobs, format, allNamespaces)
return nil
}
// Logs returns the training job log
func (t *TrainingJobClient) Logs(jobName string, jobType types.TrainingJobType, args *types.LogArgs) error {
args.Namespace = t.namespace
args.JobName = jobName
return training.AcceptJobLog(jobName, jobType, args)
}
func (t *TrainingJobClient) Attach(jobName string, jobType types.TrainingJobType, args *podexec.AttachPodArgs) error {
job, err := t.Get(jobName, jobType, false)
if err != nil {
return err
}
if len(job.Instances) == 0 {
return fmt.Errorf("can not attach the training job %v, because it has no instances", job.Name)
}
if args.Options.PodName == "" {
args.Options.PodName = job.ChiefName
}
command := []string{job.Name}
command = append(command, args.Command...)
if err := args.Options.Complete(command, t.namespace, args.CmdArgsLenAtDash); err != nil {
return err
}
if err := args.Options.Validate(); err != nil {
return err
}
return args.Options.Run()
}
// Delete deletes the target training job
func (t *TrainingJobClient) Delete(jobType types.TrainingJobType, jobNames ...string) error {
for _, jobName := range jobNames {
err := training.DeleteTrainingJob(jobName, t.namespace, jobType)
if err != nil {
if err == types.ErrTrainingJobNotFound {
return nil
}
return err
}
}
return nil
}
// LogViewer returns the log viewer
func (t *TrainingJobClient) LogViewer(jobName string, jobType types.TrainingJobType) ([]string, error) {
job, err := training.SearchTrainingJob(jobName, t.namespace, jobType)
if err != nil {
return nil, err
}
return job.GetJobDashboards(t.configer.GetClientSet(), t.namespace, t.arenaSystemNamespace)
}
// Prune cleans the not running training jobs
func (t *TrainingJobClient) Prune(allNamespaces bool, since time.Duration) error {
return training.PruneTrainingJobs(t.namespace, allNamespaces, since)
}
func (t *TrainingJobClient) Top(args []string, allNamespaces bool, jobType types.TrainingJobType, instanceName string, notStop bool, format types.FormatStyle) error {
return training.TopTrainingJobs(args, t.namespace, allNamespaces, jobType, instanceName, notStop, format)
}