import torch


def batch_unpadding(_input, lens, right=True):
    """
    :param: _input: m x n tensor
    :param: lens:   list with size of m
    :param: right:  eliminate padding from right if true
    """
    if right:
        return torch.cat([x[:lens[i]] for i, x in enumerate(_input) if lens[i] > 0 ])
    else:
        return torch.cat([x[-lens[i]:] for i, x in enumerate(_input) if lens[i] > 0 ])