oneflow.nn.functional.pad(input: oneflow.Tensor, pad: List[int], mode: str = 'constant', value: float = 0.0)oneflow.Tensor

Pads tensor.

The documentation is referenced from:

Padding size:

The padding size by which to pad some dimensions of input are described starting from the last dimension and moving forward. \(\left\lfloor\frac{\text{len(pad)}}{2}\right\rfloor\) dimensions of input will be padded. For example, to pad only the last dimension of the input tensor, then pad has the form \((\text{padding_left}, \text{padding_right})\); to pad the last 2 dimensions of the input tensor, then use \((\text{padding_left}, \text{padding_right},\) \(\text{padding_top}, \text{padding_bottom})\); to pad the last 3 dimensions, use \((\text{padding_left}, \text{padding_right},\) \(\text{padding_top}, \text{padding_bottom}\) \(\text{padding_front}, \text{padding_back})\).

Padding mode:

See oneflow.nn.ConstantPad2d, oneflow.nn.ReflectionPad2d, and oneflow.nn.ReplicationPad2d for concrete examples on how each of the padding modes works. Constant padding is implemented for arbitrary dimensions. Replicate and reflection padding is implemented for padding the last 3 dimensions of 5D input tensor, or the last 2 dimensions of 4D input tensor, or the last dimension of 3D input tensor.


When using the CUDA backend, this operation may induce nondeterministic behaviour in its backward pass that is not easily switched off.

  • input (Tensor) – N-dimensional tensor

  • pad (tuple) – m-elements tuple, where \(\frac{m}{2} \leq\) input dimensions and \(m\) is even.

  • mode'constant', 'reflect', 'replicate' or 'circular'. Default: 'constant'

  • value – fill value for 'constant' padding. Default: 0


>>> import oneflow as flow
>>> import oneflow.nn.functional as F
>>> t4d = flow.empty(3, 3, 4, 2)
>>> p1d = (1, 1)
>>> out = F.pad(t4d, p1d)
>>> out.size()
oneflow.Size([3, 3, 4, 4])