mamba2 : Flatten in/out projections to dispatch GEMM instead of GEMV - #27513
Open
pskrunner14 wants to merge 2 commits into
Open
mamba2 : Flatten in/out projections to dispatch GEMM instead of GEMV#27513pskrunner14 wants to merge 2 commits into
pskrunner14 wants to merge 2 commits into
Conversation
Author
|
@gaugarg-nv can you please review the PR? thanks! |
gaugarg-nv
reviewed
Aug 21, 2026
gaugarg-nv
approved these changes
Aug 22, 2026
Author
|
@CISC can you please review PR? Thanks |
Member
|
I think there was a reason for this, I can't recall though, @ggerganov? |
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.
Overview
With CUDA backend, I noticed that Nemotron 3 Nano (30B A3B NVFP4) model was decoding slower than alternate baseline inference engine with concurrency npl>=8. Profiling showed that it was launching mul_mat_vec_* instead of GEMM, and was dominating GPU time ~40% at npl=32.
This was due to mamba-base.cpp reshaping activations to {n_embd, n_seq_tokens, n_seqs} 3d shape before ssm_in/ssm_out. So with decode n_seq_tokens==1 the dispatcher sees ne11==1 and treats it as column-batched GEMV for what is a dense GEMM.
This PR adds a minimal fix to keep the projections flat so we optimally dispatch GEMM instead of GEMV.
Additional information
Closes #27464
Perf Impact
Measured the performance impact of this change with relevant Mamba2 models with CUDA backend on GB10 (sm121) and RTX PRO 6000 (sm120). The results are shown below as text-generation tokens/s. The numbers are on master
78ec4c3withllama-batched-bench -npp 64 -ntg 128 -npl 1,8,32,256.GB10 (sm121)
Nemotron 3 Nano 30B-A3B (NVFP4):
Other Mamba2-based models (npl=32):
RTX PRO6000 (sm120)
Nemotron 3 Nano 30B-A3B (NVFP4):
Other mamba2-based models (npl=32):
npl=1 is unchanged for all models (same GEMV either way).
Tests
Correctness: greedy decode output is numerically identical with master
07822bddf, perplexity identical,test-backend-opsand ctest pass. Also tested with CPU backend, no regressions in perf and passing all tests.Requirements