Skip to content

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

[PT FE] Support complex aten::abs and aten::exp (#29351)

Maxim Vafin16da92bd838c
diff --git a/src/bindings/python/src/openvino/frontend/pytorch/utils.py b/src/bindings/python/src/openvino/frontend/pytorch/utils.py
index 6c1ffa90137..2cc5cdc16c1 100644
--- a/src/bindings/python/src/openvino/frontend/pytorch/utils.py
+++ b/src/bindings/python/src/openvino/frontend/pytorch/utils.py
@@ -47,6 +47,8 @@ def get_type_from_py_type(value):
return OVType.boolean
if isinstance(value, int):
return OVType.i64
+ if isinstance(value, complex):
+ return OVType.f32
return OVType.dynamic
@@ -92,7 +94,10 @@ def torch_tensor_to_ov_const(torch_t: torch.Tensor, shared_memory=True):
def ivalue_to_constant(ivalue, shared_memory=True):
ov_type = get_type_from_py_type(ivalue)
if ov_type.is_static():
- return op.Constant(ov_type, Shape([]), [ivalue]).outputs()
+ if isinstance(ivalue, complex):
+ return op.Constant(ov_type, Shape([2]), [ivalue.real, ivalue.imag]).outputs()
+ else:
+ return op.Constant(ov_type, Shape([]), [ivalue]).outputs()
if isinstance(ivalue, (list, tuple)):
assert len(ivalue) > 0, "Can't deduce type for empty list"
diff --git a/src/frontends/common/include/openvino/frontend/complex_type_mark.hpp b/src/frontends/common/include/openvino/frontend/complex_type_mark.hpp
index c8f7d9f97fe..66478d4328b 100644
--- a/src/frontends/common/include/openvino/frontend/complex_type_mark.hpp
+++ b/src/frontends/common/include/openvino/frontend/complex_type_mark.hpp
@@ -98,6 +98,12 @@ public:
const ov::Output<ov::Node>& input,
const ov::Output<ov::Node>& like);
+ // Compute abs of operand that can be of complex type
+ static ov::Output<ov::Node> abs(const NodeContext& context, const ov::Output<ov::Node>& data);
+
+ // Compute exp of operand that can be of complex type
+ static ov::Output<ov::Node> exp(const NodeContext& context, const ov::Output<ov::Node>& data);
+
private:
ov::element::Type m_complex_part_type;
diff --git a/src/frontends/common/src/complex_type_mark.cpp b/src/frontends/common/src/complex_type_mark.cpp
index 5f411c21653..58d6e3b484a 100644
--- a/src/frontends/common/src/complex_type_mark.cpp
+++ b/src/frontends/common/src/complex_type_mark.cpp
@@ -4,19 +4,25 @@
#include "openvino/frontend/complex_type_mark.hpp"
+#include "openvino/op/abs.hpp"
#include "openvino/op/add.hpp"
#include "openvino/op/broadcast.hpp"
#include "openvino/op/concat.hpp"
#include "openvino/op/constant.hpp"
#include "openvino/op/convert.hpp"
#include "openvino/op/convert_like.hpp"
+#include "openvino/op/cos.hpp"
#include "openvino/op/divide.hpp"
+#include "openvino/op/exp.hpp"
#include "openvino/op/gather.hpp"
#include "openvino/op/maximum.hpp"
#include "openvino/op/multiply.hpp"
#include "openvino/op/negative.hpp"
#include "openvino/op/power.hpp"
+#include "openvino/op/reduce_sum.hpp"
#include "openvino/op/shape_of.hpp"
+#include "openvino/op/sin.hpp"
+#include "openvino/op/sqrt.hpp"
#include "openvino/op/subtract.hpp"
#include "openvino/op/unsqueeze.hpp"
@@ -251,7 +257,6 @@ ov::Output<ov::Node> ComplexTypeMark::inv(const NodeContext& context, const ov::
auto data_complex = as_type_ptr<ComplexTypeMark>(data.get_node_shared_ptr());
if (data_complex) {
- auto real_imag = data_complex->get_data();
auto real = data_complex->get_real();
auto imag = data_complex->get_imag();
@@ -315,3 +320,48 @@ ov::Output<ov::Node> ComplexTypeMark::convert_like(const NodeContext& context,
return context.mark_node(make_shared<v1::ConvertLike>(input, like_data));
}
+
+ov::Output<ov::Node> ComplexTypeMark::abs(const NodeContext& context, const ov::Output<ov::Node>& data) {
+ auto data_complex = as_type_ptr<ComplexTypeMark>(data.get_node_shared_ptr());
+
+ if (data_complex) {
+ // abs of complex number sqrt(real^2 + imag^2)
+ auto data = data_complex->get_data();
+
+ // compute element-wise square for complex representation
+ auto const_two = context.mark_node(make_shared<v0::Constant>(element::i32, Shape{}, 2));
+ const_two = context.mark_node(make_shared<v1::ConvertLike>(const_two, data));
+ auto squared_data = context.mark_node(make_shared<v1::Power>(data, const_two));
+
+ // compute sum of squared real and imaginary parts
+ auto const_minus_one = context.mark_node(make_shared<v0::Constant>(element::i32, Shape{}, -1));
+ auto complex_abs = context.mark_node(make_shared<v1::ReduceSum>(squared_data, const_minus_one, false));
+
+ return context.mark_node(make_shared<v0::Sqrt>(complex_abs));
+ }
+
+ return context.mark_node(make_shared<v0::Abs>(data));
+}
+
+ov::Output<ov::Node> ComplexTypeMark::exp(const NodeContext& context, const ov::Output<ov::Node>& data) {
+ auto data_complex = as_type_ptr<ComplexTypeMark>(data.get_node_shared_ptr());
+
+ if (data_complex) {
+ auto real = data_complex->get_real();
+ auto imag = data_complex->get_imag();
+
+ // exp of complex number e^real * cos(imag) + i e^real * sin(imag)
+ auto real_exp = context.mark_node(make_shared<v0::Exp>(real));
+
+ auto imag_cos = context.mark_node(make_shared<v0::Cos>(imag));
+ auto imag_sin = context.mark_node(make_shared<v0::Sin>(imag));
+
+ auto res_real = context.mark_node(make_shared<v1::Multiply>(real_exp, imag_cos));
+ auto res_imag = context.mark_node(make_shared<v1::Multiply>(real_exp, imag_sin));
+
+ return context.mark_node(make_shared<ComplexTypeMark>(res_real, res_imag));
+ ;
+ }
+
+ return context.mark_node(make_shared<v0::Exp>(data));
+}
diff --git a/src/frontends/pytorch/src/op/abs.cpp b/src/frontends/pytorch/src/op/abs.cpp
new file mode 100644
index 00000000000..09684cd9129
--- /dev/null
+++ b/src/frontends/pytorch/src/op/abs.cpp
@@ -0,0 +1,33 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "openvino/frontend/complex_type_mark.hpp"
+#include "utils.hpp"
+
+namespace ov {
+namespace frontend {
+namespace pytorch {
+namespace op {
+
+using namespace ov::op;
+using namespace std;
+
+OutputVector translate_abs(const NodeContext& context) {
+ num_inputs_check(context, 1, 2, true);
+
+ auto res = ComplexTypeMark::abs(context, context.get_input(0));
+ auto out_type = context.get_output_type(0);
+ if (out_type.is<element::Type>()) {
+ auto dtype = out_type.as<element::Type>();
+ if (dtype.is_static() && dtype != res.get_element_type()) {
+ res = context.mark_node(std::make_shared<ov::op::v0::Convert>(res, dtype));
+ }
+ }
+ return {res};
+};
+
+} // namespace op
+} // namespace pytorch
+} // namespace frontend
+} // namespace ov
diff --git a/src/frontends/pytorch/src/op/exp.cpp b/src/frontends/pytorch/src/op/exp.cpp
new file mode 100644
index 00000000000..9036be2eebb
--- /dev/null
+++ b/src/frontends/pytorch/src/op/exp.cpp
@@ -0,0 +1,34 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "openvino/op/exp.hpp"
+
+#include "openvino/frontend/complex_type_mark.hpp"
+#include "openvino/op/constant.hpp"
+#include "utils.hpp"
+
+namespace ov {
+namespace frontend {
+namespace pytorch {
+namespace op {
+
+using namespace ov::op;
+
+OutputVector translate_exp(const NodeContext& context) {
+ num_inputs_check(context, 1, 2, true);
+
+ auto x = context.get_input(0);
+ // This const only needed for type alignment
+ auto dummy_const = context.mark_node(v0::Constant::create(element::f32, Shape({}), {0.5}))->output(0);
+ align_eltwise_input_types(context, x, dummy_const, false, true);
+
+ auto exp = ComplexTypeMark::exp(context, x);
+
+ return {exp};
+}
+
+} // namespace op
+} // namespace pytorch
+} // namespace frontend
+} // namespace ov
\ No newline at end of file
diff --git a/src/frontends/pytorch/src/op_table.cpp b/src/frontends/pytorch/src/op_table.cpp
index 15f2b8bd47d..018812354a2 100644
--- a/src/frontends/pytorch/src/op_table.cpp
+++ b/src/frontends/pytorch/src/op_table.cpp
@@ -19,6 +19,7 @@ namespace op {
using namespace ov::frontend;
// TorchScript translations
+OP_CONVERTER(translate_abs);
OP_CONVERTER(translate_adaptive_avg_pool3d);
OP_CONVERTER(translate_adaptive_avg_pool2d);
OP_CONVERTER(translate_adaptive_avg_pool1d);
@@ -86,6 +87,7 @@ OP_CONVERTER(translate_erf);
OP_CONVERTER(translate_erfc);
OP_CONVERTER(translate_expand);
OP_CONVERTER(translate_expand_as);
+OP_CONVERTER(translate_exp);
OP_CONVERTER(translate_expm1);
OP_CONVERTER(translate_eye);
OP_CONVERTER(translate_fake_quantize_per_channel_affine);
@@ -364,8 +366,8 @@ const std::unordered_map<std::string, CreatorFunction> get_supported_ops_ts() {
{"aten::_upsample_bicubic2d_aa", op::translate_upsample_bicubic2d_aa},
{"aten::_upsample_bilinear2d_aa", op::translate_upsample_bilinear2d_aa},
{"aten::_weight_norm", op::translate_weight_norm},
- {"aten::abs", op::optional_out<op::translate_1to1_match_1_inputs<opset10::Abs>, 1>},
- {"aten::abs_", op::inplace_op<op::translate_1to1_match_1_inputs<opset10::Abs>>},
+ {"aten::abs", op::optional_out<op::translate_abs, 1>},
+ {"aten::abs_", op::inplace_op<op::translate_abs>},
{"aten::acos", op::optional_out<op::translate_1to1_match_1_inputs_with_fp32_type_alignment<opset10::Acos>, 1>},
{"aten::acos_", op::inplace_op<op::translate_1to1_match_1_inputs<opset10::Acos>>},
{"aten::acosh",
@@ -473,8 +475,8 @@ const std::unordered_map<std::string, CreatorFunction> get_supported_ops_ts() {
{"aten::equal", op::translate_1to1_match_2_inputs_align_types<opset10::Equal>},
{"aten::erf", op::translate_erf},
{"aten::erfc", op::translate_erfc},
- {"aten::exp", op::optional_out<op::translate_1to1_match_1_inputs_with_fp32_type_alignment<opset10::Exp>, 1>},
- {"aten::exp_", op::inplace_op<op::translate_1to1_match_1_inputs_with_fp32_type_alignment<opset10::Exp>>},
+ {"aten::exp", op::optional_out<op::translate_exp, 1>},
+ {"aten::exp_", op::inplace_op<op::translate_exp>},
{"aten::expand", op::translate_expand},
{"aten::expand_as", op::translate_expand_as},
{"aten::expm1", op::translate_expm1},
diff --git a/src/frontends/pytorch/src/utils.hpp b/src/frontends/pytorch/src/utils.hpp
index 165b4203306..76a97cdbcc3 100644
--- a/src/frontends/pytorch/src/utils.hpp
+++ b/src/frontends/pytorch/src/utils.hpp
@@ -162,7 +162,7 @@ OutputVector optional_out(const NodeContext& context) {
template <typename T>
OutputVector translate_1to1_match_1_inputs(const NodeContext& context) {
- FRONT_END_OP_CONVERSION_CHECK(!context.input_is_none(0), "Input should not be None.");
+ num_inputs_check(context, 1, context.get_input_size());
auto res = context.mark_node(std::make_shared<T>(context.get_input(0)));
auto out_type = context.get_output_type(0);
if (out_type.is<element::Type>()) {
@@ -176,7 +176,7 @@ OutputVector translate_1to1_match_1_inputs(const NodeContext& context) {
template <typename T>
OutputVector translate_1to1_match_1_inputs_with_fp32_type_alignment(const NodeContext& context) {
- FRONT_END_OP_CONVERSION_CHECK(!context.input_is_none(0), "Input should not be None.");
+ num_inputs_check(context, 1, context.get_input_size());
auto x = get_input_with_floating_type(context, 0);
return {context.mark_node(std::make_shared<T>(x))};
}
diff --git a/src/frontends/tensorflow_common/src/op/complex_abs.cpp b/src/frontends/tensorflow_common/src/op/complex_abs.cpp
index c7088ea6424..5232d54ba8f 100644
--- a/src/frontends/tensorflow_common/src/op/complex_abs.cpp
+++ b/src/frontends/tensorflow_common/src/op/complex_abs.cpp
@@ -25,29 +25,9 @@ OutputVector translate_complex_abs_op(const NodeContext& node) {
auto x = node.get_input(0);
auto tout = node.get_attribute<element::Type>("Tout", element::f32);
- // check that complex type mark is set to the input
- auto complex_type_mark = as_type_ptr<ComplexTypeMark>(x.get_node_shared_ptr());
- TENSORFLOW_OP_VALIDATION(node,
- complex_type_mark,
- "[TensorFlow Frontend] internal error: ComplexTypeMark is not set to input of " + op_type);
- // data is complex tensor representation in a form [N1, N2, ..., Nk, 2]
- // where slice [N1, N2, ..., Nk, 0] contains real part of the complex tensor and
- // slice [N1, N2, ..., Nk, 1] contains imaginary part of the complex tensor
- auto data = complex_type_mark->get_data();
-
- // compute element-wise square for complex representation
- auto const_two = create_same_type_const_scalar<int32_t>(data, 2);
- auto squared_data = make_shared<v1::Power>(data, const_two);
-
- // compute sum of squared real and imaginary parts
- auto const_minus_one = make_shared<v0::Constant>(element::i32, Shape{}, -1);
- auto complex_abs = make_shared<v1::ReduceSum>(squared_data, const_minus_one, false)->output(0);
-
- // compute ComplexAbs by root-squared operation
- auto const_half = create_same_type_const_scalar<float>(data, 0.5f);
- complex_abs = make_shared<v1::Power>(complex_abs, const_half);
-
- // aling output type required by tout attribute
+ auto complex_abs = ComplexTypeMark::abs(node, x);
+
+ // align output type required by tout attribute
complex_abs = make_shared<v0::Convert>(complex_abs, tout);
set_node_name(node.get_name(), complex_abs.get_node_shared_ptr());
diff --git a/tests/layer_tests/pytorch_tests/test_unary_ops.py b/tests/layer_tests/pytorch_tests/test_unary_ops.py
index 584a80fe4ce..03a6d534574 100644
--- a/tests/layer_tests/pytorch_tests/test_unary_ops.py
+++ b/tests/layer_tests/pytorch_tests/test_unary_ops.py
@@ -122,6 +122,19 @@ class prim_abs_net(torch.nn.Module):
return y, x1
+class unary_op_complex_net(torch.nn.Module):
+ def __init__(self, op, dtype):
+ super().__init__()
+ self.dtype = dtype
+ self.op = op
+
+ def forward(self, x):
+ x1 = x.to(self.dtype) * 1j
+ y = self.op(x1)
+ y = torch.view_as_real(y.to(torch.complex64))
+ return y, torch.view_as_real(x1)
+
+
class TestUnaryOp(PytorchLayerTest):
def _prepare_input(self):
# random number in range [1, 11)
@@ -286,3 +299,16 @@ class TestUnaryOp(PytorchLayerTest):
self.dtype = dtype
self._test(prim_abs_net(dtype), None, "prim::abs",
ie_device, precision, ir_version)
+
+ @pytest.mark.nightly
+ @pytest.mark.precommit
+ @pytest.mark.parametrize("dtype", [torch.float32, torch.float64, torch.int8, torch.uint8, torch.int32, torch.int64])
+ @pytest.mark.parametrize("op_type",
+ [
+ "aten::abs",
+ "aten::exp",
+ ])
+ def test_complex_unary_op(self, op_type, dtype, ie_device, precision, ir_version):
+ self.dtype = dtype
+ self._test(unary_op_complex_net(OPS[op_type], dtype), None, op_type,
+ ie_device, precision, ir_version)