Repository navigation
llama : measure which host-expert copy mode is faster and keep it (stacked on #268) - #479
danielhanchen wants to merge 10 commits into
Conversation
…ole ahead or only the routed experts is faster, and keep the faster one (GGML_SCHED_PREFETCH_EXPERTS=0/1 forces either)
…out copies on the copy queue
…span, a sync between them hides the overlap with the ubatch before
…ce of the expert copies Co-authored-by: LeoBorcherding <borchborchmail@gmail.com>
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. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 245d8cf6ef
ℹ️ 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".
| pf_span.n++; | ||
| if (pf_step.end) { | ||
| ggml_backend_sched_synchronize(sched.get()); | ||
| pf_span.open = false; | ||
| if (prefetch_pick.report(cparams.n_ubatch, pf_span.mode, (ggml_time_us() - pf_span.t0)/1e6, pf_span.n)) { |
There was a problem hiding this comment.
Keep timed spans from including time between decode calls
With the MoE cache disabled and one full, output-free ubatch per llama_decode() call, the first whole-mode probe leaves pf_span.open set when decode() returns. If the next call is also eligible, it completes the span using the original timestamp, so caller idle time or work on another context is charged exclusively to whole-mode copying. This can permanently select routed copying even when whole copying is faster, since rechecking defaults to disabled. Abort an unfinished span at the decode boundary, or otherwise exclude time spent outside decoding.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 7f1f203: a span that is still open when a new decode call starts is now discarded, so time spent outside decode is never counted. If every call holds only one eligible ubatch, the whole-mode pair never completes and that size stays on routed.
| // which one is faster depends on the platform and the ubatch size (the cost of a copy call and of a sync against the extra bytes), | ||
| // so it is measured per ubatch size: one warm-up ubatch, then 4 timed ones in the order routed, whole, whole, routed (the order | ||
| // cancels the cost that grows with the KV position). The two whole ubatches are timed as one span without a sync between them: the |
There was a problem hiding this comment.
Remove mid-sentence hard wrapping from the new comments
The new explanatory comment splits sentences across fixed-width lines, including the sentence beginning here and continuing through line 11. Root AGENTS.md explicitly prohibits splitting sentences across lines and hard-wrapping comments. Keep each sentence on one line and apply the same correction to the other newly added wrapped comments.
AGENTS.md reference: AGENTS.md:L55-L59
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in e85fc54: the comments this PR adds (llama-prefetch-pick.h, llama-context.h and the test header) are now one sentence per line with no hard wrapping.
| if (GGML_SCHED_PREFETCH_EXPERTS != nullptr && *GGML_SCHED_PREFETCH_EXPERTS != '\0') { | ||
| prefetch_pick.forced = atoi(GGML_SCHED_PREFETCH_EXPERTS) != 0 ? 1 : 0; |
There was a problem hiding this comment.
Apply the forced expert-copy mode to encoder contexts
For a Nomic BERT MoE model with host-resident experts, GGML_SCHED_PREFETCH_EXPERTS=1 no longer enables whole-expert prefetching. Previously the constructor initialized prefetch_experts directly; now it only sets prefetch_pick.forced, and the assignment to prefetch_experts occurs exclusively in the decoder's ubatch loop. These models have no memory, so decode() immediately delegates to encode(), which never applies the forced mode, leaving prefetch_experts false throughout inference. Initialize the active mode from the override or apply it in the encoder path as well.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in fabd072: the forced mode is now set in the constructor and again in encode(), so GGML_SCHED_PREFETCH_EXPERTS=1 applies to encoder-only models as it did before. Encoder ubatches are not measured, so they use only the override or routed.
…ross decode calls The time of the caller between two decode calls was charged to the whole mode.
… contexts again The mode is set in the constructor and in encode, which the measurement does not cover.
|
@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: a695b5b3ad
ℹ️ 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".
| if (pf_span.open) { | ||
| prefetch_pick.abort(cparams.n_ubatch); | ||
| pf_span.open = false; |
There was a problem hiding this comment.
Bound retries when whole-mode spans cannot complete
With automatic selection and one eligible ubatch per decode() call (for example, n_batch == n_ubatch, output-free prompt chunks, and the MoE cache disabled), every whole-mode probe is aborted at the next call boundary and resets the measurement. The actual copy modes then repeat 0, 0, 1, 0, 1, 0, 1, ..., as confirmed with a harness using the decode boundary logic, even though current() continues returning routed mode. Consequently, half of subsequent chunks use whole-expert copying indefinitely, including on machines where it is substantially slower, and the probe synchronizations also continue indefinitely. Bound unsuccessful retries or defer starting the whole span until two eligible ubatches are available in the same call, so an unmeasurable workload falls back to routed copying.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 135c048: after 3 interrupted whole spans for a ubatch size, that size is decided as routed and whole is not probed again, so one eligible ubatch per decode call no longer alternates into whole-expert copies. GGML_SCHED_PREFETCH_RECHECK still restarts the measurement later.
…span is interrupted 3 times With one eligible ubatch per decode call the whole span never completed and every retry copied whole experts again.
|
@codex review |
1 similar comment
|
@codex review |
|
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". |
# Conflicts: # src/llama-context.h
Stacked on #268 (base is its branch). With
GGML_SCHED_PREFETCH_EXPERTSunset, each context now measures whether copying the host experts of large ubatches whole ahead of the routing, or only the routed experts, is faster on this machine, and keeps the faster mode.GGML_SCHED_PREFETCH_EXPERTS=0/=1still force either mode with no measurement.Why
The two modes win on different machines, by a lot:
A per-backend default would be a guess. Measuring picks the right one everywhere.
How it measures
bfb7b810e94also drops the per-split staging retire events of graphs that have no copies on the copy queue (decode graphs).Results
Qwen4-35B-A3B UD-Q2_K_XL, llama-server, default mmap load, 8K prompt measured after two other 8K prompts (so the choice is made), 2 interleaved launches per arm, upstream run twice. Prompt tok/s vs upstream:
--cpu-moe--n-cpu-moe 20--cpu-moe--n-cpu-moe 20--n-cpu-moe 20--cpu-moe--n-cpu-moe 20test-prefetch-pickunit test;test-allocpasses.Not covered
--fittogether with--moe-cache-mibunder auto, were not measured.The two new files carry the Unsloth AGPL-3.0 SPDX header. Upstream files keep their license.