Ah OK, I’ve misunderstood your question.
If you are using odd shaped kernels, the padding would be straightforward, as we need to pad on both sides of the input. However, using even shaped kernels, we could use the functional pad method to add the appropriate padding to the input.
Here is a small example. If you don’t need to backpropagate through the kernel (smaller image), just set requires_grad=False:
batch_size = 1
c, h, w = 3, 12, 12
num_kernels = 1
kernel_size = (h//2, w//2)

x = torch.randn(batch_size, c, h, w)
kernel = torch.randn(num_kernels, c, *kernel_size, requires_grad=True)

padding = []
for k in kernel_size:
    if k%2==0:
        pad = [(k-1)//2, (k-1)//2+1]
    else:
        pad = [(k-1)//2, (k-1)//2]
    padding.extend(pad)
    
x = F.pad(x, pad=padding)
output = F.conv2d(x, kernel)
print(output.shape)
> torch.Size([1, 1, 15, 15])