Skip to content

llama : measure which host-expert copy mode is faster and keep it (stacked on #268) - #479

Open
danielhanchen wants to merge 10 commits into
moe-offload-prefetch-cachefrom
moe-offload-prefetch-auto
Open

danielhanchen wants to merge 10 commits into
moe-offload-prefetch-cachefrom
moe-offload-prefetch-auto

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Stacked on #268 (base is its branch). With GGML_SCHED_PREFETCH_EXPERTS unset, 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 / =1 still 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

  • The first large ubatch with host experts is not timed (it pages the mmapped experts in).
  • Then one routed ubatch, two consecutive whole ubatches timed as one span, and one more routed ubatch. The whole pair is one span because a sync between them hides the overlap with the ubatch before, which is where whole wins: a per-ubatch sync measured G4 ub 2048 whole as 3.4% faster when it is 10% faster end to end.
  • Whole is kept only if it is at least 3% faster per ubatch, so any remaining bias favours upstream's routed path.
  • The ubatch that seeds the MoE cache is never timed. This idea comes from the timed default @LeoBorcherding wrote for AMD, credited with Co-authored-by.
  • Graphs with no host experts skip the measurement. The mode is part of the graph reuse key. One log line reports both timings and the choice.
  • bfb7b810e94 also 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:

GPU, placement ub 512: auto ub 2048: auto ub 2048: forced whole
RTX PRO 6000, --cpu-moe -0.4% (routed) +11.0% (whole) +10.5%
RTX PRO 6000, --n-cpu-moe 20 -0.3% (routed) +9.5% (whole) +9.7%
A100 40 GB, --cpu-moe -0.2% (routed) +0.1% (routed) -6.2%
A100 40 GB, --n-cpu-moe 20 0.0% (routed) +0.4% (routed) -3.2%
L4, --n-cpu-moe 20 +0.2% (routed) +2.3% (one launch each mode) +4.9%
T4, --cpu-moe +1.4% (routed) +17.7% (whole) +17.4%
T4, --n-cpu-moe 20 +0.4% (routed) +12.4% (whole) +11.5%
  • Forced whole at ub 512 is 12 to 26% slower than upstream on every card. Auto stays at upstream there.
  • L4 at ub 2048: whole wins by about the margin, so auto went either way; both are at or above upstream.
  • Greedy text (prompt and next turn) is identical across all arms in every cell. Logits and text gate (A100, Qwen3.6, 12 configs, unset / 0 / 1 next to upstream): 72 of 72 byte-identical. Next-turn decode and peak RSS are equal within noise.
  • Cost: only before the choice is made (the first 8K prompt at ub 512, the first two at ub 2048), at most one routed-vs-whole ubatch pair in the slower mode, once per process.
  • New test-prefetch-pick unit test; test-alloc passes.

Not covered

  • HIP: not run here. It needs a rerun of the R9700 table on this branch.
  • Multi-slot servers, and --fit together with --moe-cache-mib under auto, were not measured.

The two new files carry the Unsloth AGPL-3.0 SPDX header. Upstream files keep their license.

danielhanchen and others added 4 commits December 3, 2026 16:50
…ole ahead or only the routed experts is faster, and keep the faster one (GGML_SCHED_PREFETCH_EXPERTS=0/1 forces either)
…span, a sync between them hides the overlap with the ubatch before
…ce of the expert copies

Co-authored-by: LeoBorcherding <borchborchmail@gmail.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-05T13:55:18.995087Z 135c048 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment thread src/llama-context.cpp
Comment on lines +2488 to +2492
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)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Comment thread src/llama-prefetch-pick.h Outdated
Comment on lines +9 to +11
// 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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Comment thread src/llama-context.cpp
Comment on lines +410 to +411
if (GGML_SCHED_PREFETCH_EXPERTS != nullptr && *GGML_SCHED_PREFETCH_EXPERTS != '\0') {
prefetch_pick.forced = atoi(GGML_SCHED_PREFETCH_EXPERTS) != 0 ? 1 : 0;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.
@danielhanchen danielhanchen self-assigned this Dec 5, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment thread src/llama-context.cpp
Comment on lines +2418 to +2420
if (pf_span.open) {
prefetch_pick.abort(cparams.n_ubatch);
pf_span.open = false;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 135c048f1f

ℹ️ 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".

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant