Skip to content

Commit 8d66e89

Browse files
committed
Fix (util): centralizing auto pad utility
1 parent 30b838b commit 8d66e89

File tree

4 files changed

+23
-58
lines changed

4 files changed

+23
-58
lines changed

src/qonnx/transformation/lower_convs_to_matmul.py

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,7 @@
3232

3333
from qonnx.transformation.base import Transformation
3434
from qonnx.transformation.extract_conv_bias import ExtractBiasFromConv
35-
from qonnx.util.basic import get_by_name
36-
37-
38-
def _auto_pad_to_explicit_padding(autopad_str, idim_h, idim_w, k_h, k_w, stride_h, stride_w, n_dims):
39-
pad_total_h = (stride_h - 1) * idim_h - stride_h + k_h
40-
pad_total_w = (stride_w - 1) * idim_w - stride_w + k_w
41-
pad_half_small_h = int((pad_total_h / 2))
42-
pad_half_small_w = int((pad_total_w / 2))
43-
pad_half_large_h = pad_total_h - pad_half_small_h
44-
pad_half_large_w = pad_total_w - pad_half_small_w
45-
if autopad_str == "VALID":
46-
return [0 for i in range(2 * n_dims)]
47-
elif autopad_str == "SAME_UPPER":
48-
return [pad_half_small_h, pad_half_small_w, pad_half_large_h, pad_half_large_w]
49-
elif autopad_str == "SAME_LOWER":
50-
return [pad_half_large_h, pad_half_large_w, pad_half_small_h, pad_half_small_w]
51-
else:
52-
raise Exception("Unsupported auto_pad: " + autopad_str)
53-
35+
from qonnx.util.basic import get_by_name, auto_pad_to_explicit_padding
5436

5537
class LowerConvsToMatMul(Transformation):
5638
"""Replace Conv layers with pairs of Im2Col-MatMul layers, plus Transpose
@@ -100,7 +82,7 @@ def apply(self, model):
10082
# use specified padding
10183
pad = get_by_name(n.attribute, "pads").ints
10284
else:
103-
pad = _auto_pad_to_explicit_padding(
85+
pad = auto_pad_to_explicit_padding(
10486
auto_pad,
10587
ifm_dim_h,
10688
ifm_dim_w,

src/qonnx/transformation/resize_conv_to_deconv.py

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from qonnx.core.datatype import DataType
3434
from qonnx.custom_op.general.quant import quant
3535
from qonnx.transformation.base import Transformation
36-
from qonnx.util.basic import get_by_name
36+
from qonnx.util.basic import get_by_name, auto_pad_to_explicit_padding
3737

3838

3939
def _weight_convolution(cnv_weights: np.ndarray, scale: int) -> np.ndarray:
@@ -55,23 +55,6 @@ def _weight_convolution(cnv_weights: np.ndarray, scale: int) -> np.ndarray:
5555
return dcnv_weights
5656

5757

58-
def _auto_pad_to_explicit_padding(autopad_str, idim_h, idim_w, k_h, k_w, stride_h, stride_w, n_dims):
59-
pad_total_h = (stride_h - 1) * idim_h - stride_h + k_h
60-
pad_total_w = (stride_w - 1) * idim_w - stride_w + k_w
61-
pad_half_small_h = int((pad_total_h / 2))
62-
pad_half_small_w = int((pad_total_w / 2))
63-
pad_half_large_h = pad_total_h - pad_half_small_h
64-
pad_half_large_w = pad_total_w - pad_half_small_w
65-
if autopad_str == "VALID":
66-
return [0 for i in range(2 * n_dims)]
67-
elif autopad_str == "SAME_UPPER":
68-
return [pad_half_small_h, pad_half_small_w, pad_half_large_h, pad_half_large_w]
69-
elif autopad_str == "SAME_LOWER":
70-
return [pad_half_large_h, pad_half_large_w, pad_half_small_h, pad_half_small_w]
71-
else:
72-
raise Exception("Unsupported auto_pad: " + autopad_str)
73-
74-
7558
class ResizeConvolutionToDeconvolution(Transformation):
7659
"""Replaces resize convolution layers (e.g., nearest neighbor upsample + same-padded convolution)
7760
with deconvolution layers using the weight convolution algorithm. Currently does not support
@@ -189,7 +172,7 @@ def apply(self, model):
189172
# use specified padding
190173
pad = get_by_name(conv.attribute, "pads").ints
191174
else:
192-
pad = _auto_pad_to_explicit_padding(
175+
pad = auto_pad_to_explicit_padding(
193176
auto_pad,
194177
ifm_dim_h,
195178
ifm_dim_w,

src/qonnx/transformation/subpixel_to_deconv.py

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from onnx import helper
3232

3333
from qonnx.transformation.base import Transformation
34-
from qonnx.util.basic import get_by_name
34+
from qonnx.util.basic import get_by_name, auto_pad_to_explicit_padding
3535

3636

3737
def _weight_shuffle(cnv_weights: np.ndarray, block_size: int) -> np.ndarray:
@@ -62,23 +62,6 @@ def _weight_shuffle(cnv_weights: np.ndarray, block_size: int) -> np.ndarray:
6262
return dcnv_weights
6363

6464

65-
def _auto_pad_to_explicit_padding(autopad_str, idim_h, idim_w, k_h, k_w, stride_h, stride_w, n_dims):
66-
pad_total_h = (stride_h - 1) * idim_h - stride_h + k_h
67-
pad_total_w = (stride_w - 1) * idim_w - stride_w + k_w
68-
pad_half_small_h = int((pad_total_h / 2))
69-
pad_half_small_w = int((pad_total_w / 2))
70-
pad_half_large_h = pad_total_h - pad_half_small_h
71-
pad_half_large_w = pad_total_w - pad_half_small_w
72-
if autopad_str == "VALID":
73-
return [0 for i in range(2 * n_dims)]
74-
elif autopad_str == "SAME_UPPER":
75-
return [pad_half_small_h, pad_half_small_w, pad_half_large_h, pad_half_large_w]
76-
elif autopad_str == "SAME_LOWER":
77-
return [pad_half_large_h, pad_half_large_w, pad_half_small_h, pad_half_small_w]
78-
else:
79-
raise Exception("Unsupported auto_pad: " + autopad_str)
80-
81-
8265
class SubPixelToDeconvolution(Transformation):
8366
"""Replaces sub-pixel convolution layers (i.e., same-padded convolution + depth2space)
8467
with deconvolution layers using the weight shuffle algorithm. Currently does not support
@@ -181,7 +164,7 @@ def apply(self, model):
181164
# use specified padding
182165
pad = get_by_name(n.attribute, "pads").ints
183166
else:
184-
pad = _auto_pad_to_explicit_padding(
167+
pad = auto_pad_to_explicit_padding(
185168
auto_pad,
186169
ifm_dim_h,
187170
ifm_dim_w,

src/qonnx/util/basic.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,3 +321,20 @@ def sanitize_quant_values(model, node_tensors, execution_context, check_values=F
321321
)
322322
)
323323
return execution_context
324+
325+
326+
def auto_pad_to_explicit_padding(autopad_str, idim_h, idim_w, k_h, k_w, stride_h, stride_w, n_dims):
327+
pad_total_h = (stride_h - 1) * idim_h - stride_h + k_h
328+
pad_total_w = (stride_w - 1) * idim_w - stride_w + k_w
329+
pad_half_small_h = int((pad_total_h / 2))
330+
pad_half_small_w = int((pad_total_w / 2))
331+
pad_half_large_h = pad_total_h - pad_half_small_h
332+
pad_half_large_w = pad_total_w - pad_half_small_w
333+
if autopad_str == "VALID":
334+
return [0 for i in range(2 * n_dims)]
335+
elif autopad_str == "SAME_UPPER":
336+
return [pad_half_small_h, pad_half_small_w, pad_half_large_h, pad_half_large_w]
337+
elif autopad_str == "SAME_LOWER":
338+
return [pad_half_large_h, pad_half_large_w, pad_half_small_h, pad_half_small_w]
339+
else:
340+
raise Exception("Unsupported auto_pad: " + autopad_str)

0 commit comments

Comments
 (0)