code: def _generate_global_sparsity(
      metrics: _METRICS, target_spaces: _PRUNING_TARGET_SPACES
  ) -> _MASKS:
      groups: Dict[
          str, List[Tuple[str, str, PruningTargetSpace]]
      ] = defaultdict(list)
      for module_name, ts in target_spaces.items():
          for target_name, target_space in ts.items():
              groups[target_space.global_group_id].append((module_name, target_name, target_space))  # type: ignore
  
      masks = defaultdict(dict)
      for _, group in groups.items():
          group_sparse_ratio = None
          for _, _, target_space in group:
              if target_space.sparse_ratio is not None:
                  if group_sparse_ratio is None:
                      group_sparse_ratio = target_space.sparse_ratio
                  else:
                      assert (
                          group_sparse_ratio
                          == target_space.sparse_ratio
                      )
          assert group_sparse_ratio is not None
  
          # at least how many elements to mask
          sparse_number_low = 0
          # at most how many elements to mask
          sparse_number_high = 0
          # how many elements in this group
          total_element_number = 0
          for _, _, target_space in group:
              element_number = target_space.target.numel()  # type: ignore
              total_element_number += element_number
              sparse_number_low += (
                  int(element_number * target_space.min_sparse_ratio)
                  if target_space.min_sparse_ratio
                  else 0
              )
              sparse_number_high += (
                  int(element_number * target_space.max_sparse_ratio)
                  if target_space.max_sparse_ratio
                  else element_number
              )
          # how many elements should be masked, controlled by sparse_ratio
          sparse_number = int(total_element_number * group_sparse_ratio)
  
          if sparse_number <= sparse_number_low:
              # directly generate masks with target_space.min_sparse_ratio
              for module_name, target_name, target_space in group:
                  sparse_ratio = (
                      target_space.min_sparse_ratio
                      if target_space.min_sparse_ratio
                      else 0.0
                  )
                  masks[module_name][target_name] = _ratio_mask(
                      metrics[module_name][target_name], sparse_ratio
                  )
              continue
  
          if sparse_number >= sparse_number_high:
              # directly generate masks with target_space.max_sparse_ratio
              for module_name, target_name, target_space in group:
                  sparse_ratio = (
                      target_space.max_sparse_ratio
                      if target_space.max_sparse_ratio
                      else 0.0
                  )
                  masks[module_name][target_name] = _ratio_mask(
                      metrics[module_name][target_name], sparse_ratio
                  )
              continue
  
          sparse_threshold = _global_threshold_generate(
              metrics, group, sparse_number
          )
          for module_name, target_name, target_space in group:
              masks[module_name][target_name] = _threshold_mask(
                  metrics[module_name][target_name], sparse_threshold
              )
          continue
      return masks
  
imports: from __future__ import annotations
  from collections import defaultdict
  from functools import reduce
  import heapq
  from typing import Callable, Dict, List, Tuple
  import numpy
  import torch
  from .calculate_metrics import _METRICS
  from ...base.compressor import _PRUNING_TARGET_SPACES
  from ...base.target_space import PruningTargetSpace, TargetType
label_desc: Use image node_id 6 for calling a global function with example usage: _generate_global_sparsity(metrics, target_spaces) and returns: masks