Skip to content

runtime: retain batch costs by slot count and pass packing - #741

Merged
juliankang4 merged 2 commits into
midagedev:mainfrom
juliankang4:fix-width-batch-cost
Oct 23, 2026
Merged

juliankang4 merged 2 commits into
midagedev:mainfrom
juliankang4:fix-width-batch-cost

Conversation

@juliankang4

Copy link
Copy Markdown
Collaborator

MTP draft 폭을 고르는 비용 모델이 서로 다른 batch 구성의 측정값을 섞고 슬롯 수가 바뀔 때마다 학습을 잃던 두 가지 결함을 고칩니다.

Part of #565. Tracked in #699.

What and why

The cost-based draft width chooser prices one shared verify round by its measured wall time. Two defects in how it keeps those measurements made it choose widths from the wrong readings. Both come from bb2173656 (#690).

  1. Readings were keyed by the round's total row count. A round of five draft windows with 16 verified rows can be cut into passes of at most 8 rows in different ways. With a pass bound of 8, windows of [3, 3, 3, 3, 4] rows run as three passes and windows of [4, 4, 3, 3, 2] rows run as two, yet both total 16 rows and shared one reading. Three-pass and two-pass walls were averaged into one number, which distorts the width choice.
  2. Readings belonged to one active-slot count and were dropped when the count changed. When two and three slots alternate, every round started with no readings, so the chooser never finished exploring. With zero acceptance it kept proposing the maximum width of 3 and never narrowed to 0. The reset in the current main is c37a56a48, which has the same effect.

The fix has three parts.

  • Batch in crates/runtime/src/width.rs keys its readings by the sorted row count of each actual pass, and keeps readings and probe progress per active-slot count and body pass bound, so returning to a slot count resumes its learning. A packing that was never observed is priced by interpolating wall time per pass by mean rows per pass and multiplying by the pass count. An observed packing's median is never merged with another packing's.
  • The pass planning moved to one owner, runtime::round_passes in crates/runtime/src/lib.rs. The cost chooser and the app's round execution both call it, so the cost model prices the passes that actually run. The old packing code in crates/app/src/lib.rs is removed, and its function now only converts the error type. The planner tests the row sum as packed <= max_rows - n, so a window of usize::MAX rows cannot overflow.
  • crates/gpu-gates/src/bin/shared/serve_seats/rounds.rs passes the body's pass bound (SlotRows::MAX_ROWS) and each window's real row count to Batch::choose and Batch::observe. Qwen and GLM both go through this function, and the bound is data, so no rank count or model fact is hard coded.

The minimum reading count, the margin, the probe period and the allowed-width rule are unchanged. Kernel arithmetic and the token acceptance rule are unchanged.

Release notes: Fixed: The MTP cost-based draft width choice no longer mixes timings of different pass layouts and keeps what it learned when the number of active slots changes.

Code shape

  • No second copy. Pass packing has one owner, runtime::round_passes. The copy that lived in crates/app/src/lib.rs is deleted and the app calls the owner. A test in the app fails if that call is removed.
  • Model code holds only architecture facts. The shared rounds function passes the body's MAX_ROWS. No model file gained a branch.
  • Dead code is gone. The per-row-count timing table the old key needed no longer exists.
  • No silent failure. An empty or oversized window is a named RoundError, and a refusal leaves the previous plan untouched.
  • Comments state what is true now. The comments describe the current key, the interpolation and the planner contract.
  • No relaxed gate. No band, tolerance or lint level moved.
  • cargo fmt and cargo clippy are clean on what I touched. cargo clippy has not been run on this head, so the lint count is unchecked.

Duplication found or removed (path:line): the packing loop in crates/app/src/lib.rs round_passes moved to crates/runtime/src/lib.rs round_passes. The duplicate scan reports no remaining candidates.

What I ran

The PR head is c3cdb34a6d3645fa4713ddcb922e02219c77f980, two commits on main 648dd751d9ed25f9a18d8a404e5f08e3fb52e512: a test commit that pins the isolation, then the fix. The unit tests and the app test below ran on a tree whose four changed files are identical to this head's.

FAIL-first on the production width code of main, with only the two new regression tests added: both fail, batch_costs_keep_different_packings_separate (the two-pass layout got readings [0, 0, 0, 0, 0] instead of [3, 3, 2, 2, 1]) and batch_costs_survive_alternating_slot_counts (the learned plain steps for 2 slots were [3, 3] instead of [0, 0]). Test rc is 101. With the fix the runtime library passes with 288 passed and 0 failed.

Overflow boundary: with the row sum written as an unchecked addition, round_packing_keeps_windows_and_the_slot_zero_pass_last fails with attempt to add with overflow. The checked form packed <= max_rows - n passes.

App caller: with the call to runtime::round_passes removed from the app, round_passes_keep_each_drafts_hidden_rows fails. With the call restored it passes.

cargo test -p bloomery-runtime --lib
test result: ok. 288 passed; 0 failed; 0 ignored; 0 measured; 285 filtered out

cargo check --workspace --all-targets completed on the node, rc 0. All host results were produced on Linux.

GPU Evidence

An A/B of this head against its base, on a DGX Spark (NVIDIA GB10, sm_121):

| NVIDIA-SMI 595.84                 Driver Version: 595.84         CUDA Version: 13.3     |
|   0  NVIDIA GB10                    On  |   0000000F:01:00.0 Off |                  N/A |

Base is 648dd751d9ed25f9a18d8a404e5f08e3fb52e512 and head is c3cdb34a6d3645fa4713ddcb922e02219c77f980. Each tree was built with cargo oxide build --arch sm_121 -p bloomery-gpu-gates --features deepseek41,gemma-embedding2,hf,checkpoint --release --bin bloomery-serve-qwen38, and both builds finished. The model is nvidia/Qwen3.8-Flash-Next-FP4-Local at revision b9ce18a6887e18c089fdea8509a33e63429a3300. Both servers ran with BLOOMERY_DRAFT=mtp BLOOMERY_MTP_WIDTH=cost BLOOMERY_HOST_ROOM=96G and bloomery-serve-qwen38 --parallel 4 --ctx-size 16896 --cache-ram 0. Each case sends 4096 prompt ids and 128 output ids at concurrency 2 or 4, with temperature 0 or 0.7 and seed 1741, on two corpora (code and English prose). The corpora ids hash the same in both arms. Base ran first, then head, in one lease, each server loaded once. The GPU clock cap was lifted for the measurement and restored afterwards. This is one pair per case, with no repetition.

tok/s is the summed client throughput while every request was active. Delta is (head / base - 1) * 100 [derived].

Corpus Concurrency Temperature Base tok/s Head tok/s Delta [derived]
code 2 0.0 69.69 70.15 +0.65%
code 2 0.7 45.18 45.58 +0.88%
code 4 0.0 68.54 71.05 +3.67%
code 4 0.7 58.05 60.28 +3.84%
prose 2 0.0 53.87 52.77 -2.05%
prose 2 0.7 45.40 46.07 +1.49%
prose 4 0.0 63.25 65.21 +3.10%
prose 4 0.7 60.33 62.44 +3.49%

The output token ids are the same in all eight cases: 24 requests with 128 ids each matched between base and head. Seven cases are faster and prose at concurrency 2 with greedy decoding is 2.05% slower. In that case head picks width 2 more often than base. With one pair per case I do not read any of these as a speed gain or a regression. The sampled cases propose only one to six drafts, so they say little about sampled MTP.

just gate-gpu-qwen38-serve not run: it needs the maintainers' workstation. just contrib-check and the pre-push check set were not run on this head either. The comparison above is the only GPU run.

What I ran into

This section does not block the PR.

  • The prose case at concurrency 2 with greedy decoding came out 2.05% slower in its single pair, with more width-2 choices than on base. Whether the cost estimate for unseen packings needs tuning for such content is open, and this PR does not change it.
  • cargo clippy was not run on this head.

AI help

AI 코딩 에이전트가 코드를 작성한 후 설명을 썼고, 테스트를 마쳤습니다. diff 리뷰와 PR 초안 리뷰는 제가 직접 했습니다.

@github-actions github-actions Bot added gpu The card engine: crates/gpu*, crates/app host Host crates: qdot, threads, engram, engram-lab, runtime, sampler, levers, decision, vision, refset needs-gpu Changes code a card must check, and carries no gate run on a card labels Oct 23, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: CLEAN: No must-fix finding remains; the evidence and diagnostic requests are advisory.

Non-blocking

⚠ Attach evidence for the reviewed head: crates/gpu-gates/src/bin/shared/serve_seats/rounds.rs:314. The body reports an A/B of c3cdb34 against 648dd75; this review covers bc30951 against 8249381. Attach the selected owning gate commands and output for these revisions, plus identical PTX scans for the planner move, to verify the reviewed decode path.

⚠ Expose retained learning: crates/runtime/src/width.rs:459. Batch keys, reading counts and probe progress remain private; the existing width records expose the per-slot chooser. Expose the batch state through stats or records, or explain the omitted diagnostic layer, so later resets or timing merges can be diagnosed from a run.

Optimization headroom

  • E. Less work: crates/runtime/src/width.rs:437. On each batch decode round, every unseen candidate recomputes every stored median. Maintain the existing sorted buffer when readings change to remove up to 64 × C × K / T bytes of scoring copies per output token [derived]: C unseen candidates, K stored layouts, up to eight f64 readings each, and T output tokens per round. No additional state is needed.

Checked

Duplicates: the app delegates packing to runtime; no second packing owner found. Dryer reported zero pairs and excluded GPU-gates; its changed caller was read manually.
Complexity: choose 25→31 adds state-selection and packing branches; observe 3→9 adds validation and layout insertion. Both grow existing functions within the batch owner; complexity alone yields no finding.
Silent failure: missing readings request exploration; absent slot 0 preserves order; unmatched packings interpolate or panic explicitly. Planner and observation errors propagate to callers.
Tests, static inspection only: packing assertions cover overflow and refusal preserving the plan; batch assertions cover packing isolation, alternating slot counts, invalid readings and interpolation. Rust tests were not run during validation.
Gates: no tolerance or threshold changed. The removed prompt-row overflow test case has a dated PIN and reason at width.rs:3188.
Escape catalog: C1–C4 do not match this diff.

Notes

The PR carries no just gate-* command with passing output. Its A/B does not satisfy the requested GPU gate evidence record.

Card check: needs-gpu. This pull request changes crates/app/src/lib.rs, crates/gpu-gates/src/bin/shared/serve_seats/rounds.rs and carries no gate run on a card (CONTRIBUTING's evidence list); a maintainer adds gpu-verified after one.

🔨 Shingler, bloomery's review bot: approves a clean pull request and requests changes on a must-fix (how it works)

@juliankang4

Copy link
Copy Markdown
Collaborator Author

The review is done and the contribution check passed, so I'm merging this.

@juliankang4
juliankang4 merged commit 8315c7a into midagedev:main Oct 23, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gpu The card engine: crates/gpu*, crates/app host Host crates: qdot, threads, engram, engram-lab, runtime, sampler, levers, decision, vision, refset needs-gpu Changes code a card must check, and carries no gate run on a card

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant