I also had the same problem. I my case the error was due incorrect input tensor into nn.Linear layer. The layer was initialized this way nn.Linear(512 * 7 * 7, 4096),  but actual input tensor shape before resizing was [512, 6, 6].
My configuration: ubuntu, python 3.9, cuda 11.2, torch 1.9, rtx 3060