Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / middle / EltwiseChecker.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 numpy as np
18
19 from mo.graph.graph import Node, Graph
20 from mo.middle.replacement import MiddleReplacementPattern
21
22
23 class EltwiseChecker(MiddleReplacementPattern):
24     # This pass checks for each eltwise, can it be ScaleShift or not
25     enabled = True
26
27     def run_after(self):
28         from extensions.middle.EltwiseInputReshape import Eltwise1DInputReshape
29         return [Eltwise1DInputReshape]
30
31     def run_before(self):
32         from extensions.middle.pass_separator import MiddleFinish
33         return [MiddleFinish]
34
35     def find_and_replace_pattern(self, graph: Graph):
36         eltwise_nodes = [Node(graph, node) for node in graph.node if Node(graph, node).soft_get('type') == 'Eltwise']
37         for node in eltwise_nodes:
38             raw_inputs = [(inp, attr) for inp, attr in node.get_sorted_inputs()
39                           if 'control_flow_edge' not in attr or not attr['control_flow_edge']]
40             shapes = [node.graph.node[inp]['shape'] for inp, attr in raw_inputs]
41
42             max_dims = None
43             max_dims_id = None
44             input_shape = None
45             for id, s in enumerate(shapes):
46                 if max_dims is None or len(s) > max_dims:
47                     max_dims = len(s)
48                     input_shape = s
49                     max_dims_id = id
50
51             feature_dim = 1 if node.graph.graph['layout'] == 'NCHW' else (max_dims - 1)
52
53             def check_shape(shape):
54                 # Check that value has shape like 1,N,1,1
55                 return np.prod(shape) == np.max(shape) and (max_dims - feature_dim) <= len(shape) and \
56                        (input_shape[feature_dim] == shape[-1 * (max_dims - feature_dim)] or
57                         (shape[-1 * (max_dims - feature_dim)] == 1 and np.max(shape) == 1))
58
59             # Make all input shapes of the same size by adding 1's
60             axis = node.axis if node.has_valid('axis') else None
61             for id, shape in enumerate(shapes):
62                 if id != max_dims_id and len(shape) > 0 and not check_shape(shapes[id]) and np.prod(shape) != 1:
63                     node['can_be_fused'] = False
64                     node['can_be_scaleshift'] = False