code: def _do_paste_mask(masks, boxes, img_h, img_w, skip_empty=True):
      """Paste instance masks acoording to boxes.
  
      This implementation is modified from
      https://github.com/facebookresearch/detectron2/
  
      Args:
          masks (Tensor): N, 1, H, W
          boxes (Tensor): N, 4
          img_h (int): Height of the image to be pasted.
          img_w (int): Width of the image to be pasted.
          skip_empty (bool): Only paste masks within the region that
              tightly bound all boxes, and returns the results this region only.
              An important optimization for CPU.
  
      Returns:
          tuple: (Tensor, tuple). The first item is mask tensor, the second one
              is the slice object.
          If skip_empty == False, the whole image will be pasted. It will
              return a mask of shape (N, img_h, img_w) and an empty tuple.
          If skip_empty == True, only area around the mask will be pasted.
              A mask of shape (N, h', w') and its start and end coordinates
              in the original image will be returned.
      """
      # On GPU, paste all masks together (up to chunk size)
      # by using the entire image to sample the masks
      # Compared to pasting them one by one,
      # this has more operations but is faster on COCO-scale dataset.
      device = masks.device
      if skip_empty:
          x0_int, y0_int = torch.clamp(
              boxes.min(dim=0).values.floor()[:2] - 1, min=0
          ).to(dtype=torch.int32)
          x1_int = torch.clamp(
              boxes[:, 2].max().ceil() + 1, max=img_w
          ).to(dtype=torch.int32)
          y1_int = torch.clamp(
              boxes[:, 3].max().ceil() + 1, max=img_h
          ).to(dtype=torch.int32)
      else:
          x0_int, y0_int = 0, 0
          x1_int, y1_int = img_w, img_h
      x0, y0, x1, y1 = torch.split(boxes, 1, dim=1)  # each is Nx1
  
      N = masks.shape[0]
  
      img_y = (
          torch.arange(
              y0_int, y1_int, device=device, dtype=torch.float32
          )
          + 0.5
      )
      img_x = (
          torch.arange(
              x0_int, x1_int, device=device, dtype=torch.float32
          )
          + 0.5
      )
      img_y = (img_y - y0) / (y1 - y0) * 2 - 1
      img_x = (img_x - x0) / (x1 - x0) * 2 - 1
      # img_x, img_y have shapes (N, w), (N, h)
      if torch.isinf(img_x).any():
          inds = torch.where(torch.isinf(img_x))
          img_x[inds] = 0
      if torch.isinf(img_y).any():
          inds = torch.where(torch.isinf(img_y))
          img_y[inds] = 0
  
      gx = img_x[:, None, :].expand(N, img_y.size(1), img_x.size(1))
      gy = img_y[:, :, None].expand(N, img_y.size(1), img_x.size(1))
      grid = torch.stack([gx, gy], dim=3)
  
      if torch.onnx.is_in_onnx_export():
          raise RuntimeError(
              "Exporting F.grid_sample from Pytorch to ONNX is not supported."
          )
      img_masks = F.grid_sample(
          masks.to(dtype=torch.float32), grid, align_corners=False
      )
  
      if skip_empty:
          return img_masks[:, 0], (
              slice(y0_int, y1_int),
              slice(x0_int, x1_int),
          )
      else:
          return img_masks[:, 0], ()
  
file_path: insightface/detection/scrfd/mmdet/models/roi_heads/mask_heads/fcn_mask_head.py
imports: import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from mmcv.cnn import Conv2d, ConvModule, build_upsample_layer
  from mmcv.ops.carafe import CARAFEPack
  from mmcv.runner import auto_fp16, force_fp32
  from torch.nn.modules.utils import _pair
  from mmdet.core import mask_target
  from mmdet.models.builder import HEADS, build_loss
label_desc: Use image node_id 1 for calling a global function with example usage: _do_paste_mask(masks, boxes, img_h, img_w, skip_empty) and returns: img_masks,  slice,  slice, img_masks,  