forked from kubeflow/arena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
triton_builder.go
326 lines (290 loc) · 8.94 KB
/
triton_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
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
package serving
import (
"fmt"
"strings"
"github.com/kubeflow/arena/pkg/apis/types"
"github.com/kubeflow/arena/pkg/argsbuilder"
)
type TritonServingJobBuilder struct {
args *types.TritonServingArgs
argValues map[string]interface{}
argsbuilder.ArgsBuilder
}
func NewTritonServingJobBuilder() *TritonServingJobBuilder {
args := &types.TritonServingArgs{
HttpPort: 8000,
GrpcPort: 8001,
MetricsPort: 8002,
CommonServingArgs: types.CommonServingArgs{
ImagePullPolicy: "IfNotPresent",
Replicas: 1,
Namespace: "default",
Shell: "sh",
},
}
return &TritonServingJobBuilder{
args: args,
argValues: map[string]interface{}{},
ArgsBuilder: argsbuilder.NewTritonServingArgsBuilder(args),
}
}
// Name is used to set job name,match option --name
func (b *TritonServingJobBuilder) Name(name string) *TritonServingJobBuilder {
if name != "" {
b.args.Name = name
}
return b
}
// Namespace is used to set job namespace,match option --namespace
func (b *TritonServingJobBuilder) Namespace(namespace string) *TritonServingJobBuilder {
if namespace != "" {
b.args.Namespace = namespace
}
return b
}
// Shell is used to set bash or sh
func (b *TritonServingJobBuilder) Shell(shell string) *TritonServingJobBuilder {
if shell != "" {
b.args.Shell = shell
}
return b
}
// Command is used to set job command
func (b *TritonServingJobBuilder) Command(args []string) *TritonServingJobBuilder {
b.args.Command = strings.Join(args, " ")
return b
}
// GPUCount is used to set count of gpu for the job,match the option --gpus
func (b *TritonServingJobBuilder) GPUCount(count int) *TritonServingJobBuilder {
if count > 0 {
b.args.GPUCount = count
}
return b
}
// GPUMemory is used to set gpu memory for the job,match the option --gpumemory
func (b *TritonServingJobBuilder) GPUMemory(memory int) *TritonServingJobBuilder {
if memory > 0 {
b.args.GPUMemory = memory
}
return b
}
// GPUCore is used to set gpu core for the job,match the option --gpucore
func (b *TritonServingJobBuilder) GPUCore(core int) *TritonServingJobBuilder {
if core > 0 {
b.args.GPUCore = core
}
return b
}
// Image is used to set job image,match the option --image
func (b *TritonServingJobBuilder) Image(image string) *TritonServingJobBuilder {
if image != "" {
b.args.Image = image
}
return b
}
// ImagePullPolicy is used to set image pull policy,match the option --image-pull-policy
func (b *TritonServingJobBuilder) ImagePullPolicy(policy string) *TritonServingJobBuilder {
if policy != "" {
b.args.ImagePullPolicy = policy
}
return b
}
// CPU assign cpu limits,match the option --cpu
func (b *TritonServingJobBuilder) CPU(cpu string) *TritonServingJobBuilder {
if cpu != "" {
b.args.Cpu = cpu
}
return b
}
// Memory assign memory limits,match option --memory
func (b *TritonServingJobBuilder) Memory(memory string) *TritonServingJobBuilder {
if memory != "" {
b.args.Memory = memory
}
return b
}
// Envs is used to set env of job containers,match option --env
func (b *TritonServingJobBuilder) Envs(envs map[string]string) *TritonServingJobBuilder {
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
}
// Replicas is used to set serving job replicas,match the option --replicas
func (b *TritonServingJobBuilder) Replicas(count int) *TritonServingJobBuilder {
if count > 0 {
b.args.Replicas = count
}
return b
}
// EnableIstio is used to enable istio,match the option --enable-istio
func (b *TritonServingJobBuilder) EnableIstio() *TritonServingJobBuilder {
b.args.EnableIstio = true
return b
}
// ExposeService is used to expose service,match the option --expose-service
func (b *TritonServingJobBuilder) ExposeService() *TritonServingJobBuilder {
b.args.ExposeService = true
return b
}
// Version is used to set serving job version,match the option --version
func (b *TritonServingJobBuilder) Version(version string) *TritonServingJobBuilder {
if version != "" {
b.args.Version = version
}
return b
}
// Tolerations is used to set tolerations for tolerate nodes,match option --toleration
func (b *TritonServingJobBuilder) Tolerations(tolerations []string) *TritonServingJobBuilder {
b.argValues["toleration"] = &tolerations
return b
}
// NodeSelectors is used to set node selectors for scheduling job,match option --selector
func (b *TritonServingJobBuilder) NodeSelectors(selectors map[string]string) *TritonServingJobBuilder {
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 *TritonServingJobBuilder) Annotations(annotations map[string]string) *TritonServingJobBuilder {
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 *TritonServingJobBuilder) Labels(labels map[string]string) *TritonServingJobBuilder {
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 *TritonServingJobBuilder) Datas(volumes map[string]string) *TritonServingJobBuilder {
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
}
// DataSubPathExprs is used to mount k8s pvc subpath to job pods,match option data-subpath-expr
func (b *TritonServingJobBuilder) DataSubPathExprs(exprs map[string]string) *TritonServingJobBuilder {
if len(exprs) != 0 {
s := []string{}
for key, value := range exprs {
s = append(s, fmt.Sprintf("%v:%v", key, value))
}
b.argValues["data-subpath-expr"] = &s
}
return b
}
// TempDirs specify the deployment empty dir
func (b *TritonServingJobBuilder) TempDirs(volumes map[string]string) *TritonServingJobBuilder {
if len(volumes) != 0 {
s := []string{}
for key, value := range volumes {
s = append(s, fmt.Sprintf("%v:%v", key, value))
}
b.argValues["temp-dir"] = &s
}
return b
}
// EmptyDirSubPathExprs specify the datasource subpath to mount to the pod by expression
func (b *TritonServingJobBuilder) EmptyDirSubPathExprs(exprs map[string]string) *TritonServingJobBuilder {
if len(exprs) != 0 {
s := []string{}
for key, value := range exprs {
s = append(s, fmt.Sprintf("%v:%v", key, value))
}
b.argValues["temp-dir-subpath-expr"] = &s
}
return b
}
// DataDirs is used to mount host files to job containers,match option --data-dir
func (b *TritonServingJobBuilder) DataDirs(volumes map[string]string) *TritonServingJobBuilder {
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
}
// Port is used to set port,match the option --port
func (b *TritonServingJobBuilder) HttpPort(port int) *TritonServingJobBuilder {
if port > 0 {
b.args.HttpPort = port
}
return b
}
// RestfulPort is used to set restful port,match the option --restful-port
func (b *TritonServingJobBuilder) GrpcPort(port int) *TritonServingJobBuilder {
if port > 0 {
b.args.GrpcPort = port
}
return b
}
// MetricsPort is used to set metrics port,match the option --metric-port
func (b *TritonServingJobBuilder) MetricsPort(port int) *TritonServingJobBuilder {
if port > 0 {
b.args.MetricsPort = port
}
return b
}
// ModelRepository is used to set model store,match the option --model-repository
func (b *TritonServingJobBuilder) ModelRepository(repository string) *TritonServingJobBuilder {
if repository != "" {
b.args.ModelRepository = repository
}
return b
}
// AllowMetrics is enable metric,match the option --allow-metrics
func (b *TritonServingJobBuilder) AllowMetrics() *TritonServingJobBuilder {
b.args.AllowMetrics = true
return b
}
// ConfigFiles is used to mapping config files form local to job containers,match option --config-file
func (b *TritonServingJobBuilder) ConfigFiles(files map[string]string) *TritonServingJobBuilder {
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
}
// Build is used to build the job
func (b *TritonServingJobBuilder) 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.TritonServingJob, b.args), nil
}