From 76bb1f085ef21fe000ba3726115bf4c84d6ac937 Mon Sep 17 00:00:00 2001 From: Andrey Churkin Date: Thu, 26 Oct 2023 15:09:16 +0100 Subject: [PATCH] Fix index out of range (#2224) ### Changes Fix index out of range ### Reason for changes File "nncf/nncf/quantization/algorithms/pipeline.py", line 119, in run_step current_model = pipeline_step[-1].apply(current_model, current_graph, step_statistics) IndexError: list index out of range ### Related tickets N/A ### Tests N/A --- .../hyperparameter_tuner/param_grid.py | 8 +++-- nncf/quantization/algorithms/pipeline.py | 33 +++++++++++-------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/nncf/quantization/algorithms/hyperparameter_tuner/param_grid.py b/nncf/quantization/algorithms/hyperparameter_tuner/param_grid.py index 4874fc80b42..cd621295f2f 100644 --- a/nncf/quantization/algorithms/hyperparameter_tuner/param_grid.py +++ b/nncf/quantization/algorithms/hyperparameter_tuner/param_grid.py @@ -13,6 +13,7 @@ from typing import Any, Dict, List from nncf.common.quantization.structs import QuantizationPreset +from nncf.common.utils.backend import BackendType from nncf.quantization.algorithms.bias_correction.algorithm import BiasCorrection from nncf.quantization.algorithms.channel_alignment.algorithm import ChannelAlignment from nncf.quantization.algorithms.fast_bias_correction.algorithm import FastBiasCorrection @@ -89,7 +90,7 @@ def _get_bias_correction_param_grid() -> ParamGrid: return {"fast_bias_correction": [True, False]} -def get_quantization_param_grids(pipeline: Pipeline) -> List[ParamGrid]: +def get_quantization_param_grids(pipeline: Pipeline, backend: BackendType) -> List[ParamGrid]: """ Returns params grid for post-training quantization algorithm. """ @@ -105,7 +106,10 @@ def get_quantization_param_grids(pipeline: Pipeline) -> List[ParamGrid]: for step in pipeline.pipeline_steps: param_grid = {} for algorithm in step: + if backend not in algorithm.available_backends: + continue param_grid.update(algorithm_cls_to_param_grid[algorithm.__class__]) - param_grids.append(param_grid) + if param_grid: + param_grids.append(param_grid) return param_grids diff --git a/nncf/quantization/algorithms/pipeline.py b/nncf/quantization/algorithms/pipeline.py index 1b5db8a8614..2c02e3753c6 100644 --- a/nncf/quantization/algorithms/pipeline.py +++ b/nncf/quantization/algorithms/pipeline.py @@ -111,8 +111,8 @@ def run_step( current_model = model current_graph = graph - pipeline_step = self.pipeline_steps[step_index] - pipeline_step = self._remove_unsupported_algorithms(pipeline_step, get_backend(current_model)) + pipeline_steps = self._remove_unsupported_algorithms(get_backend(model)) + pipeline_step = pipeline_steps[step_index] for algorithm in pipeline_step[:-1]: current_model = algorithm.apply(current_model, current_graph, step_statistics) current_graph = NNCFGraphFactory.create(current_model) @@ -142,13 +142,14 @@ def run_from_step( :return: The updated model after executing the pipeline from the specified pipeline step to the end. """ + pipeline_steps = self._remove_unsupported_algorithms(get_backend(model)) if step_index_to_statistics is None: step_index_to_statistics = {} # The `step_model` and `step_graph` entities are required to execute `step_index`-th pipeline step step_model = model step_graph = graph - for step_index in range(start_step_index, len(self.pipeline_steps)): + for step_index in range(start_step_index, len(pipeline_steps)): # Create graph required to run current pipeline step if step_graph is None: step_graph = NNCFGraphFactory.create(step_model) @@ -178,8 +179,8 @@ def get_statistic_points_for_step( :return: Statistics that should be collected to execute `step_index`-th pipeline step. """ container = StatisticPointsContainer() - pipeline_step = self.pipeline_steps[step_index] - pipeline_step = self._remove_unsupported_algorithms(pipeline_step, get_backend(model)) + pipeline_steps = self._remove_unsupported_algorithms(get_backend(model)) + pipeline_step = pipeline_steps[step_index] for algorithm in pipeline_step: for statistic_points in algorithm.get_statistic_points(model, graph).values(): for statistic_point in statistic_points: @@ -187,13 +188,17 @@ def get_statistic_points_for_step( return container - @staticmethod - def _remove_unsupported_algorithms(pipeline_step: PipelineStep, backend: BackendType) -> PipelineStep: - step = [] - for algorithm in pipeline_step: - if backend not in algorithm.available_backends: - nncf_logger.debug(f"{backend.name} does not support {algorithm.__class__.__name__} algorithm yet.") - continue - step.append(algorithm) + def _remove_unsupported_algorithms(self, backend: BackendType) -> List[PipelineStep]: + pipeline_steps = [] + for pipeline_step in self._pipeline_steps: + step = [] + for algorithm in pipeline_step: + if backend not in algorithm.available_backends: + nncf_logger.debug(f"{backend.name} does not support {algorithm.__class__.__name__} algorithm yet.") + continue + step.append(algorithm) + + if step: + pipeline_steps.append(step) - return step + return pipeline_steps