Skip to content

fix(gemma4): release mlp_norm_manager after the layer MLP - #265

Open
EkhiAzur wants to merge 1 commit into
modelscope:mainfrom
EkhiAzur:fix/gemma4-release-mlp-norm-manager
Open

EkhiAzur wants to merge 1 commit into
modelscope:mainfrom
EkhiAzur:fix/gemma4-release-mlp-norm-manager

Conversation

@EkhiAzur

Copy link
Copy Markdown

What does this PR do?

Gemma4TransformerLayer._forward_mlp replaces mcore's TransformerLayer._forward_post_mlp but skips its last step: clearing self.mlp_norm_manager. _forward_pre_mlp_layernorm creates that manager, and FineGrainedActivationOffloadingInterface.__init__ stores the wrapped tensor on it unconditionally (self.tensor = tensor, even when offload=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 full the retained graph is the recomputed one.

The change mirrors mcore's own cleanup (group_offload(...) followed by self.mlp_norm_manager = None). It is guarded with getattr(self, 'mlp_norm_manager', None) so a Megatron version without the attribute is unaffected. gemma4_unified registers Gemma4Loader, which sets Gemma4TransformerLayer as 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:

iteration with this change without
1 29.88 30.09
2 38.94 39.24
4 38.95 39.38
6 39.02 39.38

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 HyperConnectionTransformerLayer did not clear mlp_norm_manager after group_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_offload call is the same one mcore makes in that case). Not tested on Megatron versions below 0.19.2.

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]>
@hjh0119 hjh0119 self-assigned this Nov 19, 2026
@hjh0119

hjh0119 commented Nov 24, 2026

Copy link
Copy Markdown
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

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.

2 participants