code: def sequence_mask(
      lengths: torch.Tensor,
      maxlen: Optional[int] = None,
      dtype: torch.dtype = torch.bool,
  ):
      """Returns a mask of shape (batch_size x maxlen), where mask[i] is True for each element up to lengths[i],
      otherwise False i.e. if maxlen=5 and lengths[i] = 3, mask[i] = [True, True True, False False].
  
      :param lengths: (torch.Tensor) A 1d integer tensor of shape [batch size].
      :param maxlen: (Optional[int]) The maximum sequence length.  If not specified, the max(lengths) is used.
      :param dtype: (type) The type to output.
  
      # Return
      :returns: (torch.Tensor) A sequence mask tensor of shape (batch_size x maxlen).
      """
      if maxlen is None:
          maxlen = lengths.max()
      matrix = torch.unsqueeze(lengths, dim=-1)
      row_vector = torch.arange(0, maxlen, 1, device=lengths.device)
      mask = row_vector < matrix
      mask = mask.type(dtype)
      return mask
  
imports: import math
  import os
  import warnings
  from abc import abstractmethod
  from functools import lru_cache
  from typing import List, Optional, Tuple, Union
  import torch
  from torch import nn
  from torch.nn import Module, ModuleDict
  from ludwig.api_annotations import DeveloperAPI
  from ludwig.constants import ENCODER_OUTPUT
  from ludwig.utils.strings_utils import SpecialSymbol
label_desc: Use image node_id 5 for calling a global function with example usage: sequence_mask(lengths, maxlen, dtype) and returns: mask