Skip to content

[w2] llama-bench: --moe-cache-mib / --moe-cache-cpu-share (Qwen4 baseline kit) - #161

Merged
rafasmour merged 1 commit into
masterfrom
opt/w2-qwen4-bench-prep
Dec 4, 2026
Merged

rafasmour merged 1 commit into
masterfrom
opt/w2-qwen4-bench-prep

Conversation

@rafasmour

Copy link
Copy Markdown
Owner

What

llama-bench gets two sweepable options so the MoE expert cache (#154) can be measured per depth (-d) like -ncmoe:

  • -mcm, --moe-cache-mib <n> (comma list/ranges, default 0) -> llama_context_params.moe_cache_size (MiB * 1024 * 1024)
  • -mcs, --moe-cache-cpu-share <f> (comma list, default 0.0, must be 0..1) -> moe_cache_cpu_share

Same plumbing as n_cpu_moe: help text, parser, cmd_params, cmd_params_instance, to_llama_cparams, test fields (jsonl/csv/sql carry moe_cache_mib, moe_cache_cpu_share), README. The markdown table shows the columns (mcm, mcs) only when swept or non-default, so default output is unchanged. They are context parameters, so changing them does not reload the model.

Why

Qwen4-35B-A3B runs 8-11 expert layers on the CPU of a Zen1 host with 15 GB RAM, and the production preset sets no moe-cache-mib. Configuration (n-cpu-moe, cache size, threads) may be the largest unmeasured Qwen4 lever. This PR is only the tool; the baseline/tuning kit (VRAM fit, n-cpu-moe/moe-cache/threads sweeps, traces, MTP, engram page faults, KLD base, byte/launch budget) is local: /home/tom/docker/projects/llama.cpp-RDNA3/build-bins/w2/qwen4-bench-prep/README.md (not committed). Any preset change is the user's decision.

Expected gain (estimate)

No direct speedup. budget.py on the real GGUF header: experts are 416 MiB/layer (13 MiB/layer read per token); the 11 CPU layers read 143 MiB/token, about 6 ms of a modelled ~10 ms token at an assumed 25 GB/s, so each extra GPU-resident layer is about 0.5 ms. Measurements tonight replace the assumptions.

Env switch

None. Defaults 0 = previous behaviour.

Files

tools/llama-bench/llama-bench.cpp, tools/llama-bench/README.md

Risks

  • -mcm > 0 needs a GPU backend (the cache is device-only); CPU-only llama-bench exits with "MoE cache requires a GPU backend" (verified).
  • Kit risks: llama-server runs use kern- containers on port 18080 inside gpublock.sh; 15 GB host RAM vs the 10.7 GiB engram table (scripts never drop caches); f16 at d=122880 may OOM, drop -d then.

Checked (CPU only, no GPU used)

  • COMMON-CPU build of llama-bench + test-llama-archs, no new warnings.
  • llama-bench --help lists both flags; on a tiny qwen4exp GGUF from test-llama-archs -o: -mcm 0,64 -mcs 0,0.5 -p 8 -n 4 -ncmoe 1 -r 1 runs the 0 cases and the sweep columns appear; the -mcm 64 case fails with the CPU-only error above; -mcs 1.5 and -mcm -1 give parse errors; default jsonl just gains the two fields.
  • Kit: bash -n on every script, py_compile on the python helpers, budget.py run on the finished GGUF header (header only).

GPU validation tonight (NOT YET RUN)

Preamble: R=/home/tom/docker/projects/llama.cpp-RDNA3; K=$R/build-bins/kern; TB=$R/build-bins/tools/bench; cd $K; B0=$R/build-bins/w2/base-build/bin; BA=$R/build-bins/w2/all-build/bin; Q4=qwen/qwen-4-35b-a3b/UD-IQ4_XS/Qwen4-35B-A3B-UD-IQ4_XS-00001-of-00002.gguf; Q4A='-ctk q8_0 -ctv q8_0 -ncmoe 11 -b 1024 -ub 512'; Q4F='-ctk f16 -ctv f16 -ncmoe 8 -b 1024 -ub 512'; S=$R/build-bins/w2/qwen4-bench-prep. Run first tonight, on B0 (the -mcm line needs a build with this PR: BA). Long blocks: BLOCK_TIMEOUT=7200.

./gpublock.sh ./lb.sh q4-base $B0 $Q4 $Q4A -p 512 -n 128 -d 0,32768,98304 -r 3
./gpublock.sh ./lb.sh q4f-base $B0 $Q4 $Q4F -p 512 -n 128 -d 0,32768,122880 -r 2
./gpublock.sh ./lb.sh q4-ncmoe $B0 $Q4 -ctk q8_0 -ctv q8_0 -b 1024 -ub 512 -ncmoe 13,12,11,10,9 -p 512 -n 128 -r 2
./gpublock.sh ./lb.sh q4-thr $B0 $Q4 $Q4A -t 4,6,8 -p 0 -n 128 -r 3
./gpublock.sh ./lb.sh q4-mcm $BA $Q4 $Q4A -mcm 0,512,1024,1536 -p 0 -n 128 -d 0,32768 -r 3
BLOCK_TIMEOUT=7200 ./gpublock.sh bash $S/moecache.sh $B0
BLOCK_TIMEOUT=10800 ./gpublock.sh bash $S/fit.sh $B0
./gpublock.sh ./tr.sh q4-tg-d0 $B0 $Q4 32 -- $Q4A -p 0 -n 32 -r 1
./gpublock.sh ./tr.sh q4-tg-d32k $B0 $Q4 32 -- $Q4A -p 0 -n 32 -d 32768 -r 1
./gpublock.sh python3 -I $TB/specbench.py q4-mtp-b0 $B0 --n 256 --modes greedy,preset -- $(python3 -I $TB/preset2args.py qwen-4-35b-a3b-128k-f16-mtp)
./gpublock.sh python3 -I $TB/specbench.py q4-nomtp-b0 $B0 --n 256 --modes greedy,preset -- $(python3 -I $TB/preset2args.py qwen-4-35b-a3b-128k-f16-mtp --nospec)
./gpublock.sh python3 -I $TB/longconc.py q4-conc-b0 $B0 --slots '0;0,1;0,1,2' --ptok 32000 --n 96 -- $(python3 -I $TB/preset2args.py qwen-4-35b-a3b)
./gpublock.sh ./kl.sh q4-b0 $B0 $Q4 base $Q4A -c 16384 --chunks 3
./gpublock.sh env KL_ENV="$OFFE" ./kl.sh q4-ba-off $BA $Q4 test $Q4A -c 16384 --chunks 3   # OFFE as in the wave preamble; only fa-mma-spillfree-d256 may move KLD
./gpublock.sh bash $S/trace.sh $B0     # tg d0/d32k/pp512 traces with -r 2 and tracegaps.py
./gpublock.sh bash $S/engram.sh $B0
./gpublock.sh bash $S/mtp.sh $B0
./gpublock.sh bash $S/kld-base.sh $B0
./gpublock.sh bash $S/lb-sweep.sh $B0 $BA    # all lb.sh lines above in one go
python3 -I $S/budget.py

Rules followed

No GPU was used or locked while preparing this; no production, router, docker compose, llama-cpp-rocm:* image or homelab file was touched; nothing was sent to ggml-org/llama.cpp; the build tree was deleted.

Assisted-by: Claude Sonnet 5.5

Sweepable like -ncmoe (comma lists, default 0 = disabled). They map to
llama_context_params moe_cache_size (MiB) and moe_cache_cpu_share, so the
MoE expert cache (#154) can be measured per depth with -d. Markdown output
shows the columns only when set or swept; jsonl/csv/sql carry the new fields.
The cache needs a GPU backend; a CPU-only llama-bench reports that error.

Assisted-by: Claude Sonnet 5.5
@rafasmour
rafasmour merged commit 1d9d2e4 into master Dec 4, 2026
5 of 14 checks passed
@rafasmour

Copy link
Copy Markdown
Owner Author

Reverted from master on 2026-12-04 (w2 build crashed Qwen4-single at >20k ctx; no measured gain). Commit preserved on branch archive/w2-25f0def; will be re-landed individually after GPU validation.

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