Skip to content

Commit dbef32e

Browse files
rghvshrkazants
andauthored
[TF FE] Support Angle operation for TensorFlow models (openvinotoolkit#23028)
### Details: - *Support Angle operation for TensorFlow models* ### Tickets: - Closes openvinotoolkit#22083 --------- Co-authored-by: Roman Kazantsev <roman.kazantsev@intel.com>
1 parent aebf814 commit dbef32e

File tree

5 files changed

+140
-1
lines changed

5 files changed

+140
-1
lines changed

src/frontends/tensorflow/docs/supported_ops.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ A "supported operation" is one that TensorFlow Frontend can convert to the OpenV
2626
| All | YES | |
2727
| AllCandidateSampler | NO | |
2828
| AllToAll | NO | |
29-
| Angle | NO | |
29+
| Angle | YES | |
3030
| AnonymousHashTable | NO | |
3131
| AnonymousIterator | NO | |
3232
| AnonymousIteratorV2 | NO | |

src/frontends/tensorflow/src/op_table.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ const std::map<std::string, CreatorFunction> get_supported_ops() {
205205
// Separate translators:
206206
{"AddN", CreatorFunction(translate_add_n_op)},
207207
{"AdjustContrastv2", CreatorFunction(translate_adjust_contrast_op)},
208+
{"Angle", CreatorFunction(translate_angle_op)},
208209
{"ArgMax", CreatorFunction(translate_arg_max_op)},
209210
{"ArgMin", CreatorFunction(translate_arg_min_op)},
210211
{"Assert", CreatorFunction(translate_no_op)},

src/frontends/tensorflow_common/include/common_op_table.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ OP_CONVERTER(translate_addv2_op);
3535
OP_CONVERTER(translate_add_n_op);
3636
OP_CONVERTER(translate_approximate_equal_op);
3737
OP_CONVERTER(translate_adjust_contrast_op);
38+
OP_CONVERTER(translate_angle_op);
3839
OP_CONVERTER(translate_arg_max_op);
3940
OP_CONVERTER(translate_arg_min_op);
4041
OP_CONVERTER(translate_atan2_op);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Copyright (C) 2018-2024 Intel Corporation
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#include "common_op_table.hpp"
5+
#include "helper_ops/complex_type_mark.hpp"
6+
#include "openvino/op/add.hpp"
7+
#include "openvino/op/atan.hpp"
8+
#include "openvino/op/constant.hpp"
9+
#include "openvino/op/convert.hpp"
10+
#include "openvino/op/divide.hpp"
11+
#include "openvino/op/equal.hpp"
12+
#include "openvino/op/gather.hpp"
13+
#include "openvino/op/greater.hpp"
14+
#include "openvino/op/greater_eq.hpp"
15+
#include "openvino/op/less.hpp"
16+
#include "openvino/op/logical_and.hpp"
17+
#include "openvino/op/multiply.hpp"
18+
#include "openvino/op/select.hpp"
19+
#include "openvino/op/subtract.hpp"
20+
21+
using namespace std;
22+
using namespace ov::op;
23+
24+
namespace ov {
25+
namespace frontend {
26+
namespace tensorflow {
27+
namespace op {
28+
29+
OutputVector translate_angle_op(const NodeContext& node) {
30+
default_op_checks(node, 1, {"Angle"}, true);
31+
auto complex = node.get_input(0);
32+
auto result_type = node.get_attribute<ov::element::Type>("Tout");
33+
34+
auto complex_type_mark = as_type_ptr<ComplexTypeMark>(complex.get_node_shared_ptr());
35+
36+
TENSORFLOW_OP_VALIDATION(
37+
node,
38+
complex_type_mark,
39+
"[TensorFlow Frontend] inconsistent model: Angle operation expects complex type tensor on input");
40+
41+
complex = complex_type_mark->input_value(0);
42+
auto real_index = make_shared<v0::Constant>(element::i32, Shape{}, 0);
43+
auto imag_index = make_shared<v0::Constant>(element::i32, Shape{}, 1);
44+
auto gather_axis = make_shared<v0::Constant>(element::i32, Shape{1}, -1);
45+
46+
auto x = make_shared<v8::Gather>(complex, real_index, gather_axis)->output(0);
47+
auto y = make_shared<v8::Gather>(complex, imag_index, gather_axis)->output(0);
48+
49+
// handle the first condition : x>0
50+
auto div_y_x = make_shared<v1::Divide>(y, x);
51+
auto atan = make_shared<v0::Atan>(div_y_x);
52+
auto const_zero = create_same_type_const_scalar<int32_t>(x, 0);
53+
auto result = atan->output(0);
54+
55+
// handle the second condition : x<0 && y>=0
56+
auto const_pi = create_same_type_const_scalar<double>(x, std::atan(1.0) * 4);
57+
auto is_x_negative = make_shared<v1::Less>(x, const_zero);
58+
auto y_non_negative = make_shared<v1::GreaterEqual>(y, const_zero);
59+
auto cond1 = make_shared<v1::LogicalAnd>(is_x_negative, y_non_negative);
60+
auto atan_y_x_plus_pi = make_shared<v1::Add>(atan, const_pi);
61+
result = make_shared<v1::Select>(cond1, atan_y_x_plus_pi, result);
62+
63+
// handle the third condition : x<0 && y<0
64+
auto is_y_negative = make_shared<v1::Less>(y, const_zero);
65+
auto cond2 = make_shared<v1::LogicalAnd>(is_x_negative, is_y_negative);
66+
auto atan_y_x_minus_pi = make_shared<v1::Subtract>(atan, const_pi);
67+
result = make_shared<v1::Select>(cond2, atan_y_x_minus_pi, result);
68+
69+
// handle the fourth condition : x=0 && y>0
70+
auto is_x_zero = make_shared<v1::Equal>(x, const_zero);
71+
auto is_y_positive = make_shared<v1::Greater>(y, const_zero);
72+
auto cond3 = make_shared<v1::LogicalAnd>(is_x_zero, is_y_positive);
73+
auto const_two = create_same_type_const_scalar<int32_t>(x, 2);
74+
auto pi_div_two = make_shared<v1::Divide>(const_pi, const_two);
75+
result = make_shared<v1::Select>(cond3, pi_div_two, result);
76+
77+
// handle the fifth condition : x=0 && y<0
78+
auto cond4 = make_shared<v1::LogicalAnd>(is_x_zero, is_y_negative);
79+
auto const_minus_two = create_same_type_const_scalar<int32_t>(x, -2);
80+
auto pi_div_minus_two = make_shared<v1::Divide>(const_pi, const_minus_two);
81+
result = make_shared<v1::Select>(cond4, pi_div_two, result);
82+
auto result_changed_type = make_shared<v0::Convert>(result, result_type)->output(0);
83+
84+
set_node_name(node.get_name(), result_changed_type.get_node_shared_ptr());
85+
return {result_changed_type};
86+
}
87+
} // namespace op
88+
} // namespace tensorflow
89+
} // namespace frontend
90+
} // namespace ov
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright (C) 2018-2024 Intel Corporation
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
import numpy as np
5+
import pytest
6+
import tensorflow as tf
7+
from common.tf_layer_test_class import CommonTFLayerTest
8+
9+
10+
class TestAngle(CommonTFLayerTest):
11+
def _prepare_input(self, inputs_info):
12+
assert 'y:0' in inputs_info
13+
assert 'x:0' in inputs_info
14+
y_shape = inputs_info['y:0']
15+
x_shape = inputs_info['x:0']
16+
inputs_data = {}
17+
inputs_data['y:0'] = np.random.rand(*y_shape).astype(self.input_type) - np.random.rand(*y_shape).astype(self.input_type)
18+
inputs_data['x:0'] = np.random.rand(*x_shape).astype(self.input_type) - np.random.rand(*x_shape).astype(self.input_type)
19+
return inputs_data
20+
21+
def create_angle_net(self, input_shape, input_type):
22+
self.input_type = input_type
23+
tf.compat.v1.reset_default_graph()
24+
# Create the graph and model
25+
with tf.compat.v1.Session() as sess:
26+
y = tf.compat.v1.placeholder(input_type, input_shape, 'y')
27+
x = tf.compat.v1.placeholder(input_type, input_shape, 'x')
28+
complex = tf.raw_ops.Complex(real=x, imag=y)
29+
tf.raw_ops.Angle(input=complex)
30+
tf.compat.v1.global_variables_initializer()
31+
tf_net = sess.graph_def
32+
33+
return tf_net, None
34+
35+
test_data_basic = [
36+
dict(input_shape=[1, 2], input_type=np.float32),
37+
dict(input_shape=[2, 3, 4], input_type=np.float32),
38+
]
39+
40+
@pytest.mark.parametrize("params", test_data_basic)
41+
@pytest.mark.precommit_tf_fe
42+
@pytest.mark.nightly
43+
def test_angle(self, params, ie_device, precision, ir_version, temp_dir,
44+
use_legacy_frontend):
45+
self._test(*self.create_angle_net(**params),
46+
ie_device, precision, ir_version, temp_dir=temp_dir,
47+
use_legacy_frontend=use_legacy_frontend)

0 commit comments

Comments
 (0)