Skip to content

Conversation

achartier
Copy link
Collaborator

@achartier achartier commented Jul 22, 2025

Summary by CodeRabbit

  • New Features

    • Added support for a new quantization mode ("compressed-tensors" with "float-quantized" format) for model loading and configuration.
    • Introduced a new weight loading mode for expert weights in MoE models, enhancing compatibility with FP8 quantization recipes.
    • Enabled recognition and handling of FP8 row-wise quantization in relevant modules.
  • Bug Fixes

    • Improved tensor shape handling when loading weight scales to ensure correct dimensions.
  • Refactor

    • Updated logic to accommodate new quantization modes and weight loading behaviors without altering user-facing APIs.

[feat] Add support to load fp8 Meta Llama4 weights

Description

TODO

Test Coverage

TODO

GitHub Bot Help

/bot [-h] ['run', 'kill', 'skip', 'reuse-pipeline'] ...

Provide a user friendly way for developers to interact with a Jenkins server.

Run /bot [-h|--help] to print this help message.

See details below for each supported subcommand.

run [--reuse-test (optional)pipeline-id --disable-fail-fast --skip-test --stage-list "A10-PyTorch-1, xxx" --gpu-type "A30, H100_PCIe" --test-backend "pytorch, cpp" --add-multi-gpu-test --only-multi-gpu-test --disable-multi-gpu-test --post-merge --extra-stage "H100_PCIe-TensorRT-Post-Merge-1, xxx" --detailed-log --debug(experimental)]

Launch build/test pipelines. All previously running jobs will be killed.

--reuse-test (optional)pipeline-id (OPTIONAL) : Allow the new pipeline to reuse build artifacts and skip successful test stages from a specified pipeline or the last pipeline if no pipeline-id is indicated. If the Git commit ID has changed, this option will be always ignored. The DEFAULT behavior of the bot is to reuse build artifacts and successful test results from the last pipeline.

--disable-reuse-test (OPTIONAL) : Explicitly prevent the pipeline from reusing build artifacts and skipping successful test stages from a previous pipeline. Ensure that all builds and tests are run regardless of previous successes.

--disable-fail-fast (OPTIONAL) : Disable fail fast on build/tests/infra failures.

--skip-test (OPTIONAL) : Skip all test stages, but still run build stages, package stages and sanity check stages. Note: Does NOT update GitHub check status.

--stage-list "A10-PyTorch-1, xxx" (OPTIONAL) : Only run the specified test stages. Examples: "A10-PyTorch-1, xxx". Note: Does NOT update GitHub check status.

--gpu-type "A30, H100_PCIe" (OPTIONAL) : Only run the test stages on the specified GPU types. Examples: "A30, H100_PCIe". Note: Does NOT update GitHub check status.

--test-backend "pytorch, cpp" (OPTIONAL) : Skip test stages which don't match the specified backends. Only support [pytorch, cpp, tensorrt, triton]. Examples: "pytorch, cpp" (does not run test stages with tensorrt or triton backend). Note: Does NOT update GitHub pipeline status.

--only-multi-gpu-test (OPTIONAL) : Only run the multi-GPU tests. Note: Does NOT update GitHub check status.

--disable-multi-gpu-test (OPTIONAL) : Disable the multi-GPU tests. Note: Does NOT update GitHub check status.

--add-multi-gpu-test (OPTIONAL) : Force run the multi-GPU tests in addition to running L0 pre-merge pipeline.

--post-merge (OPTIONAL) : Run the L0 post-merge pipeline instead of the ordinary L0 pre-merge pipeline.

--extra-stage "H100_PCIe-TensorRT-Post-Merge-1, xxx" (OPTIONAL) : Run the ordinary L0 pre-merge pipeline and specified test stages. Examples: --extra-stage "H100_PCIe-TensorRT-Post-Merge-1, xxx".

--detailed-log (OPTIONAL) : Enable flushing out all logs to the Jenkins console. This will significantly increase the log volume and may slow down the job.

--debug (OPTIONAL) : Experimental feature. Enable access to the CI container for debugging purpose. Note: Specify exactly one stage in the stage-list parameter to access the appropriate container environment. Note: Does NOT update GitHub check status.

For guidance on mapping tests to stage names, see docs/source/reference/ci-overview.md
and the scripts/test_to_stage_mapping.py helper.

kill

kill

Kill all running builds associated with pull request.

skip

skip --comment COMMENT

Skip testing for latest commit on pull request. --comment "Reason for skipping build/test" is required. IMPORTANT NOTE: This is dangerous since lack of user care and validation can cause top of tree to break.

reuse-pipeline

reuse-pipeline

Reuse a previous pipeline to validate current commit. This action will also kill all currently running builds associated with the pull request. IMPORTANT NOTE: This is dangerous since lack of user care and validation can cause top of tree to break.

Copy link
Contributor

coderabbitai bot commented Jul 22, 2025

Walkthrough

The changes introduce support for a new quantization mode, "compressed-tensors" with "float-quantized" format, across model configuration, weight loading, and quantization logic. Updates include new enum values, properties, and conditional branches to handle this mode, as well as adjustments to tensor shape handling during weight loading.

Changes

File(s) Change Summary
tensorrt_llm/_torch/model_config.py, tensorrt_llm/llmapi/llm_utils.py Added logic to detect and process "compressed-tensors" with "float-quantized" quantization config in model loading paths.
tensorrt_llm/_torch/models/modeling_llama.py Updated Llama4MoE to select weight loading mode based on quantization config's use_meta_recipe flag.
tensorrt_llm/_torch/modules/fused_moe/interface.py Added META_FP8_RECIPE to MoEWeightLoadingMode enum and has_fp8_rowwise property to MoE class.
tensorrt_llm/_torch/modules/fused_moe/fused_moe_cutlass.py Added support for fp8_rowwise quantization mode in config checks and quantization method selection.
tensorrt_llm/_torch/modules/fused_moe/quantization.py Added handling for META_FP8_RECIPE mode in weight and scale loading methods of FP8QDQFusedMoEMethod.
tensorrt_llm/_torch/modules/linear.py Ensured loaded weight scale tensors are squeezed to remove singleton dimensions.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant ModelLoader
    participant ModelConfig
    participant QuantConfig

    User->>ModelLoader: load(model_path, hf_quant_config)
    ModelLoader->>ModelConfig: from_pretrained(..., hf_quant_config)
    ModelConfig->>QuantConfig: process quant_method/format
    alt quant_method == "compressed-tensors" and format == "float-quantized"
        QuantConfig->>QuantConfig: set quant_algo = FP8_PER_CHANNEL_PER_TOKEN
        QuantConfig->>QuantConfig: set use_meta_recipe = True
        QuantConfig->>QuantConfig: process ignore list
    else other quantization
        QuantConfig->>QuantConfig: handle other quantization configs
    end
    ModelConfig-->>ModelLoader: return config
    ModelLoader-->>User: return loaded model
Loading

Estimated code review effort

3 (~45 minutes)

Suggested reviewers

  • juney-nvidia
  • symphonylyh
  • yizhang-nv

Poem

In the warren where tensors leap and bound,
New quantization magic has been found!
With floats compressed and recipes anew,
Our models crunch numbers with a rabbit's quick chew.
Squeeze those scales, hop through each mode—
Onward, bunnies, down this quantized road!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

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

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
tensorrt_llm/_torch/models/modeling_llama.py (1)

276-276: Fix line length violation and improve readability.

The conditional assignment logic is correct, but the line exceeds the 120-character limit flagged by static analysis.

-        weight_loading_mode = MoEWeightLoadingMode.META_FP8_RECIPE if model_config.quant_config.use_meta_recipe else MoEWeightLoadingMode.FUSED_GATE_UP_PROJ
+        weight_loading_mode = (MoEWeightLoadingMode.META_FP8_RECIPE 
+                              if model_config.quant_config.use_meta_recipe 
+                              else MoEWeightLoadingMode.FUSED_GATE_UP_PROJ)
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 5234502 and 950c5b7.

📒 Files selected for processing (7)
  • tensorrt_llm/_torch/model_config.py (1 hunks)
  • tensorrt_llm/_torch/models/modeling_llama.py (2 hunks)
  • tensorrt_llm/_torch/modules/fused_moe/fused_moe_cutlass.py (3 hunks)
  • tensorrt_llm/_torch/modules/fused_moe/interface.py (2 hunks)
  • tensorrt_llm/_torch/modules/fused_moe/quantization.py (3 hunks)
  • tensorrt_llm/_torch/modules/linear.py (2 hunks)
  • tensorrt_llm/llmapi/llm_utils.py (1 hunks)
🧠 Learnings (1)
tensorrt_llm/_torch/modules/linear.py (1)

Learnt from: yechank-nvidia
PR: #6254
File: tensorrt_llm/_torch/pyexecutor/model_engine.py:1201-1204
Timestamp: 2025-07-22T09:22:14.703Z
Learning: In TensorRT-LLM's multimodal processing pipeline, shared tensor recovery using from_shared_tensor() is only needed during the context phase. Generation requests reuse the already-recovered tensor data and only need to call strip_for_generation() to remove unnecessary multimodal data while preserving the recovered tensors. This avoids redundant tensor recovery operations during generation.

🪛 Ruff (0.12.2)
tensorrt_llm/_torch/models/modeling_llama.py

276-276: Line too long (156 > 120)

(E501)

🧰 Additional context used
🧠 Learnings (1)
tensorrt_llm/_torch/modules/linear.py (1)

Learnt from: yechank-nvidia
PR: #6254
File: tensorrt_llm/_torch/pyexecutor/model_engine.py:1201-1204
Timestamp: 2025-07-22T09:22:14.703Z
Learning: In TensorRT-LLM's multimodal processing pipeline, shared tensor recovery using from_shared_tensor() is only needed during the context phase. Generation requests reuse the already-recovered tensor data and only need to call strip_for_generation() to remove unnecessary multimodal data while preserving the recovered tensors. This avoids redundant tensor recovery operations during generation.

🪛 Ruff (0.12.2)
tensorrt_llm/_torch/models/modeling_llama.py

276-276: Line too long (156 > 120)

(E501)

⏰ 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
🔇 Additional comments (11)
tensorrt_llm/_torch/modules/linear.py (2)

501-502: LGTM: Proper tensor shape normalization for FP8 row-wise quantization.

The .squeeze() call ensures that singleton dimensions are removed from the weight scale tensor after loading, which is necessary for proper FP8 row-wise quantization handling.


537-537: LGTM: Consistent tensor shape handling in fused operations.

The .squeeze() call on the concatenated scale tensor maintains consistency with the vanilla weight loading approach and ensures proper tensor dimensionality for FP8 operations.

tensorrt_llm/_torch/modules/fused_moe/interface.py (2)

15-15: LGTM: New enum member for META FP8 recipe support.

The addition of META_FP8_RECIPE = 2 extends the weight loading modes to support the new FP8 quantization recipe, following the existing enum pattern.


115-119: LGTM: Consistent property implementation for FP8 row-wise detection.

The new has_fp8_rowwise property follows the same pattern as existing quantization detection properties and provides a clean interface for checking FP8 row-wise quantization mode.

tensorrt_llm/_torch/models/modeling_llama.py (1)

285-285: LGTM: Proper use of dynamic weight loading mode.

The use of the local weight_loading_mode variable instead of a hardcoded value enables proper conditional selection based on the quantization configuration.

tensorrt_llm/llmapi/llm_utils.py (1)

429-434: LGTM! Proper integration of Llama4 FP8 quantization support.

The new conditional branch correctly handles the "compressed-tensors" with "float-quantized" format by setting the appropriate quantization algorithm. The implementation follows the existing pattern and maintains consistency with the DeepSeek V3 FP8 case above it.

tensorrt_llm/_torch/modules/fused_moe/fused_moe_cutlass.py (3)

133-133: Good addition to quantization mode validation.

The inclusion of has_fp8_rowwise() in the supported quantization modes check is consistent with the existing validation pattern and ensures the new fp8_rowwise mode is properly recognized.


149-150: Appropriate method selection for fp8_rowwise quantization.

The extension of the condition to include has_fp8_rowwise() alongside has_fp8_qdq() makes sense, as both modes appear to use the same FP8QDQFusedMoEMethod() for quantization handling.


231-231: Consistent input quantization handling for fp8_rowwise.

The addition of has_fp8_rowwise to the condition for per-tensor static quantization aligns with treating this mode similarly to has_fp8_qdq, maintaining consistency in the quantization flow.

tensorrt_llm/_torch/model_config.py (1)

272-286: Comprehensive Llama4 FP8 quantization support implementation.

The new conditional branch properly handles the "compressed-tensors" with "float-quantized" format. The exclude_modules processing correctly transforms HF module names to TRT-LLM equivalents by removing the "language_model." prefix and mapping "q_proj" to "qkv_proj". The assignment of use_meta_recipe = True enables the META_FP8_RECIPE weight loading mode as intended.

tensorrt_llm/_torch/modules/fused_moe/quantization.py (1)

103-106: Weight loading implementation follows established patterns.

The new META_FP8_RECIPE case correctly implements the weight loading logic using Meta's naming conventions (gate_proj, up_proj, down_proj vs w1, w3, w2).

Comment on lines +371 to +375
elif module.weight_loading_mode == MoEWeightLoadingMode.META_FP8_RECIPE:
# HACK: dynamic quantization so no input scale - create empty tensors for testing
w1_input_scale = torch.tensor(1.0, dtype=torch.float32)
w3_input_scale = torch.tensor(1.0, dtype=torch.float32)
w2_input_scale = torch.tensor(1.0, dtype=torch.float32)
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Replace hardcoded scale values with proper dynamic quantization implementation.

The current implementation uses hardcoded 1.0 values as a workaround for dynamic quantization. This approach could lead to incorrect quantization behavior and reduced model accuracy.

Consider implementing proper dynamic quantization scale computation instead:

 elif module.weight_loading_mode == MoEWeightLoadingMode.META_FP8_RECIPE:
-    # HACK: dynamic quantization so no input scale - create empty tensors for testing
-    w1_input_scale = torch.tensor(1.0, dtype=torch.float32)
-    w3_input_scale = torch.tensor(1.0, dtype=torch.float32)
-    w2_input_scale = torch.tensor(1.0, dtype=torch.float32)
+    # TODO: Implement proper dynamic quantization scale computation
+    # For now, compute scales dynamically or load from appropriate keys
+    w1_input_scale = self._compute_dynamic_input_scale(weights, expert_id, "gate_proj")
+    w3_input_scale = self._compute_dynamic_input_scale(weights, expert_id, "up_proj")  
+    w2_input_scale = self._compute_dynamic_input_scale(weights, expert_id, "down_proj")

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In tensorrt_llm/_torch/modules/fused_moe/quantization.py around lines 371 to
375, the code uses hardcoded scale values of 1.0 for dynamic quantization, which
is a placeholder and can cause incorrect quantization results. Replace these
hardcoded tensors with a proper dynamic quantization scale computation by
calculating the input scales based on the actual data or model parameters to
ensure accurate quantization behavior.

Comment on lines +409 to +413
elif module.weight_loading_mode == MoEWeightLoadingMode.META_FP8_RECIPE:
# HACK: weight scale is not the right shape in blockwise recipe - create empty tensors for testing
w1_weight_scale = torch.tensor(1.0, dtype=torch.float32)
w3_weight_scale = torch.tensor(1.0, dtype=torch.float32)
w2_weight_scale = torch.tensor(1.0, dtype=torch.float32)
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Address the underlying shape mismatch instead of using hardcoded values.

The hardcoded weight scales could lead to suboptimal quantization. The comment indicates a shape mismatch with the "blockwise recipe" that should be addressed properly.

Consider addressing the root cause:

 elif module.weight_loading_mode == MoEWeightLoadingMode.META_FP8_RECIPE:
-    # HACK: weight scale is not the right shape in blockwise recipe - create empty tensors for testing
-    w1_weight_scale = torch.tensor(1.0, dtype=torch.float32)
-    w3_weight_scale = torch.tensor(1.0, dtype=torch.float32)
-    w2_weight_scale = torch.tensor(1.0, dtype=torch.float32)
+    # Load and reshape weight scales to match expected format
+    w1_weight_scale = self._load_and_reshape_weight_scale(weights, expert_id, "gate_proj")
+    w3_weight_scale = self._load_and_reshape_weight_scale(weights, expert_id, "up_proj")
+    w2_weight_scale = self._load_and_reshape_weight_scale(weights, expert_id, "down_proj")

Let me verify if there are existing implementations that handle dynamic quantization properly:


🏁 Script executed:

#!/bin/bash
# Search for dynamic quantization implementations and scale computation methods
rg -A 10 -B 5 "dynamic.*quantization|compute.*scale" --type py

Length of output: 45171


Replace hardcoded FP8 weight‐scale hack with proper loading and reshaping

The current META_FP8_RECIPE branch (lines 409–413 in
tensorrt_llm/_torch/modules/fused_moe/quantization.py) uses placeholder scalars for all three weight scales. This bypasses actual per-expert, blockwise scale data and will degrade quantization accuracy. Please implement a loader that:

  • Reads each expert’s stored weight scale (weights["{expert_id}.w1.weight_scale"], etc.)
  • Reshapes it to match the blockwise partitioning used by the fused MoE kernels
  • Registers or returns the correctly shaped tensor for w1_weight_scale, w3_weight_scale, and w2_weight_scale

For example, instead of:

-    # HACK: weight scale is not the right shape in blockwise recipe - create empty tensors for testing
-    w1_weight_scale = torch.tensor(1.0, dtype=torch.float32)
-    w3_weight_scale = torch.tensor(1.0, dtype=torch.float32)
-    w2_weight_scale = torch.tensor(1.0, dtype=torch.float32)
+    # TODO: load and reshape the per-expert FP8 weight scales for blockwise META_FP8_RECIPE
+    raw_w1 = weights[f"{expert_id}.w1.weight_scale"]
+    raw_w3 = weights[f"{expert_id}.w3.weight_scale"]
+    raw_w2 = weights[f"{expert_id}.w2.weight_scale"]
+    w1_weight_scale = raw_w1.view(expected_block_shape).to(torch.float32)
+    w3_weight_scale = raw_w3.view(expected_block_shape).to(torch.float32)
+    w2_weight_scale = raw_w2.view(expected_block_shape).to(torch.float32)

• File: tensorrt_llm/_torch/modules/fused_moe/quantization.py
• Lines: ~409–413

Implement and test this loader against your blockwise FP8 metadata to ensure correct shapes and quantization fidelity.

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In tensorrt_llm/_torch/modules/fused_moe/quantization.py around lines 409 to
413, replace the hardcoded scalar tensors used as placeholders for
w1_weight_scale, w2_weight_scale, and w3_weight_scale with actual loading logic.
Implement code to read each expert's stored weight scale from weights using keys
like "{expert_id}.w1.weight_scale", reshape these tensors to match the blockwise
partitioning expected by the fused MoE kernels, and assign or return these
properly shaped tensors instead of the placeholders. Ensure the loader correctly
handles all experts and weight scales to maintain quantization accuracy.

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.

1 participant