Repository navigation
[TRTLLM-15764][refactor] Split PyTorchModelEngine construction into named phases - #20851
Conversation
d93cac7 to
b3d2f3f
Compare
…amed phases The phase bodies are the old __init__ body moved verbatim, cut where an irreversible fact is established. Signed-off-by: Lori Ren <[email protected]>
…raph.py function plan_cuda_graphs reads only its arguments, and the new _size_cuda_graphs phase writes its plan back to the same attributes. _compile_model keeps only the compile; the prefill capture filter moves next to the other graph filters. Signed-off-by: Lori Ren <[email protected]>
… after the first phase The later phases read llm_args, mapping and spec_config from the attributes the first phase stores them in, the same objects, and drop the duplicate parameters. Signed-off-by: Lori Ren <[email protected]>
b3d2f3f to
b5808b0
Compare
… is loaded Behavior change: the loader is built after the PP communicator, the expert statistics and the input processor instead of before them, which only shows if building it fails. _init_from_args no longer hands it over. Signed-off-by: Lori Ren <[email protected]>
…pile phase Behavior change in order only: the caller is built before the attention backend, the runner type and the speculation update instead of after them, and its constructor only stores what compilation produced. _compile_model no longer hands the auxiliary streams over. Signed-off-by: Lori Ren <[email protected]>
…onstruction phases cleanup() now completes after a construction failure in any phase; before, it stopped on a missing model_loader or _torch_compile_backend. Signed-off-by: Lori Ren <[email protected]>
Draft engines are no longer built, and stub models now come in through model= rather than an overridden _load_model. Signed-off-by: Lori Ren <[email protected]>
b5808b0 to
5b06f57
Compare
|
No actionable comments were generated in the recent review.
|
| Layer / File(s) | Summary |
|---|---|
Define and validate CUDA graph planning tensorrt_llm/_torch/pyexecutor/engine/cuda_graph.py, tests/unittest/llmapi/test_llm_args.py, .claude/skills/perf-regression-cookbook/references/cuda-graph-and-compile/piecewise-cudagraph-far-ceiling-append.md |
The module exports filter_prefill_capture_num_tokens, CudaGraphPlan, and plan_cuda_graphs. The filter handles candidate limits, reachable ceilings, clamping, sorting, and deduplication. Tests and the diagnostic command use the public filter. |
Integrate graph planning into engine initialization tensorrt_llm/_torch/pyexecutor/model_engine.py |
Initialization is split into setup stages. CUDA graph sizing uses plan_cuda_graphs. Compilation and execution-service setup are also separated. |
Priority:
Estimated code review effort: 3 (Moderate) | ~20 minutes
Change: Refactor
Suggested reviewers: schetlur-nv
Merge Risk: 
Minimal · up to 5b06f
-
The refactor preserves the reviewed CUDA graph behavior, with no identified issue requiring correction before merge.
Pre-merge checks: 
5
-
Full details: Docstring Coverage
-
Explanation
Docstring coverage is 85.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 20 functions across 3 files. (1 skipped: 1 unsupported.)
-
Finishing Touches
-
🧪 Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Comment @coderabbitai help to get the list of available commands.
|
/bot run |
|
PR_Github #80534 [ run ] triggered by Bot. Commit: |
bo-nv
left a comment
There was a problem hiding this comment.
From the disagg side this looks good. Everything the disagg path reads from the engine — capacity, CUDA graph batch sizes, spec widths, attention DP — keeps its value and is still only read after __init__ returns, and the reordered steps allocate no GPU memory, so KV sizing is unchanged. Nothing blocking from my side; I'd like to see blossom-ci #80534 pass on this head before merge.
|
PR_Github #80534 [ run ] completed with state
|
kaiyux
left a comment
There was a problem hiding this comment.
Approving on behalf of agent devs. Thx.
|
Automatically added "ci: full pre-merge approved" because this PR has satisfied the required GitHub review approvals. Unresolved review conversations and other required checks remain independent merge requirements. |
|
/bot run |
|
PR_Github #80597 [ run ] triggered by Bot. Commit: |
|
PR_Github #80597 [ run ] completed with state |
Dev Engineer Review
PyTorchModelEngine.__init__now delegates setup to named phases. The newplan_cuda_graphsfunction centralizes CUDA graph sizing and prefill capture filtering incuda_graph.py. Verify that engine attributes and graph settings remain consistent, and that failures in each phase leave state available tocleanup().Construction order changes affect loader, communicator, input processor, model caller, and execution-service initialization. The supplied evidence does not include test results to confirm the reported compatibility checks.
QA Engineer Review
The reported test changes cover
test_pytorch_model_engine.py,test_pytorch_model_engine_warmup.py, tests undertests/unittest/_torch/executor/engine/, andtest_llm_args.py. Thetest_llm_args.pychanges move piecewise CUDA-graph filter tests to the new public function. The test database includestest_pytorch_model_engine.py -k "test_spec_decode_graph_step_gather_kernel"inl0_b200.yml; listing status for the other reported tests is not established. Coverage verdict: needs follow-up.Per-File QA Perspective
tensorrt_llm/_torch/pyexecutor/model_engine.py: Verify the initialization phases preserve engine attributes and values, and that failures in each phase leave state available tocleanup().tensorrt_llm/_torch/pyexecutor/engine/cuda_graph.py: Verify defaults, candidate filtering, unreachable-size handling, and the empty-decode-batch case inplan_cuda_graphsandfilter_prefill_capture_num_tokens..claude/skills/perf-regression-cookbook/references/cuda-graph-and-compile/piecewise-cudagraph-far-ceiling-append.md: The cookbook command now imports the filter fromcuda_graph.py; verify the command matches the function’s API.tests/unittest/llmapi/test_llm_args.py: The piecewise CUDA-graph filter tests now import the relocated function. The supplied test-list result does not establish whether this file is listed.tests/unittest/_torch/executor/test_pytorch_model_engine.py: The objective reports engine-construction coverage. One test selector appears intest-db/l0_b200.yml; listing status for the other tests is not established.tests/unittest/_torch/executor/test_pytorch_model_engine_warmup.pyandtests/unittest/_torch/executor/engine/: The objective reports warmup and engine coverage. Specific changed tests and test-list status are not established.Description
PyTorchModelEngine.__init__becomes a short sequence of named construction phases, each ending at a fact the next one relies on: the requested sizes and argument-derived services, the loaded model, the model's facts copied onto the engine with capacity clamped to them, the CUDA graph sizes for that capacity, the compiled model, and the execution services built around it. The phase bodies are the old constructor body moved verbatim, except that the later phases readllm_args,mappingandspec_configfrom the attributes the first phase stores them in. The constructor signature, the attribute set and every attribute's value are unchanged.The CUDA graph sizing reads only its arguments, so it moves to
engine/cuda_graph.pyasplan_cuda_graphs, together with the prefill capture filter it uses (nowfilter_prefill_capture_num_tokens). The torch.compile step stays on the engine for now: it shares_apply_torch_compilewith the refit path.Three commits change behavior, each only during construction:
cleanup()now completes after a failure in any phase, because the state it reads is declared before the first phase. Before, it stopped with anAttributeErrorwhen construction failed early.With these two moves, the only value handed from one phase to a later one is the encoder token budget before any multimodal increase, which the compile phase needs after
_init_from_modelmay have raisedencoder_max_num_tokens. Construction is the only code touched; the forward path is unchanged.Test Coverage
tests/unittest/_torch/executor/test_pytorch_model_engine.py,tests/unittest/_torch/executor/test_pytorch_model_engine_warmup.pyandtests/unittest/_torch/executor/engine/.tests/unittest/llmapi/test_llm_args.pyfor the prefill capture filter, now imported fromengine/cuda_graph.py.During development, the engine's attributes after construction were compared against the unsplit constructor for decoder-only,
torch.compile, piecewise, prefill-only, speculation, encoder-decoder, encode-only, MM-encoder and pooling engines, with a failure injected into each phase and with TP=2 userbuffers. Everything matched apart from the failure-path change above. On the final base, AST checks show that the split is a verbatim move of the constructor body, that the sizing move and the reads fromselfchange nothing else, and that each reorder moves one statement unchanged.PR Checklist
Please review the following before submitting your PR:
PR description clearly explains what and why. If using CodeRabbit's summary, please make sure it makes sense.
PR Follows TRT-LLM CODING GUIDELINES to the best of your knowledge.
Test cases are provided for new code paths (see test instructions)
If PR introduces API changes, an appropriate PR label is added - either
api-compatibleorapi-breaking. Forapi-breaking, includeBREAKINGin the PR title.Any new dependencies have been scanned for license and vulnerabilities
CODEOWNERS updated if ownership changes
Documentation updated as needed
Update tava architecture diagram if there is a significant design change in PR.
The reviewers assigned automatically/manually are appropriate for the PR.
Please check this after reviewing the above items as appropriate for this PR.
GitHub Bot Help
To see a list of available CI bot commands, please comment
/bot help.