Skip to content

[remat3] re-implement handling of checkpoint_name in custom_vjp fwd - #40309

Closed
mattjj wants to merge 2 commits into
jax-ml:mainfrom
mattjj:push-krlmspyotypy
Closed

[remat3] re-implement handling of checkpoint_name in custom_vjp fwd#40309
mattjj wants to merge 2 commits into
jax-ml:mainfrom
mattjj:push-krlmspyotypy

Conversation

@mattjj

@mattjj mattjj commented Aug 28, 2026

Copy link
Copy Markdown
Collaborator

Recover the support for policy-driving saving of residuals in custom_vjp fwd rules by using partial eval. This is a much more limited use of partial eval than under remat2, and it's essentially necessary since handling checkpoint_name in custom_vjp fwd rules basically means automatically unzipping the function into a fwd pass and a remat pass. By using the good ol' partial eval machinery, rather than bespoke stuff, regressions and unexpected bugs seem less likely.

This complexity, indeed the entire CustomVJPTraced.remat method, can be deleted when we migrate users to custom_remat or similar. But for now it's necessary to avoid the regression.

One diff from the remat2 behavior is that this doesn't work with symbolic_zeros=True on custom_vjp. It essentially can't because with remat3 we're doing the remat pass before autodiff, ie here we need to trace (and then unzip) the fwd during the remat pass, and the symbolic zeros information is only available during autodiff. An error is raised rather than silent failure.

Another issue here is we re-trace the fwd rule on each application of a rematted function. Again, that'll just disappear.

behavior remat2 this change
named value in fwd, matched policy (1) saved saved
fwd primal provenance, policy'd (2) fwd rule fwd rule
fwd primal provenance, no policy (2) fwd rule f
effects in fwd, matched policy (3) both passes both passes
effects in fwd, no policy (3) both passes bwd only
atomic-kernel fwd, no policy (5) res early never early
scan-in-fwd names (6) saved saved
unbalanced-cond-in-fwd names (6) recomputed recomputed
nested-jit-in-fwd names (6) saved saved
symz/with_logs + matched names (7) saved raises
offload names in fwd (8) offloaded offloaded

The numbers correspond to sections of custom_vjp_fwd_unzip_demos.py. I'm just leaving it here for my own future reference.

@mattjj mattjj self-assigned this Aug 28, 2026
@mattjj mattjj added the pull ready Ready for copybara import and testing label Aug 28, 2026
@mattjj
mattjj force-pushed the push-krlmspyotypy branch 3 times, most recently from c119b98 to 40ef336 Compare August 28, 2026 22:35
@mattjj
mattjj force-pushed the push-krlmspyotypy branch 2 times, most recently from 5330286 to 1f352de Compare August 28, 2026 23:46
mattjj added 2 commits August 29, 2026 12:05
Recover the support for policy-driving saving of residuals in custom_vjp
fwd rules by using partial eval. This is a much more limited use of
partial eval than under remat2, and it's essentially necessary since
handling checkpoint_name in custom_vjp fwd rules basically means
automatically unzipping the function into a fwd pass and a remat pass.
By using the good ol' partial eval machinery, rather than bespoke stuff,
regressions and unexpected bugs seem less likely.

This complexity, indeed the entire `CustomVJPTraced.remat` method, can
be deleted when we migrate users to custom_remat or similar. But for now
it's necessary to avoid the regression.

We're basically using partial eval to compile a custom_vjp(primal, fwd,
bwd) into a custom_remat(primal, fwd', rem, bwd).

One diff from the remat2 behavior is that this doesn't work with
symbolic_zeros=True on custom_vjp. It essentially can't because with
remat3 we're doing the remat pass *before* autodiff, ie here we need
to trace (and then unzip) the fwd during the remat pass, and the
symbolic zeros information is only available during autodiff. An error
is raised rather than silent failure.
@mattjj
mattjj force-pushed the push-krlmspyotypy branch from bd4e6d0 to c0485e0 Compare August 29, 2026 19:13
@mattjj

mattjj commented Aug 29, 2026

Copy link
Copy Markdown
Collaborator Author

I'm going to split this change into multiple PRs.

@mattjj mattjj closed this Aug 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kokoro:force-run pull ready Ready for copybara import and testing

2 participants