Skip to content

Commit 9a27a63

Browse files
authored
Refactor Dokcerfile.cpp and documentation (#3015)
* Refactor Dokcerfile.cpp * Update documentation * Use nvidia devel image instead of runtime image * Configure timezone and enable DEBIAN_FRONTEND=noninteractive * Include documentation about dev container behavior
1 parent ad71e0f commit 9a27a63

File tree

4 files changed

+30
-25
lines changed

4 files changed

+30
-25
lines changed

cpp/README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
## Requirements
33
* C++17
44
* GCC version: gcc-9
5-
* cmake version: 3.18+
5+
* cmake version: 3.26.4+
66
* Linux
77

8-
For convenience, a docker container can be used as the development environment to build and install Torchserve CPP
8+
For convenience, a [docker container](../docker/README.md#create-torchserve-docker-image) can be used as the development environment to build and install Torchserve CPP
99
```
1010
cd serve/docker
1111
# For CPU support
@@ -21,6 +21,7 @@ docker run [-v /path/to/build/dir:/serve/cpp/_build] -it pytorch/torchserve:cpp-
2121
# For GPU support
2222
docker run --gpus all [-v /path/to/build/dir:/serve/cpp/_build] -it pytorch/torchserve:cpp-dev-gpu /bin/bash
2323
```
24+
`Warning`: The dev docker container does not install all necessary dependencies or build Torchserve CPP. Please follow the steps below after starting the container.
2425

2526
## Installation and Running TorchServe CPP
2627
This installation instruction assumes that TorchServe is already installed through pip/conda/source. If this is not the case install it after the `Install dependencies` step through your preferred method.

docker/Dockerfile.cpp

+9-21
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,34 @@
1616
ARG BASE_IMAGE=ubuntu:20.04
1717
ARG PYTHON_VERSION=3.9
1818
ARG CMAKE_VERSION=3.26.4
19+
ARG GCC_VERSION=9
1920
ARG BRANCH_NAME="master"
2021
ARG USE_CUDA_VERSION=""
2122

2223
FROM ${BASE_IMAGE} AS cpp-dev-image
2324
ARG BASE_IMAGE
2425
ARG PYTHON_VERSION
2526
ARG CMAKE_VERSION
27+
ARG GCC_VERSION
2628
ARG BRANCH_NAME
2729
ARG USE_CUDA_VERSION
30+
ARG DEBIAN_FRONTEND=noninteractive
2831
ENV PYTHONUNBUFFERED TRUE
32+
ENV TZ=Etc/UTC
2933

3034
RUN --mount=type=cache,id=apt-dev,target=/var/cache/apt \
3135
apt-get update && \
3236
apt-get install software-properties-common -y && \
3337
add-apt-repository -y ppa:deadsnakes/ppa && \
34-
DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y \
38+
apt-get install --no-install-recommends -y \
3539
sudo \
3640
vim \
3741
git \
3842
curl \
3943
wget \
4044
rsync \
4145
gpg \
46+
gcc-$GCC_VERSION \
4247
ca-certificates \
4348
lsb-release \
4449
openjdk-17-jdk \
@@ -51,32 +56,15 @@ RUN --mount=type=cache,id=apt-dev,target=/var/cache/apt \
5156
RUN python$PYTHON_VERSION -m venv /home/venv
5257
ENV PATH="/home/venv/bin:$PATH"
5358
54-
# Enable installation of recent cmake release
59+
# Enable installation of recent cmake release and pin cmake & cmake-data version
5560
# Ref: https://apt.kitware.com/
5661
RUN (wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | gpg --dearmor - | tee /usr/share/keyrings/kitware-archive-keyring.gpg >/dev/null) \
5762
&& (echo "deb [signed-by=/usr/share/keyrings/kitware-archive-keyring.gpg] https://apt.kitware.com/ubuntu/ $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/kitware.list >/dev/null) \
5863
&& apt-get update \
5964
&& (test -f /usr/share/doc/kitware-archive-keyring/copyright || sudo rm /usr/share/keyrings/kitware-archive-keyring.gpg) \
6065
&& sudo apt-get install kitware-archive-keyring \
61-
&& rm -rf /var/lib/apt/lists/*
62-
63-
# Pin cmake and cmake-data version
64-
# Ref: https://manpages.ubuntu.com/manpages/xenial/man5/apt_preferences.5.html
65-
RUN echo "Package: cmake\nPin: version $CMAKE_VERSION*\nPin-Priority: 1001" > /etc/apt/preferences.d/cmake
66-
RUN echo "Package: cmake-data\nPin: version $CMAKE_VERSION*\nPin-Priority: 1001" > /etc/apt/preferences.d/cmake-data
67-
68-
# Install CUDA toolkit to enable "libtorch" build with GPU support
69-
RUN apt-get update && \
70-
if echo "$BASE_IMAGE" | grep -q "cuda:"; then \
71-
if [ "$USE_CUDA_VERSION" = "cu121" ]; then \
72-
apt-get -y install cuda-toolkit-12-1; \
73-
elif [ "$USE_CUDA_VERSION" = "cu118" ]; then \
74-
apt-get -y install cuda-toolkit-11-8; \
75-
else \
76-
echo "Cuda version not supported by CPP backend: $USE_CUDA_VERSION"; \
77-
exit 1; \
78-
fi; \
79-
fi \
66+
&& echo "Package: cmake\nPin: version $CMAKE_VERSION*\nPin-Priority: 1001" > /etc/apt/preferences.d/cmake \
67+
&& echo "Package: cmake-data\nPin: version $CMAKE_VERSION*\nPin-Priority: 1001" > /etc/apt/preferences.d/cmake-data \
8068
&& rm -rf /var/lib/apt/lists/*
8169
8270
RUN git clone --recursive https://github.com/pytorch/serve.git \

docker/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Use `build_image.sh` script to build the docker images. The script builds the `p
4141
|-t, --tag|Tag name for image. If not specified, script uses torchserve default tag names.|
4242
|-cv, --cudaversion| Specify to cuda version to use. Supported values `cu92`, `cu101`, `cu102`, `cu111`, `cu113`, `cu116`, `cu117`, `cu118`. `cu121`, Default `cu121`|
4343
|-ipex, --build-with-ipex| Specify to build with intel_extension_for_pytorch. If not specified, script builds without intel_extension_for_pytorch.|
44+
|-cpp, --build-cpp specify to build TorchServe CPP|
4445
|-n, --nightly| Specify to build with TorchServe nightly.|
4546
|-py, --pythonversion| Specify the python version to use. Supported values `3.8`, `3.9`, `3.10`, `3.11`. Default `3.9`|
4647

@@ -147,6 +148,16 @@ Creates a docker image with `torchserve` and `torch-model-archiver` installed fr
147148
./build_image.sh -bt dev -ipex -t torchserve-ipex:1.0
148149
```
149150

151+
- For creating image to build Torchserve CPP with CPU support:
152+
```bash
153+
./build_image.sh -bt dev -cpp
154+
```
155+
156+
- For creating image to build Torchserve CPP with GPU support:
157+
```bash
158+
./build_image.sh -bt dev -g [-cv cu121|cu118] -cpp
159+
```
160+
150161

151162
## Start a container with a TorchServe image
152163

docker/build_image.sh

+7-2
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,14 @@ then
174174

175175
if [[ "${MACHINE}" == "gpu" || "${CUDA_VERSION}" != "" ]];
176176
then
177-
if [[ "${CUDA_VERSION}" != "cu121" && "${CUDA_VERSION}" != "cu118" ]];
177+
if [ "${CUDA_VERSION}" == "cu121" ];
178178
then
179-
echo "Only cuda versions 12.1 and 11.8 are supported for CPP"
179+
BASE_IMAGE="nvidia/cuda:12.1.1-devel-ubuntu20.04"
180+
elif [ "${CUDA_VERSION}" == "cu118" ];
181+
then
182+
BASE_IMAGE="nvidia/cuda:11.8.0-devel-ubuntu20.04"
183+
else
184+
echo "Cuda version $CUDA_VERSION is not supported for CPP"
180185
exit 1
181186
fi
182187
fi

0 commit comments

Comments
 (0)