Skip to content

Commit ed4f4e0

Browse files
committed
re-work PR changes instead of #328
Signed-off-by: Srikanth Ramakrishna <srikanth.ramakrishna@intel.com>
1 parent 262a89c commit ed4f4e0

9 files changed

+302
-39
lines changed

pytorch/Dockerfile

+90-14
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ ARG BASE_IMAGE_TAG
3434
ARG PACKAGE_OPTION=pip
3535
ARG PYTHON_VERSION
3636
ARG PYTHON_BASE=${REGISTRY}/${REPO}:b-${GITHUB_RUN_NUMBER}-${BASE_IMAGE_NAME}-${BASE_IMAGE_TAG}-${PACKAGE_OPTION}-py${PYTHON_VERSION}-base
37+
ARG TORCHSERVE_BASE=${PYTHON_BASE}
3738
FROM ${PYTHON_BASE} AS ipex-base-pip
3839

3940
WORKDIR /
@@ -181,13 +182,17 @@ RUN apt-get update && \
181182
intel-oneapi-runtime-mkl=${MKL_VER} \
182183
intel-oneapi-runtime-ccl=${CCL_VER};
183184

185+
RUN rm -rf /etc/apt/sources.list.d/intel-gpu-jammy.list /etc/apt/sources.list.d/oneAPI.list
186+
187+
ENV LD_LIBRARY_PATH=/opt/intel/oneapi/redist/lib:$LD_LIBRARY_PATH
188+
189+
FROM ipex-xpu-base AS ipex-xpu-base-wheels
190+
184191
WORKDIR /
185192
COPY xpu-requirements.txt .
186193

187194
RUN python -m pip install --no-cache-dir -r xpu-requirements.txt && \
188-
rm -rf xpu-requirements.txt /etc/apt/sources.list.d/intel-gpu-jammy.list /etc/apt/sources.list.d/oneAPI.list
189-
190-
ENV LD_LIBRARY_PATH=/opt/intel/oneapi/redist/lib:$LD_LIBRARY_PATH
195+
rm -rf xpu-requirements.txt
191196

192197
FROM ipex-xpu-base AS ipex-xpu-jupyter
193198

@@ -205,7 +210,8 @@ EXPOSE 8888
205210

206211
CMD ["bash", "-c", "source /etc/bash.bashrc && jupyter notebook --notebook-dir=/jupyter --port 8888 --ip 0.0.0.0 --no-browser --allow-root --ServerApp.token= --ServerApp.password= --ServerApp.allow_origin=* --ServerApp.base_url=$NB_PREFIX"]
207212

208-
FROM ${PYTHON_BASE} as torchserve-base
213+
214+
FROM ${TORCHSERVE_BASE} as torchserve-base
209215

210216
ENV PYTHONUNBUFFERED=TRUE
211217

@@ -221,8 +227,6 @@ RUN useradd -m -s /bin/bash model-server && \
221227
mkdir -p /home/model-server/model-store && \
222228
chown -R model-server /home/model-server/
223229

224-
FROM torchserve-base AS compile
225-
226230
RUN apt-get update -y && apt-get install -y --no-install-recommends --fix-missing \
227231
g++ \
228232
git \
@@ -237,11 +241,6 @@ RUN python3 -m venv /home/venv
237241
ENV PATH="/home/venv/bin:$PATH"
238242

239243
WORKDIR /home/model-server
240-
COPY torchserve-requirements.txt .
241-
COPY requirements.txt .
242-
243-
RUN python -m pip install --no-cache-dir -r requirements.txt && \
244-
python -m pip install --no-cache-dir -r torchserve-requirements.txt
245244

246245
RUN echo -e "#!/bin/bash \n\
247246
set -e \n\
@@ -253,13 +252,29 @@ else \n\
253252
fi \n\
254253
tail -f /dev/null" >> /usr/local/bin/dockerd-entrypoint.sh
255254

256-
FROM torchserve-base AS torchserve
255+
FROM torchserve-base AS compile-cpu
256+
257+
COPY serving/torchserve-requirements.txt .
258+
COPY requirements.txt .
259+
260+
RUN python -m pip install --no-cache-dir -r requirements.txt && \
261+
python -m pip install --no-cache-dir -r torchserve-requirements.txt && \
262+
rm -rf requirements.txt torchserve-requirements.txt
263+
264+
FROM torchserve-base AS compile-xpu
265+
266+
COPY serving/torchserve-xpu-requirements.txt .
267+
268+
RUN python -m pip install --no-cache-dir -r torchserve-xpu-requirements.txt && \
269+
rm -rf torchserve-xpu-requirements.txt
270+
271+
FROM torchserve-base AS torchserve-cpu
257272

258273
USER model-server
259274
WORKDIR /home/model-server
260275

261-
COPY --chown=model-server --from=compile /home/venv /home/venv
262-
COPY --chown=model-server --chmod=755 --from=compile /usr/local/bin/dockerd-entrypoint.sh /usr/local/bin/dockerd-entrypoint.sh
276+
COPY --chown=model-server --from=compile-cpu /home/venv /home/venv
277+
COPY --chown=model-server --chmod=755 --from=compile-cpu /usr/local/bin/dockerd-entrypoint.sh /usr/local/bin/dockerd-entrypoint.sh
263278
COPY --chown=model-server serving/config.properties /home/model-server/config.properties
264279

265280
ENV PATH="/home/venv/bin:$PATH"
@@ -270,3 +285,64 @@ EXPOSE 8080 8081 8082 7070 7071
270285

271286
ENTRYPOINT ["/usr/local/bin/dockerd-entrypoint.sh"]
272287
CMD ["serve"]
288+
289+
FROM torchserve-base AS torchserve-xpu
290+
291+
RUN apt-get update && \
292+
apt-get install -y --no-install-recommends --fix-missing \
293+
gnupg2 \
294+
gpg-agent \
295+
rsync && \
296+
apt-get clean && \
297+
rm -rf /var/lib/apt/lists/*
298+
299+
RUN wget -qO - https://repositories.intel.com/gpu/intel-graphics.key | \
300+
gpg --dearmor --yes --output /usr/share/keyrings/intel-graphics.gpg
301+
RUN echo "deb [arch=amd64 signed-by=/usr/share/keyrings/intel-graphics.gpg] https://repositories.intel.com/gpu/ubuntu jammy unified" | \
302+
tee /etc/apt/sources.list.d/intel-gpu-jammy.list
303+
304+
RUN apt-get update && \
305+
apt-get install -y --no-install-recommends \
306+
jq \
307+
curl \
308+
libnl-genl-3-200 \
309+
intel-gsc \
310+
libdrm2 \
311+
intel-metrics-discovery \
312+
intel-metrics-library && \
313+
apt-get autoremove -y && \
314+
rm -rf /var/lib/apt/lists/*
315+
316+
ARG XPU_SMI_VERSION
317+
318+
ARG API_URL=https://api.github.com/repos/intel/xpumanager/releases/tags/V${XPU_SMI_VERSION}
319+
320+
RUN wget -q --header="Accept: application/vnd.github.v3+json" --header="User-Agent: MyClient/1.0.0" -O - "$API_URL" | tee /tmp/asset_data.txt && \
321+
wget -q --no-check-certificate "$(jq -r '.assets[] | select(.name | test("^xpu-smi.*u22\\.04_amd64\\.deb$")) | .browser_download_url' < /tmp/asset_data.txt)" && \
322+
ldconfig && dpkg -i --force-all -- *.deb && \
323+
rm -rf -- *.deb /etc/apt/sources.list.d/intel-gpu-jammy.list /etc/apt/sources.list.d/oneAPI.list /tmp/asset_data.txt
324+
325+
ARG GID=109
326+
327+
RUN groupadd -g ${GID} render &&\
328+
usermod -aG video,render model-server
329+
330+
USER model-server
331+
332+
WORKDIR /home/model-server
333+
334+
RUN wget --progress=dot:giga https://raw.githubusercontent.com/pytorch/serve/master/examples/intel_extension_for_pytorch/intel_gpu_metric_collector.py && \
335+
wget --progress=dot:giga https://raw.githubusercontent.com/pytorch/serve/master/examples/intel_extension_for_pytorch/intel_gpu.py
336+
337+
COPY --chown=model-server --from=compile-xpu /home/venv /home/venv
338+
COPY --chown=model-server --chmod=755 --from=compile-xpu /usr/local/bin/dockerd-entrypoint.sh /usr/local/bin/dockerd-entrypoint.sh
339+
COPY --chown=model-server serving/config-xpu.properties /home/model-server/config.properties
340+
341+
ENV PATH="/home/venv/bin:$PATH"
342+
ENV TEMP=/home/model-server/tmp
343+
344+
# 8080/8081/8082 REST and 7070/7071 gRPC
345+
EXPOSE 8080 8081 8082 7070 7071
346+
347+
ENTRYPOINT ["/usr/local/bin/dockerd-entrypoint.sh"]
348+
CMD ["serve"]

pytorch/docker-compose.yaml

+26-5
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ services:
122122
org.opencontainers.base.name: "intel/python:3.10-core"
123123
org.opencontainers.image.title: "Intel® Extension for PyTorch XPU Base Image"
124124
org.opencontainers.image.version: ${IPEX_VERSION:-2.1.40}-xpu-${PACKAGE_OPTION:-pip}-base
125-
target: ipex-xpu-base
125+
target: ipex-xpu-base-wheels
126126
command: >
127127
python -c "import torch;print(torch.device('xpu'));import
128128
intel_extension_for_pytorch as
@@ -156,7 +156,7 @@ services:
156156
image: ${REGISTRY}/${REPO}:b-${GITHUB_RUN_NUMBER:-0}-${BASE_IMAGE_NAME:-ubuntu}-${BASE_IMAGE_TAG:-22.04}-${PACKAGE_OPTION:-pip}-py${PYTHON_VERSION:-3.10}-ipex-${IPEX_VERSION:-2.1.40xpu}-xpu-jupyter
157157
ports:
158158
- 8888:8888
159-
torchserve:
159+
torchserve-cpu:
160160
build:
161161
args:
162162
PACKAGE_OPTION: pip
@@ -165,22 +165,43 @@ services:
165165
dependency.apt.openjdk-17-jdk: true
166166
dependency.idp: false
167167
dependency.python.ipex: requirements.txt
168-
dependency.python.pip: torchserve-requirements.txt
168+
dependency.python.pip: serving/torchserve-requirements.txt
169169
docs: serving
170170
org.opencontainers.base.name: "intel/python:3.10-core"
171171
org.opencontainers.image.title: "Intel® Extension for PyTorch Serving Image"
172172
org.opencontainers.image.version: ${IPEX_VERSION:-2.4.0}-serving-cpu
173-
target: torchserve
173+
target: torchserve-cpu
174174
command: torchserve --version
175175
entrypoint: ""
176176
extends: ipex-base
177-
image: ${REGISTRY}/${REPO}:b-${GITHUB_RUN_NUMBER:-0}-${BASE_IMAGE_NAME:-ubuntu}-${BASE_IMAGE_TAG:-22.04}-py${PYTHON_VERSION:-3.10}-torchserve
177+
image: ${REGISTRY}/${REPO}:b-${GITHUB_RUN_NUMBER:-0}-${BASE_IMAGE_NAME:-ubuntu}-${BASE_IMAGE_TAG:-22.04}-py${PYTHON_VERSION:-3.10}-torchserve-cpu
178178
ports:
179179
- 8080:8080
180180
- 8081:8081
181181
- 8082:8082
182182
- 7070:7070
183183
- 7071:7071
184+
torchserve-xpu:
185+
build:
186+
args:
187+
PACKAGE_OPTION: pip
188+
XPU_SMI_VERSION: ${XPU_SMI_VERSION:-1.2.38}
189+
TORCHSERVE_BASE: ipex-xpu-base
190+
labels:
191+
dependency.apt.numactl: true
192+
dependency.apt.openjdk-17-jdk: true
193+
dependency.apt.xpu-smi: ${XPU_SMI_VERSION:-1.2.38}
194+
dependency.idp: false
195+
dependency.python.pip: serving/torchserve-xpu-requirements.txt
196+
docs: serving
197+
org.opencontainers.base.name: "intel/python:3.10-core"
198+
org.opencontainers.image.title: "Intel® Extension for PyTorch XPU Serving Image"
199+
org.opencontainers.image.version: ${IPEX_VERSION:-2.1.40}-serving-xpu
200+
target: torchserve-xpu
201+
command: torchserve --version
202+
entrypoint: ""
203+
extends: xpu
204+
image: ${REGISTRY}/${REPO}:b-${GITHUB_RUN_NUMBER:-0}-${BASE_IMAGE_NAME:-ubuntu}-${BASE_IMAGE_TAG:-22.04}-py${PYTHON_VERSION:-3.10}-torchserve-xpu
184205
hf-genai:
185206
build:
186207
args:

pytorch/serving/README.md

+50-6
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,73 @@ The [Torchserve Model Archiver](https://github.com/pytorch/serve/blob/master/mod
1212

1313
Follow the instructions found in the link above depending on whether you are intending to archive a model or a workflow. Use the provided container rather than installing the archiver with the example command below:
1414

15+
#### Create a Model Archive for CPU device
16+
1517
```bash
1618
curl -O https://download.pytorch.org/models/squeezenet1_1-b8a52dc0.pth
1719
docker run --rm -it \
20+
--entrypoint='' \
21+
-u root \
1822
-v $PWD:/home/model-server \
1923
intel/intel-optimized-pytorch:2.4.0-serving-cpu \
20-
torch-model-archiver --model-name squeezenet \
21-
--version 1.0 \
22-
--model-file model-archive/model.py \
23-
--serialized-file squeezenet1_1-b8a52dc0.pth \
24-
--handler image_classifier \
25-
--export-path /home/model-server
24+
torch-model-archiver --model-name squeezenet1_1 \
25+
--version 1.1 \
26+
--model-file model-archive/model.py \
27+
--serialized-file squeezenet1_1-b8a52dc0.pth \
28+
--handler image_classifier \
29+
--export-path /home/model-server
30+
```
31+
32+
### Create a Model Archive for XPU device
33+
34+
Use a squeezenet model [optimized](./model-store/ipex_squeezenet.py) for XPU using Intel® Extension for PyTorch*.
35+
36+
```bash
37+
docker run --rm -it \
38+
--entrypoint='' \
39+
-u root \
40+
-v $PWD:/home/model-server \
41+
--device /dev/dri \
42+
intel/intel-optimized-pytorch:2.1.40-serving-xpu \
43+
sh -c 'python model-archive/ipex_squeezenet.py && \
44+
torch-model-archiver --model-name squeezenet1_1 \
45+
--version 1.1 \
46+
--serialized-file squeezenet1_1-jit.pt \
47+
--handler image_classifier \
48+
--export-path /home/model-server'
2649
```
2750

2851
### Test Model
2952

3053
Test Torchserve with the new archived model. The example below is for the squeezenet model.
3154

55+
#### Run Torchserve for CPU device
56+
3257
```bash
3358
# Assuming that the above pre-archived model is in the current working directory
3459
docker run -d --rm --name server \
3560
-v $PWD:/home/model-server/model-store \
61+
-v $PWD/wf-store:/home/model-server/wf-store \
3662
--net=host \
3763
intel/intel-optimized-pytorch:2.4.0-serving-cpu
64+
```
65+
66+
#### Run Torchserve for XPU device
67+
68+
```bash
69+
# Assuming that the above pre-archived model is in the current working directory
70+
docker run -d --rm --name server \
71+
-v $PWD:/home/model-server/model-store \
72+
-v $PWD/wf-store:/home/model-server/wf-store \
73+
-v $PWD/config-xpu.properties:/home/model-server/config.properties \
74+
--net=host \
75+
--device /dev/dri \
76+
intel/intel-optimized-pytorch:2.1.40-serving-xpu
77+
```
78+
79+
After lauching the container, follow the steps below:
80+
81+
```bash
3882
# Verify that the container has launched successfully
3983
docker logs server
4084
# Attempt to register the model and make an inference request

pytorch/serving/config-xpu.properties

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
inference_address=http://0.0.0.0:8080
2+
management_address=http://0.0.0.0:8081
3+
metrics_address=http://0.0.0.0:8082
4+
number_of_netty_threads=32
5+
install_py_dep_per_model=true
6+
job_queue_size=1000
7+
model_store=/home/model-server/model-store
8+
workflow_store=/home/model-server/wf-store
9+
allowed_urls=https://s3.amazonaws.com/.*,https://torchserve.pytorch.org/.*
10+
ipex_enable=true
11+
ipex_gpu_enable=true
12+
system_metrics_cmd=/home/model-server/intel_gpu_metric_collector.py --gpu 1
13+
disable_token_authorization=true
14+
enable_model_api=true
15+
enable_envvars_config=true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Copyright (c) 2024 Intel Corporation
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# ============================================================================
15+
#
16+
# This file was assembled from multiple pieces, whose use is documented
17+
# throughout. Please refer to the TensorFlow dockerfiles documentation
18+
# for more information.
19+
# based on https://github.com/pytorch/pytorch/blob/master/Dockerfile
20+
#
21+
# NOTE: To build this you will need a docker version >= 19.03 and DOCKER_BUILDKIT=1
22+
#
23+
# If you do not use buildkit you are not going to have a good time
24+
#
25+
# For reference:
26+
# https://docs.docker.com/develop/develop-images/build_enhancements/
27+
28+
# pylint: skip-file
29+
30+
import intel_extension_for_pytorch as ipex
31+
import torch
32+
import torchvision.models as models
33+
34+
# load the model
35+
model = models.squeezenet1_1(pretrained=True)
36+
model = model.eval()
37+
38+
# define dummy input tensor to use for the model's forward call to record operations in the model for tracing
39+
N, C, H, W = 1, 3, 224, 224
40+
data = torch.randn(N, C, H, W)
41+
42+
model.eval()
43+
data = torch.rand(1, 3, 224, 224)
44+
45+
#################### code changes #################
46+
model = model.to("xpu")
47+
data = data.to("xpu")
48+
model = ipex.optimize(model, dtype=torch.bfloat16)
49+
#################### code changes #################
50+
51+
with torch.no_grad():
52+
with torch.xpu.amp.autocast(enabled=True, dtype=torch.bfloat16):
53+
############################# code changes #####################
54+
model = torch.jit.trace(model, data)
55+
model = torch.jit.freeze(model)
56+
model(data)
57+
torch.jit.save(model, "squeezenet1_1-jit.pt")

pytorch/serving/model-archive/mar-test.sh

+13-3
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,18 @@
2626
# For reference:
2727
# https://docs.docker.com/develop/develop-images/build_enhancements/
2828

29-
wget https://download.pytorch.org/models/squeezenet1_1-b8a52dc0.pth
30-
torch-model-archiver --model-name squeezenet1_1 --version 1.1 --model-file /home/model-server/model-archive/model.py --serialized-file squeezenet1_1-b8a52dc0.pth --handler image_classifier --export-path /home/model-server/model-store
29+
if [[ "$1" == "cpu" ]]; then
30+
wget https://download.pytorch.org/models/squeezenet1_1-b8a52dc0.pth
31+
torch-model-archiver --model-name squeezenet1_1 --version 1.1 --model-file /home/model-server/model-archive/model.py --serialized-file squeezenet1_1-b8a52dc0.pth --handler image_classifier --export-path /home/model-server/model-store
32+
rm -rf squeezenet1_1-b8a52dc0.pth
33+
elif [[ "$1" == "xpu" ]]; then
34+
python /home/model-server/model-archive/ipex_squeezenet.py
35+
torch-model-archiver --model-name squeezenet1_1 --version 1.1 --serialized-file squeezenet1_1-jit.pt --handler image_classifier --export-path /home/model-server/model-store
36+
rm -rf squeezenet1_1-jit.pt
37+
else
38+
echo "Only cpu and xpu devices supported"
39+
exit 1
40+
fi
41+
3142
[ -f "/home/model-server/model-store/squeezenet1_1.mar" ] && echo "squeezenet1_1.pth Archived Succesfully at /home/model-server/model-store/squeezenet1_1.mar"
32-
rm -rf squeezenet1_1-b8a52dc0.pth
3343
find . | grep -E "(/__pycache__$|\.pyc$|\.pyo$)" | xargs rm -rf

0 commit comments

Comments
 (0)