run.sh 9.92 KB
Newer Older
Carsten Csiky's avatar
Carsten Csiky committed
1
#!/usr/bin/env bash
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# 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.

Carsten Csiky's avatar
Carsten Csiky committed
17
18
set -e

19
20
21
22
23
24
25
26
RUN_PREFIX=

# Frameworks
#
# Each framework has a corresponding base image.  Additional
# dependencies are specified in the /container/deps folder and
# installed within framework specific sections of the Dockerfile.

27
declare -A FRAMEWORKS=(["VLLM"]=1 ["TRTLLM"]=2 ["NONE"]=3 ["SGLANG"]=4)
Ryan Olson's avatar
Ryan Olson committed
28

29
DEFAULT_FRAMEWORK=VLLM
30
31
32
33

SOURCE_DIR=$(dirname "$(readlink -f "$0")")

IMAGE=
34
35
HF_HOME=${HF_HOME:-}
DEFAULT_HF_HOME=${SOURCE_DIR}/.cache/huggingface
36
37
38
GPUS="all"
PRIVILEGED=
VOLUME_MOUNTS=
39
PORT_MAPPINGS=
40
41
42
43
MOUNT_WORKSPACE=
ENVIRONMENT_VARIABLES=
REMAINING_ARGS=
INTERACTIVE=
44
USE_NIXL_GDS=
45
46
RUNTIME=nvidia
WORKDIR=/workspace
47
NETWORK=host
48
49
50
51
52
53
54
55

get_options() {
    while :; do
        case $1 in
        -h | -\? | --help)
            show_help
            exit
            ;;
56
        --framework)
57
58
59
60
            if [ "$2" ]; then
                FRAMEWORK=$2
                shift
            else
61
                missing_requirement "$1"
62
63
64
65
66
67
68
            fi
            ;;
        --image)
            if [ "$2" ]; then
                IMAGE=$2
                shift
            else
69
                missing_requirement "$1"
70
71
            fi
            ;;
72
73
74
75
76
        --target)
            if [ "$2" ]; then
                TARGET=$2
                shift
            else
Carsten Csiky's avatar
Carsten Csiky committed
77
                missing_requirement "$1"
78
79
            fi
            ;;
80
        --name)
81
82
83
84
            if [ "$2" ]; then
                NAME=$2
                shift
            else
85
                missing_requirement "$1"
86
87
            fi
            ;;
88
        --hf-cache|--hf-home)
89
            if [ "$2" ]; then
90
                HF_HOME=$2
91
92
                shift
            else
93
                missing_requirement "$1"
94
95
96
            fi
            ;;

97
        --gpus)
98
99
100
101
            if [ "$2" ]; then
                GPUS=$2
                shift
            else
102
                missing_requirement "$1"
103
104
            fi
            ;;
105
        --runtime)
106
107
108
109
            if [ "$2" ]; then
                RUNTIME=$2
                shift
            else
110
                missing_requirement "$1"
111
112
            fi
            ;;
113
        --entrypoint)
114
            if [ "$2" ]; then
115
                ENTRYPOINT=$2
116
117
                shift
            else
118
119
120
121
122
123
124
125
126
                missing_requirement "$1"
            fi
            ;;
        --workdir)
            if [ "$2" ]; then
                WORKDIR="$2"
                shift
            else
                missing_requirement "$1"
127
128
            fi
            ;;
129
        --privileged)
130
131
132
133
            if [ "$2" ]; then
                PRIVILEGED=$2
                shift
            else
134
                missing_requirement "$1"
135
136
            fi
            ;;
137
        --rm)
138
139
140
141
            if [ "$2" ]; then
                RM=$2
                shift
            else
142
                missing_requirement "$1"
143
144
            fi
            ;;
145
        -v)
146
147
148
149
            if [ "$2" ]; then
                VOLUME_MOUNTS+=" -v $2 "
                shift
            else
150
                missing_requirement "$1"
151
152
            fi
            ;;
153
154
155
156
157
158
159
160
        -p|--port)
            if [ "$2" ]; then
                PORT_MAPPINGS+=" -p $2 "
                shift
            else
                missing_requirement "$1"
            fi
            ;;
161
        -e)
162
163
164
165
            if [ "$2" ]; then
                ENVIRONMENT_VARIABLES+=" -e $2 "
                shift
            else
166
                missing_requirement "$1"
167
168
            fi
            ;;
169
170
171
172
173
174
        -it)
            INTERACTIVE=" -it "
            ;;
        --mount-workspace)
            MOUNT_WORKSPACE=TRUE
            ;;
175
176
177
        --use-nixl-gds)
            USE_NIXL_GDS=TRUE
            ;;
178
179
180
181
182
183
184
185
        --network)
            if [ "$2" ]; then
                NETWORK=$2
                shift
            else
                missing_requirement "$1"
            fi
            ;;
186
187
188
189
190
191
192
193
194
195
196
197
198
        --dry-run)
            RUN_PREFIX="echo"
            echo ""
            echo "=============================="
            echo "DRY RUN: COMMANDS PRINTED ONLY"
            echo "=============================="
            echo ""
            ;;
        --)
            shift
            break
            ;;
         -?*)
199
            error 'ERROR: Unknown option: ' "$1"
200
            ;;
201
202
         ?*)
            error 'ERROR: Unknown option: ' "$1"
203
204
205
206
207
208
209
210
211
212
            ;;
        *)
            break
            ;;
        esac

        shift
    done

    if [ -z "$FRAMEWORK" ]; then
213
        FRAMEWORK=$DEFAULT_FRAMEWORK
214
215
    fi

Carsten Csiky's avatar
Carsten Csiky committed
216
    if [ -n "$FRAMEWORK" ]; then
217
218
219
220
        FRAMEWORK=${FRAMEWORK^^}
        if [[ -z "${FRAMEWORKS[$FRAMEWORK]}" ]]; then
            error 'ERROR: Unknown framework: ' "$FRAMEWORK"
        fi
221
222
223
    fi

    if [ -z "$IMAGE" ]; then
224
        IMAGE="dynamo:latest-${FRAMEWORK,,}"
Carsten Csiky's avatar
Carsten Csiky committed
225
        if [ -n "${TARGET}" ]; then
226
227
            IMAGE="${IMAGE}-${TARGET}"
        fi
228
229
230
    fi

    if [[ ${GPUS^^} == "NONE" ]]; then
231
        GPU_STRING=""
232
    else
233
        GPU_STRING="--gpus ${GPUS}"
234
235
236
    fi

    if [[ ${NAME^^} == "" ]]; then
237
        NAME_STRING=""
238
    else
239
        NAME_STRING="--name ${NAME}"
240
241
    fi

242
    if [[ ${ENTRYPOINT^^} == "" ]]; then
243
        ENTRYPOINT_STRING=""
244
    else
245
        ENTRYPOINT_STRING="--entrypoint ${ENTRYPOINT}"
246
247
    fi

248
249
250
251
252
    if [ -n "$MOUNT_WORKSPACE" ]; then
        VOLUME_MOUNTS+=" -v ${SOURCE_DIR}/..:/workspace "
        VOLUME_MOUNTS+=" -v /tmp:/tmp "
        VOLUME_MOUNTS+=" -v /mnt/:/mnt "

253
254
        if [ -z "$HF_HOME" ]; then
            HF_HOME=$DEFAULT_HF_HOME
255
256
257
258
259
260
261
262
263
        fi

        if [ -z "${PRIVILEGED}" ]; then
            PRIVILEGED="TRUE"
        fi

        ENVIRONMENT_VARIABLES+=" -e HF_TOKEN"
    fi

264
265
    if [[ ${HF_HOME^^} == "NONE" ]]; then
        HF_HOME=
266
267
    fi

268
269
    if [ -n "$HF_HOME" ]; then
        mkdir -p "$HF_HOME"
270
271
        # Use /home/ubuntu for local-dev target, /root for dev target.
        if [ "$TARGET" = "local-dev" ] || [[ "$IMAGE" == *"local-dev"* ]]; then
272
            HF_HOME_TARGET="/home/ubuntu/.cache/huggingface"
273
        else
274
            HF_HOME_TARGET="/root/.cache/huggingface"
275
        fi
276
        VOLUME_MOUNTS+=" -v $HF_HOME:$HF_HOME_TARGET"
277
    fi
278

Carsten Csiky's avatar
Carsten Csiky committed
279
    if [ -z "${PRIVILEGED}" ]; then
280
        PRIVILEGED="FALSE"
281
282
    fi

Carsten Csiky's avatar
Carsten Csiky committed
283
    if [ -z "${RM}" ]; then
284
        RM="TRUE"
285
286
    fi

287
288
289
    if [[ ${PRIVILEGED^^} == "FALSE" ]]; then
        PRIVILEGED_STRING=""
    else
290
        PRIVILEGED_STRING="--privileged"
291
292
    fi

293
    if [[ ${RM^^} == "FALSE" ]]; then
294
        RM_STRING=""
295
    else
296
        RM_STRING=" --rm "
297
298
    fi

299
300
301
    if [ -n "$USE_NIXL_GDS" ]; then
        VOLUME_MOUNTS+=" -v /run/udev:/run/udev:ro "
        NIXL_GDS_CAPS="--cap-add=IPC_LOCK"
Ryan Olson's avatar
Ryan Olson committed
302
303
304
305
306
307
308
        # NOTE(jthomson04): In the KVBM disk pools, we currently allocate our files in /tmp.
        # For some arcane reason, GDS requires that /tmp be mounted.
        # This is already handled for us if we set --mount-workspace
        # If we aren't mounting our workspace but need GDS, we need to mount /tmp.
        if [ -z "$MOUNT_WORKSPACE" ]; then
            VOLUME_MOUNTS+=" -v /tmp:/tmp "
        fi
309
310
311
    else
        NIXL_GDS_CAPS=""
    fi
312
    if [[ "$GPUS" == "none" || "$GPUS" == "NONE" ]]; then
313
            RUNTIME=""
314
    fi
315

316
317
318
319
320
321
    REMAINING_ARGS=("$@")
}

show_help() {
    echo "usage: run.sh"
    echo "  [--image image]"
Carsten Csiky's avatar
Carsten Csiky committed
322
    echo "  [--framework framework one of ${!FRAMEWORKS[*]}]"
323
    echo "  [--name name for launched container, default NONE]"
324
325
    echo "  [--privileged whether to launch in privileged mode, default FALSE unless mounting workspace]"
    echo "  [--dry-run print docker commands without running]"
326
    echo "  [--hf-home|--hf-cache directory to volume mount as the hf home, default is NONE unless mounting workspace]"
327
    echo "  [--gpus gpus to enable, default is 'all', 'none' disables gpu support]"
328
    echo "  [--use-nixl-gds add volume mounts and capabilities needed for NVIDIA GPUDirect Storage]"
329
330
331
332
    echo "  [--network network mode for container, default is 'host']"
    echo "           Options: 'host' (default), 'bridge', 'none', 'container:name'"
    echo "           Examples: --network bridge (isolated), --network none (no network - WARNING: breaks most functionality)"
    echo "                    --network container:redis (share network with 'redis' container)"
333
    echo "  [-v add volume mount]"
334
    echo "  [-p|--port add port mapping (host_port:container_port)]"
335
336
337
    echo "  [-e add environment variable]"
    echo "  [--mount-workspace set up for local development]"
    echo "  [-- stop processing and pass remaining args as command to docker run]"
338
339
    echo "  [--workdir set the working directory inside the container]"
    echo "  [--runtime add runtime variables]"
340
341
    echo "  [--entrypoint override container entrypoint]"
    echo "  [-h, --help show this help]"
342
343
344
345
346
347
348
349
350
351
352
353
354
355
    exit 0
}

missing_requirement() {
    error "ERROR: $1 requires an argument."
}

error() {
    printf '%s %s\n' "$1" "$2" >&2
    exit 1
}

get_options "$@"

356
# RUN the image
357
358
359
360
if [ -z "$RUN_PREFIX" ]; then
    set -x
fi

361
362
363
364
${RUN_PREFIX} docker run \
    ${GPU_STRING} \
    ${INTERACTIVE} \
    ${RM_STRING} \
365
    --network "$NETWORK" \
366
    ${RUNTIME:+--runtime "$RUNTIME"} \
367
368
369
370
371
372
    --shm-size=10G \
    --ulimit memlock=-1 \
    --ulimit stack=67108864 \
    --ulimit nofile=65536:65536 \
    ${ENVIRONMENT_VARIABLES} \
    ${VOLUME_MOUNTS} \
373
    ${PORT_MAPPINGS} \
374
    -w "$WORKDIR" \
375
    --cap-add CAP_SYS_PTRACE \
376
    ${NIXL_GDS_CAPS} \
377
378
379
380
381
382
    --ipc host \
    ${PRIVILEGED_STRING} \
    ${NAME_STRING} \
    ${ENTRYPOINT_STRING} \
    ${IMAGE} \
    "${REMAINING_ARGS[@]}"
383
384

{ set +x; } 2>/dev/null