diff --git a/nncf/common/insertion_point_graph.py b/nncf/common/insertion_point_graph.py index 5cd1a8e4710..118d079fc8e 100644 --- a/nncf/common/insertion_point_graph.py +++ b/nncf/common/insertion_point_graph.py @@ -12,7 +12,7 @@ from collections import defaultdict from copy import deepcopy from enum import Enum -from typing import Dict, List, Optional, Set +from typing import Dict, List, Set import networkx as nx @@ -23,7 +23,6 @@ from nncf.common.graph.layer_attributes import Dtype from nncf.common.graph.operator_metatypes import INPUT_NOOP_METATYPES from nncf.common.graph.patterns import GraphPattern -from nncf.common.logging import nncf_logger class InsertionPointGraphNodeType(Enum): @@ -393,34 +392,3 @@ def get_pre_hook_node_key(node_key: str, input_port_id: int = 0) -> str: @staticmethod def get_post_hook_node_key(node_key: str) -> str: return InsertionPointGraph.POST_HOOK_ID_PREFIX + node_key - - -class ConstantNodesFilter: - @staticmethod - def filter(ip_graph: InsertionPointGraph, start_traversing_node_keys: Optional[List[str]]) -> InsertionPointGraph: - """ - Removes all Constant nodes from InsertionPointGraph, making it inference graph. - The traversing starts from the input nodes and nodes with weights. - - :param ip_graph: The original InsertionPointGraph. - :param start_traversing_node_keys: Keys of the nodes from which the traversing will be start. - :return: InsertionPointGraph without Constant nodes. - """ - input_nodes = ip_graph.get_input_nodes() - if not input_nodes: - nncf_logger.debug("Skipped filtering - no input nodes found") - return ip_graph - weight_nodes = [] - if start_traversing_node_keys is not None: - weight_nodes = [ - ip_graph.get_merged_node_from_single_node_key(weight_node) for weight_node in start_traversing_node_keys - ] - visited_nodes = set() - start_nodes = input_nodes + weight_nodes - for node in start_nodes: - for node_from, node_to in nx.bfs_edges(ip_graph, source=node): - visited_nodes.add(node_from) - visited_nodes.add(node_to) - constant_nodes = [node for node in ip_graph.nodes if node not in visited_nodes] - ip_graph.remove_nodes_from(constant_nodes) - return ip_graph diff --git a/tests/common/quantization/test_filter_constant_nodes.py b/tests/common/quantization/test_filter_constant_nodes.py deleted file mode 100644 index 385e9244f4c..00000000000 --- a/tests/common/quantization/test_filter_constant_nodes.py +++ /dev/null @@ -1,264 +0,0 @@ -# Copyright (c) 2024 Intel Corporation -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# http://www.apache.org/licenses/LICENSE-2.0 -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import re -from collections import Counter - -import pytest - -from nncf.common.graph.operator_metatypes import InputNoopMetatype -from nncf.common.graph.operator_metatypes import OutputNoopMetatype -from nncf.common.insertion_point_graph import ConstantNodesFilter -from nncf.common.insertion_point_graph import InsertionPointGraph -from nncf.common.quantization.structs import QuantizableWeightedLayerNode -from nncf.common.quantization.structs import QuantizerConfig -from nncf.common.utils.registry import Registry -from tests.common.quantization.metatypes import WEIGHT_LAYER_METATYPES -from tests.common.quantization.metatypes import Conv2dTestMetatype -from tests.common.quantization.metatypes import IdentityTestMetatype -from tests.common.quantization.metatypes import LinearTestMetatype -from tests.common.quantization.metatypes import ReshapeTestMetatype -from tests.common.quantization.mock_graphs import NodeWithType -from tests.common.quantization.mock_graphs import create_mock_graph -from tests.common.quantization.mock_graphs import get_ip_graph_for_test -from tests.common.quantization.mock_graphs import get_nncf_graph_from_mock_nx_graph - -SYNTHETIC_NNCF_GRAPH_WITH_CONSTANT_SUBGRAPHS = Registry("SYNTHETIC_MODELS_WITH_CONSTANT_SUBGRAPHS") - - -@SYNTHETIC_NNCF_GRAPH_WITH_CONSTANT_SUBGRAPHS.register() -class ModelToTest1: - # Original graph Filtered graph - # Input_1 Reshape_1 Input_1 - # | / | - # Conv_1 Identity_1 Conv_1 - # | / | - # FC_1 FC_1 - # | | - # Identity_2 Identity_2 - # | | - # FC_2 --- Identity_3 FC_2 - # | | - # Output_1 Output_1 - - def __init__(self): - nodes = [ - NodeWithType("Input_1", InputNoopMetatype), - NodeWithType("Conv_1", Conv2dTestMetatype), - NodeWithType("FC_1", LinearTestMetatype), - NodeWithType("Identity_1", IdentityTestMetatype), - NodeWithType("Reshape_1", ReshapeTestMetatype), - NodeWithType("Identity_2", IdentityTestMetatype), - NodeWithType("FC_2", LinearTestMetatype), - NodeWithType("Identity_3", IdentityTestMetatype), - NodeWithType("Output_1", OutputNoopMetatype), - ] - node_edges = [ - ("Input_1", "Conv_1"), - ("Conv_1", "FC_1"), - ("Identity_1", "FC_1"), - ("Reshape_1", "Identity_1"), - ("FC_1", "Identity_2"), - ("Identity_2", "FC_2"), - ("Identity_3", "FC_2"), - ("FC_2", "Output_1"), - ] - ref_nodes = [ - NodeWithType("Input_1", InputNoopMetatype), - NodeWithType("Conv_1", Conv2dTestMetatype), - NodeWithType("FC_1", LinearTestMetatype), - NodeWithType("Identity_2", IdentityTestMetatype), - NodeWithType("FC_2", IdentityTestMetatype), - NodeWithType("Output_1", OutputNoopMetatype), - ] - ref_edges = [ - ("Input_1", "Conv_1"), - ("Conv_1", "FC_1"), - ("FC_1", "Identity_2"), - ("Identity_2", "FC_2"), - ("FC_2", "Output_1"), - ] - - original_mock_graph = create_mock_graph(nodes, node_edges) - self.nncf_graph = get_nncf_graph_from_mock_nx_graph(original_mock_graph) - reference_mock_graph = create_mock_graph(ref_nodes, ref_edges) - self.ref_nncf_graph = get_nncf_graph_from_mock_nx_graph(reference_mock_graph) - - -@SYNTHETIC_NNCF_GRAPH_WITH_CONSTANT_SUBGRAPHS.register() -class ModelToTest2: - # Original graph Filtered graph - # Input_1 Conv_1 Input_1 Conv_1 - # | / | / - # Conv_2 Identity_1 Conv_2 Identity_1 - # | / | / - # FC_1 FC_1 - # | | - # Identity_2 Identity_2 - # | | - # FC_2 --- Identity_3 FC_2 - # | | - # Output_1 Output_1 - - def __init__(self): - nodes = [ - NodeWithType("Input_1", InputNoopMetatype), - NodeWithType("Conv_2", Conv2dTestMetatype), - NodeWithType("FC_1", LinearTestMetatype), - NodeWithType("Identity_1", IdentityTestMetatype), - NodeWithType("Conv_1", Conv2dTestMetatype), - NodeWithType("Identity_2", IdentityTestMetatype), - NodeWithType("FC_2", LinearTestMetatype), - NodeWithType("Identity_3", IdentityTestMetatype), - NodeWithType("Output_1", OutputNoopMetatype), - ] - node_edges = [ - ("Input_1", "Conv_1"), - ("Conv_2", "FC_1"), - ("Identity_1", "FC_1"), - ("Conv_1", "Identity_1"), - ("FC_1", "Identity_2"), - ("Identity_2", "FC_2"), - ("Identity_3", "FC_2"), - ("FC_2", "Output_1"), - ] - ref_nodes = [ - NodeWithType("Input_1", InputNoopMetatype), - NodeWithType("Conv_2", Conv2dTestMetatype), - NodeWithType("FC_1", LinearTestMetatype), - NodeWithType("Identity_1", IdentityTestMetatype), - NodeWithType("Conv_1", Conv2dTestMetatype), - NodeWithType("Identity_2", IdentityTestMetatype), - NodeWithType("FC_2", LinearTestMetatype), - NodeWithType("Output_1", OutputNoopMetatype), - ] - ref_edges = [ - ("Input_1", "Conv_1"), - ("Conv_2", "FC_1"), - ("Identity_1", "FC_1"), - ("Conv_1", "Identity_1"), - ("FC_1", "Identity_2"), - ("Identity_2", "FC_2"), - ("FC_2", "Output_1"), - ] - - original_mock_graph = create_mock_graph(nodes, node_edges) - self.nncf_graph = get_nncf_graph_from_mock_nx_graph(original_mock_graph) - reference_mock_graph = create_mock_graph(ref_nodes, ref_edges) - self.ref_nncf_graph = get_nncf_graph_from_mock_nx_graph(reference_mock_graph) - - -@SYNTHETIC_NNCF_GRAPH_WITH_CONSTANT_SUBGRAPHS.register() -class ModelToTest3: - # Original graph Filtered graph - # (Graph will not be filtered, because there is no Input node) - # Identity_1 Identity_1 - # | | - # Conv_1 Identity_2 Conv_1 Identity_2 - # | / | / - # FC_1 FC_1 - # | | - # Identity_3 Identity_3 - # | | - # FC_2 --- Identity_4 FC_2 --- Identity_4 - # | | - # Output_1 Output_1 - - def __init__(self): - nodes = [ - NodeWithType("Identity_1", IdentityTestMetatype), - NodeWithType("Conv_1", Conv2dTestMetatype), - NodeWithType("FC_1", LinearTestMetatype), - NodeWithType("Identity_2", IdentityTestMetatype), - NodeWithType("Identity_3", IdentityTestMetatype), - NodeWithType("FC_2", LinearTestMetatype), - NodeWithType("Identity_4", IdentityTestMetatype), - NodeWithType("Output_1", OutputNoopMetatype), - ] - node_edges = [ - ("Identity_1", "Conv_1"), - ("Conv_1", "FC_1"), - ("Identity_2", "FC_1"), - ("FC_1", "Identity_3"), - ("Identity_3", "FC_2"), - ("Identity_4", "FC_2"), - ("FC_2", "Output_1"), - ] - ref_nodes = [ - NodeWithType("Identity_1", IdentityTestMetatype), - NodeWithType("Conv_1", Conv2dTestMetatype), - NodeWithType("FC_1", LinearTestMetatype), - NodeWithType("Identity_2", IdentityTestMetatype), - NodeWithType("Identity_3", IdentityTestMetatype), - NodeWithType("FC_2", LinearTestMetatype), - NodeWithType("Identity_4", IdentityTestMetatype), - NodeWithType("Output_1", OutputNoopMetatype), - ] - ref_edges = [ - ("Identity_1", "Conv_1"), - ("Conv_1", "FC_1"), - ("Identity_2", "FC_1"), - ("FC_1", "Identity_3"), - ("Identity_3", "FC_2"), - ("Identity_4", "FC_2"), - ("FC_2", "Output_1"), - ] - - original_mock_graph = create_mock_graph(nodes, node_edges) - self.nncf_graph = get_nncf_graph_from_mock_nx_graph(original_mock_graph) - reference_mock_graph = create_mock_graph(ref_nodes, ref_edges) - self.ref_nncf_graph = get_nncf_graph_from_mock_nx_graph(reference_mock_graph) - - -@pytest.mark.parametrize("model_to_test", SYNTHETIC_NNCF_GRAPH_WITH_CONSTANT_SUBGRAPHS.values()) -def test_constant_nodes_filter(model_to_test): - model_to_test = model_to_test() - nncf_graph = model_to_test.nncf_graph - weight_nodes = nncf_graph.get_nodes_by_metatypes(WEIGHT_LAYER_METATYPES) - quantizable_layer_nodes = [ - QuantizableWeightedLayerNode(weight_node, [QuantizerConfig()]) for weight_node in weight_nodes - ] - quantizable_layer_node_keys = [node.node.node_key for node in quantizable_layer_nodes] - - ip_graph = get_ip_graph_for_test(nncf_graph, quantizable_layer_nodes) - filtered_ip_graph = ConstantNodesFilter.filter(ip_graph, quantizable_layer_node_keys) - - ref_ip_graph = get_ip_graph_for_test(model_to_test.ref_nncf_graph, quantizable_layer_nodes) - - check_ip_graphs_are_equal(filtered_ip_graph, ref_ip_graph) - - -def check_ip_graphs_are_equal(graph_1: InsertionPointGraph, graph_2: InsertionPointGraph): - graph_1_node_keys_without_index = [graph_1_node_key.split(" ")[-1] for graph_1_node_key in graph_1.nodes] - graph_2_node_keys_without_index = [graph_2_node_key.split(" ")[-1] for graph_2_node_key in graph_2.nodes] - assert Counter(graph_1_node_keys_without_index) == Counter(graph_2_node_keys_without_index) - - graph_1_filtered_edges, graph_2_filtered_edges = [], [] - for edge in graph_1.edges: - graph_1_filtered_edges.append((filter_edge(edge[0]), filter_edge(edge[1]))) - for edge in graph_2.edges: - graph_2_filtered_edges.append((filter_edge(edge[0]), filter_edge(edge[1]))) - assert Counter(graph_1_filtered_edges) == Counter(graph_2_filtered_edges) - - -def filter_edge(edge: str) -> str: - """ - Removes node ids from the edges. - - :param edge: Edges to remove node ids. - :return: Filtered edge. - """ - splitted_edge = edge.split(" ") - filtered_edge = [] - for word in splitted_edge: - if re.match("[0-9]+", word) is None: - filtered_edge.append(word) - return "".join(filtered_edge) diff --git a/tests/common/test_ignored_scope.py b/tests/common/test_ignored_scope.py index 2a4c3a9364f..6186e9123b3 100644 --- a/tests/common/test_ignored_scope.py +++ b/tests/common/test_ignored_scope.py @@ -20,8 +20,8 @@ from tests.common.quantization.metatypes import Conv2dTestMetatype from tests.common.quantization.metatypes import LinearTestMetatype from tests.common.quantization.mock_graphs import NodeWithType -from tests.common.quantization.test_filter_constant_nodes import create_mock_graph -from tests.common.quantization.test_filter_constant_nodes import get_nncf_graph_from_mock_nx_graph +from tests.common.quantization.mock_graphs import create_mock_graph +from tests.common.quantization.mock_graphs import get_nncf_graph_from_mock_nx_graph LINEAR_TYPE = "linear" CONV_TYPE = "conv" diff --git a/tests/post_training/test_templates/models.py b/tests/post_training/test_templates/models.py index 8c4a955fb67..6cc2f0d5c38 100644 --- a/tests/post_training/test_templates/models.py +++ b/tests/post_training/test_templates/models.py @@ -15,8 +15,8 @@ from nncf.common.graph.operator_metatypes import OutputNoopMetatype from tests.common.quantization.metatypes import ConstantTestMetatype from tests.common.quantization.mock_graphs import NodeWithType -from tests.common.quantization.test_filter_constant_nodes import create_mock_graph -from tests.common.quantization.test_filter_constant_nodes import get_nncf_graph_from_mock_nx_graph +from tests.common.quantization.mock_graphs import create_mock_graph +from tests.common.quantization.mock_graphs import get_nncf_graph_from_mock_nx_graph class NNCFGraphToTest: