This is not really a “small piece of code” haha
The way I would go about reducing this is:

First check that you do get .grad field populated (you cal del p.grad on a parameter to remove it and check after the call to .backward() that it is back again)
Check that this .grad field is full of 0s.
Start adding hooks in your forward function like out.register_hook(print) that will print the gradient for the Tensor out. You should have non-zero gradients at the complete end of the loss (it will be 1), then you can check that the network’s output does get non-zero gradient as well and then you can move up the network to see when it becomes 0 to see what is causing this