Repository navigation
Conversation
Gemma4TransformerLayer._forward_mlp replaces mcore's _forward_post_mlp but did not clear mlp_norm_manager, which keeps the pre-norm input (with its autograd history) alive after the backward pass. Mirror mcore's group_offload + reset. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Collaborator
|
Thanks for the report, and sorry for the late review. I reproduced it. But this only copies half of upstream's tail and will drift again on the next mcore bump, so I'm folding the whole thing into a bridge helper in #269 |
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?
Gemma4TransformerLayer._forward_mlpreplaces mcore'sTransformerLayer._forward_post_mlpbut skips its last step: clearingself.mlp_norm_manager._forward_pre_mlp_layernormcreates that manager, andFineGrainedActivationOffloadingInterface.__init__stores the wrapped tensor on it unconditionally (self.tensor = tensor, even whenoffload=False). That tensor is the pre-MLP-norm input and carries an autograd history, so each Gemma4 layer keeps a reference to the graph of its last forward after the backward pass has finished. With--recompute_granularity fullthe retained graph is the recomputed one.The change mirrors mcore's own cleanup (
group_offload(...)followed byself.mlp_norm_manager = None). It is guarded withgetattr(self, 'mlp_norm_manager', None)so a Megatron version without the attribute is unaffected.gemma4_unifiedregistersGemma4Loader, which setsGemma4TransformerLayeras the layer class, so it goes through the same code and gets the same fix (read from the code; I did not run that model).Evidence
Unit test (
tests/test_gemma4_mlp_norm_manager.py, CPU only): a stub layer with the real interface contract. It fails without the change (the manager is still set after the forward, and the layer input is still referenced after backward) and passes with it.Memory, stock Gemma4 SFT through ms-swift main + mcore-bridge main, 1 x A100-64GB. Random-init model with the real 26B-A4B text shapes (hidden 2816, head_dim 256/512, 8 layers, 16 experts), padded batches of 2 x ~3500 tokens,
--recompute_granularity full,--moe_grouped_gemm,--moe_permute_fusion, bf16 optimizer states, 6 iterations, identical data (losses equal to 3 digits). Peak memory in GiB as logged by swift:That is about 0.3-0.4 GiB over 8 layers (~0.04 GiB per layer). The retained amount scales with the number of layers and the tokens per layer; I have only measured this one configuration.
Same bug class as NVIDIA/Megatron-LM#6338, where
HyperConnectionTransformerLayerdid not clearmlp_norm_manageraftergroup_offload()for the same reason.Existing Gemma4 tests still pass (
test_gemma4_per_layer_config.py,test_gemma4_ffpa_attn.py: 23 passed, 7 skipped, 0 failed together with the new test).Limits
Single GPU, no expert parallelism, one model size. Not tested with fine-grained activation offloading enabled (the
group_offloadcall is the same one mcore makes in that case). Not tested on Megatron versions below 0.19.2.