Convolution Implementation


I am trying to implement the convolution (or to be accurate the cross-correlation) function with a certain padding and stride value

x = torch.randn(1, 3, 6, 8) # input
kernel = torch.randn(2, 3, 4, 5) # filter

conv = Conv2d(kernel, padding=1, stride=3)

I have already implemented the padding function,
my Q is how can I implement the Conv2d function in python/pytorch to calculate the cross correlation?
I tried with nested looping and summation but that was inefficient. a seperate variable for stride (self.stride) should be used.

P.S: the whole intuition of the task is to not use the pre-implemented Conv2d


You could try to use unfold to create the input patches and a matmul to implement the convolution as given in the Unfold docs.
Note that this would most likely still be slower than the native implementations.