code: def mask_sequence_tensor(tensor: torch.Tensor, lengths: torch.Tensor):
      """
      For tensors containing sequences, zero out out-of-bound elements given lengths of every element in the batch.
  
      tensor: tensor of shape (B, D, L) or (B, D1, D2, L),
      lengths: LongTensor of shape (B,)
      """
      batch_size, *_, max_lengths = tensor.shape
  
      if len(tensor.shape) == 2:
          mask = (
              torch.ones(batch_size, max_lengths)
              .cumsum(dim=-1)
              .type_as(lengths)
          )
          mask = mask <= rearrange(lengths, "b -> b 1")
      elif len(tensor.shape) == 3:
          mask = (
              torch.ones(batch_size, 1, max_lengths)
              .cumsum(dim=-1)
              .type_as(lengths)
          )
          mask = mask <= rearrange(lengths, "b -> b 1 1")
      elif len(tensor.shape) == 4:
          mask = (
              torch.ones(batch_size, 1, 1, max_lengths)
              .cumsum(dim=-1)
              .type_as(lengths)
          )
          mask = mask <= rearrange(lengths, "b -> b 1 1 1")
      else:
          raise ValueError(
              "Can only mask tensors of shape B x D x L and B x D1 x D2 x L"
          )
  
      return tensor * mask
  
imports: from enum import Enum
  from typing import Optional, Tuple
  import librosa
  import matplotlib.pylab
  import numpy
  import torch
  from einops import rearrange
  from numba import jit, prange
  from nemo.collections.tts.torch.tts_data_types import DATA_STR2DATA_CLASS, MAIN_DATA_TYPES, WithLens
  from nemo.utils import logging
  from nemo.utils.decorators import deprecated
label_desc: Use image node_id 33 for calling a global function with example usage: mask_sequence_tensor(tensor, lengths) and returns: unknown