Skip to content

refactor(worker): extract decode wave helpers with explicit params - #890

Merged
Andrewxu313 merged 31 commits into
mainfrom
tairan/decode-wave-helpers-params
Dec 4, 2026
Merged

Andrewxu313 merged 31 commits into
mainfrom
tairan/decode-wave-helpers-params

Conversation

@Andrewxu313

@Andrewxu313 Andrewxu313 commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Description

Extract the four decode-wave lifecycle stages in BatchGenWorker.generate() (D8 Slots 13–16) into keyword-only explicit-parameter helpers in batchgen/worker/decode_engine.py:

  • prepare_decode_wave_model_and_kv (Slot 13)
  • select_and_sync_decode_wave_batch (Slot 14)
  • configure_decode_wave_local_rows_and_graphs + DecodeWaveConfigOutcome (Slot 15)
  • finalize_decode_wave_interval + DecodeWaveExitOutcome (Slot 16)

BatchGenWorker.generate() retains _enter_decode_phase_context() / try...finally: _exit_decode_phase_context(), _cohort_shutdown_observed early exit, and _decode_group_idx += 1.

Motivation

Continues the D8 decode-engine decoupling sequence so decode-wave logic takes explicit parameters and callbacks with zero worker object reference.

Type of Change

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

File changes

File Δ Note
batchgen/batchgen_worker.py mod Delegate decode-wave prepare, batch sync, row/graph configure, and interval finalize to decode_engine helpers
batchgen/worker/decode_engine.py mod Add prepare_decode_wave_model_and_kv, select_and_sync_decode_wave_batch, configure_decode_wave_local_rows_and_graphs, and finalize_decode_wave_interval
tests/test_worker_pool_restart.py mod Re-anchor AST teardown-ordering checks to the extracted decode-wave helpers
tests/unit/test_decode_group_binding_invariant.py mod Re-anchor decode-entry binding invariant check to configure_decode_wave_local_rows_and_graphs
tests/unit/test_decode_phase_context_once.py mod Verify _enter_decode_phase_context / _exit_decode_phase_context bracket the wave helpers
tests/unit/test_prefix_drain_metric.py mod Re-anchor decode-wave completion ordering check to select_and_sync_decode_wave_batch
tests/worker/test_decode_wave_helpers_direct_params.py add Direct-parameter unit tests for all four decode-wave helpers
tests/worker/test_sequence_table.py mod Re-anchor decode-wave KV release ordering check to select_and_sync_decode_wave_batch
tests/worker/test_worker_capturing_closures.py mod Remove eliminated generate decode_uuids.sort lambda from ALLOWED

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.
Update KNOWN_MODEL_BRANCH_SPANS after extracting decode forward-prep helpers.
Update KNOWN_MODEL_BRANCH_SPANS after extracting execute_decode_step_forward_and_sample.
@Andrewxu313
Andrewxu313 changed the base branch from tairan/decode-readback-params to main December 4, 2026 04:51
…lpers-params

# Conflicts:
#	batchgen/worker/decode_engine.py
@Andrewxu313
Andrewxu313 marked this pull request as ready for review December 4, 2026 04:52
@Andrewxu313
Andrewxu313 merged commit 8191270 into main Dec 4, 2026
1 check passed
@Andrewxu313
Andrewxu313 deleted the tairan/decode-wave-helpers-params branch December 4, 2026 04:53
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