OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[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.pyindex 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 functoolsimport loggingimport torchfrom openvino.frontend.pytorch import ModuleExtension@@ -70,6 +71,8 @@ def patch_model(model, module_extensions, orig_forward_name, use_meta=False):Trampoline.stashed_kwargs = kwargsreturn 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_forwarddiff --git a/tests/layer_tests/py_frontend_tests/test_torch_frontend.py b/tests/layer_tests/py_frontend_tests/test_torch_frontend.pyindex 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_modelfrom openvino import convert_model, compile_modelimport copy+ import inspectfrom transformers.pytorch_utils import Conv1Dclass 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 gradwith torch.no_grad():converted_model = convert_model(model_fp16, example_input=example)