Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / mo / ops / power.py
1 """
2  Copyright (c) 2018-2019 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 import logging as log
18
19 import numpy as np
20
21 from mo.front.common.partial_infer.eltwise import eltwise_infer
22 from mo.graph.graph import Node, Graph
23 from mo.ops.op import Op
24
25
26 class Power(Op):
27     enabled = False
28     op = 'Power'
29
30     def __init__(self, graph: Graph, attrs: dict):
31         super().__init__(graph, {
32             'type': 'Power',
33             'op': __class__.op,
34             'power': 1,
35             'scale': 1,
36             'shift': 0,
37             'infer': __class__.infer,
38             'in_ports_count': 1,
39             'out_ports_count': 1,
40         }, attrs)
41
42     def supported_attrs(self):
43         """
44         List of attributes that can/should be set by a client.
45         """
46         return ['power', 'scale', 'shift']
47
48     @staticmethod
49     def infer(node: Node):
50         input_nodes_cnt = len(node.in_nodes())
51         if input_nodes_cnt > 2:
52             log.error('Power layer {} must have one or two inputs (given {})'.format(node.name, len(node.in_nodes())))
53             return
54
55         # In case of two inputs we should check value of the second input (should be a scalar)
56         if input_nodes_cnt == 2:
57             if not node.in_node(1).has_valid('value'):
58                 log.error('Power layer {} do not support dynamic power value'.format(node.name, len(node.in_nodes())))
59                 return
60
61             if node.in_node(1).value.ndim != 0:
62                 log.error('Power layer {} do not support not scalar power value'.format(node.name, len(node.in_nodes())))
63                 return
64
65             node['power'] = np.array(node.in_node(1).value, dtype=np.float64)
66             node.graph.remove_edge(node.in_node(1).id, node.id)
67
68         eltwise_infer(node, lambda a: np.power(a * node.scale + node.shift, node.power))
69