swift icon indicating copy to clipboard operation
swift copied to clipboard

[AutoDiff] Fix adjoints for loop-local active values

Open kovdan01 opened this issue 1 year ago • 20 comments

Fixes #78264

kovdan01 avatar Dec 27 '24 21:12 kovdan01

Tagging @asl @JaapWijnen @rxwei

kovdan01 avatar Dec 27 '24 21:12 kovdan01

@swift-ci please test

asl avatar Dec 30 '24 06:12 asl

@swift-ci please test

asl avatar Jan 13 '25 00:01 asl

Would be glad to see feedback from everyone interested

kovdan01 avatar Jan 15 '25 05:01 kovdan01

@swift-ci please test

asl avatar Jan 15 '25 06:01 asl

Would be glad to see feedback from everyone interested

kovdan01 avatar Jan 27 '25 18:01 kovdan01

Would be glad to see feedback from everyone interested

kovdan01 avatar Feb 04 '25 19:02 kovdan01

Would be glad to see feedback from everyone interested

kovdan01 avatar Feb 14 '25 06:02 kovdan01

Would be glad to see feedback from everyone interested

kovdan01 avatar Feb 20 '25 16:02 kovdan01

@kovdan01 I cherrypicked this change onto swift-DEVELOPMENT-SNAPSHOT-2025-02-20-a and built a toolchain. I used this toolchain to build all our internal swift projects, and everything builds successfully on macOS.

clackary avatar Feb 21 '25 23:02 clackary

The change looks ok to me. The explanation of the problem is at https://github.com/swiftlang/swift/issues/78264

Long story short: SSA values in the loop body should be considered as "new values" at each loop iteration. We used to reuse the adjoints for them before this patch (though we should create distinct adjoints on each loop iteration).

This used to work before due to a particular CFG generated for for-style loops. As a result we've been lucky and simply zeroed the adjoints in the loop header on each iteration (loop header BB was the first BB we encountered the adjoint, so it was initialized there and this zero-initialization happened on each loop iteration effectively producing new adjoint value).

However, this stopped to work for while-style loops as the CFG is different and while we produced adjoint initialization, it happened outside the loop and therefore the adjoint values got reused from the previous loop iteration.

@rxwei will you please take a look as a second pair of eyes? :)

asl avatar Feb 21 '25 23:02 asl

However, this stopped to work for while-style loops

To be a bit more precise: it not stopped working, it just never worked for repeat-while loops (if not never, at least, for several latest years). For regular while loops, we had the same situation that we observed with for loops, when the behavior looked correct because of a particular CFG.

kovdan01 avatar Feb 22 '25 07:02 kovdan01

@kovdan01 I cherrypicked this change onto swift-DEVELOPMENT-SNAPSHOT-2025-02-20-a and built a toolchain. I used this toolchain to build all our internal swift projects, and everything builds successfully on macOS.

@clackary Great, thanks for letting me know! BTW, does "everything builds successfully on macOS" imply that your internal tests are also passing?

kovdan01 avatar Feb 22 '25 07:02 kovdan01

your internal tests are also passing?

@kovdan01 No, just the compile-time checks for now. Further testing is more of a manual process at the moment, but I can get to testing a few projects this week.

clackary avatar Feb 25 '25 00:02 clackary

@rxwei Could you please take a look at this PR? Thanks!

kovdan01 avatar Mar 06 '25 15:03 kovdan01

@kovdan01 I was able to finish testing a bunch of our internal projects. The issue we discussed earlier was caused by a flaw in my invocation, so nothing to worry about there. This looks good from my end.

clackary avatar Mar 06 '25 23:03 clackary

@rxwei Could you please take a look at this PR? Thanks!

kovdan01 avatar Mar 13 '25 11:03 kovdan01

@swift-ci please smoke test

asl avatar Apr 02 '25 05:04 asl

@swift-ci please test

asl avatar Apr 07 '25 17:04 asl

@swift-ci please test

asl avatar Apr 08 '25 23:04 asl

@swift-ci please smoke test

asl avatar Apr 15 '25 09:04 asl