-
Notifications
You must be signed in to change notification settings - Fork 14.1k
Autodiff Upstreaming - enzyme frontend #129458
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
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
☔ The latest upstream changes (presumably #129563) made this pull request unmergeable. Please resolve the merge conflicts. |
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 for the work on autodiff! I have some feedback and questions. As you may have gathered, I am not very knowledgeable about autodiff. If I ask some questions for more context / explanation, it might be good to encode them as comments in the impl itself for more context. So that if someone else (or even yourself) comes back later to try to change this impl, they are better equipped to figure out what this is doing.
EDIT: please ignore panic! -> bug! suggestions as that might not be available yet in macro expansion here.
9d8ec28 to
a989f28
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This is what I'm concerned about too, because AFAICT this PR really only touches the front-end things, and the code paths are gated behind unstable feature gates. Nothing is sticking out to me as something that can explode compile times. |
| let blackbox_path = ecx.std_path(&[sym::hint, sym::black_box]); | ||
| let noop = ast::InlineAsm { | ||
| asm_macro: ast::AsmMacro::Asm, | ||
| template: vec![ast::InlineAsmTemplatePiece::String("NOP".into())], | ||
| template_strs: Box::new([]), | ||
| operands: vec![], | ||
| clobber_abis: vec![], | ||
| options: ast::InlineAsmOptions::PURE | ast::InlineAsmOptions::NOMEM, | ||
| line_spans: vec![], | ||
| }; | ||
| let noop_expr = ecx.expr_asm(span, P(noop)); | ||
| let unsf = ast::BlockCheckMode::Unsafe(ast::UnsafeSource::CompilerGenerated); | ||
| let unsf_block = ast::Block { | ||
| stmts: thin_vec![ecx.stmt_semi(noop_expr)], | ||
| id: ast::DUMMY_NODE_ID, | ||
| tokens: None, | ||
| rules: unsf, | ||
| span, | ||
| could_be_bare_literal: false, | ||
| }; | ||
| let unsf_expr = ecx.expr_block(P(unsf_block)); | ||
| let blackbox_call_expr = ecx.expr_path(ecx.path(span, blackbox_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.
In theory, if this part is wrong, then we may generate a test that never terminates due to UB. I'm not sure exactly on which test (suite) the job gets stuck on. It seems unlikely for crashes tests because they don't enable the autodiff feature gates?
Do you have any "insider" infra info on which test suite this job gets stuck on? And do you know if any of the CI jobs have enzyme available (esp. |
|
Hmm, I only have the same info as you, the log of the CI run. From that it's pretty clear where it is wrong - it's |
|
That's the funny part. That crashes test looks like a trait system thing, which this PR doesn't really touch. Worst part is I can't repro the failure locally (it passes just fine). |
This comment was marked as resolved.
This comment was marked as resolved.
|
Might be interesting to see if this also happens on Linux. You'd need to disable |
|
Doing that over in #131685. |
|
Didn't fail in try job. So what if we try again. Surely changing nothing will lead to a different outcome. @bors r+ |
|
💡 This pull request was already approved, no need to approve it again.
|
|
☀️ Test successful - checks-actions |
|
...? ok bors whatever u say |
|
Finished benchmarking commit (785c830): comparison URL. Overall result: ✅ improvements - no action needed@rustbot label: -perf-regression Instruction countThis is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.
Max RSS (memory usage)Results (secondary 1.3%)This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesThis benchmark run did not return any relevant results for this metric. Binary sizeResults (primary 0.5%)This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Bootstrap: 781.98s -> 782.278s (0.04%) |
This is an upstream PR for the
autodiffrustc_builtin_macro that is part of the autodiff feature.For the full implementation, see: #129175
Content:
It contains a new
#[autodiff(<args>)]rustc_builtin_macro, as well as a#[rustc_autodiff]builtin attribute.The autodiff macro is applied on function
fand will expand to a second functiondf(name given by user).It will add a dummy body to
dfto make sure it type-checks. The body will later be replaced by enzyme on llvm-ir level,we therefore don't really care about the content. Most of the changes (700 from 1.2k) are in
compiler/rustc_builtin_macros/src/autodiff.rs, which expand the macro. Nothing except expansion is implemented for now.I have a fallback implementation for relevant functions in case that rustc should be build without autodiff support. The default for now will be off, although we want to flip it later (once everything landed) to on for nightly. For the sake of CI, I have flipped the defaults, I'll revert this before merging.
Dummy function Body:
The first line is an
inline_asmnop to make inlining less likely (I have additional checks to prevent this in the middle end of rustc. Iffgets inlined too early, we can't pass it to enzyme and thus can't differentiate it.If
dfgets inlined too early, the call site will just compute this dummy code instead of the derivatives, a correctness issue. The following black_box lines make sure that none of the input arguments is getting optimized away before we replace the body.Motivation:
The user facing autodiff macro can verify the user input. Then I write it as args to the rustc_attribute, so from here on I can know that these values should be sensible. A rustc_attribute also turned out to be quite nice to attach this information to the corresponding function and carry it till the backend.
This is also just an experiment, I expect to adjust the user facing autodiff macro based on user feedback, to improve usability.
As a simple example of what this will do, we can see this expansion:
From:
to
I will add a few more tests once I figured out why rustc rebuilds every time I touch a test.
Tracking:
try-job: dist-x86_64-msvc