Skip to content

[TRTLLM-13077][feat] Decompose post_load_weights()#14770

Merged
pcastonguay merged 2 commits into
NVIDIA:mainfrom
chienchunhung:staged-post-load-hooks-prep-clean
Jun 3, 2026
Merged

[TRTLLM-13077][feat] Decompose post_load_weights()#14770
pcastonguay merged 2 commits into
NVIDIA:mainfrom
chienchunhung:staged-post-load-hooks-prep-clean

Conversation

@chienchunhung

@chienchunhung chienchunhung commented May 30, 2026

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

  • Refactor

    • Enhanced model loading with staged weight transformation and improved module traversal for optimized resource handling.
    • Streamlined post-load weight processing with better hook coordination.
  • Tests

    • Added tests for staged hook behavior, weight transformation sequences, and model lifecycle event ordering.

Summary

Add the prep layer for staged post-load hooks without migrating existing model implementations yet.

  • Add default setup_aliases(), transform_weights(), and cache_derived_state() hooks on DecoderModelForCausalLM, with post_load_weights() preserving the current full sequence.
  • Add duck-typed ModelLoader helpers for top-level alias setup, transform walks, cache-state walks, full post-load walks, and _weights_transformed reset.
  • Keep reload() behavior unchanged; reload remains weight-copy-only and does not run post-load hooks.
  • Add focused mock tests for staged-hook helper behavior.

Test Plan

  • python3 -m pytest tests/unittest/_torch/pyexecutor/test_model_loader_mx.py -q

Note: local run was blocked in this environment by missing built tensorrt_llm.bindings; should be run in the normal TRT-LLM build/test environment.

Next steps

This prep PR adds the contract surface only — no model is migrated. The follow-up rollout is sequenced as four waves:

  • Wave 1 — Alias migration + GMS-RO cutover. Move alias wiring on seven model classes (Llama, DeepSeek V3, GLM, Exaone-MoE, Qwen3-MoE, Qwen3-Next, GPT-OSS) from post_load_weights() to setup_aliases(), and cut over the GMS RO branch in model_loader.py from the meta-tensor workaround to the staged-hook protocol. LOW risk; the immediate next PR.
  • Wave 2 — Linear / Attention transform migration. Move transform bodies in modules/linear.py and modules/attention.py to transform_weights() with the _weights_transformed guard. HIGH risk: touches every model.
  • Wave 3 — MoE + Mamba transform migration. Same migration for fused_moe/quantization.py (six quant variants), fused_moe/fused_moe_triton.py, fused_moe/configurable_moe.py, mamba/mamba2_mixer.py, models/modeling_llama_min_latency.py, and attention_backend/sparse/dsa.py. HIGH risk.
  • Wave 4 — MX publish-after-transform flip + receiver-side fingerprint check. Flip the MX publisher from publish-PRE to publish-POST-transform; receiver-side fingerprint check (covering attn_backend, quant backend list, FP8 / NVFP4 fusion strategy, TP / PP / EP layout, model revision) raises on mismatch rather than silently consuming incompatible weights; per-model allow-list for incremental rollout. MEDIUM risk; gated on Waves 2 and 3.

Each wave will be a standalone, independently revertable PR.

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-compatible or api-breaking. For api-breaking, include BREAKING in 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.

@chienchunhung

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #51131 [ run ] triggered by Bot. Commit: d93df17 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #51131 [ run ] completed with state SUCCESS. Commit: d93df17
/LLM/main/L0_MergeRequest_PR pipeline #40568 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

Signed-off-by: Chien-Chun Hung <2679986+chienchunhung@users.noreply.github.com>
@chienchunhung
chienchunhung force-pushed the staged-post-load-hooks-prep-clean branch from d93df17 to b235f18 Compare May 30, 2026 16:21
@chienchunhung

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #51197 [ run ] triggered by Bot. Commit: b235f18 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #51197 [ run ] completed with state FAILURE. Commit: b235f18
/LLM/main/L0_MergeRequest_PR pipeline #40625 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@chienchunhung

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #51227 [ run ] triggered by Bot. Commit: b235f18 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #51227 [ run ] completed with state SUCCESS. Commit: b235f18
/LLM/main/L0_MergeRequest_PR pipeline #40650 completed with status: 'SUCCESS'

CI Report

Link to invocation

@chienchunhung
chienchunhung marked this pull request as ready for review June 1, 2026 19:31
@chienchunhung
chienchunhung requested review from a team as code owners June 1, 2026 19:31
@coderabbitai

coderabbitai Bot commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

This PR introduces a staged post-load hook lifecycle for model loading. DecoderModel now defines four methods—setup_aliases, transform_weights, cache_derived_state, and post_load_weights—where the latter orchestrates the three stages in sequence. ModelLoader adds static helpers to traverse modules and conditionally invoke these hooks, with the GMS load path refactored to use the new centralized orchestration. Tests verify hook ordering, flag-based module skipping, and lifecycle integration.

Changes

Staged post-load hook lifecycle

Layer / File(s) Summary
DecoderModel staged hooks contract
tensorrt_llm/_torch/models/modeling_utils.py
DecoderModel adds four new methods: no-op setup_aliases(), transform_weights(), and cache_derived_state() stubs, plus post_load_weights() that orchestrates them in sequence.
ModelLoader hook traversal helpers and load integration
tensorrt_llm/_torch/pyexecutor/model_loader.py
Five new static helpers (_setup_aliases, _walk_transform, _walk_cache_state, _walk_full_post_load, _reset_weights_transformed) walk eligible modules and invoke hooks while respecting _weights_removed and _weights_transformed flags. GMS load path is updated to use _walk_full_post_load() instead of inline module iteration. reload() method signature updated to declare return type None.
Hook lifecycle tests and assertions
tests/unittest/_torch/pyexecutor/test_model_loader_mx.py
Test helpers record per-module hook invocations. New tests verify alias setup runs only at top level, that hook walking order is correct while skipping removed/transformed modules, and that _reset_weights_transformed clears flags only on existing modules. MX success test assertion updated to verify both post_load_weights and load_weights events.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 44.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title '[TRTLLM-13077][feat] Decompose post_load_weights()' clearly and specifically describes the main change: introducing staged post-load hook decomposition as a feature.
Description check ✅ Passed The PR description fully covers required template sections: Summary explains the staged hooks prep work, Test Plan provides specific pytest command, Next steps details the phased rollout plan, and PR Checklist is completed with confirmation.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/unittest/_torch/pyexecutor/test_model_loader_mx.py`:
- Around line 160-187: The __init__ of _HookRecorder currently always creates
_weights_transformed (and similarly _weights_removed) even when False; change
_HookRecorder.__init__ to only set those attributes when the corresponding flag
is True (e.g., if transformed: self._weights_transformed = True and if removed:
self._weights_removed = True) so modules that shouldn't have the attribute won't
have it and the test test_reset_weights_transformed_only_resets_existing_flags
will pass.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 2268b343-c928-4ce0-9f24-64c2bd09ef48

📥 Commits

Reviewing files that changed from the base of the PR and between f20858c and b235f18.

📒 Files selected for processing (3)
  • tensorrt_llm/_torch/models/modeling_utils.py
  • tensorrt_llm/_torch/pyexecutor/model_loader.py
  • tests/unittest/_torch/pyexecutor/test_model_loader_mx.py

Comment thread tests/unittest/_torch/pyexecutor/test_model_loader_mx.py
chienchunhung added a commit to chienchunhung/TensorRT-LLM that referenced this pull request Jun 1, 2026
Sync the in-tree design doc with the standalone version kept offline
(2026-05-31 last update). Substantive additions / corrections:

- Foundation references section: split into merged (TRTLLM-11851, -12440,
  end-to-end prototype) and inflight (TRTLLM-13077 prep PR, MX-team
  Delegate-to-ModelExpress refactor proposal) sub-sections.
- Audience and intent section: name the two intended readers (MX/GMS
  upstream engineers + TRT-LLM code owners) and what each needs to take
  away.
- P1 precondition reframed: source-identity matching is fundamentally a
  TRT-LLM concern (only TRT-LLM knows which knobs affect layout), so the
  API surface lives in TRT-LLM as `tllm.disagg.compute_source_identity()`
  / `is_source_compatible()` and is consumed by both MX and GMS.  The
  opaque-bytes design protects transport libraries from churn when
  TRT-LLM adds new layout-affecting parameters.
- Wave 4 scope updated to land the TRT-LLM source-identity API (~80 LOC)
  as a foundational sub-step, used by both MX and GMS receiver paths.
- New "Coordination with MX and GMS" section:
  - Source-identity API directionality (TRT-LLM-owned, MX/GMS-consumed).
  - Scope of MX checkpoint loading: discusses the wholesale vs.
    transport-only delegation question raised by the MX-team refactor
    proposal, with TRT-LLM advocating transport-only (fallback /
    validation / telemetry concerns are TRT-LLM-internal and parallel
    the NIXL/UCX division-of-labor model in the disagg KV-cache
    transceiver).
  - What this asks of MX vs. what this asks of GMS as separate bullets.
- Status / Created / Last-updated header brought to 2026-05-31.

All PR-number cross-references (NVIDIA#13531, NVIDIA#13926, NVIDIA#13045, NVIDIA#14770, NVIDIA#14151)
deliberately omitted to keep this docs-and-plans branch from triggering
back-references on the public NVIDIA/TensorRT-LLM PRs; cited via JIRA
tickets and descriptive titles instead.  Only external cross-ref
retained is ai-dynamo/dynamo PR NVIDIA#7053 (different repo/org).

Signed-off-by: Chien-Chun Hung <2679986+chienchunhung@users.noreply.github.com>
…eck stage

Signed-off-by: Chien-Chun Hung <2679986+chienchunhung@users.noreply.github.com>
@chienchunhung chienchunhung changed the title [TRTLLM-13077][feat] Deocmpose post_load_weights() [TRTLLM-13077][feat] Decompose post_load_weights() Jun 2, 2026
@chienchunhung

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #51491 [ run ] triggered by Bot. Commit: 2b521ee Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #51491 [ run ] completed with state SUCCESS. Commit: 2b521ee
/LLM/main/L0_MergeRequest_PR pipeline #40897 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

Link to invocation

@chienchunhung

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #51630 [ run ] triggered by Bot. Commit: 2b521ee Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #51630 [ run ] completed with state FAILURE. Commit: 2b521ee
/LLM/main/L0_MergeRequest_PR pipeline #41015 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@brb-nv brb-nv left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comments.

Comment thread tensorrt_llm/_torch/pyexecutor/model_loader.py
Comment thread tensorrt_llm/_torch/pyexecutor/model_loader.py
Comment thread tensorrt_llm/_torch/pyexecutor/model_loader.py
@chienchunhung

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

1 similar comment
@chienchunhung

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #51725 [ run ] triggered by Bot. Commit: 2b521ee Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #51725 [ run ] completed with state SUCCESS. Commit: 2b521ee
/LLM/main/L0_MergeRequest_PR pipeline #41100 completed with status: 'SUCCESS'

CI Report

Link to invocation

@pcastonguay pcastonguay left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

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.

4 participants