This line of code looks strange, as you shouldn’t have to reset the .requires_grad attribute of the output of the model:
out =d_ out.sum().requires_grad_()

Is this operation necessary or are you seeing a valid .grad_fn in d_out.grad_fn?
If that’s not the case, then the computation graph was detached in your model at one point and it would also explain the error you are seeing.