[PR #4777] [Engine] Dynamic route_prior runtime control via API, per-request params, and engine protocol #5176

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

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/4777
Author: @code-martin
Created: 10/31/2026
Status: 🔄 Open

Base: main ← Head: feature/dynamic-route-prior-v028


📝 Commits (1)

  • 55ec585 feat(routing): dynamic route_prior control via API, per-request params, and engine protocol

📊 Changes

6 files changed (+142 additions, -8 deletions)

View changed files

📝 include/strata/core/verify.hpp (+6 -0)
📝 serve/server.py (+25 -0)
📝 serve/web/app.js (+10 -2)
📝 serve/web/index.html (+5 -0)
📝 src/core/verify.cpp (+65 -6)
📝 src/program/generate.cpp (+31 -0)

📄 Description

Problem and result

Before:
Residency-biased MoE routing (STRATA_ROUTE_PRIOR) was read once from the environment at startup or locked to static hardware heuristics. Calibrating or sweeping \lambda required terminating the server, restarting the process, reloading ~40 GB of model weights and expert caches, and dropping active KV/session contexts.

After:
Dynamic runtime control over route_prior \lambda across the full Strata stack:

  1. Engine interactive stdin protocol command: ROUTE_PRIOR <val>.
  2. Immediate verifier stage synchronization (stage_ver(st).set_route_prior(v)) with automatic CUDA graph invalidation (clear_graphs()) so new priors take effect on the next window without engine restart.
  3. Per-request override via OpenAI-compatible completion bodies ("route_prior": 0.5 or "strata_tune": {"route_prior": 0.5}).
  4. Server REST endpoints: GET /v1/route_prior and POST /v1/route_prior.

@telegram

Approach

  • Engine Core (src/core/verify.cpp, include/strata/core/verify.hpp): Added thread-safe atomic g_dyn_route_prior and per-instance route_prior_ field on Verifier. Added Verifier::set_route_prior(float rp) and Verifier::clear_graphs() to flush captured CUDA graphs when \lambda changes.
  • Server Protocol (src/program/generate.cpp): Handled ROUTE_PRIOR command on stdin and parsed per-request route_prior parameters in GEN protocol lines across both primary and draft stages.
  • HTTP Server (serve/server.py): Added /v1/route_prior REST endpoints and forwarded per-request route_prior down to the engine runner.

Validation

Passed:

  • Environment: Revision c3066e1c on feature/dynamic-route-prior-v028 (based on v0.2.8), Windows 11 x64 (Build 26100), NVIDIA GeForce RTX 3080 10GB (Driver 610.88, CUDA 13.1), 64GB DDR4 RAM, Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS (262k context, int8 KV, --expert-cache -1, --spec 2, --suffix-draft 3).
  • Build: cmake --build M:/Strata/build --config Release --target strata — built strata.exe with 0 errors.
  • Dynamic Protocol & REST API Verification:
    • curl -s http://127.0.0.1:8080/v1/route_prior -> {"route_prior": 0.0}
    • curl -s -X POST http://127.0.0.1:8080/v1/route_prior -H "Content-Type: application/json" -d '{"route_prior": 0.5}' -> {"ok": true, "route_prior": 0.5}
    • Prior updated dynamically in < 2\text{ ms} without dropping running server state or reloading model weights.
  • Mathematical Workload Benchmark (Newton's Method on \sqrt{55463}, x_0 = 100, 10 iterations):
    • Prompt: "Compute square root of 55463 using 10 iterations of Newton's method with initial guess=100. List all iterations." (seed = 42, max_tokens = 8192, 1 warm-up run, randomized order [1.0, 0.5, 1.5, 0.0], reference \sqrt{55463} \approx 235.505838569).
\lambda (route_prior) Generation Speed (predicted_per_second) Speedup Expert Cache Hit PCIe Routed Completion Tokens (predicted_n) Final Estimate x_{10} Convergence Accuracy
0.0 (baseline) 78.4 tok/s 1.00x 72.6% 8.9% 6,833 235.505838 Accuracy: 6 decimals
0.5 123.8 tok/s +57.9% 91.5% 2.6% 6,820 235.505841 Exact (x^2 = 55463.0000)
1.0 124.1 tok/s +58.3% 96.3% 0.9% 6,985 235.505800 Accuracy: 4 decimals
1.5 150.0 tok/s +91.3% 97.8% 0.5% 6,791 235.505818 Accuracy: 4 decimals

Failed:
none observed

Not run:

  • Non-CUDA backends (HIP / SYCL / Metal): Engine and verifier modifications are generic C++ host/stage logic and Python server code, not tested on AMD or Apple hardware.

Limits

  • Graph invalidation flushes captured CUDA graphs upon changing \lambda, requiring a single graph recapture on the first subsequent window.
  • Parameter applies to models with MoE expert residency biasing.

🔄 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/4777 **Author:** [@code-martin](https://github.com/code-martin) **Created:** 10/31/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `feature/dynamic-route-prior-v028` --- ### 📝 Commits (1) - [`55ec585`](https://github.com/Niko1221/Strata/commit/55ec585f7da0120cfe7ad7c837f56a7a45464ec5) feat(routing): dynamic route_prior control via API, per-request params, and engine protocol ### 📊 Changes **6 files changed** (+142 additions, -8 deletions) <details> <summary>View changed files</summary> 📝 `include/strata/core/verify.hpp` (+6 -0) 📝 `serve/server.py` (+25 -0) 📝 `serve/web/app.js` (+10 -2) 📝 `serve/web/index.html` (+5 -0) 📝 `src/core/verify.cpp` (+65 -6) 📝 `src/program/generate.cpp` (+31 -0) </details> ### 📄 Description ## Problem and result **Before:** Residency-biased MoE routing (`STRATA_ROUTE_PRIOR`) was read once from the environment at startup or locked to static hardware heuristics. Calibrating or sweeping $\lambda$ required terminating the server, restarting the process, reloading ~40 GB of model weights and expert caches, and dropping active KV/session contexts. **After:** Dynamic runtime control over `route_prior` $\lambda$ across the full Strata stack: 1. Engine interactive `stdin` protocol command: `ROUTE_PRIOR <val>`. 2. Immediate verifier stage synchronization (`stage_ver(st).set_route_prior(v)`) with automatic CUDA graph invalidation (`clear_graphs()`) so new priors take effect on the next window without engine restart. 3. Per-request override via OpenAI-compatible completion bodies (`"route_prior": 0.5` or `"strata_tune": {"route_prior": 0.5}`). 4. Server REST endpoints: `GET /v1/route_prior` and `POST /v1/route_prior`. ## Related discussion @telegram ## Approach - **Engine Core (`src/core/verify.cpp`, `include/strata/core/verify.hpp`):** Added thread-safe atomic `g_dyn_route_prior` and per-instance `route_prior_` field on `Verifier`. Added `Verifier::set_route_prior(float rp)` and `Verifier::clear_graphs()` to flush captured CUDA graphs when $\lambda$ changes. - **Server Protocol (`src/program/generate.cpp`):** Handled `ROUTE_PRIOR` command on stdin and parsed per-request `route_prior` parameters in `GEN` protocol lines across both primary and draft stages. - **HTTP Server (`serve/server.py`):** Added `/v1/route_prior` REST endpoints and forwarded per-request `route_prior` down to the engine runner. ## Validation **Passed:** - **Environment:** Revision `c3066e1c` on `feature/dynamic-route-prior-v028` (based on `v0.2.8`), Windows 11 x64 (Build 26100), NVIDIA GeForce RTX 3080 10GB (Driver 610.88, CUDA 13.1), 64GB DDR4 RAM, `Qwen3.8-Flash-Next-GSQ-RCO-IQ2_XS` (`262k` context, int8 KV, `--expert-cache -1`, `--spec 2`, `--suffix-draft 3`). - **Build:** `cmake --build M:/Strata/build --config Release --target strata` — built `strata.exe` with 0 errors. - **Dynamic Protocol & REST API Verification:** - `curl -s http://127.0.0.1:8080/v1/route_prior` -> `{"route_prior": 0.0}` - `curl -s -X POST http://127.0.0.1:8080/v1/route_prior -H "Content-Type: application/json" -d '{"route_prior": 0.5}'` -> `{"ok": true, "route_prior": 0.5}` - Prior updated dynamically in $< 2\text{ ms}$ without dropping running server state or reloading model weights. - **Mathematical Workload Benchmark (Newton's Method on $\sqrt{55463}$, $x_0 = 100$, 10 iterations):** - Prompt: `"Compute square root of 55463 using 10 iterations of Newton's method with initial guess=100. List all iterations."` (`seed = 42`, `max_tokens = 8192`, 1 warm-up run, randomized order `[1.0, 0.5, 1.5, 0.0]`, reference $\sqrt{55463} \approx 235.505838569$). | $\lambda$ (`route_prior`) | Generation Speed (`predicted_per_second`) | Speedup | Expert Cache Hit | PCIe Routed | Completion Tokens (`predicted_n`) | Final Estimate $x_{10}$ | Convergence Accuracy | |---|---|---|---|---|---|---|---| | **0.0** (baseline) | **78.4 tok/s** | 1.00x | 72.6% | 8.9% | 6,833 | 235.505838 | Accuracy: 6 decimals | | **0.5** | **123.8 tok/s** | **+57.9%** | 91.5% | 2.6% | 6,820 | 235.505841 | **Exact ($x^2 = 55463.0000$)** | | **1.0** | **124.1 tok/s** | **+58.3%** | 96.3% | 0.9% | 6,985 | 235.505800 | Accuracy: 4 decimals | | **1.5** | **150.0 tok/s** | **+91.3%** | 97.8% | 0.5% | 6,791 | 235.505818 | Accuracy: 4 decimals | **Failed:** none observed **Not run:** - Non-CUDA backends (HIP / SYCL / Metal): Engine and verifier modifications are generic C++ host/stage logic and Python server code, not tested on AMD or Apple hardware. ## Limits - Graph invalidation flushes captured CUDA graphs upon changing $\lambda$, requiring a single graph recapture on the first subsequent window. - Parameter applies to models with MoE expert residency biasing. --- <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#5176
No description provided.