Start documenting autodiff activities#148201
Conversation
This comment has been minimized.
This comment has been minimized.
|
Thanks, @ZuseZ4 ! I will have a look tomorrow. |
There was a problem hiding this comment.
Thanks @ZuseZ4 ! This already clarifies some things. Knowing which activity types are valid for forward/reverse mode is very helpful.
For me, it is still a bit unclear how the input/output annotations work in the reverse mode case where the output of the function is via a mutable reference function.
For example if we have a function f(x) = C * x^2 and we want to compute df / dx, is this how we would apply the macro?
#[autodiff_reverse(d_foo, Const, Active, Duplicated)]
fn foo(c: f32: x: f32, out: &mut f32){
c * x * x
}If so, is this how we call the function?
let C: f32 = 4.0;
let x: f32 = 3.0;
// store the result of foo
let mut foo_result: f32 = 0.0;
// shadow variable to store the dF/dx value
let mut dFoo_dx = 1.0;
d_foo(C, x, &mut foo_result, &mut dFoo_dx);
// I would expect dFoo_dx to be 2* 4 * 3 = 24
*[View changes since this review](https://triagebot.infra.rust-lang.org/gh-changes-since/rust-lang/rust/148201/df984edf44203c862e01b5a20c8092d5614d872e..a2dce774bc35a7fbafbe2d191a4eedf99023e17a)*
library/core/src/macros/mod.rs
Outdated
| /// if we are not interested in computing the derivatives with respect to this argument. | ||
| /// | ||
| /// We often want to track how one or more input floats affect one output float. This output can | ||
| /// be a scalar return value, or a mutable reference or pointer argument. In this case, the |
There was a problem hiding this comment.
To me, it is unclear what "this case" refers to. Based on the following text, I think this refers to the case when the output is a mutable reference or pointer argument. Is that right? If so, maybe something like the following would be more clear
| /// be a scalar return value, or a mutable reference or pointer argument. In this case, the | |
| /// be a scalar return value, or a mutable reference or pointer argument. In the latter case, the |
or
| /// be a scalar return value, or a mutable reference or pointer argument. In this case, the | |
| /// be a scalar return value, or a mutable reference or pointer argument. If the output is stored in a mutable reference or pointer argument, the |
| /// the output should be marked as active or duplicated and initialized to `1.0`. After calling | ||
| /// the generated function, the shadow(s) of the input(s) will contain the derivatives. If the | ||
| /// function has more than one output float marked as active or duplicated, users might want to | ||
| /// set one of them to `1.0` and the others to `0.0` to compute partial derivatives. |
There was a problem hiding this comment.
It is a bit unclear to me how the 1.0 and 0.0 are used to specify the partial derivatives. I would assume the output given 1.0 has it's derivative evaluated and the one 0.0 not, but I think it would be good to be explicit in the docs.
|
I think you're right with asking for a concrete example, the enzyme.jl (julia) docs have some as well: https://enzyme.mit.edu/julia/stable/#Reverse-mode I'll add two (in place and returning) examples. |
|
Awesome. Thank you! |
|
@kevinyamauchi sorry, it took me a bit. I added examples and addressed your nit, does that help? Any parts which you still find hard to understand? |
This comment has been minimized.
This comment has been minimized.
fa5746d to
170d256
Compare
This comment has been minimized.
This comment has been minimized.
|
r? @jieyouxu Since you seem to be quite active on cleaning up tests. |
|
Do you want to only express them as "examples", or do you mean actual doctests? |
|
I'd prefer them to be doctests, just to guarantee that our docs are always up to date. |
|
As doctests, that'd have to be gated with a cfg when building libcore (like #[cfg_attr(bootstrap_autodiff_enabled, doc = " ```rust")]
#[cfg_attr(not(bootstrap_autodiff_enabled), doc = " ```rust,compile_fail")]
/// use meow;
|
|
It seems like bjorn has already added such a feature here: #146598 |
|
@jieyouxu I've added those checks, however things are still failing due to autodiff depending on |
|
Hm, I think it's easiest to leave as examples only then. Otherwise you'd need to build the doctests against a compiler/library toolchain that has autodiff enabled with fat LTO IIUC |
9208be8 to
dadf836
Compare
|
Sad, but sure. I marked them as ignore, I guess that's what we want? |
This comment has been minimized.
This comment has been minimized.
dadf836 to
f5892da
Compare
|
r? libs |
|
@bors r+ rollup Can iterate more in the future but let's ship this initial version so users can have sth to refer to |
Rollup of 7 pull requests Successful merges: - #147171 (recommend using a HashMap if a HashSet's second generic parameter doesn't implement BuildHasher) - #147421 (Add check if span is from macro expansion) - #147521 (Make SIMD intrinsics available in `const`-contexts) - #148201 (Start documenting autodiff activities) - #148797 (feat: Add `bit_width` for unsigned `NonZero<T>`) - #148798 (Match <OsString as Debug>::fmt to that of str) - #149082 (autodiff: update formating, improve examples for the unstable-book) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of #148201 - ZuseZ4:autodiff-activity-docs, r=oli-obk Start documenting autodiff activities Some initial documentation of the autodiff macros and usage examples
Rollup of 7 pull requests Successful merges: - rust-lang/rust#147171 (recommend using a HashMap if a HashSet's second generic parameter doesn't implement BuildHasher) - rust-lang/rust#147421 (Add check if span is from macro expansion) - rust-lang/rust#147521 (Make SIMD intrinsics available in `const`-contexts) - rust-lang/rust#148201 (Start documenting autodiff activities) - rust-lang/rust#148797 (feat: Add `bit_width` for unsigned `NonZero<T>`) - rust-lang/rust#148798 (Match <OsString as Debug>::fmt to that of str) - rust-lang/rust#149082 (autodiff: update formating, improve examples for the unstable-book) r? `@ghost` `@rustbot` modify labels: rollup
…li-obk Start documenting autodiff activities Some initial documentation of the autodiff macros and usage examples
Some initial documentation of the autodiff macros and usage examples