Repository navigation
refactor(worker): unify decode CUDA-graph replay via adapter - #896
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.pyandbatchgen/worker/decode_engine.pyintoGlm5CudaGraphAdapterand model-agnostic helpers inbatchgen/config/cuda_graph_flag_policy.py.Deliberate interface pin updates (
tests/contracts/test_pin_cuda_graph.py):BatchState: added optionalattention_mask,graph_required, anddebug_valuesfields so adapters evaluate eligibility and debug options purely fromBatchState.GraphDecision: added optionalspan_exceededtuple 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) sobatchgen_worker.pyanddecode_engine.pyhave zero model-family branches.Motivation
After Phase E2-b unified decode graph capture behind
ModelCudaGraphAdapter, the decode replay loop (decode_engine.py),GraphRuntime, andbatchgen_worker.pystill carried model-specific GLM-5/K2.5 fields, helper methods, and fallback branches. Moving all remaining model-specific decode graph state and replay logic behindModelCudaGraphAdapterdrivesKNOWN_MODEL_BRANCH_SPANSintests/worker/test_worker_model_branches.pyto 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 / Dockerdocs— documentation onlyFile changes
batchgen/batchgen_worker.pybatchgen/config/cuda_graph_flag_policy.pyany_cuda_graph_requested,whole_model_cuda_graph_requested, andshould_warmup_decode_graphs_before_decodehelpersbatchgen/cuda_graph/adapter.pyBatchState,GraphDecision(span_exceeded), andModelCudaGraphAdapterlifecycle hooksbatchgen/models/glm/glm5/cuda_graph_adapter.pynum_valid_tokensbuffer, cached debug options, admission validation, and unified eligibilitybatchgen/models/moonshotai/kimi_k25/cuda_graph_adapter.pyk25_whole_model_graphruntime flag and emitspan_exceededon capture-span overrunbatchgen/worker/decode_engine.pyModelCudaGraphAdapterpath and updateMODULE_CONTRACTbatchgen/worker/graph_runtime.pycapture_reuse_snapshotfromGraphRuntimebatchgen/worker/worker_lifecycle_engine.pyrelease_decode_graph_stateand updateMODULE_CONTRACTtests/contracts/test_pin_cuda_graph.pyBatchState,GraphDecision, andModelCudaGraphAdapterinterface pins for Phase E2-ctests/test_glm5_decode_utils.pyGlm5CudaGraphAdapterand model-agnostic worker helperstests/test_glm5_graph_context_span.pyGlm5CudaGraphAdapter.prepare_capturetests/test_glm5_persistent_phase.pyGraphRuntimefield referencetests/test_glm5_worker_fa3_wiring.pyGlm5CudaGraphAdaptertests/test_kimi_k3_server_startup_init.py_k3_startup_completedto_pre_readiness_startup_completedtests/test_worker_weight_passes.pytests/worker/test_admission_module_contracts.pysibling_surfacestests/worker/test_decode_forward_prep_direct_params.pytests/worker/test_decode_forward_sample_direct_params.pytests/worker/test_decode_width_bucket_invariant.pyadapter.eligibilityindecode_engine.pytests/worker/test_e2c_adapter_replay.pynum_valid_tokens,all_shortadmission,span_exceededvsfail_on_eager, debug caching, and unified replaytests/worker/test_glm5_criticality_probe_worker.pytests/worker/test_glm5_debug_keys.pyGlm5CudaGraphAdaptertests/worker/test_graph_capture_oom_outcome.py_failed_bucketsand_release_stale_whole_model_graph_statetests/worker/test_graph_runtime.pyGraphRuntimefield inventory, helper list, and adapter-owned failed-bucket teststests/worker/test_graph_setup_context.pytests/worker/test_startup_decode_graph_capture.pytests/worker/test_worker_capturing_closures.pyALLOWEDcapturing closures list after removing legacy closurestests/worker/test_worker_lifecycle_engine.pytests/worker/test_worker_model_branches.pyKNOWN_MODEL_BRANCH_SPANSto empty (0 remaining model-family branches)Checklist
README.mdchanged, I ranpython3 scripts/check_readme_update.py --base-ref origin/mainand preserved all News history.