Repository navigation
Inference | Hybrid prefix caching. - #3225
Merged
Merged
Conversation
Signed-off-by: Onur Yilmaz <oyilmaz@nvidia.com>
Signed-off-by: Onur Yilmaz <oyilmaz@nvidia.com>
Signed-off-by: Onur Yilmaz <oyilmaz@nvidia.com>
Signed-off-by: Onur Yilmaz <oyilmaz@nvidia.com>
Signed-off-by: Onur Yilmaz <oyilmaz@nvidia.com>
Signed-off-by: Onur Yilmaz <oyilmaz@nvidia.com>
Signed-off-by: Onur Yilmaz <oyilmaz@nvidia.com>
Signed-off-by: Onur Yilmaz <oyilmaz@nvidia.com>
lmcafee-nvidia
force-pushed
the
prefix-caching-mamba
branch
from
February 17, 2026 02:16
5944bd7 to
296d684
Compare
lmcafee-nvidia
force-pushed
the
prefix-caching-mamba
branch
4 times, most recently
from
March 3, 2026 20:15
27831b6 to
e4a98ee
Compare
Bug fixes: 1. conv_state save-before-read: extract initial conv states BEFORE causal_conv1d_varlen_states + tensor_masked_update overwrites the conv_state buffer. Previously, initial_conv_states was read AFTER the buffer was updated, so restored requests would see their own newly-computed final states instead of the pre-existing initial states, corrupting the convolution output. 2. cu_chunk_seqlens OOB: the SSM Triton kernels allocate per-chunk output arrays of size chunk_size (128). Passing cu_seqlens directly as cu_chunk_seqlens caused out-of-bounds memory access when any sequence exceeded chunk_size tokens. Fix: subdivide each sequence into chunks of at most self.chunk_size, producing correct cu_chunk_seqlens boundaries. 3. zxBCdt padding mismatch: after conv1d, the per-request loop rebuilt xBC with only real tokens while dt and z retained padded token count. This caused a shape assertion failure in the SSM kernel. Fix: strip padded tokens from zxBCdt before _ssm_prefill, then pad the output back to the original padded size for downstream residual add. 4. Per-request conv1d with initial_states: causal_conv1d_fn cannot accept both seq_idx and initial_states simultaneously. The old code passed seq_idx to handle multiple sequences but this zeroes state at sequence boundaries instead of using the cached initial states. Fix: loop over requests, calling causal_conv1d_fn per-request with initial_states and channels-last layout. Improvements: - Unify all Mamba prefill (including chunked) through single varlen SSM kernel call, removing separate chunked-prefill routing and the _batch_indices_chunked_prefill / _device_chunked_prefill metadata - Simplify _dynamic_inference to flat decode + prefill structure - Add _dynamic_inference_prefill helper that strips CUDA-graph padding from metadata and data tensors before calling _ssm_prefill - Remove deprecated constructor parameters (use_mem_eff_path, d_state, headdim, ngroups) and their warnings - Add assertion format string in ssd_combined.py for easier debugging Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…raction With chunk-aligned sequences (one sequence per chunk boundary), the final SSM state for each sequence is simply states[last_chunk_indices], making the separate chunk_state_varlen Triton kernel unnecessary. Construct last_chunk_indices in mamba_mixer.py alongside cu_chunk_seqlens and remove the cu_seqlens parameter from the varlen API since it was only needed by chunk_state_varlen. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
For prefix caching of Mamba layers, extract SSM and conv states at block-aligned chunk boundaries during varlen prefill. Since block_size_tokens % chunk_size == 0, every block boundary falls on a chunk boundary, making intermediate SSM state extraction pure indexing with no extra computation. Conv states are sliced from the pre-conv input tensor. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Contributor
Author
|
/ok to test ee7dfa7 |
chtruong814
approved these changes
Mar 18, 2026
Contributor
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/23231656658 |
Contributor
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/23233038271 |
github-merge-queue
Bot
removed this pull request from the merge queue due to failed status checks
Mar 18, 2026
Contributor
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/23234613368 |
ilml
added a commit
to ilml/Megatron-LM
that referenced
this pull request
Mar 20, 2026
…#3225) New files: - megatron/core/inference/contexts/kv_block_allocator.py - megatron/core/inference/contexts/mamba_slot_allocator.py - megatron/core/ssm/ops/causal_conv1d_varlen.py - megatron/core/ssm/ops/determinism.py - megatron/core/ssm/ops/ssd_bmm.py - megatron/core/ssm/ops/ssd_chunk_scan.py - megatron/core/ssm/ops/ssd_chunk_state.py - megatron/core/ssm/ops/ssd_combined.py - megatron/core/ssm/ops/ssd_state_passing.py - tests/unit_tests/inference/engines/test_mamba_prefix_caching_e2e.py - tests/unit_tests/ssm/ops/test_causal_conv1d_varlen.py - tests/unit_tests/ssm/ops/test_ops_init.py - tests/unit_tests/ssm/ops/test_ssd_bmm.py - tests/unit_tests/ssm/ops/test_ssd_chunk_scan.py - tests/unit_tests/ssm/ops/test_ssd_chunk_state.py - tests/unit_tests/ssm/ops/test_ssd_combined.py - tests/unit_tests/ssm/ops/test_ssd_state_passing.py - tests/unit_tests/ssm/ops/test_ssm_kernel.py
3 tasks
yangbofun
pushed a commit
to xlm-research/Megatron-LM
that referenced
this pull request
May 22, 2026
Signed-off-by: Onur Yilmaz <oyilmaz@nvidia.com> Signed-off-by: Keshav Santhanam <ksanthanam@nvidia.com> Co-authored-by: Onur Yilmaz <oyilmaz@nvidia.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Jon Barker <jbarker@nvidia.com> Co-authored-by: Keshav Santhanam <ksanthanam@nvidia.com>
yhgalaxy
pushed a commit
to yhgalaxy/Megatron-LM
that referenced
this pull request
Jun 17, 2026
Signed-off-by: Onur Yilmaz <oyilmaz@nvidia.com> Signed-off-by: Keshav Santhanam <ksanthanam@nvidia.com> Co-authored-by: Onur Yilmaz <oyilmaz@nvidia.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Jon Barker <jbarker@nvidia.com> Co-authored-by: Keshav Santhanam <ksanthanam@nvidia.com> Signed-off-by: yhgalaxy <yhgalaxy@outlook.com>
jon-barker
added a commit
to jon-barker/Megatron-LM
that referenced
this pull request
Jul 10, 2026
Signed-off-by: Onur Yilmaz <oyilmaz@nvidia.com> Signed-off-by: Keshav Santhanam <ksanthanam@nvidia.com> Co-authored-by: Onur Yilmaz <oyilmaz@nvidia.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Jon Barker <jbarker@nvidia.com> Co-authored-by: Keshav Santhanam <ksanthanam@nvidia.com> Signed-off-by: Jon Barker <jbarker@aws-cmh-slurm-1-vscode-02.cm.cluster>
terminator123
pushed a commit
to 021ai/Megatron-LM
that referenced
this pull request
Aug 3, 2026
Signed-off-by: Onur Yilmaz <oyilmaz@nvidia.com> Signed-off-by: Keshav Santhanam <ksanthanam@nvidia.com> Co-authored-by: Onur Yilmaz <oyilmaz@nvidia.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com> Co-authored-by: Jon Barker <jbarker@nvidia.com> Co-authored-by: Keshav Santhanam <ksanthanam@nvidia.com>
2 of 6 tasks
Connor-XY
added a commit
to Connor-XY/Megatron-LM
that referenced
this pull request
Sep 8, 2026
…on/determinism.py alloc_tile_workspace() and finalize_tile_workspace() were copied from the mamba repository in NVIDIA#3225 together with the autotune pinning, but nothing in Megatron-LM has ever called them (git grep over megatron/ and tests/ finds only the definitions; the Mamba2 training kernels come from the installed mamba_ssm package). Delete them. The live copy is mamba_ssm/utils/determinism.py with seven call sites (ssd_chunk_scan.py:1465,1582, ssd_chunk_state.py:860,868,917,986, ssd_combined.py:298), all zero_init=True. Measured there (mamba_ssm 2.3.2.post1, GB300, MAMBA_DETERMINISTIC=1): prefilling the workspace with NaN or leaving it torch.empty gives bit-identical y and all seven gradients of mamba_chunk_scan_combined across seven shape variants (partial chunks, ngroups=1 with chunk 256, the z path, initial states, seq_idx varlen, odd head dims) and under forced/free autotune block sizes -- every tile slot is written before finalize sums them. The zeroing costs about 2-3% of the SSM fwd+bwd (3.45 -> 3.33 ms at b=2, l=4096, h=64, p=64). The mamba repo can pass zero_init=False; one caveat for its owners is recorded in the clean-up summary (tile_count is derived from the smallest BLOCK_SIZE_N in the config list at import time, while the kernel writes one slot per launched N tile). Signed-off-by: Yan Xu <yxu1@nvidia.com>
Connor-XY
added a commit
to Connor-XY/Megatron-LM
that referenced
this pull request
Sep 8, 2026
…on/determinism.py alloc_tile_workspace() and finalize_tile_workspace() were copied from the mamba repository in NVIDIA#3225 together with the autotune pinning, but nothing in Megatron-LM has ever called them (git grep over megatron/ and tests/ finds only the definitions; the Mamba2 training kernels come from the installed mamba_ssm package). Delete them. The live copy is mamba_ssm/utils/determinism.py with seven call sites (ssd_chunk_scan.py:1465,1582, ssd_chunk_state.py:860,868,917,986, ssd_combined.py:298), all zero_init=True. Measured there (mamba_ssm 2.3.2.post1, GB300, MAMBA_DETERMINISTIC=1): prefilling the workspace with NaN or leaving it torch.empty gives bit-identical y and all seven gradients of mamba_chunk_scan_combined across seven shape variants (partial chunks, ngroups=1 with chunk 256, the z path, initial states, seq_idx varlen, odd head dims) and under forced/free autotune block sizes -- every tile slot is written before finalize sums them. The zeroing costs about 2-3% of the SSM fwd+bwd (3.45 -> 3.33 ms at b=2, l=4096, h=64, p=64). The mamba repo can pass zero_init=False; one caveat for its owners is recorded in the clean-up summary (tile_count is derived from the smallest BLOCK_SIZE_N in the config list at import time, while the kernel writes one slot per launched N tile). Signed-off-by: Yan Xu <yxu1@nvidia.com>
Connor-XY
added a commit
to Connor-XY/Megatron-LM
that referenced
this pull request
Sep 8, 2026
…on/determinism.py alloc_tile_workspace() and finalize_tile_workspace() were copied from the mamba repository in NVIDIA#3225 together with the autotune pinning, but nothing in Megatron-LM has ever called them (git grep over megatron/ and tests/ finds only the definitions; the Mamba2 training kernels come from the installed mamba_ssm package). Delete them. The live copy is mamba_ssm/utils/determinism.py with seven call sites (ssd_chunk_scan.py:1465,1582, ssd_chunk_state.py:860,868,917,986, ssd_combined.py:298), all zero_init=True. Measured there (mamba_ssm 2.3.2.post1, GB300, MAMBA_DETERMINISTIC=1): prefilling the workspace with NaN or leaving it torch.empty gives bit-identical y and all seven gradients of mamba_chunk_scan_combined across seven shape variants (partial chunks, ngroups=1 with chunk 256, the z path, initial states, seq_idx varlen, odd head dims) and under forced/free autotune block sizes -- every tile slot is written before finalize sums them. The zeroing costs about 2-3% of the SSM fwd+bwd (3.45 -> 3.33 ms at b=2, l=4096, h=64, p=64). The mamba repo can pass zero_init=False; one caveat for its owners is recorded in the clean-up summary (tile_count is derived from the smallest BLOCK_SIZE_N in the config list at import time, while the kernel writes one slot per launched N tile). Signed-off-by: Yan Xu <yxu1@nvidia.com>
Connor-XY
added a commit
to Connor-XY/Megatron-LM
that referenced
this pull request
Sep 9, 2026
…on/determinism.py alloc_tile_workspace() and finalize_tile_workspace() were copied from the mamba repository in NVIDIA#3225 together with the autotune pinning, but nothing in Megatron-LM has ever called them (git grep over megatron/ and tests/ finds only the definitions; the Mamba2 training kernels come from the installed mamba_ssm package). Delete them. The live copy is mamba_ssm/utils/determinism.py with seven call sites (ssd_chunk_scan.py:1465,1582, ssd_chunk_state.py:860,868,917,986, ssd_combined.py:298), all zero_init=True. Measured there (mamba_ssm 2.3.2.post1, GB300, MAMBA_DETERMINISTIC=1): prefilling the workspace with NaN or leaving it torch.empty gives bit-identical y and all seven gradients of mamba_chunk_scan_combined across seven shape variants (partial chunks, ngroups=1 with chunk 256, the z path, initial states, seq_idx varlen, odd head dims) and under forced/free autotune block sizes -- every tile slot is written before finalize sums them. The zeroing costs about 2-3% of the SSM fwd+bwd (3.45 -> 3.33 ms at b=2, l=4096, h=64, p=64). The mamba repo can pass zero_init=False; one caveat for its owners is recorded in the clean-up summary (tile_count is derived from the smallest BLOCK_SIZE_N in the config list at import time, while the kernel writes one slot per launched N tile). Signed-off-by: Yan Xu <yxu1@nvidia.com>
Draft
3 of 7 tasks
This branch was previously deployed
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 ?
Add Mamba state prefix caching for hybrid Transformer-Mamba models, enabling KV cache prefix sharing to also share corresponding Mamba conv/SSM states.
Key Features
--inference-dynamic-batching-prefix-caching-mamba-gbargument controls the memory budget for cached Mamba statesChanges
Core Implementation (
megatron/core/inference/contexts/dynamic_context.py,megatron/core/inference/engines/dynamic_engine.py):Block Allocator (
megatron/core/inference/contexts/dynamic_block_allocator.py):Arguments (
megatron/training/arguments.py):--inference-dynamic-batching-prefix-caching-mamba-gbparameterTest plan
tests/unit_tests/inference/contexts/test_mamba_prefix_caching.py)Contribution process
flowchart LR A[Pre-checks] --> B[PR Tests] subgraph Code Review/Approval C1[Expert Review] --> C2[Final Review] end B --> C1 C2 --> D[Merge]Pre-checks
Core 0.8)Code review
The following process is enforced via the CODEOWNERS file for changes into
megatron/core. For changes outside ofmegatron/core, it is up to the PR author whether or not to tag the Final Reviewer team.For MRs into `main` branch
Feel free to message or comment the @mcore-oncall to help accelerate your merge into main. The less complex your PR is, the faster it will be approved and merged!
(Step 1): Add PR label
Expert Review(Step 2): Collect the expert reviewers reviews
Expert Reviewlabel when your PR is ready for review.Final Review might get declined if these requirements are not fulfilled.
(Step 3): Final Review
Final Reviewlabel(Optional Step 4): Cherry-pick into release branch
If this PR also needs to be merged into
core_r*release branches, after this PR has been merged, selectCherry-pickto open a new PR into the release branch.For MRs into `dev` branch
The proposed review process for `dev` branch is under active discussion.MRs are mergable after one approval by either
eharper@nvidia.comorzijiey@nvidia.com.Merging your PR
Any member of core-adlr and
core-nemowill be able to merge your PR.