Skip to content

context : prevent excessive CPU polling without OpenMP - #32378

Merged
ggerganov merged 3 commits into
ggml-org:masterfrom
sanmai:issue-29858/poll-pause
Nov 24, 2026
Merged

ggerganov merged 3 commits into
ggml-org:masterfrom
sanmai:issue-29858/poll-pause

Conversation

@sanmai

@sanmai sanmai commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

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-completion had 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, and ggml_threadpool_pause is a no-op there. Possibly the reason it wasn't apparent for anyone else on Linux.

Benchmarks on macOS

case build t/s CPU time wall CPU power
Qwen3-0.6B UD-Q4_K_XL master 96.4 16.4 s 4.42 s 7.7 W
Qwen3-0.6B UD-Q4_K_XL patch 82.8 1.36 s 4.90 s 1.2 W
LFM3-1.7B Q8_0 master 27.0 32.8 s 15.5 s 6.1 W
LFM3-1.7B Q8_0 patch 30.0 1.48 s 13.3 s 0.7 W
Qwen3-4B-2507 UD-Q4_K_XL master 21.0 44.6 s 20.0 s 3.3 W
Qwen3-4B-2507 UD-Q4_K_XL patch 21.0 1.87 s 18.8 s 1.4 W
Gemma-4-E4B-it QAT Q4_0 master 18.8 50.0 s 24.0 s 2.3 W
Gemma-4-E4B-it QAT Q4_0 patch 18.4 3.99 s 21.8 s 0.5 W

Numbers are from the run with the least outliers.

Benchmarks on Linux without OpenMP

Benchmarks with GGML_OPENMP=OFF on UHD 770 with Vulkan.

case build t/s CPU time wall
stories15M -ngl 99 master 633 2.65 s 0.78 s
stories15M -ngl 99 patch 663 0.61 s 0.73 s
LFM3-1.7B Q8_0 -ngl 99 master 10.05 154.0 s 41.7 s
LFM3-1.7B Q8_0 -ngl 99 patch 10.07 13.8 s 39.2 s
LFM3-1.7B Q8_0 -ngl 8 master 12.43, 12.97 121, 116 s 16.5, 15.8 s
LFM3-1.7B Q8_0 -ngl 8 patch 12.91, 12.92 62, 62 s 15.9, 15.9 s

Times are for full llama-bench runs with -p 0 -n 128 -r 3; -n 64

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES, let Claude hammer the MacBook loaner

@sanmai
sanmai requested a review from nikwen November 16, 2026 06:01
@sanmai

sanmai commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

@nikwen if I can trouble you reviewing/testing this short PR, much appreciated 🙇

@github-actions github-actions Bot added the ggml changes relating to the ggml tensor library for machine learning label Nov 16, 2026
@sanmai sanmai changed the title common : prevent excessive polling while GPU computes (w/Metal or without OpenMP) common : prevent excessive CPU polling on Metal (or without OpenMP) Nov 16, 2026
@nikwen

nikwen commented Nov 16, 2026

Copy link
Copy Markdown
Member

Thanks! I'll take a look soon.

@nikwen nikwen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

@max-krasnyansky

Copy link
Copy Markdown
Member

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.
We tuned it carefully back when threadpool was first introduced to make sure it doesn't interfere with other backends, yet keep the latency low between ubatches/etc, but the workloads have changed quite a bit (if I recall correctly, we didn't even have proper backends back then).

@sanmai sanmai changed the title common : prevent excessive CPU polling on Metal (or without OpenMP) context : prevent excessive CPU polling on Metal (or without OpenMP) Nov 19, 2026
@sanmai
sanmai marked this pull request as ready for review November 19, 2026 09:35
@sanmai
sanmai requested review from a team and ggerganov as code owners November 19, 2026 09:35
@sanmai sanmai changed the title context : prevent excessive CPU polling on Metal (or without OpenMP) context : prevent excessive CPU polling on Metal Nov 19, 2026
@sanmai

sanmai commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

whether the default is too aggressive

Planning to look into that.

@nikwen nikwen changed the title context : prevent excessive CPU polling on Metal context : prevent excessive CPU polling without OpenMP Nov 19, 2026
@nikwen

nikwen commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

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.

@sanmai

sanmai commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

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.

@sanmai
sanmai marked this pull request as draft November 20, 2026 02:14
@sanmai

sanmai commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

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:

// Temporary performance fix:
// Setting device scheduling strategy for iGPUs with cc121 to "spinning" to avoid delays in cuda synchronize calls.
// TODO: Check for future drivers the default scheduling strategy and
// remove this call again when cudaDeviceScheduleSpin is default.
if (prop.major == 12 && prop.minor == 1) {
CUDA_CHECK(cudaSetDevice(physical_id));
CUDA_CHECK(cudaSetDeviceFlags(cudaDeviceScheduleSpin));
}

Not feeling great about this approach. I also tried thread_policy_set with THREAD_TIME_CONSTRAINT_POLICY and that did not work.

Looking into what else is there.

@github-actions github-actions Bot added the Apple Metal https://en.wikipedia.org/wiki/Metal_(API) label Nov 20, 2026
@sanmai

sanmai commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Workgroup here is a hack from a7270d2

model setup t/s CPU mW
Qwen3-0.6B patch, workgroup off 77-81 315-405
Qwen3-0.6B patch, workgroup on 93-96 420-1900
Qwen3-0.6B master poll=50 104-105 7800-9800
LFM3-1.7B patch, workgroup off 31 70-250
LFM3-1.7B patch, workgroup on 31-32 205-425
LFM3-1.7B master poll=50 32 5200-5600

With Gemma-4 E4B the workgroup adds nothing but costs some extra CPU usage.

variant t/s P busy CPU mW GPU mW
master poll=50 19-19.2 146-149 2030-2360 7160-7340
patch 18-19.0 3-5 620-970 7200-7350
patch + workgroup 18-19.1 12-29 730-1400 7120-7280

I don't think it is worth it: adds complexity and hurts CPU power where the original approach has no issues.

@sanmai

sanmai commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

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.

@sanmai
sanmai marked this pull request as ready for review November 20, 2026 12:43
@sanmai sanmai changed the title context : prevent excessive CPU polling without OpenMP context : prevent excessive CPU polling without OpenMP (50% reduction) Nov 21, 2026
@sanmai sanmai changed the title context : prevent excessive CPU polling without OpenMP (50% reduction) context : prevent excessive CPU polling without OpenMP Nov 21, 2026
@sanmai
sanmai force-pushed the issue-29858/poll-pause branch from e81e3ce to 7e9e14c Compare November 21, 2026 01:44
@ggerganov ggerganov self-assigned this Nov 24, 2026
@ggerganov

ggerganov commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Ran a few tests and look good (Qwen3.5-0.8B Q8_0):

Machine pp512 (base → PR) tg128 (base → PR) CPU user time
M5 Ultra 21921 → 21889 (−0.2 %) 326.1 → 347.2 (+6.5 %) 7.0 s → 0.44 s (−93.8 %)
M2 Ultra 11054 → 11103 (+0.4 %) 229.9 → 260.1 (+13.1 %) 13.5 s → 0.62 s (−95.4 %)

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.

@ggerganov
ggerganov merged commit d3c257a into ggml-org:master Nov 24, 2026
32 of 41 checks passed
@sanmai
sanmai deleted the issue-29858/poll-pause branch November 25, 2026 08:13
abeloin added a commit to abeloin/llama.cpp-mix that referenced this pull request Nov 25, 2026
…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)
  ...
Mr-J-369 pushed a commit to Mr-J-369/llama.cpp-Android that referenced this pull request Nov 26, 2026
* 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Apple Metal https://en.wikipedia.org/wiki/Metal_(API) ggml changes relating to the ggml tensor library for machine learning

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants