DataParallel doesn’t support the situation where you have optimizer inside the forward function.
Instead you have to keep forward to only compute and return the loss, and as part of training loop, you should do the lines:
loss = model(data, target)
loss.mean(0).backward()
optim.step()