-
Notifications
You must be signed in to change notification settings - Fork 14k
autodiff rlib handling #149033
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
Draft
ZuseZ4
wants to merge
1
commit into
rust-lang:main
Choose a base branch
from
ZuseZ4:autodiff-rlib
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.
+34
−2
Draft
autodiff rlib handling #149033
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| #![feature(autodiff)] | ||
| #![crate_type = "rlib"] | ||
| //@ needs-enzyme | ||
| //@ compile-flags: -Zautodiff=Enable -C opt-level=3 -Clto=fat | ||
| //@ build-fail | ||
|
|
||
| // We test that we fail to compile if a user applied an autodiff_ macro in src/lib.rs, | ||
| // since autodiff doesn't work in libraries yet. In the past we used to just return zeros in the | ||
| // autodiffed functions, which is obviously confusing and wrong, so erroring is an improvement. | ||
|
|
||
| use std::autodiff::autodiff_reverse; | ||
| //~? ERROR: using the autodiff feature with library builds is not yet supported | ||
|
|
||
| #[autodiff_reverse(d_square, Duplicated, Active)] | ||
| pub fn square(x: &f64) -> f64 { | ||
| *x * *x | ||
| } |
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.
Is
rustc_monomorphize::collector::autodiff::collect_autodiff_fneven necessary in the first place. Thecatch_unwindintrinsic also needs the function arguments to be codegened, yet it doesn't have any special casing in the monomorphization collector. Enzyme needs the function to differentiate to be in the same module, but I would expect fat LTO to run early enough to already cause that to happen. If you removecollect_autodiff_fnthen I would assume this special case incross_crate_inlineableisn't necessary either.Uh oh!
There was an error while loading. Please reload this page.
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.
@bjorn3 Unfortunately it seems to be. I've tried removing both and we are back to an old bug,
could not find source function.In the beginning we had this case when we'd differentiate
fto generateg, but then only callg, notf.During the lowering we'd realize that
fis unused, delete it, and then fail on the llvm-ir level, since we now can't generate the body ofgsince it depended onf. Our new intrinsic ingin theory references the source functionf, but it seems that the monomorphize adjustments are still needed.Uh oh!
There was an error while loading. Please reload this page.
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. Maybe the reason why it works for
catch_unwindis thatcatch_unwindaccepts function pointers rather than function items as arguments?Is it possible to replace fetching
#[rustc_autodiff]from the function to be differentiated with a regular argument (or const generic) passed to theautodiffintrinsic, pass a function pointer to theautodiffintrinsic and then get the symbol name from the function pointer value (under the assumption that the function pointer value is a compile-time known value, which should hold true for the#[autodiff_forward]and#[autodiff_backward]expansions) without involvingInstance?Edit: I got the source and differentiated function backwards.
#[rustc_autodiff]is applied to the differentiated function which contains theautodiffcall, so that one would already work. You would only need to replace the function item with a function pointer and adjustadjust_activity_to_abito accept the function pointer type rather than a concreteInstanceand then usefn_abi_of_fn_ptrinstead offn_abi_of_instance.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.
That might be a reason, though it feels weird to me that a fn item does not count as use, whereas a function pointer does. And yes, they should always be compile-time known for now.
We have to add support for dyn (vtable) in the future, though, which in other languages works by differentiating all candidates, and then at runtime looking up which primal function was given and picking the responding differentiated function. That might require extra work anyway, so I'm also fine with not considering it for now.
For reference, here is the current expansion of placing
#[autodiff_forward(...)]on a function_f1.cc @oli-obk for thoughts and @Sa4dUs who developed our new intrinsic. I think moving it shouldn't be too much work?