Skip to content

Inference: Reduce mamba scratch space size by an order of magnitude. - #5863

Merged
sidsingh-nvidia merged 13 commits into
NVIDIA:mainfrom
sidsingh-nvidia:siddharth/reduce-mamba-scratch-size
Jul 23, 2026
Merged

sidsingh-nvidia merged 13 commits into
NVIDIA:mainfrom
sidsingh-nvidia:siddharth/reduce-mamba-scratch-size

Conversation

@sidsingh-nvidia

@sidsingh-nvidia sidsingh-nvidia commented Jul 17, 2026 •

Copy link
Copy Markdown
Contributor
  • I, the PR author, have personally reviewed every line of this PR.

What does this PR do?

When prefix caching is used, inference extracts mamba states into an intermediate scratch space before committing them to the dedicated prefix buffer. This scratch space is currently sized to hold 3 * MAX_REQUESTS mamba states, assuming a worst case where each request produces up to three states.

However, this is a significant overallocation. In a given engine step, we can only create max_tokens//block_size mamba states in the worst case. This is much smaller than the previous bound. For example, with max_requests=100, max_tokens=2048, and block_size=256: 3 * MAX_REQUESTS gives 300 scratch slots, while max_tokens//block_size gives only 8 - a ~37× reduction.

This PR tightens the upper bound to a function of max_tokens instead. This frees GPU memory that was previously reserved for scratch space, allowing more durable prefix cache slots to be allocated and thereby increasing the number of prefix caches that can be held in GPU memory.

⚠️ For major changes (either in lines of code or in its impact), please make sure to first share a design doc with the team. If you're unsure what's the best way to do so, contact @NVIDIA/mcore-oncall.

Issue tracking

For PRs from open-source community contributors:

  • New features: a linked issue is required. Please open a feature request and reference it here before submitting the PR.
  • Small updates (bug fixes, minor improvements): a linked issue is recommended and will accelerate the PR review process.

Linked issue:

Contribution process

Pre-checks

  • I have added relevant unit tests
  • I have added relevant functional tests
  • I have added proper typing to my code Typing guidelines
  • I have added relevant documentation
  • I have run the autoformatter.sh on my PR

Code review

Feel free to message or comment @NVIDIA/mcore-oncall to help accelerate your merge into main. The less complex your PR is, the faster it will be approved and merged!

All PRs start as draft. If you open a non-draft PR, it will be automatically converted to draft.

Step 1: Mark PR as "Ready for Review"

  1. When your PR is ready, click Ready for Review.
  2. An oncall reviewer is auto-assigned and expert reviewers are notified based on your changes.
    • Some PRs may jump straight to step 2. This is determined by .github/CODEOWNERS.

⚠️ Only mark as ready once merge-conflicts are resolved and the CI is passing.
Final Review might get declined if these requirements are not fulfilled.

Step 2: Final Review

For PRs that change megatron/core, once all expert reviewers have approved, the Final Review label is applied automatically and final reviewers are assigned.

For PRs outside megatron/core, this step is skipped.

Step 3: Approved

Once all required reviewers have approved, the Approved label is applied automatically.

Merge

Any member of mcore-engineers will be able to merge your PR.

@sidsingh-nvidia
sidsingh-nvidia requested review from a team as code owners July 17, 2026 18:31
@svcnvidia-nemo-ci
svcnvidia-nemo-ci marked this pull request as draft July 17, 2026 18:32
@github-actions

Copy link
Copy Markdown
Contributor

This PR has been automatically converted to draft because all PRs must start as drafts.

When you are ready for review, click Ready for Review to begin the review process. This will:

  1. Add the oncall reviewer (optional reviewer)
  2. Add required review teams based on your changes

See the contribution guide for more details.

@copy-pr-bot

copy-pr-bot Bot commented Jul 17, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@sidsingh-nvidia sidsingh-nvidia changed the title Reduce mamba scratch space size Inference: Reduce mamba scratch space size by an order of magnitude. Jul 17, 2026
@sidsingh-nvidia

Copy link
Copy Markdown
Contributor Author

/claude strict-review

Comment thread megatron/core/inference/contexts/mamba_slot_allocator.py
Comment thread megatron/core/inference/contexts/dynamic_context.py Outdated
Comment thread tests/unit_tests/inference/contexts/test_dynamic_prefix_caching.py
Comment thread megatron/core/inference/config.py Outdated
Comment thread megatron/training/arguments.py Outdated
@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/29983603449

@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/29989588761

@svcnvidia-nemo-ci

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started!

You can track the progress here: https://github.com/NVIDIA/Megatron-LM/actions/runs/30002440463

This branch was previously deployed

2 inactive deployments
public — 9fb837b6 Deployed Jul 23, 2026 by copy-pr-bot[bot] via finalize / publish-docs #4327
test — 9fb837b6 Deployed Jul 23, 2026 by copy-pr-bot[bot] via cicd-wait-in-queue #17384
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Approved All necessary approvals have been made complexity: low

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants