F.pad Output Shape Calculator

Enter the input shape and the pad tuple of torch.nn.functional.pad. The calculator gives the output shape and shows which dimension each pair of values changes. The first pair changes the last dimension.

Input shape and pad tuple

No inputs leave this page.
Whole numbers of 1 or more, with a comma between them. Example: 1, 3, 224, 224
Two whole numbers for each padded dimension. The first pair is for the last dimension.

How F.pad reads the pad tuple

The PyTorch 2.14 documentation of torch.nn.functional.pad says that the padding sizes are described "starting from the last dimension". The first two values of the tuple change the last dimension. The next two values change the dimension before it, and the order continues toward the first dimension. A dimension that has no pair keeps its size.

The documentation names the values. For the last dimension the tuple is (padding_left, padding_right). For the last 2 dimensions it is (padding_left, padding_right, padding_top, padding_bottom). For the last 3 dimensions, padding_front and padding_back follow these four values. The tuple must have an even number of values, and it cannot have more pairs than the input has dimensions.

The documentation page shows three examples on an input of shape (3, 3, 4, 2).

Pad tupleComment in the docsSize printed in the docs
(1, 1)pad last dim by 1 on each sidetorch.Size([3, 3, 4, 4])
(1, 1, 2, 2)pad last dim by (1, 1) and 2nd to last by (2, 2)torch.Size([3, 3, 8, 4])
(0, 1, 2, 1, 3, 3)pad by (0, 1), (2, 1), and (3, 3)torch.Size([3, 9, 7, 3])

In the third example the input has 4 dimensions and the tuple has 3 pairs. The third pair changes dimension 1, from 3 to 9. A third pair on a 4D input does not change dimension 0.

The default mode is 'constant'. For the fill value the documentation says "Default: 0", and the signature on the same page shows value=None. The calculator has no value field, because the fill value does not change a size in constant mode.

The output size rule and the padding layers

The F.pad page prints no size formula. The pages of the padding layers print it. For the 2D layers the formulas are:

H_out = H_in + padding_top + padding_bottom
W_out = W_in + padding_left + padding_right

The calculator uses the same sum for each dimension that has a pair: size out = size in + first value + second value. The five 2D layers below call F.pad with one mode each, as the PyTorch source at tag v2.14.0 shows. Each layer page says "For N-dimensional padding, use torch.nn.functional.pad()."

Layer and its mode in F.padSize limit on the layer page
nn.ZeroPad2d
constant, fill value 0
None stated
nn.ConstantPad2d
constant, fill value from the layer
None stated
nn.ReflectionPad2d
reflect
"padding size should be less than the corresponding input dimension"
nn.ReplicationPad2d
replicate
"the output dimensions must remain positive"
nn.CircularPad2d
circular
"padding size should be less than or equal to the corresponding input dimension"

Each 2D layer takes a 4-tuple in the order (padding_left, padding_right, padding_top, padding_bottom). The F.pad tuple has the same order. An int gives the same padding on all four sides. The documentation example nn.ReflectionPad2d(2) on an input of shape (1, 1, 3, 3) prints a 7 x 7 result. In the calculator, enter 1, 1, 3, 3, the tuple 2, 2, 2, 2 and the mode reflect to get (1, 1, 7, 7).

Which mode works on which input

The documentation says "Constant padding is implemented for arbitrary dimensions." For the other three modes it lists the permitted inputs: "Circular, replicate and reflection padding are implemented for padding the last 3 dimensions of a 4D or 5D input tensor, the last 2 dimensions of a 3D or 4D input tensor, or the last dimension of a 2D or 3D input tensor."

Pairs in the tuplePadded dimensionsInput for reflect, replicate and circular
1 pair (2 values)The last dimension2D or 3D
2 pairs (4 values)The last 2 dimensions3D or 4D
3 pairs (6 values)The last 3 dimensions4D or 5D

A 4D input with one pair and the mode reflect is not in this list. The calculator shows a message and no shape for such a combination. Constant mode takes one pair on a 4D input: the first example of the documentation does this.

The size limits are different for each mode. In reflect mode a pad value must be less than the input size of its dimension. In circular mode a pad value can be equal to the input size. The replication pages state no upper limit. On an input of shape (1, 1, 3, 3), the tuple (3, 3, 3, 3) is an error in reflect mode and gives (1, 1, 9, 9) in circular mode, and the tuple (4, 4, 4, 4) is an error in the two modes. In replicate mode, an input of shape (1, 2, 4) with the tuple (10, 10) gives (1, 2, 24). These results are from runs on torch 2.8.0 and torch 2.14.1.

Negative pad values

The F.pad page does not mention negative values. The pages of the circular layers do: "If negative padding is applied then the ends of the tensor get removed." The pages of the reflection, replication, constant and zero layers do not mention negative values.

In runs on torch 2.8.0 and torch 2.14.1, a negative value removed elements in all four modes. An input of shape (1, 3, 8, 8) with the tuple (-1, -1) gave (1, 3, 8, 6) in constant mode. An input of shape (1, 2, 4) with the tuple (-1, -1) gave (1, 2, 2) in reflect, replicate and circular mode. The calculator applies the same sum to a negative value and adds a note to the result.

A size of 0 depends on the mode and on the version. In constant mode, (1, 3, 8, 8) with (-8, 0, 1, 1) was a RuntimeError on torch 2.8.0 and gave (1, 3, 10, 0) on torch 2.14.1. In replicate mode, (1, 1, 4, 4) with (-2, -2, 0, 0) gave (1, 1, 4, 0) on torch 2.8.0 and was a RuntimeError on torch 2.14.1. For this reason the calculator gives no shape when a size is 0. A size below 0 was an error in each run. The calculator also takes no input shape that has a size of 0. In runs on torch 2.8.0 and torch 2.14.1, F.pad gave a shape for some such inputs and an error for other such inputs: (0, 3) with (1, 1) gave (0, 5) in constant mode, and (1, 0, 3) with (1, 1) was a RuntimeError in reflect mode.

The sum does not hold for each pair that has a negative value and a positive value. In constant mode, a negative value that removes more elements than the dimension has was a RuntimeError on torch 2.8.0 and torch 2.14.1: (1, 3, 8, 8) with (-9, 10) gave no result, and (-8, 9) gave (1, 3, 8, 9). In circular mode, the result depended on the input when the positive value was larger than the size that stays after the removal: (-3, 4) gave (1, 2, 5) on an input of shape (1, 2, 4) and was a RuntimeError on an input of shape (1, 1, 4). The calculator gives no shape for these pairs. In reflect and replicate mode, runs with such pairs gave the sum, for example (1, 2, 4) with (-5, 3) in reflect mode gave (1, 2, 2). Each result in this paragraph was the same on torch 2.8.0 and torch 2.14.1.

Error messages by torch version

The documentation pages print no error message. The messages below are from runs on torch 2.8.0 and torch 2.14.1 (CPU builds, macOS on arm64, 2026-10-11). A different version can print a different text.

Methodology and primary sources

The rules on this page are from the PyTorch 2.14 documentation pages below, read on 2026-10-11. On that date the stable documentation address pointed to version 2.14. The calculator is JavaScript arithmetic in this page and does not run PyTorch. It does not check that a tensor of the given size fits in memory. The run results are from torch 2.8.0 and torch 2.14.1, CPU builds on macOS (arm64), on 2026-10-11. CUDA, MPS and other torch versions were not run.

  1. PyTorch 2.14 docs, torch.nn.functional.pad, tuple order, value names, the three examples, the mode list and the defaults.
  2. PyTorch 2.14 docs, ReflectionPad2d, size formulas, the reflect limit and the 7 x 7 example.
  3. PyTorch 2.14 docs, ReplicationPad2d, the sentence on positive output dimensions.
  4. PyTorch 2.14 docs, CircularPad2d, the circular limit and the sentence on negative padding.
  5. PyTorch 2.14 docs, ConstantPad2d, size formulas for a constant fill value.
  6. PyTorch 2.14 docs, ZeroPad2d, size formulas for zero padding.
  7. PyTorch source at tag v2.14.0, torch/nn/modules/padding.py, the F.pad call and the mode of each layer.

Executable browser fixtures

The fixtures run in this browser when JavaScript is on.

FixtureExpectedObservedStatus

Fixtures 1, 2, 3 and 5 are examples in the PyTorch 2.14 docs. The other expected values are from runs on torch 2.8.0 and torch 2.14.1.

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