-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[AutoDiff] Handle materializing adjoints with non-differentiable fields #67319
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
Conversation
@asl @BradLarson could one of you invoke swift-ci tests? |
...6522-pullback-generation-when-tangentvector-of-input-contains-nondifferentiable-fields.swift
Outdated
Show resolved
Hide resolved
I am confused. I thought the discussion was that we need to fix adjoint generation for What is the test coverage for newly added code? |
@swift-ci please test |
I think I may have misunderstood how to go about fixing the issue. Are you suggesting that we never add non-differentiable fields to the adjoint for |
If we do make the fix in the adjoint generation code, then I believe we'd have to at least do the same for |
In unit tests, none. In the end to end tests, I added a tests for the path where we need to materialize an adjoint with non-differentiable fields. I should perhaps also have added a test for materializing normal adjoints with all differentiable fields. |
There are multiple questions that need to be answered:
So, instead of materalizing that bunch of zeros, why we simply cannot do the following (in
After all, we know that things should conform to |
Please ensure that all codepaths are covered by tests. |
Since the adjoint materialization code is shared by all instructions, we shouldn't need to handle things separately for different instructions. As for the handling of adjoint buffer handling, it looks like that code isn't actually called anywhere. |
Ah, indeed this is what I tried to do first and I ran into an issue with
It looks like I may have misinterpreted the meaning of the error, however. And I think I simply needed to change the type of the input arguments. I can definitely try this out. |
Would you know of an existing unit test that tests auto diff code? I wasn't quite sure how I can setup |
Allright, but what are those "different instruction"? Besides |
|
The |
Oh, I think I was mistaking unit tests for the ones that occur in the unittests directory in the Swift repo. |
Yeah it was the right one. I was a little bit surprised because I was thinking that only the left one needs to be taken as a pointer (or by reference). But I'm guessing this more generalized SIL definition exists to accomodate for different types that may want to conform to AdditiveArithmetic? |
I see the point. Indeed, I missed a number of tests that could be added with this change. I was thinking we can start by covering pullbacks for instructions listed here. Can you think of any others that I should also test? |
@asl I've incorporated this feedback about adjoint materialization and am creating a new PR revision for some early feedback. I'm still working on adding test coverage but am struggling a bit to understand what exactly the tests should be doing. I'll post my questions about tests in another comment shortly. |
@rxwei Could you also please take a look at the approach and comment whether it makes sense and whether I am making changes at the right place? |
Have you forgotten to push your new changes? |
I don't think so. I amended the existing commit, however, instead of creating a new one. |
...6522-pullback-generation-when-tangentvector-of-input-contains-nondifferentiable-fields.swift
Show resolved
Hide resolved
|
@@ -430,6 +430,8 @@ class PullbackCloner::Implementation final | |||
} | |||
case AdjointValueKind::AddElement: { | |||
auto baseAdjoint = val; | |||
assert(baseAdjoint.getType().is<TupleType>() || |
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.
This is not what should be checked here. This assertion is enforced during AddElement
construction. Here you need to ensure that all nested adjoints are of the same kind: either all tuples or all structs, but not the mixture.
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 see. Sorry I sent out that last revision in haste. Fixed in the latest revision and squashed commits.
addEltAdjValues.push_back(addElementValue); | ||
baseAdjoint = addElementValue->baseAdjoint; | ||
assert(baseAdjointType == baseAdjoint.getType()); | ||
} while (baseAdjoint.getKind() == AdjointValueKind::AddElement); |
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.
Do we have tests for this loop, btw? Maybe I'm missing something, but all loops are doing single struct/tuple extract?
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.
Not explicit tests but the other tests I have added do use this code path -- I needed to fix new test failures after making these changes. But I think it's a good idea to add explicit tests for this.
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.
@asl I have added tests exercising the nested add element adjoint materialization path.
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.
Thanks!
@swift-ci please test |
0cf3dbb
to
9343748
Compare
@asl Had to make some changes after I discovered some small bugs while compiling and running internal test suite. I have added tests for these cases as well. |
During internal testing we discovered 2 more bugs - 1. The element adjoint of a struct_extract can itself be an AddElement. 2. Indirect concrete adjoint materialization was missing a copy operation. This commit fixes these bugs and adds relevant test cases.
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.
Makes sense, thanks!
@swift-ci please test |
@asl looks like the "Swift Test Linux Platform" failed due to some network issues. Can we restart them? |
@swift-ci Please test Linux platform |
@swift-ci please test linux |
Huh I see the following error due to which the Linux tests failed -
Seems like something may be up with the Linux build hosts? |
@shahmishal Is CI down? |
@swift-ci please test linux |
This PR fixes a compiler crasher in AutoDiff.
The compiler used to crash while generating a pullback for differentiable functions that take a differentiable input, whose tangent vector contains non-differentiable fields.
Changes in this PR fix the issue by specializing adjoint materialization logic for user-defined tangent vectors containing non-differentiable fields.
Fixes #66522