Skip to content

CUDA: fix duplicate expert IDs - #30477

Draft
JohannesGaessler wants to merge 1 commit into
ggml-org:masterfrom
JohannesGaessler:cuda-mmid-fix-duplicate
Draft

JohannesGaessler wants to merge 1 commit into
ggml-org:masterfrom
JohannesGaessler:cuda-mmid-fix-duplicate

Conversation

@JohannesGaessler

Copy link
Copy Markdown
Contributor

See #29998 (comment) .

The CUDA code on master is assuming that each expert is used either 0 or 1 times. This PR provides a proof-of-concept for the minimal changes needed to correctly handle duplicate expert IDs. This requires some algorithmic changes needed for mmid.cu that I think are quite doable and would not add meaningful amounts of complexity. However, there are 2 relevant issues for performance:

  1. The mm_ids_helper kernel is launched with one CUDA block per expert. On master the amount of shared memory to store expert IDs is proportional to n_tokens because each expert can show up at most 1 time. With duplicate IDs this is now proportional to n_tokens*n_expert_used and I think that can already become problematic for real-life use cases in llama.cpp.
  2. The MMQ kernel is dynamically deciding whether or not a given output tile needs to be calculated based on the content of the IDs. On master the relevant width is at most n_tokens, anything beyond that can be excluded from the loop bounds/CUDA grid in host code. With duplicate IDs the GPU code now needs to consider a width of up to n_tokens*n_expert_used for the worst case. IIRC that was a difference of something like 1.5x or 1.7x.

To be clear: this PR is a PoC and is not in a state where it can be merged.

@am17an when you said in #26294 (review) that that PR fixes the issue, did you mean that you cherry-picked the commit and had a successful test-backend-ops run? When I cherry-picked it myself changes to mmid.cu were insufficient to fix the issue.

Requirements

@github-actions github-actions Bot added ggml changes relating to the ggml tensor library for machine learning CUDA Related to the CUDA backend labels Oct 13, 2026
@ngxson

ngxson commented Oct 13, 2026

Copy link
Copy Markdown
Collaborator

I'm interested: #19182

@JohannesGaessler

Copy link
Copy Markdown
Contributor Author

My understanding is that duplicate IDs are also relevant for #26631 . The PoC in this particular PR is not what I would want to merge. I think I know the correct way to do it and it would be compatible with skipping computation in case of expert ID -1. Can you spin out the CPU implementation and the new tests into a separate PR so that I have something on master that I can use as a point of reference for my implementation? To be clear: from my end there is no urgency since I think I can handle duplicate IDs and skipping in separate PRs without issue.

@ngxson

ngxson commented Oct 19, 2026

Copy link
Copy Markdown
Collaborator

Can you spin out the CPU implementation and the new tests into a separate PR so that I have something on master that I can use as a point of reference for my implementation?

That would be tricky: we cannot tell if the op must run on CPU or CUDA without looking at tensor data. That's why I need to do all backend in one single PR, otherwise test-backend-ops will need to be hacked a bit

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CUDA Related to the CUDA backend ggml changes relating to the ggml tensor library for machine learning

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants