I am trying to run a simple model
import torch
import torchvision.models as models

model = models.resnet18(pretrained=True)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# model = torch.nn.DataParallel(model)
model.to(device)

loss_criterion = torch.nn.CrossEntropyLoss()
inp = torch.randn(5, 3, 224, 224).to(device)
target = torch.empty(5, dtype=torch.long).random_(1000).to(device)
output = model(inp)
loss = loss_criterion(output, target)
loss.backward()

I observed that backward fails when I give the input with batch size less than 5. Further if I am using nn.dataparallel, the model also requires there are atleaset 5 samples per batch other wise it gives an error.
I am attaching the screenshot of the error below.

Screenshot 2021-05-01 at 10.11.01 PM1249×141 13.6 KB

My system:
Ubuntu: 20.04
Pytorch:1.8.1
CUDA: 11.1