code: def generate_sparsity(
      metrics: _METRICS, target_spaces: _PRUNING_TARGET_SPACES
  ) -> _MASKS:
      """
      There are many ways to generate masks, in this function, most of the common generation rules are implemented,
      and these rules execute in a certain order.
  
      The following rules are included in this function:
  
          * Threshold. If sparse_threshold is set in target space, the mask will be generated by metric >= threshold is 1,
          and metric < threshold is 0.
          * Dependency. If dependency_group_id is set in target space, the metrics of the targets in the same group will be
          meaned as the group metric, then if target_space.internal_metric_block is set, all internal_metric_block of the
          targets will be put in one set to compute the lcm as the group internal block number.
          Split the group metric to group internal block number parts, compute mask for each part and merge as a group mask.
          All targets in this group share the group mask value.
          * Global. If global_group_id is set in target space, the metrics of the targets in the same group will be global ranked
          and generate the mask by taking smaller metric values as 0, others as 1 by sparse_ratio.
          * Ratio. The most common rule, directly generate the mask by taking the smaller metric values as 0, others as 1 by sparse_ratio.
          * Align. If align is set in target space, the mask will be generated by another existed mask.
      """
  
      def condition_dependency(
          target_space: PruningTargetSpace,
      ) -> bool:
          return target_space.dependency_group_id is not None
  
      def condition_global(target_space: PruningTargetSpace) -> bool:
          return target_space.global_group_id is not None
  
      def condition_ratio(target_space: PruningTargetSpace) -> bool:
          return target_space.sparse_ratio is not None
  
      def condition_threshold(target_space: PruningTargetSpace) -> bool:
          return target_space.sparse_threshold is not None
  
      def condition_align(target_space: PruningTargetSpace) -> bool:
          return target_space.align is not None
  
      masks = defaultdict(dict)
  
      (
          threshold_target_spaces,
          remained_target_spaces,
      ) = target_spaces_filter(target_spaces, condition_threshold)
      update_masks = _generate_threshold_sparsity(
          metrics, threshold_target_spaces
      )
      _nested_multiply_update_masks(
          masks, _expand_masks(update_masks, threshold_target_spaces)
      )
  
      (
          dependency_target_spaces,
          remained_target_spaces,
      ) = target_spaces_filter(target_spaces, condition_dependency)
      update_masks = _generate_dependency_sparsity(
          metrics, dependency_target_spaces
      )
      _nested_multiply_update_masks(
          masks, _expand_masks(update_masks, dependency_target_spaces)
      )
  
      (
          global_target_spaces,
          remained_target_spaces,
      ) = target_spaces_filter(remained_target_spaces, condition_global)
      update_masks = _generate_global_sparsity(
          metrics, global_target_spaces
      )
      _nested_multiply_update_masks(
          masks, _expand_masks(update_masks, global_target_spaces)
      )
  
      (
          ratio_target_spaces,
          remained_target_spaces,
      ) = target_spaces_filter(remained_target_spaces, condition_ratio)
      update_masks = _generate_ratio_sparsity(
          metrics, ratio_target_spaces
      )
      _nested_multiply_update_masks(
          masks, _expand_masks(update_masks, ratio_target_spaces)
      )
  
      (
          align_target_spaces,
          remained_target_spaces,
      ) = target_spaces_filter(remained_target_spaces, condition_align)
      update_masks = _generate_align_sparsity(
          masks, align_target_spaces
      )
      _nested_multiply_update_masks(
          masks, _expand_masks(update_masks, align_target_spaces)
      )
  
      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 1 for calling a global function with example usage: generate_sparsity(metrics, target_spaces) and returns: masks, unknown, unknown, unknown, unknown, unknown