code: def _ratio_mask(
      metric: torch.Tensor,
      sparse_ratio: float,
      view_size: int | List[int] = -1,
  ):
      if sparse_ratio == 0.0:
          return torch.ones_like(metric)
  
      if sparse_ratio == 1.0:
          return torch.zeros_like(metric)
  
      assert 0.0 < sparse_ratio < 1.0
      if isinstance(view_size, int) or len(view_size[:-1]) == 0:
          block_number = 1
      else:
          block_number = numpy.prod(view_size[:-1])
      sparse_number_per_block = int(
          metric.numel() // block_number * sparse_ratio
      )
      viewed_metric = metric.view(view_size)
      _, indices = viewed_metric.topk(
          sparse_number_per_block, largest=False
      )
      return (
          torch.ones_like(viewed_metric)
          .scatter(-1, indices, 0.0)
          .reshape_as(metric)
      )
  
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 8 for calling a global function with example usage: _ratio_mask(metric, sparse_ratio, view_size) and returns: torch, torch, torch