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

Algorithm.py refactoring #2013

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 1 addition & 3 deletions nncf/experimental/torch/quantization/quantize_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@ def quantize_impl(
advanced_parameters=advanced_parameters,
)

quantized_model = quantization_algorithm.apply(
nncf_network, nncf_network.nncf.get_graph(), dataset=calibration_dataset
)
quantized_model = quantization_algorithm.apply(nncf_network, dataset=calibration_dataset)

quantized_model.nncf.disable_dynamic_graph_building()

Expand Down
4 changes: 1 addition & 3 deletions nncf/onnx/quantization/quantize_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from nncf.common.logging.logger import nncf_logger
from nncf.common.quantization.structs import QuantizationPreset
from nncf.data import Dataset
from nncf.onnx.graph.nncf_graph_builder import GraphConverter
from nncf.parameters import ModelType
from nncf.parameters import TargetDevice
from nncf.quantization.advanced_parameters import AdvancedQuantizationParameters
Expand Down Expand Up @@ -66,7 +65,6 @@ def quantize_impl(
advanced_parameters=advanced_parameters,
)

graph = GraphConverter.create_nncf_graph(model)
quantized_model = quantization_algorithm.apply(model, graph, dataset=calibration_dataset)
quantized_model = quantization_algorithm.apply(model, dataset=calibration_dataset)

return quantized_model
4 changes: 1 addition & 3 deletions nncf/openvino/quantization/quantize_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from nncf.common.logging import nncf_logger
from nncf.common.quantization.structs import QuantizationPreset
from nncf.data import Dataset
from nncf.openvino.graph.nncf_graph_builder import GraphConverter
from nncf.openvino.quantization.backend_parameters import BackendParameters
from nncf.openvino.quantization.backend_parameters import is_weight_compression_needed
from nncf.parameters import DropType
Expand Down Expand Up @@ -112,8 +111,7 @@ def native_quantize_impl(
advanced_parameters=advanced_parameters,
)

graph = GraphConverter.create_nncf_graph(model)
quantized_model = quantization_algorithm.apply(model, graph, dataset=calibration_dataset)
quantized_model = quantization_algorithm.apply(model, dataset=calibration_dataset)

if is_weight_compression_needed(advanced_parameters):
compress_quantize_weights_transformation(quantized_model)
Expand Down
36 changes: 27 additions & 9 deletions nncf/quantization/algorithms/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from abc import ABC
from abc import abstractmethod
from typing import Dict, Optional, TypeVar
from typing import Dict, TypeVar

from nncf import Dataset
from nncf.common.graph.graph import NNCFGraph
Expand All @@ -36,20 +36,13 @@ def available_backends(self) -> Dict[str, BackendType]:
"""

@abstractmethod
def apply(
self,
model: TModel,
graph: NNCFGraph,
statistic_points: Optional[StatisticPointsContainer] = None,
dataset: Optional[Dataset] = None,
) -> TModel:
def apply(self, model: TModel, graph: NNCFGraph, statistic_points: StatisticPointsContainer) -> TModel:
"""
Applies the algorithm to the model.

:param model: Model for applying algorithm.
:param graph: Model graph.
:param statistic_points: Statistic points with collected statistics values.
:param dataset: A representative dataset for the calibration process.
:return: A resulting model.
"""

Expand All @@ -62,3 +55,28 @@ def get_statistic_points(self, model: TModel, graph: NNCFGraph) -> StatisticPoin
:param graph: Model graph.
:retrun: Statistic points, for which StatisticsCollector should collect statistics.
"""


class QuantizationAlgorithm(ABC):
"""
Facade of quantization algorithms.
"""

@property
@abstractmethod
def available_backends(self) -> Dict[str, BackendType]:
"""
Returns dictionary of the available backends for the algorithm.

:return: Dict of backends supported by the algorithm.
"""

@abstractmethod
def apply(self, model: TModel, dataset: Dataset) -> TModel:
"""
Applies the algorithm to the model.

:param model: Model for applying algorithm.
:param dataset: A representative dataset for the calibration process.
:return: A resulting model.
"""
15 changes: 2 additions & 13 deletions nncf/quantization/algorithms/bias_correction/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@
# limitations under the License.

from collections import defaultdict
from typing import Any, Dict, List, Optional, Tuple, TypeVar
from typing import Dict, List, Tuple, TypeVar

import numpy as np
from tqdm import tqdm

from nncf import Dataset
from nncf import nncf_logger
from nncf.common.factory import EngineFactory
from nncf.common.factory import ModelTransformerFactory
Expand Down Expand Up @@ -70,7 +69,6 @@ def __init__(
threshold: float = BIAS_CORRECTION_THRESHOLD,
apply_for_all_nodes: bool = False,
inplace_statistics: bool = True,
backend_params: Optional[Dict[str, Any]] = None,
):
"""
:param subset_size: Size of a subset for the statistics collection,
Expand All @@ -86,15 +84,12 @@ def __init__(
:param inplace_statistics: Defines wheather to calculate quantizers statistics
by backend graph operations or by default Python implementation, defaults
to True.
:param backend_params: Backend specific parameters.
"""
super().__init__()
self.subset_size = subset_size
self.threshold = threshold
self.apply_for_all_nodes = apply_for_all_nodes
self.inplace_statistics = inplace_statistics
self.backend_params = backend_params
self.nncf_graph = None
self._backend_entity = None
self._collected_stat_inputs_map = {}
self._fp_inputs = defaultdict(list)
Expand Down Expand Up @@ -127,13 +122,7 @@ def _set_backend_entity(self, model: TModel) -> None:
"Cannot return backend-specific entity because {} is not supported!".format(model_backend)
)

def apply(
self,
model: TModel,
graph: NNCFGraph,
statistic_points: Optional[StatisticPointsContainer] = None,
dataset: Optional[Dataset] = None,
) -> TModel:
def apply(self, model: TModel, graph: NNCFGraph, statistic_points: StatisticPointsContainer) -> TModel:
self._set_backend_entity(model)
model = self._backend_entity.insert_null_biases(model, graph)
main_transformations_layout = TransformationLayout()
Expand Down
14 changes: 2 additions & 12 deletions nncf/quantization/algorithms/channel_alignment/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Any, Dict, List, Optional, Tuple, TypeVar
from typing import Dict, List, Optional, Tuple, TypeVar

import numpy as np
from tqdm import tqdm

from nncf import Dataset
from nncf.common.factory import CommandCreatorFactory
from nncf.common.factory import ModelTransformerFactory
from nncf.common.graph.graph import NNCFGraph
Expand Down Expand Up @@ -59,20 +58,17 @@ def __init__(
self,
subset_size: int = 100,
inplace_statistics: bool = True,
backend_params: Optional[Dict[str, Any]] = None,
):
"""
:param subset_size: Size of a subset for the statistics collection,
defaults to 100.
:param inplace_statistics: Defines wheather to calculate quantizers statistics
by backend graph operations or by default Python implementation, defaults
to True.
:param backend_params: Backend specific parameters.
"""
super().__init__()
self.subset_size = subset_size
self.inplace_statistics = inplace_statistics
self.backend_params = backend_params
self._backend_entity = None
self._quantile = 1e-4
self._algorithm_key = f"CA_{hash(self)}"
Expand All @@ -93,13 +89,7 @@ def _set_backend_entity(self, model: TModel) -> None:

self._backend_entity = OVChannelAlignmentAlgoBackend()

def apply(
self,
model: TModel,
graph: NNCFGraph,
statistic_points: Optional[StatisticPointsContainer] = None,
dataset: Optional[Dataset] = None,
) -> TModel:
def apply(self, model: TModel, graph: NNCFGraph, statistic_points: StatisticPointsContainer) -> TModel:
self._set_backend_entity(model)
model_transformer = ModelTransformerFactory.create(model)
transformation_layout = TransformationLayout()
Expand Down
14 changes: 2 additions & 12 deletions nncf/quantization/algorithms/fast_bias_correction/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Any, Dict, List, Optional, Tuple, TypeVar, Union
from typing import Dict, List, Tuple, TypeVar, Union

from tqdm import tqdm

from nncf import Dataset
from nncf.common.factory import EngineFactory
from nncf.common.factory import ModelTransformerFactory
from nncf.common.graph.graph import NNCFGraph
Expand Down Expand Up @@ -58,7 +57,6 @@ def __init__(
threshold: float = FAST_BIAS_CORRECTION_THRESHOLD,
apply_for_all_nodes: bool = False,
inplace_statistics: bool = True,
backend_params: Optional[Dict[str, Any]] = None,
):
"""
:param subset_size: Size of a subset for the statistics collection,
Expand All @@ -74,14 +72,12 @@ def __init__(
:param inplace_statistics: Defines wheather to calculate quantizers statistics
by backend graph operations or by default Python implementation, defaults
to True.
:param backend_params: Backend specific parameters.
"""
super().__init__()
self.subset_size = subset_size
self.threshold = threshold
self.apply_for_all_nodes = apply_for_all_nodes
self.inplace_statistics = inplace_statistics
self.backend_params = backend_params
self._backend_entity = None
self._algorithm_key = f"FBC_{hash(self)}"

Expand Down Expand Up @@ -118,13 +114,7 @@ def _set_backend_entity(self, model: TModel) -> None:
"Cannot return backend-specific entity because {} is not supported!".format(model_backend)
)

def apply(
self,
model: TModel,
graph: NNCFGraph,
statistic_points: Optional[StatisticPointsContainer] = None,
dataset: Optional[Dataset] = None,
) -> TModel:
def apply(self, model: TModel, graph: NNCFGraph, statistic_points: StatisticPointsContainer) -> TModel:
self._set_backend_entity(model)

model_transformer = ModelTransformerFactory.create(model)
Expand Down
14 changes: 2 additions & 12 deletions nncf/quantization/algorithms/min_max/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
import collections
import dataclasses
from copy import deepcopy
from typing import Any, Dict, List, Optional, OrderedDict, Set, TypeVar
from typing import Dict, List, Optional, OrderedDict, Set, TypeVar

import numpy as np

from nncf import Dataset
from nncf.common.factory import ModelTransformerFactory
from nncf.common.graph.graph import NNCFGraph
from nncf.common.graph.graph import NNCFNode
Expand Down Expand Up @@ -109,7 +108,6 @@ def __init__(
weights_quantization_params: Optional[QuantizationParameters] = None,
activations_range_estimator_params: Optional[RangeEstimatorParameters] = None,
weights_range_estimator_params: Optional[RangeEstimatorParameters] = None,
backend_params: Optional[Dict[str, Any]] = None,
):
"""
:param preset: A preset that controls the quantization mode,
Expand Down Expand Up @@ -137,7 +135,6 @@ def __init__(
parameters for activation.
:param weights_range_estimator_params: Quantization range estimation parameters
for weights.
:param backend_params: Backend specific parameters.
"""
self._target_device = target_device
self._subset_size = subset_size
Expand All @@ -146,7 +143,6 @@ def __init__(
self._overflow_fix = overflow_fix
self._quantize_outputs = quantize_outputs
self._inplace_statistics = inplace_statistics
self._backend_params = backend_params

self._quantization_params = {
QuantizerGroup.WEIGHTS: weights_quantization_params,
Expand Down Expand Up @@ -599,13 +595,7 @@ def _get_quantization_points_overflow_fix(
output.update(nodes)
return output

def apply(
self,
model: TModel,
graph: NNCFGraph,
statistic_points: Optional[StatisticPointsContainer] = None,
dataset: Optional[Dataset] = None,
) -> TModel:
def apply(self, model: TModel, graph: NNCFGraph, statistic_points: StatisticPointsContainer) -> TModel:
transformation_layout = TransformationLayout()
model_transformer = ModelTransformerFactory.create(model)
quantization_target_points, unified_scale_groups = self._get_quantization_target_points(model, graph)
Expand Down
Loading