You could try to manipulate the native conv2d implementation, but note that you won’t be able to manipulate the backend implementations from e.g. cudnn.
I’m also not sure, if that would be easier than to write out your operations manually.
The padding argument in nn.Conv2d can be simply added to the unfolding approach using F.pad.
stride, kernel_size and dilation are all set in the unfold call.
You could of course write the nested loop as a CPU/CUDA extension and use it in your Python script as explained here 17.