What Is padding='same' in PyTorch?

PyTorch supports padding='same' since v1.9. It automatically pads so output_size = input_size (with stride=1). Equivalent to padding = (kernel_size - 1) / 2 for odd kernels.

Using padding='same'

import torch
import torch.nn as nn

# With padding='same' — output matches input spatially
conv = nn.Conv2d(3, 64, kernel_size=3, padding='same')
x = torch.randn(1, 3, 224, 224)
print(conv(x).shape) # [1, 64, 224, 224] ✓

# Works with any odd kernel size
conv5 = nn.Conv2d(3, 64, kernel_size=5, padding='same')
print(conv5(x).shape) # [1, 64, 224, 224] ✓

conv7 = nn.Conv2d(3, 64, kernel_size=7, padding='same')
print(conv7(x).shape) # [1, 64, 224, 224] ✓

Manual Equivalent

For odd kernel sizes with stride=1, the manual padding is:

padding = (kernel_size - 1) // 2

# kernel=3: padding = (3-1)//2 = 1
nn.Conv2d(3, 64, kernel_size=3, padding=1)   # same as padding='same'

# kernel=5: padding = (5-1)//2 = 2
nn.Conv2d(3, 64, kernel_size=5, padding=2)   # same as padding='same'

# kernel=7: padding = (7-1)//2 = 3
nn.Conv2d(3, 64, kernel_size=7, padding=3)   # same as padding='same'

Limitations

# padding='same' does NOT work with stride > 1
conv = nn.Conv2d(3, 64, kernel_size=3, stride=2, padding='same')
# RuntimeError: padding='same' is not supported for strided convolutions

# For strided convolutions, calculate padding manually:
conv = nn.Conv2d(3, 64, kernel_size=3, stride=2, padding=1)
# output = floor((224 - 3 + 2) / 2) + 1 = 112

padding='same' vs padding='valid'

Try the Conv2d Calculator

TENSOR PREFLIGHT · DOWNLOADABLE PYTORCH TOOLKIT

Take the next step in your own PyTorch model.

Trace a local forward pass, inspect tensor shapes in an HTML report, and turn a repair into a regression check. Includes eight broken-and-fixed workflows, source code and setup instructions.

Is this toolkit right for my problem?

Use the free calculator for a layer formula or a quick shape check.

Consider Tensor Preflight when you have a local PyTorch model and want a forward-pass report plus repeatable checks. Python 3.9+ and PyTorch 2.8+ are required. It does not run your model in this website or automatically repair it.

Open the sample report before buying to check the output format.

Python 3.9+ · PyTorch 2.8+ · Runs locally

Get Tensor Preflight — $29 One-time payment · ZIP download See a sample report
By the same builder: GitHub, theluckystrike BeLikeNative, Grammar AI EarlyThunder, Dev Blog Bug Bounty Reality Zovo, AI Dev Tools