refactor(pytorch): derive CUDA step metadata from selected operators - #4805
Open
grimoire wants to merge 3 commits into
Open
refactor(pytorch): derive CUDA step metadata from selected operators#4805grimoire wants to merge 3 commits into
grimoire wants to merge 3 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors CUDA step-metadata preparation in the PyTorch backend so metadata is derived from the CUDA operators actually instantiated during model construction, enabling grouped (per-configuration) metadata and better alignment with CUDA graph capture requirements while retaining a legacy, model-config-driven fallback path.
Changes:
- Introduces a model-owned
CudaStepMetaPlan(resolved during model build) and runner-owned graph buffers to build/fill grouped attention metadata and per-step updaters. - Moves FlashMLA and FA3 metadata construction into operator-owned builder/update helpers and routes CUDAGraph capture/warmup through the resolved plan when supported.
- Adds a backend
model_build_contextscope used during model construction to collect implementations and attach the resolved plan (no-op for non-CUDA backends).
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| lmdeploy/pytorch/models/utils/cudagraph.py | Threads an optional CudaStepMetaPlan through cudagraph buffer allocation/fill, with compatibility fallbacks for legacy paths. |
| lmdeploy/pytorch/models/patch.py | Wraps model build in backend model_build_context and passes an explicit StepContextManager into model construction. |
| lmdeploy/pytorch/model_inputs.py | Adds storage on StepContextManager for backend-resolved step-metadata plans. |
| lmdeploy/pytorch/backends/cuda/step_metadata.py | Adds the core step-metadata planning, grouped builder contracts, and build-time collection mechanism. |
| lmdeploy/pytorch/backends/cuda/op_backend.py | Switches step-context preparation to use CudaSequenceMetadata + implementation-derived plans, retaining a legacy fallback. |
| lmdeploy/pytorch/backends/cuda/graph_runner.py | Wires the resolved step-metadata plan into cudagraph capture and pre-capture lifecycle hooks. |
| lmdeploy/pytorch/backends/cuda/gated_delta_rule.py | Refactors gated-delta prefill prep into a reusable helper and integrates it as a step-updater provider. |
| lmdeploy/pytorch/backends/cuda/attention/mla.py | Adds FlashMLA typed metadata + builder contract, including cudagraph buffer handling and pre-capture behavior. |
| lmdeploy/pytorch/backends/cuda/attention/fa3.py | Adds FA3 typed metadata + builder contract and consolidates scheduler-metadata construction/update helpers. |
| lmdeploy/pytorch/backends/cuda/attention/default.py | Adds shared sequence-to-attention metadata projection and default attention builder/registration hooks. |
| lmdeploy/pytorch/backends/base.py | Adds a default no-op model_build_context for backend-specific model-build scoping. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Motivation
CUDA step metadata was previously prepared mainly from model configuration flags in
update_step_context. This can diverge from the operators actually selected by backend dispatch, especially when:Changes
CudaStepMetaPlanwithout traversing the completed model.Compatibility