Skip to content
This repository was archived by the owner on Aug 7, 2025. It is now read-only.

Refactor Dokcerfile.cpp and documentation #3015

Merged
merged 6 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
## Requirements
* C++17
* GCC version: gcc-9
* cmake version: 3.18+
* cmake version: 3.26.4+
* Linux

For convenience, a docker container can be used as the development environment to build and install Torchserve CPP
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
```
cd serve/docker
# For CPU support
Expand All @@ -21,6 +21,7 @@ docker run [-v /path/to/build/dir:/serve/cpp/_build] -it pytorch/torchserve:cpp-
# For GPU support
docker run --gpus all [-v /path/to/build/dir:/serve/cpp/_build] -it pytorch/torchserve:cpp-dev-gpu /bin/bash
```
`Warning`: The dev docker container does not install all necessary dependencies or build Torchserve CPP. Please follow the steps below after starting the container.

## Installation and Running TorchServe CPP
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.
Expand Down
30 changes: 9 additions & 21 deletions docker/Dockerfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,34 @@
ARG BASE_IMAGE=ubuntu:20.04
ARG PYTHON_VERSION=3.9
ARG CMAKE_VERSION=3.26.4
ARG GCC_VERSION=9
ARG BRANCH_NAME="master"
ARG USE_CUDA_VERSION=""

FROM ${BASE_IMAGE} AS cpp-dev-image
ARG BASE_IMAGE
ARG PYTHON_VERSION
ARG CMAKE_VERSION
ARG GCC_VERSION
ARG BRANCH_NAME
ARG USE_CUDA_VERSION
ARG DEBIAN_FRONTEND=noninteractive
ENV PYTHONUNBUFFERED TRUE
ENV TZ=Etc/UTC

RUN --mount=type=cache,id=apt-dev,target=/var/cache/apt \
apt-get update && \
apt-get install software-properties-common -y && \
add-apt-repository -y ppa:deadsnakes/ppa && \
DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends -y \
apt-get install --no-install-recommends -y \
sudo \
vim \
git \
curl \
wget \
rsync \
gpg \
gcc-$GCC_VERSION \
ca-certificates \
lsb-release \
openjdk-17-jdk \
Expand All @@ -51,32 +56,15 @@ RUN --mount=type=cache,id=apt-dev,target=/var/cache/apt \
RUN python$PYTHON_VERSION -m venv /home/venv
ENV PATH="/home/venv/bin:$PATH"

# Enable installation of recent cmake release
# Enable installation of recent cmake release and pin cmake & cmake-data version
# Ref: https://apt.kitware.com/
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) \
&& (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) \
&& apt-get update \
&& (test -f /usr/share/doc/kitware-archive-keyring/copyright || sudo rm /usr/share/keyrings/kitware-archive-keyring.gpg) \
&& sudo apt-get install kitware-archive-keyring \
&& rm -rf /var/lib/apt/lists/*

# Pin cmake and cmake-data version
# Ref: https://manpages.ubuntu.com/manpages/xenial/man5/apt_preferences.5.html
RUN echo "Package: cmake\nPin: version $CMAKE_VERSION*\nPin-Priority: 1001" > /etc/apt/preferences.d/cmake
RUN echo "Package: cmake-data\nPin: version $CMAKE_VERSION*\nPin-Priority: 1001" > /etc/apt/preferences.d/cmake-data

# Install CUDA toolkit to enable "libtorch" build with GPU support
RUN apt-get update && \
if echo "$BASE_IMAGE" | grep -q "cuda:"; then \
if [ "$USE_CUDA_VERSION" = "cu121" ]; then \
apt-get -y install cuda-toolkit-12-1; \
elif [ "$USE_CUDA_VERSION" = "cu118" ]; then \
apt-get -y install cuda-toolkit-11-8; \
else \
echo "Cuda version not supported by CPP backend: $USE_CUDA_VERSION"; \
exit 1; \
fi; \
fi \
&& echo "Package: cmake\nPin: version $CMAKE_VERSION*\nPin-Priority: 1001" > /etc/apt/preferences.d/cmake \
&& echo "Package: cmake-data\nPin: version $CMAKE_VERSION*\nPin-Priority: 1001" > /etc/apt/preferences.d/cmake-data \
&& rm -rf /var/lib/apt/lists/*

RUN git clone --recursive https://github.com/pytorch/serve.git \
Expand Down
11 changes: 11 additions & 0 deletions docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Use `build_image.sh` script to build the docker images. The script builds the `p
|-t, --tag|Tag name for image. If not specified, script uses torchserve default tag names.|
|-cv, --cudaversion| Specify to cuda version to use. Supported values `cu92`, `cu101`, `cu102`, `cu111`, `cu113`, `cu116`, `cu117`, `cu118`. `cu121`, Default `cu121`|
|-ipex, --build-with-ipex| Specify to build with intel_extension_for_pytorch. If not specified, script builds without intel_extension_for_pytorch.|
|-cpp, --build-cpp specify to build TorchServe CPP|
|-n, --nightly| Specify to build with TorchServe nightly.|
|-py, --pythonversion| Specify the python version to use. Supported values `3.8`, `3.9`, `3.10`, `3.11`. Default `3.9`|

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

- For creating image to build Torchserve CPP with CPU support:
```bash
./build_image.sh -bt dev -cpp
```

- For creating image to build Torchserve CPP with GPU support:
```bash
./build_image.sh -bt dev -g [-cv cu121|cu118] -cpp
```


## Start a container with a TorchServe image

Expand Down
9 changes: 7 additions & 2 deletions docker/build_image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,14 @@ then

if [[ "${MACHINE}" == "gpu" || "${CUDA_VERSION}" != "" ]];
then
if [[ "${CUDA_VERSION}" != "cu121" && "${CUDA_VERSION}" != "cu118" ]];
if [ "${CUDA_VERSION}" == "cu121" ];
then
echo "Only cuda versions 12.1 and 11.8 are supported for CPP"
BASE_IMAGE="nvidia/cuda:12.1.1-devel-ubuntu20.04"
elif [ "${CUDA_VERSION}" == "cu118" ];
then
BASE_IMAGE="nvidia/cuda:11.8.0-devel-ubuntu20.04"
else
echo "Cuda version $CUDA_VERSION is not supported for CPP"
exit 1
fi
fi
Expand Down