Skip to content

Autodiff batching2 #139351

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

Merged
merged 2 commits into from
Apr 18, 2025
Merged

Autodiff batching2 #139351

merged 2 commits into from
Apr 18, 2025

Conversation

ZuseZ4
Copy link
Member

@ZuseZ4 ZuseZ4 commented Apr 4, 2025

I will rebase it once my first PR landed. done.
This autodiff batch mode is more similar to scalar autodiff, since it still only takes one shadow argument.
However, that argument is supposed to be width times larger.

r? @oli-obk

Tracking:

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) F-autodiff `#![feature(autodiff)]` S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 4, 2025
@rust-log-analyzer

This comment has been minimized.

@ZuseZ4 ZuseZ4 force-pushed the autodiff-batching2 branch from 4b84744 to 2935695 Compare April 4, 2025 23:01
@rust-log-analyzer

This comment has been minimized.

@ZuseZ4 ZuseZ4 force-pushed the autodiff-batching2 branch from 2935695 to 88389b5 Compare April 5, 2025 07:10
@rust-log-analyzer

This comment has been minimized.

@ZuseZ4 ZuseZ4 force-pushed the autodiff-batching2 branch from 88389b5 to ce3ab30 Compare April 9, 2025 01:00
@ZuseZ4 ZuseZ4 marked this pull request as ready for review April 9, 2025 01:03
@ZuseZ4
Copy link
Member Author

ZuseZ4 commented Apr 9, 2025

@oli-obk I'm almost done with features, but there are two paths forward here, so I'd appreciate some help with the design.

The *v variants (dupv, dualvonly) allow better vectorization, by accepting larger shadow arguments.
Each shadow of a slice &[type] is supposed to be width * num_elements_of_primal_slice * byte_sizeof(type) bytes large.
We currently don't support generics but we should keep them in mind, and we already support aliases.

If you look at rustc_codegen_llvm you'll see that I hardcoded the byte_size_of(type) to 4, since my tests use floats.
An upstream version of course needs to figure that out more reliably.

In my typetree work (the only part I have not upstreamed from my fork) I have a little bit of logic here to handle them, but I haven't used it yet to figure out the byte size, so I'm not sure if that's legal: https://github.com/EnzymeAD/rust/blob/322f2226c1f672c9b5e934b15d255ae0d66bd0e2/compiler/rustc_middle/src/ty/typetree.rs#L196

If you say it's too hard for now, I could merge a workaround which analyzes the types in the ast frontend, which wouldn't support aliases or generics, but at least could handle &[f32] vs &[f64]. It's getting late for me so I might miss something obvious, but I feel like we should be able to figure out the size in rustc_monomorphize to handle more than that.

Also, there are reasons due to which a user might specify a larger stride than what I'd compute by default,
so I'll allow users under all combinations to provide an extra integer after *v arguments, which would replace whatever we computed here. But this way they could easily index out of bounds, so I'll mark generated functions in that case as unsafe. Once we figured out the part above, I'll add the code and tests for this to clarify it.

Copy link
Contributor

@oli-obk oli-obk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generic code will require a lot of extra work anyway (we'll need to find the right trait bounds and such so that a generic function will never fail to monomorphize)

@ZuseZ4
Copy link
Member Author

ZuseZ4 commented Apr 9, 2025

Generic code will require a lot of extra work anyway (we'll need to find the right trait bounds and such so that a generic function will never fail to monomorphize)

If my old trick still work (and I don't see why not) it's trivial to add, as long as we don't move to the rustc_intrinsic which we discussed.

A dummy function calls the source function in a black-boxed call. In the past it used to do this with the same generics, in the latest rework from proc-macros to builtin_macros however I didn't implement it anymore. We could re-add it though.
So if you call df with T=f32, then df will (in it's dummy body) call f with T=f32.
Now on llvm-ir level I delete the function body, but I first safe which function we're calling.
So even if a source function is instantiated with different types, I always remember which instantiation of f belongs to which instantiation of df. Now, if we remove the body (which would help with the other things discussed) then of course we'd start from zero.

@ZuseZ4 ZuseZ4 force-pushed the autodiff-batching2 branch from ce3ab30 to ae6247c Compare April 10, 2025 09:22
@rust-log-analyzer

This comment has been minimized.

@ZuseZ4 ZuseZ4 force-pushed the autodiff-batching2 branch from ae6247c to d7c0c32 Compare April 16, 2025 21:37
@ZuseZ4 ZuseZ4 requested a review from oli-obk April 16, 2025 22:22
@oli-obk
Copy link
Contributor

oli-obk commented Apr 17, 2025

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Apr 17, 2025

📌 Commit d7c0c32 has been approved by oli-obk

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 17, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 17, 2025
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#139351 (Autodiff batching2)
 - rust-lang#139483 (f*::NAN: guarantee that this is a quiet NaN)
 - rust-lang#139498 (Ignore zero-sized types in wasm future-compat warning)
 - rust-lang#139967 (Introduce and use specialized `//@ ignore-auxiliary` for test support files instead of using `//@ ignore-test`)
 - rust-lang#139969 (update libc)
 - rust-lang#139971 (Make C string merging test work on MIPS)
 - rust-lang#139974 (Change `InterpCx::instantiate*` function visibility to pub)
 - rust-lang#139977 (Fix drop handling in `hint::select_unpredictable`)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 87a1635 into rust-lang:master Apr 18, 2025
6 checks passed
@rustbot rustbot added this to the 1.88.0 milestone Apr 18, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Apr 18, 2025
Rollup merge of rust-lang#139351 - EnzymeAD:autodiff-batching2, r=oli-obk

Autodiff batching2

~I will rebase it once my first PR landed.~ done.
This autodiff batch mode is more similar to scalar autodiff, since it still only takes one shadow argument.
However, that argument is supposed to be `width` times larger.

r? `@oli-obk`

Tracking:

- rust-lang#124509
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) F-autodiff `#![feature(autodiff)]` S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants