[PR #3194] [Engine] serve: per-layer expert routing metrics for /metrics ("routing_counts", opt-in) #4496

Open
opened 2026-11-07 15:36:27 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/3194
Author: @blange48
Created: 10/19/2026
Status: 🔄 Open

Base: main ← Head: routing-counts


📝 Commits (5)

  • c82cd96 serve: per-layer expert routing metrics (--routing-counts, "routing_counts" in the config, opt-in)
  • d2defb7 serve: each layer's 8 most routed experts as their own counters (strata:expert_routed_by_expert_total)
  • 9888cff docs: per-layer expert routing metrics ("routing_counts")
  • 7ede4a3 docs/monitoring: an Experts row in grafana-strata.json for the routing_counts metrics
  • 47e57d9 routing counts: replace the last file on Windows too; all 8 tests run as a script; the counts are decode windows only

📊 Changes

6 files changed (+599 additions, -3 deletions)

View changed files

📝 docs/DETAILS.md (+16 -1)
📝 docs/monitoring/grafana-strata.json (+413 -0)
📝 serve/prometheus.py (+23 -0)
📝 serve/server.py (+52 -2)
📝 serve/test_prometheus.py (+34 -0)
📝 src/program/generate.cpp (+61 -0)

📄 Description

Problem and result

Which experts do the requests actually use? Today the only way to see it is a --dump-routing trace (~4 KB per generated token, rewritten at every start) and an offline script. This PR adds a cheap view of the same data on /metrics, so Prometheus and Grafana can show it.

  • Before: no routing information on /metrics.
  • After, with the opt-in key:
    • the JSON /metrics gets an experts section: per layer, the experts seen, the top 32's share, the entropy in bits and the top 10 experts;
    • the Prometheus text gets strata:expert_routed_total, the per-layer gauges strata:expert_seen, strata:expert_top32_share and strata:expert_entropy_bits (label layer), and strata:expert_routed_by_expert_total for each layer's 8 most routed experts (labels layer, expert). That is ~530 series, not 24,576.
  • Without the key nothing is counted, written or reported.

#1547, closed in the 0.2.0 tracker reset. The /reopen check passed there, but GitHub would not reopen it after the rebase force-push, so this is the same branch as a new pull request. The earlier review (SuperDistance, Windows builds included) is in #1547.

Approach

  • Engine (--routing-counts PATH, --routing-counts-every MIN): one uint64 counter per (layer, expert), incremented where the routing trace reads the ids (drive_pool / drive_pool_multi, every layer of every decode window, drafted tokens included; prompt reads are not counted). Nothing is written per token. The counts are saved as JSON every minute between requests (the same spot as the expert-profile save) and on QUIT, through a temporary file renamed over the last one with std::filesystem::rename, so it replaces the old file on Windows too. A 48 × 512 file is ~55-85 KB.
  • Server ("routing_counts": "<path>", optional "routing_counts_every"): passes the flags, and GET /metrics reads the file, cached on its mtime.
  • Docs: docs/DETAILS.md, and an Experts row in docs/monitoring/grafana-strata.json.
  • Compatibility: off by default; no change to any other metric or to the engine's work when the key is absent.

Validation

Passed:

  • python serve/test_prometheus.py on this head (47e57d95, rebased on main): 8 tests OK (summary with counts, entropy, an empty layer, a missing file; gauges; per-expert counters, at most 8 per layer; config key to engine flags). macOS, Python 3.
  • Engine and server built from v0.2.0-beta plus this PR (CUDA 13.0, CUDA_ARCHITECTURES=120, Linux, docker), then pytest serve in that image: 864 passed, 18 skipped, 1 failed: test_lifeline.py::test_the_portable_watcher_ends_the_child_when_the_server_is_killed (the POSIX watcher path inside a container; this PR does not touch it, I have not checked whether it fails on v0.2.0-beta alone).
  • In production since 2026-10-08 on 0.1.40.3 to 0.1.45 builds: 4× RTX 5080, layer split 12,24,36, Qwen3.8-Flash-Next IQ3_S, --batch 8 --batch-groups 4. 384 strata:expert_routed_by_expert_total series exported at every release check, a Grafana row on top. Throughput with and without the key, 1 / 4 / 8 clients (T=0.7, 300 tokens, warm pass): 147 / 269 / 394 against 143 / 268 / 392 tok/s, within run-to-run noise. The counts agreed with a --dump-routing trace of the same traffic (layer 0's top experts 382, 300, 324 in both; every layer counted the same number of times).

Failed:

  • none observed in this PR's code (the lifeline test above is outside it).

Not run:

  • The runtime check on this exact head with 0.2.0-beta on the 4× RTX 5080 box: scheduled for tonight with our release gate; I will add the numbers here.
  • Windows and HIP builds of this head (SuperDistance built an earlier revision on Windows for CUDA sm_120 and HIP gfx1100, which is how the rename bug was found and fixed).

Limits

  • Counts restart at zero with the engine (a Prometheus counter handles that). The first file appears at the first request a minute after the start.
  • Prompt reads are not counted, only decode windows.

🤖 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/Niko1221/Strata/pull/3194 **Author:** [@blange48](https://github.com/blange48) **Created:** 10/19/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `routing-counts` --- ### 📝 Commits (5) - [`c82cd96`](https://github.com/Niko1221/Strata/commit/c82cd9691c386d393c7d3f695425dd5e24f91196) serve: per-layer expert routing metrics (--routing-counts, "routing_counts" in the config, opt-in) - [`d2defb7`](https://github.com/Niko1221/Strata/commit/d2defb762659e588c410b0db787a96bdc0d41ee6) serve: each layer's 8 most routed experts as their own counters (strata:expert_routed_by_expert_total) - [`9888cff`](https://github.com/Niko1221/Strata/commit/9888cffab3984756fd4c110c838289da04f0e686) docs: per-layer expert routing metrics ("routing_counts") - [`7ede4a3`](https://github.com/Niko1221/Strata/commit/7ede4a36e540de79fe376d18feb6cf1c321f674a) docs/monitoring: an Experts row in grafana-strata.json for the routing_counts metrics - [`47e57d9`](https://github.com/Niko1221/Strata/commit/47e57d95cdee3d6dcb17882351bd4650d9db0331) routing counts: replace the last file on Windows too; all 8 tests run as a script; the counts are decode windows only ### 📊 Changes **6 files changed** (+599 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `docs/DETAILS.md` (+16 -1) 📝 `docs/monitoring/grafana-strata.json` (+413 -0) 📝 `serve/prometheus.py` (+23 -0) 📝 `serve/server.py` (+52 -2) 📝 `serve/test_prometheus.py` (+34 -0) 📝 `src/program/generate.cpp` (+61 -0) </details> ### 📄 Description ## Problem and result Which experts do the requests actually use? Today the only way to see it is a `--dump-routing` trace (~4 KB per generated token, rewritten at every start) and an offline script. This PR adds a cheap view of the same data on `/metrics`, so Prometheus and Grafana can show it. - **Before:** no routing information on `/metrics`. - **After, with the opt-in key:** - the JSON `/metrics` gets an `experts` section: per layer, the experts seen, the top 32's share, the entropy in bits and the top 10 experts; - the Prometheus text gets `strata:expert_routed_total`, the per-layer gauges `strata:expert_seen`, `strata:expert_top32_share` and `strata:expert_entropy_bits` (label `layer`), and `strata:expert_routed_by_expert_total` for each layer's 8 most routed experts (labels `layer`, `expert`). That is ~530 series, not 24,576. - **Without the key** nothing is counted, written or reported. ## Related discussion #1547, closed in the 0.2.0 tracker reset. The `/reopen` check passed there, but GitHub would not reopen it after the rebase force-push, so this is the same branch as a new pull request. The earlier review (SuperDistance, Windows builds included) is in #1547. ## Approach - **Engine** (`--routing-counts PATH`, `--routing-counts-every MIN`): one `uint64` counter per (layer, expert), incremented where the routing trace reads the ids (`drive_pool` / `drive_pool_multi`, every layer of every decode window, drafted tokens included; prompt reads are not counted). Nothing is written per token. The counts are saved as JSON every minute between requests (the same spot as the expert-profile save) and on QUIT, through a temporary file renamed over the last one with `std::filesystem::rename`, so it replaces the old file on Windows too. A 48 × 512 file is ~55-85 KB. - **Server** (`"routing_counts": "<path>"`, optional `"routing_counts_every"`): passes the flags, and `GET /metrics` reads the file, cached on its mtime. - **Docs:** `docs/DETAILS.md`, and an Experts row in `docs/monitoring/grafana-strata.json`. - Compatibility: off by default; no change to any other metric or to the engine's work when the key is absent. ## Validation **Passed:** - `python serve/test_prometheus.py` on this head (47e57d95, rebased on main): 8 tests OK (summary with counts, entropy, an empty layer, a missing file; gauges; per-expert counters, at most 8 per layer; config key to engine flags). macOS, Python 3. - Engine and server built from v0.2.0-beta plus this PR (CUDA 13.0, `CUDA_ARCHITECTURES=120`, Linux, docker), then `pytest serve` in that image: 864 passed, 18 skipped, 1 failed: `test_lifeline.py::test_the_portable_watcher_ends_the_child_when_the_server_is_killed` (the POSIX watcher path inside a container; this PR does not touch it, I have not checked whether it fails on v0.2.0-beta alone). - In production since 2026-10-08 on 0.1.40.3 to 0.1.45 builds: 4× RTX 5080, layer split 12,24,36, Qwen3.8-Flash-Next IQ3_S, `--batch 8 --batch-groups 4`. 384 `strata:expert_routed_by_expert_total` series exported at every release check, a Grafana row on top. Throughput with and without the key, 1 / 4 / 8 clients (T=0.7, 300 tokens, warm pass): 147 / 269 / 394 against 143 / 268 / 392 tok/s, within run-to-run noise. The counts agreed with a `--dump-routing` trace of the same traffic (layer 0's top experts 382, 300, 324 in both; every layer counted the same number of times). **Failed:** - none observed in this PR's code (the lifeline test above is outside it). **Not run:** - The runtime check on this exact head with 0.2.0-beta on the 4× RTX 5080 box: scheduled for tonight with our release gate; I will add the numbers here. - Windows and HIP builds of this head (SuperDistance built an earlier revision on Windows for CUDA sm_120 and HIP gfx1100, which is how the rename bug was found and fixed). ## Limits - Counts restart at zero with the engine (a Prometheus counter handles that). The first file appears at the first request a minute after the start. - Prompt reads are not counted, only decode windows. 🤖 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>
Sign in to join this conversation.
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
Niko1221/Strata#4496
No description provided.