Skip to content

common: support CPU parameters across tools - #27026

Merged
ngxson merged 1 commit into
ggml-org:masterfrom
nikwen:cpu-params
Aug 13, 2026
Merged

ngxson merged 1 commit into
ggml-org:masterfrom
nikwen:cpu-params

Conversation

@nikwen

@nikwen nikwen commented Aug 13, 2026

Copy link
Copy Markdown
Member

Overview

Fixes #26997

Adds support for CPU parameters such as --cpu-mask, --cpu-range, and --prio to llama-cli, llama-server, and other tools.

I mostly moved code from tools/completion/completion.cpp into common/common.cpp.

Additional information

If applying CPU parameters fails, it will log a warning, but it won't crash the app. In my opinion, it's better to continue with the wrong parameters than to exit.

There's a follow-up to use thread pools properly with speculative decoding, which probably means sharing thread pools between the draft model and the target model. I'll work on that as a follow-up PR after this is merged.

Requirements

  • I have read and agree with the contributing guidelines
  • AI usage disclosure: YES, used Grok 4.6 to help me, but I manually modified the code it produced

@ngxson

ngxson commented Aug 13, 2026

Copy link
Copy Markdown
Collaborator

/bot review

@ggml-gh-bot

ggml-gh-bot Bot commented Aug 13, 2026 •

Copy link
Copy Markdown
Automated code review

I reviewed the PR diff and explored the surrounding code (common.cpp init path, common.h, completion.cpp before/after, the server and parallel tools, ggml threadpool lifecycle, and llama_context attach/detach). The change moves the threadpool/priority setup that previously lived in tools/completion/completion.cpp into common_init_result so that every tool using common_init_from_params (cli, server, perplexity, parallel, etc.) gets CPU-mask/priority/threadpool support. The RAII placement and destruction order are correct, and the move itself is sound. A few findings below.

Will slow the review

(point 1) No null check on the proc-address function pointers before use (common/common.cpp, common_threadpools::init, ~lines 1772-1774). ggml_threadpool_new_fn and free_fn are obtained via ggml_backend_reg_get_proc_address and then immediately dereferenced (threadpool_batch = ggml_threadpool_new_fn(&tpp_batch)). If the CPU backend ever fails to export one of these symbols, this is a null-pointer dereference crash. This was latent in the old completion.cpp too, but this PR widens it to every tool. A one-line guard (if (!ggml_threadpool_new_fn || !free_fn) { COM_WRN(...); return; }) before use would make the "warn and continue" degradation actually reachable. Right now the early-return paths only cover cpu_dev == NULL and pool-creation failure.

(point 2) Behavior change for set_process_priority is intentional but worth confirming per tool (common/common.cpp, line 1382). The old completion.cpp did if (!set_process_priority(...)) { return 1; }; now the return value is discarded and only a warning is logged. The PR description explicitly endorses this ("better to continue with the wrong parameters than to exit"). That is reasonable for interactive/serving tools, but note it now applies to all tools via common_init_from_params. For benchmark-style consumers that is fine here because llama-bench does not go through common_init_from_params (it uses llama_init_from_model directly), so this is just a heads-up, not a request to change it.

(point 3) Threadpool lifetime vs. llama_backend_free() ordering. common_threadpools is destroyed when the common_init_result goes out of scope, which in several tools (completion, parallel, server) happens after their explicit llama_backend_free() call (e.g. examples/parallel/parallel.cpp:515 then return 0;). This is currently safe: the CPU backend is statically linked so the free_fn pointer stays valid, and ggml_threadpool_free only joins its own worker threads and calls ggml_aligned_free on memory it allocated itself (it does not touch the backend). But it is a fragile ordering that this PR newly introduces across many tools. If you want it robust against dynamically-loaded backends, the clean fix is for tools to llama_init.reset() before llama_backend_free(); that is a follow-up concern, not a blocker for this PR.

Nits

(point 4) ggml_threadpool_params_from_cpu_params is now only called from within common/common.cpp (the completion.cpp caller was removed). It is still declared in the public header. If there are no external consumers, consider making it internal (anonymous namespace) to avoid growing the public common surface; if you intend to keep it public for SDK users, that is fine too - just confirm intent.

(point 5) The header re-alignment of common_model_params_to_llama / common_context_params_to_llama (common.h, ~line 932) is purely cosmetic and inflates the diff a little. Not a problem, just noting it is unrelated to the threadpool move.

What looks right

  • The common_threadpools member is placed at the top of common_init_result::impl, so (per the file's own "destructors run bottom-to-top" comment) it is destroyed after context and model. That is the correct order: the context is gone before its attached threadpool is freed, so there is no use-after-free on teardown.
  • ggml_threadpool_free(NULL) is a no-op, and the destructor guards on !free_fn, so all the partial-init / skipped-init paths (model-only, cpu_dev == NULL, pool creation failure) are safe.
  • Graceful degradation is good: when init() bails without attaching, the context's threadpool stays nullptr and ggml falls back to a disposable pool per compute, so the app keeps running without the custom mask/priority.
  • set_process_priority is called before threadpools.init, matching the old ordering so worker threads inherit the process priority. It is a no-op when priority is GGML_SCHED_PRIO_NORMAL.
  • ASCII-only, concise comments, snake_case, COM_INF/COM_WRN logging consistent with the rest of common.cpp. The speculative-decoding draft/target threadpool-sharing limitation is acknowledged in the PR description as a follow-up, and this PR does not regress that case (neither pool was shared before either).

This review was generated automatically by pi coding agent using zai-org/GLM-5.2. It may contain mistakes. Maintainers make the final call.

@nikwen

nikwen commented Aug 13, 2026

Copy link
Copy Markdown
Member Author

Regarding the review:

  1. This was already in the code I moved. I can add a quick check if people think it makes sense.
  2. Intentional.
  3. That's how the code previously did it in completion.cpp, too.
  4. I have no strong feeling about this. Please let me know if you think we should remove it from the header.
  5. Yes, cosmetic to bring it under the "Threadpool utils" comment.

@ngxson

ngxson commented Aug 13, 2026

Copy link
Copy Markdown
Collaborator

There is one concern that I'm thinking about though (not sure if it's mentioned in the bot review)

An example can create multiple common_init_result instances, and now each will carry its own threadpool. I'm not sure if by design, all llama_context should share one single threadpool, or each should have its own.

This case is valid on server: target and draft models have different contexts, managed by different instances of common_init_result

@nikwen

nikwen commented Aug 13, 2026

Copy link
Copy Markdown
Member Author

@ngxson Yes, 100%. I was planning to address this in a follow-up PR by making both contexts share the thread pool.

I saw this happening in examples/speculative/speculative.cpp and common/speculative.cpp.

The bot was reasoning about it but it didn't bring it up because of this note from the PR description:

There's a follow-up to use thread pools properly with speculative decoding, which probably means sharing thread pools between the draft model and the target model. I'll work on that as a follow-up PR after this is merged.

@ngxson

ngxson commented Aug 13, 2026

Copy link
Copy Markdown
Collaborator

hmm unfortunately I'm not experience enough in this part to tell if we gonna run into any correctness issues if we don't implement the threadpool reuse right away (in other words, will be version between when this is merged and the follow-up fix will be broken somehow?)

otherwise, I think it might be better to ship everything in this PR?

CC @ggerganov if you have more info about this

@ngxson

ngxson commented Aug 13, 2026

Copy link
Copy Markdown
Collaborator

ok so a bit more deep dive, seems like before this PR, ggml's cplan creates a disposable threadpool for each decode pass

so this PR only make all decode calls of the same context reuse the same threadpool, should be a harmless change

so it's ok to merge as-is, thanks for the refactoring

@ngxson
ngxson merged commit a94d563 into ggml-org:master Aug 13, 2026
26 of 27 checks passed
@nikwen

nikwen commented Aug 13, 2026

Copy link
Copy Markdown
Member Author

Sorry for the delayed response!

seems like before this PR, ggml's cplan creates a disposable threadpool for each decode pass

so this PR only make all decode calls of the same context reuse the same threadpool, should be a harmless change

That was also my conclusion. I wouldn’t have opened the PR as-is if that wasn’t the case. :)

In the next few days, I’ll follow up with a PR to re-use the thread pool across drafter and main model contexts. Tracking issue: #27039

Thanks for reviewing and merging! Appreciate your help! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Misc. bug: --cpu-mask --cpu-range --cpu-strict options ignored

2 participants