The torch.no_grad() guard will make sure that Autograd doesn’t track any new operations applied inside the block. However, inplace operations on tensors, which are needed for gradient computation, can still be disallowed.
In your particular case a specific value tensor is needed during the backward pass to calculate the gradient, while you are modifying it inplace, which would yield the wrong result.