code: def sequence_mask(
      lengths: Tensor, maxlen: Optional[int] = None, dtype=torch.bool
  ) -> Tensor:
      """Implements tf.sequence_mask in torch.
  
      From https://discuss.pytorch.org/t/pytorch-equivalent-for-tf-sequence-mask/39036/2.
      """
      if maxlen is None:
          maxlen = lengths.max()
      row_vector = torch.arange(0, maxlen, 1).to(lengths.device)
      matrix = torch.unsqueeze(lengths, dim=-1)
      mask = row_vector < matrix
  
      return mask.type(dtype)
  
file_path: ludwig/ludwig/utils/metric_utils.py
imports: from collections import defaultdict, namedtuple
  from typing import Dict, List, Optional
  import torch
  from torch import Tensor
  from torchmetrics.metric import Metric
  from ludwig.constants import COMBINED, LOSS, NAME, TYPE
  from ludwig.modules.metric_registry import get_metric_names_for_type
  from ludwig.types import FeatureConfigDict
label_desc: Use image node_id 1 for calling a global function with example usage: sequence_mask(lengths, maxlen, dtype) and returns: mask