From 49ab2887b97536fc346bf51916fb8bb0f3af9ba2 Mon Sep 17 00:00:00 2001 From: Boris Fomitchev Date: Wed, 15 Sep 2021 21:50:25 -0700 Subject: [PATCH 1/6] Refactored Dockerfiles Signed-off-by: Boris Fomitchev --- docker/{Dockerfile.21.07 => Dockerfile} | 47 +++++++++++++------------ docker/Dockerfile.21.06 | 19 +++++----- docker/dist-build.sh | 12 +++++++ py/setup.py | 4 +-- 4 files changed, 49 insertions(+), 33 deletions(-) rename docker/{Dockerfile.21.07 => Dockerfile} (50%) create mode 100755 docker/dist-build.sh diff --git a/docker/Dockerfile.21.07 b/docker/Dockerfile similarity index 50% rename from docker/Dockerfile.21.07 rename to docker/Dockerfile index f8b84ce549..74a7e98be3 100644 --- a/docker/Dockerfile.21.07 +++ b/docker/Dockerfile @@ -1,42 +1,43 @@ -FROM nvcr.io/nvidia/pytorch:21.07-py3 as builder +ARG BASE=21.07 +FROM nvcr.io/nvidia/pytorch:${BASE}-py3 as base -RUN apt-get update && apt-get install -y curl gnupg && rm -rf /var/lib/apt/lists/* +FROM base as trtorch-builder-base -RUN curl -fsSL https://bazel.build/bazel-release.pub.gpg | gpg --dearmor > bazel.gpg -RUN mv bazel.gpg /etc/apt/trusted.gpg.d/ +RUN apt-get update && apt-get install -y curl gnupg +RUN curl -fsSL https://bazel.build/bazel-release.pub.gpg | gpg --dearmor > /etc/apt/trusted.gpg.d/bazel.gpg RUN echo "deb [arch=amd64] https://storage.googleapis.com/bazel-apt stable jdk1.8" | tee /etc/apt/sources.list.d/bazel.list -RUN apt-get update && apt-get install -y bazel-4.0.0 && rm -rf /var/lib/apt/lists/* +RUN apt-get update && apt-get install -y bazel-4.0.0 RUN ln -s /usr/bin/bazel-4.0.0 /usr/bin/bazel + # Workaround for bazel expecting both static and shared versions, we only use shared libraries inside container RUN cp /usr/lib/x86_64-linux-gnu/libnvinfer.so /usr/lib/x86_64-linux-gnu/libnvinfer_static.a -RUN pip install notebook +RUN apt-get update && apt-get install -y locales ninja-build && rm -rf /var/lib/apt/lists/* && locale-gen en_US.UTF-8 + +FROM trtorch-builder-base as trtorch-builder + +COPY . /workspace/trtorch/src +WORKDIR /workspace/trtorch/src +RUN cp ./docker/WORKSPACE.cu.docker WORKSPACE -FROM builder as trtorch +RUN ./docker/dist-build.sh -COPY . /opt/trtorch -RUN rm /opt/trtorch/WORKSPACE -COPY ./docker/WORKSPACE.cu.docker /opt/trtorch/WORKSPACE +FROM base as trtorch -WORKDIR /opt/trtorch -RUN bazel build //:libtrtorch --compilation_mode opt +# copy source repo +COPY . /workspace/trtorch +COPY --from=trtorch-builder /workspace/trtorch/src/dist/ . -WORKDIR /opt/trtorch/py +RUN conda init bash RUN pip install ipywidgets --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host=files.pythonhosted.org RUN jupyter nbextension enable --py widgetsnbextension -# Locale is not set by default -RUN apt-get update && apt-get install -y locales ninja-build && rm -rf /var/lib/apt/lists/* && locale-gen en_US.UTF-8 -ENV LANG en_US.UTF-8 -ENV LANGUAGE en_US:en -ENV LC_ALL en_US.UTF-8 -RUN python3 setup.py install --use-cxx11-abi - -RUN conda init bash +RUN mkdir -p /opt/trtorch && tar xvf libtrtorch.tar.gz --strip-components 2 -C /opt/trtorch --exclude=LICENSE && pip3 install *.whl && rm -fr /workspace/trtorch/dist/* -ENV LD_LIBRARY_PATH /opt/conda/lib/python3.8/site-packages/torch/lib:$LD_LIBRARY_PATH +ENV LD_LIBRARY_PATH /opt/conda/lib/python3.8/site-packages/torch/lib:/opt/trtorch/lib:${LD_LIBRARY_PATH} +ENV PATH /opt/trtorch/bin:${PATH} -WORKDIR /opt/trtorch/ +WORKDIR /workspace/trtorch/ CMD /bin/bash diff --git a/docker/Dockerfile.21.06 b/docker/Dockerfile.21.06 index a66bbb7832..9c717a1051 100644 --- a/docker/Dockerfile.21.06 +++ b/docker/Dockerfile.21.06 @@ -15,14 +15,13 @@ RUN pip install notebook FROM builder as trtorch -COPY . /opt/trtorch -RUN rm /opt/trtorch/WORKSPACE -COPY ./docker/WORKSPACE.cu.docker /opt/trtorch/WORKSPACE +COPY . /opt/trtorch/src +WORKDIR /opt/trtorch/src +RUN cp ./docker/WORKSPACE.cu.docker WORKSPACE -WORKDIR /opt/trtorch -RUN bazel build //:libtrtorch --compilation_mode opt +RUN bazel build //:libtrtorch //:bin --compilation_mode opt -WORKDIR /opt/trtorch/py +WORKDIR /opt/trtorch/src/py RUN pip install ipywidgets --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host=files.pythonhosted.org RUN jupyter nbextension enable --py widgetsnbextension @@ -32,11 +31,15 @@ RUN apt-get update && apt-get install -y locales ninja-build && rm -rf /var/lib/ ENV LANG en_US.UTF-8 ENV LANGUAGE en_US:en ENV LC_ALL en_US.UTF-8 -RUN python3 setup.py install --use-cxx11-abi +RUN MAX_JOBS=1 python3 setup.py install --use-cxx11-abi RUN conda init bash -ENV LD_LIBRARY_PATH /opt/conda/lib/python3.8/site-packages/torch/lib:$LD_LIBRARY_PATH +WORKDIR /opt/trtorch/src + +RUN cd ./bazel-bin && tar xvf libtrtorch.tar.gz -C /usr/local --strip-components 2 --exclude=LICENSE + +ENV LD_LIBRARY_PATH /opt/conda/lib/python3.8/site-packages/torch/lib:${LD_LIBRARY_PATH} WORKDIR /opt/trtorch/ CMD /bin/bash diff --git a/docker/dist-build.sh b/docker/dist-build.sh new file mode 100755 index 0000000000..4715c4fc7b --- /dev/null +++ b/docker/dist-build.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +mkdir -p dist + +bazel build //:libtrtorch //:bin --compilation_mode opt + +cd py && MAX_JOBS=1 LANG=en_US.UTF-8 LANGUAGE=en_US:en LC_ALL=en_US.UTF-8 python3 setup.py bdist_wheel --use-cxx11-abi + +cd .. + +mv bazel-bin/libtrtorch.tar.gz dist/ +mv py/dist/* dist/ diff --git a/py/setup.py b/py/setup.py index 7f67aace5f..f75dbd216f 100644 --- a/py/setup.py +++ b/py/setup.py @@ -203,7 +203,7 @@ def run(self): 'trtorch/csrc/tensorrt_classes.cpp', 'trtorch/csrc/register_tensorrt_classes.cpp', ], - library_dirs=[(dir_path + '/trtorch/lib/'), "/opt/conda/lib/python3.6/config-3.6m-x86_64-linux-gnu"], + library_dirs=[(dir_path + '/trtorch/lib/')], libraries=["trtorch"], include_dirs=[ dir_path + "trtorch/csrc", @@ -234,7 +234,7 @@ def run(self): long_description=long_description, ext_modules=ext_modules, install_requires=[ - 'torch>=1.9.0+cu111,<1.10.0', + 'torch>=1.9.0+cu111,<1.11.0', ], setup_requires=[], cmdclass={ From 91507a7feb3e267c07601945a17f80535fdfb342 Mon Sep 17 00:00:00 2001 From: Boris Fomitchev Date: Mon, 20 Sep 2021 04:13:37 -0700 Subject: [PATCH 2/6] Added dist-test.sh Signed-off-by: Boris Fomitchev --- docker/Dockerfile | 4 ++++ docker/dist-build.sh | 15 +++++++++++++-- docker/dist-test.sh | 5 +++++ py/requirements.txt | 2 +- tests/modules/requirements.txt | 2 +- 5 files changed, 24 insertions(+), 4 deletions(-) create mode 100755 docker/dist-test.sh diff --git a/docker/Dockerfile b/docker/Dockerfile index 74a7e98be3..7552298930 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -21,8 +21,12 @@ COPY . /workspace/trtorch/src WORKDIR /workspace/trtorch/src RUN cp ./docker/WORKSPACE.cu.docker WORKSPACE +# This script builds both libtrtorch bin/lib/include tarball and the Pythin wheel, in dist/ RUN ./docker/dist-build.sh +# This currently does not work with BUILDKIT=1 +# RUN ./docker/dist-test.sh + FROM base as trtorch # copy source repo diff --git a/docker/dist-build.sh b/docker/dist-build.sh index 4715c4fc7b..24ac387f1b 100755 --- a/docker/dist-build.sh +++ b/docker/dist-build.sh @@ -8,5 +8,16 @@ cd py && MAX_JOBS=1 LANG=en_US.UTF-8 LANGUAGE=en_US:en LC_ALL=en_US.UTF-8 python cd .. -mv bazel-bin/libtrtorch.tar.gz dist/ -mv py/dist/* dist/ +cp bazel-bin/libtrtorch.tar.gz dist/ +cp py/dist/* dist/ + +pip3 install ipywidgets --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host=files.pythonhosted.org +jupyter nbextension enable --py widgetsnbextension + +pip3 install timm + +# test install +mkdir -p /opt/trtorch && tar xvf dist/libtrtorch.tar.gz --strip-components 2 -C /opt/trtorch --exclude=LICENSE && pip3 install dist/*.whl + +cd tests/modules && python3 ./hub.py +cd ../.. diff --git a/docker/dist-test.sh b/docker/dist-test.sh new file mode 100755 index 0000000000..04e7ff857b --- /dev/null +++ b/docker/dist-test.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +# Build and run unit tests +bazel test //tests:tests //tests:python_api_tests --compilation_mode opt --jobs 4 + diff --git a/py/requirements.txt b/py/requirements.txt index aecf06b4a8..72b074f6ff 100644 --- a/py/requirements.txt +++ b/py/requirements.txt @@ -1,3 +1,3 @@ -f https://download.pytorch.org/whl/torch_stable.html -torch==1.9.0+cu111 +torch>=1.9.0+cu111 pybind11==2.6.2 diff --git a/tests/modules/requirements.txt b/tests/modules/requirements.txt index 24f17aacf5..690e7fb661 100644 --- a/tests/modules/requirements.txt +++ b/tests/modules/requirements.txt @@ -1,3 +1,3 @@ -f https://download.pytorch.org/whl/torch_stable.html timm==v0.4.12 -torch==1.9.0+cu111 +torch>=1.9.0+cu111 From 24089cc128f2db933e8df84eddfa45ae7fff8f12 Mon Sep 17 00:00:00 2001 From: Boris Fomitchev Date: Mon, 20 Sep 2021 20:27:27 -0700 Subject: [PATCH 3/6] Cleanup Signed-off-by: Boris Fomitchev --- docker/Dockerfile | 3 --- 1 file changed, 3 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index 7552298930..ca58cbfda8 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -24,9 +24,6 @@ RUN cp ./docker/WORKSPACE.cu.docker WORKSPACE # This script builds both libtrtorch bin/lib/include tarball and the Pythin wheel, in dist/ RUN ./docker/dist-build.sh -# This currently does not work with BUILDKIT=1 -# RUN ./docker/dist-test.sh - FROM base as trtorch # copy source repo From 3dd7b6afa4ec6b70edfeb399ccddfc17f1c92fbf Mon Sep 17 00:00:00 2001 From: Boris Fomitchev Date: Tue, 21 Sep 2021 03:14:23 -0700 Subject: [PATCH 4/6] Moving hub.py to tests Signed-off-by: Boris Fomitchev --- docker/dist-build.sh | 2 -- docker/dist-test.sh | 3 +++ 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/docker/dist-build.sh b/docker/dist-build.sh index 24ac387f1b..83564eb000 100755 --- a/docker/dist-build.sh +++ b/docker/dist-build.sh @@ -19,5 +19,3 @@ pip3 install timm # test install mkdir -p /opt/trtorch && tar xvf dist/libtrtorch.tar.gz --strip-components 2 -C /opt/trtorch --exclude=LICENSE && pip3 install dist/*.whl -cd tests/modules && python3 ./hub.py -cd ../.. diff --git a/docker/dist-test.sh b/docker/dist-test.sh index 04e7ff857b..9daadffbc5 100755 --- a/docker/dist-test.sh +++ b/docker/dist-test.sh @@ -1,5 +1,8 @@ #!/bin/bash # Build and run unit tests +cd tests/modules && python3 ./hub.py +cd ../.. + bazel test //tests:tests //tests:python_api_tests --compilation_mode opt --jobs 4 From 4301ca5a941debd40d9051c8e74acd05d974319d Mon Sep 17 00:00:00 2001 From: Boris Fomitchev Date: Tue, 21 Sep 2021 12:52:00 -0700 Subject: [PATCH 5/6] Added Dockefile documentation section Signed-off-by: Boris Fomitchev --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 5db9a591e2..f06c577483 100644 --- a/README.md +++ b/README.md @@ -206,6 +206,10 @@ docker run -it -v$(pwd)/..:/workspace/TRTorch build_trtorch_wheel /bin/bash /wor ``` Python compilation expects using the tarball based compilation strategy from above. +## Building Docker Image +`Dockerfile` in `//docker` directory builds an image, based on latest NGC PyTorch container, with TRTorch binaries/libraries/headers installed in /opt/trtorch, and Python wheel installed locally. +To specify different NGC PyTorch container release as the base, use `--build-arg BASE=21.06` for 21.06 etc. + ## How do I add support for a new op... ### In TRTorch? From 67ba228e28ce14babdc4cd70080e3532ddc63474 Mon Sep 17 00:00:00 2001 From: Boris Fomitchev Date: Wed, 22 Sep 2021 13:48:17 -0700 Subject: [PATCH 6/6] Bumping up base container version, cleanup Signed-off-by: Boris Fomitchev --- docker/Dockerfile | 2 +- docker/dist-build.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index ca58cbfda8..c24de5b66e 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -1,4 +1,4 @@ -ARG BASE=21.07 +ARG BASE=21.08 FROM nvcr.io/nvidia/pytorch:${BASE}-py3 as base FROM base as trtorch-builder-base diff --git a/docker/dist-build.sh b/docker/dist-build.sh index 83564eb000..9874ccd326 100755 --- a/docker/dist-build.sh +++ b/docker/dist-build.sh @@ -2,7 +2,7 @@ mkdir -p dist -bazel build //:libtrtorch //:bin --compilation_mode opt +bazel build //:libtrtorch --compilation_mode opt cd py && MAX_JOBS=1 LANG=en_US.UTF-8 LANGUAGE=en_US:en LC_ALL=en_US.UTF-8 python3 setup.py bdist_wheel --use-cxx11-abi