Oh sorry I tend to read partially 
The approach you are following is the simplest one IMO. In fact when you call backward you can see an iterative process like that so I don’t think it would have the performance.
Still you can use Module — PyTorch 1.10.0 documentation
register_full_backward_hook