Skip to content

Commit bea8814

Browse files
committed
feat: Add option to specify Python distribution
- Add support for pyenv in Docker file, allowing specification of a custom Python version - Update WORKSPACE files to automatically synchronize regardless of Python version choice, using symbolic links - Update dependencies accordingly - Add documentation for changing the base image of the Docker container
1 parent dfec229 commit bea8814

File tree

5 files changed

+50
-28
lines changed

5 files changed

+50
-28
lines changed

docker/Dockerfile

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,30 @@ RUN test -n "$TENSORRT_VERSION" || (echo "No tensorrt version specified, please
77
ARG CUDNN_VERSION
88
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)
99

10+
ARG PYTHON_VERSION=3.10
11+
ENV PYTHON_VERSION=${PYTHON_VERSION}
12+
1013
ARG USE_CXX11_ABI
1114
ENV USE_CXX11=${USE_CXX11_ABI}
1215
ENV DEBIAN_FRONTEND=noninteractive
1316

1417
# Install basic dependencies
1518
RUN apt-get update
16-
RUN apt install -y build-essential manpages-dev wget zlib1g software-properties-common git
17-
RUN add-apt-repository ppa:deadsnakes/ppa
18-
RUN apt install -y python3.8 python3.8-distutils python3.8-dev
19-
RUN wget https://bootstrap.pypa.io/get-pip.py
20-
RUN ln -s /usr/bin/python3.8 /usr/bin/python
21-
RUN python get-pip.py
22-
RUN pip3 install wheel
23-
24-
# Install CUDNN + TensorRT
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
2534
RUN wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-ubuntu2204.pin
2635
RUN mv cuda-ubuntu2204.pin /etc/apt/preferences.d/cuda-repository-pin-600
2736
RUN apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/7fa2af80.pub
@@ -37,9 +46,9 @@ RUN apt-get update
3746

3847
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*
3948

40-
# Setup Bazel
41-
RUN wget -q https://github.com/bazelbuild/bazelisk/releases/download/v1.16.0/bazelisk-linux-amd64 -O /usr/bin/bazel \
42-
&& 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
4352

4453
# Build Torch-TensorRT in an auxillary container
4554
FROM base as torch-tensorrt-builder-base
@@ -50,16 +59,22 @@ ARG TARGETARCH="amd64"
5059
RUN apt-get install -y python3-setuptools
5160
RUN apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/3bf863cc.pub
5261

53-
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
5466

5567
FROM torch-tensorrt-builder-base as torch-tensorrt-builder
5668

5769
COPY . /workspace/torch_tensorrt/src
5870
WORKDIR /workspace/torch_tensorrt/src
5971
RUN cp ./docker/WORKSPACE.docker WORKSPACE
6072

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

6479
# Copy and install Torch-TRT into the main container
6580
FROM base as torch-tensorrt
@@ -70,10 +85,11 @@ COPY --from=torch-tensorrt-builder /workspace/torch_tensorrt/src/py/dist/ .
7085
RUN cp /opt/torch_tensorrt/docker/WORKSPACE.docker /opt/torch_tensorrt/WORKSPACE
7186
RUN pip install -r /opt/torch_tensorrt/py/requirements.txt
7287
RUN pip install tensorrt==${TENSORRT_VERSION}.*
73-
RUN pip3 install *.whl && rm -fr /workspace/torch_tensorrt/py/dist/* *.whl
88+
RUN pip install *.whl && rm -fr /workspace/torch_tensorrt/py/dist/* *.whl
7489

7590
WORKDIR /opt/torch_tensorrt
76-
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}
77-
ENV PATH /usr/local/lib/python3.8/dist-packages/torch_tensorrt/bin:${PATH}
91+
92+
ENV LD_LIBRARY_PATH /usr/local/lib/python/site-packages/torch/lib:/usr/local/lib/python/site-packages/torch_tensorrt/lib:/usr/lib/x86_64-linux-gnu:${LD_LIBRARY_PATH}
93+
ENV PATH /usr/local/lib/python/site-packages/torch_tensorrt/bin:${PATH}
7894

7995
CMD /bin/bash

docker/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

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

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>. 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`
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+
* 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
68

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

docker/WORKSPACE.docker

Lines changed: 2 additions & 2 deletions
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 = "/usr/local/lib/python/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 = "/usr/local/lib/python/site-packages/torch/",
6060
build_file = "third_party/libtorch/BUILD"
6161
)
6262

docker/WORKSPACE.ngc

Lines changed: 3 additions & 3 deletions
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 = "/usr/local/lib/python/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 = "/usr/local/lib/python/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 = "/usr/local/lib/python/site-packages/torch/",
6565
build_file = "third_party/libtorch/BUILD"
6666
)
6767

docker/dist-build.sh

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,20 @@
33
TOP_DIR=$(cd $(dirname $0); pwd)/..
44

55
if [[ -z "${USE_CXX11}" ]]; then
6-
BUILD_CMD="python setup.py bdist_wheel"
6+
BUILD_CMD="python3 setup.py bdist_wheel"
77
else
8-
BUILD_CMD="python setup.py bdist_wheel --use-cxx11-abi"
8+
BUILD_CMD="python3 setup.py bdist_wheel --use-cxx11-abi"
99
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 /usr/local/lib/ for package sourcing
16+
ln -s "`pyenv which python | xargs dirname | xargs dirname`/lib/python$PYTHON_VERSION/site-packages" "/usr/local/lib/python"
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)