Skip to content

refactor(worker): unify decode CUDA-graph replay via adapter - #896

Merged
Andrewxu313 merged 3 commits into
mainfrom
tairan/r4b-e2c-decode-replay
Dec 4, 2026
Merged

Andrewxu313 merged 3 commits into
mainfrom
tairan/r4b-e2c-decode-replay

Conversation

@Andrewxu313

@Andrewxu313 Andrewxu313 commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Description

Migrate the remaining GLM-5 decode CUDA-graph replay, batch admission validation, eager-vs-graph comparison, and graph bookkeeping out of batchgen/batchgen_worker.py and batchgen/worker/decode_engine.py into Glm5CudaGraphAdapter and model-agnostic helpers in batchgen/config/cuda_graph_flag_policy.py.

Deliberate interface pin updates (tests/contracts/test_pin_cuda_graph.py):

  • BatchState: added optional attention_mask, graph_required, and debug_values fields so adapters evaluate eligibility and debug options purely from BatchState.
  • GraphDecision: added optional span_exceeded tuple so long-context span overruns trigger the rate-limited span warning instead of a hard fail-loud raise.
  • ModelCudaGraphAdapter: added default-no-op lifecycle hooks (validate_decode_batch_admission, needs_capture_for_batch, on_decode_step_pre_forward, decode_eager_forward) so batchgen_worker.py and decode_engine.py have zero model-family branches.

Motivation

After Phase E2-b unified decode graph capture behind ModelCudaGraphAdapter, the decode replay loop (decode_engine.py), GraphRuntime, and batchgen_worker.py still carried model-specific GLM-5/K2.5 fields, helper methods, and fallback branches. Moving all remaining model-specific decode graph state and replay logic behind ModelCudaGraphAdapter drives KNOWN_MODEL_BRANCH_SPANS in tests/worker/test_worker_model_branches.py to 0 and keeps steady-state decode replay free of host-device synchronization.

Type of Change

  • model — add/extend model support (models/** + registration seam + model kernels only)
  • kernel — add/optimize a compute kernel (batchgen_kernels/** + in-tree kernel dirs)
  • core — change scheduling/serving/runtime scaffolding (the only type that may)
  • fix — narrow bug fix (+ a regression test)
  • infra — build / CI / packaging / scripts / Docker
  • docs — documentation only

File changes

File Δ Note
batchgen/batchgen_worker.py mod Delete legacy GLM-5 replay/comparison methods; delegate decode graph admission, warmup, and forward to adapter/policy seams
batchgen/config/cuda_graph_flag_policy.py mod Add model-agnostic any_cuda_graph_requested, whole_model_cuda_graph_requested, and should_warmup_decode_graphs_before_decode helpers
batchgen/cuda_graph/adapter.py mod Extend BatchState, GraphDecision (span_exceeded), and ModelCudaGraphAdapter lifecycle hooks
batchgen/models/glm/glm5/cuda_graph_adapter.py mod Own GLM-5 capture/replay state, pre-allocated num_valid_tokens buffer, cached debug options, admission validation, and unified eligibility
batchgen/models/moonshotai/kimi_k25/cuda_graph_adapter.py mod Drop retired k25_whole_model_graph runtime flag and emit span_exceeded on capture-span overrun
batchgen/worker/decode_engine.py mod Collapse decode replay to the single ModelCudaGraphAdapter path and update MODULE_CONTRACT
batchgen/worker/graph_runtime.py mod Remove 10 retired model-specific fields and capture_reuse_snapshot from GraphRuntime
batchgen/worker/worker_lifecycle_engine.py mod Remove retired model-specific field resets from release_decode_graph_state and update MODULE_CONTRACT
tests/contracts/test_pin_cuda_graph.py mod Update BatchState, GraphDecision, and ModelCudaGraphAdapter interface pins for Phase E2-c
tests/test_glm5_decode_utils.py mod Update unit tests to exercise Glm5CudaGraphAdapter and model-agnostic worker helpers
tests/test_glm5_graph_context_span.py mod Point capture-span AST checks at Glm5CudaGraphAdapter.prepare_capture
tests/test_glm5_persistent_phase.py mod Update retired GraphRuntime field reference
tests/test_glm5_worker_fa3_wiring.py mod Point FA3/all-short wiring checks at Glm5CudaGraphAdapter
tests/test_kimi_k3_server_startup_init.py mod Rename _k3_startup_completed to _pre_readiness_startup_completed
tests/test_worker_weight_passes.py mod Update unstreamed-forward AST allowlist after deleting legacy GLM-5 forward helper
tests/worker/test_admission_module_contracts.py mod Remove retired model-specific fields from sibling_surfaces
tests/worker/test_decode_forward_prep_direct_params.py mod Update forward-prep direct-parameter assertions for adapter pre-forward hook
tests/worker/test_decode_forward_sample_direct_params.py mod Update forward-sample direct-parameter assertions for unified adapter replay
tests/worker/test_decode_width_bucket_invariant.py mod Check bucket assertion precedes adapter.eligibility in decode_engine.py
tests/worker/test_e2c_adapter_replay.py add Add unit tests for pre-allocated num_valid_tokens, all_short admission, span_exceeded vs fail_on_eager, debug caching, and unified replay
tests/worker/test_glm5_criticality_probe_worker.py mod Update criticality-probe test fixtures for adapter-owned state
tests/worker/test_glm5_debug_keys.py mod Test GLM-5 debug key parsing through Glm5CudaGraphAdapter
tests/worker/test_graph_capture_oom_outcome.py mod Assert OOM bucket quarantine on adapter _failed_buckets and _release_stale_whole_model_graph_state
tests/worker/test_graph_runtime.py mod Update GraphRuntime field inventory, helper list, and adapter-owned failed-bucket tests
tests/worker/test_graph_setup_context.py mod Update setup-context fixtures for adapter-owned capture state
tests/worker/test_startup_decode_graph_capture.py mod Update startup capture fixtures for model-agnostic policy helpers and adapter state
tests/worker/test_worker_capturing_closures.py mod Shrink ALLOWED capturing closures list after removing legacy closures
tests/worker/test_worker_lifecycle_engine.py mod Remove retired model-specific fields from lifecycle release assertions
tests/worker/test_worker_model_branches.py mod Set KNOWN_MODEL_BRANCH_SPANS to empty (0 remaining model-family branches)

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.

Migrate remaining GLM-5 decode replay, admission validation,
eager-vs-graph comparison, and graph bookkeeping out of
batchgen_worker.py and decode_engine.py into Glm5CudaGraphAdapter
and model-agnostic cuda_graph_flag_policy helpers.

Key changes:
- Extend ModelCudaGraphAdapter with model-agnostic decode lifecycle
  hooks (validate_decode_batch_admission, needs_capture_for_batch,
  on_decode_step_pre_forward, decode_eager_forward) and GraphDecision
  span_exceeded / fail_on_eager semantics.
- Move GLM-5 capture/replay state (_all_short, _index_topk,
  _table_sig, _whole_model_active, _failed_buckets, cached debug
  options, and pre-allocated GPU num_valid_tokens buffer) onto
  Glm5CudaGraphAdapter.
- Remove retired model-specific fields from GraphRuntime and
  worker_lifecycle_engine.py, collapsing decode_engine.py to a single
  adapter-driven replay path with zero decode hot-path host syncs.
- Drive KNOWN_MODEL_BRANCH_SPANS in test_worker_model_branches.py to 0.
Remove unused imports and local variable flagged by pr-lint-gate.
Pin the added optional BatchState/GraphDecision fields and default-no-op
ModelCudaGraphAdapter decode-lifecycle hooks, and preserve the existing
test_glm5_deep_free_resets_segmented_graph_capture_attempts test ID.
@Andrewxu313
Andrewxu313 merged commit b2dd05d into main Dec 4, 2026
1 check passed
@Andrewxu313
Andrewxu313 deleted the tairan/r4b-e2c-decode-replay branch December 4, 2026 12:09
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