from typing import List
import torch


def batchstack(x:List[torch.Tensor]):
    """ Stack given tensors along dim but equalize their dimensions first. """
    maxlen = 0
    for xe in x:
        maxlen = max(maxlen, xe.size(0))
    othersizes = x[0].size()[1:]
    x = [torch.cat([xe, xe.new_zeros(maxlen - xe.size(0), *othersizes)]) for xe in x]
    ret = torch.stack(x, 0)
    return ret