Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions test/inductor/test_cpu_repro.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,33 @@ def test_conv_transpose2d_packed_cpu(self):
(v,),
)

@config.patch(freezing=True)
@unittest.skipIf(not torch._C._has_mkldnn, "MKLDNN is not enabled")
@torch._dynamo.config.patch(dynamic_shapes=True)
@torch._dynamo.config.patch(assume_static_by_default=False)
def test_conv_in_channel_1_dynamic_shapes(self):
class M(torch.nn.Module):
def __init__(self, in_channel, out_channel) -> None:
super().__init__()
self.conv = torch.nn.Conv2d(in_channel, out_channel, 3)

def forward(self, x):
res = self.conv(x)
res = F.relu(res)
return res

# test the case where the channels dim of the input is 1
# Reproducer from the maml_omniglot model in Torchbench
in_channel = 1
out_channel = 3
mod = M(in_channel, out_channel).eval()
v = torch.randn(5, in_channel, 15, 15)
with torch.no_grad():
self.common(
mod,
(v,),
)

@unittest.skipIf(not torch._C._has_mkldnn, "MKLDNN is not enabled")
@patch("torch.cuda.is_available", lambda: False)
@torch._dynamo.config.patch(dynamic_shapes=True)
Expand Down
15 changes: 14 additions & 1 deletion torch/_inductor/ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -5477,9 +5477,22 @@ def _original_deconv_weight_size(

req_stride_order = [0] + list(reversed(range(1, len(stride) + 1)))
req_stride_order = [len(req_stride_order)] + req_stride_order
output_stride = make_channels_last_strides_for(output_size)

x = cls.require_stride_order(x, req_stride_order)

# We won't do weight prepack for Conv if dynamic_shapes.
# In static shape cases, since weight is prepacked, we'll always force output to be channels last in the Conv kernel.
# In dynamic shape cases, for input with channels = 1, like tensor of size (s0, 1, 28, 28) and stride (784, 784, 28, 1),
# x = cls.require_stride_order(x, req_stride_order) where req_stride_order is in the channels last order
# won't change the stride of this tensor since stride for dimensions of size 1 is ignored. While in Conv kernel,
# this tensor is considered as channels first and the output will be in contiguous format.
# To align the behavior of the Conv kernel, we set the output_stride in such case to be contiguous instead of channels last.
dynamic_shapes = not all(isinstance(i, int) for i in (output_size))
if dynamic_shapes and is_contiguous_storage_and_layout(x):
output_stride = make_contiguous_strides_for(output_size)
else:
output_stride = make_channels_last_strides_for(output_size)

assert x.get_device().type == "cpu" and weight.get_device().type == "cpu"
inputs = [x, weight]

Expand Down