code: def sequence_mask(
      lengths: TensorType,
      maxlen: Optional[int] = None,
      dtype=None,
      time_major: bool = False,
  ) -> TensorType:
      """Offers same behavior as tf.sequence_mask for torch.
  
      Thanks to Dimitris Papatheodorou
      (https://discuss.pytorch.org/t/pytorch-equivalent-for-tf-sequence-mask/
      39036).
  
      Args:
          lengths: The tensor of individual lengths to mask by.
          maxlen: The maximum length to use for the time axis. If None, use
              the max of `lengths`.
          dtype: The torch dtype to use for the resulting mask.
          time_major: Whether to return the mask as [B, T] (False; default) or
              as [T, B] (True).
  
      Returns:
           The sequence mask resulting from the given input and parameters.
      """
      # If maxlen not given, use the longest lengths in the `lengths` tensor.
      if maxlen is None:
          maxlen = lengths.max()
  
      mask = torch.ones(tuple(lengths.shape) + (int(maxlen),))
  
      mask = ~(mask.to(lengths.device).cumsum(dim=1).t() > lengths)
      # Time major transformation.
      if not time_major:
          mask = mask.t()
  
      # By default, set the mask to be boolean.
      mask.type(dtype or torch.bool)
  
      return mask
  
imports: import logging
  import os
  import warnings
  from typing import Dict, List, Optional, TYPE_CHECKING, Union
  import gymnasium
  import numpy
  import tree
  from gymnasium.spaces import Discrete, MultiDiscrete
  from packaging import version
  import ray
  from ray.rllib.models.repeated_values import RepeatedValues
  from ray.rllib.utils.annotations import Deprecated, PublicAPI, DeveloperAPI
  from ray.rllib.utils.framework import try_import_torch
  from ray.rllib.utils.numpy import SMALL_NUMBER
  from ray.rllib.utils.typing import LocalOptimizer, SpaceStruct, TensorStructType, TensorType
label_desc: Use image node_id 17 for calling a global function with example usage: sequence_mask(lengths, maxlen, dtype, time_major) and returns: mask