def _setup_and_propagate_quantizers(self, qpsg: QPSG) -> QPSG:
            pq_1 = qpsg.add_propagating_quantizer([QuantizerConfig()],
                                                  InsertionPointGraph.get_pre_hook_node_key('F'))

            qpsg.propagate_quantizer_via_path(pq_1, [
                (InsertionPointGraph.get_post_hook_node_key('C'),
                 InsertionPointGraph.get_pre_hook_node_key('F'))
            ])
            _ = qpsg.add_propagating_quantizer([QuantizerConfig(bits=6)],
                                               InsertionPointGraph.get_pre_hook_node_key('F'))
            return qpsg
 def _setup_and_propagate_quantizers(self, qpsg: QPSG) -> QPSG:
     pq_1 = qpsg.add_propagating_quantizer([QuantizerConfig(per_channel=True)],
                                           InsertionPointGraph.get_pre_hook_node_key('C'))
     pq_2 = qpsg.add_propagating_quantizer([QuantizerConfig(per_channel=True)],
                                           InsertionPointGraph.get_pre_hook_node_key('D'))
     qpsg.merge_quantizers_for_branching_node([pq_1, pq_2], [QuantizerConfig(per_channel=True)],
                                              [None, None],
                                              InsertionPointGraph.get_post_hook_node_key('B'))
     pq_3 = qpsg.add_propagating_quantizer([QuantizerConfig()],
                                           InsertionPointGraph.get_pre_hook_node_key('E'))
     paths = get_edge_paths_for_propagation(qpsg,
                                            InsertionPointGraph.get_pre_hook_node_key('D'),
                                            InsertionPointGraph.get_pre_hook_node_key('E'))
     path = paths[0]
     qpsg.propagate_quantizer_via_path(pq_3, path)
     return qpsg
Exemple #3
0
    def test_merge_quantizer_into_path(self,
                                       merge_quantizer_into_path_test_struct):

        mock_graph = self.get_model_graph()
        ip_graph = InsertionPointGraph(mock_graph)
        quant_prop_graph = QPSG(ip_graph)

        for quantizers_test_struct in merge_quantizer_into_path_test_struct.start_set_quantizers:

            init_node_to_trait_and_configs_dict = quantizers_test_struct.init_node_to_trait_and_configs_dict
            starting_quantizer_ip_node = quantizers_test_struct.starting_quantizer_ip_node
            target_node = quantizers_test_struct.target_node_for_quantizer
            is_merged = quantizers_test_struct.is_merged
            prop_path = quantizers_test_struct.prop_path
            for node in quant_prop_graph.nodes.values():
                node[
                    QPSG.
                    QUANTIZATION_TRAIT_NODE_ATTR] = QuantizationTrait.QUANTIZATION_AGNOSTIC
            master_prop_quant = None
            merged_prop_quant = []
            for node_key, trait_and_configs_tuple in init_node_to_trait_and_configs_dict.items(
            ):
                trait = trait_and_configs_tuple[0]
                qconfigs = trait_and_configs_tuple[1]
                quant_prop_graph.nodes[node_key][
                    QPSG.QUANTIZATION_TRAIT_NODE_ATTR] = trait
                if trait == QuantizationTrait.INPUTS_QUANTIZABLE:
                    ip_node_key = InsertionPointGraph.get_pre_hook_node_key(
                        node_key)
                    prop_quant = quant_prop_graph.add_propagating_quantizer(
                        qconfigs, ip_node_key)
                    if ip_node_key == starting_quantizer_ip_node:
                        master_prop_quant = prop_quant

            path = get_edge_paths_for_propagation(quant_prop_graph,
                                                  target_node,
                                                  starting_quantizer_ip_node)
            master_prop_quant = quant_prop_graph.propagate_quantizer_via_path(
                master_prop_quant, path[0])
            if is_merged:
                merged_prop_quant.append((master_prop_quant, prop_path))

        for prop_quant, prop_path in merged_prop_quant:
            quant_prop_graph.merge_quantizer_into_path(prop_quant, prop_path)

        expected_quantizers_test_struct = merge_quantizer_into_path_test_struct.expected_set_quantizers
        self.check_final_state_qpsg(quant_prop_graph,
                                    expected_quantizers_test_struct)