Skip to content

Commit

Permalink
Remove the need to enforce transformers library for OVModelForFeature…
Browse files Browse the repository at this point in the history
…Extraction export step (#895)

* remove specific export for feature extraction model making sure the library is transformers

* style
  • Loading branch information
echarlaix authored Sep 12, 2024
1 parent ba74307 commit 9921469
Showing 1 changed file with 1 addition and 67 deletions.
68 changes: 1 addition & 67 deletions optimum/intel/openvino/modeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
import logging
import os
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Dict, Optional, Union
from typing import Optional, Union

import numpy as np
import openvino
Expand Down Expand Up @@ -53,9 +52,7 @@

from optimum.exporters import TasksManager

from ...exporters.openvino import main_export
from ..utils.import_utils import is_timm_available, is_timm_version
from .configuration import OVConfig, OVWeightQuantizationConfig
from .modeling_base import OVBaseModel
from .utils import _is_timm_ov_dir

Expand Down Expand Up @@ -423,69 +420,6 @@ def forward(
)
return BaseModelOutput(last_hidden_state=last_hidden_state)

@classmethod
def _from_transformers(
cls,
model_id: str,
config: PretrainedConfig,
token: Optional[Union[bool, str]] = None,
revision: Optional[str] = None,
force_download: bool = False,
cache_dir: str = HUGGINGFACE_HUB_CACHE,
subfolder: str = "",
local_files_only: bool = False,
task: Optional[str] = None,
trust_remote_code: bool = False,
load_in_8bit: Optional[bool] = None,
quantization_config: Union[OVWeightQuantizationConfig, Dict] = None,
**kwargs,
):
compile_only = kwargs.get("compile_only", False)
if compile_only:
logger.warning(
"`compile_only` mode will be disabled because it does not support model export."
"Please provide openvino model obtained using optimum-cli or saved on disk using `save_pretrained`"
)
compile_only = False

save_dir = TemporaryDirectory()
save_dir_path = Path(save_dir.name)
# This attribute is needed to keep one reference on the temporary directory, since garbage collecting
# would end-up removing the directory containing the underlying OpenVINO model
cls._model_save_dir_tempdirectory_instance = save_dir

# If load_in_8bit and quantization_config not specified then ov_config is set to None and will be set by default in convert depending on the model size
if load_in_8bit is None and not quantization_config:
ov_config = None
else:
ov_config = OVConfig(dtype="fp32")

# OVModelForFeatureExtraction works with Transformers type of models, thus even sentence-transformers models are loaded as such.
main_export(
model_name_or_path=model_id,
output=save_dir_path,
task=task or cls.export_feature,
subfolder=subfolder,
revision=revision,
cache_dir=cache_dir,
token=token,
local_files_only=local_files_only,
force_download=force_download,
trust_remote_code=trust_remote_code,
ov_config=ov_config,
library_name="transformers",
)

config.save_pretrained(save_dir_path)
return cls._from_pretrained(
model_id=save_dir_path,
config=config,
load_in_8bit=load_in_8bit,
quantization_config=quantization_config,
compile_only=compile_only,
**kwargs,
)


MASKED_LM_EXAMPLE = r"""
Example of masked language modeling using `transformers.pipelines`:
Expand Down

0 comments on commit 9921469

Please sign in to comment.