Skip to content

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

[PT FE] Return floating point from atan2 on integer inputs (#38752)

Muhammad Usman Mateenf466feeab627
diff --git a/src/frontends/pytorch/src/op/atan2.cpp b/src/frontends/pytorch/src/op/atan2.cpp
index caf1b3645e8..ba16aad34f7 100644
--- a/src/frontends/pytorch/src/op/atan2.cpp
+++ b/src/frontends/pytorch/src/op/atan2.cpp
@@ -4,6 +4,7 @@
#include "common_translators.hpp"
#include "openvino/frontend/pytorch/node_context.hpp"
+#include "openvino/op/convert.hpp"
#include "utils.hpp"
namespace ov::frontend::pytorch::op {
@@ -17,6 +18,17 @@ OutputVector translate_atan2(const NodeContext& context) {
Output<Node> rhs;
std::tie(lhs, rhs) = get_inputs_with_promoted_types(context, 0, 1);
+ // torch.atan2 returns the default floating point type for integer and boolean inputs
+ const auto dtype = lhs.get_element_type();
+ if (dtype.is_static() && !dtype.is_real()) {
+ auto float_type = element::f32;
+ const auto out_type = simplified_type_interpret(context.get_output_type(0));
+ if (out_type.is<element::Type>() && out_type.as<element::Type>().is_real()) {
+ float_type = out_type.as<element::Type>();
+ }
+ lhs = context.mark_node(std::make_shared<v0::Convert>(lhs, float_type));
+ rhs = context.mark_node(std::make_shared<v0::Convert>(rhs, float_type));
+ }
return common_translators::translate_atan2_util(context, lhs, rhs);
}
diff --git a/tests/layer_tests/pytorch_tests/test_atan2.py b/tests/layer_tests/pytorch_tests/test_atan2.py
index c0bd936ec31..19058749b1c 100644
--- a/tests/layer_tests/pytorch_tests/test_atan2.py
+++ b/tests/layer_tests/pytorch_tests/test_atan2.py
@@ -58,6 +58,9 @@ class TestAtan2Types(PytorchLayerTest):
@pytest.mark.parametrize(("lhs_type", "rhs_type"),
[[torch.int, torch.float32],
[torch.int, torch.float64],
+ [torch.int, torch.int],
+ [torch.int64, torch.int32],
+ [torch.bool, torch.bool],
[torch.float32, torch.float64],
[torch.int64, torch.float32]
])
@@ -76,6 +79,24 @@ class TestAtan2Types(PytorchLayerTest):
self._test(*self.create_model(lhs_type, rhs_type),
ie_device, precision, ir_version, freeze_model=False, trace_model=True, fx_kind="aten.arctan2")
+ @pytest.mark.nightly
+ @pytest.mark.precommit
+ def test_atan2_int_default_dtype(self, ie_device, precision, ir_version):
+ # integer inputs give torch.get_default_dtype(), not always float32
+ if self.use_torch_export() or self.use_torch_compile_backend():
+ pytest.skip("The FX decoder reports a dynamic output type for this node, so f32 is used")
+ self.lhs_type = torch.int32
+ self.lhs_shape = [2, 3]
+ self.rhs_type = torch.int32
+ self.rhs_shape = [2, 3]
+ default_dtype = torch.get_default_dtype()
+ torch.set_default_dtype(torch.float64)
+ try:
+ self._test(*self.create_model(torch.int32, torch.int32),
+ ie_device, precision, ir_version, freeze_model=False, trace_model=True, fx_kind="aten.arctan2")
+ finally:
+ torch.set_default_dtype(default_dtype)
+
class TestAtan2ZeroEdgeCases(PytorchLayerTest):
"""Test atan2 with zero in the inputs - regression test for atan2(0, 0)