Skip to content

[baseten] fix(peft): extend recompute input-grad hook to HybridStack - #18

Merged
pstefa1707 merged 1 commit into
trainers-main-20260907from
baseten/peft-recompute-hybrid-stack
Jul 11, 2026
Merged

[baseten] fix(peft): extend recompute input-grad hook to HybridStack#18
pstefa1707 merged 1 commit into
trainers-main-20260907from
baseten/peft-recompute-hybrid-stack

Conversation

@pstefa1707

Copy link
Copy Markdown
Collaborator

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

  • Since megatron-core 0.19, HybridStack honours recompute_granularity='full' through the same reentrant tensor_parallel.checkpoint as TransformerBlock.
  • A reentrant checkpoint only records a backward node when one of its tensor inputs requires grad; parameters inside the chunk (the LoRA adapters) are invisible to autograd at apply time.
  • With a frozen base model at PP=1, the embedding output has requires_grad=False, so every checkpointed chunk cascades to grad_fn=None and the whole decoder drops out of the autograd graph.
  • maybe_enable_recompute_inputs_grad already fixes exactly this — but it only patched TransformerBlock, so pure transformers (Qwen, Kimi) work while every hybrid model silently trains nothing.

Fix

Patch HybridStack the same way as TransformerBlock (guarded import for older megatron-core without the hybrid block). Unit test added mirroring the existing TransformerBlock test.

Verification

On Nemotron-3-Super-120B-A12B LoRA (TP=8, EP=8, PP=1, B200, recompute_granularity='full'):

  • Before: grad_norm=0.0, decoder output grad_fn=None, 1 CheckpointFunctionBackward node in the loss graph (the MTP branch only).
  • After: 89 CheckpointFunctionBackward nodes, grad_norm 4.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.py passes (2 passed) in the trainer image venv; ruff check/format clean.

🤖 Generated with Claude Code

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
pstefa1707 force-pushed the baseten/peft-recompute-hybrid-stack branch from e991e3d to 4dcafcf Compare July 11, 2026 15:09
@pstefa1707
pstefa1707 merged commit 8e2d2db into trainers-main-20260907 Jul 11, 2026
4 checks passed
@pstefa1707
pstefa1707 deleted the baseten/peft-recompute-hybrid-stack branch July 11, 2026 15:19
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.
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.

1 participant