code: def compute_sparsity_mask2compact(
      compact_model: Module,
      compact_model_masks: Dict[str, Dict[str, Tensor]],
      config_list: List[Dict],
  ):
      """
      Apply masks on compact model, return the sparsity of each group mentioned in config list.
      A group means all layer mentioned in one config.
      This function count all zero elements of the masks in one group,
      then divide by the elements number of the weights in this group to compute sparsity.
      """
      mask2compact_sparsity = []
      for config in config_list:
          left_weight_num = 0
          total_weight_num = 0
          for module_name, module in compact_model.named_modules():
              module_type = type(module).__name__
              if (
                  "op_types" in config
                  and module_type not in config["op_types"]
              ):
                  continue
              if (
                  "op_names" in config
                  and module_name not in config["op_names"]
              ):
                  continue
              module_weight_num = module.weight.data.numel()  # type: ignore
              total_weight_num += module_weight_num
              if module_name in compact_model_masks:
                  weight_mask = compact_model_masks[module_name][
                      "weight"
                  ]
                  left_weight_num += len(
                      torch.nonzero(weight_mask, as_tuple=False)
                  )
              else:
                  left_weight_num += module_weight_num
          mask2compact_sparsity.append(deepcopy(config))
          mask2compact_sparsity[-1]["total_sparsity"] = (
              1 - left_weight_num / total_weight_num
          )
      return mask2compact_sparsity
  
file_path: nni/nni/compression/utils/mask_counter.py
imports: from copy import deepcopy
  import math
  from typing import Dict, List, Tuple
  import torch
  from torch import Tensor
  from torch.nn import Module
label_desc: Use image node_id 2 for calling a global function with example usage: compute_sparsity_mask2compact(compact_model, compact_model_masks, config_list) and returns: mask2compact_sparsity