Skip to content

feat(router): token-weighted load_based for prefill - #99

Closed
AdaAibaby wants to merge 1 commit into
infrawaves/dev/v0.5.21from
feat/v0.5.21-router-loadbase-token-1104
Closed

AdaAibaby wants to merge 1 commit into
infrawaves/dev/v0.5.21from
feat/v0.5.21-router-loadbase-token-1104

Conversation

@AdaAibaby

@AdaAibaby AdaAibaby commented Nov 4, 2026 •

Copy link
Copy Markdown

Summary

load_based ranks prefill workers by request count, but prefill time follows the uncached prompt tokens, so two workers with the same count can carry very different work. This adds --load-based-metric tokens (default requests, unchanged), which ranks prefill workers by prompt tokens still to prefill.

Rebased from feat/v0.5.21-router-loadbase-token (based on infrawaves/prod/v0.5.21) onto infrawaves/dev/v0.5.21 @ 4e30be7dba.

Engine

  • num_waiting_uncached_tokens counts the waiting and bootstrap queues, discounted by the matched prefix and the queried L3 hit, plus the chunked and round-robin remainders. The L3 hit stays discounted after the fetch completes and before admission: cache mode keeps the parked span start even when a concurrent fetch already inserted the whole prefix; buffer mode reads the staged prefetch.
  • New cumulative total_prefill_input_tokens (prompt tokens prefilled, cache hits included), appended to the LoadStat wire array so older routers ignore it.

Router

  • Pending tokens per worker: the engine snapshot when every candidate has a fresh, rank-complete sample, plus this replica's dispatches since the snapshot scaled by the worker's cold ratio. Otherwise, router-tracked in-flight tokens for all candidates.
  • Cold ratio: EMA over 200k-token windows of the engine's uncached / input counter deltas, clamped to [0.01, 1]. Engines without the input counter keep 1.0.
  • Gate: --load-based-token-slack (default 8) excludes workers whose request count exceeds the fleet minimum by more than the slack. --load-based-token-tie-band (default 16384) treats token loads within the band as tied and ranks them by request count. The gate is an eligibility filter, so fused / score_policy / Pipeline compositions cannot score a gated worker back in.
  • Routing uses a text estimate of the prompt length instead of a full encode; capacity admission keeps exact lengths. /v1/responses keeps the body-size estimate, since the text estimate reads chat messages only.
  • tokens requires the legacy chat routing path; startup fails under --chat-routing reorg.

Observability

New per-prefill-worker gauges on /metrics, computed at scrape time:

Metric Meaning
sgl_router_worker_engine_load_fresh 1 if the worker has a fresh, rank-complete engine sample
sgl_router_worker_engine_load_age_seconds Age of that sample
sgl_router_worker_pending_prefill_tokens{source} Pending tokens from the same source routing uses: engine only when every prefill worker of the model is fresh, otherwise local
sgl_router_worker_prefill_cold_ratio Measured cold ratio; absent until the first window closes

Rebase conflicts

  • chat/preparation.rs: kept dev's Responses handling, and kept Responses off the text estimate (see above). Covered by responses_token_count_keeps_body_size_estimate.
  • chat/reorg.rs: dev's comment plus the new prepare argument.
  • tests/proxy/pd_bootstrap_injection.rs: took dev's version.

Testing

  • cargo clippy --all-targets -- -D warnings: clean.
  • cargo test --lib: 833 passed.
  • cargo test --test component: 114 passed. render_parity was skipped because it needs model snapshots.
  • cargo test --test proxy: 163 passed. caller_ids_fan_out_and_rejected_streams_do_not_abort was skipped because it hangs; it also hangs on the base branch.
  • Python (test_storage_prefetch_lifecycle.py, test_loadstat_wire.py, test_disagg_idle_step_counters.py, test_load_inquirer.py): 80 passed.

Not verified

  • PD smoke test on the dev baseline with HiCache L3.
  • A/B tests on the Kimi-K3 PD deployment sent all traffic through a single router replica. Production runs 3 replicas; that case relies on the engine snapshot plus the tie band to avoid herding and has not been measured.

…ric)

load_based ranks prefill workers by request count, but prefill time follows
the uncached prompt tokens, so two workers with equal counts can carry very
different work. --load-based-metric tokens (default requests, unchanged)
ranks them by prompt tokens still to prefill.

Engine
- num_waiting_uncached_tokens counts the waiting and bootstrap queues,
  discounted by the matched prefix and by the queried L3 hit
  (get_pending_storage_hit_end), plus the chunked and round-robin
  remainders.
  The L3 hit stays discounted after the fetch completes and before
  admission: cache mode keeps the parked span start even when a concurrent
  fetch already inserted the whole prefix, buffer mode reads the staged
  prefetch.
- New cumulative total_prefill_input_tokens: prompt tokens prefilled, cache
  hits included. It advances with total_prefill_uncached_tokens under the
  same step gate and is appended to the LoadStat wire array, so older
  routers ignore it.

Router
- Pending tokens per worker: the engine snapshot when every candidate
  reports, plus this replica's dispatches since the snapshot scaled by the
  worker's cold ratio; otherwise router-tracked in-flight tokens for all.
- Cold ratio: EMA over 200k-token windows of the engine's uncached / input
  counter deltas, clamped to [0.01, 1]. Both counters cover every router
  replica. Engines without the input counter leave in-flight tokens raw.
- Gate: workers whose request count exceeds the fleet minimum by more than
  --load-based-token-slack (default 8) are not picked. Token loads within
  --load-based-token-tie-band (default 16384) tie and rank by request count.
  The gate is exposed as an eligibility filter, so fused / score_policy /
  Pipeline compositions cannot score a gated worker back in.
- Routing uses a text estimate of the prompt length instead of a full
  encode; capacity admission keeps exact lengths. /v1/responses keeps the
  body-size estimate, since the text estimate reads chat messages only.

Observability
- /metrics adds per prefill worker: sgl_router_worker_engine_load_fresh,
  sgl_router_worker_engine_load_age_seconds,
  sgl_router_worker_pending_prefill_tokens{source} and
  sgl_router_worker_prefill_cold_ratio. Pending tokens follow the routing
  source: source="engine" (engine_pending_prefill_tokens) only when every
  prefill worker of the worker's models is fresh, otherwise
  source="local" with router in-flight tokens. The cold ratio is
  absent until the first window closes. Computed at scrape time; the
  cold ratio is read without feeding the estimator.
- The pending-prefill debug log adds snapshot_age_ms.

Also fixes two stale test fixtures on dev: the idle-step Scheduler fixture
now sets enable_chunked_prefill_round_robin, read by
reset_prefill_ready_seq_if_idle(); test_load_inquirer derives its totals
from the queue list after the demotion revert dropped one queue.
@chencjcj
chencjcj force-pushed the feat/v0.5.21-router-loadbase-token-1104 branch from b2b996a to a2ae340 Compare November 4, 2026 07:42
@chencjcj chencjcj closed this Nov 4, 2026
Nozom1ff added a commit that referenced this pull request Nov 7, 2026
…vation

v0.5.23 (sgl-project#44401) made get_num_allocatable_reqs give back the rows that
counted requests already hold, and the admission loop now passes the
continuation's row. The replayed HRRN chunk-limit reservation (#99) still
asked for the cap without it and then subtracted the continuation again,
so with free rows binding it reserved budget for one short request fewer
than the pass admits (none at one free row).

Co-Authored-By: Claude <[email protected]>
Nozom1ff added a commit that referenced this pull request Nov 7, 2026
The per-node MLA host tests (ported with the #99-#105 net diff) still
passed v0.5.23's mamba_layer_mapping/use_mla and unpacked two values;
the day-0 assembler takes start_layer and returns (group, controller,
config). The pool-host mocks are unchanged and still need a run on GPU.

Co-Authored-By: Claude <[email protected]>
Nozom1ff added a commit that referenced this pull request Nov 7, 2026
sgl-project#46977 moved PrefillAdder.rem_mamba_slots onto memory_budget.rem_state_slots,
but reservable_new_reqs (our #99 HRRN chunk-limit reservation) still read the
removed attribute. The merge was clean, so it surfaced only at runtime:
AttributeError: 'PrefillAdder' object has no attribute 'rem_mamba_slots' the
first time HRRN admits new requests next to a chunked continuation, which kills
the prefill scheduler under load.

Read rem_state_slots from the budget instead; budgets without state slots have
no such attribute and keep no mamba cap, as before. Update the unit test that
set the removed attribute.

Co-Authored-By: Claude <[email protected]>
@chencjcj
chencjcj deleted the feat/v0.5.21-router-loadbase-token-1104 branch November 20, 2026 06:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants