Skip to content

Commit d77461d

Browse files
authored
Surface ConvTranspose padding=0+output_padding ValueError instead of TypeError (fixes #2377) (#2711)
* Surface ConvTranspose padding=0+output_padding ValueError instead of TypeError The torch conv code normalizes the all-zero `pad` to `None` (the conv builder treats `None` as the all-zero default). When a ConvTranspose also has `output_padding > 0` -- which happens implicitly when the user calls `convt(x, output_size=...)` and the implied stride/kernel math demands extra output -- `_construct_conv_transpose` then tries `sum(pad)`, `pad.copy()`, and `pad *= 0` on that `None`, blowing up with: TypeError: 'NoneType' object is not iterable The existing code path already documents this combination as unsupported and raises a clear `ValueError` for it, but only when `pad` is an array. Re-materialize the implicit zeros so the documented ValueError fires instead of the cryptic TypeError; users with the unsupported configuration now get an actionable message. Adds `TestConvTranspose::test_convtranspose1d_padding_zero_output_padding_clear_error` covering exactly the issue repro (Conv1d + ConvTranspose1d with padding=0 and `output_size=(1024,)`). Runs without BlobWriter. Fixes #2377 * Address review: trim verbose comment in _construct_conv_transpose
1 parent dc0c41c commit d77461d

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

coremltools/converters/mil/frontend/torch/ops.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,6 +1379,9 @@ def _construct_conv_transpose(
13791379
post_crop = [0] * pad_len
13801380

13811381
if out_padding is not None and any(out_padding):
1382+
# Re-materialize the implicit-zero pad so the ValueError below fires (#2377).
1383+
if pad is None:
1384+
pad = np.zeros(pad_len, dtype=np.int32)
13821385
output_padding = [0] * pad_len
13831386
# output padding adds additional padding on one of the side of dimension
13841387
# i.e. bottom from top-bottom,

coremltools/converters/mil/frontend/torch/test/test_torch_ops.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2290,6 +2290,43 @@ def test_convolution_transpose3d(
22902290
compute_unit=compute_unit,
22912291
)
22922292

2293+
def test_convtranspose1d_padding_zero_output_padding_clear_error(self):
2294+
"""Regression test for https://github.com/apple/coremltools/issues/2377.
2295+
2296+
A `ConvTranspose1d` with `padding=0` that calls `forward(..., output_size=...)`
2297+
in a way that implies a non-zero `output_padding` previously crashed with
2298+
``TypeError: 'NoneType' object is not iterable`` because the converter
2299+
had normalized the all-zero `pad` to `None` and then tried to `sum`/
2300+
`.copy()` it. The fix materializes the implicit zeros so the
2301+
already-existing "padding=0 and output_padding > 0 not supported"
2302+
ValueError fires instead.
2303+
"""
2304+
2305+
class SimpleUNetLike(nn.Module):
2306+
def __init__(self):
2307+
super().__init__()
2308+
self.conv1 = nn.Conv1d(1, 16, 3, padding=0, stride=2)
2309+
self.convt1 = nn.ConvTranspose1d(16, 1, 3, padding=0, stride=2)
2310+
2311+
def forward(self, x):
2312+
x = self.conv1(x)
2313+
return self.convt1(x, output_size=(1024,))
2314+
2315+
model = SimpleUNetLike().eval()
2316+
x = torch.randn(1, 1, 1024)
2317+
traced = torch.jit.trace(model, x)
2318+
2319+
with pytest.raises(
2320+
ValueError,
2321+
match=r"ConvTranspose configuration of padding=0 and output_padding > 0 not supported",
2322+
):
2323+
ct.convert(
2324+
traced,
2325+
inputs=[ct.TensorType(name="x", shape=x.shape)],
2326+
convert_to="milinternal",
2327+
minimum_deployment_target=ct.target.iOS17,
2328+
)
2329+
22932330

22942331
class TestUpsample(TorchBaseTest):
22952332
@staticmethod

0 commit comments

Comments
 (0)