Skip to content

Conversation

@hthadicherla
Copy link
Contributor

@hthadicherla hthadicherla commented Jan 23, 2026

What does this PR do?

Type of change: ? New feature

Overview:
TensorRT-RTX requires the weights and scales in the ONNX models to be in column-major format. So whenever the model loads TRT-RTX JIT transposes the weights and scales during load time, causing increased load time.

Proposed feature is after quantization, transpose the weights and scales in DQ node and add a transpose node right after i.e,
A × B = A × ((Bᵀ)ᵀ)

The transformation is post processing step and is disabled by default. It can be enabled by quantizing with --use_column_major

Usage

python -m modelopt.onnx.quantization --onnx_path "model.onnx" --output_path "model_quant.onnx" --quantize_mode int4 --calibration_method awq_lite --use_column_major --skip_shared_constants_duplication

Testing

Tested a few LLM's and their MMLU scores with and without this transformation. No degradations were observed.

Summary by CodeRabbit

Release Notes

  • New Features
    • Added --use_column_major command-line flag to ONNX quantization script for enabling column-major weight storage optimization compatible with NvTensorRtRtx execution provider. This optimization applies to DQ-only quantization modes (rtn_dq, awq_lite, awq_clip).

✏️ Tip: You can customize this high-level summary in your review settings.

…improvement in TRT-RTX

Signed-off-by: Hrishith Thadicherla <hthadicherla@nvidia.com>
@hthadicherla hthadicherla requested review from a team as code owners January 23, 2026 11:42
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 23, 2026

📝 Walkthrough

Walkthrough

This PR introduces a column-major storage optimization feature for ONNX INT4 quantization targeting the NvTensorRtRtx execution provider. It adds a CLI flag to the quantization script, integrates it through the quantization pipeline, and provides utility functions for applying column-major transformations to GEMM weights and inserting transpose operations in DQ-only quantization modes.

Changes

Cohort / File(s) Summary
CLI & API Integration
examples/windows/onnx_ptq/genai_llm/quantize.py, modelopt/onnx/quantization/int4.py
Adds --use_column_major CLI argument and threads it through quantization function signature. Integrates flag handling into quantize_rtn, quantize, _quantize_awq_clip, and _quantize_awq_lite pathways. When enabled, branches control flow to apply column-major transformation to GEMM weights prior to DQ node creation. Flag is logged and guarded to avoid usage in incompatible modes (e.g., QDQ mode).
Transformation Utilities
modelopt/onnx/quantization/qdq_utils.py
Adds three new public functions: _apply_transpose_perm_to_shape() for computing transposed shapes, apply_column_major_transformation() to transpose quantized weights/scales in-place and return DQ attributes with axis set to 1, and add_transpose_nodes_for_column_major() to conditionally insert Transpose nodes after DQ nodes feeding MatMul/Gemm and update graph connections. Includes safeguards to skip already-processed nodes and avoid altering Gemm when transB is set.

Sequence Diagram(s)

sequenceDiagram
    actor User
    participant CLI as quantize.py<br/>(CLI)
    participant API as int4.py<br/>(quantize)
    participant Transform as qdq_utils.py<br/>(apply_column_major)
    participant Graph as Graph<br/>(ONNX)
    
    User->>CLI: --use_column_major flag
    CLI->>API: quantize(...,<br/>use_column_major=True)
    API->>Transform: apply_column_major_transformation(<br/>weights, scales, ...)
    Transform->>Transform: Transpose weights &<br/>scales in-place
    Transform->>API: Return DQ attributes<br/>(axis=1)
    API->>Graph: Create DQ nodes with<br/>column-major attributes
    API->>Transform: add_transpose_nodes_for_column_major(graph)
    Transform->>Graph: Insert Transpose nodes<br/>after DQ nodes
    Transform->>Graph: Update MatMul/Gemm<br/>inputs
    Graph-->>User: Optimized ONNX model
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title clearly and specifically describes the main change: adding column-major storage of weights and scales in INT4 quantization for TRT-RTX model load time improvement. It directly summarizes the primary objective and is well-suited to the changeset.
Docstring Coverage ✅ Passed Docstring coverage is 90.00% which is sufficient. The required threshold is 80.00%.

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

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

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: 1

🤖 Fix all issues with AI agents
In `@modelopt/onnx/quantization/qdq_utils.py`:
- Around line 1083-1091: The current Gemm handling in
apply_column_major_transformation (qdq_utils.py) skips nodes with node.op ==
"Gemm" when node.attrs contains transB=1, which breaks semantics for
column-major weights; instead, when encountering a Gemm with transB set, update
the node.attrs transB to 0 (or remove/normalize it to zero) so the graph expects
B^T (matching the earlier weight transpose) and do not skip inserting the
transpose-back; locate the Gemm handling block (check for node.op == "Gemm" and
the transB logic) and replace the early continue with logic that flips
node.attrs["transB"] to 0 (or deletes the attr) so outputs remain correct while
keeping the transpose-back insertion.

Comment on lines +1083 to +1091
# For Gemm nodes, check if transB is already set
if node.op == "Gemm":
trans_b = False
if hasattr(node, "attrs") and "transB" in node.attrs:
trans_b = node.attrs["transB"] > 0
if trans_b:
logger.debug(f"Gemm node {node.name} already has transB=1, skipping")
continue

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 | 🔴 Critical

Gemm transB=1 skip breaks correctness with column‑major weights.
apply_column_major_transformation already transposes weights. If a Gemm has transB=1, skipping the transpose‑back makes Gemm consume B instead of B^T, changing outputs. Either always insert the transpose‑back or flip transB to 0 so Gemm consumes B^T directly.

🐛 Proposed fix (flip transB to 0 and keep semantics)
-            # For Gemm nodes, check if transB is already set
-            if node.op == "Gemm":
-                trans_b = False
-                if hasattr(node, "attrs") and "transB" in node.attrs:
-                    trans_b = node.attrs["transB"] > 0
-                if trans_b:
-                    logger.debug(f"Gemm node {node.name} already has transB=1, skipping")
-                    continue
+            # For Gemm nodes with transB=1, flip to 0 since weights are already transposed
+            if node.op == "Gemm":
+                trans_b = bool((node.attrs or {}).get("transB", 0))
+                if trans_b:
+                    node.attrs = node.attrs or {}
+                    node.attrs["transB"] = 0
+                    logger.debug(
+                        f"Gemm node {node.name} has transB=1; setting transB=0 for column-major weights"
+                    )
+                    continue
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# For Gemm nodes, check if transB is already set
if node.op == "Gemm":
trans_b = False
if hasattr(node, "attrs") and "transB" in node.attrs:
trans_b = node.attrs["transB"] > 0
if trans_b:
logger.debug(f"Gemm node {node.name} already has transB=1, skipping")
continue
# For Gemm nodes with transB=1, flip to 0 since weights are already transposed
if node.op == "Gemm":
trans_b = bool((node.attrs or {}).get("transB", 0))
if trans_b:
node.attrs = node.attrs or {}
node.attrs["transB"] = 0
logger.debug(
f"Gemm node {node.name} has transB=1; setting transB=0 for column-major weights"
)
continue
🤖 Prompt for AI Agents
In `@modelopt/onnx/quantization/qdq_utils.py` around lines 1083 - 1091, The
current Gemm handling in apply_column_major_transformation (qdq_utils.py) skips
nodes with node.op == "Gemm" when node.attrs contains transB=1, which breaks
semantics for column-major weights; instead, when encountering a Gemm with
transB set, update the node.attrs transB to 0 (or remove/normalize it to zero)
so the graph expects B^T (matching the earlier weight transpose) and do not skip
inserting the transpose-back; locate the Gemm handling block (check for node.op
== "Gemm" and the transB logic) and replace the early continue with logic that
flips node.attrs["transB"] to 0 (or deletes the attr) so outputs remain correct
while keeping the transpose-back insertion.

@codecov
Copy link

codecov bot commented Jan 23, 2026

Codecov Report

❌ Patch coverage is 24.28571% with 53 lines in your changes missing coverage. Please review.
✅ Project coverage is 73.99%. Comparing base (4f4558a) to head (dc4096d).

Files with missing lines Patch % Lines
modelopt/onnx/quantization/qdq_utils.py 6.38% 44 Missing ⚠️
modelopt/onnx/quantization/int4.py 60.86% 9 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #811      +/-   ##
==========================================
- Coverage   74.17%   73.99%   -0.19%     
==========================================
  Files         192      192              
  Lines       19246    19313      +67     
==========================================
+ Hits        14276    14290      +14     
- Misses       4970     5023      +53     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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