Skip to content

autodiff fails in impl blocks #139557

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

Closed
ZuseZ4 opened this issue Apr 8, 2025 · 8 comments · Fixed by #140104
Closed

autodiff fails in impl blocks #139557

ZuseZ4 opened this issue Apr 8, 2025 · 8 comments · Fixed by #140104
Assignees
Labels
C-bug Category: This is a bug. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. F-autodiff `#![feature(autodiff)]`

Comments

@ZuseZ4
Copy link
Member

ZuseZ4 commented Apr 8, 2025

I tried this code:
https://github.com/EnzymeAD/rustbook/blob/main/samples/tests/traits/mod.rs

#![feature(autodiff)]
use std::autodiff::autodiff;
trait Volumetric {
    /// Strain energy density
    fn psi(&self, j: f64) -> f64;
    /// Derivative of strain energy with respect to $J$
    fn d_psi(&self, j: f64, b_psi: f64) -> (f64, f64);
    /// The volumetric contribution to the second Piola-Kirchhoff stress is
    /// a scalar multiplied by $C^{-1}$ where $C = I + 2E$ in terms of the
    /// Green-Lagrange strain $E$. The derivative of $J$ with respect to $E$
    /// is $J C^{-1}$. We'll call the volumetric contribution that scalar
    /// multiple of $C^{-1}$.
    fn stress(&self, j: f64) -> f64 {
        let (_, d_psi) = self.d_psi(j, 1.0);
        d_psi * j
    }
}

struct Ogden {
    k: f64,
}
impl Ogden {
    pub fn stress_analytic(&self, j: f64) -> f64 {
        self.k * 0.5 * (j * j - 1.0)
    }
}
impl Volumetric for Ogden {
    #[autodiff(d_psi, Reverse, Const, Active, Active)]
    fn psi(&self, j: f64) -> f64 {
        self.k * 0.25 * (j * j - 1.0 - 2.0 * j.ln())
    }
}

fn main() {
    let j = 0.8;
    let vol = Ogden { k: 1.0 };
    let s = vol.stress(j);
    let s_ref = vol.stress_analytic(j);
    assert!((s - s_ref).abs() < 1e-15, "{}", s - s_ref);
}

I expected to see this happen: Passes the assertion

Instead, this happened:

➜  ad git:(main) RUSTFLAGS="-Zautodiff=Enable" cargo +enzyme run  --release
   Compiling ad v0.1.0 (/home/manuel/prog/ad)
error: autodiff must be applied to function
  --> src/main.rs:29:5
   |
29 | /     fn psi(&self, j: f64) -> f64 {
30 | |         self.k * 0.25 * (j * j - 1.0 - 2.0 * j.ln())
31 | |     }
   | |_____^

error[E0046]: not all trait items implemented, missing: `d_psi`
  --> src/main.rs:27:1
   |
7  |     fn d_psi(&self, j: f64, b_psi: f64) -> (f64, f64);
   |     -------------------------------------------------- `d_psi` from trait
...
27 | impl Volumetric for Ogden {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^ missing `d_psi` in implementation

For more information about this error, try `rustc --explain E0046`.

Meta

rustc --version --verbose:

build from source
Backtrace

<backtrace>

This is an easy bug and a got way to get started. It used to work a few weeks ago, so it's likely that just one of the other improvement PRs accidentally regressed this test, since it's not upstream yet. Supporting autodiff in impl blocks is of course important, so it would be nice to fix this regression.

To prevent further regressions, this autodiff invocation (under impl Volumentric for Ogden) should be added in tests/pretty/autodiff. We emit the error as dcx.emit_err(errors::AutoDiffInvalidApplication { span: item.span() }); in compiler/rustc_builtin_macros/src/autodiff.rs so the bugfix likely goes there, this PR could be an inspiration: https://github.com/rust-lang/rust/pull/138314/files

Tracking:

@ZuseZ4 ZuseZ4 added C-bug Category: This is a bug. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. F-autodiff `#![feature(autodiff)]` labels Apr 8, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Apr 8, 2025
@jieyouxu jieyouxu removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Apr 8, 2025
@SpencerWightman
Copy link

@rustbot claim

@ZuseZ4
Copy link
Member Author

ZuseZ4 commented Apr 19, 2025

@SpencerWightman Did you have a chance to look into the issue, or do you have any questions?

@SpencerWightman
Copy link

@ZuseZ4 Yes, I've made some progress. Sorry for holding it up so long. I'll make a PR today or drop the claim.

@Shourya742
Copy link
Contributor

Hi @SpencerWightman, I'm currently exploring autodiff and would be happy to collaborate on this if you're interested. Otherwise, please let me know if it's okay for me to take it on—I'm excited to contribute either way!

@ZuseZ4
Copy link
Member Author

ZuseZ4 commented Apr 20, 2025

@SpencerWightman no rush if you don't get to it today, but I hope to have a PR for this ready around the 22nd, so it has a bit of time to land before I enable autodiff on the 26th. Feel free to experiment with it a bit more, otherwise I can probably come up with a fix on the 22nd. @Shourya742 I pinged you in another issue with something you could look at if you have time.

@SpencerWightman
Copy link

@rustbot release-assignment
@ZuseZ4 @Shourya742 I don't want to hold this up any longer. Thanks for letting me have a shot at it.

bors added a commit to rust-lang-ci/rust that referenced this issue Apr 20, 2025
[DO NOT MERGE] start building enzyme on x86_64-gnu-llvm-{19|20} builders

My goal is to put this in CI on April 26, to have a week to land some of the outstanding PRs:
rust-lang#139700
rust-lang#139308
rust-lang#139557
rust-lang#140030
rust-lang#140049
The autodiff flags PR should land first, but otherwise they don't overlap and are mostly ready, so it shouldn't be too hard to land them. In the meantime, I'll experiment here with some builders.

r? `@oli-obk`

Tracking:

- rust-lang#124509

try-job: x86_64-gnu-llvm-19
try-job: x86_64-gnu-llvm-20
bors added a commit to rust-lang-ci/rust that referenced this issue Apr 20, 2025
[DO NOT MERGE] start building enzyme on x86_64-gnu-llvm-{19|20} builders

My goal is to put this in CI on April 26, to have a week to land some of the outstanding PRs:
rust-lang#139700
rust-lang#139308
rust-lang#139557
rust-lang#140030
rust-lang#140049
The autodiff flags PR should land first, but otherwise they don't overlap and are mostly ready, so it shouldn't be too hard to land them. In the meantime, I'll experiment here with some builders.

r? `@oli-obk`

Tracking:

- rust-lang#124509

try-job: dist-x86_64-linux
@Shourya742
Copy link
Contributor

@ZuseZ4, Is it ok now, if I take this up now?

@Shourya742
Copy link
Contributor

@rustbot claim

ChrisDenton added a commit to ChrisDenton/rust that referenced this issue Apr 22, 2025
…ils-on-impl-block, r=ZuseZ4

Fix auto diff failing on inherent impl blocks

closes: rust-lang#139557

r? `@ZuseZ4`
@bors bors closed this as completed in 264249f Apr 23, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Apr 23, 2025
Rollup merge of rust-lang#140104 - Shourya742:2025-04-21-auto-diff-fails-on-impl-block, r=ZuseZ4

Fix auto diff failing on inherent impl blocks

closes: rust-lang#139557

r? ``@ZuseZ4``
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. F-autodiff `#![feature(autodiff)]`
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants