import torch


def make_length_mask(lengths):
    """
    Compute binary length mask.

    lengths: torch.Tensor(batch, dtype=int) should be on the desired
        output device.

    Returns
    =======

    mask: torch.ByteTensor(batch x seq_len)
    """
    maxlen, batch = lengths.detach().max(), len(lengths)
    return torch.arange(0, maxlen, dtype=torch.int64, device=lengths.device) \
                .repeat(batch, 1) \
                .lt(lengths.unsqueeze(1))