Thanks for your reply! In my cases, n1, n2…may differ by a lot. So I think zero padding may not be efficient. Since I also want the autogradient in Pytorch, to implement CUDA kernels may not be easy.  I guess for loops may be the only choice.