-
Notifications
You must be signed in to change notification settings - Fork 84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Raise when in place operations occur on leafs requiring grad #1458
Open
beverlylytle
wants to merge
11
commits into
main
Choose a base branch
from
check_inplace_leafs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b56dd80
raise if is_leaf and require_grad in inplace operations
beverlylytle d79173c
Merge branch 'main' into check_inplace_leafs
beverlylytle e4ad972
restore wraps
beverlylytle 551de30
add test and comment
beverlylytle a179d12
test thunder, not torch
beverlylytle a26ed67
add parameter to copy__meta
beverlylytle c279c14
Merge branch 'main' into check_inplace_leafs
beverlylytle 590ef18
Merge branch 'main' into check_inplace_leafs
t-vi 3fb1f53
apply function with compile data
beverlylytle cfd5143
pass grad_enabled bool instead of relying on compile data
beverlylytle ad518a5
Merge branch 'main' into check_inplace_leafs
beverlylytle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am wondering if Symbol
copy_
inthunder/torch/__init__.py
is more appropriate location for the check.lightning-thunder/thunder/torch/__init__.py
Lines 1961 to 1963 in 60f3ee1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a
andb
are proxies and it it not clear to me if a proxy knows that it is a leaf.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They do not. It's only a PyTorch concept that's available at runtime inside
_copy__impl
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, previously I missed that the fix was in
copy_impl
. And since, it is happening at runtime, I am wondering ifcompile_data
is actually available.Quick test shows (see below) that it wouldn't be. So, we probably need a way to check if this
copy
was called underno_grad
in users code (as PyTorch supports inplace of leaf tensors underno_grad
, see comment).Snippet to check if compile_data is available -
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, compile_data was not available, but now it should be with the added context manager in thunder/init.py
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is still incorrect because as discussed in #1486, the value of compile_data.is_grad_enabled here would be that of last updated state which can lead to incorrectness when used outside of tracing context.
We can see the discrepancy here.
So, whether the
copy
is in no_grad region needs to be captured during the tracing time.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, this is why I created the other issue. This PR fixes the leaf/grad issue when there is no annotation. When there is an annotation, another approach is required. This other approach may or may not involve using compile data in
_copy__impl
.As far as I understand, compile data is the medium for passing around data such as whether grad is enabled. But as the other issue points out, compile data reflects the end state of a function call and not the "live" state, at least at the time it reaches
_copy__impl
. So I'm left with the questions "are there other mechanisms for passing around whether grad is enabled?" "where else in the execution is it simultaneously knowable that a (1) leaf tensor (2) requiring grad is being (3) copied when (4) grad is enabled?" "is it feasible/desirable to make the compile data more dynamic?" "is there a way to context-manage the tensors so that theirrequires_grad
flags are set toFalse
when the interpreter seestorch._C._set_grad_enabled(False)
, and then later restored, thereby obviating the need for the compile data for this check?" Do you have suggestions for a fix that addresses both issues? Or can we close out this issue and move the discussion to the more involved issue?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So to tackle - leaf tensor requiring grad being copied into when grad is enabled, I think similar to a previous commit,
we can update
prims.copy
to take a argumentis_grad_enabled
. With this,ltorch.copy
will querycd.is_grad_enabled
and callprims.copy
by also passing this argument.lightning-thunder/thunder/torch/__init__.py
Lines 1984 to 1986 in 9de5434
With these changes, the
copy_impl
's signature will also change to acceptis_grad_enabled
and it will be called at runtime with a tensor which we can query if it is a leaf and also whether grad was enabled or not when calling that particular copy. Wdyt @beverlylytle?Though, I am curious if there is another approach to this - cc: @IvanYashchuk
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's see what the CI thinks.