[ONNX] Add type conversion for Pow op inputs (#2589)
[platform/upstream/dldt.git] / ngraph / frontend / onnx_import / src / op / pow.cpp
1 //*****************************************************************************
2 // Copyright 2020 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //*****************************************************************************
16
17 #include <memory>
18
19 #include "ngraph/node.hpp"
20 #include "onnx_import/default_opset.hpp"
21 #include "onnx_import/op/pow.hpp"
22
23 namespace ngraph
24 {
25     namespace onnx_import
26     {
27         namespace op
28         {
29             namespace set_1
30             {
31                 OutputVector pow(const Node& node)
32                 {
33                     auto inputs = node.get_ng_inputs();
34                     NGRAPH_CHECK(inputs.size() == 2,
35                                  "Power operation requires 2 inputs. Got: ",
36                                  inputs.size());
37
38                     auto base = inputs[0];
39                     auto exponent = inputs[1];
40                     auto base_type = inputs[0].get_element_type();
41                     auto exponent_type = inputs[1].get_element_type();
42                     if (exponent_type != base_type)
43                     {
44                         if (exponent_type.is_integral() ||
45                             (base_type.is_real() &&
46                              base_type.bitwidth() >= exponent_type.bitwidth()))
47                         {
48                             exponent =
49                                 std::make_shared<default_opset::Convert>(exponent, base_type);
50                         }
51                         else
52                         {
53                             base = std::make_shared<default_opset::Convert>(base, exponent_type);
54                             auto power = std::make_shared<default_opset::Power>(base, exponent);
55                             return {std::make_shared<default_opset::Convert>(power, base_type)};
56                         }
57                     }
58                     return {std::make_shared<default_opset::Power>(base, exponent)};
59                 }
60
61             } // namespace set_1
62
63         } // namespace op
64
65     } // namespace onnx_import
66
67 } // namespace ngraph