Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TF FE] Support Atan2 operation for TensorFlow models #21076

Merged
merged 42 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
b3d0593
Create atan2.cpp
rghvsh Nov 14, 2023
edf1a45
Update common_op_table.hpp
rghvsh Nov 14, 2023
0c30dcd
Update op_table.cpp
rghvsh Nov 14, 2023
457d195
Create test_tf_Atan2.py
rghvsh Nov 14, 2023
0a3531b
Update src/frontends/tensorflow_common/src/op/atan2.cpp
rghvsh Nov 15, 2023
464eb34
Update src/frontends/tensorflow/src/op_table.cpp
rghvsh Nov 15, 2023
7810152
Update atan2.cpp
rghvsh Nov 16, 2023
d45b8e0
Merge branch 'master' into master
rkazants Nov 16, 2023
f1e3649
Update src/frontends/tensorflow_common/src/op/atan2.cpp
rghvsh Nov 17, 2023
f88f1ee
Update atan2.cpp
rghvsh Nov 17, 2023
121a223
Update atan2.cpp
rghvsh Nov 17, 2023
0924fd7
Update atan2.cpp
rghvsh Nov 27, 2023
8862194
Update atan2.cpp
rghvsh Nov 28, 2023
017ee7c
Update src/frontends/tensorflow_common/src/op/atan2.cpp
rkazants Nov 29, 2023
250994d
Update src/frontends/tensorflow_common/src/op/atan2.cpp
rkazants Nov 29, 2023
93b16d0
Update src/frontends/tensorflow_common/src/op/atan2.cpp
rkazants Nov 29, 2023
32117b7
Update src/frontends/tensorflow_common/src/op/atan2.cpp
rkazants Nov 29, 2023
b385484
Update src/frontends/tensorflow_common/src/op/atan2.cpp
rkazants Nov 29, 2023
1d3925a
Update src/frontends/tensorflow_common/src/op/atan2.cpp
rkazants Nov 29, 2023
b6a71c2
Merge branch 'master' into master
rghvsh Nov 29, 2023
d9f7f44
Update src/frontends/tensorflow_common/src/op/atan2.cpp
rkazants Nov 29, 2023
b80c5ca
Update src/frontends/tensorflow_common/src/op/atan2.cpp
rkazants Nov 29, 2023
2ac3d34
Update atan2.cpp
rghvsh Nov 29, 2023
3c94106
Update atan2.cpp
rghvsh Nov 29, 2023
556bd38
Update src/frontends/tensorflow_common/src/op/atan2.cpp
rkazants Nov 29, 2023
b03f5a8
Update src/frontends/tensorflow_common/src/op/atan2.cpp
rkazants Nov 29, 2023
4c74777
Update src/frontends/tensorflow_common/src/op/atan2.cpp
rkazants Nov 29, 2023
d5a0507
Update src/frontends/tensorflow_common/src/op/atan2.cpp
rkazants Nov 29, 2023
45d2625
Update src/frontends/tensorflow_common/src/op/atan2.cpp
rkazants Nov 29, 2023
33f89ef
Update src/frontends/tensorflow_common/src/op/atan2.cpp
rkazants Nov 29, 2023
2cd6de8
Update src/frontends/tensorflow_common/src/op/atan2.cpp
rkazants Nov 29, 2023
67e83b3
Update src/frontends/tensorflow_common/src/op/atan2.cpp
rkazants Nov 29, 2023
0e0637e
Update src/frontends/tensorflow_common/src/op/atan2.cpp
rkazants Nov 30, 2023
7983413
Update test_tf_Atan2.py
rghvsh Dec 2, 2023
fd3e1ea
Update tests/layer_tests/tensorflow_tests/test_tf_Atan2.py
rkazants Dec 2, 2023
fd4a312
Merge branch 'master' into master
rghvsh Dec 2, 2023
e6affbd
Update test_tf_Atan2.py
rghvsh Dec 2, 2023
53cf410
Update tests/layer_tests/tensorflow_tests/test_tf_Atan2.py
rkazants Dec 4, 2023
5defa1c
Update tests/layer_tests/tensorflow_tests/test_tf_Atan2.py
rkazants Dec 4, 2023
371cd63
Update tests/layer_tests/tensorflow_tests/test_tf_Atan2.py
rkazants Dec 4, 2023
f5f3757
Update test_tf_Atan2.py
rghvsh Dec 5, 2023
b99c5e5
Update src/frontends/tensorflow_common/src/op/atan2.cpp
rkazants Dec 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/frontends/tensorflow/src/op_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ const std::map<std::string, CreatorFunction> get_supported_ops() {
// note: BinaryOp translator declaration for each op must to be added in binary_op.cpp file
{"Add", CreatorFunction(translate_binary_op<opset8::Add>)},
{"AddV2", CreatorFunction(translate_binary_op<opset8::Add>)},
{"Atan2", CreatorFunction(translate_atan2_op)},
{"BitwiseAnd", CreatorFunction(translate_binary_op<opset13::BitwiseAnd>)},
{"BitwiseOr", CreatorFunction(translate_binary_op<opset13::BitwiseOr>)},
{"BitwiseXor", CreatorFunction(translate_binary_op<opset13::BitwiseXor>)},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ OP_CONVERTER(translate_add_n_op);
OP_CONVERTER(translate_adjust_contrast_op);
OP_CONVERTER(translate_arg_max_op);
OP_CONVERTER(translate_arg_min_op);
OP_CONVERTER(translate_atan2_op);
OP_CONVERTER(translate_avg_pool_op);
OP_CONVERTER(translate_batch_mat_mul_op);
OP_CONVERTER(translate_batch_mat_mul_with_type_op);
Expand Down Expand Up @@ -173,4 +174,4 @@ OP_CONVERTER_NAMED(translate_unique_op);
} // namespace op
} // namespace tensorflow
} // namespace frontend
} // namespace ov
} // namespace ov
72 changes: 72 additions & 0 deletions src/frontends/tensorflow_common/src/op/atan2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (C) 2018-2023 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#include "common_op_table.hpp"
#include "openvino/op/add.hpp"
#include "openvino/op/atan.hpp"
rkazants marked this conversation as resolved.
Show resolved Hide resolved
#include "openvino/op/constant.hpp"
#include "openvino/op/convert_like.hpp"
#include "openvino/op/divide.hpp"
#include "openvino/op/equal.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_atan2_op(const NodeContext& node) {
default_op_checks(node, 2, {"Atan2"});
rkazants marked this conversation as resolved.
Show resolved Hide resolved
auto y = node.get_input(0);
auto x = node.get_input(1);

// handle the first condition : x>0
auto div_y_x = make_shared<v1::Divide>(y, x);
auto atan = make_shared<v0::Atan>(div_y_x);
auto const_zero = create_same_type_const_scalar<int32_t>(x, 0);
auto result = atan->output(0);

// handle the second condition : x<0 && y>=0
auto const_pi = create_same_type_const_scalar<double>(x, std::atan(1.0) * 4);
auto is_x_negative = make_shared<v1::Less>(x, const_zero);
auto y_non_negative = make_shared<v1::GreaterEqual>(y, const_zero);
auto cond1 = make_shared<v1::LogicalAnd>(is_x_negative, y_non_negative);
auto atan_y_x_plus_pi = make_shared<v1::Add>(atan, const_pi);
result = make_shared<v1::Select>(cond1, atan_y_x_plus_pi, result);

// handle the third condition : x<0 && y<0
auto is_y_negative = make_shared<v1::Less>(y, const_zero);
auto cond2 = make_shared<v1::LogicalAnd>(is_x_negative, is_y_negative);
auto atan_y_x_minus_pi = make_shared<v1::Subtract>(atan, const_pi);
result = make_shared<v1::Select>(cond2, atan_y_x_minus_pi, result);

// handle the fourth condition : x=0 && y>0
auto is_x_zero = make_shared<v1::Equal>(x, const_zero);
auto is_y_positive = make_shared<v1::Greater>(y, const_zero);
auto cond3 = make_shared<v1::LogicalAnd>(is_x_zero, is_y_positive);
auto const_two = create_same_type_const_scalar<int32_t>(x, 2);
auto pi_div_two = make_shared<v1::Divide>(const_pi, const_two);
result = make_shared<v1::Select>(cond3, pi_div_two, result);

// handle the fifth condition : x=0 && y<0
auto cond4 = make_shared<v1::LogicalAnd>(is_x_zero, is_y_negative);
auto const_minus_two = create_same_type_const_scalar<int32_t>(x, -2);
auto pi_div_minus_two = make_shared<v1::Divide>(const_pi, const_minus_two);
result = make_shared<v1::Select>(cond4, pi_div_two, result);

set_node_name(node.get_name(), result.get_node_shared_ptr());
return {result};
}
} // namespace op
} // namespace tensorflow
} // namespace frontend
} // namespace ov
46 changes: 46 additions & 0 deletions tests/layer_tests/tensorflow_tests/test_tf_Atan2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright (C) 2018-2023 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 TestAtan2(CommonTFLayerTest):
def _prepare_input(self, inputs_info):
assert 'y' in inputs_info
assert 'x' in inputs_info
y_shape = inputs_info['y']
x_shape = inputs_info['x']
inputs_data = {}
inputs_data['y'] = np.random.rand(*y_shape).astype(self.input_type) - np.random.rand(*y_shape).astype(self.input_type)
inputs_data['x'] = np.random.rand(*x_shape).astype(self.input_type) - np.random.rand(*x_shape).astype(self.input_type)
return inputs_data

def create_atan2_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')
tf.raw_ops.Atan2(y=y, x=x)
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_atan2_basic(self, params, ie_device, precision, ir_version, temp_dir,
use_new_frontend, use_old_api):
self._test(*self.create_atan2_net(**params),
ie_device, precision, ir_version, temp_dir=temp_dir,
use_new_frontend=use_new_frontend, use_old_api=use_old_api)
Loading