run.sh 9.93 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
34
35
36
37
38

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

IMAGE=
HF_CACHE=
DEFAULT_HF_CACHE=${SOURCE_DIR}/.cache/huggingface
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)
89
90
91
92
            if [ "$2" ]; then
                HF_CACHE=$2
                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
253
254
255
256
257
258
259
260
261
262
263
264
265
    if [ -n "$MOUNT_WORKSPACE" ]; then
        VOLUME_MOUNTS+=" -v ${SOURCE_DIR}/..:/workspace "
        VOLUME_MOUNTS+=" -v /tmp:/tmp "
        VOLUME_MOUNTS+=" -v /mnt/:/mnt "

        if [ -z "$HF_CACHE" ]; then
            HF_CACHE=$DEFAULT_HF_CACHE
        fi

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

        ENVIRONMENT_VARIABLES+=" -e HF_TOKEN"

        INTERACTIVE=" -it "
    fi

266
    if [[ ${HF_CACHE^^} == "NONE" ]]; then
267
        HF_CACHE=
268
269
    fi

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

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

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

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

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

301
302
303
    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
304
305
306
307
308
309
310
        # 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
311
312
313
    else
        NIXL_GDS_CAPS=""
    fi
314
    if [[ "$GPUS" == "none" || "$GPUS" == "NONE" ]]; then
315
            RUNTIME=""
316
    fi
317

318
319
320
321
322
323
    REMAINING_ARGS=("$@")
}

show_help() {
    echo "usage: run.sh"
    echo "  [--image image]"
Carsten Csiky's avatar
Carsten Csiky committed
324
    echo "  [--framework framework one of ${!FRAMEWORKS[*]}]"
325
    echo "  [--name name for launched container, default NONE]"
326
327
328
329
    echo "  [--privileged whether to launch in privileged mode, default FALSE unless mounting workspace]"
    echo "  [--dry-run print docker commands without running]"
    echo "  [--hf-cache directory to volume mount as the hf cache, default is NONE unless mounting workspace]"
    echo "  [--gpus gpus to enable, default is 'all', 'none' disables gpu support]"
330
    echo "  [--use-nixl-gds add volume mounts and capabilities needed for NVIDIA GPUDirect Storage]"
331
332
333
334
    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)"
335
    echo "  [-v add volume mount]"
336
    echo "  [-p|--port add port mapping (host_port:container_port)]"
337
338
339
    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]"
340
341
    echo "  [--workdir set the working directory inside the container]"
    echo "  [--runtime add runtime variables]"
342
343
    echo "  [--entrypoint override container entrypoint]"
    echo "  [-h, --help show this help]"
344
345
346
347
348
349
350
351
352
353
354
355
356
357
    exit 0
}

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

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

get_options "$@"

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

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

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