After you called.



 ilkarman:

loss.backward()


And based on the fact your tensor T requires grad the gradients will be calculated for the entire T
You cannot make just part of a T getting the gradient. No way to set the mask. The info about the gradients is inside the computational tree = dynamic computational graph.
So, I think the only way is to zero the gradients like you did.