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

fix conversion for text embeddings for fp16 models #968

Merged
merged 4 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions optimum/exporters/openvino/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,10 @@ def ts_patched_forward(*args, **kwargs):
if patch_16bit_model:
from openvino.frontend.pytorch.patch_model import __make_16bit_traceable

# frontend may riases confusing warnings about modules already patched if model splitted on several parts
eaidova marked this conversation as resolved.
Show resolved Hide resolved
logging.disable(logging.WARNING)
__make_16bit_traceable(model)
logging.disable(logging.NOTSET)
eaidova marked this conversation as resolved.
Show resolved Hide resolved
eaidova marked this conversation as resolved.
Show resolved Hide resolved
check_dummy_inputs_are_allowed(model, dummy_inputs)
input_info = _get_input_info(model, config, dummy_inputs)
ov_model = convert_model(
Expand Down
6 changes: 6 additions & 0 deletions optimum/exporters/openvino/model_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
GptNeoxJapaneseModelPatcher,
GptNeoxModelPatcher,
IBertModelPatcher,
InputEmbeddingPatcher,
InternLM2Patcher,
InternLMModelPatcher,
InternVLChatImageEmbeddingModelPatcher,
Expand Down Expand Up @@ -1264,6 +1265,11 @@ def rename_ambiguous_inputs(self, inputs):
model_inputs["input"] = inputs["input_ids"]
return model_inputs

def patch_model_for_export(
self, model: Union["PreTrainedModel", "TFPreTrainedModel"], model_kwargs: Optional[Dict[str, Any]] = None
) -> "ModelPatcher":
return InputEmbeddingPatcher(self, model, model_kwargs)


class LlavaConfigBehavior(str, enum.Enum):
LANGUAGE = "language"
Expand Down
22 changes: 22 additions & 0 deletions optimum/exporters/openvino/model_patcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -2991,3 +2991,25 @@ def __init__(
def __exit__(self, exc_type, exc_value, traceback):
super().__exit__(exc_type, exc_value, traceback)
self._model.forward = self._model.__orig_forward


class InputEmbeddingPatcher(ModelPatcher):
eaidova marked this conversation as resolved.
Show resolved Hide resolved
def __init__(
self,
config: "OnnxConfig",
model: Union["PreTrainedModel", "TFPreTrainedModel"],
model_kwargs: Dict[str, Any],
):
# making 16bit tracable overrides embeedings input signature these changes required to prevent this issue
eaidova marked this conversation as resolved.
Show resolved Hide resolved
model.__orig_forward = model.forward

def forward(self, input):
return self.__orig_forward(input)

model.forward = types.MethodType(forward, model)

super().__init__(config, model, model_kwargs)

def __exit__(self, exc_type, exc_value, traceback):
super().__exit__(exc_type, exc_value, traceback)
self._model.forward = self._model.__orig_forward
Loading