import torch


def generate_padding_mask(seq_len, max_len=None):
    """
    Generate padding mask for the Transformer Encoder or Decoder memory.

    From the nn.Transformer:
    If a BoolTensor is provided, the positions with the value of True
    will be ignored while the position with the value of False will be unchanged.
    """
    if max_len is None:
        max_len = seq_len.max().int()
    mask = torch.arange(max_len).to(seq_len.device)
    mask = mask.expand(seq_len.size(0), max_len)
    mask = (mask >= seq_len.unsqueeze(1))
    return mask