Description
I tried this code:
#![feature(autodiff)]
use std::autodiff::autodiff_reverse;
//#[no_mangle]
#[autodiff_reverse(df, Active, Active)]
fn primal(x: f32) -> f32 {
x * x
}
fn main() {
let x = std::hint::black_box(3.0);
let scalar = std::hint::black_box(1.0);
let (r1, r2) = df(x, scalar);
// f(x) = x*x => f'(x) = 2.0 * x
// 2.0*x*1.0 = 2*3= 6.0
assert_eq!(r2, 6.0);
// f(x) = x*x = 3*3 = 9
assert_eq!(r1, 9.0);
}
RUSTFLAGS="-Zautodiff=Enable" cargo run
I expected to see this happen: Rustc generates the function df, which I can call inside of main. The function df returns the derivative.
Instead, this happened: calling df just gives zeros, the assertion fails.
thread 'main' panicked at src/main.rs:16:5:
assertion `left == right` failed
left: 0.0
right: 6.0
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
The issue is, that we (for now) require lto=fat in your Cargo.toml.
If people forget to set it, they don't get an error, but instead all derivatives are zero. This is obviously frustrating since they don't get any warning and then need to figure out on their own what went wrong. A solution would be to either
A) Throw an error if we recognize Autodiff=Enable without fat-lto being enabled.
B) Just enable fat-lto, since we know better and should give users a good UX. Potentially we could also warn them, that we enabled it for them.
I lean towards option B). It should be sufficiently straightforward to implement, someone would need to grep through the rustc codebase to see where we parse settings, and overwrite the lto settings if autodiff is enabled.
There are already a few cases where we check for autodiff, looking like this:
let enable_ad = config.autodiff.contains(&config::AutoDiff::Enable);
I think it's a nice beginner issue, you don't need any rustc specific knowledge to understand our config parser, and it should be doable with a bit of try and error. If anyone is interested, here are build instructions to build rustc with autodiff Enabled.: https://rustc-dev-guide.rust-lang.org/autodiff/installation.html#build-instructions
The test case is just creating a small test project, posting the code above into src/main.rs, not enabling lto=fat in your cargo.toml, and executing it through:
RUSTFLAGS="-Zautodiff=Enable" cargo +offload run
If the test pass, you got it to work. Feel free to ping me here with questions.
Meta
rustc --version --verbose
:
self-build
Backtrace
<backtrace>