|
| 1 | +// Copyright (C) 2018-2024 Intel Corporation |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// |
| 4 | +#include "openvino/frontend/paddle/node_context.hpp" |
| 5 | +#include "openvino/op/add.hpp" |
| 6 | +#include "openvino/op/atan.hpp" |
| 7 | +#include "openvino/op/constant.hpp" |
| 8 | +#include "openvino/op/convert_like.hpp" |
| 9 | +#include "openvino/op/divide.hpp" |
| 10 | +#include "openvino/op/equal.hpp" |
| 11 | +#include "openvino/op/greater.hpp" |
| 12 | +#include "openvino/op/greater_eq.hpp" |
| 13 | +#include "openvino/op/less.hpp" |
| 14 | +#include "openvino/op/logical_and.hpp" |
| 15 | +#include "openvino/op/multiply.hpp" |
| 16 | +#include "openvino/op/select.hpp" |
| 17 | +#include "openvino/op/subtract.hpp" |
| 18 | +#include "openvino/opsets/opset6.hpp" |
| 19 | +using namespace std; |
| 20 | +using namespace ov::op; |
| 21 | + |
| 22 | +namespace ov { |
| 23 | +namespace frontend { |
| 24 | +namespace paddle { |
| 25 | + |
| 26 | +template <typename T> |
| 27 | +ov::Output<ov::Node> create_same_type_const_scalar(const ov::Output<ov::Node>& same_type_output, const T& value) { |
| 28 | + if (same_type_output.get_element_type().is_static()) { |
| 29 | + return std::make_shared<ov::op::v0::Constant>(same_type_output.get_element_type(), ov::Shape{}, value); |
| 30 | + } else { |
| 31 | + ov::Output<ov::Node> const_res = |
| 32 | + std::make_shared<ov::op::v0::Constant>(ov::element::from<T>(), ov::Shape{}, value); |
| 33 | + const_res = std::make_shared<ov::op::v1::ConvertLike>(const_res, same_type_output); |
| 34 | + return const_res; |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +namespace op { |
| 39 | +NamedOutputs atan2(const NodeContext& node) { |
| 40 | + // default_op_checks(node, 2, {"Atan2"}); |
| 41 | + auto y = node.get_input("X1"); |
| 42 | + auto x = node.get_input("X2"); |
| 43 | + |
| 44 | + // handle the first condition : x>0 |
| 45 | + auto div_y_x = make_shared<v1::Divide>(y, x); |
| 46 | + auto atan = make_shared<v0::Atan>(div_y_x); |
| 47 | + auto const_zero = create_same_type_const_scalar<int32_t>(x, 0); |
| 48 | + auto result = atan->output(0); |
| 49 | + |
| 50 | + // handle the second condition : x<0 && y>=0 |
| 51 | + auto const_pi = create_same_type_const_scalar<double>(x, std::atan(1.0) * 4); |
| 52 | + auto is_x_negative = make_shared<v1::Less>(x, const_zero); |
| 53 | + auto y_non_negative = make_shared<v1::GreaterEqual>(y, const_zero); |
| 54 | + auto cond1 = make_shared<v1::LogicalAnd>(is_x_negative, y_non_negative); |
| 55 | + auto atan_y_x_plus_pi = make_shared<v1::Add>(atan, const_pi); |
| 56 | + result = make_shared<v1::Select>(cond1, atan_y_x_plus_pi, result); |
| 57 | + |
| 58 | + // handle the third condition : x<0 && y<0 |
| 59 | + auto is_y_negative = make_shared<v1::Less>(y, const_zero); |
| 60 | + auto cond2 = make_shared<v1::LogicalAnd>(is_x_negative, is_y_negative); |
| 61 | + auto atan_y_x_minus_pi = make_shared<v1::Subtract>(atan, const_pi); |
| 62 | + result = make_shared<v1::Select>(cond2, atan_y_x_minus_pi, result); |
| 63 | + |
| 64 | + // handle the fourth condition : x=0 && y>0 |
| 65 | + auto is_x_zero = make_shared<v1::Equal>(x, const_zero); |
| 66 | + auto is_y_positive = make_shared<v1::Greater>(y, const_zero); |
| 67 | + auto cond3 = make_shared<v1::LogicalAnd>(is_x_zero, is_y_positive); |
| 68 | + auto const_two = create_same_type_const_scalar<int32_t>(x, 2); |
| 69 | + auto pi_div_two = make_shared<v1::Divide>(const_pi, const_two); |
| 70 | + result = make_shared<v1::Select>(cond3, pi_div_two, result); |
| 71 | + |
| 72 | + // handle the fifth condition : x=0 && y<0 |
| 73 | + auto cond4 = make_shared<v1::LogicalAnd>(is_x_zero, is_y_negative); |
| 74 | + auto const_minus_two = create_same_type_const_scalar<int32_t>(x, -2); |
| 75 | + auto pi_div_minus_two = make_shared<v1::Divide>(const_pi, const_minus_two); |
| 76 | + result = make_shared<v1::Select>(cond4, pi_div_two, result); |
| 77 | + NamedOutputs named_outputs; |
| 78 | + named_outputs["Out"] = {result}; |
| 79 | + return named_outputs; |
| 80 | +} |
| 81 | + |
| 82 | +} // namespace op |
| 83 | +} // namespace paddle |
| 84 | +} // namespace frontend |
| 85 | +} // namespace ov |
0 commit comments