import torch


def create_padding_mask_from_length(length, maxlen=None):
    """
    Transform a sequence length matrix to padding mask

    Args:
        length: sequence length matrix
            :math:`(N)` where N is batch size

    Returns:
        - padding mask indicating length
            :math:`(N, L)` where N is batch size and L is maximum length in `length`
    """
    bsz = length.size(0)
    if maxlen is None:
        maxlen = length.max()
    index = torch.arange(maxlen).long().unsqueeze(0).repeat(bsz, 1).to(length)
    padding_mask = index.ge(length.unsqueeze(1))
    return padding_mask