Skip to content

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

[TF FE]: Support complex tensors for Equal operation (#29339)

Ning Li8d18cd68ecd4
diff --git a/src/frontends/common_translators/include/common_translators.hpp b/src/frontends/common_translators/include/common_translators.hpp
index 7f791974938..551a27406da 100644
--- a/src/frontends/common_translators/include/common_translators.hpp
+++ b/src/frontends/common_translators/include/common_translators.hpp
@@ -20,6 +20,8 @@ COMMON_OP_CONVERTER(translate_atan2);
COMMON_OP_CONVERTER(translate_angle);
COMMON_OP_CONVERTER(translate_erfc);
+COMMON_OP_CONVERTER(translate_equal);
+
OutputVector translate_atan2_util(const NodeContext& context, const Output<Node>& lhs, const Output<Node>& rhs);
OutputVector translate_erfc_util(const NodeContext& context, const Output<Node>& data);
diff --git a/src/frontends/common_translators/src/op/equal.cpp b/src/frontends/common_translators/src/op/equal.cpp
new file mode 100644
index 00000000000..836ef5b64ef
--- /dev/null
+++ b/src/frontends/common_translators/src/op/equal.cpp
@@ -0,0 +1,53 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "openvino/op/equal.hpp"
+
+#include "common_translators.hpp"
+#include "openvino/frontend/complex_type_mark.hpp"
+#include "openvino/frontend/exception.hpp"
+#include "openvino/op/constant.hpp"
+#include "openvino/op/reduce_logical_and.hpp"
+#include "utils.hpp"
+
+using namespace std;
+using namespace ov::op;
+
+namespace ov {
+namespace frontend {
+namespace common_translators {
+
+OutputVector translate_equal(const NodeContext& node) {
+ num_inputs_check(node, 2, 2, true);
+ auto lhs = node.get_input(0);
+ auto rhs = node.get_input(1);
+
+ auto lhs_complex = as_type_ptr<ComplexTypeMark>(lhs.get_node_shared_ptr());
+ auto rhs_complex = as_type_ptr<ComplexTypeMark>(rhs.get_node_shared_ptr());
+
+ auto op_type = node.get_op_type();
+ FRONT_END_OP_CONVERSION_CHECK(!(lhs_complex && !rhs_complex) && !(!lhs_complex && rhs_complex),
+ op_type + " operation expects both operands to be of the same type.");
+
+ // both operands are of complex type
+ if (lhs_complex && rhs_complex) {
+ auto lhs_data = lhs_complex->get_data();
+ auto rhs_data = rhs_complex->get_data();
+ auto equal = make_shared<v1::Equal>(lhs_data, rhs_data);
+
+ // reduce along the last dimension using ReduceAnd
+ auto reduce_axes = make_shared<v0::Constant>(element::i32, Shape{1}, std::vector<int32_t>{-1});
+ auto equal_reduced = make_shared<v1::ReduceLogicalAnd>(equal, reduce_axes, false);
+
+ return {equal_reduced};
+ }
+
+ // both operands are real
+ auto result = make_shared<v1::Equal>(lhs, rhs);
+ return {result};
+};
+
+} // namespace common_translators
+} // namespace frontend
+} // namespace ov
diff --git a/src/frontends/tensorflow/src/op_table.cpp b/src/frontends/tensorflow/src/op_table.cpp
index 56e0a7d7612..c1c430e92f0 100644
--- a/src/frontends/tensorflow/src/op_table.cpp
+++ b/src/frontends/tensorflow/src/op_table.cpp
@@ -177,7 +177,7 @@ const std::map<std::string, CreatorFunction> get_supported_ops() {
{"RightShift", CreatorFunction(translate_binary_op<v15::BitwiseRightShift>)},
{"LeftShift", CreatorFunction(translate_binary_op<v15::BitwiseLeftShift>)},
{"Div", CreatorFunction(translate_div_op)},
- {"Equal", CreatorFunction(translate_binary_op<v1::Equal>)},
+ {"Equal", CreatorFunction(translate_equal_op)},
{"FloorMod", CreatorFunction(translate_binary_op<v1::FloorMod>)},
{"Greater", CreatorFunction(translate_binary_op<v1::Greater>)},
{"GreaterEqual", CreatorFunction(translate_binary_op<v1::GreaterEqual>)},
diff --git a/src/frontends/tensorflow_common/include/common_op_table.hpp b/src/frontends/tensorflow_common/include/common_op_table.hpp
index 6fc06835318..8c4161f1d65 100644
--- a/src/frontends/tensorflow_common/include/common_op_table.hpp
+++ b/src/frontends/tensorflow_common/include/common_op_table.hpp
@@ -74,6 +74,7 @@ OP_CONVERTER(translate_mul_op);
OP_CONVERTER(translate_dynamic_partition_op);
OP_CONVERTER(translate_einsum_op);
OP_CONVERTER(translate_elu_op);
+OP_CONVERTER(translate_equal_op);
OP_CONVERTER(translate_erfc_op);
OP_CONVERTER(translate_expm1_op);
OP_CONVERTER(translate_expand_dims_op);
diff --git a/src/frontends/tensorflow_common/src/op/binary_op.cpp b/src/frontends/tensorflow_common/src/op/binary_op.cpp
index 1e8beb56e12..d9b8f689295 100644
--- a/src/frontends/tensorflow_common/src/op/binary_op.cpp
+++ b/src/frontends/tensorflow_common/src/op/binary_op.cpp
@@ -3,6 +3,7 @@
//
#include "common_op_table.hpp"
+#include "common_translators.hpp"
#include "helper_ops/complex_type_mark.hpp"
#include "openvino/op/add.hpp"
#include "openvino/op/bitwise_and.hpp"
@@ -127,6 +128,15 @@ OutputVector translate_sub_op(const NodeContext& node) {
return {result};
}
+OutputVector translate_equal_op(const NodeContext& node) {
+ default_op_checks(node, 2, {"Equal"}, true);
+
+ auto result = common_translators::translate_equal(node);
+
+ set_node_name(node.get_name(), result[0].get_node_shared_ptr());
+ return result;
+}
+
template OutputVector translate_binary_op<v1::Add>(const NodeContext& node);
template OutputVector translate_binary_op<v13::BitwiseAnd>(const NodeContext& node);
template OutputVector translate_binary_op<v13::BitwiseOr>(const NodeContext& node);
diff --git a/tests/layer_tests/tensorflow_tests/test_tf_Equal.py b/tests/layer_tests/tensorflow_tests/test_tf_Equal.py
index 4db05e00b58..b0f8400ea0c 100644
--- a/tests/layer_tests/tensorflow_tests/test_tf_Equal.py
+++ b/tests/layer_tests/tensorflow_tests/test_tf_Equal.py
@@ -259,3 +259,57 @@ class TestEqualStr(CommonTFLayerTest):
self._test(*self.create_equal_net(x_shape=x_shape, y_shape=y_shape),
ie_device, precision, ir_version, temp_dir=temp_dir,
use_legacy_frontend=use_legacy_frontend)
+
+
+class TestComplexEqual(CommonTFLayerTest):
+ def _prepare_input(self, inputs_info):
+ rng = np.random.default_rng()
+ assert 'param_real_1:0' in inputs_info
+ assert 'param_imag_1:0' in inputs_info
+ assert 'param_real_2:0' in inputs_info
+ assert 'param_imag_2:0' in inputs_info
+ param_real_shape_1 = inputs_info['param_real_1:0']
+ param_imag_shape_1 = inputs_info['param_imag_1:0']
+ param_real_shape_2 = inputs_info['param_real_2:0']
+ param_imag_shape_2 = inputs_info['param_imag_2:0']
+ inputs_data = {}
+ inputs_data['param_real_1:0'] = 4 * rng.random(param_real_shape_1).astype(np.float32) - 2
+ inputs_data['param_imag_1:0'] = 4 * rng.random(param_imag_shape_1).astype(np.float32) - 2
+ inputs_data['param_real_2:0'] = 4 * rng.random(param_real_shape_2).astype(np.float32) - 2
+ inputs_data['param_imag_2:0'] = 4 * rng.random(param_imag_shape_2).astype(np.float32) - 2
+ return inputs_data
+
+ def create_complex_equal_net(self, input_shape):
+ tf.compat.v1.reset_default_graph()
+ # Create the graph and model
+ with tf.compat.v1.Session() as sess:
+ param_real1 = tf.compat.v1.placeholder(np.float32, input_shape, 'param_real_1')
+ param_imag1 = tf.compat.v1.placeholder(np.float32, input_shape, 'param_imag_1')
+ param_real2 = tf.compat.v1.placeholder(np.float32, input_shape, 'param_real_2')
+ param_imag2 = tf.compat.v1.placeholder(np.float32, input_shape, 'param_imag_2')
+ complex1 = tf.raw_ops.Complex(real=param_real1, imag=param_imag1)
+ complex2 = tf.raw_ops.Complex(real=param_real2, imag=param_imag2)
+ tf.raw_ops.Equal(x=complex1, y=complex2, name="complex_equal")
+ tf.compat.v1.global_variables_initializer()
+ tf_net = sess.graph_def
+
+ return tf_net, None
+
+ test_data_basic = [
+ dict(input_shape=[]),
+ dict(input_shape=[2]),
+ dict(input_shape=[1, 3]),
+ dict(input_shape=[2, 3, 4]),
+ dict(input_shape=[3, 4, 5, 6]),
+ ]
+
+ @pytest.mark.parametrize("params", test_data_basic)
+ @pytest.mark.precommit
+ @pytest.mark.nightly
+ def test_complex_equal(self, params, ie_device, precision, ir_version, temp_dir,
+ use_legacy_frontend):
+ self._test(
+ *self.create_complex_equal_net(**params),
+ ie_device, precision, ir_version, temp_dir=temp_dir,
+ use_legacy_frontend=use_legacy_frontend
+ )