Skip to content

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

[PT FE] Add support for built-in neg in FX graph (#30277)

Maxim Vafin8fba2bb15166
diff --git a/src/frontends/pytorch/src/op_table.cpp b/src/frontends/pytorch/src/op_table.cpp
index 6fb3eca4e9b..88d11b44a26 100644
--- a/src/frontends/pytorch/src/op_table.cpp
+++ b/src/frontends/pytorch/src/op_table.cpp
@@ -805,6 +805,7 @@ const std::unordered_map<std::string, CreatorFunction> get_supported_ops_fx() {
{"<built-in function floordiv>", op::translate_floor_divide},
{"<built-in function getitem>", op::translate_getitem}, // TODO: Check if there is any other way to handle this
{"<built-in function mul>", op::translate_mul},
+ {"<built-in function neg>", op::translate_neg},
{"<built-in function sub>", op::translate_sub},
{"aten._adaptive_avg_pool1d.default", op::translate_adaptive_avg_pool1d},
{"aten._adaptive_avg_pool2d.default", op::translate_adaptive_avg_pool2d},
diff --git a/tests/layer_tests/pytorch_tests/pytorch_layer_test_class.py b/tests/layer_tests/pytorch_tests/pytorch_layer_test_class.py
index f3c7f8d8f6f..5874639b98f 100644
--- a/tests/layer_tests/pytorch_tests/pytorch_layer_test_class.py
+++ b/tests/layer_tests/pytorch_tests/pytorch_layer_test_class.py
@@ -123,7 +123,9 @@ class PytorchLayerTest:
from openvino import convert_model
from torch.export import export
- em = export(model, tuple(torch_inputs))
+ dynamic_shapes = kwargs.get('dynamic_shapes_for_export', {})
+
+ em = export(model, tuple(torch_inputs), dynamic_shapes=dynamic_shapes)
converted_model = convert_model(
em, example_input=torch_inputs, verbose=True)
diff --git a/tests/layer_tests/pytorch_tests/test_neg.py b/tests/layer_tests/pytorch_tests/test_neg.py
new file mode 100644
index 00000000000..14bb8d2f60a
--- /dev/null
+++ b/tests/layer_tests/pytorch_tests/test_neg.py
@@ -0,0 +1,28 @@
+# Copyright (C) 2018-2025 Intel Corporation
+# SPDX-License-Identifier: Apache-2.0
+
+import pytest
+import torch
+
+from pytorch_layer_test_class import PytorchLayerTest
+
+
+class neg_model(torch.nn.Module):
+ def forward(self, x):
+ return x * (-x.shape[-1])
+
+
+class TestNeg(PytorchLayerTest):
+ def _prepare_input(self):
+ import numpy as np
+ return (np.random.randn(2, 4, 224, 224).astype(np.float32),)
+
+ @pytest.mark.nightly
+ @pytest.mark.precommit
+ @pytest.mark.precommit_torch_export
+ def test_neg(self, ie_device, precision, ir_version):
+ self._test(neg_model(), None, "aten::neg",
+ ie_device, precision, ir_version,
+ dynamic_shapes_for_export={
+ "x": {3: torch.export.Dim("width")}
+ })