Skip to content

fiery.bounds.padding

This module reimplements torch.nn.functional.pad and torch.roll with a larger set of boundary conditions.

Functions:

Name Description
pad

Pad a tensor

roll

Roll a tensor

ensure_shape

Pad/crop a tensor so that it has a given shape

Functions:

pad

pad(inp: Tensor, padsize: SequenceOrScalar[int], mode: SequenceOrScalar[BoundLike] = 'constant', value: Number = 0, side: str | None = None) -> Tensor

Pad a tensor.

This function is a bit more generic than torch's native pad (torch.nn.functional.pad), but probably a bit slower:

  • works with any input type
  • works with arbitrarily large padding size
  • crops the tensor for negative padding values
  • implements additional padding modes

When used with defaults parameters (side=None), it behaves exactly like torch.nn.functional.pad

Boundary modes

Like in PyTorch's pad, boundary modes include:

  • 'circular' (or 'dft')
  • 'mirror' (or 'dct1')
  • 'reflect' (or 'dct2')
  • 'replicate' (or 'nearest')
  • 'constant' (or 'zero')

as well as the following new modes:

  • 'antimirror' (or 'dst1')
  • 'antireflect' (or 'dst2')

Side modes

Side modes are 'pre' (or 'left'), 'post' (or 'right'), 'both' or None.

  • If side is not None, inp.dim() values (or less) should be provided.
  • If side is None, twice as many values should be provided, indicating different padding sizes for the 'pre' and 'post' sides.
  • If the number of padding values is less than the dimension of the input tensor, zeros are prepended.

Parameters:

Name Type Description Default
inp tensor

Input tensor

required
padsize SequenceOrScalar[int]

Amount of padding in each dimension.

required
mode SequenceOrScalar[BoundLike]

Padding mode

'constant'
value scalar

Value to pad with in mode 'constant'.

0
side {'pre', 'post', 'both', None}

Use padsize to pad on left side ('pre'), right side ('post') or both sides ('both'). If None, the padding side for the left and right sides should be provided in alternate order.

None

Returns:

Name Type Description
out tensor

Padded tensor.

ensure_shape

ensure_shape(inp: Tensor, shape: SequenceOrScalar[int | None], mode: SequenceOrScalar[BoundLike] = 'constant', value: Number = 0, side: str = 'post', ceil: bool = False) -> Tensor

Pad/crop a tensor so that it has a given shape

Parameters:

Name Type Description Default
inp tensor

Input tensor

required
shape SequenceOrScalar[int]

Output shape. A value of None for a given dimension keeps that dimension's current size.

required
mode SequenceOrScalar[BoundLike]

Boundary mode

'constant'
value scalar

Value for mode 'constant'

0
side {'pre', 'post', 'both'}

Side to crop/pad

'post'
ceil bool

When side='both' and the amount to crop/pad cannot be split evenly, put the extra unit on the 'pre' side (True) rather than the 'post' side (False).

False

Returns:

Name Type Description
out tensor

Padded tensor with shape shape

roll

roll(inp: Tensor, shifts: SequenceOrScalar[int] = 1, dims: SequenceOrScalar[int] | None = None, bound: SequenceOrScalar[BoundLike] = 'circular') -> Tensor

Like torch.roll, but with any boundary condition

Warning

When dims is None, we do not flatten but shift all dimensions. This differs from the behavior of torch.roll .

Parameters:

Name Type Description Default
inp tensor

Input

required
shifts SequenceOrScalar[int]

Amount by which to roll. Positive shifts to the right, negative to the left.

1
dims SequenceOrScalar[int]

Dimensions to roll. By default, shifts apply to all dimensions if a scalar, or to the last N if a sequence.

None
bound SequenceOrScalar[BoundLike]

Boundary condition

'circular'

Returns:

Name Type Description
out tensor

Rolled tensor