Hi,
To fix your original question,
You want to add the hook only if x.requires_grad == True.
There are no gradient computed and so to avoid the user registering a hook and having it not being called, we raise an error.
Note that since you override the values of x in a differentiable manner, the gradients for these entries will be zeroed out by the backward of x[:10,:] = 0. So you don’t actually need the hook here!