code: def mask_to_rle_pytorch(tensor: torch.Tensor) -> List[Dict[str, Any]]:
      """
      Encodes masks to an uncompressed RLE, in the format expected by
      pycoco tools.
      """
      # Put in fortran order and flatten h,w
      b, h, w = tensor.shape
      tensor = tensor.permute(0, 2, 1).flatten(1)
  
      # Compute change indices
      diff = tensor[:, 1:] ^ tensor[:, :-1]
      change_indices = diff.nonzero()
  
      # Encode run length
      out = []
      for i in range(b):
          cur_idxs = change_indices[change_indices[:, 0] == i, 1]
          cur_idxs = torch.cat(
              [
                  torch.tensor(
                      [0], dtype=cur_idxs.dtype, device=cur_idxs.device
                  ),
                  cur_idxs + 1,
                  torch.tensor(
                      [h * w],
                      dtype=cur_idxs.dtype,
                      device=cur_idxs.device,
                  ),
              ]
          )
          btw_idxs = cur_idxs[1:] - cur_idxs[:-1]
          counts = [] if tensor[i, 0] == 0 else [0]
          counts.extend(btw_idxs.detach().cpu().tolist())
          out.append({"size": [h, w], "counts": counts})
      return out
  
imports: import numpy
  import torch
  import math
  from copy import deepcopy
  from itertools import product
  from typing import Any, Dict, Generator, ItemsView, List, Tuple
label_desc: Use image node_id 4 for calling a global function with example usage: mask_to_rle_pytorch(tensor) and returns: out