fix(fsdp): restore full-layer activation checkpointing for KV-shared models - #3677
Draft
athitten wants to merge 1 commit into
Draft
fix(fsdp): restore full-layer activation checkpointing for KV-shared models#3677athitten wants to merge 1 commit into
athitten wants to merge 1 commit into
Conversation
…models PR #3513 added a `not _has_kv_sharing` guard to the full-layer activation checkpointing branch. Gemma4 E2B/E4B have `num_kv_shared_layers > 0`, so they now fall back to `apply_submodule_checkpointing`, which deliberately skips `self_attn`. Attention activations across all 42 language layers stopped being recomputed and peak memory rose 16% on the nightly Gemma4-4B benchmark. The guard is not needed here. Gemma4 hands its shared layers `_Gemma4KVShareHolder`, whose `update()` is a pass-through, so replaying a whole block during backward does not write K/V twice. Measured on 2x GB200 with examples/vlm_finetune/gemma4/gemma4_4b_mock.yaml and activation checkpointing on: 87.37 GiB before, 70.06 GiB after, matching the pre-regression nightly value. Losses and gradient norms are identical to four decimals over 8 steps. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Signed-off-by: Abhishree <abhishreetm@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do ?
Fixes a 16% memory regression on the nightly Gemma4-4B benchmark by putting KV-shared
models (Gemma4 E2B/E4B) back on full-layer activation checkpointing.
The bug
Activation checkpointing can be applied two ways:
mlpand the norms individuallyThe sub-module path deliberately skips
self_attnfor KV-shared models.PR #3513 added
and not _has_kv_sharingto the whole-block branch. Gemma4 E4B hasnum_kv_shared_layers: 18, so it started taking the sub-module path instead. Attentionactivations across all 42 language layers stopped being recomputed and stayed resident:
Nothing crashed and the loss did not move, so only the memory metric caught it.
Why the guard is not needed
The guard exists because recomputing attention could write K/V into the cache a second
time. That does not happen here: Gemma4 passes its shared layers
_Gemma4KVShareHolder,whose
update()just returns what it was given. Replaying a block writes nothing.Evidence
2x GB200,
examples/vlm_finetune/gemma4/gemma4_4b_mock.yaml, activation checkpointing on:70.06 GiB matches the nightly's pre-regression value of
7.005e+01exactly, and lossesand gradient norms agree to four decimals across all 8 steps. This was purely a memory
regression, not a numerics change.
Changelog
parallelizer.py: drop theand not _has_kv_sharingguard so KV-shared models getwhole-block checkpointing again, keeping fix(checkpoint): preserve FSDP2 mixed precision during recompute #3513's mixed-precision-safe wrapper.
test_parallelizer.py:test_hf_native_candidate_with_kv_sharing_uses_full_layer_checkpointingnow asserts whole-block wrapping. fix(checkpoint): preserve FSDP2 mixed precision during recompute #3513 had rewritten this test to assert the regressed
behaviour, so it went green on the way in.
L2_Parallelism_VLM_Gemma4_KVShared_AC(2 GPUs, builds a smallE4B-shaped model in-process, nothing staged). It asserts every decoder block is
checkpoint-wrapped, and that gradients match an un-checkpointed run. It lands in the
parallelismfolder added by test(pp): add gemma4 parallelism parity tests [1/4] #3529, so no CI wiring changes are needed.Verified the new test fails on the regressed code:
and passes with the fix (
OK (63 gradients compared)).Peak-memory magnitude stays with the nightly benchmark — a proxy this small can't measure
it without a threshold that says more about the proxy than about the code.
Before your PR is "Ready for review"
Pre checks:
Additional Information
memory_usageregression onbasic/tracked-finetune-gemma4-4b-automodel-ac(nightly-gb200-finetune-automodel), first seen 2026-08-14.