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

17

18
19
20
TAG=
RUN_PREFIX=
PLATFORM=linux/amd64
21
22
23
24

# Get short commit hash
commit_id=$(git rev-parse --short HEAD)

25
26
# if COMMIT_ID matches a TAG use that
current_tag=$(git describe --tags --exact-match 2>/dev/null | sed 's/^v//') || true
27

28
29
# Get latest TAG and add COMMIT_ID for dev
latest_tag=$(git describe --tags --abbrev=0 $(git rev-list --tags --max-count=1 main) | sed 's/^v//') || true
30
31
32
33
if [[ -z ${latest_tag} ]]; then
    latest_tag="0.0.1"
    echo "No git release tag found, setting to unknown version: ${latest_tag}"
fi
34

35
36
37
38
# Use tag if available, otherwise use latest_tag.dev.commit_id
VERSION=v${current_tag:-$latest_tag.dev.$commit_id}

PYTHON_PACKAGE_VERSION=${current_tag:-$latest_tag.dev+$commit_id}
39
40
41
42
43
44

# 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.
45

46
declare -A FRAMEWORKS=(["VLLM"]=1 ["TENSORRTLLM"]=2 ["NONE"]=3)
47
DEFAULT_FRAMEWORK=VLLM
48
49
50
51
52
53

SOURCE_DIR=$(dirname "$(readlink -f "$0")")
DOCKERFILE=${SOURCE_DIR}/Dockerfile
BUILD_CONTEXT=$(dirname "$(readlink -f "$SOURCE_DIR")")

# Base Images
54
55
TENSORRTLLM_BASE_IMAGE=tensorrt_llm/release
TENSORRTLLM_BASE_IMAGE_TAG=latest
56
TENSORRTLLM_PIP_WHEEL_PATH=""
57

58
59
VLLM_BASE_IMAGE="nvcr.io/nvidia/cuda-dl-base"
VLLM_BASE_IMAGE_TAG="25.01-cuda12.8-devel-ubuntu24.04"
60

61
62
63
NONE_BASE_IMAGE="ubuntu"
NONE_BASE_IMAGE_TAG="24.04"

64
NIXL_COMMIT=f35faf8ba4e725f1724177d0772200481d1d3446
65
66
NIXL_REPO=ai-dynamo/nixl.git

67
68
69
70
71
72
73
get_options() {
    while :; do
        case $1 in
        -h | -\? | --help)
            show_help
            exit
            ;;
74
        --platform)
75
76
77
78
            if [ "$2" ]; then
                PLATFORM=$2
                shift
            else
79
                missing_requirement $1
80
81
            fi
            ;;
82
        --framework)
83
84
85
86
            if [ "$2" ]; then
                FRAMEWORK=$2
                shift
            else
87
                missing_requirement $1
88
89
            fi
            ;;
90
        --tensorrtllm-pip-wheel-path)
91
            if [ "$2" ]; then
92
                TENSORRTLLM_PIP_WHEEL_PATH=$2
93
94
95
96
97
                shift
            else
                missing_requirement $1
            fi
            ;;
98
99
100
101
102
        --base-image)
            if [ "$2" ]; then
                BASE_IMAGE=$2
                shift
            else
103
                missing_requirement $1
104
105
            fi
            ;;
106
        --base-image-tag)
107
108
109
110
            if [ "$2" ]; then
                BASE_IMAGE_TAG=$2
                shift
            else
111
112
113
114
115
116
117
118
119
                missing_requirement $1
            fi
            ;;
        --target)
            if [ "$2" ]; then
                TARGET=$2
                shift
            else
                missing_requirement $1
120
121
122
123
124
125
126
            fi
            ;;
        --build-arg)
            if [ "$2" ]; then
                BUILD_ARGS+="--build-arg $2 "
                shift
            else
127
                missing_requirement $1
128
129
130
131
            fi
            ;;
        --tag)
            if [ "$2" ]; then
132
                TAG="--tag $2"
133
134
                shift
            else
135
                missing_requirement $1
136
137
138
139
140
141
142
143
144
145
            fi
            ;;
        --dry-run)
            RUN_PREFIX="echo"
            echo ""
            echo "=============================="
            echo "DRY RUN: COMMANDS PRINTED ONLY"
            echo "=============================="
            echo ""
            ;;
146
147
        --no-cache)
            NO_CACHE=" --no-cache"
148
            ;;
149
150
        --cache-from)
            if [ "$2" ]; then
151
152
153
                CACHE_FROM="--cache-from $2"
                shift
            else
154
                missing_requirement $1
155
156
            fi
            ;;
157
158
159
160
161
162
163
164
        --cache-to)
            if [ "$2" ]; then
                CACHE_TO="--cache-to $2"
                shift
            else
                missing_requirement $1
            fi
            ;;
ptarasiewiczNV's avatar
ptarasiewiczNV committed
165
166
167
168
169
170
171
172
        --build-context)
            if [ "$2" ]; then
                BUILD_CONTEXT_ARG="--build-context $2"
                shift
            else
                missing_requirement $1
            fi
            ;;
173
174
175
176
177
        --)
            shift
            break
            ;;
         -?*)
178
            error 'ERROR: Unknown option: ' $1
179
            ;;
180
181
         ?*)
            error 'ERROR: Unknown option: ' $1
182
183
184
185
186
187
188
189
190
            ;;
        *)
            break
            ;;
        esac
        shift
    done

    if [ -z "$FRAMEWORK" ]; then
191
        FRAMEWORK=$DEFAULT_FRAMEWORK
192
193
194
    fi

    if [ ! -z "$FRAMEWORK" ]; then
195
        FRAMEWORK=${FRAMEWORK^^}
196

197
198
199
        if [[ ! -n "${FRAMEWORKS[$FRAMEWORK]}" ]]; then
            error 'ERROR: Unknown framework: ' $FRAMEWORK
        fi
200

201
202
203
204
        if [ -z $BASE_IMAGE_TAG ]; then
            BASE_IMAGE_TAG=${FRAMEWORK}_BASE_IMAGE_TAG
            BASE_IMAGE_TAG=${!BASE_IMAGE_TAG}
        fi
205

206
207
208
209
        if [ -z $BASE_IMAGE ]; then
            BASE_IMAGE=${FRAMEWORK}_BASE_IMAGE
            BASE_IMAGE=${!BASE_IMAGE}
        fi
210

211
212
213
        if [ -z $BASE_IMAGE ]; then
            error "ERROR: Framework $FRAMEWORK without BASE_IMAGE"
        fi
214

215
216
        BASE_VERSION=${FRAMEWORK}_BASE_VERSION
        BASE_VERSION=${!BASE_VERSION}
217
218
219
220

    fi

    if [ -z "$TAG" ]; then
221
        TAG="--tag dynamo:${VERSION}-${FRAMEWORK,,}"
222
        if [ ! -z "${TARGET}" ]; then
223
224
            TAG="${TAG}-${TARGET}"
        fi
225
226
227
228
229
230
    fi

    if [ ! -z "$PLATFORM" ]; then
        PLATFORM="--platform ${PLATFORM}"
    fi

231
232
    if [ ! -z "$TARGET" ]; then
        TARGET_STR="--target ${TARGET}"
233
    else
234
        TARGET_STR="--target dev"
235
    fi
236
237
238
239
240
}


show_image_options() {
    echo ""
241
    echo "Building Dynamo Image: '${TAG}'"
242
243
244
245
    echo ""
    echo "   Base: '${BASE_IMAGE}'"
    echo "   Base_Image_Tag: '${BASE_IMAGE_TAG}'"
    if [[ $FRAMEWORK == "TENSORRTLLM" ]]; then
246
        echo "   Tensorrtllm_Pip_Wheel_Path: '${TENSORRTLLM_PIP_WHEEL_PATH}'"
247
248
249
250
251
252
253
254
255
256
257
258
259
    fi
    echo "   Build Context: '${BUILD_CONTEXT}'"
    echo "   Build Arguments: '${BUILD_ARGS}'"
    echo "   Framework: '${FRAMEWORK}'"
    echo ""
}

show_help() {
    echo "usage: build.sh"
    echo "  [--base base image]"
    echo "  [--base-imge-tag base image tag]"
    echo "  [--platform platform for docker build"
    echo "  [--framework framework one of ${!FRAMEWORKS[@]}]"
260
    echo "  [--tensorrtllm-pip-wheel-path path to tensorrtllm pip wheel]"
261
    echo "  [--build-arg additional build args to pass to docker build]"
262
263
    echo "  [--cache-from cache location to start from]"
    echo "  [--cache-to location where to cache the build output]"
264
265
266
    echo "  [--tag tag for image]"
    echo "  [--no-cache disable docker build cache]"
    echo "  [--dry-run print docker commands without running]"
ptarasiewiczNV's avatar
ptarasiewiczNV committed
267
    echo "  [--build-context name=path to add build context]"
268
269
270
271
272
273
274
275
276
277
278
279
280
281
    exit 0
}

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

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

get_options "$@"

282
283
284
# Update DOCKERFILE if framework is VLLM
if [[ $FRAMEWORK == "VLLM" ]]; then
    DOCKERFILE=${SOURCE_DIR}/Dockerfile.vllm
285
286
elif [[ $FRAMEWORK == "TENSORRTLLM" ]]; then
    DOCKERFILE=${SOURCE_DIR}/Dockerfile.tensorrt_llm
287
288
elif [[ $FRAMEWORK == "NONE" ]]; then
    DOCKERFILE=${SOURCE_DIR}/Dockerfile.none
289
290
fi

291
if [[ $FRAMEWORK == "VLLM" ]]; then
292
    NIXL_DIR="/tmp/nixl/nixl_src"
293
294

    # Clone original NIXL to temp directory
295
296
    if [ -d "$NIXL_DIR" ]; then
        echo "Warning: $NIXL_DIR already exists, skipping clone"
297
    else
298
299
300
301
302
303
304
305
        if [ ! -z ${GITHUB_TOKEN} ]; then
            git clone https://oauth2:${GITHUB_TOKEN}@github.com/${NIXL_REPO} "$NIXL_DIR"
        else
            # Try HTTPS first with credential prompting disabled, fall back to SSH if it fails
            if ! GIT_TERMINAL_PROMPT=0 git clone https://github.com/${NIXL_REPO} "$NIXL_DIR"; then
                echo "HTTPS clone failed, falling back to SSH..."
                git clone git@github.com:${NIXL_REPO} "$NIXL_DIR"
            fi
306
307
308
        fi
    fi

309
310
311
312
313
314
    cd "$NIXL_DIR"
    if ! git checkout ${NIXL_COMMIT}; then
        echo "ERROR: Failed to checkout NIXL commit ${NIXL_COMMIT}. The cached directory may be out of date."
        echo "Please delete $NIXL_DIR and re-run the build script."
        exit 1
    fi
315

316
    BUILD_CONTEXT_ARG+=" --build-context nixl=$NIXL_DIR"
317

318
319
    # Add NIXL_COMMIT as a build argument to enable caching
    BUILD_ARGS+=" --build-arg NIXL_COMMIT=${NIXL_COMMIT} "
320
321
fi

322
323
# BUILD DEV IMAGE

324
BUILD_ARGS+=" --build-arg BASE_IMAGE=$BASE_IMAGE --build-arg BASE_IMAGE_TAG=$BASE_IMAGE_TAG --build-arg FRAMEWORK=$FRAMEWORK --build-arg ${FRAMEWORK}_FRAMEWORK=1 --build-arg VERSION=$VERSION --build-arg PYTHON_PACKAGE_VERSION=$PYTHON_PACKAGE_VERSION"
325
326
327
328
329
330
331
332
333

if [ ! -z ${GITHUB_TOKEN} ]; then
    BUILD_ARGS+=" --build-arg GITHUB_TOKEN=${GITHUB_TOKEN} "
fi

if [ ! -z ${GITLAB_TOKEN} ]; then
    BUILD_ARGS+=" --build-arg GITLAB_TOKEN=${GITLAB_TOKEN} "
fi

334
335
336
337
if [[ $FRAMEWORK == "TENSORRTLLM" ]]; then
    if [ ! -z ${TENSORRTLLM_PIP_WHEEL_PATH} ]; then
        BUILD_ARGS+=" --build-arg TENSORRTLLM_PIP_WHEEL_PATH=${TENSORRTLLM_PIP_WHEEL_PATH} "
    fi
338
339
340
341
342
343
fi

if [ ! -z ${HF_TOKEN} ]; then
    BUILD_ARGS+=" --build-arg HF_TOKEN=${HF_TOKEN} "
fi

344
LATEST_TAG="--tag dynamo:latest-${FRAMEWORK,,}"
345
346
347
if [ ! -z ${TARGET} ]; then
    LATEST_TAG="${LATEST_TAG}-${TARGET}"
fi
348

349
350
351
352
353
354
show_image_options

if [ -z "$RUN_PREFIX" ]; then
    set -x
fi

355
356
357
358
359
360
361
362
363
364
365
366
367
# Check if the TensorRT-LLM base image exists
if [[ $FRAMEWORK == "TENSORRTLLM" ]]; then
    if docker inspect --type=image "$BASE_IMAGE:$BASE_IMAGE_TAG" > /dev/null 2>&1; then
        echo "Image '$BASE_IMAGE:$BASE_IMAGE_TAG' is found."
    else
        echo "Image '$BASE_IMAGE:$BASE_IMAGE_TAG' is not found." >&2
        echo "Please build the TensorRT-LLM base image first. Run ./build_trtllm_base_image.sh" >&2
        echo "or use --base-image and --base-image-tag to an existing TensorRT-LLM base image." >&2
        echo "See https://nvidia.github.io/TensorRT-LLM/installation/build-from-source-linux.html for more information." >&2
        exit 1
    fi
fi

368
$RUN_PREFIX docker build -f $DOCKERFILE $TARGET_STR $PLATFORM $BUILD_ARGS $CACHE_FROM $CACHE_TO $TAG $LATEST_TAG $BUILD_CONTEXT_ARG $BUILD_CONTEXT $NO_CACHE
369
370
371
372
373
374
375

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

if [ -z "$RUN_PREFIX" ]; then
    set -x
fi

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