-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubmit.go
209 lines (182 loc) · 6.88 KB
/
submit.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
// Copyright 2018 The Kubeflow Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package commands
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/kubeflow/arena/pkg/util"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var (
envs []string
dataset []string
dataDirs []string
annotations []string
)
// The common parts of the submitAthd
type submitArgs struct {
// Name string `yaml:"name"` // --name
Image string `yaml:"image"` // --image
GPUCount int `yaml:"gpuCount"` // --gpuCount
Envs map[string]string `yaml:"envs"` // --envs
WorkingDir string `yaml:"workingDir"` // --workingDir
Command string `yaml:"command"`
// for horovod
Mode string `yaml:"mode"` // --mode
WorkerCount int `yaml:"workers"` // --workers
// SSHPort int `yaml:"sshPort"` // --sshPort
Retry int `yaml:"retry"` // --retry
// DataDir string `yaml:"dataDir"` // --dataDir
DataSet map[string]string `yaml:"dataset"`
DataDirs []dataDirVolume `yaml:"dataDirs"`
EnableRDMA bool `yaml:"enableRDMA"` // --rdma
UseENI bool `yaml:"useENI"`
Annotations map[string]string `yaml:"annotations"`
}
type dataDirVolume struct {
HostPath string `yaml:"hostPath"`
ContainerPath string `yaml:"containerPath"`
Name string `yaml:"name"`
}
func (s submitArgs) check() error {
if name == "" {
return fmt.Errorf("--name must be set")
}
// return fmt.Errorf("must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character.")
err := util.ValidateJobName(name)
if err != nil {
return err
}
// if s.DataDir == "" {
// return fmt.Errorf("--dataDir must be set")
// }
return nil
}
// transform common parts of submitArgs
func (s *submitArgs) transform() (err error) {
// 1. handle data dirs
log.Debugf("dataDir: %v", dataDirs)
if len(dataDirs) > 0 {
s.DataDirs = []dataDirVolume{}
for i, dataDir := range dataDirs {
hostPath, containerPath, err := util.ParseDataDirRaw(dataDir)
if err != nil {
return err
}
s.DataDirs = append(s.DataDirs, dataDirVolume{
Name: fmt.Sprintf("training-data-%d", i),
HostPath: hostPath,
ContainerPath: containerPath,
})
}
}
// 2. handle data sets
log.Debugf("dataset: %v", dataset)
if len(dataset) > 0 {
err = util.ValidateDatasets(dataset)
if err != nil {
return err
}
s.DataSet = transformSliceToMap(dataset, ":")
}
// 3. handle annotations
log.Debugf("annotations: %v", annotations)
if len(annotations) > 0 {
s.Annotations = transformSliceToMap(annotations, "=")
if value, _ := s.Annotations[aliyunENIAnnotation]; value == "true" {
s.UseENI = true
}
}
return nil
}
func (submitArgs *submitArgs) addJobInfoToEnv() {
if len(submitArgs.Envs) == 0 {
submitArgs.Envs = map[string]string{}
}
submitArgs.Envs["workers"] = strconv.Itoa(submitArgs.WorkerCount)
submitArgs.Envs["gpus"] = strconv.Itoa(submitArgs.GPUCount)
}
func (submitArgs *submitArgs) addCommonFlags(command *cobra.Command) {
// create subcommands
command.Flags().StringVar(&name, "name", "", "override name")
command.MarkFlagRequired("name")
command.Flags().StringVar(&submitArgs.Image, "image", "", "the docker image name of training job")
// command.MarkFlagRequired("image")
command.Flags().IntVar(&submitArgs.GPUCount, "gpus", 0,
"the GPU count of each worker to run the training.")
// command.Flags().StringVar(&submitArgs.DataDir, "dataDir", "", "the data dir. If you specify /data, it means mounting hostpath /data into container path /data")
command.Flags().IntVar(&submitArgs.WorkerCount, "workers", 1,
"the worker number to run the distributed training.")
command.Flags().IntVar(&submitArgs.Retry, "retry", 0,
"retry times.")
// command.MarkFlagRequired("syncSource")
command.Flags().StringVar(&submitArgs.WorkingDir, "workingDir", "/root", "working directory to extract the code. If using syncMode, the $workingDir/code contains the code")
command.Flags().MarkDeprecated("workingDir", "please use --working-dir instead")
command.Flags().StringVar(&submitArgs.WorkingDir, "working-dir", "/root", "working directory to extract the code. If using syncMode, the $workingDir/code contains the code")
// command.MarkFlagRequired("workingDir")
command.Flags().StringArrayVarP(&envs, "env", "e", []string{}, "the environment variables")
command.Flags().StringArrayVarP(&dataset, "data", "d", []string{}, "specify the datasource to mount to the job, like <name_of_datasource>:<mount_point_on_job>")
command.Flags().StringArrayVar(&dataDirs, "dataDir", []string{}, "the data dir. If you specify /data, it means mounting hostpath /data into container path /data")
command.Flags().MarkDeprecated("dataDir", "please use --data-dir instead")
command.Flags().StringArrayVar(&dataDirs, "data-dir", []string{}, "the data dir. If you specify /data, it means mounting hostpath /data into container path /data")
command.Flags().StringArrayVarP(&annotations, "annotation", "a", []string{}, "the annotations")
// enable RDMA or not, support hostnetwork for now
command.Flags().BoolVar(&submitArgs.EnableRDMA, "rdma", false, "enable RDMA")
}
func init() {
if os.Getenv(CHART_PKG_LOC) != "" {
standalone_training_chart = filepath.Join(os.Getenv(CHART_PKG_LOC), "training")
}
}
var (
submitLong = `Submit a job.
Available Commands:
tfjob,tf Submit a TFJob.
horovod,hj Submit a Horovod Job.
mpijob,mpi Submit a MPIJob.
standalonejob,sj Submit a standalone Job.
tfserving,tfserving Submit a Serving Job.
`
)
func NewSubmitCommand() *cobra.Command {
var command = &cobra.Command{
Use: "submit",
Short: "Submit a job.",
Long: submitLong,
Run: func(cmd *cobra.Command, args []string) {
cmd.HelpFunc()(cmd, args)
},
}
command.AddCommand(NewSubmitTFJobCommand())
command.AddCommand(NewSubmitMPIJobCommand())
command.AddCommand(NewSubmitHorovodJobCommand())
// This will be deprecated soon.
command.AddCommand(NewSubmitStandaloneJobCommand())
command.AddCommand(NewSparkApplicationCommand())
return command
}
func transformSliceToMap(sets []string, split string) (valuesMap map[string]string) {
valuesMap = map[string]string{}
for _, member := range sets {
splits := strings.SplitN(member, split, 2)
if len(splits) == 2 {
valuesMap[splits[0]] = splits[1]
}
}
return valuesMap
}