Scope of torch.no_grad()

what is the scope of with torch.no_grad:
in function1() I need to set [with torch.no_grad] again or it is not needed because calling function1 is in with torch.no_grad scope

… some code…
with torch.no_grad():
%%%%%%… some code…
%%%%%%self.function1() # disable gradient
%%%%%%… some code…
… some code …
self.function1() # gradient calculation done
… some code …

def function1(self):
… some code …

torch.no_grad() disables gradient calculation inside the body of the with statement (it acts just as an ordinary context manager, but it can be used as a decorator as well). Even if other functions are invoked inside the body, the operations defined by these functions will not be recorded by the autograd engine.

1 Like