forked from kubeflow/arena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile_builder.go
262 lines (233 loc) · 6.96 KB
/
profile_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
package model
import (
"fmt"
"github.com/kubeflow/arena/pkg/apis/types"
"github.com/kubeflow/arena/pkg/argsbuilder"
"strings"
)
type ModelProfileJobBuilder struct {
args *types.ModelProfileArgs
argValues map[string]interface{}
argsbuilder.ArgsBuilder
}
func NewModelProfileJobBuilder() *ModelProfileJobBuilder {
args := &types.ModelProfileArgs{
CommonModelArgs: types.CommonModelArgs{
Image: argsbuilder.DefaultModelJobImage,
Namespace: "default",
},
}
return &ModelProfileJobBuilder{
args: args,
argValues: map[string]interface{}{},
ArgsBuilder: argsbuilder.NewModelProfileArgsBuilder(args),
}
}
// Name is used to set job name,match option --name
func (m *ModelProfileJobBuilder) Name(name string) *ModelProfileJobBuilder {
if name != "" {
m.args.Name = name
}
return m
}
// Namespace is used to set job namespace,match option --namespace
func (m *ModelProfileJobBuilder) Namespace(namespace string) *ModelProfileJobBuilder {
if namespace != "" {
m.args.Namespace = namespace
}
return m
}
// Shell is used to specify linux shell type
func (m *ModelProfileJobBuilder) Shell(shell string) *ModelProfileJobBuilder {
if shell != "" {
m.args.Shell = shell
}
return m
}
// Command is used to set job command
func (m *ModelProfileJobBuilder) Command(args []string) *ModelProfileJobBuilder {
m.args.Command = strings.Join(args, " ")
return m
}
// Image is used to set job image,match the option --image
func (m *ModelProfileJobBuilder) Image(image string) *ModelProfileJobBuilder {
if image != "" {
m.args.Image = image
}
return m
}
// ImagePullPolicy is used to set image pull policy,match the option --image-pull-policy
func (m *ModelProfileJobBuilder) ImagePullPolicy(policy string) *ModelProfileJobBuilder {
if policy != "" {
m.args.ImagePullPolicy = policy
}
return m
}
// ImagePullSecrets is used to set image pull secrests,match option --image-pull-secret
func (m *ModelProfileJobBuilder) ImagePullSecrets(secrets []string) *ModelProfileJobBuilder {
if secrets != nil {
m.argValues["image-pull-secret"] = &secrets
}
return m
}
// GPUCount is used to set count of gpu for the job,match the option --gpus
func (m *ModelProfileJobBuilder) GPUCount(count int) *ModelProfileJobBuilder {
if count > 0 {
m.args.GPUCount = count
}
return m
}
// GPUMemory is used to set gpu memory for the job,match the option --gpumemory
func (m *ModelProfileJobBuilder) GPUMemory(memory int) *ModelProfileJobBuilder {
if memory > 0 {
m.args.GPUMemory = memory
}
return m
}
// GPUCore is used to set gpu core for the job,match the option --gpucore
func (m *ModelProfileJobBuilder) GPUCore(core int) *ModelProfileJobBuilder {
if core > 0 {
m.args.GPUCore = core
}
return m
}
// CPU assign cpu limits,match the option --cpu
func (m *ModelProfileJobBuilder) CPU(cpu string) *ModelProfileJobBuilder {
if cpu != "" {
m.args.Cpu = cpu
}
return m
}
// Memory assign memory limits,match option --memory
func (m *ModelProfileJobBuilder) Memory(memory string) *ModelProfileJobBuilder {
if memory != "" {
m.args.Memory = memory
}
return m
}
// Envs is used to set env of job containers,match option --env
func (m *ModelProfileJobBuilder) Envs(envs map[string]string) *ModelProfileJobBuilder {
if len(envs) != 0 {
envSlice := []string{}
for key, value := range envs {
envSlice = append(envSlice, fmt.Sprintf("%v=%v", key, value))
}
m.argValues["env"] = &envSlice
}
return m
}
// Tolerations is used to set tolerations for tolerate nodes,match option --toleration
func (m *ModelProfileJobBuilder) Tolerations(tolerations []string) *ModelProfileJobBuilder {
m.argValues["toleration"] = &tolerations
return m
}
// NodeSelectors is used to set node selectors for scheduling job,match option --selector
func (m *ModelProfileJobBuilder) NodeSelectors(selectors map[string]string) *ModelProfileJobBuilder {
if len(selectors) != 0 {
selectorsSlice := []string{}
for key, value := range selectors {
selectorsSlice = append(selectorsSlice, fmt.Sprintf("%v=%v", key, value))
}
m.argValues["selector"] = &selectorsSlice
}
return m
}
// Annotations is used to add annotations for job pods,match option --annotation
func (m *ModelProfileJobBuilder) Annotations(annotations map[string]string) *ModelProfileJobBuilder {
if len(annotations) != 0 {
s := []string{}
for key, value := range annotations {
s = append(s, fmt.Sprintf("%v=%v", key, value))
}
m.argValues["annotation"] = &s
}
return m
}
// Labels is used to add labels for job
func (m *ModelProfileJobBuilder) Labels(labels map[string]string) *ModelProfileJobBuilder {
if len(labels) != 0 {
s := []string{}
for key, value := range labels {
s = append(s, fmt.Sprintf("%v=%v", key, value))
}
m.argValues["label"] = &s
}
return m
}
// Datas is used to mount k8s pvc to job pods,match option --data
func (m *ModelProfileJobBuilder) Datas(volumes map[string]string) *ModelProfileJobBuilder {
if len(volumes) != 0 {
s := []string{}
for key, value := range volumes {
s = append(s, fmt.Sprintf("%v:%v", key, value))
}
m.argValues["data"] = &s
}
return m
}
// DataDirs is used to mount host files to job containers,match option --data-dir
func (m *ModelProfileJobBuilder) DataDirs(volumes map[string]string) *ModelProfileJobBuilder {
if len(volumes) != 0 {
s := []string{}
for key, value := range volumes {
s = append(s, fmt.Sprintf("%v:%v", key, value))
}
m.argValues["data-dir"] = &s
}
return m
}
// ModelConfigFile is used to set model config file,match the option --model-config-file
func (m *ModelProfileJobBuilder) ModelConfigFile(filePath string) *ModelProfileJobBuilder {
if filePath != "" {
m.args.ModelConfigFile = filePath
}
return m
}
// ModelName is used to set model name,match the option --model-name
func (m *ModelProfileJobBuilder) ModelName(name string) *ModelProfileJobBuilder {
if name != "" {
m.args.ModelName = name
}
return m
}
// ModelPath is used to set model path,match the option --model-path
func (m *ModelProfileJobBuilder) ModelPath(path string) *ModelProfileJobBuilder {
if path != "" {
m.args.ModelPath = path
}
return m
}
// Inputs is used to specify model inputs
func (m *ModelProfileJobBuilder) Inputs(inputs string) *ModelProfileJobBuilder {
if inputs != "" {
m.args.Inputs = inputs
}
return m
}
// Outputs is used to specify model outputs
func (m *ModelProfileJobBuilder) Outputs(outputs string) *ModelProfileJobBuilder {
if outputs != "" {
m.args.Outputs = outputs
}
return m
}
// ReportPath is used to specify profile result path
func (m *ModelProfileJobBuilder) ReportPath(reportPath string) *ModelProfileJobBuilder {
if reportPath != "" {
m.args.ReportPath = reportPath
}
return m
}
// Build is used to build the job
func (m *ModelProfileJobBuilder) Build() (*Job, error) {
for key, value := range m.argValues {
m.AddArgValue(key, value)
}
if err := m.PreBuild(); err != nil {
return nil, err
}
if err := m.ArgsBuilder.Build(); err != nil {
return nil, err
}
return NewJob(m.args.Name, types.ModelProfileJob, m.args), nil
}