I need further to know the shape of output y. In your above code, y is overwritten 16 times. And which axis you want to cat along,? since the shape of A[:,i,:] and B[:, j, :] is 506x256(CXW), cat along C-rows or W-cols?