2 Copyright (c) 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 typing import Dict
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
26 class AddQuantizeFuse(MiddleReplacementPattern):
27 """ Fuses Add --> Quantize sequence if possible
40 ('preop', dict(op='Add')),
42 ('quantize', dict(op='Quantize')),
46 ('preoped', 'quantize', {'in': 0}),
50 def replace_pattern(self, graph: Graph, match: Dict[str, Node]):
52 quantize = match['quantize']
53 preop = match['preop']
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')
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
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 \
71 log.debug('AddQuantizeFuse: cannot fuse because Quantize op has '
72 'unexpected number of consumers for ports 1, 2, 3 or 4')
75 tensor_port, value_port = get_tensor_in_port(preop), get_value_in_port(preop)
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))