Skimming through the code I cannot find any obvious errors, so we would need your model definition to debug it.
No, the .grad attribute won’t be kept for non-leaf tensors by default.
If you want to inspect this gradient, you would have to call output1.retain_grad() before calling backward.