-
Notifications
You must be signed in to change notification settings - Fork 30
/
Dockerfile
332 lines (239 loc) · 12 KB
/
Dockerfile
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
## Global Args #################################################################
ARG BASE_UBI_IMAGE_TAG=9.4-1214
ARG PROTOC_VERSION=25.3
ARG PYTORCH_INDEX="https://download.pytorch.org/whl"
# ARG PYTORCH_INDEX="https://download.pytorch.org/whl/nightly"
ARG AUTO_GPTQ_VERSION=0.7.1
# match PyTorch version that was used to compile flash-attention v2 pre-built wheels
# e.g. flash-attn v2.5.2 => torch ['1.12.1', '1.13.1', '2.0.1', '2.1.2', '2.2.0', '2.3.0.dev20240126']
# https://github.com/Dao-AILab/flash-attention/blob/v2.5.2/.github/workflows/publish.yml#L47
# use nightly build index for torch .dev pre-release versions
ARG PYTORCH_VERSION=2.2.1
ARG PYTHON_VERSION=3.11
## Base Layer ##################################################################
FROM registry.access.redhat.com/ubi9/ubi:${BASE_UBI_IMAGE_TAG} as base
WORKDIR /app
ARG PYTHON_VERSION
RUN dnf remove -y --disableplugin=subscription-manager \
subscription-manager \
# we install newer version of requests via pip
python${PYTHON_VERSION}-requests \
&& dnf install -y make \
# to help with debugging
procps \
&& dnf clean all
ENV LANG=C.UTF-8 \
LC_ALL=C.UTF-8
## CUDA Base ###################################################################
FROM base as cuda-base
# Ref: https://docs.nvidia.com/cuda/archive/12.1.0/cuda-toolkit-release-notes/
ENV CUDA_VERSION=12.1.0 \
NV_CUDA_LIB_VERSION=12.1.0-1 \
NVIDIA_VISIBLE_DEVICES=all \
NVIDIA_DRIVER_CAPABILITIES=compute,utility \
NV_CUDA_CUDART_VERSION=12.1.55-1 \
NV_CUDA_COMPAT_VERSION=530.30.02-1
RUN dnf config-manager \
--add-repo https://developer.download.nvidia.com/compute/cuda/repos/rhel9/x86_64/cuda-rhel9.repo \
&& dnf install -y \
cuda-cudart-12-1-${NV_CUDA_CUDART_VERSION} \
cuda-compat-12-1-${NV_CUDA_COMPAT_VERSION} \
&& echo "/usr/local/nvidia/lib" >> /etc/ld.so.conf.d/nvidia.conf \
&& echo "/usr/local/nvidia/lib64" >> /etc/ld.so.conf.d/nvidia.conf \
&& dnf clean all
ENV CUDA_HOME="/usr/local/cuda" \
PATH="/usr/local/nvidia/bin:${CUDA_HOME}/bin:${PATH}" \
LD_LIBRARY_PATH="/usr/local/nvidia/lib:/usr/local/nvidia/lib64:$CUDA_HOME/lib64:$CUDA_HOME/extras/CUPTI/lib64:${LD_LIBRARY_PATH}"
## CUDA Development ############################################################
FROM cuda-base as cuda-devel
# Ref: https://developer.nvidia.com/nccl/nccl-legacy-downloads
ENV NV_CUDA_CUDART_DEV_VERSION=12.1.55-1 \
NV_NVML_DEV_VERSION=12.1.55-1 \
NV_LIBCUBLAS_DEV_VERSION=12.1.0.26-1 \
NV_LIBNPP_DEV_VERSION=12.0.2.50-1 \
NV_LIBNCCL_DEV_PACKAGE_VERSION=2.18.3-1+cuda12.1
RUN dnf config-manager \
--add-repo https://developer.download.nvidia.com/compute/cuda/repos/rhel9/x86_64/cuda-rhel9.repo \
&& dnf install -y \
cuda-command-line-tools-12-1-${NV_CUDA_LIB_VERSION} \
cuda-libraries-devel-12-1-${NV_CUDA_LIB_VERSION} \
cuda-minimal-build-12-1-${NV_CUDA_LIB_VERSION} \
cuda-cudart-devel-12-1-${NV_CUDA_CUDART_DEV_VERSION} \
cuda-nvml-devel-12-1-${NV_NVML_DEV_VERSION} \
libcublas-devel-12-1-${NV_LIBCUBLAS_DEV_VERSION} \
libnpp-devel-12-1-${NV_LIBNPP_DEV_VERSION} \
libnccl-devel-${NV_LIBNCCL_DEV_PACKAGE_VERSION} \
&& dnf clean all
ENV LIBRARY_PATH="$CUDA_HOME/lib64/stubs"
## Rust builder ################################################################
# Using bookworm for compilation so the rust binaries get linked against libssl.so.3
FROM rust:1.78-bookworm as rust-builder
ARG PROTOC_VERSION
ENV CARGO_REGISTRIES_CRATES_IO_PROTOCOL=sparse
# Install protoc, no longer included in prost crate
RUN cd /tmp && \
curl -L -O https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-linux-x86_64.zip && \
unzip protoc-*.zip -d /usr/local && rm protoc-*.zip
WORKDIR /usr/src
COPY rust-toolchain.toml rust-toolchain.toml
RUN rustup component add rustfmt
## Internal router builder #####################################################
FROM rust-builder as router-builder
COPY proto proto
COPY router router
WORKDIR /usr/src/router
#RUN --mount=type=cache,target=/root/.cargo --mount=type=cache,target=/usr/src/router/target cargo install --path .
RUN cargo install --path .
## Launcher builder ############################################################
FROM rust-builder as launcher-builder
ARG GIT_COMMIT_HASH
COPY launcher launcher
WORKDIR /usr/src/launcher
#RUN --mount=type=cache,target=/root/.cargo --mount=type=cache,target=/usr/src/launcher/target cargo install --path .
RUN env GIT_COMMIT_HASH=${GIT_COMMIT_HASH} cargo install --path .
## Tests base ##################################################################
FROM base as test-base
ARG PYTHON_VERSION
RUN dnf install -y make unzip python${PYTHON_VERSION} python${PYTHON_VERSION}-pip gcc openssl-devel gcc-c++ git && \
dnf clean all && \
ln -fs /usr/bin/python${PYTHON_VERSION} /usr/bin/python3 && \
ln -s /usr/bin/python${PYTHON_VERSION} /usr/local/bin/python && ln -s /usr/bin/pip${PYTHON_VERSION} /usr/local/bin/pip
RUN pip install --upgrade pip --no-cache-dir && pip install pytest --no-cache-dir && pip install pytest-asyncio --no-cache-dir
# CPU only
ENV CUDA_VISIBLE_DEVICES=""
## Tests #######################################################################
FROM test-base as cpu-tests
ARG PYTORCH_INDEX
ARG PYTORCH_VERSION
ARG PYTHON_VERSION
ARG SITE_PACKAGES=/usr/local/lib/python${PYTHON_VERSION}/site-packages
WORKDIR /usr/src
# Install specific version of torch
RUN pip install torch=="$PYTORCH_VERSION+cpu" --index-url "${PYTORCH_INDEX}/cpu" --no-cache-dir
COPY server/Makefile server/Makefile
# Install server
COPY proto proto
COPY server server
RUN cd server && \
make gen-server && \
pip install ".[accelerate]" --no-cache-dir
# Patch codegen model changes into transformers
RUN cp server/transformers_patch/modeling_codegen.py ${SITE_PACKAGES}/transformers/models/codegen/modeling_codegen.py
# Install router
COPY --from=router-builder /usr/local/cargo/bin/text-generation-router /usr/local/bin/text-generation-router
# Install launcher
COPY --from=launcher-builder /usr/local/cargo/bin/text-generation-launcher /usr/local/bin/text-generation-launcher
# Install integration tests
COPY integration_tests integration_tests
RUN cd integration_tests && make install
## Python builder #############################################################
FROM cuda-devel as python-builder
ARG PYTORCH_INDEX
ARG PYTORCH_VERSION
ARG PYTHON_VERSION
ARG MINIFORGE_VERSION=23.11.0-0
# consistent arch support anywhere we compile CUDA code
ENV TORCH_CUDA_ARCH_LIST="8.0;8.6+PTX;8.9"
RUN dnf install -y unzip git ninja-build && dnf clean all
RUN curl -fsSL -v -o ~/miniforge3.sh -O "https://github.com/conda-forge/miniforge/releases/download/${MINIFORGE_VERSION}/Miniforge3-$(uname)-$(uname -m).sh" && \
chmod +x ~/miniforge3.sh && \
bash ~/miniforge3.sh -b -p /opt/conda && \
source "/opt/conda/etc/profile.d/conda.sh" && \
conda create -y -p /opt/tgis python=${PYTHON_VERSION} && \
conda activate /opt/tgis && \
rm ~/miniforge3.sh
ENV PATH=/opt/tgis/bin/:$PATH
# Install specific version of torch
RUN pip install ninja==1.11.1.1 --no-cache-dir
RUN pip install packaging --no-cache-dir
RUN pip install torch==$PYTORCH_VERSION+cu121 --index-url "${PYTORCH_INDEX}/cu121" --no-cache-dir
## Build flash attention v2 ####################################################
FROM python-builder as flash-att-v2-builder
ARG FLASH_ATT_VERSION=v2.5.6
WORKDIR /usr/src/flash-attention-v2
# Download the wheel or build it if a pre-compiled release doesn't exist
# MAX_JOBS: For CI, limit number of parallel compilation threads otherwise the github runner goes OOM
RUN MAX_JOBS=2 pip --verbose wheel --no-deps flash-attn==${FLASH_ATT_VERSION} \
"git+https://github.com/Dao-AILab/flash-attention.git@${FLASH_ATT_VERSION}#subdirectory=csrc/layer_norm" \
"git+https://github.com/Dao-AILab/flash-attention.git@${FLASH_ATT_VERSION}#subdirectory=csrc/rotary" \
--no-build-isolation --no-cache-dir
## Install auto-gptq ###########################################################
## Uncomment if a custom autogptq build is required
#FROM python-builder as auto-gptq-installer
#ARG AUTO_GPTQ_REF=896d8204bc89a7cfbda42bf3314e13cf4ce20b02
#
#WORKDIR /usr/src/auto-gptq-wheel
#
## numpy is required to run auto-gptq's setup.py
#RUN pip install numpy
#RUN DISABLE_QIGEN=1 pip wheel git+https://github.com/AutoGPTQ/AutoGPTQ@${AUTO_GPTQ_REF} --no-cache-dir --no-deps --verbose
## Build libraries #############################################################
FROM python-builder as build
# Build custom kernels
COPY server/custom_kernels/ /usr/src/.
RUN cd /usr/src && python setup.py build_ext && python setup.py install
## Flash attention v2 cached build image #######################################
FROM base as flash-att-v2-cache
# Copy just the wheels we built for flash-attention
COPY --from=flash-att-v2-builder /usr/src/flash-attention-v2 /usr/src/flash-attention-v2
## Auto gptq cached build image ################################################
## Uncomment if a custom autogptq build is required
#FROM base as auto-gptq-cache
#
## Copy just the wheel we built for auto-gptq
#COPY --from=auto-gptq-installer /usr/src/auto-gptq-wheel /usr/src/auto-gptq-wheel
## Full set of python installations for server release #########################
FROM python-builder as python-installations
ARG PYTHON_VERSION
ARG AUTO_GPTQ_VERSION
ARG SITE_PACKAGES=/opt/tgis/lib/python${PYTHON_VERSION}/site-packages
COPY --from=build /opt/tgis /opt/tgis
# `pip` is installed in the venv here
ENV PATH=/opt/tgis/bin:$PATH
# Install flash attention v2 from the cache build
RUN --mount=type=bind,from=flash-att-v2-cache,src=/usr/src/flash-attention-v2,target=/usr/src/flash-attention-v2 \
pip install /usr/src/flash-attention-v2/*.whl --no-cache-dir
# Copy over the auto-gptq wheel and install it
#RUN --mount=type=bind,from=auto-gptq-cache,src=/usr/src/auto-gptq-wheel,target=/usr/src/auto-gptq-wheel \
# pip install /usr/src/auto-gptq-wheel/*.whl --no-cache-dir
# We only need to install a custom-built auto-gptq version if we need a pre-release
# or are using a PyTorch nightly version
RUN pip install auto-gptq=="${AUTO_GPTQ_VERSION}" --no-cache-dir
# Install server
# git is required to pull the fms-extras dependency
RUN dnf install -y git && dnf clean all
COPY proto proto
COPY server server
# Extra url is required to install cuda-12 version of onnxruntime-gpu
# Ref: https://onnxruntime.ai/docs/install/#install-onnx-runtime-gpu-cuda-12x
RUN cd server && make gen-server && pip install ".[accelerate, ibm-fms, onnx-gpu, quantize]" --no-cache-dir --extra-index-url=https://aiinfra.pkgs.visualstudio.com/PublicPackages/_packaging/onnxruntime-cuda-12/pypi/simple/
# Patch codegen model changes into transformers 4.35
RUN cp server/transformers_patch/modeling_codegen.py ${SITE_PACKAGES}/transformers/models/codegen/modeling_codegen.py
## Final Inference Server image ################################################
FROM base as server-release
ARG PYTHON_VERSION
ARG SITE_PACKAGES=/opt/tgis/lib/python${PYTHON_VERSION}/site-packages
# Install C++ compiler (required at runtime when PT2_COMPILE is enabled)
RUN dnf install -y gcc-c++ && dnf clean all \
&& useradd -u 2000 tgis -m -g 0
# Copy in the full python environment
COPY --from=python-installations /opt/tgis /opt/tgis
ENV PATH=/opt/tgis/bin:$PATH
# Print a list of all installed packages and versions
RUN pip list -v --disable-pip-version-check --no-python-version-warning
# Install router
COPY --from=router-builder /usr/local/cargo/bin/text-generation-router /usr/local/bin/text-generation-router
# Install launcher
COPY --from=launcher-builder /usr/local/cargo/bin/text-generation-launcher /usr/local/bin/text-generation-launcher
ENV PORT=3000 \
GRPC_PORT=8033 \
HOME=/home/tgis
# Runs as arbitrary user in OpenShift
RUN chmod -R g+rwx ${HOME}
# Temporary for dev
RUN chmod -R g+w ${SITE_PACKAGES}/text_generation_server /usr/src /usr/local/bin
# Run as non-root user by default
USER tgis
EXPOSE ${PORT}
EXPOSE ${GRPC_PORT}
CMD text-generation-launcher