Skip to content

Commit

Permalink
Fix index out of range (#2224)
Browse files Browse the repository at this point in the history
### 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
  • Loading branch information
andrey-churkin authored Oct 26, 2023
1 parent 4f0a6de commit 76bb1f0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
"""
Expand All @@ -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
33 changes: 19 additions & 14 deletions nncf/quantization/algorithms/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -178,22 +179,26 @@ 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:
container.add_statistic_point(statistic_point)

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

0 comments on commit 76bb1f0

Please sign in to comment.