-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add IPEX sentence transformers support (#1034)
* add import * add IPEX sentence transformers support * style * fix style * fix for python < 3.10 * Update tests/ipex/utils_tests.py * Update tests/ipex/test_modeling.py --------- Co-authored-by: Ilyas Moutawwakil <[email protected]>
- Loading branch information
1 parent
5c73548
commit 965540f
Showing
6 changed files
with
183 additions
and
19 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# Copyright 2024 The HuggingFace Team. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
from pathlib import Path | ||
from typing import Any, Dict, Optional | ||
|
||
import torch | ||
from sentence_transformers import SentenceTransformer | ||
from sentence_transformers.models import Transformer | ||
from sentence_transformers.models.Transformer import _save_pretrained_wrapper | ||
from sentence_transformers.util import import_from_string | ||
from transformers import MT5Config, T5Config | ||
from transformers.dynamic_module_utils import get_class_from_dynamic_module | ||
|
||
from .modeling_base import IPEXModel | ||
|
||
|
||
class IPEXTransformer(Transformer): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.backend = "ipex" | ||
|
||
def _load_model(self, model_name_or_path, config, cache_dir, backend, **model_args) -> None: | ||
self._load_ipex_model(model_name_or_path, config, cache_dir, **model_args) | ||
|
||
def _load_ipex_model(self, model_name_or_path, config, cache_dir, **model_args) -> None: | ||
if isinstance(config, T5Config) or isinstance(config, MT5Config): | ||
raise ValueError("T5 models are not yet supported by the IPEX backend.") | ||
|
||
export = model_args.pop("export", None) | ||
|
||
if export is None: | ||
export = not getattr(config, "torchscript", False) | ||
|
||
load_path = Path(model_name_or_path) | ||
is_local = load_path.exists() | ||
|
||
self.auto_model = IPEXModel.from_pretrained( | ||
model_name_or_path, | ||
config=config, | ||
cache_dir=cache_dir, | ||
export=export, | ||
**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, "ipex") | ||
|
||
# Warn the user to save the model if they haven't already | ||
if export: | ||
self._backend_warn_to_save(model_name_or_path, is_local, "IPEX") | ||
|
||
|
||
class IPEXSentenceTransformer(SentenceTransformer): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
|
||
self.backend = "ipex" | ||
|
||
def _load_module_class_from_ref( | ||
self, | ||
class_ref: str, | ||
model_name_or_path: str, | ||
trust_remote_code: bool, | ||
revision: Optional[str] = None, | ||
model_kwargs: Optional[Dict[str, Any]] = None, | ||
) -> torch.nn.Module: | ||
if class_ref.startswith("sentence_transformers."): | ||
if class_ref == "sentence_transformers.models.Transformer": | ||
class_ref = "optimum.intel.ipex.modeling_sentence_transformers.IPEXTransformer" | ||
return import_from_string(class_ref) | ||
|
||
if trust_remote_code: | ||
code_revision = model_kwargs.pop("code_revision", None) if model_kwargs else None | ||
try: | ||
return get_class_from_dynamic_module( | ||
class_ref, | ||
model_name_or_path, | ||
revision=revision, | ||
code_revision=code_revision, | ||
) | ||
except OSError: | ||
# Ignore the error if the file does not exist, and fall back to the default import | ||
pass | ||
|
||
return import_from_string(class_ref) |
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