-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TF FE] Support Angle operation for TensorFlow models (#23028)
### Details: - *Support Angle operation for TensorFlow models* ### Tickets: - Closes #22083 --------- Co-authored-by: Roman Kazantsev <[email protected]>
- Loading branch information
Showing
5 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<ov::element::Type>("Tout"); | ||
|
||
auto complex_type_mark = as_type_ptr<ComplexTypeMark>(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<v0::Constant>(element::i32, Shape{}, 0); | ||
auto imag_index = make_shared<v0::Constant>(element::i32, Shape{}, 1); | ||
auto gather_axis = make_shared<v0::Constant>(element::i32, Shape{1}, -1); | ||
|
||
auto x = make_shared<v8::Gather>(complex, real_index, gather_axis)->output(0); | ||
auto y = make_shared<v8::Gather>(complex, imag_index, gather_axis)->output(0); | ||
|
||
// 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); | ||
auto result_changed_type = make_shared<v0::Convert>(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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |