So for stride of 1, you have:
padding = dilation * (kernel -1) / 2

Assuming dilation of 1 as you don’t use it in your code, you have (kernel - 1)/2.
Since pytorch only supports padding as an integer number (what would be non-integer padding?), then you need kernel to be an odd number and you have a padding that can be (kernel - 1)/2