def test_build_quantizer_propagation_state_graph_from_ip_graph(self):
        ip_graph = InsertionPointGraph(get_two_branch_mock_model_graph())
        quant_prop_graph = QPSG(ip_graph)
        assert len(ip_graph.nodes) == len(quant_prop_graph.nodes)
        assert len(ip_graph.edges) == len(quant_prop_graph.edges)

        for ip_graph_node_key, ip_graph_node in ip_graph.nodes.items():
            qpg_node = quant_prop_graph.nodes[ip_graph_node_key]
            assert qpg_node[QPSG.NODE_TYPE_NODE_ATTR] == QPSG.ipg_node_type_to_qpsg_node_type(ip_graph_node[
                InsertionPointGraph.NODE_TYPE_NODE_ATTR])
            qpg_node_type = qpg_node[QPSG.NODE_TYPE_NODE_ATTR]
            if qpg_node_type == QuantizerPropagationStateGraphNodeType.INSERTION_POINT:
                assert qpg_node[QPSG.PROPAGATING_QUANTIZER_NODE_ATTR] is None
                assert not qpg_node[QPSG.AFFECTING_PROPAGATING_QUANTIZERS_ATTR]
                assert qpg_node[QPSG.INSERTION_POINT_DATA_NODE_ATTR] == ip_graph_node[
                    InsertionPointGraph.INSERTION_POINT_DATA_NODE_ATTR]
            elif qpg_node_type == QuantizerPropagationStateGraphNodeType.OPERATOR:
                assert not qpg_node[QPSG.ALLOWED_INPUT_QUANTIZATION_TYPES_NODE_ATTR]
                assert qpg_node[QPSG.QUANTIZATION_TRAIT_NODE_ATTR] == QuantizationTrait.NON_QUANTIZABLE
                assert not qpg_node[QPSG.AFFECTING_PROPAGATING_QUANTIZERS_ATTR]

        for from_node, to_node, edge_data in ip_graph.edges(data=True):
            qpg_edge_data = quant_prop_graph.edges[from_node, to_node]
            assert not qpg_edge_data[QPSG.AFFECTING_PROPAGATING_QUANTIZERS_ATTR]
            for key, value in edge_data.items():
                assert qpg_edge_data[key] == value

        quant_prop_graph.run_consistency_check()
 def mock_qp_graph():
     ip_graph = InsertionPointGraph(get_two_branch_mock_model_graph())
     qpsg = QPSG(ip_graph)
     qpsg.skip_check = False
     yield qpsg
     if not qpsg.skip_check:
         qpsg.run_consistency_check()
Esempio n. 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)
 def model_graph_qpsg(self):
     mock_graph = self.get_model_graph()
     ip_graph = InsertionPointGraph(mock_graph)
     quant_prop_graph = QPSG(ip_graph)
     return quant_prop_graph
Esempio n. 5
0
 def mock_qp_graph():
     ip_graph = InsertionPointGraph(get_two_branch_mock_model_graph())
     yield QPSG(ip_graph)