|
| 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 |
0 commit comments