Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / mo / middle / passes / fusing / mark_unfused_nodes.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 import re
19
20 from mo.graph.graph import Node, Graph
21 from mo.middle.passes.fusing.helpers import get_value_id
22
23
24 def _check_lin_op(node: Node, layout: str):
25     lin_ops = ['Mul', 'Add']
26     if node.soft_get('op') in lin_ops:
27         weights_id = get_value_id(node)
28         if weights_id is None:
29             node.graph.node[node.id]['can_be_fused'] = False
30             log.info('[ FUSING ] Node {} wasn\'t marked as fusable (no weights, probably this is Eltwise that is not '
31                      'fusable)'.format(node.id))
32             return
33
34         node.graph.node[node.id]['can_be_fused'] = True
35         log.info('[ FUSING ] Node {} marked as fusable'.format(node.id))
36
37
38 def mark_unfused_nodes(graph: Graph, regex_masks: str):
39     regex_masks = [] if not regex_masks else regex_masks.split(',')
40     nodes = graph.get_op_nodes()
41     for node in nodes:
42         if node.has_valid('can_be_fused'):
43             continue
44         disabled = False
45         for mask in regex_masks:
46             res = re.findall(mask, node.name)
47             if res and len(res):
48                 graph.node[node.id]['can_be_fused'] = False
49                 log.info('[ FUSING ] Node {} wasn\'t marked as fusable (user decision {})'.format(node.id,mask))
50                 disabled = True
51         if not disabled:
52             _check_lin_op(node, graph.graph['layout'])
53
54