EnzymeAD / rust

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

support self ty #110

Closed ZuseZ4 closed 5 months ago

ZuseZ4 commented 5 months ago

This doesn't introduce any new magic, but just supports self. Example:

struct MyStruct {
    x: f64,
    y: f64,
}

impl MyStruct {

    #[autodiff(df, Reverse, Duplicated, Duplicated, Duplicated)]
    fn f(&self, x: &[&f64], out: &mut f64) {
        *out = self.x * x[0] + self.y;
    }
}

with df being

impl MyStruct {

    #[rustc_autodiff]
    #[inline(never)]    
    fn f(&self, x: &[&f64], out: &mut f64) {
        *out = self.x * x[0] + self.y;
    }

    #[rustc_autodiff(Reverse, Duplicated, Duplicated, Duplicated, None)]
    #[inline(never)]
    fn df(&self, &mut MyStruct, x: &[&f64], dx: &mut [&f64], out: &mut f64,  dout: &mut f64) {...}
}

@jedbrown Feel free to make a pr with your own example against rustbook, then I can make sure that it passes CI.

jedbrown commented 5 months ago

Thanks! Is this deliberately using x: &[&f64] instead of x: &[f64]? As written, I think the reverse will be unsound due to writing through an immutable reference.

ZuseZ4 commented 5 months ago

No, it's just the last example I had used to develop the corresponding warning. If you add another example I will take that instead.