MCPcopy Create free account
hub / github.com/apple/axlearn / conv_explicit_padding

Function conv_explicit_padding

axlearn/common/convolution.py:119–217  ·  view source on GitHub ↗

Returns the explicit padding for "SAME", "VALID", and "CAUSAL" modes. Each mode follows the formulas below: * SAME: (pad_total//2, pad_total - pad_total//2) s.t. pad_total = window-1 * VALID: (0, 0) * CAUSAL: (window - stride, stride - 1) Note: In the above equation, `window` w

(
    *,
    window: Sequence[int],
    strides: Sequence[int],
    padding: ConvPaddingType,
    dilation: Optional[Sequence[int]] = None,
)

Source from the content-addressed store, hash-verified

117# Copied from subroutine in jax.lax.reduce_window.
118# Extend lax.padtype_to_pads for CAUSAL.
119def conv_explicit_padding(
120 *,
121 window: Sequence[int],
122 strides: Sequence[int],
123 padding: ConvPaddingType,
124 dilation: Optional[Sequence[int]] = None,
125) -> ConvPaddingType:
126 """Returns the explicit padding for "SAME", "VALID", and "CAUSAL" modes.
127
128 Each mode follows the formulas below:
129 * SAME: (pad_total//2, pad_total - pad_total//2) s.t. pad_total = window-1
130 * VALID: (0, 0)
131 * CAUSAL: (window - stride, stride - 1)
132
133 Note: In the above equation, `window` will be replaced with `dilate_window` when dilation > 1.
134 dilate_window = (window - 1) * dilation + 1. Check conv_dilate_window()
135
136 For example, window=5 and stride=2,
137 * SAME: padding = (2, 2)
138 pad| |pad
139 paddings: 0 0|0 0 0 0 1 1|1 1
140 |___^___|
141 |___^___|
142 |___^___|
143
144 * VALID: padding = (0, 0)
145 | |
146 paddings: |0 0 0 0 1 1|
147 |^_______|
148
149 * CAUSAL: padding = (3, 1)
150 pad | |pad
151 paddings: 0 0 0|0 0 0 0 1 1|1
152 |_____^_|
153 |_____^_|
154 |_____^_|
155
156
157 For example, window=5, stride=2 and dilation=2
158 -> dilate_window = 9 (== (window-1)*dilation + 1) and pad_total = 8
159 * SAME: padding = (4, 4)
160 pad| |pad
161 paddings: 0 0 0 0|0 0 0 0 0 0 0 0 1 1|1 1 1 1
162 |_______^_______|
163 |_______^_______|
164 |_______^_______|
165 |_______^_______|
166 |_______^_______|
167
168 * VALID: padding = (0, 0)
169 | |pad
170 paddings: |0 0 0 0 0 0 0 0 1 1|
171 |^_______________|
172
173 * CAUSAL: padding = (7, 1)
174 pad | |pad
175 paddings: 0 0 0 0 0 0 0|0 0 0 0 0 0 0 0 1 1|1
176 |_____________^_|

Callers 6

conv_output_shapeFunction · 0.85
compute_conv_paddingsFunction · 0.85
forwardMethod · 0.85
forwardMethod · 0.85
forwardMethod · 0.85
forwardMethod · 0.85

Calls 2

conv_dilate_windowFunction · 0.85
same_paddingFunction · 0.85

Tested by

no test coverage detected