-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable the IPEX optimization for python backend
Signed-off-by: yuanwu <yuan.wu@intel.com>
- Loading branch information
1 parent
d221b99
commit 4d285bd
Showing
8 changed files
with
159 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
FROM lukemathwalker/cargo-chef:latest-rust-1.75-bookworm AS chef | ||
WORKDIR /usr/src | ||
|
||
ENV SCCACHE=0.5.4 | ||
ENV RUSTC_WRAPPER=/usr/local/bin/sccache | ||
|
||
# Download and configure sccache | ||
RUN curl -fsSL https://github.com/mozilla/sccache/releases/download/v$SCCACHE/sccache-v$SCCACHE-x86_64-unknown-linux-musl.tar.gz | tar -xzv --strip-components=1 -C /usr/local/bin sccache-v$SCCACHE-x86_64-unknown-linux-musl/sccache && \ | ||
chmod +x /usr/local/bin/sccache | ||
|
||
FROM chef AS planner | ||
|
||
COPY backends backends | ||
COPY core core | ||
COPY router router | ||
COPY Cargo.toml ./ | ||
COPY Cargo.lock ./ | ||
|
||
RUN cargo chef prepare --recipe-path recipe.json | ||
|
||
FROM chef AS builder | ||
|
||
ARG GIT_SHA | ||
ARG DOCKER_LABEL | ||
|
||
# sccache specific variables | ||
ARG ACTIONS_CACHE_URL | ||
ARG ACTIONS_RUNTIME_TOKEN | ||
ARG SCCACHE_GHA_ENABLED | ||
|
||
COPY --from=planner /usr/src/recipe.json recipe.json | ||
|
||
RUN cargo chef cook --release --features python --no-default-features --recipe-path recipe.json && sccache -s | ||
|
||
COPY backends backends | ||
COPY core core | ||
COPY router router | ||
COPY Cargo.toml ./ | ||
COPY Cargo.lock ./ | ||
|
||
RUN PROTOC_ZIP=protoc-21.12-linux-x86_64.zip && \ | ||
curl -OL https://github.com/protocolbuffers/protobuf/releases/download/v21.12/$PROTOC_ZIP && \ | ||
unzip -o $PROTOC_ZIP -d /usr/local bin/protoc && \ | ||
unzip -o $PROTOC_ZIP -d /usr/local 'include/*' && \ | ||
rm -f $PROTOC_ZIP | ||
|
||
FROM builder as http-builder | ||
|
||
RUN cargo build --release --bin text-embeddings-router -F python -F http --no-default-features && sccache -s | ||
|
||
FROM builder as grpc-builder | ||
|
||
COPY proto proto | ||
|
||
RUN cargo build --release --bin text-embeddings-router -F grpc -F python --no-default-features && sccache -s | ||
|
||
FROM ubuntu:22.04 as base | ||
|
||
ENV HUGGINGFACE_HUB_CACHE=/data \ | ||
PORT=80 | ||
|
||
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \ | ||
build-essential \ | ||
ca-certificates \ | ||
ccache \ | ||
curl \ | ||
python3 \ | ||
python3-pip \ | ||
git && \ | ||
rm -rf /var/lib/apt/lists/* | ||
RUN ln -s /usr/bin/python3.10 /usr/bin/python | ||
|
||
WORKDIR /usr/src | ||
COPY backends backends | ||
COPY backends/python/server/text_embeddings_server/models/__init__.py backends/python/server/text_embeddings_server/models/__init__.py | ||
COPY backends/python/server/pyproject.toml backends/python/server/pyproject.toml | ||
COPY backends/python/server/requirements.txt backends/python/server/requirements.txt | ||
RUN cd backends/python/server && \ | ||
make install | ||
RUN python -m pip install torch==2.2.0 torchvision==0.17.0 torchaudio==2.2.0 --index-url https://download.pytorch.org/whl/cpu | ||
RUN python -m pip install intel-extension-for-pytorch | ||
|
||
FROM base as grpc | ||
|
||
COPY --from=grpc-builder /usr/src/target/release/text-embeddings-router /usr/local/bin/text-embeddings-router | ||
|
||
ENTRYPOINT ["text-embeddings-router"] | ||
CMD ["--json-output"] | ||
|
||
FROM base | ||
|
||
COPY --from=http-builder /usr/src/target/release/text-embeddings-router /usr/local/bin/text-embeddings-router | ||
|
||
ENTRYPOINT ["text-embeddings-router"] | ||
CMD ["--json-output"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
backends/python/server/text_embeddings_server/utils/device.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import os | ||
from loguru import logger | ||
import importlib | ||
from packaging import version | ||
import torch | ||
|
||
def is_ipex_available(): | ||
def get_major_and_minor_from_version(full_version): | ||
return str(version.parse(full_version).major) + "." + str(version.parse(full_version).minor) | ||
|
||
_torch_version = importlib.metadata.version("torch") | ||
if importlib.util.find_spec("intel_extension_for_pytorch") is None: | ||
return False | ||
_ipex_version = "N/A" | ||
try: | ||
_ipex_version = importlib.metadata.version("intel_extension_for_pytorch") | ||
except importlib.metadata.PackageNotFoundError: | ||
return False | ||
torch_major_and_minor = get_major_and_minor_from_version(_torch_version) | ||
ipex_major_and_minor = get_major_and_minor_from_version(_ipex_version) | ||
if torch_major_and_minor != ipex_major_and_minor: | ||
logger.warning( | ||
f"Intel Extension for PyTorch {ipex_major_and_minor} needs to work with PyTorch {ipex_major_and_minor}.*," | ||
f" but PyTorch {_torch_version} is found. Please switch to the matching version and run again." | ||
) | ||
return False | ||
return True | ||
|
||
def use_ipex() : | ||
value = os.environ.get("USE_IPEX", "True") | ||
if value in ["True", "true", "1"] and is_ipex_available(): | ||
return True | ||
else: | ||
return False | ||
|
||
def get_device() : | ||
if torch.cuda.is_available(): | ||
device = torch.device("cuda") | ||
elif is_ipex_available(): | ||
if hasattr(torch, "xpu") and torch.xpu.is_available(): | ||
device = torch.device("xpu") | ||
else: | ||
device = torch.device("cpu") | ||
else: | ||
device = torch.device("cpu") | ||
return device | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters