import torch


def get_mask_from_sequence_lengths(sequence_lengths: torch.Tensor, max_length: int) -> torch.Tensor:
  """Generate mask from variable ``(batch_size,)`` which represents the sequence lengths of each
  batch element.

  Returns:
    torch.Tensor: ``(batch_size, max_length)``
  """
  ones = sequence_lengths.new_ones(sequence_lengths.size(0), max_length)
  range_tensor = ones.cumsum(dim=1)
  return (range_tensor <= sequence_lengths.unsqueeze(1)).long()