padding'same' conversion to PyTorch padding
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When you port a convolutional model from TensorFlow to PyTorch, padding='same' is one of the first details that can change numerical results. The idea sounds simple, but the exact padding depends on kernel size, stride, and dilation, and older PyTorch code often needs manual padding.
What same Padding Means
In TensorFlow, same padding tries to preserve the spatial size for stride 1, and for larger strides it chooses enough padding so the output follows TensorFlow's shape rule. The important point is that the framework may use asymmetric padding, which means the top and bottom, or left and right, do not always receive the same value.
In PyTorch, there are three common cases:
- Modern PyTorch can accept
padding="same"directly in many convolution layers. - Older code uses an integer padding value such as
padding=1. - Model conversion pipelines use
torch.nn.functional.padornn.ZeroPad2dbefore a convolution.
If your model has an odd kernel, stride 1, and dilation 1, the conversion is easy. A 3 x 3 kernel usually maps to padding=1, and a 5 x 5 kernel maps to padding=2.
Simple Conversion For Common Cases
For symmetric, odd-sized kernels, a direct integer often works:
This matches TensorFlow same behavior for the most common image-model setup. The trouble starts when the stride is greater than 1, the kernel is even-sized, or dilation expands the effective kernel width.
Computing TensorFlow-Style Padding Explicitly
When you need exact TensorFlow behavior, compute the padding from the input size and apply it yourself:
This pattern is reliable during model conversion because it mirrors the shape rule rather than guessing a single integer padding value.
Wrapping The Logic In A Reusable Module
If you need the same behavior in several places, wrap it in a small module:
This makes converted code easier to read and keeps shape handling close to the layer that depends on it.
Common Pitfalls
The biggest mistake is assuming padding = kernel_size // 2 always matches TensorFlow same. That only holds in simple symmetric cases. With even kernels or larger strides, TensorFlow may pad one side more than the other.
Another issue is forgetting dilation. Dilation changes the effective kernel size, so the required padding grows even if the original kernel shape stays the same.
Version differences also matter. Recent PyTorch releases support padding="same" in many places, but exported or legacy code may still need manual padding. If exact parity matters, test shapes and outputs against the original model instead of assuming the built-in option is identical in every deployment path.
Finally, verify padding order when calling F.pad. For 2D images, PyTorch expects (left, right, top, bottom). Reversing that order gives wrong feature alignment while still producing a tensor of plausible size.
Summary
- TensorFlow
samepadding may be asymmetric, especially with larger strides or even kernels. - For odd kernels with stride
1, integer padding likepadding=1often matches. - For exact TensorFlow behavior, compute padding from input shape, stride, kernel size, and dilation.
- '
F.padplus apadding=0convolution is a dependable conversion pattern.' - Always validate both output shape and numerical alignment after conversion.

