import torch


def construct_length_mask(seq_lengths):
    """
    construct sequence length mask to rule out padding terms.
    :param seq_lengths: a list or 1D Tensor containing the lengths of the sequences in a batch
    :return: a 2D Tensor containing the mask
    """

    max_sequence_length = max(seq_lengths)
    mask = torch.zeros([len(seq_lengths), max_sequence_length]).bool()
    for line, length in zip(mask, seq_lengths):
        line[: length] = True
    return mask