nncf.torch.pruning.filter_pruning.algo#

Classes#

FilterPruningController

Serves as a handle to the additional modules, parameters and hooks inserted

class nncf.torch.pruning.filter_pruning.algo.FilterPruningController(target_model, prunable_types, pruned_module_groups, pruned_norms_operators, config)[source]#

Bases: nncf.torch.pruning.base_algo.BasePruningAlgoController

Serves as a handle to the additional modules, parameters and hooks inserted into the original uncompressed model in order to enable algorithm-specific compression. Hosts entities that are to be used during the training process, such as compression scheduler and compression loss.

Parameters:
  • target_model (nncf.torch.nncf_network.NNCFNetwork) –

  • prunable_types (List[str]) –

  • pruned_module_groups (nncf.common.pruning.clusterization.Clusterization[nncf.torch.pruning.structs.PrunedModuleInfo]) –

  • pruned_norms_operators (List[Tuple[nncf.common.graph.NNCFNode, nncf.torch.pruning.filter_pruning.layers.FilterPruningMask, torch.nn.Module]]) –

  • config (nncf.NNCFConfig) –

property loss: nncf.api.compression.CompressionLoss[source]#

The compression loss for this particular algorithm combination.

Return type:

nncf.api.compression.CompressionLoss

property scheduler: nncf.common.pruning.schedulers.PruningScheduler[source]#

The compression scheduler for this particular algorithm combination.

Return type:

nncf.common.pruning.schedulers.PruningScheduler

property pruning_level: float[source]#

Global pruning level in the model

Return type:

float

property current_groupwise_pruning_level: Dict[int, float][source]#

Return the dict of layer group id’s and corresponding current groupwise pruning levels in the model

Return type:

Dict[int, float]

property compression_rate[source]#

Returns a float compression rate value ranging from 0 to 1 (e.g. the sparsity level, or the ratio of filters pruned).

property maximal_compression_rate: float[source]#

Returns the maximal model compression rate supported by the compression controller.

Return type:

float

static get_mask(minfo)[source]#

Returns pruning mask for minfo.module.

Parameters:

minfo (nncf.torch.pruning.structs.PrunedModuleInfo) –

Return type:

torch.Tensor

statistics(quickly_collected_only=False)[source]#

Returns a Statistics class instance that contains compression algorithm statistics.

Parameters:

quickly_collected_only (bool) – Enables collection of the statistics that don’t take too much time to compute. Can be helpful for the case when need to keep track of statistics on each training batch/step/iteration.

Return type:

nncf.common.statistics.NNCFStatistics

set_pruning_level(pruning_level, run_batchnorm_adaptation=False)[source]#

Set the global or groupwise pruning level in the model. If pruning_level is a float, the correspoding global pruning level is set in the model, either in terms of the percentage of filters pruned or as the percentage of flops removed, the latter being true in case the “prune_flops” flag of the controller is set to True. If pruning_level is a dict, the keys should correspond to layer group id’s and the values to groupwise pruning level to be set in the model.

Parameters:
  • pruning_level (Union[float, Dict[int, float]]) –

  • run_batchnorm_adaptation (bool) –

Return type:

None

prepare_for_export()[source]#

Applies pruning masks to layer weights before exporting the model to ONNX.

compression_stage()[source]#

Returns the compression stage. Should be used on saving best checkpoints to distinguish between uncompressed, partially compressed, and fully compressed models.

Returns:

The compression stage of the target model.

Return type:

nncf.api.compression.CompressionStage

disable_scheduler()[source]#

Disables current compression scheduler during training by changing it to a dummy one that does not change the compression rate.

strip_model(model, do_copy=False)[source]#

Strips auxiliary layers that were used for the model compression, as it’s only needed for training. The method is used before exporting the model in the target format.

Parameters:
  • model (nncf.torch.nncf_network.NNCFNetwork) – The compressed model.

  • do_copy (bool) – Modify copy of the model, defaults to False.

Returns:

The stripped model.

Return type:

nncf.torch.nncf_network.NNCFNetwork