Hard question for me, it’s c++ based. Anyway I’m not really familiarized with torch core. @ptrblck may help you. Looks like https://github.com/pytorch/pytorch/blob/f8086845aacdb6e5c3e46313899105255a5a9822/torch/nn/grad.py 49 may be relevant but I don’t really know where is it exactly called
Consider that overwriting pytorch is not forward-compatible so the option of modifying gradients before backpropagating seems more secure for me (and compatible with further versions of pytorch), in fact they developed those tools not to modify the core.