Your first code snippet should just work. Could you post more information how it doesn’t work for you? The second won’t work since weight has not ._grad attribute and .grad should be a Variable.
The optimizer just uses the .grad attribute. So you can manually inspect whether you successfully changed gradient after doing backward.
Also, it is worth noting if you do the hook approach, the gradients calculation of other variables that use weight.grad will also be incorrect.