EnzymeAD / rust

A rust fork to work towards Enzyme integration
https://www.rust-lang.org
Other
53 stars 7 forks source link

Examples do not compile #71

Closed OwenTrokeBillard closed 4 months ago

OwenTrokeBillard commented 6 months ago

The examples do not compile.

I followed the build instructions for Linux and attempted to run one of the examples: cargo +enzyme run --example rosenbrock_fwd_iter. There are four compilation errors:

error: cannot find attribute `dup` in this scope
 --> examples/rosenbrock_fwd_iter.rs:4:17
  |
4 | fn rosenbrock(#[dup] x: &[f64; 2]) -> f64 {
  |                 ^^^
error[E0659]: `autodiff` is ambiguous
 --> examples/rosenbrock_fwd_iter.rs:3:3
  |
3 | #[autodiff(d_rosenbrock, Forward, DuplicatedNoNeed)]
  |   ^^^^^^^^ ambiguous name
  |
  = note: ambiguous because of a name conflict with a builtin attribute
  = note: `autodiff` could refer to a built-in attribute
note: `autodiff` could also refer to the attribute macro imported here
 --> examples/rosenbrock_fwd_iter.rs:1:5
  |
1 | use autodiff::autodiff;
  |     ^^^^^^^^^^^^^^^^^^
  = help: use `crate::autodiff` to refer to this attribute macro unambiguously
error[E0425]: cannot find function `d_rosenbrock` in this scope
  --> examples/rosenbrock_fwd_iter.rs:18:17
   |
4  | fn rosenbrock(#[dup] x: &[f64; 2]) -> f64 {
   | ----------------------------------------- similarly named function `rosenbrock` defined here
...
18 |     let df_dx = d_rosenbrock(&x, &[1.0, 0.0]);
   |                 ^^^^^^^^^^^^ help: a function with a similar name exists: `rosenbrock`
error[E0425]: cannot find function `d_rosenbrock` in this scope
  --> examples/rosenbrock_fwd_iter.rs:19:17
   |
4  | fn rosenbrock(#[dup] x: &[f64; 2]) -> f64 {
   | ----------------------------------------- similarly named function `rosenbrock` defined here
...
19 |     let df_dy = d_rosenbrock(&x, &[0.0, 1.0]);
   |                 ^^^^^^^^^^^^ help: a function with a similar name exists: `rosenbrock`

The second error states there are two definitions of the autodiff attribute:

Two competing definitions of the attribute suggests a configuration issue. Am I missing a step?

ZuseZ4 commented 6 months ago

That's not you, this branch is just a bit dated and unfortunately ended up having both the old (src/lib) and the new (builtin) autodiff frontend. The new branch is macro2, I will push a few more of my local changes after cleaning up. The new branch compiles and should today be ready to be tested on windows to resolve potential path issues, but for the examples to fully work I will need a bit more time to Cherry-pick all the changes from my last branch.

OwenTrokeBillard commented 6 months ago

All good. Thanks for your quick response. I'll wait for your changes to merge and then I'll resume my efforts to get it working on Windows. No rush.

ZuseZ4 commented 6 months ago

I just pushed a change to macro2 which now successfully builds and links Enzyme on Linux, so that should be a nice mwe to get it to work on windows, if you want to try. It uses / as path separator, but that's also done by the original code around it, so I assume it is not the reason for failing on windows. I will now work on getting the intermediate glue code ready.

ZuseZ4 commented 6 months ago

By now what's remaining is moving from the syn/quote/parse usage in library/autodiff/src to the rustc ast::MetaItem walking in compiler/rustc_builtin_macros/src/autodiff.rs, the rest should be ported over. I will try to get a first small example to work this evening.

ZuseZ4 commented 6 months ago

basic examples now indeed get expanded, the span metadata seems to still be wrong though, since it fails to compile with gibberish error messages. I'll have another look at it next week, after traveling.

fn rosenbrock(x: &[f64; 2]) -> f64 {
    let mut res = 0.0;
    for i in 0..(x.len() - 1) {
        let a = x[i + 1] - x[i] * x[i];
        let b = x[i] - 1.0;
        res += 100.0 * a * a + b * b;
    }
    res
}
fn d_rosenbrock(x: &[f64; 2], dx: &mut [f64; 2]) -> f64 {
    std::hint::black_box(rosenbrock(x), dx);
    std::hint::black_box(unsafe { std::mem::zeroed() })
}
motabbara commented 6 months ago

As an aside, src/bootstrap/src/core/build_steps/compile.rs wants to copy enzyme libs but assumes the library names have an .so extension, failing on Windows and macOS. I'll see if I can make a PR for it.

ZuseZ4 commented 5 months ago

@motabbara Ping in case that you have time to fix and test the extension on another system :)

ZuseZ4 commented 5 months ago

Also the macro2 branch is slowly getting in shape to replace the current main branch. I hope to get to it this weekend.

ZuseZ4 commented 4 months ago

Closing since the new macro implementation is now on Master and at least some examples compile in debug mode. It would be nice to port over old examples from https://github.com/EnzymeAD/rust/pull/75 at some point.