And another thing, correct me if I wrong, if you had two nn.Linear layers, you still have to do the hook only once because of the chain rule (and the multiplications of all the gradients according to it, correct ?)?

Hmm I probably misunderstood your original question. In my example I used two modules in sequence to demonstrate that it is possible to use hooks to selectively alter the gradients of a subgraph of the original network, and in this particular case, so that the first module’s parameters remain the original gradient, but the second module’s gradients are a tenth of what they would’ve been.
We have to register a hook individually for each tensor (aka parameter) in module 2 because if we register the hook directly on the tensor, it triggers when grad is computed for that tensor, and alters only the gradient computation for that individual tensor.
I actually don’t quite understand what you are going for yet, but if you wanted to alter the gradients of both nn.Linear layers you’d need to individually register hooks for four tensors. Or do “for each mod.parameters()” twice.

And that is possible if I can indeed to check through the backward path using a hook the current layer name

Not for hooks registered directly on a tensor. But you do still have access when you loop through the parameters of the module itself.