@Shen @lucamocerino If we are talking about images, another approach is to unfold the tensor into columns (with nn.Unfold), do whatever your operation is, then view it as the output shape.
Unfold (i.e., im2col) + gemm (i.e., batched matrix multiplication) + view is actually a common and pretty efficient implementation of convolution.