Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignored scope matching working with multiple NNCFGraphs #2723

Merged
merged 32 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
47269e8
draft
kshpv Jun 5, 2024
5d6edac
introduce IgnoredScopeMatch
kshpv Jun 6, 2024
3bb3cda
minor
kshpv Jun 6, 2024
9948daa
code polishing
kshpv Jun 6, 2024
a08960e
fix
kshpv Jun 6, 2024
c402e9b
fix
kshpv Jun 6, 2024
71773d1
fix is_model_no_batchwise_support
kshpv Jun 6, 2024
396bc6d
add check find pattern/subgraph
kshpv Jun 6, 2024
85d580f
polishing
kshpv Jun 6, 2024
d94fd59
doc
kshpv Jun 6, 2024
8e87d60
formatting
kshpv Jun 7, 2024
de36464
grammar, renames, typehints, docstrings
kshpv Jun 10, 2024
670b8e2
warning_model_no_batchwise_support
kshpv Jun 10, 2024
e5753f4
error_unmatched_ignored_scope
kshpv Jun 10, 2024
2d89d52
asdict -> getattr
kshpv Jun 10, 2024
40e864f
upd error message in test
kshpv Jun 10, 2024
8a3276c
rm unnecessary set
kshpv Jun 10, 2024
4797cdf
Merge remote-tracking branch 'remote/develop' into if_ignored_scope
kshpv Jun 10, 2024
f8cbfdf
rm any
kshpv Jun 11, 2024
d5264af
rm assert
kshpv Jun 11, 2024
6343dd7
minor
kshpv Jun 11, 2024
d3c2326
refactoring
kshpv Jun 13, 2024
d60bc0e
minor
kshpv Jun 13, 2024
a364cfd
upd err msg
kshpv Jun 13, 2024
55fab8f
Merge remote-tracking branch 'remote/develop' into if_ignored_scope
kshpv Jun 13, 2024
37b8432
mypy
kshpv Jun 13, 2024
39f8544
aligned traversing
kshpv Jun 13, 2024
a4a219c
upd graph ids based on IF node name
kshpv Jun 13, 2024
a0e3b92
typehint
kshpv Jun 13, 2024
b66028a
add test_ignored_scope_diff
kshpv Jun 18, 2024
e59610c
add test if model with ingored scope
kshpv Jun 18, 2024
1c88dee
mv refs
kshpv Jun 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion nncf/common/graph/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def get_nodes_by_types(self, type_list: List[str]) -> List[NNCFNode]:
:return: List of nodes with provided types.
"""
all_nodes_of_type = []
for nncf_node in self.get_all_nodes():
for nncf_node in self.nodes.values():
if nncf_node.node_type in type_list:
all_nodes_of_type.append(nncf_node)
return all_nodes_of_type
Expand Down
6 changes: 4 additions & 2 deletions nncf/onnx/quantization/quantize_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
from nncf.quantization.algorithms.accuracy_control.algorithm import calculate_accuracy_drop
from nncf.quantization.algorithms.accuracy_control.evaluator import Evaluator
from nncf.quantization.algorithms.post_training.algorithm import PostTrainingQuantization
from nncf.quantization.quantize_model import BATCHWISE_STATISTICS_WARNING
from nncf.quantization.quantize_model import is_model_no_batchwise_support
from nncf.quantization.quantize_model import quantize_with_tune_hyperparams
from nncf.quantization.quantize_model import warning_model_no_batchwise_support
from nncf.quantization.telemetry_extractors import CompressionStartedWithQuantizeApi
from nncf.scopes import IgnoredScope
from nncf.telemetry import tracked_function
Expand Down Expand Up @@ -83,7 +84,8 @@ def quantize_impl(
)

graph = GraphConverter.create_nncf_graph(model)
warning_model_no_batchwise_support(graph, advanced_parameters, model_type, OPERATIONS_OUTPUT_HAS_NO_BATCH_AXIS)
if is_model_no_batchwise_support(graph, advanced_parameters, model_type, OPERATIONS_OUTPUT_HAS_NO_BATCH_AXIS):
nncf_logger.warning(BATCHWISE_STATISTICS_WARNING)
AlexanderDokuchaev marked this conversation as resolved.
Show resolved Hide resolved
quantized_model = quantization_algorithm.apply(model, graph, dataset=calibration_dataset)

return quantized_model
Expand Down
18 changes: 7 additions & 11 deletions nncf/openvino/quantization/quantize_ifmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@
# limitations under the License.

from itertools import islice
from typing import List, Optional, Tuple
from typing import Dict, List, Optional, Tuple

import openvino.runtime as ov

from nncf import Dataset
from nncf.common import factory
from nncf.common.engine import Engine
from nncf.common.factory import NNCFGraphFactory
from nncf.common.graph.graph import NNCFGraph
from nncf.common.graph.graph import NNCFNode
from nncf.common.graph.model_transformer import ModelTransformer
Expand Down Expand Up @@ -135,26 +134,25 @@ def _add_outputs_before_if_node(model_transformer: ModelTransformer, model: ov.M
def apply_algorithm_if_bodies(
algorithm: Algorithm,
parent_model: ov.Model,
parent_graph: NNCFGraph,
graphs: Dict[int, NNCFGraph],
parent_dataset: Dataset,
subset_size: int,
current_model_num: int,
all_models_num: int,
parent_statistic_points: Optional[StatisticPointsContainer] = None,
) -> Tuple[ov.Model, int]:
"""
Applies an algorithm recursievley to each bodies of If node.

:param parent_model: Model to apply algorithm.
:param parent_graph: Graph of a model.
:param graphs: Mapping from model_number and its graph.
:param parent_dataset: Dataset for algorithm.
:param subset_size: Size of a dataset to use for calibration.
:param current_model_num: Current model number.
:param all_models_num: All model numbers.
:param parent_statistic_points: Statistics points for algorithm.
:return: A model for every bodies of If nodes the algorithm was applied and the latest model number.
"""
nncf_logger.info(f"Iteration [{current_model_num}/{all_models_num}] ...")
nncf_logger.info(f"Iteration [{current_model_num}/{len(graphs)}] ...")
parent_graph = graphs[current_model_num]
quantized_model = algorithm.apply(parent_model, parent_graph, parent_statistic_points, parent_dataset)
if get_number_if_op(parent_model) == 0:
return quantized_model, current_model_num
Expand Down Expand Up @@ -183,20 +181,18 @@ def apply_algorithm_if_bodies(
then_quantized_model, current_model_num = apply_algorithm_if_bodies(
algorithm,
then_model,
NNCFGraphFactory.create(then_model),
graphs,
then_dataset,
subset_size,
current_model_num + 1,
all_models_num,
)
else_quantized_model, current_model_num = apply_algorithm_if_bodies(
algorithm,
else_model,
NNCFGraphFactory.create(else_model),
graphs,
else_dataset,
subset_size,
current_model_num + 1,
all_models_num,
)
model_transformer_int8 = factory.ModelTransformerFactory.create(quantized_model)
quantized_model = _update_if_body(model_transformer_int8, if_node, True, then_quantized_model)
Expand Down
36 changes: 29 additions & 7 deletions nncf/openvino/quantization/quantize_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
from nncf.common.quantization.structs import QuantizationPreset
from nncf.data import Dataset
from nncf.openvino.graph.metatypes.groups import OPERATIONS_OUTPUT_HAS_NO_BATCH_AXIS
from nncf.openvino.graph.metatypes.openvino_metatypes import OVIfMetatype
from nncf.openvino.graph.metatypes.openvino_metatypes import get_node_metatype
from nncf.openvino.graph.model_utils import remove_friendly_name_duplicates
from nncf.openvino.graph.nncf_graph_builder import GraphConverter
from nncf.openvino.graph.node_utils import get_number_if_op
Expand All @@ -42,10 +44,13 @@
from nncf.quantization.algorithms.accuracy_control.evaluator import Evaluator
from nncf.quantization.algorithms.post_training.algorithm import PostTrainingQuantization
from nncf.quantization.algorithms.weight_compression.algorithm import WeightCompression
from nncf.quantization.quantize_model import BATCHWISE_STATISTICS_WARNING
from nncf.quantization.quantize_model import is_model_no_batchwise_support
from nncf.quantization.quantize_model import quantize_with_tune_hyperparams
from nncf.quantization.quantize_model import warning_model_no_batchwise_support
from nncf.quantization.telemetry_extractors import CompressionStartedWithQuantizeApi
from nncf.scopes import IgnoredScope
from nncf.scopes import get_ignored_scope_match
from nncf.scopes import validate_ignored_scope
from nncf.telemetry.decorator import tracked_function
from nncf.telemetry.events import NNCF_OV_CATEGORY

Expand All @@ -72,6 +77,22 @@ def native_quantize_if_op_impl(
raise NotImplementedError(
"The BiasCorrection algorithm is not supported for OpenVINO models with If operation."
)
graphs = {}

def _get_all_graphs(model: ov.Model, current_cnt: int):
AlexanderDokuchaev marked this conversation as resolved.
Show resolved Hide resolved
graphs[current_cnt] = NNCFGraphFactory.create(model)
KodiaqQ marked this conversation as resolved.
Show resolved Hide resolved
for op in model.get_ops():
if get_node_metatype(op) == OVIfMetatype:
current_cnt = _get_all_graphs(op.get_function(0), current_cnt + 1)
current_cnt = _get_all_graphs(op.get_function(1), current_cnt + 1)
return current_cnt

_get_all_graphs(model, 1)
KodiaqQ marked this conversation as resolved.
Show resolved Hide resolved
if ignored_scope and ignored_scope.validate:
validate_ignored_scope(ignored_scope, get_ignored_scope_match(ignored_scope, graphs.values())[0])
KodiaqQ marked this conversation as resolved.
Show resolved Hide resolved
ignored_scope = IgnoredScope(
ignored_scope.names, ignored_scope.patterns, ignored_scope.types, ignored_scope.subgraphs, validate=False
)
quantization_algorithm = PostTrainingQuantization(
mode=mode,
preset=preset,
Expand All @@ -82,17 +103,17 @@ def native_quantize_if_op_impl(
ignored_scope=ignored_scope,
advanced_parameters=advanced_parameters,
)

graph = GraphConverter.create_nncf_graph(model)
warning_model_no_batchwise_support(graph, advanced_parameters, model_type, OPERATIONS_OUTPUT_HAS_NO_BATCH_AXIS)
for graph in graphs.values():
if is_model_no_batchwise_support(graph, advanced_parameters, model_type, OPERATIONS_OUTPUT_HAS_NO_BATCH_AXIS):
nncf_logger.warning(BATCHWISE_STATISTICS_WARNING)
break
if_ops_number = get_number_if_op(model)
all_models_number = if_ops_number * 2 + 1
nncf_logger.info(
f"The model consists of {if_ops_number} If node(-s) with then and else bodies. \
Main model and all If bodies will be quantized recursively."
)
quantized_model, _ = apply_algorithm_if_bodies(
quantization_algorithm, model, graph, calibration_dataset, subset_size, 1, all_models_number
quantization_algorithm, model, graphs, calibration_dataset, subset_size, 1
alexsu52 marked this conversation as resolved.
Show resolved Hide resolved
)

if is_weight_compression_needed(advanced_parameters):
Expand Down Expand Up @@ -140,7 +161,8 @@ def native_quantize_impl(
advanced_parameters=advanced_parameters,
)
graph = GraphConverter.create_nncf_graph(model)
warning_model_no_batchwise_support(graph, advanced_parameters, model_type, OPERATIONS_OUTPUT_HAS_NO_BATCH_AXIS)
if is_model_no_batchwise_support(graph, advanced_parameters, model_type, OPERATIONS_OUTPUT_HAS_NO_BATCH_AXIS):
nncf_logger.warning(BATCHWISE_STATISTICS_WARNING)
quantized_model = quantization_algorithm.apply(model, graph, dataset=calibration_dataset)

if is_weight_compression_needed(advanced_parameters):
Expand Down
10 changes: 4 additions & 6 deletions nncf/quantization/quantize_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from nncf.common.deprecation import warning_deprecated
from nncf.common.graph import NNCFGraph
from nncf.common.graph.operator_metatypes import OperatorMetatype
from nncf.common.logging.logger import nncf_logger
from nncf.common.quantization.structs import QuantizationPreset
from nncf.common.utils.api_marker import api
from nncf.common.utils.backend import BackendType
Expand Down Expand Up @@ -47,26 +46,25 @@
)


def warning_model_no_batchwise_support(
def is_model_no_batchwise_support(
KodiaqQ marked this conversation as resolved.
Show resolved Hide resolved
graph: NNCFGraph,
advanced_quantization_parameters: Optional[AdvancedQuantizationParameters],
model_type: ModelType,
no_batchwise_support_metatypes: List[OperatorMetatype],
) -> None:
"""
Prints the warning message if batchwise statistics could lead to a significant accuracy drop.
Returns True if batchwise statistics could lead to a significant accuracy drop.

:param graph: Model's NNCFGraph.
:param advanced_quantization_parameters: AdvancedQuantizationParameters.
:param model_type: Model type algorithm option.
:param no_batchwise_support_metatypes: Meatypes having no batchwise statistics support.
"""
if (
return (
advanced_quantization_parameters
and advanced_quantization_parameters.batchwise_statistics
and (graph.get_nodes_by_metatypes(no_batchwise_support_metatypes) or model_type == ModelType.TRANSFORMER)
):
nncf_logger.warning(BATCHWISE_STATISTICS_WARNING)
)


def _update_advanced_quantization_parameters(
Expand Down
Loading
Loading