Compression Utilities

SensitivityAnalysis

ChannelDependency

GroupDependency

ChannelMaskConflict

GroupMaskConflict

count_flops_params

compute_sparsity

nni.algorithms.compression.v2.pytorch.utils.pruning.compute_sparsity(origin_model, compact_model, compact_model_masks, config_list)[source]

This function computes how much the origin model has been compressed in the current state. The current state means compact_model + compact_model_masks (i.e., compact_model_masks applied on compact_model). The compact model is the origin model after pruning, and it may have different structure with origin_model cause of speedup.

Parameters:
  • origin_model (torch.nn.Module) – The original un-pruned model.

  • compact_model (torch.nn.Module) – The model after speedup or original model.

  • compact_model_masks (Dict[str, Dict[str, Tensor]]) – The masks applied on the compact model, if the original model have been speedup, this should be {}.

  • config_list (List[Dict]) – The config_list used by pruning the original model.

Returns:

(current2origin_sparsity, compact2origin_sparsity, mask2compact_sparsity). current2origin_sparsity is how much the origin model has been compressed in the current state. compact2origin_sparsity is the sparsity obtained by comparing the structure of origin model and compact model. mask2compact_sparsity is the sparsity computed by count the zero value in the mask.

Return type:

Tuple[List[Dict], List[Dict], List[Dict]]