code: def compute_sparsity_compact2origin(
      origin_model: Module,
      compact_model: Module,
      config_list: List[Dict],
  ) -> List[Dict]:
      """
      Compare origin model and compact model, return the sparsity of each group mentioned in config list.
      A group means all layer mentioned in one config.
      e.g., a linear named 'linear1' and its weight size is [100, 100] in origin model, but in compact model,
      the layer weight size with same layer name is [100, 50],
      then this function will return [{'op_names': 'linear1', 'total_sparsity': 0.5}].
      """
      compact2origin_sparsity = []
      for config in config_list:
          left_weight_num = 0
          total_weight_num = 0
          for module_name, module in origin_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
              total_weight_num += module.weight.data.numel()  # type: ignore
          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
              left_weight_num += module.weight.data.numel()  # type: ignore
          compact2origin_sparsity.append(deepcopy(config))
          compact2origin_sparsity[-1]["total_sparsity"] = (
              1 - left_weight_num / total_weight_num
          )
      return compact2origin_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 1 for calling a global function with example usage: compute_sparsity_compact2origin(origin_model, compact_model, config_list) and returns: compact2origin_sparsity