Repository navigation
perf(gguf): run llama.cpp on PyTorch's thread count, not its default 4; same probabilities - #20
Merged
Merged
Conversation
Butterski
added a commit
to Butterski/basal
that referenced
this pull request
Oct 19, 2026
Reviewer's GuideGGUF CPU inference now inherits PyTorch’s configured thread count for both regular and batch llama.cpp work instead of using llama.cpp’s default of four threads, improving CPU throughput while regression tests verify bit-identical probabilities across multiple thread counts; release notes document the behavior and benchmark result. Sequence diagram for GGUF CPU inference thread configurationsequenceDiagram
participant GGUFBackend
participant PyTorch
participant LlamaCpp
GGUFBackend->>PyTorch: get_num_threads()
PyTorch-->>GGUFBackend: configured thread count
GGUFBackend->>LlamaCpp: llama_init_from_model(cp)
Note over GGUFBackend,LlamaCpp: cp.n_threads = cp.n_threads_batch = configured count
LlamaCpp-->>GGUFBackend: CPU inference with configured threads
Flow diagram for thread-count-invariant GGUF probabilitiesflowchart LR
Config["PyTorch thread configuration"] --> Context["GGUF context"]
Context --> Threads["CPU inference with n_threads and n_threads_batch"]
Threads --> Logits["Logits and probabilities"]
Logits --> Equal["Bit-identical probabilities across tested counts"]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 2 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="basal/engine.py" line_range="1877" />
<code_context>
cp.swa_full = True # sliding-window layers (Gemma 4) keep every cell: a shared prefix cell stays readable
+ # llama.cpp's own default is 4 threads (GGML_DEFAULT_N_THREADS); PyTorch's count is the physical cores (or
+ # OMP_NUM_THREADS / torch.set_num_threads). Each output element stays one dot product in one thread: same logits.
+ cp.n_threads = cp.n_threads_batch = torch.get_num_threads()
self.ctx = C.llama_init_from_model(self.cmodel, cp)
if not self.ctx:
</code_context>
<issue_to_address>
**GGUF inference can regress**
When pyTorch reports SMT siblings or more CPUs than the process can use, `torch.get_num_threads()` assigns that oversubscribed count to llama.cpp for prompt processing and generation, so GGUF inference can be slower than the physical-core setting measured in the PR and the release note’s physical-core claim is inaccurate.
Choose the physical-core count by default instead of copying `torch.get_num_threads()` unconditionally, while preserving explicit thread-count overrides.
Also at `docs/WHATS_NEW.md:8-10`.
</issue_to_address>
### Comment 2
<location path="tests/test_gguf.py" line_range="137" />
<code_context>
+ try:
+ runs = []
+ for k in (1, 2, 3, 5):
+ C.llama_set_n_threads(backend.ctx, k, k)
+ runs.append(backend.run_shared([g])[0])
+ finally:
</code_context>
<issue_to_address>
**Constructor thread count goes untested**
When the constructor’s thread-count assignment is removed or changed to the wrong value, the test setters overwrite both context thread counts before the output comparison, so the test passes without checking the constructor’s value and users can receive llama.cpp’s default instead of PyTorch’s count.
Assert the context’s thread counts immediately after construction, before changing them for the output comparisons.
</issue_to_address>…4; same probabilities GGUFBackend left n_threads / n_threads_batch at GGML_DEFAULT_N_THREADS (4). Use torch.get_num_threads(): the physical cores by default, and it honours OMP_NUM_THREADS / torch.set_num_threads. ggml computes every output element as one dot product in one thread, so the thread count does not change the sum order. basal-2.0-mini Q8_0, 6-core Zen 2, 16 recorded requests: 28.8 s -> 21.6 s (1.34x), bit-identical on 16/16. Logical cores (12, SMT) were slower than 4 in the research runs, so physical cores is the right count.
…within the CPU affinity
Butterski
force-pushed
the
perf/gguf-physical-threads
branch
from
October 20, 2026 12:11
ac086eb to
2ce06f4
Compare
Owner
|
Thanks, Milosz! Merged for basal 2.3.1. llama.cpp now runs on PyTorch's thread count, and your tests show the same probabilities bit for bit at 1, 2, 3 and 5 threads. You are credited in WHATS_NEW and in the 2.3.1 release notes. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
GGUFBackendnever set the thread counts, so llama.cpp ran on its defaultGGML_DEFAULT_N_THREADS(4) and leftcores idle. Now:
That is the physical cores by default and follows
OMP_NUM_THREADS/torch.set_num_threads. Physical, not logical:12 threads (SMT) on a 6-core CPU was slower than 4. Builds with every layer on Metal or CUDA are not affected.
Same output. ggml computes every output element as one dot product inside one thread; the thread count only changes
which thread computes it, not the order of the sum.
Measured (basal-2.0-mini Q8_0, CPU only, Ryzen 5 PRO 4650GE 6c/12t, 16 recorded requests, variants alternated):
4 threads 28.8 s, 6 threads 21.6 s (1.34x); probabilities equal with
==on 16/16.Tests: new
test_thread_count_does_not_change_probabilities(bit-identical at 1, 2, 3 and 5 threads);tests/test_gguf.py,tests/test_context.py: 12 passed. One line inWHATS_NEW.md2.2.1 (it sits next to #16's;whichever merges second needs the two lines side by side).
Summary by Sourcery
Align GGUF CPU inference with PyTorch's configured thread count to improve performance without changing model outputs.
Enhancements:
Documentation:
Tests: