Skip to content

[TRTLLM-14054][fix] LMHead: pass true full dims to Linear for quantized TP>1 - #16352

Merged
QiJune merged 1 commit into
NVIDIA:mainfrom
kaiyux:fix/lmhead-quant-tp-dims-20260713
Jul 14, 2026
Merged

[TRTLLM-14054][fix] LMHead: pass true full dims to Linear for quantized TP>1#16352
QiJune merged 1 commit into
NVIDIA:mainfrom
kaiyux:fix/lmhead-quant-tp-dims-20260713

Conversation

@kaiyux

@kaiyux kaiyux commented Jul 14, 2026

Copy link
Copy Markdown
Member

Description

LMHead.__init__ passed local_{in,out}_features * tp_size for both dims to Linear.__init__, but Linear only re-shards the dim selected by tensor_parallel_mode — the unsharded dim arrived tp_size-× inflated.

The dense path never hit this because LMHead re-creates a dense weight from corrected local dims after super().__init__. The quantized path, however, sizes its (packed) weight and scales inside Linear.__init__ via quant_method.create_weights, so it saw the inflated dim: under COLUMN (vocab-parallel) TP2, the NVFP4-quantized lm_head of Qwen3.6-35B-A3B-NVFP4 got its packed weight allocated as [vocab/2, hidden] instead of [vocab/2, hidden/2], and weight load failed on every rank:

RuntimeError: The size of tensor a (2048) must match the size of tensor b (1024) at non-singleton dimension 1

Fix: pass local * tp_size only on the sharded dim (it carries the ceil-div vocab/hidden padding); pass the true full size on the other dim.

This unblocks TP>1 for any model with a quantized lm_head (any quant algo whose weights are created in Linear.__init__); it was hit while bringing up Qwen3.6-35B-A3B-NVFP4 under TP2 for the perf work in #16313 / #16314, and the TP2 measurements in #16353 were taken with this fix applied.

Test Coverage

  • Exercised end-to-end on Qwen3.6-35B-A3B-NVFP4 (NVFP4 lm_head, vocab-parallel): TP2 previously failed weight load on both ranks with the RuntimeError above; with the fix the server loads and serves, and both ranks' lm_head shards were verified to hold the correct packed shape and calibrated scales ([124160, 1024] packed + 124160×128 scales per rank).
  • TP1 allocation is unchanged by construction (both selectors reduce to the same full dims): packed [248320, 1024] + 248320×128 scales, byte-identical to before the fix.
  • Dense LMHead TP paths are covered by the existing tests/unittest/_torch/multi_gpu/test_embedding.py and are unaffected (the dense weight is re-created from local dims after super().__init__).

PR Checklist

  • 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.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved language model output layer handling for tensor-parallel configurations.
    • Fixed weight shape and padding behavior for sharded, packed, and quantized models.
    • Enhanced compatibility and reliability when loading or running models with different parallelization settings.

LMHead.__init__ passed local_{in,out}_features * tp_size for BOTH dims
to Linear.__init__, but Linear only re-shards the dim selected by
tensor_parallel_mode - the unsharded dim arrived tp_size-x inflated.
Under COLUMN (vocab-parallel) TP2 the NVFP4-quantized lm_head of
Qwen3.6-35B-A3B-NVFP4 got its packed weight allocated as
[vocab/2, hidden] instead of [vocab/2, hidden/2], so weight load
failed on every rank:

  RuntimeError: The size of tensor a (2048) must match the size of
  tensor b (1024) at non-singleton dimension 1

The dense path never hit this because LMHead re-creates a dense weight
from corrected local dims after super().__init__; the quantized path
sizes its (packed) weight and scales inside Linear.__init__ via
quant_method.create_weights, so it saw the inflated dim.

Fix: pass local*tp only on the sharded dim (it carries the ceil-div
vocab/hidden padding); pass the true full size on the other dim. TP1
shapes are unchanged; quantized TP2 now allocates the correct per-rank
shard ([124160, 1024] packed weight + 124160x128 scales per rank for
the model above) and weight load succeeds.

Signed-off-by: Kaiyu Xie <26294424+kaiyux@users.noreply.github.com>
@kaiyux

kaiyux commented Jul 14, 2026

Copy link
Copy Markdown
Member Author

/bot run --disable-fail-fast

@coderabbitai

coderabbitai Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

LMHead.__init__ now computes tensor-parallel-aware full feature sizes and passes them to Linear, preserving full sizing for non-sharded dimensions.

Changes

LMHead sizing

Layer / File(s) Summary
Tensor-parallel feature sizing
tensorrt_llm/_torch/modules/embedding.py
LMHead computes full input and output feature sizes based on tensor_parallel_mode, applying padded sizing only to the re-sharded dimension before calling Linear.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Suggested reviewers: nv-guomingz

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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 matches the main change and follows the required [ticket][type] summary pattern.
Description check ✅ Passed The description includes Description, Test Coverage, and PR Checklist, and it explains the issue, fix, and validation clearly.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

@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.

🧹 Nitpick comments (1)
tensorrt_llm/_torch/modules/embedding.py (1)

61-73: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add regression coverage for tensor-parallel feature sizing.

The calculation matches Linear’s split-dimension contract, but this fix should include tests that verify quantized COLUMN TP2 allocations use the true full hidden dimension and padded vocabulary dimension, while TP1 and dense LMHead shapes remain unchanged.

🤖 Prompt for 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.

In `@tensorrt_llm/_torch/modules/embedding.py` around lines 61 - 73, The updated
sizing logic in the embedding module lacks regression coverage. Add tests for
quantized LMHead/embedding construction verifying COLUMN tensor parallelism at
TP2 uses the true full hidden dimension and padded vocabulary size, while TP1
and dense LMHead cases retain their existing shapes.
🤖 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.

Nitpick comments:
In `@tensorrt_llm/_torch/modules/embedding.py`:
- Around line 61-73: The updated sizing logic in the embedding module lacks
regression coverage. Add tests for quantized LMHead/embedding construction
verifying COLUMN tensor parallelism at TP2 uses the true full hidden dimension
and padded vocabulary size, while TP1 and dense LMHead cases retain their
existing shapes.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: c27d79cc-cf65-4c19-b6b6-614f3d0f07df

📥 Commits

Reviewing files that changed from the base of the PR and between 7d7c364 and c82386a.

📒 Files selected for processing (1)
  • tensorrt_llm/_torch/modules/embedding.py

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59093 [ run ] triggered by Bot. Commit: c82386a Link to invocation

@kaiyux kaiyux changed the title [None][fix] LMHead: pass true full dims to Linear for quantized TP>1 [TRTLLM-14054][fix] LMHead: pass true full dims to Linear for quantized TP>1 Jul 14, 2026
@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59093 [ run ] completed with state SUCCESS. Commit: c82386a
/LLM/main/L0_MergeRequest_PR pipeline #47608 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

@kaiyux

kaiyux commented Jul 14, 2026

Copy link
Copy Markdown
Member Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59143 [ run ] triggered by Bot. Commit: c82386a Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59143 [ run ] completed with state SUCCESS. Commit: c82386a
/LLM/main/L0_MergeRequest_PR pipeline #47652 completed with status: 'SUCCESS'

CI Report

Link to invocation

@QiJune QiJune 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

@QiJune
QiJune merged commit 5e16fdb into NVIDIA:main Jul 14, 2026
17 checks passed
@kaiyux
kaiyux deleted the fix/lmhead-quant-tp-dims-20260713 branch July 14, 2026 09:54
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.

3 participants