-
Notifications
You must be signed in to change notification settings - Fork 0
UPSTREAM PR #16982: Mamba2 SSD #63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DajanaV
wants to merge
69
commits into
main
Choose a base branch
from
upstream-PR16982-branch_gabe-l-hart-Mamba2SSD
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
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
This reverts commit 00f115f.
* 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]>
…sors 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]>
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]>
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]>
e336e72 to
cb8e115
Compare
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.
Mirrored from ggml-org/llama.cpp#16982
DRAFT STATUS
This PR will remain in
Draftuntil 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_SCANop 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 ofSSM_SCANis 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
mlx-lm: https://github.com/ml-explore/mlx-lm/blob/main/mlx_lm/models/ssm.pyChanges
Introduce new primitive operations in
ggml:ggml_cumsum/ggml_cumsum_0: Perform a cumulative sum along a give dimensionggml_tri_dims/ggml_tri/ggml_tri_keep: Apply a triangular mask to the given matrixggml_tri_dimsggml_softplus: Perform the unarysoftplusoperationImplement an alternate path through
llm_graph_context_mamba::build_mamba2_layerwhen a multi-token update is detectedSSM_SCANin favor of the chunked pseudo-attention formulationDiscussion
There are a number of outstanding discussion points on this work that need to be resolved before moving it forward:
SSM_SCANwhich roundly defeats the purpose of the change! I suspect that the performance issues are due to the number ofggml_permute/ggml_contops that are added to the graph, but could use assistance figuring out how to eliminate them or identifying other sources of slowness.ubatchchunking 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.repeat_interleave: Similar to the issue that came up when initially implementingNemotronHsupport, I believe thatggml_repeatbehaves differently thanmx.repeat, resulting in incorrect results for models withn_groups > 1(tested withNemotronH).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 withn_groups == 1, I get nearly identical results to running with purelySSM_SCAN, butNemotronHstill struggles due torepeat_interleaveissues (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
mamba2implementation.