Skip to content

refactor(worker): move decode composition root to worker - #892

Merged
Andrewxu313 merged 50 commits into
mainfrom
tairan/decode-round-dispatch-params
Dec 4, 2026
Merged

Andrewxu313 merged 50 commits into
mainfrom
tairan/decode-round-dispatch-params

Conversation

@Andrewxu313

@Andrewxu313 Andrewxu313 commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Description

Complete D8 (19/19) decode decoupling by moving the thin DecoupledGpuDecode.run composition root back onto BatchGenWorker._run_decoupled_gpu_decode and removing DecoupledGpuDecode and DecodeModelStateBinder from batchgen/worker/decode_engine.py.

Every remaining decode stage in batchgen/worker/decode_engine.py is now a pure explicit-parameter helper with zero worker parameters, zero worker attribute writes, and zero closures over the worker:

  • tests/worker/test_engine_worker_writes_single_owner.py ALLOWED -> {}
  • tests/worker/test_engine_closures_over_the_worker.py ALLOWED -> set()

Motivation

Matches the D5 (worker_lifecycle_engine.py) convergence pattern so batchgen/worker/decode_engine.py owns zero worker state and satisfies P1 single-writer and P3 one-way dependency invariants.

Type of Change

  • core — engine scaffolding, worker, scheduler, KV cache, or server

File changes

File Δ Note
batchgen/batchgen_worker.py mod Host _run_decoupled_gpu_decode thin composition root calling decode_engine explicit-parameter helpers
batchgen/worker/decode_engine.py mod Remove DecoupledGpuDecode and DecodeModelStateBinder and keep resolve_decode_step_rank_batch_size explicit-parameter helper
tests/test_decode_deferred_token_readback.py mod Re-anchor _loop() AST lookup to BatchGenWorker._run_decoupled_gpu_decode
tests/test_decode_exit_pending_load_integration.py mod Re-anchor AST decode-loop lookup to BatchGenWorker._run_decoupled_gpu_decode
tests/test_decode_host_kv_stream_order.py mod Re-anchor AST decode-loop lookup to BatchGenWorker._run_decoupled_gpu_decode
tests/test_decode_step_host_bookkeeping.py mod Re-anchor AST decode-loop lookup to BatchGenWorker._run_decoupled_gpu_decode
tests/test_decode_step_timing_wiring.py mod Re-anchor AST decode-loop lookup to BatchGenWorker._run_decoupled_gpu_decode
tests/test_worker_host_kv_append_completion.py mod Re-anchor AST decode-loop lookup to BatchGenWorker._run_decoupled_gpu_decode
tests/test_worker_pool_restart.py mod Re-anchor AST decode-loop lookup to BatchGenWorker._run_decoupled_gpu_decode
tests/test_worker_weight_passes.py mod Re-anchor decode forward weight-pass checks to extracted decode_engine helpers
tests/test_decode_transition_metadata.py mod Re-anchor decode wave transition sync check to decode_engine.configure_decode_wave_local_rows_and_graphs
tests/test_glm5_worker_fa3_wiring.py mod Update inline replay gate variable spellings in decode_engine.py
tests/test_kimi_k3_server_startup_init.py mod Re-anchor prewarm_decode_jit ordering check across generate() and decode_engine.configure_decode_wave_local_rows_and_graphs
tests/unit/test_debug_state_adoption.py mod Inspect both batchgen_worker.py and decode_engine.py for phase-entry debug adoption
tests/unit/test_decode_dp_group_packing.py mod Include decode_engine.py completion-cleanup loop in saved-group cleanup verification
tests/unit/test_decode_driver_unification.py mod Re-anchor AST decode-loop lookup to BatchGenWorker._run_decoupled_gpu_decode
tests/unit/test_decode_engine_empty_rank_names.py mod Re-anchor AST decode-loop lookup to BatchGenWorker._run_decoupled_gpu_decode
tests/unit/test_eventlog_step_counters_wiring.py mod Re-anchor AST decode-loop lookup to BatchGenWorker._run_decoupled_gpu_decode
tests/worker/test_boundary_decisions_direct_params.py mod Re-anchor boundary AST/caller checks to BatchGenWorker._run_decoupled_gpu_decode
tests/worker/test_boundary_follow_up_direct_params.py mod Re-anchor boundary follow-up AST checks to BatchGenWorker._run_decoupled_gpu_decode
tests/worker/test_decode_forward_prep_direct_params.py mod Re-anchor inference_mode AST check to BatchGenWorker._run_decoupled_gpu_decode
tests/worker/test_decode_forward_sample_direct_params.py mod Re-anchor inference_mode AST check to BatchGenWorker._run_decoupled_gpu_decode
tests/worker/test_decode_group_rows_log.py mod Re-anchor phase-entry sync AST check to BatchGenWorker._run_decoupled_gpu_decode
tests/worker/test_decode_round_obs_direct_params.py mod Re-anchor round-observability caller checks to BatchGenWorker._run_decoupled_gpu_decode
tests/worker/test_decode_round_prep_direct_params.py mod Re-anchor round-prep caller checks to BatchGenWorker._run_decoupled_gpu_decode
tests/worker/test_decode_width_bucket_invariant.py mod Track third _load_decode_model site in decode_engine.py and re-anchor over-bucket assertion
tests/worker/test_engine_closures_over_the_worker.py mod Clear ALLOWED to set() (zero worker-capturing engine closures remain)
tests/worker/test_engine_worker_writes_single_owner.py mod Clear ALLOWED to {} (zero engine writes to worker attributes remain)
tests/worker/test_glm5_criticality_probe_worker.py mod Re-anchor AST decode-loop and configure-wave lookups
tests/worker/test_worker_model_branches.py mod Shrink decode_engine.py model-branch span budget from 107 to 104

Checklist

  • I have read the CONTRIBUTING guide and the PR Merge Policy Contract.
  • I have updated the tests (if applicable).
  • I have updated the documentation (if applicable).
  • If README.md changed, I ran python3 scripts/check_readme_update.py --base-ref origin/main and preserved all News history.

Move the post-boundary page-table verification block of
DecoupledGpuDecode.run into a module-level verify_boundary_page_table
(keyword-only params, frozen BoundaryFollowUpOutcome): the guard, the
slot-order snapshot, the mapper call, the ordered comparison and the
rebuild. The row-epoch bump, both cumulative boundary counters and the
verification-flag reset stay verbatim in the caller, so they remain
published before the check runs and survive a mapper or rebuild error.
The deferred-readback drain and its shutdown exit are untouched.
Statements move verbatim apart from one spelling substitution; no new
import.

Tests: new declared-input tests for the check plus a caller-order pin
from the dispatch assignment through the flag rebind.
Move the post-boundary decision region of DecoupledGpuDecode.run into a
module-level decide_after_page_boundary (keyword-only params, frozen
BoundaryDecisionOutcome): the watermark hold, admission polling, the
rank-0 boundary log, the empty-decode pending-load integration and the
host-capacity votes. Each original break/continue becomes a return whose
outcome carries an explicit control result; the caller rebinds the eight
loop locals and then executes the break or continue itself. Worker
methods are passed as bound callables and invoked at their original
points, so every worker publication and every collective keeps its
order on all six exit paths. Statements move verbatim apart from
spelling substitution; no new import.

Tests: new declared-input tests for the six exit paths, wiring and
caller order; the watermark-hold and slot-8 caller pins re-anchor
through the new function without weakening.
Main gained the squash of the page-table check this branch is stacked on;
the tree is unchanged by this merge (conflicts were history-only).
Fix F541 lint violation on line 875 of batchgen/worker/decode_engine.py so the Slot 9 PR passes pr-lint-gate.
Update the hardcoded _BASELINE_REGION fixture in test_boundary_decisions_direct_params.py to drop the unused f-string prefix removed from decide_after_page_boundary.
Extract D8 Slot 10 forward metadata assembly, replay metadata preparation, and KV callback plus profiling setup out of DecoupledGpuDecode.run into keyword-only helpers with frozen outcomes, preserving the torch.inference_mode scope and Ruling 4 rank-count publication boundary.
…-params' into tairan/decode-forward-prep-params
Extract D8 Slot 11 (CUDA graph replay, eager forward, token selection, and post-graph KV staging) out of DecoupledGpuDecode.run into execute_decode_step_forward_and_sample with model-agnostic keyword-only parameters and DecodeStepForwardOutcome, preserving torch.inference_mode scope and re-anchoring decode dispatch AST tests.
Extract D8 Slot 12 (deferred and immediate token readback, host KV drain, trajectory append, per-row token writeback, and timing split) out of DecoupledGpuDecode.run into complete_decode_step_readback with keyword-only parameters and DecodeStepReadbackOutcome, keeping the ordered shutdown exit and cumulative forward timing write in the caller.
Drop the unused pytest import from tests/worker/test_decode_readback_direct_params.py to satisfy pr-lint-gate F401.
Align the three abandoned-exit AST tests in test_worker_pool_restart.py with complete_decode_step_readback and the decode loop _READBACK_SHUTDOWN handler.
Extract D8 Slots 13-16 decode wave preparation, batch sync, local row/graph configuration, and interval finalization into keyword-only explicit-parameter helpers in decode_engine.py with direct-parameter unit tests.
Remove the eliminated generate decode_uuids.sort lambda from test_worker_capturing_closures.py and re-anchor test_prefix_drain_metric.py to select_and_sync_decode_wave_batch.
Extract D8 Slots 17-18 legacy decode page boundary and CPU_ATTN / LEGACY_MICROBATCH_GPU step helpers into keyword-only explicit-parameter functions in decode_engine.py with direct-parameter tests.
Remove unused DecodeStrategy import from batchgen/worker/decode_engine.py after splitting CPU_ATTN and LEGACY_MICROBATCH_GPU helpers.
Complete D8 19/19 decode decoupling by moving the thin DecoupledGpuDecode.run composition root onto BatchGenWorker._run_decoupled_gpu_decode and removing DecoupledGpuDecode from decode_engine.py. All worker-attribute writes and worker-capturing closures across engine modules drop to zero.
Update the eight remaining decode-engine AST/direct-params tests to inspect BatchGenWorker._run_decoupled_gpu_decode after Slot 19 removed DecoupledGpuDecode.
Fix F401 unused import left after re-anchoring test_decode_round_obs_direct_params.py to BatchGenWorker._run_decoupled_gpu_decode.
Slot 18 changed BatchGenWorker.update_new_token to return CPU token_ids: List[int] so legacy decode avoids an extra per-token .item() host sync.
Update five remaining AST tests to inspect decode_engine.py alongside batchgen_worker.py after D8 extracted wave configuration, completion cleanup, and step replay guards.
Update _loop() in test_decode_deferred_token_readback.py to inspect BatchGenWorker._run_decoupled_gpu_decode after Slot 19 moved the composition root back to the worker.
Removes the unused legacy_past_kv_byte_size import left after extracting legacy decode helpers to decode_engine.py.
…egacy-helpers-params

# Conflicts:
#	batchgen/batchgen_worker.py
Update KNOWN_MODEL_BRANCH_SPANS after extracting decode forward-prep helpers.
Update KNOWN_MODEL_BRANCH_SPANS after extracting execute_decode_step_forward_and_sample.
Extract resolve_decode_step_rank_batch_size into decode_engine.py so batchgen_worker.py model-branch span stays at 95 and decode_engine.py shrinks to 104.
Use lowercase tuple return annotation in resolve_decode_step_rank_batch_size to satisfy F821 lint check.
@Andrewxu313
Andrewxu313 changed the base branch from tairan/decode-legacy-helpers-params to main December 4, 2026 04:56
…ispatch-params

# Conflicts:
#	batchgen/worker/decode_engine.py
#	tests/test_decode_exit_pending_load_integration.py
#	tests/test_decode_step_timing_wiring.py
#	tests/unit/test_decode_engine_empty_rank_names.py
#	tests/worker/test_boundary_decisions_direct_params.py
#	tests/worker/test_boundary_follow_up_direct_params.py
#	tests/worker/test_decode_forward_prep_direct_params.py
#	tests/worker/test_decode_forward_sample_direct_params.py
#	tests/worker/test_worker_model_branches.py
@Andrewxu313
Andrewxu313 marked this pull request as ready for review December 4, 2026 04:57
@Andrewxu313
Andrewxu313 merged commit 0944660 into main Dec 4, 2026
1 check passed
@Andrewxu313
Andrewxu313 deleted the tairan/decode-round-dispatch-params branch December 4, 2026 04:59
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.

1 participant