You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For a function decorated with torch.no_grad, the compile data of the jitted version has is_grad_enabled set to True when I would expect it to be False.
Code sample
importtorchimportthunder@torch.no_graddeff(x):
print(torch.is_grad_enabled())
returnx*2jf=thunder.jit(f)
x=torch.ones((2,2))
f(x) # prints Falsejf(x) # prints Truethunder.compile_data(jf).is_grad_enabled# True
The text was updated successfully, but these errors were encountered:
# This is how the thunder interpreter sees the above function.deff(x):
torch._C._set_grad_enabled(False)
print(torch.is_grad_enabled())
result=x*2# CompileData.is_grad_enabled = False only for this part.torch._C._set_grad_enabled(True)
returnresult
We can verify this from thunder.last_traces, adding the following lines to the above script
# Verifying from tracetraces=thunder.last_traces(jf)
print(traces[0])
So, we can see that at the end of the function, we have this _set_grad_enabled(True) which sets the CompileData.is_grad_enabled to True.
CompileData.is_grad_enabled will only be False between the no_grad region. So, if ltorch.mul or other operator queried this state, it would have seen False.
NOTE - The Symbols which care about this should query this state during Tracing. Post that is_grad_enabled will reflect the last state it was updated to.
Also, the reason we see print(torch.is_grad_enabled()) printing True is because, our lookaside for _set_grad_enabled only updates the state in CompileData and doesn't actually call torch._C._set_grad_enabled which would have updated the state for PyTorch. So, PyTorch never knows this while tracing and hence is_grad_enabled returns True. (I am not sure if we do/want to support printing during tracing).
🐛 Bug
For a function decorated with
torch.no_grad
, the compile data of the jitted version hasis_grad_enabled
set to True when I would expect it to be False.Code sample
The text was updated successfully, but these errors were encountered: