VGG19 RuntimeError: stride should not be zero

I am using transfer learning to tune a pretrained VGG19 model. I am using MPS on Mac M1. Here is the printed model and error that occurs during forward pass:
MODEL:
VGG(
(features): Sequential(
(0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(1): ReLU(inplace=True)
(2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(3): ReLU(inplace=True)
(4): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(5): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(6): ReLU(inplace=True)
(7): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(8): ReLU(inplace=True)
(9): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(10): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(11): ReLU(inplace=True)
(12): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(13): ReLU(inplace=True)
(14): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(15): ReLU(inplace=True)
(16): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(17): ReLU(inplace=True)
(18): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(19): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(20): ReLU(inplace=True)
(21): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(22): ReLU(inplace=True)
(23): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(24): ReLU(inplace=True)
(25): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(26): ReLU(inplace=True)
(27): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(28): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(29): ReLU(inplace=True)
(30): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(31): ReLU(inplace=True)
(32): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(33): ReLU(inplace=True)
(34): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(35): ReLU(inplace=True)
(36): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
)
(avgpool): AdaptiveAvgPool2d(output_size=(7, 7))
(classifier): Sequential(
(0): Linear(in_features=25088, out_features=8192, bias=True)
(1): ReLU(inplace=True)
(2): Linear(in_features=8192, out_features=256, bias=True)
(3): ReLU(inplace=True)
(4): Linear(in_features=256, out_features=2, bias=True)
)
)

ERROR:
Traceback (most recent call last):
File “~/Desktop/ObjectSecurity/adversarial-segmentation/catsdogs_torch.py”, line 124, in
pred = model(x)
File “~/miniforge3/envs/myenv2/lib/python3.9/site-packages/torch/nn/modules/module.py”, line 1186, in _call_impl
return forward_call(*input, **kwargs)
File “~/miniforge3/envs/myenv2/lib/python3.9/site-packages/torchvision/models/vgg.py”, line 67, in forward
x = self.avgpool(x)
File “~/miniforge3/envs/myenv2/lib/python3.9/site-packages/torch/nn/modules/module.py”, line 1186, in _call_impl
return forward_call(*input, **kwargs)
File “~/miniforge3/envs/myenv2/lib/python3.9/site-packages/torch/nn/modules/pooling.py”, line 1183, in forward
return F.adaptive_avg_pool2d(input, self.output_size)
File “~/miniforge3/envs/myenv2/lib/python3.9/site-packages/torch/nn/functional.py”, line 1214, in adaptive_avg_pool2d
return torch._C._nn.adaptive_avg_pool2d(input, _output_size)
RuntimeError: stride should not be zero

Could you check what the input shape to the adaptive avg pool layer is? My first guess is that could be the cause of the inferred 0 stride.

input shape to avgpool: torch.Size([64, 512, 4, 4])

Interesting, do you see any errors with the following snippet?

import torch

avgpool = torch.nn.AdaptiveAvgPool2d((7, 7))
a = torch.randn(64, 512, 4, 4, device='mps')
avgpool(a)

I get the same error. Any idea why? Should the input size be bigger than the output size?

I believe this is an issue that is specific to the MPS backend as I cannot observe it on cpu or cuda. Will open a bug to track this issue.

EDIT: issue is here [MPS] AdaptiveAvgPool2D doesn't accept input spatial size smaller than output shape · Issue #80732 · pytorch/pytorch · GitHub

1 Like