Repository navigation
Show the MoE cache hit rate in perf output and llama-server /metrics - #259
LeoBorcherding wants to merge 15 commits into
Conversation
- add MoE cache hits, misses and uploaded bytes to llama_perf_context_data, reset by llama_perf_context_reset - print the hit rate in llama_perf_context_print and common_perf_print - server: add llamacpp:moe_cache_* counters to /metrics and moe_cache_hits/misses to the request timings - cli: show the hit rate next to the prompt and generation speed
…red a decode The cache counters belong to the context, so with concurrent slots each request's delta included the other slots' lookups. /metrics keeps the totals.
…each decode /metrics can run on the queue worker during llama_process(), which raced with the cache updating its counters.
|
Pushed The per-request
Checked on an Apple M5 (Metal, granite-3.1-1b-a400m, Codex review on the mirror is clean at |
…ts device lookup counters Conflicts resolved by keeping both sides: llama_perf_context_data keeps upstream's n_reallocs_same / n_reallocs_diff and adds the MoE cache fields after them; llama-moe-cache.h keeps unslothai#268's declarations and adds llama_moe_cache_stats.
On the pinned unslothai#268 cache, ubatches of up to 32 tokens look up their experts on the device and count hits and misses in each group's state tensor, so the host-only stats reported 0 for decode. get_stats now adds them (the exit log reads the same counters). The misses help text also covers misses computed on the CPU.
…abled The snapshot now reads the MoE cache device counters, so skip it after each decode when nothing serves it.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
…ives sleep During sleep /metrics renders the cached server_metrics only, so the separate perf snapshot read 0, and a wake creates a new context whose counters restart. The totals now live in server_metrics, with the counts of the contexts destroyed by sleep carried over.
Those slots decode on the target context outside the shared batch, so /metrics stayed stale while they ran.
…as it is pin_contract checks that a later pin keeps every line an earlier pin adds.
…ice is done The CUDA getter copies on another stream, so get_stats now synchronizes each device first. The server refreshes /metrics only after a synced decode, and catches up an unsynced one (a prompt chunk without outputs, e.g. before a cancel) when the slots go idle.
|
Codex Review: Didn't find any major issues. Keep them coming! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
The cache is destroyed after the context backends, so synchronizing there crashed llama-server at shutdown with device lookups on (CUDA). The context already synchronizes before it is destroyed; only get_stats waits for the devices.
|
Codex Review: Didn't find any major issues. Delightful! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
… they do not wait for the device
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 73bda9bf39
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| llama_moe_cache_stats llama_moe_cache::get_stats() const { | ||
| const auto & s = pimpl->stats_small; | ||
| const auto & l = pimpl->stats_large; | ||
| const auto d = pimpl->device_stats(true); |
There was a problem hiding this comment.
Keep device synchronization out of the SIGINT perf path
On Unix CUDA runs with device-side MoE cache lookups enabled, SIGINT during CUDA graph capture now reaches this synchronized read: sigint_handler() in tools/completion/completion.cpp:63-77 calls common_perf_print(), which calls llama_perf_context(). device_stats(true) then synchronizes the cache backend through ggml_backend_cuda_synchronize(). Synchronizing a capturing stream is invalid and the operation is wrapped in CUDA_CHECK, so Ctrl-C can now trigger a CUDA abort instead of reaching the handler's _exit(130) path. Collect device counters at normal synchronization points and let the signal-handler performance path read a host snapshot.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in d6e67f4: while encode / decode runs, the perf read serves the device counts of the last read and makes no device call, so a SIGINT that lands mid graph (capture included) prints from the host copy. Reads outside a decode, the server's and the final perf print, still read the device.
…runs, so a SIGINT perf print never touches the device
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Can't wait for the next one! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |

--moe-cache-mibonly reports its hits and misses from its destructor, at INFO level. llama-cli shows them only with-lv 4and a clean exit. llama-server never shows them: Ctrl-C skips the line, and there is no metric. Once Unsloth Studio passes--moe-cache-mib auto, there is no way to tell whether the cache is helping on a given machine.The hit rate is the number that decides that. On an R9700 32 GB (gfx1201, Windows 11, HIP 7.1), with Qwen3.8-Flash-Next IQ1_M and
-cmoe, at 74db68e:-np 1)At 85% hits the cache uploaded 41.9 GB over 300 tokens and gained nothing. At 94% it doubled decode. From the outside, both runs look the same.
What this adds
llama_perf_context_data, and the hit rate in the perf output and on llama-cli's timings line.moe_cache_hits/moe_cache_missesin each server response's timings.llamacpp:moe_cache_hits_total,moe_cache_misses_totalandmoe_cache_uploaded_bytes_totalon/metrics.Pin model and base
The prebuilt is an aged upstream
b####tag plus the pins inscripts/unsloth/pr-set.json, merged in order; each pin is upstream plus one change. This PR reads the counters of the MoE cache pin (#268), so it is stacked on it: the base branchbase/pin268-38c731590is #268's current head, and the diff here is only this PR's 14 files. Inpr-set.jsonit has to come right after #268. If #268 is repinned with changes in these files, this branch needs another merge of #268's head (git merge --no-ff, #268's lines kept verbatim), as done for38c731590.Fixes on top of the original patch
/metricsand the perf data showed 0 for decode while-lv 4showed the real hit rate. The stats now add the device counters, read after the device has finished its work./metricsreads the counters only when--metricsis on; the totals survive--sleep-idle-seconds; TTS and realtime decodes refresh them too; prompt progress updates leave the counts out, so they never wait for the device.Evidence
Tiny MoE model (
stories15M_MOE, F16),-ngl 99 -cmoe --moe-cache-mib 64, three greedy completions, then/metrics.ROCm, gfx1151 (Radeon 8060S, Linux, HIP build from source), this branch at
3a56bdda7:/metricshits / misses / bytesLLAMA_MOE_CACHE_DEVICE=0)Same generated text both ways. The exit log counts more misses than
/metricsbecause it also covers the warmup before the perf counters are reset.CUDA, Colab L4, this branch at
d6e67f4e2: the same numbers as on ROCm with GPU lookups on and off (2331 / 10 / 13.27 MB, same text), hits across two sleep / wake cycles 778, 778, 1557, 1557, thellama-completionperf lineMoE cache hits = 98.29 % (574 / 584 experts, 12.66 MiB uploaded), and 12 Ctrl-C interrupts ofllama-completionmid generation all exiting with 130 and no CUDA error.CUDA, Colab T4, the original patch against these fixes (on the earlier #268 pin):
/metricswith GPU lookups (hits / misses / bytes)CPU: builds; the server tests for metrics, sleep and prompt progress pass (23 tests). Pin set simulation on
b13004with this PR right after #268: merges cleanly and adds no failures (the same pins fail with and without it).Not tested:
-np > 1, multi-GPU, Metal, Windows; speed of the per-decode counter read (it only runs with--metrics).AI disclosure from the original author: an AI assistant was used to write the patch, run the benchmarks and draft this PR; the numbers were checked against the logs.