Skip to content

llama_moe_cache: run everything without syncs - #32076

Open
am17an wants to merge 3 commits into
masterfrom
aman/moe-single-graph-cuda
Open

am17an wants to merge 3 commits into
masterfrom
aman/moe-single-graph-cuda

Conversation

@am17an

@am17an am17an commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Overview

This is the most complex piece of the running the MoE cache on devices where the syncs are expensive. On Vulkan or Windows there should be a huge speed-up. On CUDA on linux I see something like 10%.

The architecture looks a bit like this:

image

I've used GGML_OP_CUSTOM to do the signalling between host and the GPU.

Additional information

Requirements

@am17an
am17an requested review from a team, CISC and ggerganov as code owners November 11, 2026 09:44
@github-actions github-actions Bot added documentation Improvements or additions to documentation testing Everything test related ggml changes relating to the ggml tensor library for machine learning CUDA Related to the CUDA backend labels Nov 11, 2026
@zarbis

zarbis commented Nov 11, 2026

Copy link
Copy Markdown

Hey @am17an! Sorry for a tanget, but I'm trying to learn more about running MoE models efficiently and noticed updated moe-cache.md. What would be your advice for --load-mode if model doesn't fit in RAM? mlock or mmap+mlock? Or nothing but default mmap makes sense?

@am17an

am17an commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

mmap is the usually the way as you need to read from disk

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you forgot to include it in GGML_PUBLIC_HEADERS ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed the public header

@Green-Sky

Copy link
Copy Markdown
Collaborator

@zarbis Depends, none if it fits into ram. Otherwise if you are on linux dio, and mmap in any other case.

@zarbis

zarbis commented Nov 12, 2026

Copy link
Copy Markdown

Otherwise if you are on linux dio, and mmap in any other case.

@Green-Sky sorry again for hijacking this conversation, just wanna make sure if I need to file a bug report or not.

Trying to load 60GB model on 16+32 VRAM+RAM system using dio causes 100% memory usage and system freeze with load average flying into 100+. mmap is "fine" as in "not freezing an entire system".

If that is not expected behavior - I will open an issue with full details.

- moe-cpu-misses: pass the width to the warp shuffles, the HIP macros need it,
  and a 64-bit mask to __ballot_sync for ROCm 7
- musa: map cudaHostAllocMapped, cudaHostAllocPortable and cudaHostGetDevicePointer
- llama-moe-cache: const_cast the custom op userdata (-Wcast-qual)
- test-llama-archs: only the backend tests use 4 layers for the host experts archs,
  the models written with -o keep the layer count of the fusion baselines
- test-llama-archs: scale the layer split MoE cache budget by the number of GPUs,
  each GPU gets the budget of the single GPU tests
- libllama and the CUDA backend each keep a copy of the definitions
  (src/llama-moe-cpu-misses.h, ggml/src/ggml-cuda/moe-cpu-misses.cuh),
  no public ggml header
- get_api takes GGML_MOE_CPU_MISSES_VERSION, a backend with another version
  returns NULL and the MoE cache uses the split path
- the class count and the decay table size of the ClassLRU are fields of
  ggml_moe_cpu_layer, the CRF half-life stays in libllama
@am17an
am17an force-pushed the aman/moe-single-graph-cuda branch from fcc88a6 to fde147e Compare November 13, 2026 12:03

@am17an am17an left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ggerganov PTAL at the code structure, most of the complexity is behind the llama-moe-cache submodule and there are other minimal changes.

Comment on lines +5 to +6
// the MoE cache ops, obtained with ggml_backend_reg_get_proc_address(reg, "ggml_moe_cpu_misses_get_api")
// a copy of the definitions in src/llama-moe-cpu-misses.h: keep the two the same and bump GGML_MOE_CPU_MISSES_VERSION on a change

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not acceptable - it couples the ggml backend with the llama.cpp code in some very convoluted way.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this avoid new ggml ops, otherwise we would have to add 3 of them, is that okay?

@ggerganov ggerganov Nov 13, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better, but adding ops that have very narrow application generally should be avoided. I'm not sure if it is the case here - need to see what the ops look like and try to see if they would be useful to other projects.

Alternatively, we can try to think of some new ggml mechanism that would allow to inject custom ops with their backend implementations from user code. This way llama.cpp-specific ops could live in the project and do not pollute the ggml API. For Metal it's probably not difficult to come up with something since it can already compile code at runtime. But CUDA might be more complicated - not sure.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other way is to have a public header in ggml, src/ggml-ext.h which can avoid this coupling. I removed it earlier as I wanted minimal changes. If you're okay with that then it can be done this way. Later on we can come up with a solution to inject ops, but I don't think it's easy to do.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried ggml-ext.h at some point and realized that it does not really work: #21869. It only works for the examples/tools that are inside the ggml repo. llama.cpp is technically a 3rd-party project for ggml so it does not fall in this category.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at the branch, but I don't think it's good enough. I find it a bit difficult to follow the idea. The mechanism has to be something like a plugin system, though I am not sure exactly how the interface should look like. It probably has to be able to expose different implementations for the different backends. And some generic scheduling information (i.e. number of threads / workgroups). The overlap/join logic is probably a second iteration of the design - it's not even clear to me if it should be supported. None of the existing ops support this. Including the backend headers directly in user code is also something that should be avoided - we want to keep the implementation backend-agnostic.

Atm I don't see a clear path how to implement this. Either try to start from scratch with some simple and small plugin system where we can discuss details and prototype gradually. Or try to express the logic of this PR in some generic ops that would make sense to be added to the library.

I am also not sure what exactly we want to implement. Is it just new ops, or is it an entirely new mechanism for running 2 backends? Are the new ops some sort of workaround for the actual goal? The diagram in the OP is not enough to understand completely.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is to run everything in the GPU. The cache, as well as the CPU experts to run are signalled via device memory. Instead of 2 syncs per layer we have 0. Introducing the ops would be an easy way to do this imo. The ops can be used by any ggml user who uses moe cache, it can be diffusion or audio models which utilize this feature and have MoE models.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you think the ops could find broader application, the best way would be to add a standalone example in https://github.com/ggml-org/ggml/tree/master/examples to demonstrate how they can be used. Something very basic - almost like the matrix multiplication example: https://github.com/ggml-org/ggml/tree/master/examples/simple.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually since the MoE cache is based inside llama.cpp, it doesn't make a lot of sense if the ops are generic. So idk at this point, I don't want to leave this halfway but if we can't find a way to fit it into llama.cpp and so be it 🤷. @gaugarg-nv do you have ideas?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ggerganov I had AI spin-up a prototype like you asked am17an#73

@sanmai

sanmai commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Few rounds of greedy benchmarks with 2048 generated tokens on top of 71k context.

quant load mode CPU share master PR change
QFN UD-Q4_K_XL dio 0.95 53.46 55.06 +3%
QFN UD-Q4_K_XL dio 0 52.92 54.63 +3%
QFN UD-Q6_K_XL mmap 0.95 36.98 37.05 ~0%
QFN UD-Q6_K_XL mmap 0 21.12 21.44 0% (control)
  • Warm up rounds excluded
  • 5090, 128GB DDR4 (so can't pin the Q6 experts)
  • CPU share is for --moe-cache-cpu-share

@gaugarg-nv

Copy link
Copy Markdown
Contributor

FWIW, I am seeing around 25% perf improvement with this PR on RTX 5090 + Windows with Qwen 3.6-35B with 8 GB cache and 0 CPU share.

@am17an

am17an commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

@gaugarg-nv what improvement do you see with non-zero CPU share?

@gaugarg-nv

Copy link
Copy Markdown
Contributor

With 0.5 CPU share, I see 53% perf improvement with 4 GB cache.

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 documentation Improvements or additions to documentation ggml changes relating to the ggml tensor library for machine learning testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants