code: def _generate_dependency_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.dependency_group_id].append((module_name, target_name, target_space))  # type: ignore
  
      masks = defaultdict(dict)
      for _, group in groups.items():
          block_numbers = [1]
          group_sparsity_ratio = None
          filtered_metrics = defaultdict(dict)
  
          for module_name, target_name, target_space in group:
              assert (
                  target_space.internal_metric_block is None
                  or isinstance(target_space.internal_metric_block, int)
              )
              block_numbers.append(
                  target_space.internal_metric_block
                  if target_space.internal_metric_block
                  else 1
              )
              if target_space.sparse_ratio is not None:
                  if group_sparsity_ratio is None:
                      group_sparsity_ratio = target_space.sparse_ratio
                  else:
                      assert (
                          group_sparsity_ratio
                          == target_space.sparse_ratio
                      )
              filtered_metrics[module_name][target_name] = metrics[
                  module_name
              ][target_name]
          block_number = reduce(numpy.lcm, block_numbers)
          assert group_sparsity_ratio is not None
          group_metric = _metric_fuse(filtered_metrics)
          group_mask = _ratio_mask(
              group_metric,
              group_sparsity_ratio,
              view_size=[block_number, -1],
          )
  
          for module_name, target_name, _ in group:
              masks[module_name][target_name] = group_mask.clone()
  
      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 7 for calling a global function with example usage: _generate_dependency_sparsity(metrics, target_spaces) and returns: masks