Hi,
p.data = x should work, but using the .data parameter is generally not recommended. Because yeah, if you already have @torch.no_grad() before the function, p.add_ does what you want in a simpler and more efficient way.
You can think of .data as something similar to detach(), when you ‘get’ the .data attribute, you are actually creating a new tensor that happens to share storage with the old tensor, but does not share the computational graph. If you ‘set’, the .data, attribute, it does handle it underneath so you do modify the original Tensor as you’d expect, but its going to be less efficient because you create more Tensor objects unnecessarily
Also subtle difference to note: something like @torch.no_grad(), doesn’t exactly turn off “gradient storage”. It just means when you perform an op on it, no new nodes get added to its backward graph. For example, you can still perform a .backward() within while @torch.no_grad() and .grad is still populated.