Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / middle / AddQuantizeFuse.py
1 """
2  Copyright (c) 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 from typing import Dict
20
21 from mo.graph.graph import Graph, Node
22 from mo.middle.passes.conv import get_tensor_in_port, get_value_in_port
23 from mo.middle.replacement import MiddleReplacementPattern
24
25
26 class AddQuantizeFuse(MiddleReplacementPattern):
27     """ Fuses Add --> Quantize sequence if possible
28     """
29     enabled = False
30
31     def run_after(self):
32         return []
33
34     def run_before(self):
35         return []
36
37     def pattern(self):
38         return dict(
39             nodes=[
40                 ('preop', dict(op='Add')),
41                 ('preoped', dict()),
42                 ('quantize', dict(op='Quantize')),
43             ],
44             edges=[
45                 ('preop', 'preoped'),
46                 ('preoped', 'quantize', {'in': 0}),
47             ]
48         )
49
50     def replace_pattern(self, graph: Graph, match: Dict[str, Node]):
51
52         quantize = match['quantize']
53         preop = match['preop']
54
55         # Check for total number of Add consumers -- if something else consume its output it cannot be fused
56         if len(preop.out_node().out_nodes()) > 1:
57             log.debug('AddQuantizeFuse: cannot fuse because Add have Addtiple consumers')
58             return
59
60         # If the fusion is applicable, direct modifications to quantize 1-st and 2-nd inputs
61         # are performed. So the data nodes at those inputs shouldn't have more than 1 consumer
62         # maximum 2 consumers to the same quantize op (consumed by 1st and 2nd ports).
63         # TODO: relax this limitation and duplicate data nodes accordingly to modify the input range freely
64
65         # Provisional limitation that related to binary quantization
66         # TODO: Relax it beyond binarization case
67         if len(quantize.in_node(1).out_nodes()) != 1 or \
68                 len(quantize.in_node(2).out_nodes()) != 1 or \
69                 len(quantize.in_node(3).out_nodes()) != 1 or len(quantize.in_node(4).out_nodes()) != 1 or \
70                 quantize.levels != 2:
71             log.debug('AddQuantizeFuse: cannot fuse because Quantize op has '
72                       'unexpected number of consumers for ports 1, 2, 3 or 4')
73             return
74
75         tensor_port, value_port = get_tensor_in_port(preop), get_value_in_port(preop)
76
77         quantize.in_port(1).data.set_value(quantize.in_port(1).data.get_value() - value_port.data.get_value())
78         quantize.in_port(2).data.set_value(quantize.in_port(2).data.get_value() - value_port.data.get_value())
79         quantize.in_port(0).disconnect()
80         tensor_port.get_connection().set_destination(quantize.in_port(0))