[PR #178] [CLOSED] Calibration: measure more CPU-lane threads than the engine's own (STRATA_GLM_CPU_LANE_MAX) #375

Closed
opened 2026-10-29 21:24:49 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/mw00/project-maya/pull/178
Author: @Jw88888
Created: 10/14/2026
Status: ❌ Closed

Base: main ← Head: calibrate-more-threads


📝 Commits (2)

  • 8feffa4 Calibration: more CPU-lane threads than the engine's own are measured too - the engine runs with STRATA_GLM_CPU_LANE_MAX (a pool of the PC's CPUs, the engine's own count active, the rest asleep), so a request's cpu_threads can try up to the CPUs less two; upward only while each count beats the best so far. A KVM guest's 16 vCPUs have no SMT siblings and the engine takes two thirds of them: the calibration kept 10 before and keeps 14 now (Maya-L, one RTX 3090 Ti, --bench decode 14.9 -> 17.6 tok/s, #177)
  • 556ed79 Calibration's higher thread counts: only up to the pool the engine reports (INFO cpu_threads_max) - a pool a split of 3+ GPUs shares, or DFlash2's two lanes, does not take STRATA_GLM_CPU_LANE_MAX, and a request above it ran the engine's own count again under the higher count's name; the bigger pool only on one GPU without SMT siblings. Workers left out by set_active wait on a condition of their own, outside sleeping_ - every batch's notify_all woke them to sleep again, which made the lower counts measure slower (32 vCPUs, 10 of a pool of 32 active: 19.4 -> 7.1 us a synthetic batch; a pool of its own count unchanged, 7.4 / 7.2). DFlash2's two-lane line names the active count (#178's review)

📊 Changes

9 files changed (+198 additions, -31 deletions)

View changed files

📝 docs/SETTINGS.md (+4 -1)
📝 include/strata/core/glm_model.hpp (+3 -1)
📝 maya.py (+5 -5)
📝 serve/server.py (+1 -1)
📝 src/core/glm_fast_path.cu (+18 -3)
📝 src/core/glm_fast_state.hpp (+27 -9)
📝 src/program/generate.cpp (+5 -3)
📝 tools/calibrate_glm.py (+58 -3)
📝 tools/test_calibrate_glm.py (+77 -5)

📄 Description

Follow-up to #177. ./maya.sh --calibrate only measures fewer CPU-lane threads than the engine's own count: it lowers the active count of the pool the engine started with. So it cannot find a count above the engine's own. On a KVM guest with 16 vCPUs (no SMT siblings visible) the engine takes two thirds of them (10), and 14 decodes about 18% faster. The calibration kept 10. With this change it measures counts above too, and keeps 14.

Two commits on v1.1.11: 1a1eb52 (the change) and 7847aba (the review's fixes: the reported pool size, the sleeping workers, one GPU without SMT, the DFlash2 log line).

The change

Engine: STRATA_GLM_CPU_LANE_MAX=N (unset by default, so nothing changes without it).

  • When N is more than the lane's count, the CPU-lane pool holds N threads with the lane's own count active. A request's cpu_threads can then go up to N without a restart.
  • A request that names no cpu_threads returns to the lane's own count (FastState::cpu_threads0), not to the whole pool, so the calibration's "engine's own" baseline stays the engine's own.
  • It applies only to a pool of its own. A pool a split of 3+ GPUs shares does not take it, and DFlash2's two-lane rebuild clears cpu_threads0.
  • The INFO line reports the pool's size as cpu_threads_max (Glm5Model::cpu_lane_threads_max()), so a caller knows the most threads a request gets.

Engine: Workers threads above active() wait on a condition variable of their own (idle_cv_) that only set_active wakes (under mu_, so no wake-up is missed). They are not counted in sleeping_. Before, every batch's notify_all woke them to sleep again, and their presence made run() notify at all. The fewer threads active, the more of them there were, which slowed the lower counts the calibration measures. This also removes that cost from the existing downward sweep.

tools/calibrate_glm.py:

  • The calibration's engine runs with STRATA_GLM_CPU_LANE_MAX set to the CPUs the process may use (sched_getaffinity, at most 64). This applies only on one GPU and where no CPU's thread_siblings_list names a sibling (lane_max_env, smt_visible). With SMT the engine's own count is already a thread per physical core, and a split has a pool per GPU or a shared one.
  • After the existing downward sweep, it measures counts above the engine's own: the CPUs less two, and halfway there (more_thread_candidates). It goes up to the pool the engine reports (cpu_threads_max) and never above it. Without that key (an older engine) there is no upward sweep.
  • Going upward stops at the first count that does not beat the best so far. The winner goes through the same interleaved confirmation and MIN_GAIN as before.

Tests: seven new cases in tools/test_calibrate_glm.py. The fake engine now has a pool that caps cpu_threads as set_active does, and records the count that really ran.

  • Higher counts are measured and kept.
  • The upward sweep stops once slower.
  • No upward sweep without pool room.
  • A pool that ignores MAX is never asked for more. This is the review's case (22 shared threads, MAX 64, one 1% high measurement and a winning 0.10 share); it fails on 1a1eb52 alone.
  • An engine without cpu_threads_max gets no upward sweep.
  • lane_max_env and smt_visible on a fake sysfs.
  • more_thread_candidates.

Docs:

  • A STRATA_GLM_CPU_LANE_MAX row in the tuning table (docs/SETTINGS.md, where the README's table moved).
  • The tuning's description in docs/SETTINGS.md and maya.py says "fewer and more" threads.
  • The server's strata_tune comment.
  • DFlash2's two-lane log line prints the active count.

The engine's own choice of thread count is not changed.

The machine

  • RTX 3090 Ti 24 GB, driver 615.71.09, CUDA 13.4, PCIe gen 3 x16 as reported.
  • AMD EPYC 7302P as a KVM guest, 1 NUMA node, no SMT siblings visible: 16 vCPUs for the first measurements, 32 vCPUs for the later ones (the host gave the guest 16 more; on the 7302P these are the same 16 cores' second threads).
  • 240 GB RAM. Debian 12, kernel 6.1.
  • Maya-L, context 32768, --prefill auto, --kv-streaming on, the shipped expert profile, no vision. Every off-card expert fits in the RAM tier (0 disk reads a token).

Before and after

16 vCPUs (first commit, on v1.1.7)

tools/calibrate_glm.py on the installer's config (no "env"), main's tool and engine, then the first commit's:

main first commit
engine at start 10 threads, 0.303 ms an expert on the CPU 10 threads (pool of 16), 0.311 ms
thread sweep (tok/s) 10: 15.1, 8: 13.0, 7: 11.3, 5: 8.6 10: 14.6, 8: 12.8, 7: 11.0, 5: 8.4, 12: 16.2, 14: 18.6
confirmation own/10: 15.0 15.0 14.5 vs 0.10/10: 15.7 15.2 15.1 own/10: 14.9 14.2 15.7 vs 0.10/14: 17.7 17.8 17.0
kept nothing (0.10 within MIN_GAIN) PCIE_SHARE=0.10, CPU_LANE=14

The first commit's lower counts here still carried the sleeping-worker cost the review found (10 threads: 14.6 against main's 15.1). 7847aba removes it (below). These 16-vCPU runs were not repeated after the guest moved to 32 vCPUs.

./maya.sh --bench: the installer's config against the same config with only STRATA_GLM_CPU_LANE=14, four runs alternating, a fresh engine each:

stock, run 1 14 threads, run 1 stock, run 2 14 threads, run 2
decode, CSV function 14.3 16.7 14.3 17.1
decode, photosynthesis 15.9 18.6 16.0 18.8
decode, bouncing ball 14.5 17.2 14.6 17.3
decode, mean 14.9 17.5 15.0 17.7 (+18%)
prefill 2048 265 291 264 291 (+10%)
prefill 8192 743 789 744 789 (+6%)

32 vCPUs (both commits, on v1.1.11)

tools/calibrate_glm.py, same stock config, main's tool and engine, then this branch's:

main this branch
engine at start 21 threads (pool 21), 0.275 ms an expert 21 threads (pool 32), 0.275 ms
thread sweep (tok/s) 21: 15.9, 16: 17.4, 14: 16.7, 10: 13.8, 5: 8.4 21: 16.0, 16: 17.5, 14: 17.3, 10: 14.6, 5: 8.9, 26: 18.6, 30: 17.9
confirmation own/21: 16.6 15.9 16.2 vs 0.10/16: 16.9 16.2 17.8 own/21: 16.0 16.3 15.0 vs 0.10/26: 17.0 18.5 17.8
kept PCIE_SHARE=0.10, CPU_LANE=16 PCIE_SHARE=0.10, CPU_LANE=26
time 670 s 712 s
  • Baseline: the engine's own count measures the same with 11 sleeping workers as main's does with none (16.0 against 15.9).
  • Lower counts: these measure 3-6% higher than on main, whose downward sweep has the sleeping-worker cost.
  • The upward sweep's value: with 32 vCPUs, main's downward sweep finds a better count itself (16). The upward sweep matters where the engine's own count sits below the best, as with 16 vCPUs.
  • A separate harness on this box, a fresh engine start per count (six chat prompts, ~2,100 tokens), had 14 / 21 / 24 / 27 / 30 threads at 19.5 / 17.8 / 18.7 / 19.6 / 19.8 tok/s. So 14 to 30 land within a few percent of each other here, and the default 21 is the slow one.

Workers alone

Workers copied verbatim from main's header and this branch's into one program. The job is synthetic (about 4,000 multiply-adds), spin_us 20000 as the engine uses, best of three alternating runs, 32 vCPUs:

pool, active main this branch
32, 10 19.4 µs a batch 7.1 µs
32, 14 18.6 7.8
32, 21 19.8 8.4
16, 10 12.8 6.8
10, 10 (no extra workers) 7.4 7.2
21, 21 (no extra workers) 8.6 8.3

Checks

On the same Linux box, with the repository's CI commands, on this branch over v1.1.11:

  • tools/check_hip_compat.py: every CUDA name has a mapping (no new CUDA calls).
  • python -m unittest discover -s serve -p "test_*.py" -t .: 189 tests OK (3 skipped).
  • python -m unittest discover -s tools -p "test_*.py": 272 tests OK (27 skipped), against 265 on main.

The engine was built for sm_86 only.

Output

Different thread counts can change which expert runs on the CPU or the GPU, so the last bits of an answer can change, as CONTRIBUTING says. With STRATA_GLM_CPU_LANE_MAX unset, the lane's pool and its counts are as before. The Workers change only affects threads above active(), which a pool has only with MAX set or while the calibration's downward sweep runs.

Not measured

  • Other NVIDIA targets, Windows, AMD.
  • A split of 2+ GPUs, where the upward sweep is now off.
  • A PC with SMT siblings, where it is off too.
  • A hybrid CPU with disk reads without SMT (the case beside the two-thirds rule). There the upward sweep would try the CPUs less two and halfway there, and stop at the first that is not faster.

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/mw00/project-maya/pull/178 **Author:** [@Jw88888](https://github.com/Jw88888) **Created:** 10/14/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `calibrate-more-threads` --- ### 📝 Commits (2) - [`8feffa4`](https://github.com/mw00/project-maya/commit/8feffa43f3419bf7f48a82314d10e7cc0fa57abc) Calibration: more CPU-lane threads than the engine's own are measured too - the engine runs with STRATA_GLM_CPU_LANE_MAX (a pool of the PC's CPUs, the engine's own count active, the rest asleep), so a request's cpu_threads can try up to the CPUs less two; upward only while each count beats the best so far. A KVM guest's 16 vCPUs have no SMT siblings and the engine takes two thirds of them: the calibration kept 10 before and keeps 14 now (Maya-L, one RTX 3090 Ti, --bench decode 14.9 -> 17.6 tok/s, #177) - [`556ed79`](https://github.com/mw00/project-maya/commit/556ed795d72150c33d03f678064989e0f537d1c2) Calibration's higher thread counts: only up to the pool the engine reports (INFO cpu_threads_max) - a pool a split of 3+ GPUs shares, or DFlash2's two lanes, does not take STRATA_GLM_CPU_LANE_MAX, and a request above it ran the engine's own count again under the higher count's name; the bigger pool only on one GPU without SMT siblings. Workers left out by set_active wait on a condition of their own, outside sleeping_ - every batch's notify_all woke them to sleep again, which made the lower counts measure slower (32 vCPUs, 10 of a pool of 32 active: 19.4 -> 7.1 us a synthetic batch; a pool of its own count unchanged, 7.4 / 7.2). DFlash2's two-lane line names the active count (#178's review) ### 📊 Changes **9 files changed** (+198 additions, -31 deletions) <details> <summary>View changed files</summary> 📝 `docs/SETTINGS.md` (+4 -1) 📝 `include/strata/core/glm_model.hpp` (+3 -1) 📝 `maya.py` (+5 -5) 📝 `serve/server.py` (+1 -1) 📝 `src/core/glm_fast_path.cu` (+18 -3) 📝 `src/core/glm_fast_state.hpp` (+27 -9) 📝 `src/program/generate.cpp` (+5 -3) 📝 `tools/calibrate_glm.py` (+58 -3) 📝 `tools/test_calibrate_glm.py` (+77 -5) </details> ### 📄 Description Follow-up to #177. `./maya.sh --calibrate` only measures fewer CPU-lane threads than the engine's own count: it lowers the active count of the pool the engine started with. So it cannot find a count above the engine's own. On a KVM guest with 16 vCPUs (no SMT siblings visible) the engine takes two thirds of them (10), and 14 decodes about 18% faster. The calibration kept 10. With this change it measures counts above too, and keeps 14. Two commits on v1.1.11: 1a1eb52 (the change) and 7847aba (the review's fixes: the reported pool size, the sleeping workers, one GPU without SMT, the DFlash2 log line). ## The change **Engine: `STRATA_GLM_CPU_LANE_MAX=N`** (unset by default, so nothing changes without it). - When N is more than the lane's count, the CPU-lane pool holds N threads with the lane's own count active. A request's `cpu_threads` can then go up to N without a restart. - A request that names no `cpu_threads` returns to the lane's own count (`FastState::cpu_threads0`), not to the whole pool, so the calibration's "engine's own" baseline stays the engine's own. - It applies only to a pool of its own. A pool a split of 3+ GPUs shares does not take it, and DFlash2's two-lane rebuild clears `cpu_threads0`. - The INFO line reports the pool's size as `cpu_threads_max` (`Glm5Model::cpu_lane_threads_max()`), so a caller knows the most threads a request gets. **Engine: `Workers` threads above `active()`** wait on a condition variable of their own (`idle_cv_`) that only `set_active` wakes (under `mu_`, so no wake-up is missed). They are not counted in `sleeping_`. Before, every batch's `notify_all` woke them to sleep again, and their presence made `run()` notify at all. The fewer threads active, the more of them there were, which slowed the lower counts the calibration measures. This also removes that cost from the existing downward sweep. **`tools/calibrate_glm.py`:** - The calibration's engine runs with `STRATA_GLM_CPU_LANE_MAX` set to the CPUs the process may use (`sched_getaffinity`, at most 64). This applies only on one GPU and where no CPU's `thread_siblings_list` names a sibling (`lane_max_env`, `smt_visible`). With SMT the engine's own count is already a thread per physical core, and a split has a pool per GPU or a shared one. - After the existing downward sweep, it measures counts above the engine's own: the CPUs less two, and halfway there (`more_thread_candidates`). It goes up to the pool the engine reports (`cpu_threads_max`) and never above it. Without that key (an older engine) there is no upward sweep. - Going upward stops at the first count that does not beat the best so far. The winner goes through the same interleaved confirmation and `MIN_GAIN` as before. **Tests:** seven new cases in `tools/test_calibrate_glm.py`. The fake engine now has a pool that caps `cpu_threads` as `set_active` does, and records the count that really ran. - Higher counts are measured and kept. - The upward sweep stops once slower. - No upward sweep without pool room. - A pool that ignores `MAX` is never asked for more. This is the review's case (22 shared threads, `MAX` 64, one 1% high measurement and a winning 0.10 share); it fails on 1a1eb52 alone. - An engine without `cpu_threads_max` gets no upward sweep. - `lane_max_env` and `smt_visible` on a fake sysfs. - `more_thread_candidates`. **Docs:** - A `STRATA_GLM_CPU_LANE_MAX` row in the tuning table (`docs/SETTINGS.md`, where the README's table moved). - The tuning's description in `docs/SETTINGS.md` and `maya.py` says "fewer and more" threads. - The server's `strata_tune` comment. - DFlash2's two-lane log line prints the active count. The engine's own choice of thread count is not changed. ## The machine - RTX 3090 Ti 24 GB, driver 615.71.09, CUDA 13.4, PCIe gen 3 x16 as reported. - AMD EPYC 7302P as a KVM guest, 1 NUMA node, no SMT siblings visible: **16 vCPUs** for the first measurements, **32 vCPUs** for the later ones (the host gave the guest 16 more; on the 7302P these are the same 16 cores' second threads). - 240 GB RAM. Debian 12, kernel 6.1. - Maya-L, context 32768, `--prefill auto`, `--kv-streaming on`, the shipped expert profile, no vision. Every off-card expert fits in the RAM tier (0 disk reads a token). ## Before and after ### 16 vCPUs (first commit, on v1.1.7) **`tools/calibrate_glm.py`** on the installer's config (no `"env"`), main's tool and engine, then the first commit's: | | main | first commit | | --- | --- | --- | | engine at start | 10 threads, 0.303 ms an expert on the CPU | 10 threads (pool of 16), 0.311 ms | | thread sweep (tok/s) | 10: 15.1, 8: 13.0, 7: 11.3, 5: 8.6 | 10: 14.6, 8: 12.8, 7: 11.0, 5: 8.4, **12: 16.2, 14: 18.6** | | confirmation | own/10: 15.0 15.0 14.5 vs 0.10/10: 15.7 15.2 15.1 | own/10: 14.9 14.2 15.7 vs 0.10/14: 17.7 17.8 17.0 | | kept | nothing (0.10 within `MIN_GAIN`) | `PCIE_SHARE=0.10`, `CPU_LANE=14` | The first commit's lower counts here still carried the sleeping-worker cost the review found (10 threads: 14.6 against main's 15.1). 7847aba removes it (below). These 16-vCPU runs were not repeated after the guest moved to 32 vCPUs. **`./maya.sh --bench`**: the installer's config against the same config with only `STRATA_GLM_CPU_LANE=14`, four runs alternating, a fresh engine each: | | stock, run 1 | 14 threads, run 1 | stock, run 2 | 14 threads, run 2 | | --- | ---: | ---: | ---: | ---: | | decode, CSV function | 14.3 | 16.7 | 14.3 | 17.1 | | decode, photosynthesis | 15.9 | 18.6 | 16.0 | 18.8 | | decode, bouncing ball | 14.5 | 17.2 | 14.6 | 17.3 | | **decode, mean** | **14.9** | **17.5** | **15.0** | **17.7** (+18%) | | prefill 2048 | 265 | 291 | 264 | 291 (+10%) | | prefill 8192 | 743 | 789 | 744 | 789 (+6%) | ### 32 vCPUs (both commits, on v1.1.11) **`tools/calibrate_glm.py`**, same stock config, main's tool and engine, then this branch's: | | main | this branch | | --- | --- | --- | | engine at start | 21 threads (pool 21), 0.275 ms an expert | 21 threads (pool 32), 0.275 ms | | thread sweep (tok/s) | 21: 15.9, 16: 17.4, 14: 16.7, 10: 13.8, 5: 8.4 | 21: 16.0, 16: 17.5, 14: 17.3, 10: 14.6, 5: 8.9, **26: 18.6, 30: 17.9** | | confirmation | own/21: 16.6 15.9 16.2 vs 0.10/16: 16.9 16.2 17.8 | own/21: 16.0 16.3 15.0 vs 0.10/26: 17.0 18.5 17.8 | | kept | `PCIE_SHARE=0.10`, `CPU_LANE=16` | `PCIE_SHARE=0.10`, `CPU_LANE=26` | | time | 670 s | 712 s | - **Baseline:** the engine's own count measures the same with 11 sleeping workers as main's does with none (16.0 against 15.9). - **Lower counts:** these measure 3-6% higher than on main, whose downward sweep has the sleeping-worker cost. - **The upward sweep's value:** with 32 vCPUs, main's downward sweep finds a better count itself (16). The upward sweep matters where the engine's own count sits below the best, as with 16 vCPUs. - **A separate harness** on this box, a fresh engine start per count (six chat prompts, ~2,100 tokens), had 14 / 21 / 24 / 27 / 30 threads at 19.5 / 17.8 / 18.7 / 19.6 / 19.8 tok/s. So 14 to 30 land within a few percent of each other here, and the default 21 is the slow one. ### `Workers` alone `Workers` copied verbatim from main's header and this branch's into one program. The job is synthetic (about 4,000 multiply-adds), `spin_us` 20000 as the engine uses, best of three alternating runs, 32 vCPUs: | pool, active | main | this branch | | --- | ---: | ---: | | 32, 10 | 19.4 µs a batch | 7.1 µs | | 32, 14 | 18.6 | 7.8 | | 32, 21 | 19.8 | 8.4 | | 16, 10 | 12.8 | 6.8 | | 10, 10 (no extra workers) | 7.4 | 7.2 | | 21, 21 (no extra workers) | 8.6 | 8.3 | ## Checks On the same Linux box, with the repository's CI commands, on this branch over v1.1.11: - `tools/check_hip_compat.py`: every CUDA name has a mapping (no new CUDA calls). - `python -m unittest discover -s serve -p "test_*.py" -t .`: 189 tests OK (3 skipped). - `python -m unittest discover -s tools -p "test_*.py"`: 272 tests OK (27 skipped), against 265 on main. The engine was built for sm_86 only. ## Output Different thread counts can change which expert runs on the CPU or the GPU, so the last bits of an answer can change, as CONTRIBUTING says. With `STRATA_GLM_CPU_LANE_MAX` unset, the lane's pool and its counts are as before. The `Workers` change only affects threads above `active()`, which a pool has only with `MAX` set or while the calibration's downward sweep runs. ## Not measured - Other NVIDIA targets, Windows, AMD. - A split of 2+ GPUs, where the upward sweep is now off. - A PC with SMT siblings, where it is off too. - A hybrid CPU with disk reads without SMT (the case beside the two-thirds rule). There the upward sweep would try the CPUs less two and halfway there, and stop at the first that is not faster. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-29 21:24:49 +07:00
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
mw00/project-maya#375
No description provided.