2 Copyright (c) 2018-2019 Intel Corporation
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
19 from mo.graph.graph import Node, Graph
20 from mo.middle.replacement import MiddleReplacementPattern
23 class EltwiseChecker(MiddleReplacementPattern):
24 # This pass checks for each eltwise, can it be ScaleShift or not
28 from extensions.middle.EltwiseInputReshape import Eltwise1DInputReshape
29 return [Eltwise1DInputReshape]
32 from extensions.middle.pass_separator import MiddleFinish
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]
45 for id, s in enumerate(shapes):
46 if max_dims is None or len(s) > max_dims:
51 feature_dim = 1 if node.graph.graph['layout'] == 'NCHW' else (max_dims - 1)
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))
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