Repository navigation
Fix checkpoint backward scheduling for chunked recurrences - #4907
Conversation
|
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
|
|
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
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:
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 Thanks again for offering to test this on Metal. |
|
Happy to be a co-author, thanks! Please use 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.
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 |
5fd18df to
9aff72b
Compare
9aff72b to
a67e70b
Compare
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]>
a67e70b to
dd4d60f
Compare
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.