This shouldn’t be the case.
Skimming through your code, I cannot find anything which breaks the graph or detaches the tensors.
Could you try to isolate the issue and post a code to reproduce this behavior?