How does NoGradGuard works in Cpp?

This is more a conceptual question… Consider:

NoGradGuard guard;
b -= lr * b.grad();

I know that NoGradGuard will disable action recording for the next gradient calculation.
But how does it really works?


It actually sets a global flag that is checked by the backend whenever an op is done on a variable.
The guard itself saved the current status and set it to false in the constructor. And restore the saved status in it’s destructor. That way it is similar to a with torch.no_grad(): block in python.


This post was flagged by the community and is temporarily hidden.