Skip to content

Commit

Permalink
Add transformers v4.45 support
Browse files Browse the repository at this point in the history
  • Loading branch information
echarlaix committed Sep 16, 2024
1 parent e70ff84 commit a042720
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 50 deletions.
13 changes: 13 additions & 0 deletions optimum/exporters/openvino/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
_torch_version,
_transformers_version,
compare_versions,
is_transformers_version,
)
from optimum.utils import DEFAULT_DUMMY_SHAPES, is_diffusers_available
from optimum.utils.save_utils import maybe_save_preprocessors
Expand Down Expand Up @@ -615,6 +616,18 @@ def export_from_model(
logging.disable(logging.NOTSET)

if library_name != "diffusers":
if is_transformers_version(">=", "4.44.99"):
misplaced_generation_parameters = model.config._get_non_default_generation_parameters()
if model.can_generate() and len(misplaced_generation_parameters) > 0:
logger.warning(
"Moving the following attributes in the config to the generation config: "
f"{misplaced_generation_parameters}. You are seeing this warning because you've set "
"generation parameters in the model config, as opposed to in the generation config.",
)
for param_name, param_value in misplaced_generation_parameters.items():
setattr(model.generation_config, param_name, param_value)
setattr(model.config, param_name, None)

# Saving the model config and preprocessor as this is needed sometimes.
model.config.save_pretrained(output)
generation_config = getattr(model, "generation_config", None)
Expand Down
29 changes: 14 additions & 15 deletions optimum/intel/openvino/modeling_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from optimum.modeling_base import FROM_PRETRAINED_START_DOCSTRING, OptimizedModel

from ...exporters.openvino import export, main_export
from ..utils.import_utils import is_nncf_available
from ..utils.import_utils import is_nncf_available, is_transformers_version
from ..utils.modeling_utils import _find_files_matching_pattern
from .configuration import OVConfig, OVDynamicQuantizationConfig, OVWeightQuantizationConfig
from .utils import (
Expand Down Expand Up @@ -125,11 +125,23 @@ def __init__(

self.output_names = output_names
self.output_dtypes = output_dtypes

self.model = model
self.request = None if not self._compile_only else self.model
if self.can_generate():
self.generation_config = kwargs.get("generation_config", GenerationConfig.from_model_config(config))

if is_transformers_version(">=", "4.44.99"):
misplaced_generation_parameters = self.config._get_non_default_generation_parameters()
if len(misplaced_generation_parameters) > 0:
logger.warning(
"Moving the following attributes in the config to the generation config: "
f"{misplaced_generation_parameters}. You are seeing this warning because you've set "
"generation parameters in the model config, as opposed to in the generation config.",
)
for param_name, param_value in misplaced_generation_parameters.items():
setattr(self.generation_config, param_name, param_value)
setattr(self.config, param_name, None)

else:
self.generation_config = None

Expand Down Expand Up @@ -351,19 +363,6 @@ def _from_pretrained(
model_save_dir=model_cache_path.parent,
)

try:
generation_config = GenerationConfig.from_pretrained(
model_id,
token=token,
revision=revision,
subfolder=subfolder,
force_download=force_download,
cache_dir=cache_dir,
)
kwargs["generation_config"] = generation_config
except Exception:
pass

return cls(
model,
config=config,
Expand Down
55 changes: 39 additions & 16 deletions optimum/intel/openvino/modeling_base_seq2seq.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from transformers.file_utils import add_start_docstrings

from ...exporters.openvino import main_export
from ..utils.import_utils import is_transformers_version
from .configuration import OVConfig, OVWeightQuantizationConfig
from .modeling_base import OVBaseModel
from .utils import (
Expand Down Expand Up @@ -78,10 +79,21 @@ def __init__(
self.encoder_model = encoder
self.decoder_model = decoder
self.decoder_with_past_model = decoder_with_past
if self.can_generate():
self.generation_config = kwargs.get("generation_config", GenerationConfig.from_model_config(config))
else:
self.generation_config = None

self.generation_config = kwargs.get("generation_config", GenerationConfig.from_model_config(config))

if is_transformers_version(">=", "4.44.99"):
misplaced_generation_parameters = self.config._get_non_default_generation_parameters()
if len(misplaced_generation_parameters) > 0:
logger.warning(
"Moving the following attributes in the config to the generation config: "
f"{misplaced_generation_parameters}. You are seeing this warning because you've set "
"generation parameters in the model config, as opposed to in the generation config.",
)
for param_name, param_value in misplaced_generation_parameters.items():
setattr(self.generation_config, param_name, param_value)
setattr(self.config, param_name, None)

self._openvino_config = None
if quantization_config:
self._openvino_config = OVConfig(quantization_config=quantization_config)
Expand Down Expand Up @@ -166,6 +178,9 @@ def _from_pretrained(
local_files_only(`bool`, *optional*, defaults to `False`):
Whether or not to only look at local files (i.e., do not try to download the model).
"""
generation_config = kwargs.pop("generation_config", None)
subfolder = kwargs.pop("subfolder", "")

default_encoder_file_name = ONNX_ENCODER_NAME if from_onnx else OV_ENCODER_NAME
default_decoder_file_name = ONNX_DECODER_NAME if from_onnx else OV_DECODER_NAME
default_decoder_with_past_file_name = ONNX_DECODER_WITH_PAST_NAME if from_onnx else OV_DECODER_WITH_PAST_NAME
Expand Down Expand Up @@ -229,6 +244,7 @@ def _from_pretrained(
cache_dir=cache_dir,
force_download=force_download,
local_files_only=local_files_only,
subfolder=subfolder,
)
file_names[name] = model_cache_path

Expand All @@ -252,18 +268,24 @@ def _from_pretrained(
kwargs.get("ov_config"),
model_save_dir,
)
try:
generation_config = GenerationConfig.from_pretrained(
model_id,
token=token,
revision=revision,
cache_dir=cache_dir,
force_download=force_download,
local_files_only=local_files_only,
)
kwargs["generation_config"] = generation_config
except Exception:
pass

if generation_config is None:
try:
generation_config = GenerationConfig.from_pretrained(
model_id,
cache_dir=cache_dir,
force_download=force_download,
local_files_only=local_files_only,
token=token,
revision=revision,
subfolder=subfolder,
)
if getattr(generation_config, "cache_implementation", None) is not None:
generation_config.cache_implementation = None
except OSError:
logger.info(
"Generation config file not found, using a generation config created from the model config."
)

return cls(
encoder=encoder,
Expand All @@ -272,6 +294,7 @@ def _from_pretrained(
config=config,
model_save_dir=model_save_dir,
quantization_config=quantization_config,
generation_config=generation_config,
**kwargs,
)

Expand Down
38 changes: 21 additions & 17 deletions optimum/intel/openvino/modeling_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,8 @@ def __init__(
self.num_pkv = 2
self.key_value_input_names = [key for key in self.input_names if "key_values" in key]
self.key_value_output_names = [key for key in self.output_names if "present" in key]
self._original_model = (
self.model.clone() if not compile_only else None
) # keep original model for serialization
# Keeping the original model for serialization
self._original_model = self.model.clone() if not compile_only else None
self._pkv_precision = Type.f32
self.next_beam_idx = None
self._past_length = 0
Expand Down Expand Up @@ -787,6 +786,7 @@ def _from_pretrained(
quantization_config: Optional[Union[OVWeightQuantizationConfig, Dict]] = None,
**kwargs,
):
generation_config = kwargs.pop("generation_config", None)
model_path = Path(model_id)
default_file_name = ONNX_WEIGHTS_NAME if from_onnx else OV_XML_FILE_NAME
file_name = file_name or default_file_name
Expand Down Expand Up @@ -827,20 +827,23 @@ def _from_pretrained(

enable_compilation = kwargs.pop("compile", True) and not quantization_config

try:
generation_config = GenerationConfig.from_pretrained(
model_id,
token=token,
revision=revision,
cache_dir=cache_dir,
force_download=force_download,
local_files_only=local_files_only,
)
if getattr(generation_config, "cache_implementation", None) is not None:
generation_config.cache_implementation = None
kwargs["generation_config"] = generation_config
except Exception:
pass
if generation_config is None:
try:
generation_config = GenerationConfig.from_pretrained(
model_id,
cache_dir=cache_dir,
force_download=force_download,
local_files_only=local_files_only,
token=token,
revision=revision,
subfolder=subfolder,
)
if getattr(generation_config, "cache_implementation", None) is not None:
generation_config.cache_implementation = None
except OSError:
logger.info(
"Generation config file not found, using a generation config created from the model config."
)

causal_model = init_cls(
model=model,
Expand All @@ -849,6 +852,7 @@ def _from_pretrained(
compile=enable_compilation,
compile_only=compile_only,
quantization_config=quantization_config,
generation_config=generation_config,
**kwargs,
)

Expand Down
6 changes: 4 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@

INSTALL_REQUIRE = [
"torch>=1.11",
"transformers>=4.36,<4.45",
"optimum~=1.22",
"transformers @ git+https://github.com/huggingface/transformers.git",
"optimum @ git+https://github.com/huggingface/optimum.git@trfs-4.45",
# "transformers>=4.36,<4.46",
# "optimum~=1.22",
"datasets>=1.4.0",
"sentencepiece",
"setuptools",
Expand Down

0 comments on commit a042720

Please sign in to comment.