Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ipex backend #3083

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install '.[train, onnx, openvino, dev]'
python -m pip install '.[train, onnx, openvino, ipex, dev]'

- name: Install model2vec
run: python -m pip install model2vec
Expand Down
19 changes: 19 additions & 0 deletions docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ We recommend **Python 3.9+**, **[PyTorch 1.11.0+](https://pytorch.org/get-starte
* **Default:** This allows for loading, saving, and inference (i.e., getting embeddings) of models.
* **ONNX:** This allows for loading, saving, inference, optimizing, and quantizing of models using the ONNX backend.
* **OpenVINO:** This allows for loading, saving, and inference of models using the OpenVINO backend.
* **IPEX:** This allows for loading, saving, and inference of models using the IPEX backend.
* **Default and Training**: Like **Default**, plus training.
* **Development**: All of the above plus some dependencies for developing Sentence Transformers, see [Editable Install](#editable-install).

Expand Down Expand Up @@ -37,6 +38,12 @@ Note that you can mix and match the various extras, e.g. ``pip install -U "sente

pip install -U "sentence-transformers[openvino]"

.. tab:: IPEX

::

pip install -U "sentence-transformers[ipex]"

.. tab:: Default and Training

::
Expand Down Expand Up @@ -87,6 +94,12 @@ Note that you can mix and match the various extras, e.g. ``pip install -U "sente

pip install -U "sentence-transformers[openvino]"

.. tab:: IPEX

::

pip install -U "sentence-transformers[ipex]"

.. tab:: Default and Training

::
Expand Down Expand Up @@ -139,6 +152,12 @@ You can install ``sentence-transformers`` directly from source to take advantage

pip install -U "sentence-transformers[openvino] @ git+https://github.com/UKPLab/sentence-transformers.git"

.. tab:: IPEX

::

pip install -U "sentence-transformers[ipex] @ git+https://github.com/UKPLab/sentence-transformers.git"

.. tab:: Default and Training

::
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ train = ["datasets", "accelerate>=0.20.3"]
onnx = ["optimum[onnxruntime]>=1.23.1"]
onnx-gpu = ["optimum[onnxruntime-gpu]>=1.23.1"]
openvino = ["optimum-intel[openvino]>=1.20.0"]
ipex = ["optimum-intel[ipex]>=1.21.0"]
dev = ["datasets", "accelerate>=0.20.3", "pre-commit", "pytest", "pytest-cov", "peft"]

[build-system]
Expand Down
8 changes: 4 additions & 4 deletions sentence_transformers/SentenceTransformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class SentenceTransformer(nn.Sequential, FitMixin, PeftAdapterMixin):
model_card_data (:class:`~sentence_transformers.model_card.SentenceTransformerModelCardData`, optional): A model
card data object that contains information about the model. This is used to generate a model card when saving
the model. If not set, a default model card data object is created.
backend (str): The backend to use for inference. Can be one of "torch" (default), "onnx", or "openvino".
backend (str): The backend to use for inference. Can be one of "torch" (default), "onnx", "openvino", or "ipex".
See https://sbert.net/docs/sentence_transformer/usage/efficiency.html for benchmarking information
on the different backends.

Expand Down Expand Up @@ -177,7 +177,7 @@ def __init__(
tokenizer_kwargs: dict[str, Any] | None = None,
config_kwargs: dict[str, Any] | None = None,
model_card_data: SentenceTransformerModelCardData | None = None,
backend: Literal["torch", "onnx", "openvino"] = "torch",
backend: Literal["torch", "onnx", "openvino", "ipex"] = "torch",
) -> None:
# Note: self._load_sbert_model can also update `self.prompts` and `self.default_prompt_name`
self.prompts = prompts or {}
Expand Down Expand Up @@ -382,8 +382,8 @@ def __init__(
# Pass the model to the model card data for later use in generating a model card upon saving this model
self.model_card_data.register_model(self)

def get_backend(self) -> Literal["torch", "onnx", "openvino"]:
"""Return the backend used for inference, which can be one of "torch", "onnx", or "openvino".
def get_backend(self) -> Literal["torch", "onnx", "openvino", "ipex"]:
"""Return the backend used for inference, which can be one of "torch", "onnx", "openvino" or "ipex".

Returns:
str: The backend used for inference.
Expand Down
26 changes: 24 additions & 2 deletions sentence_transformers/models/Transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Transformer(nn.Module):
tokenizer_name_or_path: Name or path of the tokenizer. When
None, then model_name_or_path is used
backend: Backend used for model inference. Can be `torch`, `onnx`,
or `openvino`. Default is `torch`.
`openvino`, or `ipex`. Default is `torch`.
"""

save_in_root: bool = True
Expand Down Expand Up @@ -187,8 +187,12 @@ def _load_model(
self._load_onnx_model(model_name_or_path, config, cache_dir, **model_args)
elif backend == "openvino":
self._load_openvino_model(model_name_or_path, config, cache_dir, **model_args)
elif backend == "ipex":
self._load_ipex_model(model_name_or_path, config, cache_dir, **model_args)
else:
raise ValueError(f"Unsupported backend '{backend}'. `backend` should be `torch`, `onnx`, or `openvino`.")
raise ValueError(
f"Unsupported backend '{backend}'. `backend` should be `torch`, `onnx`, `openvino`, or `ipex`."
)

def _load_peft_model(self, model_name_or_path: str, config: PeftConfig, cache_dir: str, **model_args) -> None:
from peft import PeftModel
Expand Down Expand Up @@ -254,6 +258,24 @@ def _load_openvino_model(
if export:
self._backend_warn_to_save(model_name_or_path, is_local, backend_name)

def _load_ipex_model(self, model_name_or_path, config, cache_dir, **model_args) -> None:
try:
from optimum.intel import IPEXModel
except ModuleNotFoundError:
raise Exception(
"Using the IPEX backend requires installing Optimum and IPEX. "
"You can install them with pip: `pip install optimum-intel[ipex]`."
)

self.auto_model: IPEXModel = IPEXModel.from_pretrained(
model_name_or_path,
config=config,
cache_dir=cache_dir,
**model_args,
)
# Wrap the save_pretrained method to save the model in the correct subfolder
self.auto_model._save_pretrained = _save_pretrained_wrapper(self.auto_model._save_pretrained, self.backend)

def _load_onnx_model(
self, model_name_or_path: str, config: PretrainedConfig, cache_dir: str, **model_args
) -> None:
Expand Down
10 changes: 6 additions & 4 deletions tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@
except ImportError:
pytest.skip("OpenVINO and ONNX backends are not available", allow_module_level=True)

try:
from optimum.intel import IPEXModel
except ImportError:
pytest.skip("IPEX backend is not available", allow_module_level=True)

from sentence_transformers import SentenceTransformer


## Testing exporting:
@pytest.mark.parametrize(
["backend", "expected_auto_model_class"],
[
("onnx", ORTModelForFeatureExtraction),
("openvino", OVModelForFeatureExtraction),
],
[("onnx", ORTModelForFeatureExtraction), ("openvino", OVModelForFeatureExtraction), ("ipex", IPEXModel)],
)
@pytest.mark.parametrize(
"model_kwargs", [{}, {"file_name": "wrong_file_name"}]
Expand Down
Loading