forked from kubeflow/arena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpijob_builder.go
291 lines (257 loc) · 7.32 KB
/
mpijob_builder.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
package training
import (
"fmt"
"strings"
"github.com/kubeflow/arena/pkg/apis/types"
"github.com/kubeflow/arena/pkg/argsbuilder"
)
type MPIJobBuilder struct {
args *types.SubmitMPIJobArgs
argValues map[string]interface{}
argsbuilder.ArgsBuilder
}
func NewMPIJobBuilder() *MPIJobBuilder {
args := &types.SubmitMPIJobArgs{
CleanPodPolicy: "All",
CommonSubmitArgs: DefaultCommonSubmitArgs,
SubmitTensorboardArgs: DefaultSubmitTensorboardArgs,
}
return &MPIJobBuilder{
args: args,
argValues: map[string]interface{}{},
ArgsBuilder: argsbuilder.NewSubmitMPIJobArgsBuilder(args),
}
}
// Name is used to set job name,match option --name
func (b *MPIJobBuilder) Name(name string) *MPIJobBuilder {
if name != "" {
b.args.Name = name
}
return b
}
// Shell is used to set bash or sh
func (b *MPIJobBuilder) Shell(shell string) *MPIJobBuilder {
if shell != "" {
b.args.Shell = shell
}
return b
}
// Command is used to set job command
func (b *MPIJobBuilder) Command(args []string) *MPIJobBuilder {
b.args.Command = strings.Join(args, " ")
return b
}
// WorkingDir is used to set working directory of job containers,default is '/root'
// match option --working-dir
func (b *MPIJobBuilder) WorkingDir(dir string) *MPIJobBuilder {
if dir != "" {
b.args.WorkingDir = dir
}
return b
}
// Envs is used to set env of job containers,match option --env
func (b *MPIJobBuilder) Envs(envs map[string]string) *MPIJobBuilder {
if len(envs) != 0 {
envSlice := []string{}
for key, value := range envs {
envSlice = append(envSlice, fmt.Sprintf("%v=%v", key, value))
}
b.argValues["env"] = &envSlice
}
return b
}
// GPUCount is used to set count of gpu for the job,match the option --gpus
func (b *MPIJobBuilder) GPUCount(count int) *MPIJobBuilder {
if count > 0 {
b.args.GPUCount = count
}
return b
}
// Image is used to set job image,match the option --image
func (b *MPIJobBuilder) Image(image string) *MPIJobBuilder {
if image != "" {
b.args.Image = image
}
return b
}
// Tolerations is used to set tolerations for tolerate nodes,match option --toleration
func (b *MPIJobBuilder) Tolerations(tolerations []string) *MPIJobBuilder {
b.argValues["toleration"] = &tolerations
return b
}
// ConfigFiles is used to mapping config files form local to job containers,match option --config-file
func (b *MPIJobBuilder) ConfigFiles(files map[string]string) *MPIJobBuilder {
if len(files) != 0 {
filesSlice := []string{}
for localPath, containerPath := range files {
filesSlice = append(filesSlice, fmt.Sprintf("%v:%v", localPath, containerPath))
}
b.argValues["config-file"] = &filesSlice
}
return b
}
// NodeSelectors is used to set node selectors for scheduling job,match option --selector
func (b *MPIJobBuilder) NodeSelectors(selectors map[string]string) *MPIJobBuilder {
if len(selectors) != 0 {
selectorsSlice := []string{}
for key, value := range selectors {
selectorsSlice = append(selectorsSlice, fmt.Sprintf("%v=%v", key, value))
}
b.argValues["selector"] = &selectorsSlice
}
return b
}
// Annotations is used to add annotations for job pods,match option --annotation
func (b *MPIJobBuilder) Annotations(annotations map[string]string) *MPIJobBuilder {
if len(annotations) != 0 {
s := []string{}
for key, value := range annotations {
s = append(s, fmt.Sprintf("%v=%v", key, value))
}
b.argValues["annotation"] = &s
}
return b
}
// Labels is used to add labels for job
func (b *MPIJobBuilder) Labels(labels map[string]string) *MPIJobBuilder {
if len(labels) != 0 {
s := []string{}
for key, value := range labels {
s = append(s, fmt.Sprintf("%v=%v", key, value))
}
b.argValues["label"] = &s
}
return b
}
// Datas is used to mount k8s pvc to job pods,match option --data
func (b *MPIJobBuilder) Datas(volumes map[string]string) *MPIJobBuilder {
if len(volumes) != 0 {
s := []string{}
for key, value := range volumes {
s = append(s, fmt.Sprintf("%v:%v", key, value))
}
b.argValues["data"] = &s
}
return b
}
// DataDirs is used to mount host files to job containers,match option --data-dir
func (b *MPIJobBuilder) DataDirs(volumes map[string]string) *MPIJobBuilder {
if len(volumes) != 0 {
s := []string{}
for key, value := range volumes {
s = append(s, fmt.Sprintf("%v:%v", key, value))
}
b.argValues["data-dir"] = &s
}
return b
}
// LogDir is used to set log directory,match option --logdir
func (b *MPIJobBuilder) LogDir(dir string) *MPIJobBuilder {
if dir != "" {
b.args.TrainingLogdir = dir
}
return b
}
// Priority sets the priority
func (b *MPIJobBuilder) Priority(priority string) *MPIJobBuilder {
if priority != "" {
b.args.PriorityClassName = priority
}
return b
}
// EnableRDMA is used to enabled rdma,match option --rdma
func (b *MPIJobBuilder) EnableRDMA() *MPIJobBuilder {
b.args.EnableRDMA = true
return b
}
// SyncImage is used to set syncing image,match option --sync-image
func (b *MPIJobBuilder) SyncImage(image string) *MPIJobBuilder {
if image != "" {
b.args.SyncImage = image
}
return b
}
// SyncMode is used to set syncing mode,match option --sync-mode
func (b *MPIJobBuilder) SyncMode(mode string) *MPIJobBuilder {
if mode != "" {
b.args.SyncMode = mode
}
return b
}
// SyncSource is used to set syncing source,match option --sync-source
func (b *MPIJobBuilder) SyncSource(source string) *MPIJobBuilder {
if source != "" {
b.args.SyncSource = source
}
return b
}
// EnableTensorboard is used to enable tensorboard
func (b *MPIJobBuilder) EnableTensorboard() *MPIJobBuilder {
b.args.UseTensorboard = true
return b
}
// TensorboardImage is used to enable tensorboard image
func (b *MPIJobBuilder) TensorboardImage(image string) *MPIJobBuilder {
if image != "" {
b.args.TensorboardImage = image
}
return b
}
// ImagePullSecrets is used to set image pull secrests,match option --image-pull-secret
func (b *MPIJobBuilder) ImagePullSecrets(secrets []string) *MPIJobBuilder {
if secrets != nil {
b.argValues["image-pull-secret"] = &secrets
}
return b
}
// WorkerCount is used to set count of worker
func (b *MPIJobBuilder) WorkerCount(count int) *MPIJobBuilder {
if count > 0 {
b.args.WorkerCount = count
}
return b
}
// CPU assign cpu limts,match option --cpu
func (b *MPIJobBuilder) CPU(cpu string) *MPIJobBuilder {
if cpu != "" {
b.args.Cpu = cpu
}
return b
}
// Memory assign memory limits,match option --memory
func (b *MPIJobBuilder) Memory(memory string) *MPIJobBuilder {
if memory != "" {
b.args.Memory = memory
}
return b
}
// EnableGPUTopology is used to enable gpu topology scheduling
func (b *MPIJobBuilder) EnableGPUTopology() *MPIJobBuilder {
b.args.GPUTopology = true
return b
}
// EnableMountPVCOnLauncher is used to enable mount pvcs on launcher
func (b *MPIJobBuilder) EnableMountPVCOnLauncher() *MPIJobBuilder {
b.args.MountsOnLauncher = true
return b
}
// CleanPodPolicy is used to set cleaning pod policy,match option --clean-task-policy
func (b *MPIJobBuilder) CleanPodPolicy(policy string) *MPIJobBuilder {
if policy != "" {
b.args.CleanPodPolicy = policy
}
return b
}
// Build is used to build the job
func (b *MPIJobBuilder) Build() (*Job, error) {
for key, value := range b.argValues {
b.AddArgValue(key, value)
}
if err := b.PreBuild(); err != nil {
return nil, err
}
if err := b.ArgsBuilder.Build(); err != nil {
return nil, err
}
return NewJob(b.args.Name, types.MPITrainingJob, b.args), nil
}