with torch.no_grad#21
Conversation
samuela
left a comment
There was a problem hiding this comment.
i'm hesitant to add a global variable but this may indeed be the simplest route. at some point we ought to also consider supporting/testing https://docs.pytorch.org/docs/stable/generated/torch.enable_grad.html, https://docs.pytorch.org/docs/stable/generated/torch.autograd.grad_mode.set_grad_enabled.html, and https://docs.pytorch.org/docs/stable/generated/torch.is_grad_enabled.html
| _RNG_POOPER_STACK.pop() | ||
|
|
||
|
|
||
| _GRAD_ENABLED = True |
There was a problem hiding this comment.
let's add a comment here documenting why this exists and what parts of the api it interacts with
There was a problem hiding this comment.
Replaced with forwarding torch._C._set_grad_enabled to its original implementation, which is better than this global variable.
It is somehow necessary, otherwise torch will complain about no __torch_function__ handler defined for torch._C._set_grad_enabled.
These are added |
|
BTW, I've changed |
samuela
left a comment
There was a problem hiding this comment.
nice! reusing torch's internal thing is still global state but hey at least it's not our global state :)
|
ci should be fixed after b958825 |
Great, thanks! |
samuela
left a comment
There was a problem hiding this comment.
it's worth mentioning that
with torch.no_grad():
jax.grad(t2j(torch.sin))(x)will be None or zero. it's debatable what should really happen here. i'm fine with merging as is and we can revisit later if necessary
When using
torch.no_grad, an error is raised complaining abouttorch._C.set_grad_enabledis not implemented forTorchish.This PR added a global variable to track whether gradient is enabled. Any new
Torchishelement created under theno_gradcontext will have its.valuewrapped withlax.stop_gradient.