Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[PT FE] Inherit signature from forward while patching (#27413)

Maxim Vafin5c834609fcf9
diff --git a/src/bindings/python/src/openvino/frontend/pytorch/patch_model.py b/src/bindings/python/src/openvino/frontend/pytorch/patch_model.py
index fb8f70e2a56..0d1c4803e31 100644
--- a/src/bindings/python/src/openvino/frontend/pytorch/patch_model.py
+++ b/src/bindings/python/src/openvino/frontend/pytorch/patch_model.py
@@ -4,6 +4,7 @@
# flake8: noqa
# mypy: ignore-errors
+import functools
import logging
import torch
from openvino.frontend.pytorch import ModuleExtension
@@ -70,6 +71,8 @@ def patch_model(model, module_extensions, orig_forward_name, use_meta=False):
Trampoline.stashed_kwargs = kwargs
return extension.convert(m, Trampoline.apply, *args, **kwargs)
+ # make signature of new_forward same as of forward
+ new_forward = functools.wraps(m.forward)(new_forward)
setattr(m, orig_forward_name, m.forward)
m.forward = new_forward
diff --git a/tests/layer_tests/py_frontend_tests/test_torch_frontend.py b/tests/layer_tests/py_frontend_tests/test_torch_frontend.py
index faee72bb5d9..b659c1735d8 100644
--- a/tests/layer_tests/py_frontend_tests/test_torch_frontend.py
+++ b/tests/layer_tests/py_frontend_tests/test_torch_frontend.py
@@ -687,6 +687,7 @@ def test_patched_16bit_model_converts():
from openvino.frontend.pytorch import patch_model
from openvino import convert_model, compile_model
import copy
+ import inspect
from transformers.pytorch_utils import Conv1D
class ModelWithLinear(torch.nn.Module):
@@ -716,6 +717,9 @@ def test_patched_16bit_model_converts():
model_fp16 = copy.deepcopy(model_ref).half()
patch_model.__make_16bit_traceable(model_fp16)
+ # verify torch.nn.Linear signature after patching
+ signature = inspect.signature(model_ref.branch1[0].forward).parameters
+ assert ["input"] == list(signature)
# the approach with patching only works for node with no grad
with torch.no_grad():
converted_model = convert_model(model_fp16, example_input=example)