Conv2dStaticSamePadding¶

class Conv2dStaticSamePadding(in_channels, out_channels, kernel_size, stride=1, groups=1, dilation=1, *, bias=False, **kwargs)[source]¶

2D Convolution with static same padding.

Inherits from nn.Conv2d to match state_dict keys (weight/bias directly accessible). This layer computes padding dynamically based on input size to achieve ‘same’ padding behavior, ensuring output spatial dimensions are predictable.

stride¶

Stride of the convolution operation.

Type:

tuple[int, int]

kernel_size¶

Size of the convolution kernel.

Type:

tuple[int, int]

dilation¶

Dilation rate of the convolution.

Type:

tuple[int, int]

static_padding¶

Identity layer for module tree matching.

Type:

nn.Module

Example

>>> conv = Conv2dStaticSamePadding(32, 64, kernel_size=3, stride=2)
>>> x = torch.randn(1, 32, 128, 128)
>>> output = conv(x)
>>> output.shape
... torch.Size([1, 64, 64, 64])

Initialize Conv2dStaticSamePadding.

Creates a 2D convolutional layer with dynamic same padding.

Parameters:
  • in_channels (int) – Number of input channels.

  • out_channels (int) – Number of output channels.

  • kernel_size (int | tuple[int, int]) – Size of the convolution kernel.

  • stride (int | tuple[int, int]) – Stride of the convolution. Defaults to 1.

  • bias (bool) – If True, adds a learnable bias. Default: False.

  • groups (int) – Number of blocked connections from input to output. Defaults to 1.

  • dilation (int | tuple[int, int]) – Dilation rate of the convolution. Defaults to 1.

  • **kwargs (dict) – Additional keyword arguments for nn.Conv2d.

Methods

forward

Forward pass with dynamic same padding.

Attributes

bias

in_channels

out_channels

kernel_size

stride

padding

dilation

transposed

output_padding

groups

padding_mode

weight

training

forward(x)[source]¶

Forward pass with dynamic same padding.

Computes padding dynamically based on input spatial dimensions to achieve ‘same’ padding behavior.

Parameters:
Returns:

(B, C_out, H’, W’). Output tensor after convolution.

Return type:

torch.Tensor