import torch


def get_position(lens, maxlen, left_pad=False):
    """ transform sequence length to a series of positions. e.g., 3 -> 1,2,3"""
    batch_size = lens.size(0)
    pos = torch.zeros((batch_size, maxlen), dtype=torch.long, device=lens.device)
    for i, input_len in enumerate(lens):
        if not left_pad:
            pos[i,:input_len] = torch.arange(1, input_len+1, dtype=torch.long, device=lens.device)
        else:
            pos[i,maxlen-input_len:] = torch.arange(1, input_len+1, dtype=torch.long, device=lens.device)
    return pos