Repository navigation
feat(recompute): add mamba to recompute_modules - #5463
yuchenwang3 wants to merge 3 commits into
Conversation
e7519a9 to
741c3ba
Compare
|
/ok to test 741c3ba |
|
/claude strict-review |
|
Strict Review Summary — Findings by severity: CRITICAL: 0 · IMPORTANT: 1 · SUGGESTION: 0. The change is small, follows the established selective-recompute pattern, correctly gates to training-only (self.training and inference_context is None, so the stateful SSM update is never re-executed in inference), and tensor_parallel.checkpoint already bypasses recomputation during CUDA-graph warmup/capture (random.py:647). The config allow-list, docstring, and validation updates are consistent. The mandatory unused-identifier check passes: recompute_mamba_mixer is read in forward, and the new functools/tensor_parallel imports are both used. Most impactful finding — [IMPORTANT] fp8/fp4 recompute path: The Mamba mixer runs fp8/fp4 GEMMs (in_proj/out_proj are TE linear modules; mamba_mixer.py:227 has an explicit fp8 assertion), but the new code unconditionally uses tensor_parallel.checkpoint. The established selective-recompute pattern (transformer_layer.py:824-842) branches on config.fp8 or config.fp4 and routes through te_checkpoint, which enters TE activation-recompute phase so fp8 amax/scaling state is not double-updated during the backward-pass recompute. Without this branch, fp8 Mamba-hybrid training re-runs the mixer fp8 GEMMs under a plain enable_grad context, double-updating amax history and silently corrupting fp8 scaling — invisible at bf16, harmful under fp8. Recommend mirroring the transformer-layer fp8 branch. Risk assessment — Low-to-moderate. For the common bf16/CP long-context case this is a clean, well-scoped memory/speed trade-off and is a no-op unless "mamba" is explicitly added to recompute_modules (fully backward compatible). The one gap is fp8/fp4, where fp8-scaling correctness is at risk. As the PR notes, it is still a draft pending convergence/loss-parity and peak-memory validation vs full recompute — that validation should include an fp8 run to exercise the path above. |
20eeb77 to
4811efa
Compare
|
@janEbert The CI run for |
Checkpoint the prepared mixer input while preserving the current residual scheduling and packed CP metadata. Assisted-by: OpenAI Codex Signed-off-by: Yuchen Wang <[email protected]>
414aed8 to
a683e8e
Compare
|
/ok to test a683e8e |
The residual-state tests bypass MambaLayer.__init__ with __new__. Set the default-off recompute flag in their shared fixture before the inherited forward path reads it. Assisted-by: OpenAI Codex Signed-off-by: Yuchen Wang <[email protected]>
|
@janEbert Fixed in e2cb2ac: the residual-state fixture bypasses Source-isolated CPU validation reproduced the 36 failures before the fix; all 44 cases pass afterward. The 14 recompute-routing checks and Black/isort/Pylint/Ruff also pass. Full package/CUDA validation remains pending because local collection is blocked by missing Triton. Could you run Assisted-by: OpenAI Codex |
|
/ok to test e2cb2ac |
|
@NVIDIA/core-adlr and @NVIDIA/core-nemo, could you review
Of 103 checks, 88 passed and 13 were skipped, including GB200 unit and integration tests and the Megatron-Bridge and NeMo RL downstream jobs. The two failures are Assisted-by: OpenAI Codex |
|
@janEbert The current head is 5f90cab, which includes the November 5 main merge. The earlier /ok to test 5f90cab |
What
Add
mambaas a selective activation-recompute target. With--recompute-granularity selective --recompute-modules '[mamba]',MambaLayercheckpoints the mixer (convolution plus selective SSM/SSD compute) instead of retaining its activations.Why
In Mamba-hybrid models the mixer activations can be a material part of long-sequence memory. Megatron already lets selective recompute target attention, MLP, MoE, and several GDN components, but not Mamba, leaving full-layer recompute as the only way to trade compute for this activation memory.
Implementation
mambato therecompute_modulesvalidation and documentation.mambais selected withmoe_shortcut_connection: shortcut MoE uses two-stage mixer execution and does not enter this checkpoint path.tensor_parallel.checkpointfor BF16/FP16/FP32.inference_contextandpacked_seq_paramsthrough both paths..grad()incompatibility.The option is default-off and does not change existing runs unless
mambais explicitly selected.Tests
October 27 fixture correction (
e2cb2ac21f)test_mamba_residual_state.pybuilds its ordinary and wide-residual layers with__new__, bypassingMambaLayer.__init__. Its shared fixture now initializesrecompute_mamba_mixer = False, matching the default eager path before the inherited forward reads that field.On macOS with PyTorch 2.14.1, a source-isolated CPU harness reproduced the same 36 failures (8 passes) without the fixture initialization; all 44 residual-state cases pass with it. The existing 14 source-method recompute routing checks also pass. These exercise the actual schedules and CPU residual/autograd operators while bypassing the GPU framework imports; they are not a normal-package or CUDA test run.
tools/autoformat.shpasses Black, isort, Pylint and Ruff; its optional mypy step could not run because mypy is not installed. Normal package collection on this host is blocked by missing Triton. Full CI for the updated commit remains pending; the prior/ok to testcovereda683e8e, not this follow-up.Earlier validation
The focused suite retains the 12 routing/bypass cases and adds three shortcut-MoE warning cases. Six new tests build a real
HybridModelwithMambaMixer, compare recompute-off versus selective logits and all parameter gradients, and verify that the mixer runs again during backward. The matrix covers TP1, TP2, TP2 with sequence parallelism, and both memory-efficient and explicit Mamba paths. The mixer and checkpoint functions are not mocked in these model tests.On the preceding head
6f88cf61288a, NVIDIA CI passed the focused suite and H100 integration jobs; the SSM bucket passed 387 tests with 9 skips. Those results do not cover the new tests.For the October 5 review update, Black, isort, Ruff, Pylint,
py_compile, andgit diff --checkpass. The repository formatter script exits successfully; its non-blocking mypy step still reports missing dependencies/stubs and an existingnormalizationoverride type error. The local macOS pytest attempt stopped during normal Core import because Triton is unavailable; no test cases ran. Linux CPU and CUDA validation of the new cases remain pending, and CI needs authorization for the updated head.An earlier 8×B200 run covered packed BF16 and FP8 training plus local Mamba CUDA-graph capture/replay. At sequence length 16K (CP1/DP8), selective and full FP8 recompute both completed without NaNs or OOMs and differed by about
1e-5relative loss. A 20-step BF16 selective/full comparison had a maximum absolute difference of2.37e-3; repeating selective with the same seed produced2.24e-3run-to-run variation, so the cross-mode difference was within the measured baseline nondeterminism.At the 24K BF16 boundary with the same model/topology, no recompute OOMed before completing its first step, while selective Mamba recompute completed both steps without NaNs (
10.58632 → 10.09310) with rank-0 peak allocated memory of 96.13 GiB and 102.09 GiB. At 32K both modes OOMed before a complete step. Because the failed baseline has no common completed step, this is capacity evidence rather than a like-for-like percentage-memory comparison.The intended CP8 matrix could not run because this environment did not expose a compatible deterministic attention backend, so these results are explicitly CP1/DP8 rather than CP8. Raw numbers and the CUDA-graph baseline comparison are in the PR discussion.
Assisted-by: OpenAI Codex