i suggest using standard loop like
y_hat = model(y)
loss = loss_function(y_hat,y)
loss.backward()
with torch.no_grad():
                             optimizer.step()
                             optimizer.zero_grad()


with model doing all the forward calculation and loss_function computing loss just by using y_hat and y