forked from triton-inference-server/server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compose.py
425 lines (381 loc) · 15.8 KB
/
compose.py
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
#!/usr/bin/env python3
# Copyright 2021-2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of NVIDIA CORPORATION nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import argparse
import os
import platform
import subprocess
import sys
FLAGS = None
#### helper functions
def log(msg, force=False):
if force or not FLAGS.quiet:
try:
print(msg, file=sys.stderr)
except Exception:
print('<failed to log>', file=sys.stderr)
def log_verbose(msg):
if FLAGS.verbose:
log(msg, force=True)
def fail(msg):
print('error: {}'.format(msg), file=sys.stderr)
sys.exit(1)
def fail_if(p, msg):
if p:
fail(msg)
def start_dockerfile(ddir, images, argmap, dockerfile_name, backends):
# Set enviroment variables, set default user and install dependencies
df = '''
#
# Multistage build.
#
ARG TRITON_VERSION={}
ARG TRITON_CONTAINER_VERSION={}
FROM {} AS full
'''.format(argmap['TRITON_VERSION'], argmap['TRITON_CONTAINER_VERSION'],
images["full"])
# PyTorch backend needs extra CUDA and other dependencies during runtime
# that are missing in the CPU only base container. These dependencies
# must be copied from the Triton Min image
if not FLAGS.enable_gpu and ('pytorch' in backends):
df += '''
FROM {} AS min_container
'''.format(images["gpu-min"])
df += '''
FROM {}
'''.format(images["min"])
import build
df += build.dockerfile_prepare_container_linux(argmap, backends,
FLAGS.enable_gpu,
platform.machine().lower())
# Copy over files
df += '''
WORKDIR /opt/tritonserver
COPY --chown=1000:1000 --from=full /opt/tritonserver/LICENSE .
COPY --chown=1000:1000 --from=full /opt/tritonserver/TRITON_VERSION .
COPY --chown=1000:1000 --from=full /opt/tritonserver/NVIDIA_Deep_Learning_Container_License.pdf .
COPY --chown=1000:1000 --from=full /opt/tritonserver/bin bin/
COPY --chown=1000:1000 --from=full /opt/tritonserver/lib lib/
COPY --chown=1000:1000 --from=full /opt/tritonserver/include include/
'''
with open(os.path.join(ddir, dockerfile_name), "w") as dfile:
dfile.write(df)
def add_requested_backends(ddir, dockerfile_name, backends):
df = "# Copying over backends \n"
for backend in backends:
if backend == 'openvino':
import build
ver = next(iter(build.TRITON_VERSION_MAP.values()))
backend = build.tagged_backend(backend, ver[4][0])
df += '''COPY --chown=1000:1000 --from=full /opt/tritonserver/backends/{} /opt/tritonserver/backends/{}
'''.format(backend, backend)
if len(backends) > 0:
df += '''
# Top-level /opt/tritonserver/backends not copied so need to explicitly set permissions here
RUN chown triton-server:triton-server /opt/tritonserver/backends
'''
with open(os.path.join(ddir, dockerfile_name), "a") as dfile:
dfile.write(df)
def add_requested_repoagents(ddir, dockerfile_name, repoagents):
df = "# Copying over repoagents \n"
for ra in repoagents:
df += '''COPY --chown=1000:1000 --from=full /opt/tritonserver/repoagents/{} /opt/tritonserver/repoagents/{}
'''.format(ra, ra)
if len(repoagents) > 0:
df += '''
# Top-level /opt/tritonserver/repoagents not copied so need to explicitly set permissions here
RUN chown triton-server:triton-server /opt/tritonserver/repoagents
'''
with open(os.path.join(ddir, dockerfile_name), "a") as dfile:
dfile.write(df)
def end_dockerfile(ddir, dockerfile_name, argmap):
# Install additional dependencies
df = ""
if argmap['SAGEMAKER_ENDPOINT']:
df += '''
LABEL com.amazonaws.sagemaker.capabilities.accept-bind-to-port=true
COPY --chown=1000:1000 --from=full /usr/bin/serve /usr/bin/.
'''
with open(os.path.join(ddir, dockerfile_name), "a") as dfile:
dfile.write(df)
def build_docker_image(ddir, dockerfile_name, container_name):
# Create container with docker build
p = subprocess.Popen(['docker', 'build', '-t', container_name, '-f', \
os.path.join(ddir, dockerfile_name), '.'])
p.wait()
fail_if(p.returncode != 0, 'docker build {} failed'.format(container_name))
def get_container_version_if_not_specified():
if FLAGS.container_version is None:
# Read from TRITON_VERSION file in server repo to determine version
with open('TRITON_VERSION', "r") as vfile:
version = vfile.readline().strip()
import build
_, FLAGS.container_version = build.container_versions(
version, None, FLAGS.container_version)
log('version {}'.format(version))
log('using container version {}'.format(FLAGS.container_version))
def create_argmap(images):
# Extract information from upstream build and create map other functions can
# use
full_docker_image = images["full"]
min_docker_image = images["min"]
enable_gpu = FLAGS.enable_gpu
# Docker inspect enviroment variables
base_run_args = ['docker', 'inspect', '-f']
import re # parse all PATH enviroment variables
# first pull docker images
log("pulling container:{}".format(full_docker_image))
p = subprocess.run(['docker', 'pull', full_docker_image])
fail_if(
p.returncode != 0,
'docker pull container {} failed, {}'.format(full_docker_image,
p.stderr))
if enable_gpu:
pm = subprocess.run(['docker', 'pull', min_docker_image])
fail_if(
pm.returncode != 0, 'docker pull container {} failed, {}'.format(
min_docker_image, pm.stderr))
pm_path = subprocess.run(base_run_args + [
'{{range $index, $value := .Config.Env}}{{$value}} {{end}}',
min_docker_image
],
capture_output=True,
text=True)
fail_if(
pm_path.returncode != 0,
'docker inspect to find triton enviroment variables for min container failed, {}'
.format(pm_path.stderr))
# min container needs to be GPU support enabled if the build is GPU build
vars = pm_path.stdout
e = re.search("CUDA_VERSION", vars)
gpu_enabled = False if e is None else True
fail_if(
not gpu_enabled,
'Composing container with gpu support enabled but min container provided does not have CUDA installed'
)
# Check full container enviroment variables
p_path = subprocess.run(base_run_args + [
'{{range $index, $value := .Config.Env}}{{$value}} {{end}}',
full_docker_image
],
capture_output=True,
text=True)
fail_if(
p_path.returncode != 0,
'docker inspect to find enviroment variables for full container failed, {}'
.format(p_path.stderr))
vars = p_path.stdout
log_verbose("inspect args: {}".format(vars))
e0 = re.search("TRITON_SERVER_GPU_ENABLED=([\S]{1,}) ", vars)
e1 = re.search("CUDA_VERSION", vars)
gpu_enabled = False
if (e0 != None):
gpu_enabled = e0.group(1) == "1"
elif (e1 != None):
gpu_enabled = True
fail_if(
gpu_enabled != enable_gpu,
'Error: full container provided was build with \'TRITON_SERVER_GPU_ENABLED\' as {} and you are composing container with \'TRITON_SERVER_GPU_ENABLED\' as {}'
.format(gpu_enabled, enable_gpu))
e = re.search("TRITON_SERVER_VERSION=([\S]{6,}) ", vars)
version = "" if e is None else e.group(1)
fail_if(
len(version) == 0,
'docker inspect to find triton server version failed, {}'.format(
p_path.stderr))
e = re.search("NVIDIA_TRITON_SERVER_VERSION=([\S]{5,}) ", vars)
container_version = "" if e is None else e.group(1)
fail_if(
len(container_version) == 0,
'docker inspect to find triton container version failed, {}'.format(
vars))
dcgm_ver = re.search("DCGM_VERSION=([\S]{4,}) ", vars)
dcgm_version = ""
if dcgm_ver is None:
dcgm_version = "2.2.3"
log("WARNING: DCGM version not found from image, installing the earlierst version {}"
.format(dcgm_version))
else:
dcgm_version = dcgm_ver.group(1)
fail_if(
len(dcgm_version) == 0,
'docker inspect to find DCGM version failed, {}'.format(vars))
p_sha = subprocess.run(
base_run_args +
['{{ index .Config.Labels "com.nvidia.build.ref"}}', full_docker_image],
capture_output=True,
text=True)
fail_if(
p_sha.returncode != 0,
'docker inspect of upstream docker image build sha failed, {}'.format(
p_sha.stderr))
p_build = subprocess.run(
base_run_args +
['{{ index .Config.Labels "com.nvidia.build.id"}}', full_docker_image],
capture_output=True,
text=True)
fail_if(
p_build.returncode != 0,
'docker inspect of upstream docker image build sha failed, {}'.format(
p_build.stderr))
p_find = subprocess.run(
['docker', 'run', full_docker_image, 'bash', '-c', 'ls /usr/bin/'],
capture_output=True,
text=True)
f = re.search("serve", p_find.stdout)
fail_if(p_find.returncode != 0,
"Cannot search for 'serve' in /usr/bin, {}".format(p_find.stderr))
argmap = {
'NVIDIA_BUILD_REF': p_sha.stdout.rstrip(),
'NVIDIA_BUILD_ID': p_build.stdout.rstrip(),
'TRITON_VERSION': version,
'TRITON_CONTAINER_VERSION': container_version,
'DCGM_VERSION': dcgm_version,
'SAGEMAKER_ENDPOINT': f is not None,
}
return argmap
if __name__ == '__main__':
parser = argparse.ArgumentParser()
group_qv = parser.add_mutually_exclusive_group()
group_qv.add_argument('-q',
'--quiet',
action="store_true",
required=False,
help='Disable console output.')
group_qv.add_argument('-v',
'--verbose',
action="store_true",
required=False,
help='Enable verbose output.')
parser.add_argument(
'--output-name',
type=str,
required=False,
help='Name for the generated Docker image. Default is "tritonserver".')
parser.add_argument(
'--work-dir',
type=str,
required=False,
help=
'Generated dockerfiles are placed here. Default to current directory.')
parser.add_argument(
'--container-version',
type=str,
required=False,
help=
'The version to use for the generated Docker image. If not specified the container version will be chosen automatically based on the repository branch.'
)
parser.add_argument(
'--image',
action='append',
required=False,
help=
'Use specified Docker image to generate Docker image. Specified as <image-name>,<full-image-name>. <image-name> can be "min", "gpu-min" or "full". Both "min" and "full" need to be specified at the same time. This will override "--container-version". "gpu-min" is needed for CPU only container to copy TensorFlow and PyTorch deps.'
)
parser.add_argument('--enable-gpu',
nargs='?',
type=lambda x: (str(x).lower() == 'true'),
const=True,
default=True,
required=False,
help=argparse.SUPPRESS)
parser.add_argument(
'--backend',
action='append',
required=False,
help=
'Include <backend-name> in the generated Docker image. The flag may be specified multiple times.'
)
parser.add_argument(
'--repoagent',
action='append',
required=False,
help=
'Include <repoagent-name> in the generated Docker image. The flag may be specified multiple times.'
)
parser.add_argument(
'--dry-run',
action="store_true",
required=False,
help='Only creates Dockerfile.compose, does not build the Docker image.'
)
FLAGS = parser.parse_args()
if FLAGS.work_dir is None:
FLAGS.work_dir = "."
if FLAGS.output_name is None:
FLAGS.output_name = "tritonserver"
dockerfile_name = 'Dockerfile.compose'
if FLAGS.backend is None:
FLAGS.backend = []
if FLAGS.repoagent is None:
FLAGS.repoagent = []
# Initialize map of docker images.
images = {}
if FLAGS.image:
for img in FLAGS.image:
parts = img.split(',')
fail_if(
len(parts) != 2,
'--image must specific <image-name>,<full-image-registry>')
fail_if(parts[0] not in ['min', 'full', 'gpu-min'],
'unsupported image-name \'{}\' for --image'.format(parts[0]))
log('image "{}": "{}"'.format(parts[0], parts[1]))
images[parts[0]] = parts[1]
else:
get_container_version_if_not_specified()
if FLAGS.enable_gpu:
images = {
"full":
"nvcr.io/nvidia/tritonserver:{}-py3".format(
FLAGS.container_version),
"min":
"nvcr.io/nvidia/tritonserver:{}-py3-min".format(
FLAGS.container_version)
}
else:
images = {
"full":
"nvcr.io/nvidia/tritonserver:{}-cpu-only-py3".format(
FLAGS.container_version),
"min":
"ubuntu:20.04"
}
fail_if(
len(images) < 2,
"Need to specify both 'full' and 'min' images if at all")
# For cpu-only image we need to copy some cuda libraries and dependencies
# since we are using a PyTorch container that is not CPU-only
if ('pytorch' in FLAGS.backend) and ('gpu-min' not in images):
images["gpu-min"] = "nvcr.io/nvidia/tritonserver:{}-py3-min".format(
FLAGS.container_version)
argmap = create_argmap(images)
start_dockerfile(FLAGS.work_dir, images, argmap, dockerfile_name,
FLAGS.backend)
add_requested_backends(FLAGS.work_dir, dockerfile_name, FLAGS.backend)
add_requested_repoagents(FLAGS.work_dir, dockerfile_name, FLAGS.repoagent)
end_dockerfile(FLAGS.work_dir, dockerfile_name, argmap)
if (not FLAGS.dry_run):
build_docker_image(FLAGS.work_dir, dockerfile_name, FLAGS.output_name)