[baseten] fix(peft): extend recompute input-grad hook to HybridStack - #18
Merged
pstefa1707 merged 1 commit intoJul 11, 2026
Merged
Conversation
maybe_enable_recompute_inputs_grad only patched TransformerBlock, but since megatron-core 0.19 HybridStack honours recompute_granularity='full' through the same reentrant tensor_parallel.checkpoint. A reentrant checkpoint records a backward node only when a tensor input requires grad, so with a frozen base model (adapter-only training at PP=1) every hybrid decoder chunk dropped out of the autograd graph and LoRA gradients were silently zero. Patch HybridStack the same way as TransformerBlock. Verified on Nemotron-3-Super-120B-A12B LoRA (TP=8, EP=8, PP=1, B200): grad_norm goes from 0.0 to healthy values and loss descends on a repeated sample. Signed-off-by: Paras Stefanopoulos <paras@parsed.com>
pstefa1707
force-pushed
the
baseten/peft-recompute-hybrid-stack
branch
from
July 11, 2026 15:09
e991e3d to
4dcafcf
Compare
jerryhong21
pushed a commit
that referenced
this pull request
Aug 21, 2026
…18) maybe_enable_recompute_inputs_grad only patched TransformerBlock, but since megatron-core 0.19 HybridStack honours recompute_granularity='full' through the same reentrant tensor_parallel.checkpoint. A reentrant checkpoint records a backward node only when a tensor input requires grad, so with a frozen base model (adapter-only training at PP=1) every hybrid decoder chunk dropped out of the autograd graph and LoRA gradients were silently zero. Patch HybridStack the same way as TransformerBlock. Verified on Nemotron-3-Super-120B-A12B LoRA (TP=8, EP=8, PP=1, B200): grad_norm goes from 0.0 to healthy values and loss descends on a repeated sample.
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.
Problem
Adapter-only (LoRA) training of hybrid Mamba models (Nemotron 3 Super) returns a real loss but
grad_norm=0.0— every LoRA tensor gets zero gradients.Root cause
HybridStackhonoursrecompute_granularity='full'through the same reentranttensor_parallel.checkpointasTransformerBlock.requires_grad=False, so every checkpointed chunk cascades tograd_fn=Noneand the whole decoder drops out of the autograd graph.maybe_enable_recompute_inputs_gradalready fixes exactly this — but it only patchedTransformerBlock, so pure transformers (Qwen, Kimi) work while every hybrid model silently trains nothing.Fix
Patch
HybridStackthe same way asTransformerBlock(guarded import for older megatron-core without the hybrid block). Unit test added mirroring the existingTransformerBlocktest.Verification
On Nemotron-3-Super-120B-A12B LoRA (TP=8, EP=8, PP=1, B200,
recompute_granularity='full'):grad_norm=0.0, decoder outputgrad_fn=None, 1CheckpointFunctionBackwardnode in the loss graph (the MTP branch only).CheckpointFunctionBackwardnodes,grad_norm4.65 → 72.9 → 54.6 and loss 4.43 → 2.54 → 0.76 over repeated steps on a fixed sample; both LoRA A and B matrices receive gradients.tests/unit_tests/peft/test_recompute.pypasses (2 passed) in the trainer image venv; ruff check/format clean.🤖 Generated with Claude Code