How does torch.no_grad work that makes inner tensors without grad?

Viewed 23

For example:

x = torch.tensor([1.], requires_grad=True)
with torch.no_grad():
  b = torch.tensor([1.])
  y = x * 2 + b

How do y and b know they should be initialized with requires_grad=False?

AFAIK, from the source code for torch.no_grad(), it will finally call torch._C._set_grad_enabled(False). I am stuck here because I do not know what happened with this call. Is it setting a global variable that tensors can access when initializing and thus tensors know it does not require gradient for now?

0 Answers
Related