Repository navigation
context : prevent excessive CPU polling without OpenMP - #32378
Conversation
|
@nikwen if I can trouble you reviewing/testing this short PR, much appreciated 🙇 |
|
Thanks! I'll take a look soon. |
nikwen
left a comment
There was a problem hiding this comment.
I ran the PR on my M4 Pro with -DGGML_OPENMP=OFF. llama-bench CPU usage dropped from 250% on master to 8% with the patch.
Thanks for working on it! For some reason, I got no notification about the mention on the bug report.
One small note: the commit prefix should be llama or context. It doesn't touch files from common.
Looking at the code, I see no obvious correctness bugs, but I'm also not an expert in this area and have gotten things wrong before. @max-krasnyansky is our thread pool expert as far as I know.
Max, would you be able to take a look at the PR, too? If you think it's good, I'll happily give it the second approval.
|
Looks good to me. I like the idea in general. In theory, with the default settings the CPU worker threads should quickly backoff (ie they poll for N rounds and then go to sleep if no new work shows up). That maybe the only thing to double check, whether the default is too aggressive. |
Planning to look into that. |
|
Thanks for taking a look, Max! I've updated the PR title. Metal with OpenMP enabled (the default when I build locally) works fine. It only does these shenanigans without OpenMP. |
|
Folks, I appreciate the reviews. Apologies for undrafting this too early. I've run some benchmarks on MacBook M1 that I loaned, and it does not look right. I'm going to draft this for now, and post more details later. |
|
So this PR works, but rather too well: macOS moves the sleeping decode thread to an E-core, and, well, it runs rather slow. I noticed this on my fairly outdated loaner M1 with a small model (Qwen3-0.6B), but the same could happen on better hardware with even larger models. That's 15-30% slower generation, with the CPU pulling only 0.75 W against 11 W on master. One workaround is to follow in the footsteps of CUDA: llama.cpp/ggml/src/ggml-cuda/ggml-cuda.cu Lines 463 to 470 in 9d6f04f Not feeling great about this approach. I also tried Looking into what else is there. |
|
Workgroup here is a hack from a7270d2
With Gemma-4 E4B the workgroup adds nothing but costs some extra CPU usage.
I don't think it is worth it: adds complexity and hurts CPU power where the original approach has no issues. |
|
So I can only see any negative effects to decode on a very small model and no issues on more practical models. Therefore I'd say this change is net positive and I'll get it back into the review. |
e81e3ce to
7e9e14c
Compare
|
Ran a few tests and look good (
I wonder if this will have a positive impact on RTX Sparks too. At some point, we added OpenMP to the Windows Arm build because it improved the performance compared to ggml threadpools - maybe this is was the culprit. |
…owner 2026-11-25) Base b12723 (merge a940ab9), pin ggml-org#401 removed (80211b4), RoPE multi-section fusion from upstream ggml-org#32500 (owner's decision), MIX.md (3563400). Build gate: test-backend-ops ROCm 21878/21878, bench r17 (llama-mix-notes, mesures/2026.md): Inkling tg +4.5 % [bon], Qwen3.6 and Gemma 4 without regression. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01QBjnvFCJ83vkmygsLNv5cu * mix/wip-b12723: (55 commits) mix: MIX.md — base b12723 (branche wip), retrait du pin ggml-org#401, conflit RoPE tranché (amont ggml-org#32500) revert: pin: CUDA: fuse the row L2 clip chain (unslothai#401, commit 6c0e84d) model : inkling2 graph with fewer ops in mix() and control() (ggml-org#32948) opencl: add clc coopmat moe kernel for q4_0/q4_1/q4_k/q5_0/q5_1/q5_k/q6_k/mxfp4 (ggml-org#32971) tests : fix MSVC static build of test-batch-alloc and test-server-component (ggml-org#32870) feat: Inkling 2 DSpark support (ggml-org#32218) cuda : fix int overflow of the MMVF matrix offsets (ggml-org#32965) docs : describe SpacemiT runtime environment variables (ggml-org#32452) ggml : fix SpacemiT flash attention null mask, ALiBi and softcap (ggml-org#32182) cmake : include the llguidance header from the source tree (ggml-org#32928) ci : move some jobs to gb10 runners (ggml-org#32968) ui: run Llama Chat apart from llama-server, named in config.json or npx --server (ggml-org#32945) ui: fix prebuilt UI download in shallow clones (ggml-org#32952) convert : fix qwen3-omni conversion (ggml-org#32941) ggml-cpu: repack kernels for targets without dotprod (ARMv8.0 64-bit) for Q4_0/Q8_0 (ggml-org#32142) ui: expect the Llama Chat name in the PWA manifest e2e test (ggml-org#32959) ggml-cpu: add SVE-optimized q5_0 and q5_1 dot-product kernels (ggml-org#30867) server : include MCP image and audio data in tool results (ggml-org#32064) context : prevent excessive CPU polling without OpenMP (ggml-org#32378) metal : reserve the top-k scratch only for short rows (ggml-org#32942) ...
* common : prevent polling while GPU computes (on Metal or without OpenMP) * use AudioWorkIntervalCreate to keep the thread on P-cores * Revert "use AudioWorkIntervalCreate to keep the thread on P-cores" This reverts commit a7270d2.
Overview
This is a possible fix for #29858; it reduces the CPU time by 50% on my Linux machine on builds without OpenMP.
Although I haven't tested it on the actual hardware from the bug report, I tested it on a MacBook Pro M1, and the results were inspiring: the CPU power went down fivefold (from watts to under a watt), with no negative changes to decode on models larger than 1B.
The only drawback is the performance on a small model (Qwen3-0.6B): it suffered because macOS moved the almost idle main thread to E-core as per
powermetrics. The question is if this extrapolates to larger models on better hardware: more benchmarks are necessary on more current hardware.Additional information
This is a follow-up to #27026 that moved threadpool creation so that every tool now has a persistent CPU when before only
llama-completionhad one. The fix pauses the pool when the graph has more than one split, sleeping while other backends could wrap up the work, letting the main thread wake the pool up for the next graph.This should be safe because the CPU backend is synchronous and at this point we are done with our part of the work.
Regular Linux builds have OpenMP by default, and it ignores
poll, andggml_threadpool_pauseis a no-op there. Possibly the reason it wasn't apparent for anyone else on Linux.Benchmarks on macOS
Numbers are from the run with the least outliers.
Benchmarks on Linux without OpenMP
Benchmarks with
GGML_OPENMP=OFFon UHD 770 with Vulkan.Times are for full llama-bench runs with
-p 0 -n 128 -r 3;-n 64Requirements