Skip to content

Commit

Permalink
Add new _export method
Browse files Browse the repository at this point in the history
  • Loading branch information
echarlaix committed Jan 9, 2024
1 parent c64025d commit 87f9a7c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 40 deletions.
26 changes: 11 additions & 15 deletions optimum/intel/openvino/modeling_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,16 @@
from huggingface_hub import hf_hub_download
from openvino import Core, convert_model
from openvino._offline_transformations import apply_moc_transformations, compress_model_transformation
from transformers import PretrainedConfig
from transformers import PretrainedConfig, GenerationConfig
from transformers.file_utils import add_start_docstrings

from optimum.exporters.onnx import OnnxConfig
from optimum.modeling_base import OptimizedModel

from ...exporters.openvino import export, main_export
from ..utils.import_utils import is_nncf_available, is_transformers_version
from ..utils.import_utils import is_nncf_available
from .utils import ONNX_WEIGHTS_NAME, OV_XML_FILE_NAME, _print_compiled_model_properties


if is_transformers_version("<", "4.25.0"):
from transformers.generation_utils import GenerationMixin
else:
from transformers.generation import GenerationMixin
from transformers.generation import GenerationMixin

core = Core()

Expand Down Expand Up @@ -92,12 +87,7 @@ def __init__(
if enable_compilation:
self.compile()

if is_transformers_version("<=", "4.25.1"):
self.generation_config = None
else:
from transformers import GenerationConfig

self.generation_config = GenerationConfig.from_model_config(config) if self.can_generate() else None
self.generation_config = GenerationConfig.from_model_config(config) if self.can_generate() else None

@staticmethod
def load_model(file_name: Union[str, Path], load_in_8bit: bool = False):
Expand Down Expand Up @@ -247,7 +237,7 @@ def _cached_file(
return model_cache_path

@classmethod
def _from_transformers(
def _export(
cls,
model_id: str,
config: PretrainedConfig,
Expand Down Expand Up @@ -304,6 +294,12 @@ def _from_transformers(
config.save_pretrained(save_dir_path)
return cls._from_pretrained(model_id=save_dir_path, config=config, load_in_8bit=False, **kwargs)

@classmethod
def _from_transformers(cls, *args, **kwargs):
# TODO : add warning when from_pretrained_method is set to cls._export instead of cls._from_transformers when export=True
# logger.warning("The method `_from_transformers` is deprecated, please use `_export` instead")
return cls._export(*args, **kwargs)

@classmethod
def _to_load(
cls,
Expand Down
11 changes: 2 additions & 9 deletions optimum/intel/openvino/modeling_base_seq2seq.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@
import openvino
from huggingface_hub import hf_hub_download
from openvino._offline_transformations import apply_moc_transformations, compress_model_transformation
from transformers import PretrainedConfig
from transformers import PretrainedConfig, GenerationConfig
from transformers.file_utils import add_start_docstrings

from ...exporters.openvino import main_export
from ..utils.import_utils import is_transformers_version
from .modeling_base import OVBaseModel
from .utils import (
ONNX_DECODER_NAME,
Expand Down Expand Up @@ -75,13 +74,7 @@ def __init__(
self.encoder_model = encoder
self.decoder_model = decoder
self.decoder_with_past_model = decoder_with_past

if is_transformers_version("<=", "4.25.1"):
self.generation_config = None
else:
from transformers import GenerationConfig

self.generation_config = GenerationConfig.from_model_config(config) if self.can_generate() else None
self.generation_config = GenerationConfig.from_model_config(config) if self.can_generate() else None

def _save_pretrained(self, save_directory: Union[str, Path]):
"""
Expand Down
8 changes: 1 addition & 7 deletions optimum/intel/openvino/modeling_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,10 @@
from optimum.utils import NormalizedConfigManager

from ...exporters.openvino import main_export
from ..utils.import_utils import is_transformers_version
from ..utils.modeling_utils import MULTI_QUERY_ATTN_MODELS
from .modeling import _TOKENIZER_FOR_DOC, INPUTS_DOCSTRING, MODEL_START_DOCSTRING, OVModel
from .utils import ONNX_WEIGHTS_NAME, OV_XML_FILE_NAME, STR_TO_OV_TYPE


if is_transformers_version("<", "4.25.0"):
from transformers.generation_utils import GenerationMixin
else:
from transformers.generation import GenerationMixin
from transformers.generation import GenerationMixin


logger = logging.getLogger(__name__)
Expand Down
7 changes: 1 addition & 6 deletions optimum/intel/openvino/modeling_seq2seq.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,11 @@
from transformers.modeling_outputs import BaseModelOutput, Seq2SeqLMOutput
from transformers.models.whisper.tokenization_whisper import TASK_IDS, TO_LANGUAGE_CODE

from ..utils.import_utils import is_transformers_version
from .modeling_base_seq2seq import OVBaseModelForSeq2SeqLM
from .utils import _print_compiled_model_properties
from transformers.generation import GenerationMixin


if is_transformers_version("<", "4.25.0"):
from transformers.generation_utils import GenerationMixin
else:
from transformers.generation import GenerationMixin

if TYPE_CHECKING:
from transformers import PretrainedConfig

Expand Down
6 changes: 3 additions & 3 deletions tests/openvino/test_modeling_basic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
The goal of the test in this file is to test that basic functionality of optimum[openvino] works:
- Load the model with `from_transformers=True`
- Load the model with `export=True`
- Do inference with appropriate pipeline
- Save the model to disk
Expand Down Expand Up @@ -58,7 +58,7 @@ def test_pipeline(self, model_id):
tokenizer = AutoTokenizer.from_pretrained(model_id)
model_class_str = MODEL_NAMES[model_id]
model_class = eval(model_class_str)
model = model_class.from_pretrained(model_id, from_transformers=True)
model = model_class.from_pretrained(model_id, export=True)
model.save_pretrained(f"{model_id}_ov")
model = model_class.from_pretrained(f"{model_id}_ov")

Expand All @@ -80,7 +80,7 @@ def test_openvino_methods(self):
"""
model_id = "hf-internal-testing/tiny-random-distilbert"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = OVModelForSequenceClassification.from_pretrained(model_id, from_transformers=True)
model = OVModelForSequenceClassification.from_pretrained(model_id, export=True)
model.reshape(1, 16)
model.half()
model.to("cpu")
Expand Down

0 comments on commit 87f9a7c

Please sign in to comment.