diff --git a/src/frontends/tensorflow/docs/supported_ops.md b/src/frontends/tensorflow/docs/supported_ops.md index f4b06fafa06283..367cf6cb2408a2 100644 --- a/src/frontends/tensorflow/docs/supported_ops.md +++ b/src/frontends/tensorflow/docs/supported_ops.md @@ -26,7 +26,7 @@ A "supported operation" is one that TensorFlow Frontend can convert to the OpenV | All | YES | | | AllCandidateSampler | NO | | | AllToAll | NO | | -| Angle | NO | | +| Angle | YES | | | AnonymousHashTable | NO | | | AnonymousIterator | NO | | | AnonymousIteratorV2 | NO | | diff --git a/src/frontends/tensorflow/src/op_table.cpp b/src/frontends/tensorflow/src/op_table.cpp index fb1597c926e6c8..81d016ec67c68a 100644 --- a/src/frontends/tensorflow/src/op_table.cpp +++ b/src/frontends/tensorflow/src/op_table.cpp @@ -205,6 +205,7 @@ const std::map get_supported_ops() { // Separate translators: {"AddN", CreatorFunction(translate_add_n_op)}, {"AdjustContrastv2", CreatorFunction(translate_adjust_contrast_op)}, + {"Angle", CreatorFunction(translate_angle_op)}, {"ArgMax", CreatorFunction(translate_arg_max_op)}, {"ArgMin", CreatorFunction(translate_arg_min_op)}, {"Assert", CreatorFunction(translate_no_op)}, diff --git a/src/frontends/tensorflow_common/include/common_op_table.hpp b/src/frontends/tensorflow_common/include/common_op_table.hpp index 6d4e4a971c2f98..af59b862c89234 100644 --- a/src/frontends/tensorflow_common/include/common_op_table.hpp +++ b/src/frontends/tensorflow_common/include/common_op_table.hpp @@ -35,6 +35,7 @@ OP_CONVERTER(translate_addv2_op); OP_CONVERTER(translate_add_n_op); OP_CONVERTER(translate_approximate_equal_op); OP_CONVERTER(translate_adjust_contrast_op); +OP_CONVERTER(translate_angle_op); OP_CONVERTER(translate_arg_max_op); OP_CONVERTER(translate_arg_min_op); OP_CONVERTER(translate_atan2_op); diff --git a/src/frontends/tensorflow_common/src/op/angle.cpp b/src/frontends/tensorflow_common/src/op/angle.cpp new file mode 100644 index 00000000000000..a60363216b7d55 --- /dev/null +++ b/src/frontends/tensorflow_common/src/op/angle.cpp @@ -0,0 +1,90 @@ +// Copyright (C) 2018-2024 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 + +#include "common_op_table.hpp" +#include "helper_ops/complex_type_mark.hpp" +#include "openvino/op/add.hpp" +#include "openvino/op/atan.hpp" +#include "openvino/op/constant.hpp" +#include "openvino/op/convert.hpp" +#include "openvino/op/divide.hpp" +#include "openvino/op/equal.hpp" +#include "openvino/op/gather.hpp" +#include "openvino/op/greater.hpp" +#include "openvino/op/greater_eq.hpp" +#include "openvino/op/less.hpp" +#include "openvino/op/logical_and.hpp" +#include "openvino/op/multiply.hpp" +#include "openvino/op/select.hpp" +#include "openvino/op/subtract.hpp" + +using namespace std; +using namespace ov::op; + +namespace ov { +namespace frontend { +namespace tensorflow { +namespace op { + +OutputVector translate_angle_op(const NodeContext& node) { + default_op_checks(node, 1, {"Angle"}, true); + auto complex = node.get_input(0); + auto result_type = node.get_attribute("Tout"); + + auto complex_type_mark = as_type_ptr(complex.get_node_shared_ptr()); + + TENSORFLOW_OP_VALIDATION( + node, + complex_type_mark, + "[TensorFlow Frontend] inconsistent model: Angle operation expects complex type tensor on input"); + + complex = complex_type_mark->input_value(0); + auto real_index = make_shared(element::i32, Shape{}, 0); + auto imag_index = make_shared(element::i32, Shape{}, 1); + auto gather_axis = make_shared(element::i32, Shape{1}, -1); + + auto x = make_shared(complex, real_index, gather_axis)->output(0); + auto y = make_shared(complex, imag_index, gather_axis)->output(0); + + // handle the first condition : x>0 + auto div_y_x = make_shared(y, x); + auto atan = make_shared(div_y_x); + auto const_zero = create_same_type_const_scalar(x, 0); + auto result = atan->output(0); + + // handle the second condition : x<0 && y>=0 + auto const_pi = create_same_type_const_scalar(x, std::atan(1.0) * 4); + auto is_x_negative = make_shared(x, const_zero); + auto y_non_negative = make_shared(y, const_zero); + auto cond1 = make_shared(is_x_negative, y_non_negative); + auto atan_y_x_plus_pi = make_shared(atan, const_pi); + result = make_shared(cond1, atan_y_x_plus_pi, result); + + // handle the third condition : x<0 && y<0 + auto is_y_negative = make_shared(y, const_zero); + auto cond2 = make_shared(is_x_negative, is_y_negative); + auto atan_y_x_minus_pi = make_shared(atan, const_pi); + result = make_shared(cond2, atan_y_x_minus_pi, result); + + // handle the fourth condition : x=0 && y>0 + auto is_x_zero = make_shared(x, const_zero); + auto is_y_positive = make_shared(y, const_zero); + auto cond3 = make_shared(is_x_zero, is_y_positive); + auto const_two = create_same_type_const_scalar(x, 2); + auto pi_div_two = make_shared(const_pi, const_two); + result = make_shared(cond3, pi_div_two, result); + + // handle the fifth condition : x=0 && y<0 + auto cond4 = make_shared(is_x_zero, is_y_negative); + auto const_minus_two = create_same_type_const_scalar(x, -2); + auto pi_div_minus_two = make_shared(const_pi, const_minus_two); + result = make_shared(cond4, pi_div_two, result); + auto result_changed_type = make_shared(result, result_type)->output(0); + + set_node_name(node.get_name(), result_changed_type.get_node_shared_ptr()); + return {result_changed_type}; +} +} // namespace op +} // namespace tensorflow +} // namespace frontend +} // namespace ov diff --git a/tests/layer_tests/tensorflow_tests/test_tf_angle.py b/tests/layer_tests/tensorflow_tests/test_tf_angle.py new file mode 100644 index 00000000000000..7c7d35a3e99c9c --- /dev/null +++ b/tests/layer_tests/tensorflow_tests/test_tf_angle.py @@ -0,0 +1,47 @@ +# Copyright (C) 2018-2024 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 + +import numpy as np +import pytest +import tensorflow as tf +from common.tf_layer_test_class import CommonTFLayerTest + + +class TestAngle(CommonTFLayerTest): + def _prepare_input(self, inputs_info): + assert 'y:0' in inputs_info + assert 'x:0' in inputs_info + y_shape = inputs_info['y:0'] + x_shape = inputs_info['x:0'] + inputs_data = {} + inputs_data['y:0'] = np.random.rand(*y_shape).astype(self.input_type) - np.random.rand(*y_shape).astype(self.input_type) + inputs_data['x:0'] = np.random.rand(*x_shape).astype(self.input_type) - np.random.rand(*x_shape).astype(self.input_type) + return inputs_data + + def create_angle_net(self, input_shape, input_type): + self.input_type = input_type + tf.compat.v1.reset_default_graph() + # Create the graph and model + with tf.compat.v1.Session() as sess: + y = tf.compat.v1.placeholder(input_type, input_shape, 'y') + x = tf.compat.v1.placeholder(input_type, input_shape, 'x') + complex = tf.raw_ops.Complex(real=x, imag=y) + tf.raw_ops.Angle(input=complex) + tf.compat.v1.global_variables_initializer() + tf_net = sess.graph_def + + return tf_net, None + + test_data_basic = [ + dict(input_shape=[1, 2], input_type=np.float32), + dict(input_shape=[2, 3, 4], input_type=np.float32), + ] + + @pytest.mark.parametrize("params", test_data_basic) + @pytest.mark.precommit_tf_fe + @pytest.mark.nightly + def test_angle(self, params, ie_device, precision, ir_version, temp_dir, + use_legacy_frontend): + self._test(*self.create_angle_net(**params), + ie_device, precision, ir_version, temp_dir=temp_dir, + use_legacy_frontend=use_legacy_frontend)