jax.custom_remat#
- jax.custom_remat(f, f_fwd, f_rem, f_bwd, *, static_argnums=(), static_argnames=())[source]#
Wrap
fwith custom rematerialization behavior for reverse-mode AD.Where
jax.checkpoint()policies select saveable values by name, acustom_remat-wrapped function carries its own rematerialization rules, which can depend on the ambient checkpoint policy. Requires thejax_remat3implementation.- Parameters:
f – the function to wrap, called (or traced) for ordinary evaluation.
f_fwd – forward-pass rule under rematerialized differentiation, of signature
f_fwd(policy, *args) -> (out, res). It receives the ambient checkpoint policy along with the arguments off, and returns the primal output paired with residuals to save (which may beNone, to save nothing).f_rem – rematerialization rule, of signature
f_rem(res, *args) -> (out, res2). On the backward pass it receives the residuals saved byf_fwdand the arguments off, and recomputes the primal output paired with the residuals thatf_bwdneeds.f_bwd – backward-pass rule, of signature
f_bwd(res2, out_ct) -> args_ct, returning a tuple of cotangents with one entry per argument off.static_argnums – as in
jax.jit().static_argnames – as in
jax.jit().
- Returns:
A wrapped version of
fwith the same call behavior, but with the given rules applied when it is differentiated in reverse mode under rematerialization (e.g. underjax.checkpoint()). Forward-mode differentiation falls back to differentiatingf.