Skip to content

Fix checkpoint backward scheduling for chunked recurrences - #4907

Merged
zcbenz merged 1 commit into
ml-explore:mainfrom
adimalkar:fix/4837-checkpoint-ordering
Nov 24, 2026
Merged

zcbenz merged 1 commit into
ml-explore:mainfrom
adimalkar:fix/4837-checkpoint-ordering

Conversation

@adimalkar

Copy link
Copy Markdown
Contributor

Fixes #4837

Forward output gate recomputation and incoming cotangent updates along with synchronizing all required VJPs before sending back gradients up stream will be used as restrictions to prevent a buildup of previous computed states through out different chunks.

Use synchronization after selecting gradients to ensure that pruning of unused gradients continue. Update the public interface, and include a new recurrence memory regression test.

Validated on Linux CPU (Python suite passes, C++ suite passes) and no failures occur during pre-commit check. However, metal validation has not been validated.

The cotangent gate will inhibit further computation until it receives its incoming gradients. The requested VJP barrier ensures that the state gradient associated with the recomputed state does not unlock an earlier chunk until all the requested gradients have consumed all the recomputed states. In order to accomplish this both barriers are required: without the barrier a chain of state gradients can run ahead of time. Without the cotangent gate an early recomputations can still occur.

Peak backward memory usage at t = 256 / 512 for all gradients and k-only gradients were measured as 754 / 1302 MB and 623 / 1166 MB when using a chunk size of 32 on CPU. The addition of only the cotangent gate had no effect on the memory usage which remained essentially unchanged at 755 / 1302 MB and 622 / 1166 MB for all gradients and k-only gradients respectively. When only adding the barrier as a restriction, peak memory usage decreased to 212 / 314 MB and 243 / 344 MB for all gradients and k-only gradients respectively. Even though there was a large reduction in memory usage compared to previous results, k-only gradients continued to grow significantly. Therefore, combining both restrictions resulted in memory usage decreasing to 202 / 293 MB and 152 / 172 MB for all gradients and k-only gradients respectively. This demonstrates why both restrictions are necessary.

This solution limits how long previously computed states are stored. It does not affect how many full length SliceUpdate allocations are created by the original reproducers’ use of slice gradients.

  • ☑️ I understand it is strictly prohibited to use AI to write PR description
  • AI usage disclosure: Claude was used for experiments, implementation, and testing. I did the diagnosis, design discussion, and reviews

@mahimn01

Copy link
Copy Markdown
Contributor

I was working on #4837 in parallel and ended up with almost the same fix on my fork (checkpoint-memory-4837): an internal flag on CustomTransforms and depends(vjps, vjps) when more than one VJP is requested. So I'm glad to see this one up. A few things from my version are worth checking here.

  1. The new test only checks memory. Since the change reorders the backward graph, it would be good to also compare the gradients against the same loss without mx.checkpoint. On my branch they came out bit-identical, and an allclose check would catch it if the barrier ever changed the result.
  2. The cotangent gate in the checkpoint VJP runs for every checkpoint, including when only one gradient is requested. In my version the wait only happens when more than one gradient is requested. Your k-only numbers make a good case for the gate, but it would help to confirm that the initial-state-only path and a plain checkpointed MLP don't get slower or use more memory. A test case for the initial-state-only gradient would also guard that path.
  3. Timing. On an M1 Max with the issue script (H=32, D=128, chunk 32, fp32), synchronizing the VJPs made the all-gradient runs faster (1.09s to 0.84s at T=1024, 5.84s to 2.08s at T=2048), but the q-only gradient got about 15 to 25% slower because the kernels are tiny. Before and after times for your version would be useful too.
  4. Metal. That's where most people will hit this, so Metal numbers at T=1024 and 2048 would make the case stronger. For reference, with my version on the M1 Max the backward peak for all gradients went from 2686 to 1191 MiB at T=1024 and from 4897 to 2114 MiB at T=2048. I can run this branch on my machine and post the numbers if that's useful.

@adimalkar

Copy link
Copy Markdown
Contributor Author

Thanks @mahimn01, these are helpful points. And yes, I’d really appreciate you trying this branch on your M1 Max.

I ran CPU benchmarks using same-source builds of main and my branch. These are the median results from five runs:

Case Peak memory, main → fix Time, main → fix
All gradients, T=512 822 → 210 MiB 1.89 → 1.80 s
All gradients, T=1024 859 → 249 MiB 3.80 → 3.98 s
All gradients, T=1024, chunk 128 6010 → 707 MiB 5.50 → 4.57 s
k-only 1330 → 212 MiB 3.16 → 3.03 s
q-only 356 → 169 MiB 2.37 → 3.04 s
Initial-state-only 158 → 158 MiB 1.97 → 1.93 s
Checkpointed MLPs: 8×1024 and 16×512 Essentially unchanged Essentially unchanged

The initial-state-only path and the checkpointed MLPs showed no meaningful change in memory or runtime. I also see the q-only slowdown you mentioned—it persisted across three repeat benchmark runs. Memory still improves, but the runtime cost is worth calling out.

I have a gradient-equality test passing locally now, comparing the checkpointed and plain versions for both all-input and initial-state-only gradients. I still need to review and commit it; I’ll push that before you run the tests.

Once it’s up, it would be helpful to get:

  • Peak memory and runtime for main versus this branch on Metal at T=1024 and T=2048.
  • Whether the q-only slowdown also appears with this branch on Metal, and roughly how much.
  • Whether the memory and gradient-equality tests pass on your machine.

Also, since you independently worked out nearly the same fix and have helped review this one, I’d like to include you as a co-author if you’d like. Would you be happy with a Co-authored-by trailer on the next commit? If so, let me know which name and email you’d prefer—a GitHub no-reply address works too.

Thanks again for offering to test this on Metal.

@mahimn01

Copy link
Copy Markdown
Contributor

Happy to be a co-author, thanks! Please use Mahimn Patel <[email protected]>.

I ran your current head (bbc8c5d) against its base on main (fbedcee) on an M1 Max with Metal. Both are same-source builds with the issue script (H=32, D=128, chunk 32, fp32). Times are medians, and the MLP and q-only times come from interleaved runs because sequential runs were order-sensitive.

Case Peak memory, main → fix Time, main → fix
All gradients, T=1024 2686 → 1191 MiB 0.90 → 0.90 s
All gradients, T=2048 4909 → 2114 MiB 1.95 → 1.89 s
k-only, T=1024 2464 → 850 MiB 0.76 → 0.71 s
k-only, T=2048 4639 → 1125 MiB 1.86 → 1.50 s
q-only, T=1024 673 → 828 MiB 0.36 → 0.41 s
q-only, T=2048 1493 → 1274 MiB 0.73 → 0.86 s
q-only, T=1024, chunk 128 850 → 614 MiB 0.56 → 0.67 s
Initial-state-only, T=1024 490 → 488 MiB 0.48 → 0.45 s
Checkpointed MLP, 16×512 3008 → 1712 MiB 0.65 → 0.64 s

test_checkpoint_recurrence_memory passes on Metal. Checkpointed and plain gradients came out bit-identical (max abs diff 0) for all inputs, k, q, the initial state and a small MLP, so I expect your equality test to pass too. I'll run it once it's pushed.

The q-only slowdown shows up on Metal as well, about 15 to 17% at both lengths. q-only memory also goes up at T=1024 with chunk 32, but it goes down at T=2048 and at chunk 128. I tried two variations to see where the slowdown comes from. My branch has the same slowdown, and so does yours with the cotangent gate removed, so it seems to come from routing mx.checkpoint through custom_function_impl rather than from the gate. The gate does help q-only memory at chunk 128 (614 MiB with it, 983 without), so I'd keep it.

@zcbenz
zcbenz force-pushed the fix/4837-checkpoint-ordering branch 2 times, most recently from 5fd18df to 9aff72b Compare November 23, 2026 10:29
@adimalkar
adimalkar force-pushed the fix/4837-checkpoint-ordering branch from 9aff72b to a67e70b Compare November 23, 2026 10:48
Gate recomputation on forward outputs and incoming cotangents, and synchronize requested VJPs before passing gradients upstream. Together, these constraints prevent recomputed states from accumulating across chunks.

Apply synchronization after gradient selection to preserve pruning of unrequested gradients. Keep the public API unchanged and add a recurrence memory regression test.

Validated on Linux CPU: Python and C++ suites pass, and pre-commit is clean. Metal validation remains outstanding.

Co-authored-by: Mahimn Patel <[email protected]>
@zcbenz
zcbenz force-pushed the fix/4837-checkpoint-ordering branch from a67e70b to dd4d60f Compare November 23, 2026 23:57
@zcbenz
zcbenz merged commit 6793c9a into ml-explore:main Nov 24, 2026
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mx.checkpoint does not bound backward memory of a chunked recurrence when per-step inputs need gradients

3 participants