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

Add Pipeline class #2140

Closed
Closed
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
8 changes: 3 additions & 5 deletions nncf/experimental/torch/quantization/quantize_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from nncf.parameters import ModelType
from nncf.parameters import TargetDevice
from nncf.quantization.advanced_parameters import AdvancedQuantizationParameters
from nncf.quantization.algorithms.post_training.algorithm import PostTrainingQuantization
from nncf.quantization.pipelines.post_training.pipeline import create_ptq_pipeline
from nncf.scopes import IgnoredScope
from nncf.torch.dynamic_graph.context import no_nncf_trace
from nncf.torch.dynamic_graph.io_handling import replicate_same_tensors
Expand Down Expand Up @@ -105,7 +105,7 @@ def quantize_impl(

nncf_network = create_nncf_network(model.eval(), calibration_dataset)

quantization_algorithm = PostTrainingQuantization(
quantization_pipeline = create_ptq_pipeline(
preset=preset,
target_device=target_device,
subset_size=subset_size,
Expand All @@ -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_pipeline.run(nncf_network, calibration_dataset)

quantized_model.nncf.disable_dynamic_graph_building()

Expand Down
8 changes: 3 additions & 5 deletions nncf/onnx/quantization/quantize_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
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
from nncf.quantization.algorithms.post_training.algorithm import PostTrainingQuantization
from nncf.quantization.pipelines.post_training.pipeline import create_ptq_pipeline
from nncf.quantization.telemetry_extractors import CompressionStartedWithQuantizeApi
from nncf.scopes import IgnoredScope
from nncf.telemetry import tracked_function
Expand Down Expand Up @@ -56,7 +55,7 @@ def quantize_impl(
advanced_parameters.weights_quantization_params.per_channel = False
advanced_parameters.activations_quantization_params.per_channel = False

quantization_algorithm = PostTrainingQuantization(
quantization_pipeline = create_ptq_pipeline(
preset=preset,
target_device=target_device,
subset_size=subset_size,
Expand All @@ -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_pipeline.run(model, calibration_dataset)

return quantized_model
8 changes: 3 additions & 5 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.openvino.quantization.weights_compression import insert_pre_compression_operations
Expand All @@ -32,7 +31,7 @@
from nncf.quantization.algorithms.accuracy_control.algorithm import QuantizationAccuracyRestorer
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.pipelines.post_training.pipeline import create_ptq_pipeline
from nncf.quantization.quantize_model import quantize_with_tune_hyperparams
from nncf.quantization.telemetry_extractors import CompressionStartedWithQuantizeApi
from nncf.scopes import IgnoredScope
Expand Down Expand Up @@ -106,7 +105,7 @@ def native_quantize_impl(
"""
Implementation of the `quantize()` method for the OpenVINO backend via the OpenVINO Runtime API.
"""
quantization_algorithm = PostTrainingQuantization(
quantization_algorithm = create_ptq_pipeline(
preset=preset,
target_device=target_device,
subset_size=subset_size,
Expand All @@ -116,8 +115,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.run(model, calibration_dataset)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quantization_pipeline.run? For the consistency.


if is_weight_compression_needed(advanced_parameters):
compress_quantize_weights_transformation(quantized_model)
Expand Down
17 changes: 15 additions & 2 deletions nncf/quantization/algorithms/channel_alignment/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from nncf.common.graph.transformations.commands import TargetPoint
from nncf.common.graph.transformations.commands import TargetType
from nncf.common.graph.transformations.layout import TransformationLayout
from nncf.common.logging import nncf_logger
from nncf.common.logging.track_progress import track
from nncf.common.tensor_statistics.statistic_point import StatisticPoint
from nncf.common.tensor_statistics.statistic_point import StatisticPointsContainer
Expand Down Expand Up @@ -97,6 +98,12 @@ def apply(
statistic_points: Optional[StatisticPointsContainer] = None,
dataset: Optional[Dataset] = None,
) -> TModel:
if model is not None:
backend = get_backend(model)
if backend != BackendType.OPENVINO:
nncf_logger.debug(f"{backend.name} does not support ChannelAlignment algorithm yet.")
return model

self._set_backend_entity(model)
model_transformer = ModelTransformerFactory.create(model)
transformation_layout = TransformationLayout()
Expand Down Expand Up @@ -368,9 +375,15 @@ def _get_target_point_and_node_in(self, conv_in, add_in) -> Tuple[TargetPoint, N
)

def get_statistic_points(self, model: TModel, graph: NNCFGraph) -> StatisticPointsContainer:
self._set_backend_entity(model)

statistic_container = StatisticPointsContainer()

if model is not None:
backend = get_backend(model)
if backend != BackendType.OPENVINO:
nncf_logger.debug(f"{backend.name} does not support ChannelAlignment algorithm yet.")
return statistic_container

self._set_backend_entity(model)
for conv_in, add_in, _ in self._get_node_pairs(graph):
target_point, node_in = self._get_target_point_and_node_in(conv_in, add_in)
channel_axis = conv_in.metatype.output_channel_axis
Expand Down
215 changes: 0 additions & 215 deletions nncf/quantization/algorithms/post_training/algorithm.py

This file was deleted.

10 changes: 10 additions & 0 deletions nncf/quantization/algorithms/smooth_quant/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ def apply(
statistic_points: Optional[StatisticPointsContainer] = None,
dataset: Optional[Dataset] = None,
) -> TModel:
backend = get_backend(model)
if backend != BackendType.OPENVINO:
nncf_logger.debug(f"{backend.name} does not support SmoothQuant algorithm yet.")
return model

self._set_backend_entity(model)

nodes_to_smooth_data = self._get_nodes_to_smooth_data(graph)
Expand Down Expand Up @@ -221,6 +226,11 @@ def filter_func(point: StatisticPoint) -> bool:
def get_statistic_points(self, model: TModel, graph: NNCFGraph) -> StatisticPointsContainer:
statistic_container = StatisticPointsContainer()

backend = get_backend(model)
if backend != BackendType.OPENVINO:
nncf_logger.debug(f"{backend.name} does not support SmoothQuant algorithm yet.")
return statistic_container

self._set_backend_entity(model)

nodes_to_smooth_data = self._get_nodes_to_smooth_data(graph)
Expand Down
Loading