Skip to content

Conversation

tomeras91
Copy link
Collaborator

@tomeras91 tomeras91 commented Jul 29, 2025

Summary by CodeRabbit

  • New Features

    • Improved support for distinguishing between key-value heads and attention heads in model configurations.
  • Bug Fixes

    • Simplified internal logic for handling head dimensions and key-value head duplication in model weight mapping.
  • Tests

    • Re-enabled a previously skipped integration test for automatic data type handling.

Description

This PR fixes weight loading for NemotronNAS with TP>1.

The issue was in layers where the number of KV heads is smaller than TP size. In this case, KV heads should be duplicated to match the TP size, so each TP rank can hold a full KV head. The logic to call _duplicate_kv where _num_kv_heads is a list was flawed.

The fix is to compute the number of KV heads from the shape of the K or V weight and the head dimension. The list logic that was present in _duplicate_kv_weights was added in a PR relevant only for Qwen3MOE, so it is now encapsulated only in Qwen3MoeHfWeightMapper.

Other than this, this PR also fixes the type hint for BaseWeightMapper._config.

Test Coverage

test_llm_api_pytorch.TestNemotronNas.test_auto_dtype_tp8 now passes

…d uses head_dim to get number of kv heads for the specific module

Signed-off-by: Tomer Asida <[email protected]>
…en3 the special case. Now VGQA works for the general case and no need for special code for NemotronNAS

Signed-off-by: Tomer Asida <[email protected]>
…mpute num_kv_heads from head_dim and weight shape in standard flow

Signed-off-by: Tomer Asida <[email protected]>
@tomeras91 tomeras91 requested a review from a team as a code owner July 29, 2025 10:59
@tomeras91 tomeras91 requested review from dongxuy04 and nv-yilinf July 29, 2025 10:59
Copy link
Contributor

coderabbitai bot commented Jul 29, 2025

📝 Walkthrough

Walkthrough

The changes update type annotations and initialization logic in weight mapper classes, refine key-value head handling, and simplify key-value weight duplication logic. Additionally, a waiver for a specific integration test is removed, enabling its execution.

Changes

Cohort / File(s) Change Summary
Base Weight Mapper Type Updates
tensorrt_llm/_torch/models/checkpoints/base_weight_mapper.py
Refactored type annotations from TConfig to ModelConfig for attributes, method parameters, and property. Adjusted logic for setting _head_dim and related configuration handling in init_model_and_config.
Qwen3 MoE Weight Mapper Extension
tensorrt_llm/_torch/models/checkpoints/hf/qwen3_moe_weight_mapper.py
Added/overrode init_model_and_config method to set _num_kv_heads based on model configuration, distinguishing between key-value and attention heads.
Key-Value Weight Duplication Logic
tensorrt_llm/_torch/models/checkpoints/hf/weight_mapper.py
Simplified _duplicate_kv_weights by directly computing num_kv_heads from tensor shape and _head_dim, removing list-based logic and enumeration.
Test Waiver Removal
tests/integration/test_lists/waives.txt
Removed skip entry for accuracy/test_llm_api_pytorch.py::TestNemotronNas::test_auto_dtype_tp8, re-enabling this test in integration runs.

Sequence Diagram(s)

sequenceDiagram
    participant Tester
    participant WaiverList

    Tester->>WaiverList: Check if test_auto_dtype_tp8 is skipped
    WaiverList-->>Tester: No longer skipped (entry removed)
    Tester->>Tester: Run test_auto_dtype_tp8 as part of integration suite
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~7 minutes

Suggested reviewers

  • yilin-void
  • brb-nv

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f35504f and 3ed48e0.

📒 Files selected for processing (1)
  • tests/integration/test_lists/waives.txt (0 hunks)
💤 Files with no reviewable changes (1)
  • tests/integration/test_lists/waives.txt
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Pre-commit Check
✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @coderabbitai title anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@tomeras91 tomeras91 requested review from shaharmor98 and Copilot July 29, 2025 10:59
@coderabbitai coderabbitai bot requested review from syuoni and yilin-void July 29, 2025 10:59
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes weight loading for NemotronNAS models when tensor parallelization (TP) is greater than 1, specifically addressing an issue where KV heads with fewer heads than TP size weren't being properly duplicated.

  • Fixes KV head duplication logic by computing heads from weight shapes instead of using a flawed list-based approach
  • Moves Qwen3MOE-specific logic into its dedicated weight mapper class
  • Corrects type hints for BaseWeightMapper configuration

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
tests/integration/test_lists/waives.txt Removes test skip for NemotronNas TP8 test since it now passes
tensorrt_llm/_torch/models/checkpoints/hf/weight_mapper.py Simplifies KV weight duplication by computing heads from weight shape
tensorrt_llm/_torch/models/checkpoints/hf/qwen3_moe_weight_mapper.py Adds Qwen3MOE-specific KV heads initialization logic
tensorrt_llm/_torch/models/checkpoints/base_weight_mapper.py Updates type hints and replaces num_kv_heads with head_dim calculation

@coderabbitai coderabbitai bot requested a review from brb-nv July 29, 2025 11:28
@tomeras91
Copy link
Collaborator Author

/bot run

@tensorrt-cicd
Copy link
Collaborator

PR_Github #13364 [ run ] triggered by Bot

Copy link
Collaborator

@shaharmor98 shaharmor98 left a comment

Choose a reason for hiding this comment

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

Short and sweet.
One little ask, approved anyway

@tensorrt-cicd
Copy link
Collaborator

PR_Github #13364 [ run ] completed with state SUCCESS
/LLM/main/L0_MergeRequest_PR pipeline #9994 completed with status: 'FAILURE'

Signed-off-by: Tomer Asida <[email protected]>
@tomeras91
Copy link
Collaborator Author

/bot run

@tensorrt-cicd
Copy link
Collaborator

PR_Github #13373 [ run ] triggered by Bot

@coderabbitai coderabbitai bot requested a review from liji-nv July 29, 2025 14:10
@tensorrt-cicd
Copy link
Collaborator

PR_Github #13373 [ run ] completed with state SUCCESS
/LLM/main/L0_MergeRequest_PR pipeline #10002 completed with status: 'FAILURE'

@shaharmor98 shaharmor98 enabled auto-merge (squash) July 30, 2025 05:22
@shaharmor98
Copy link
Collaborator

/bot run

@tensorrt-cicd
Copy link
Collaborator

PR_Github #13481 [ run ] triggered by Bot

@tensorrt-cicd
Copy link
Collaborator

PR_Github #13481 [ run ] completed with state SUCCESS
/LLM/main/L0_MergeRequest_PR pipeline #10095 completed with status: 'FAILURE'

@shaharmor98
Copy link
Collaborator

/bot run --disable-fail-fast

@tensorrt-cicd
Copy link
Collaborator

PR_Github #13491 [ run ] triggered by Bot

@tensorrt-cicd
Copy link
Collaborator

PR_Github #13491 [ run ] completed with state SUCCESS
/LLM/main/L0_MergeRequest_PR pipeline #10103 completed with status: 'SUCCESS'
Pipeline passed with automatic retried tests. Check the rerun report for details.

@shaharmor98 shaharmor98 merged commit a2514d9 into NVIDIA:main Jul 30, 2025
3 checks passed
@tomeras91 tomeras91 deleted the fix-nemotron-nas-tp-gt-1 branch July 30, 2025 12:03
lancelly pushed a commit to lancelly/TensorRT-LLM that referenced this pull request Aug 6, 2025
jain-ria pushed a commit to jain-ria/TensorRT-LLM that referenced this pull request Aug 7, 2025
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