Skip to content

Commit d708fa5

Browse files
gs-olivebowang007
authored andcommitted
feat: Upgrade Docker build to use custom TRT + CUDNN (#1805)
1 parent e0b305a commit d708fa5

File tree

5 files changed

+70
-37
lines changed

5 files changed

+70
-37
lines changed

docker/Dockerfile

+50-27
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,54 @@
11
# Base image starts with CUDA
2-
ARG BASE_IMG=nvidia/cuda:11.7.1-devel-ubuntu20.04
2+
ARG BASE_IMG=nvidia/cuda:11.7.1-devel-ubuntu22.04
33
FROM ${BASE_IMG} as base
44

5+
ARG TENSORRT_VERSION
6+
RUN test -n "$TENSORRT_VERSION" || (echo "No tensorrt version specified, please use --build-arg TENSORRT_VERSION=x.y.z to specify a version." && exit 1)
7+
ARG CUDNN_VERSION
8+
RUN test -n "$CUDNN_VERSION" || (echo "No cudnn version specified, please use --build-arg CUDNN_VERSION=x.y.z to specify a version." && exit 1)
9+
10+
ARG PYTHON_VERSION=3.10
11+
ENV PYTHON_VERSION=${PYTHON_VERSION}
12+
513
ARG USE_CXX11_ABI
614
ENV USE_CXX11=${USE_CXX11_ABI}
15+
ENV DEBIAN_FRONTEND=noninteractive
716

817
# Install basic dependencies
918
RUN apt-get update
10-
RUN DEBIAN_FRONTEND=noninteractive apt install -y build-essential manpages-dev wget zlib1g software-properties-common git
11-
RUN add-apt-repository ppa:deadsnakes/ppa
12-
RUN apt install -y python3.8 python3.8-distutils python3.8-dev
13-
RUN wget https://bootstrap.pypa.io/get-pip.py
14-
RUN ln -s /usr/bin/python3.8 /usr/bin/python
15-
RUN python get-pip.py
16-
RUN pip3 install wheel
17-
18-
# Install CUDNN + TensorRT
19-
RUN wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-ubuntu2004.pin
20-
RUN mv cuda-ubuntu2004.pin /etc/apt/preferences.d/cuda-repository-pin-600
21-
RUN apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/7fa2af80.pub
19+
RUN apt install -y build-essential manpages-dev wget zlib1g software-properties-common git libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget ca-certificates curl llvm libncurses5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev mecab-ipadic-utf8
20+
21+
# Install PyEnv and desired Python version
22+
ENV HOME="/root"
23+
ENV PYENV_DIR="$HOME/.pyenv"
24+
ENV PATH="$PYENV_DIR/shims:$PYENV_DIR/bin:$PATH"
25+
RUN wget -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer &&\
26+
chmod 755 pyenv-installer &&\
27+
bash pyenv-installer &&\
28+
eval "$(pyenv init -)"
29+
30+
RUN pyenv install -v ${PYTHON_VERSION}
31+
RUN pyenv global ${PYTHON_VERSION}
32+
33+
# Install CUDNN + TensorRT + dependencies
34+
RUN wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-ubuntu2204.pin
35+
RUN mv cuda-ubuntu2204.pin /etc/apt/preferences.d/cuda-repository-pin-600
36+
RUN apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/7fa2af80.pub
2237
RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 536F8F1DE80F6A35
2338
RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys A4B469963BF863CC
24-
RUN add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/ /"
39+
RUN add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/ /"
2540
RUN apt-get update
26-
RUN apt-get install -y libcudnn8=8.5.0* libcudnn8-dev=8.5.0*
41+
RUN apt-get install -y libcudnn8=${CUDNN_VERSION}* libcudnn8-dev=${CUDNN_VERSION}*
2742

28-
RUN apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/3bf863cc.pub
29-
RUN add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/ /"
43+
RUN apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/3bf863cc.pub
44+
RUN add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/ /"
3045
RUN apt-get update
3146

32-
RUN apt-get install -y libnvinfer8=8.5.1* libnvinfer-plugin8=8.5.1* libnvinfer-dev=8.5.1* libnvinfer-plugin-dev=8.5.1* libnvonnxparsers8=8.5.1-1* libnvonnxparsers-dev=8.5.1-1* libnvparsers8=8.5.1-1* libnvparsers-dev=8.5.1-1*
47+
RUN apt-get install -y libnvinfer8=${TENSORRT_VERSION}* libnvinfer-plugin8=${TENSORRT_VERSION}* libnvinfer-dev=${TENSORRT_VERSION}* libnvinfer-plugin-dev=${TENSORRT_VERSION}* libnvonnxparsers8=${TENSORRT_VERSION}-1* libnvonnxparsers-dev=${TENSORRT_VERSION}-1* libnvparsers8=${TENSORRT_VERSION}-1* libnvparsers-dev=${TENSORRT_VERSION}-1*
3348

34-
# Setup Bazel
35-
RUN wget -q https://github.com/bazelbuild/bazelisk/releases/download/v1.16.0/bazelisk-linux-amd64 -O /usr/bin/bazel \
36-
&& chmod a+x /usr/bin/bazel
49+
# Setup Bazel via Bazelisk
50+
RUN wget -q https://github.com/bazelbuild/bazelisk/releases/download/v1.16.0/bazelisk-linux-amd64 -O /usr/bin/bazel &&\
51+
chmod a+x /usr/bin/bazel
3752

3853
# Build Torch-TensorRT in an auxillary container
3954
FROM base as torch-tensorrt-builder-base
@@ -42,18 +57,24 @@ ARG ARCH="x86_64"
4257
ARG TARGETARCH="amd64"
4358

4459
RUN apt-get install -y python3-setuptools
45-
RUN apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/3bf863cc.pub
60+
RUN apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/3bf863cc.pub
4661

47-
RUN apt-get update && apt-get install -y --no-install-recommends locales ninja-build && rm -rf /var/lib/apt/lists/* && locale-gen en_US.UTF-8
62+
RUN apt-get update &&\
63+
apt-get install -y --no-install-recommends locales ninja-build &&\
64+
rm -rf /var/lib/apt/lists/* &&\
65+
locale-gen en_US.UTF-8
4866

4967
FROM torch-tensorrt-builder-base as torch-tensorrt-builder
5068

5169
COPY . /workspace/torch_tensorrt/src
5270
WORKDIR /workspace/torch_tensorrt/src
5371
RUN cp ./docker/WORKSPACE.docker WORKSPACE
5472

73+
# Symlink the path pyenv is using for python with the /opt directory for package sourcing
74+
RUN ln -s "`pyenv which python | xargs dirname | xargs dirname`/lib/python$PYTHON_VERSION/site-packages" "/opt/python3"
75+
5576
# This script builds both libtorchtrt bin/lib/include tarball and the Python wheel, in dist/
56-
RUN ./docker/dist-build.sh
77+
RUN bash ./docker/dist-build.sh
5778

5879
# Copy and install Torch-TRT into the main container
5980
FROM base as torch-tensorrt
@@ -63,10 +84,12 @@ COPY --from=torch-tensorrt-builder /workspace/torch_tensorrt/src/py/dist/ .
6384

6485
RUN cp /opt/torch_tensorrt/docker/WORKSPACE.docker /opt/torch_tensorrt/WORKSPACE
6586
RUN pip install -r /opt/torch_tensorrt/py/requirements.txt
66-
RUN pip3 install *.whl && rm -fr /workspace/torch_tensorrt/py/dist/* *.whl
87+
RUN pip install tensorrt==${TENSORRT_VERSION}.*
88+
RUN pip install *.whl && rm -fr /workspace/torch_tensorrt/py/dist/* *.whl
6789

6890
WORKDIR /opt/torch_tensorrt
69-
ENV LD_LIBRARY_PATH /usr/local/lib/python3.8/dist-packages/torch/lib:/usr/local/lib/python3.8/dist-packages/torch_tensorrt/lib:/usr/lib/x86_64-linux-gnu:${LD_LIBRARY_PATH}
70-
ENV PATH /usr/local/lib/python3.8/dist-packages/torch_tensorrt/bin:${PATH}
91+
92+
ENV LD_LIBRARY_PATH /opt/python3/site-packages/torch/lib:/opt/python3/site-packages/torch_tensorrt/lib:/usr/lib/x86_64-linux-gnu:${LD_LIBRARY_PATH}
93+
ENV PATH /opt/python3/site-packages/torch_tensorrt/bin:${PATH}
7194

7295
CMD /bin/bash

docker/README.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
* Use `Dockerfile` to build a container which provides the exact development environment that our master branch is usually tested against.
44

5-
* `Dockerfile` currently uses the exact library versions (Torch, CUDA, CUDNN, TensorRT) listed in <a href="https://github.com/pytorch/TensorRT#dependencies">dependencies</a> to build Torch-TensorRT.
5+
* The `Dockerfile` currently uses <a href="https://github.com/bazelbuild/bazelisk">Bazelisk</a> to select the Bazel version, and uses the exact library versions of Torch and CUDA listed in <a href="https://github.com/pytorch/TensorRT#dependencies">dependencies</a>.
6+
* The desired versions of CUDNN and TensorRT must be specified as build-args, with major, minor, and patch versions as in: `--build-arg TENSORRT_VERSION=a.b.c --build-arg CUDNN_VERSION=x.y.z`
7+
* [**Optional**] The desired base image be changed by explicitly setting a base image, as in `--build-arg BASE_IMG=nvidia/cuda:11.7.1-devel-ubuntu22.04`, though this is optional
8+
* [**Optional**] Additionally, the desired Python version can be changed by explicitly setting a version, as in `--build-arg PYTHON_VERSION=3.10`, though this is optional as well.
69

710
* This `Dockerfile` installs `pre-cxx11-abi` versions of Pytorch and builds Torch-TRT using `pre-cxx11-abi` libtorch as well.
811

@@ -14,11 +17,14 @@ Note: By default the container uses the `pre-cxx11-abi` version of Torch + Torch
1417

1518
### Instructions
1619

20+
- The example below uses CUDNN 8.5.0 and TensorRT 8.5.1
21+
- See <a href="https://github.com/pytorch/TensorRT#dependencies">dependencies</a> for a list of current default dependencies.
22+
1723
> From root of Torch-TensorRT repo
1824
1925
Build:
2026
```
21-
DOCKER_BUILDKIT=1 docker build -f docker/Dockerfile -t torch_tensorrt:latest .
27+
DOCKER_BUILDKIT=1 docker build --build-arg TENSORRT_VERSION=8.5.1 --build-arg CUDNN_VERSION=8.5.0 -f docker/Dockerfile -t torch_tensorrt:latest .
2228
```
2329

2430
Run:

docker/WORKSPACE.docker

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ new_local_repository(
5050

5151
new_local_repository(
5252
name = "libtorch",
53-
path = "/usr/local/lib/python3.8/dist-packages/torch/",
53+
path = "/opt/python3/site-packages/torch/",
5454
build_file = "third_party/libtorch/BUILD"
5555
)
5656

5757
new_local_repository(
5858
name = "libtorch_pre_cxx11_abi",
59-
path = "/usr/local/lib/python3.8/dist-packages/torch/",
59+
path = "/opt/python3/site-packages/torch/",
6060
build_file = "third_party/libtorch/BUILD"
6161
)
6262

docker/WORKSPACE.ngc

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ git_repository(
3333
# This is currently used in pytorch NGC container CI testing.
3434
local_repository(
3535
name = "torch_tensorrt",
36-
path = "/usr/local/lib/python3.8/dist-packages/torch_tensorrt"
36+
path = "/opt/python3/site-packages/torch_tensorrt/"
3737
)
3838

3939
# CUDA should be installed on the system locally
@@ -55,13 +55,13 @@ new_local_repository(
5555

5656
new_local_repository(
5757
name = "libtorch",
58-
path = "/usr/local/lib/python3.8/dist-packages/torch",
58+
path = "/opt/python3/site-packages/torch/",
5959
build_file = "third_party/libtorch/BUILD"
6060
)
6161

6262
new_local_repository(
6363
name = "libtorch_pre_cxx11_abi",
64-
path = "/usr/local/lib/python3.8/dist-packages/torch",
64+
path = "/opt/python3/site-packages/torch/",
6565
build_file = "third_party/libtorch/BUILD"
6666
)
6767

docker/dist-build.sh

+7-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ fi
1010

1111
cd ${TOP_DIR} \
1212
&& mkdir -p dist && cd py \
13-
&& pip install -r requirements.txt \
14-
&& MAX_JOBS=1 LANG=en_US.UTF-8 LANGUAGE=en_US:en LC_ALL=en_US.UTF-8 \
15-
${BUILD_CMD} $* || exit 1
13+
&& pip install -r requirements.txt
14+
15+
# Symlink the path pyenv is using for python with the /opt directory for package sourcing
16+
ln -s "`pyenv which python | xargs dirname | xargs dirname`/lib/python$PYTHON_VERSION/site-packages" "/opt/python3"
17+
18+
# Build Torch-TRT
19+
MAX_JOBS=1 LANG=en_US.UTF-8 LANGUAGE=en_US:en LC_ALL=en_US.UTF-8 ${BUILD_CMD} $* || exit 1
1620

1721
pip3 install ipywidgets --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host=files.pythonhosted.org
1822
jupyter nbextension enable --py widgetsnbextension

0 commit comments

Comments
 (0)