/
singularity.go
436 lines (363 loc) · 11.3 KB
/
singularity.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// Copyright 2018-2020 (c) Cognizant Digital Business, Evolutionary AI. All rights reserved. Issued under the Apache 2.0 License.
package runner
// This file contains the implementation of an execution module for singularity
// within the studioML go runner
//
import (
"bufio"
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
"sync"
"text/template"
"time"
"github.com/leaf-ai/studio-go-runner/internal/request"
"github.com/leaf-ai/studio-go-runner/internal/resources"
"github.com/go-stack/stack"
"github.com/jjeffery/kv" // MIT License
)
// Singularity is a data structure that contains the description of a singularity container resource
type Singularity struct {
Request *request.Request
BaseDir string
BaseImage string
}
// NewSingularity is used to instantiate a singularity resource based upon a request, typically sent
// across a go channel or similar
func NewSingularity(rqst *request.Request, dir string) (sing *Singularity, err kv.Error) {
sing = &Singularity{
Request: rqst,
BaseDir: dir,
}
art, isPresent := rqst.Experiment.Artifacts["_singularity"]
if !isPresent {
return nil, kv.NewError("_singularity artifact is missing").With("stack", stack.Trace().TrimRuntime())
}
// Look for the singularity artifact and extract the base image name
// that will be used from shub://sentient-singularity
//
if errGo := os.MkdirAll(filepath.Join(dir, "_runner"), 0700); errGo != nil {
return nil, kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime())
}
os.MkdirAll(filepath.Join(dir, "..", "blob-cache"), 0700)
os.MkdirAll(filepath.Join(dir, "..", "queue"), 0700)
os.MkdirAll(filepath.Join(dir, "..", "artifact-mappings", rqst.Experiment.Key), 0700)
sing.BaseImage = art.Qualified
switch {
case strings.HasPrefix(art.Qualified, "shub://sentient-singularity/"):
case strings.HasPrefix(art.Qualified, "dockerhub://tensorflow/"):
default:
return nil, kv.NewError("untrusted image specified").With("stack", stack.Trace().TrimRuntime()).With("artifact", art)
}
return sing, nil
}
func (s *Singularity) makeDef(alloc *resources.Allocated, e interface{}) (fn string, err kv.Error) {
// Extract all of the python variables into two collections with the studioML extracted out
// Ignore the tensorflow version as the container is responsible for cuda
pips, cfgPips, studioPIP, _ := pythonModules(s.Request, alloc)
// If the studioPIP was specified but we have a dist directory then we need to clear the
// studioPIP, otherwise leave it there
pth, errGo := filepath.Abs(filepath.Join(s.BaseDir, "workspace", "dist", "studioml-*.tar.gz"))
if errGo != nil {
return "", kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime())
}
matches, _ := filepath.Glob(pth)
if len(matches) != 0 {
// Extract the most recent version of studioML from the dist directory
sort.Strings(matches)
studioPIP = matches[len(matches)-1]
}
params := struct {
E interface{}
S *Singularity
I string
Dir string
Pips []string
CfgPips []string
StudioPIP string
ImgType string
}{
E: e,
S: s,
I: s.BaseImage,
Dir: filepath.Join(s.BaseDir, "_runner"),
Pips: pips,
CfgPips: cfgPips,
StudioPIP: studioPIP,
}
switch {
case strings.HasPrefix(params.I, "shub://singularity-hub/sentient-singularity"):
params.ImgType = "debootstrap"
case strings.HasPrefix(params.I, "dockerhub://tensorflow/"):
params.ImgType = "docker"
params.I = strings.Replace(params.I, "dockerhub://", "", 1)
}
// Create a shell script that will do everything needed to run
// the python environment in a virtual env
tmpl, errGo := template.New("singularityRunner").Parse(
`Bootstrap: {{.ImgType}}
From: {{.I}}
%labels
ai.sentient.maintainer Karl Mutch
ai.sentient.version 0.0
%post
{{range $key, $value := .E.Request.Config.Env}}
echo 'export {{$key}}="{{$value}}"' >> $SINGULARITY_ENVIRONMENT
{{end}}
{{range $key, $value := .E.ExprEnvs}}
echo 'export {{$key}}="{{$value}}"' >> $SINGULARITY_ENVIRONMENT
{{end}}
echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib64/:/usr/lib/x86_64-linux-gnu:/lib/x86_64-linux-gnu/' >> $SINGULARITY_ENVIRONMENT
echo 'export STUDIOML_EXPERIMENT={{.E.ExprSubDir}}' >> $SINGULARITY_ENVIRONMENT
echo 'export STUDIOML_HOME={{.E.RootDir}}' >> $SINGULARITY_ENVIRONMENT
pip install virtualenv
virtualenv {{.Dir}}
chmod +x {{.Dir}}/bin/activate
{{.Dir}}/bin/activate
pip freeze
{{if .StudioPIP}}
pip install -I {{.StudioPIP}}
{{end}}
{{if .Pips}}
pip install -I {{range .Pips}} {{.}}{{end}}
{{end}}
pip install pyopenssl --upgrade
{{if .CfgPips}}
pip install {{range .CfgPips}} {{.}}{{end}}
{{end}}
pip freeze
%runscript
{{.Dir}}/bin/activate
cd {{.E.ExprDir}}/workspace
python {{.E.Request.Experiment.Filename}} {{range .E.Request.Experiment.Args}}{{.}} {{end}}
date
`)
if errGo != nil {
return "", kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime())
}
content := new(bytes.Buffer)
errGo = tmpl.Execute(content, params)
if errGo != nil {
return "", kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime())
}
fn = filepath.Join(s.BaseDir, "_runner", "Singularity.def")
if errGo = ioutil.WriteFile(fn, content.Bytes(), 0600); errGo != nil {
return "", kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime())
}
return fn, nil
}
func (s *Singularity) makeBuildScript(e interface{}) (fn string, err kv.Error) {
fn = filepath.Join(s.BaseDir, "_runner", "build.sh")
params := struct {
Dir string
BaseImage string
}{
Dir: filepath.Join(s.BaseDir, "_runner"),
BaseImage: s.BaseImage,
}
tmpl, errGo := template.New("singularityRunner").Parse(
`#!/bin/bash -x
sudo singularity build {{.Dir}}/runner.img {{.Dir}}/Singularity.def
`)
if errGo != nil {
return "", kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime())
}
content := new(bytes.Buffer)
errGo = tmpl.Execute(content, params)
if errGo != nil {
return "", kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime())
}
if errGo := ioutil.WriteFile(fn, content.Bytes(), 0700); errGo != nil {
return "", kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime())
}
return fn, nil
}
func (s *Singularity) runBuildScript(script string) (err kv.Error) {
ctx := context.Background()
outputFN := filepath.Join(s.BaseDir, "output", "output")
// Move to starting the process that we will monitor with the experiment running within
// it
//
reporterC := make(chan *string)
defer close(reporterC)
go func() {
for {
select {
case msg := <-reporterC:
if msg == nil {
return
}
}
}
}()
return runWait(ctx, script, filepath.Join(s.BaseDir, "_runner"), outputFN, reporterC)
}
func (s *Singularity) makeExecScript(e interface{}) (fn string, err kv.Error) {
fn = filepath.Join(s.BaseDir, "_runner", "exec.sh")
params := struct {
Dir string
}{
Dir: filepath.Join(s.BaseDir, "_runner"),
}
tmpl, errGo := template.New("singularityRunner").Parse(
`#!/bin/bash -x
singularity run --home {{.Dir}} -B /tmp:/tmp -B /usr/local/cuda:/usr/local/cuda -B /usr/lib/nvidia-384:/usr/lib/nvidia-384 --nv {{.Dir}}/runner.img
`)
if errGo != nil {
return "", kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime())
}
content := new(bytes.Buffer)
errGo = tmpl.Execute(content, params)
if errGo != nil {
return "", kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime())
}
if errGo := ioutil.WriteFile(fn, content.Bytes(), 0700); errGo != nil {
return "", kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime())
}
return fn, nil
}
// Make is used to write a script file that is generated for the specific TF tasks studioml has sent
// to retrieve any python packages etc then to run the task
//
func (s *Singularity) Make(alloc *resources.Allocated, e interface{}) (err kv.Error) {
_, err = s.makeDef(alloc, e)
if err != nil {
return err
}
script, err := s.makeBuildScript(e)
if err != nil {
return err
}
if err = s.runBuildScript(script); err != nil {
return err
}
if _, err = s.makeExecScript(e); err != nil {
return err
}
return nil
}
// Run will use a generated script file and will run it to completion while marshalling
// results and files from the computation. Run is a blocking call and will only return
// upon completion or termination of the process it starts
//
func (s *Singularity) Run(ctx context.Context, refresh map[string]request.Artifact) (err kv.Error) {
outputFN := filepath.Join(s.BaseDir, "output", "output")
script := filepath.Join(s.BaseDir, "_runner", "exec.sh")
reporterC := make(chan *string)
defer close(reporterC)
go func() {
for {
select {
case msg := <-reporterC:
if msg == nil {
return
}
}
}
}()
return runWait(ctx, script, filepath.Join(s.BaseDir, "_runner"), outputFN, reporterC)
}
func runWait(ctx context.Context, script string, dir string, outputFN string, errorC chan *string) (err kv.Error) {
stopCmd, stopCmdCancel := context.WithCancel(context.Background())
// defers are stacked in LIFO order so cancelling this context is the last
// thing this function will do
defer stopCmdCancel()
// Move to starting the process that we will monitor with the experiment running within
// it
// #nosec
cmd := exec.Command("/bin/bash", "-c", filepath.Clean(script))
cmd.Dir = dir
stdout, errGo := cmd.StdoutPipe()
if errGo != nil {
return kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime())
}
stderr, errGo := cmd.StderrPipe()
if errGo != nil {
return kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime())
}
outC := make(chan []byte)
defer close(outC)
errC := make(chan string)
defer close(errC)
f, errGo := os.Create(outputFN)
if errGo != nil {
return kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime()).With("outputFN", outputFN)
}
stopCopy := make(chan struct{}, 1)
go procOutput(stopCopy, f, outC, errC)
if errGo = cmd.Start(); err != nil {
return kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime())
}
waitOnIO := sync.WaitGroup{}
waitOnIO.Add(2)
go func() {
defer waitOnIO.Done()
time.Sleep(time.Second)
s := bufio.NewScanner(stdout)
s.Split(bufio.ScanRunes)
for s.Scan() {
outC <- s.Bytes()
}
if errGo := s.Err(); errGo != nil {
if err != nil {
err = kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime())
}
}
}()
go func() {
defer waitOnIO.Done()
time.Sleep(time.Second)
s := bufio.NewScanner(stderr)
s.Split(bufio.ScanLines)
for s.Scan() {
errC <- s.Text()
}
if errGo := s.Err(); errGo != nil {
if err != nil {
err = kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime())
}
}
}()
go func() {
for {
select {
case <-ctx.Done():
if errGo := cmd.Process.Kill(); errGo != nil {
msg := fmt.Sprintf("could not be killed, maximum life time reached, due to %v", errGo)
select {
case errorC <- &msg:
default:
}
return
}
msg := "killed, maximum life time reached"
select {
case errorC <- &msg:
default:
}
return
case <-stopCmd.Done():
return
}
}
}()
if errGo = cmd.Wait(); err != nil {
return kv.Wrap(errGo).With("stack", stack.Trace().TrimRuntime())
}
waitOnIO.Wait()
close(stopCopy)
if err == nil && ctx.Err() != nil {
err = kv.Wrap(ctx.Err()).With("stack", stack.Trace().TrimRuntime())
}
return err
}
// Close is a stub method for termination of a singularity resource
func (*Singularity) Close() (err kv.Error) {
return nil
}