Skip to content

Conversation

@DajanaV
Copy link
Contributor

@DajanaV DajanaV commented Nov 4, 2025

Mirrored from ggml-org/llama.cpp#16982

DRAFT STATUS

This PR will remain in Draft until the items in the discussion section are resolved.

Description

This PR is a draft implementation of the Structured Statespace Duality described in the original mamba2 paper which reframes the SSM_SCAN op as a pseudo-attention operation. The paper describes it in great detail, but the short version is that when performing a multi-token scan, the recurrent formulation of SSM_SCAN is inefficient because it cannot parallelize over the sequence dimension the way an attention calculation can. With the SSD formulation, the logical attention matrix is decomposed into chunks and the state is updated at the chunk boundaries, allowing prefill to "jump" by the size of the chunk rather than proceed with tokens one-at-a-time.

Reference Links

Changes

  • Introduce new primitive operations in ggml:

    • ggml_cumsum / ggml_cumsum_0: Perform a cumulative sum along a give dimension
      • NOTE: This adds the ability to specify dimension on top of the implementation in #16623 and relaxes the need for contiguous rows
    • ggml_tri_dims / ggml_tri / ggml_tri_keep: Apply a triangular mask to the given matrix
      • NOTE: This adds the ability to specify dimension on top of the implementation in #16623 and relaxes the need for contiguous rows with ggml_tri_dims
    • ggml_softplus: Perform the unary softplus operation
  • Implement an alternate path through llm_graph_context_mamba::build_mamba2_layer when a multi-token update is detected

    • This path is the core of the SSD implementation and avoids calling SSM_SCAN in favor of the chunked pseudo-attention formulation

Discussion

There are a number of outstanding discussion points on this work that need to be resolved before moving it forward:

  1. Performance: Currently, this implementation appears to be significantly slower than simply using SSM_SCAN which roundly defeats the purpose of the change! I suspect that the performance issues are due to the number of ggml_permute / ggml_cont ops that are added to the graph, but could use assistance figuring out how to eliminate them or identifying other sources of slowness.
  2. To chunk or not to chunk: In this PR I have sub-ubatch chunking implemented. I had it mostly working before the corresponding discussion on Qwen3Next. The inter-chunk update would be needed anyway, so I didn't strip it out, but it would be fairly trivial to do so and might offer some performance improvements.
  3. Handling of repeat_interleave: Similar to the issue that came up when initially implementing NemotronH support, I believe that ggml_repeat behaves differently than mx.repeat, resulting in incorrect results for models with n_groups > 1 (tested with NemotronH).

Testing

I've tested this locally with various members of the Granite 4 family and with nvidia/NVIDIA-Nemotron-Nano-9B-v2. For the Granite 4 models with n_groups == 1, I get nearly identical results to running with purely SSM_SCAN, but NemotronH still struggles due to repeat_interleave issues (see above). I'll flesh out more testing results once we've worked through some of the above issues.

cc @compilade since I know this has been on your TODO list since the original mamba2 implementation.

ggerganov and others added 30 commits October 9, 2025 19:40
* gg/metal-mul-mat-fixes:
metal : fix mul-mm condition + fix mul-mv permuted kernels
Cherry-picked and edited from 7ec2df64a46f4697d9c95f3f07753c3e3b1926fa

The original commit contained the DELTA_NET op as well which I've removed
in this cherry-picked version.

Co-Authored-By: Piotr Wilkin <[email protected]>

Signed-off-by: Gabe Goodhart <[email protected]>
Branch: Mamba2SSD

Signed-off-by: Gabe Goodhart <[email protected]>
Branch: Mamba2SSD

Signed-off-by: Gabe Goodhart <[email protected]>
Branch: Mamba2SSD

Signed-off-by: Gabe Goodhart <[email protected]>
Branch: Mamba2SSD

Signed-off-by: Gabe Goodhart <[email protected]>
This should be using simd operations for better parallelism, but that will
come next.

Branch: Mamba2SSD

Signed-off-by: Gabe Goodhart <[email protected]>
Branch: Mamba2SSD

Signed-off-by: Gabe Goodhart <[email protected]>
Branch: Mamba2SSD

Signed-off-by: Gabe Goodhart <[email protected]>
* origin/master: (32 commits)
metal : FA support F32 K and V and head size = 32 (#16531)
graph : support cacheless embeddings with FA and iSWA (#16528)
opencl: fix build targeting CL 2 (#16554)
CUDA: fix numerical issues in tile FA kernel (#16540)
ggml : fix build broken with -march=armv9-a on MacOS (#16520)
CANN: fix CPU memory leak in CANN backend (#16549)
fix: add remark plugin to render raw HTML as literal text (#16505)
metal: add support for opt_step_sgd (#16539)
ggml : fix scalar path for computing norm (#16558)
CANN: Update several operators to support FP16 data format (#16251)
metal : add opt_step_adamw and op_sum (#16529)
webui: remove client-side context pre-check and rely on backend for limits (#16506)
[SYCL] fix UT fault cases: count-equal, argsort, pad OPs (#16521)
ci : add Vulkan on Ubuntu with default packages build (#16532)
common : handle unicode during partial json parsing (#16526)
common : update presets (#16504)
ggml : Fix FP16 ELU positive branch (#16519)
hparams : add check for layer index in is_recurrent (#16511)
ggml: Correct SVE implementation in ggml_vec_dot_f16_unroll (#16518)
CUDA: faster tile FA, add oob checks, more HSs (#16492)
...
Branch: Mamba2SSD

Signed-off-by: Gabe Goodhart <[email protected]>
Branch: Mamba2Perf

Signed-off-by: Gabe Goodhart <[email protected]>
Branch: Mamba2SSD

Signed-off-by: Gabe Goodhart <[email protected]>
Branch: Mamba2SSD

Signed-off-by: Gabe Goodhart <[email protected]>
* origin/master:
Add server-driven parameter defaults and syncing (#16515)
metal: optimise `GGML_OP_SUM` (#16559)
server : fix img token logs (#16595)
llama-quant: add support for mmproj (#16592)
CUDA: Changing the CUDA scheduling strategy to spin (#16585)
server : fix mtmd checkpoints (#16591)
metal : avoid using Metal's gpuAddress property (#16576)
vulkan: Add ACC_TYPE_VEC2 implementation (#16203)
CUDA + openCL: fix bug in accessing rms_norm->src while doing fusion (#16577)
vulkan: Support FA with K/V in F32 (#16543)
vulkan: Improve build time for MSVC (#16545)
CUDA: enable FA for FP32 KV cache (#16546)
CUDA: use fastdiv + ggml_cuda_mad for mmvf (#16557)
CUDA: add fp kernel for larger batch size MoE (#16512)
cuda : remove legacy copy-op pointer indirection code (#16485)
server : dynamic token limit for prompt cache (#16560)
Branch: Mamba2SSD

Signed-off-by: Gabe Goodhart <[email protected]>
Branch: Mamba2SSD

Signed-off-by: Gabe Goodhart <[email protected]>
Branch: Mamba2SSD

Signed-off-by: Gabe Goodhart <[email protected]>
Branch: Mamba2SSD

Signed-off-by: Gabe Goodhart <[email protected]>
Branch: Mamba2Perf

Signed-off-by: Gabe Goodhart <[email protected]>
@DajanaV DajanaV force-pushed the main branch 30 times, most recently from e336e72 to cb8e115 Compare November 17, 2025 21:08
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.

4 participants