Skip to content

[None][perf] Fuse Qwen3.5 gated QKV preprocessing for qwen3.5 v2 ckpt#16322

Open
nv-guomingz wants to merge 7 commits into
NVIDIA:mainfrom
nv-guomingz:user/guomingz/fuse_attn
Open

[None][perf] Fuse Qwen3.5 gated QKV preprocessing for qwen3.5 v2 ckpt#16322
nv-guomingz wants to merge 7 commits into
NVIDIA:mainfrom
nv-guomingz:user/guomingz/fuse_attn

Conversation

@nv-guomingz

@nv-guomingz nv-guomingz commented Jul 13, 2026

Copy link
Copy Markdown
Collaborator

It's a successor version of #16024

Description

Test Coverage

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.

Summary by CodeRabbit

  • New Features
    • Added support for strided fused QKV inputs in supported attention paths.
    • Added optional fused Q/K RMSNorm preprocessing, including Gemma-style normalization.
    • Added gate-tail QKV layout support for compatible attention configurations.
    • Exposed new normalization options through the attention API.
  • Bug Fixes
    • Added validation and clearer fallback behavior for unsupported layouts, data types, and configurations.
  • Tests
    • Added coverage for gate-tail layouts, backend capability detection, and quantized weights.

@nv-guomingz

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@coderabbitai

coderabbitai Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The change propagates QK normalization and token-stride parameters through attention APIs and preprocessing kernels, adds BF16 fused QK normalization with GPT-NeoX RoPE, enables backend selection for strided fused QKV layouts, and introduces gate-tail QKV projection support with tests.

Changes

Attention parameter and preprocessing pipeline

Layer / File(s) Summary
Attention API and parameter wiring
cpp/tensorrt_llm/common/attentionOp.*, cpp/tensorrt_llm/kernels/..., cpp/tensorrt_llm/thop/..., tensorrt_llm/_torch/attention_backend/fmha/*
Attention interfaces, enqueue parameters, XQA conversion, bindings, and fallback calls now carry token stride and QK normalization weights/configuration.
Strided preprocessing and fused QK normalization
cpp/tensorrt_llm/kernels/unfusedAttentionKernels*, cpp/tensorrt_llm/kernels/xqaDispatcher.cpp, cpp/tensorrt_llm/common/attentionOp.cpp
Preprocessing validates supported layouts and normalization inputs, indexes row-strided QKV data, and adds BF16 fused QK normalization with GPT-NeoX RoPE.
Python backend integration
tensorrt_llm/_torch/attention_backend/*, tensorrt_llm/_torch/modules/qk_norm_attention.py
TRTLLM backend configuration, strided fused-QKV capability checks, and QK norm/RoPE preprocessing are wired into attention execution.
Gate-tail QKV layout
tensorrt_llm/_torch/modules/attention.py, tests/unittest/_torch/attention/test_gate_tail_layout.py
Supported configurations permute QKV projection rows into a gate-tail layout, extract the gate during forward, slice the attention input, and verify behavior across backend and quantization cases.

Estimated code review effort: 4 (Complex) | ~60 minutes

Sequence Diagram(s)

sequenceDiagram
  participant QKNormRoPEAttention
  participant TrtllmAttention
  participant thop_attention
  participant QKVPreprocessing
  QKNormRoPEAttention->>TrtllmAttention: configure QK normalization preprocessing
  TrtllmAttention->>thop_attention: pass Q/K weights and normalization settings
  thop_attention->>QKVPreprocessing: enqueue strided QKV preprocessing
  QKVPreprocessing->>TrtllmAttention: return normalized and RoPE-transformed Q/K
Loading

Possibly related PRs

Suggested reviewers: schetlur-nv, qijune, arysef, sunnyqgg, suyoggupta

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description only says it is a successor to #16024 and leaves the required issue, solution, test coverage, and checklist sections empty. Add a short problem/solution summary, test coverage, and complete the PR checklist sections required by the template.
Docstring Coverage ⚠️ Warning Docstring coverage is 27.03% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title is specific and matches the main change: fused gated QKV preprocessing for Qwen3.5 v2 checkpoint.
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.
✨ 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.

Actionable comments posted: 3

🧹 Nitpick comments (2)
tests/unittest/_torch/attention/test_gate_tail_layout.py (1)

23-88: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Test coverage for _maybe_permute_gate_tail_layout is insufficient; runtime forward() gate-tail path is entirely untested.

As per path instructions, coverage assessment for tests/**: this suite only exercises the load-time permutation helper, and even there leaves gaps:

  • No test sets attention.qkv_proj = nn.Linear(..., bias=True), so the bias-permutation branch in attention.py (if self.qkv_proj.bias is not None: ...) is never exercised. Suggest adding e.g. test_gate_tail_layout_permutes_bias.
  • The guard chain in _maybe_permute_gate_tail_layout (attn_backend == "TRTLLM", support_fused_qkv, fuse_qk_norm_rope, not skip_rope, cp_size == 1) is only exercised through the supported/quant_kind parameters; none of these individual flags are flipped to False in isolation, so a regression that drops one of these checks wouldn't be caught. Suggest parametrizing _make_gate_tail_attention over each guard flag independently.
  • Attention.forward()'s gate-tail branch (attention.py lines 1049-1066 and 1086-1090) — the q/k/v/gate extraction and q re-slicing that this permutation exists to enable — has no test at all in this file. A numerical-equivalence test (e.g. test_gate_tail_layout_forward_matches_unpermuted_reference) comparing forward() output/gate values between the permuted-active and interleaved-inactive paths on identical inputs would give real confidence the permutation preserves inference correctness, which the current unit tests can't demonstrate.

Coverage is currently insufficient for the runtime-critical path; the load-time permutation itself is reasonably well covered.

🤖 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 `@tests/unittest/_torch/attention/test_gate_tail_layout.py` around lines 23 -
88, The tests need broader coverage for gate-tail layout behavior. Extend
_make_gate_tail_attention and add focused tests that exercise qkv_proj bias
permutation, each guard in _maybe_permute_gate_tail_layout independently
disabled, and Attention.forward’s active gate-tail branch. Include a numerical
comparison between permuted-active and unpermuted-reference attention paths,
validating equivalent outputs and gate values while preserving the existing
permutation tests.

Source: Path instructions

tensorrt_llm/_torch/modules/qk_norm_attention.py (1)

304-312: 🩺 Stability & Availability | 🔵 Trivial | ⚡ Quick win

Caching via a monkey-patched tensor attribute is fragile under graph capture.

position_ids._tllm_flat_int32 = position_ids_arg attaches an ad-hoc Python attribute to the position_ids tensor to memoize the flattened int32 cast across layers within a forward. This works for eager execution, but attribute-based side channels on tensors can behave unpredictably under torch.compile/dynamo tracing or CUDA-graph capture (attribute reads/writes on captured/traced tensors aren't guaranteed to be preserved or may trigger recompilation), especially since this module already threads disable_on_compile elsewhere for compile-awareness.

Consider caching by id(position_ids) in an external dict (cleared per forward) instead of mutating the tensor object itself.

🤖 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/qk_norm_attention.py` around lines 304 - 312,
Replace the monkey-patched _tllm_flat_int32 attribute access in the position_ids
handling with an external cache keyed by id(position_ids), initialized and
cleared per forward so it is shared across layers without mutating tensors.
Preserve the existing reshape, contiguous, int32 conversion, and reuse behavior.
🤖 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 `@cpp/tensorrt_llm/common/attentionOp.cpp`:
- Around line 2127-2129: Update the validation guards for the unfused context
MHA and masked-MHA generation fallback paths near the existing
attention_input_token_stride checks to reject non-null q_norm_weight or
k_norm_weight before dispatch. Preserve the current stride validation, and
ensure both fallback paths cannot silently proceed when QK normalization
parameters are provided.
- Around line 250-254: Update enqueueGeneration where xqaParams.qkv is replaced
by the packed Ulysses workspace for mCpSize > 1: reject non-packed inputs or
reset qkv_token_stride to the packed workspace’s row stride so it remains
consistent with xqaParams.qkv. Preserve the existing stride for non-Ulysses
generation.

In
`@cpp/tensorrt_llm/kernels/unfusedAttentionKernels/unfusedAttentionKernels_2_template.h`:
- Around line 993-1006: Update invokeQKVPreprocessing around qk_norm_applied so
a non-null params.q_norm_weight with T other than __nv_bfloat16 fails fast
instead of entering the plain GPT-NeoX rotation fallback. Preserve the existing
BF16 applyQkNormRopeGptNeox path and only allow the fallback when QK-norm is not
requested.

---

Nitpick comments:
In `@tensorrt_llm/_torch/modules/qk_norm_attention.py`:
- Around line 304-312: Replace the monkey-patched _tllm_flat_int32 attribute
access in the position_ids handling with an external cache keyed by
id(position_ids), initialized and cleared per forward so it is shared across
layers without mutating tensors. Preserve the existing reshape, contiguous,
int32 conversion, and reuse behavior.

In `@tests/unittest/_torch/attention/test_gate_tail_layout.py`:
- Around line 23-88: The tests need broader coverage for gate-tail layout
behavior. Extend _make_gate_tail_attention and add focused tests that exercise
qkv_proj bias permutation, each guard in _maybe_permute_gate_tail_layout
independently disabled, and Attention.forward’s active gate-tail branch. Include
a numerical comparison between permuted-active and unpermuted-reference
attention paths, validating equivalent outputs and gate values while preserving
the existing permutation tests.
🪄 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: 6bc2544d-98b1-4bd7-be9e-0db484d4d291

📥 Commits

Reviewing files that changed from the base of the PR and between 11a880f and 5b3d188.

📒 Files selected for processing (16)
  • cpp/tensorrt_llm/common/attentionOp.cpp
  • cpp/tensorrt_llm/common/attentionOp.h
  • cpp/tensorrt_llm/kernels/decoderMaskedMultiheadAttention/xqaParams.h
  • cpp/tensorrt_llm/kernels/unfusedAttentionKernels.h
  • cpp/tensorrt_llm/kernels/unfusedAttentionKernels/unfusedAttentionKernels_2_template.h
  • cpp/tensorrt_llm/kernels/xqaDispatcher.cpp
  • cpp/tensorrt_llm/nanobind/thop/bindings.cpp
  • cpp/tensorrt_llm/thop/attentionOp.cpp
  • cpp/tensorrt_llm/thop/attentionOp.h
  • tensorrt_llm/_torch/attention_backend/fmha/fallback.py
  • tensorrt_llm/_torch/attention_backend/fmha/flashinfer_trtllm_gen.py
  • tensorrt_llm/_torch/attention_backend/interface.py
  • tensorrt_llm/_torch/attention_backend/trtllm.py
  • tensorrt_llm/_torch/modules/attention.py
  • tensorrt_llm/_torch/modules/qk_norm_attention.py
  • tests/unittest/_torch/attention/test_gate_tail_layout.py

Comment thread cpp/tensorrt_llm/common/attentionOp.cpp
Comment thread cpp/tensorrt_llm/common/attentionOp.cpp
@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58961 [ run ] triggered by Bot. Commit: 5b3d188 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58961 [ run ] completed with state SUCCESS. Commit: 5b3d188
/LLM/main/L0_MergeRequest_PR pipeline #47493 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

@nv-guomingz

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58995 [ run ] triggered by Bot. Commit: 5b3d188 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #58995 [ run ] completed with state FAILURE. Commit: 5b3d188
/LLM/main/L0_MergeRequest_PR pipeline #47525 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

@nv-guomingz

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59047 [ run ] triggered by Bot. Commit: 5b3d188 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59047 [ run ] completed with state FAILURE. Commit: 5b3d188
/LLM/main/L0_MergeRequest_PR pipeline #47573 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

mingyangHao and others added 4 commits July 14, 2026 15:07
Add an opt-in QKV gate-tail layout to remove intermediate layout copies, and optionally fold Gemma QK RMSNorm plus RoPE into the KV-cache preprocessing kernel. Keep unsupported configurations on guarded fallback paths.

Signed-off-by: Mingyang Hao <200044211+mingyangHao@users.noreply.github.com>
Remove the environment-variable gates so supported output-gated Qwen3.5 attention layers use the gate-tail layout and fused QK norm/RoPE preprocessing by default. Unsupported configurations retain the guarded fallback.

Signed-off-by: Mingyang Hao <200044211+mingyangHao@users.noreply.github.com>
Signed-off-by: Mingyang Hao <200044211+mingyangHao@users.noreply.github.com>
Signed-off-by: nv-guomingz <137257613+nv-guomingz@users.noreply.github.com>
@nv-guomingz
nv-guomingz force-pushed the user/guomingz/fuse_attn branch from 5b3d188 to bf9342a Compare July 14, 2026 07:07
@nv-guomingz

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59140 [ run ] triggered by Bot. Commit: bf9342a Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59140 [ run ] completed with state FAILURE. Commit: bf9342a
/LLM/main/L0_MergeRequest_PR pipeline #47649 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

@nv-guomingz

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59225 [ run ] triggered by Bot. Commit: bf9342a Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59225 [ run ] completed with state FAILURE. Commit: bf9342a
/LLM/main/L0_MergeRequest_PR pipeline #47721 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

@nv-guomingz

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59309 [ run ] triggered by Bot. Commit: bf9342a Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59309 [ run ] completed with state SUCCESS. Commit: bf9342a
/LLM/main/L0_MergeRequest_PR pipeline #47793 completed with status: 'SUCCESS'

CI Report

Link to invocation

@nv-guomingz

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59371 [ run ] triggered by Bot. Commit: f69fded Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59371 [ run ] completed with state FAILURE. Commit: f69fded
/LLM/main/L0_MergeRequest_PR pipeline #47845 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

Guard the strided (gate-tail) QKV / fused QK-norm preprocessing so
unsupported combinations raise instead of silently corrupting output:
- Reject strided attention_input under Ulysses/CP (packed-layout preprocess).
- Reject fused QK-norm for non-bf16 inputs (bf16-only kernel).
- Reject fused QK-norm weights on the unfused context / masked-MHA fallbacks.

Signed-off-by: nv-guomingz <137257613+nv-guomingz@users.noreply.github.com>
…ycle

The gate-tail permutation ran only in post_load_weights with a permanent
flag, giving wrong output on the GMS read-only and RLHF reload paths. Split
it across the staged hooks: transform_weights() does the one-shot permute
(guarded by _weights_transformed), cache_derived_state() recomputes the layout
state and QK-norm backend config, and pre_reload_weights() resets the guard so
reloads re-permute.

Signed-off-by: nv-guomingz <137257613+nv-guomingz@users.noreply.github.com>
Gate-tail has no LoRA-aware path and asserts on non-empty lora_params, so every
LoRA request failed. Gate the layout on the absence of a LoRA config.

Signed-off-by: nv-guomingz <137257613+nv-guomingz@users.noreply.github.com>
@nv-guomingz
nv-guomingz force-pushed the user/guomingz/fuse_attn branch from f69fded to 47a0e5d Compare July 15, 2026 07:45
@nv-guomingz

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59395 [ run ] triggered by Bot. Commit: 47a0e5d Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59395 [ run ] completed with state SUCCESS. Commit: 47a0e5d
/LLM/main/L0_MergeRequest_PR pipeline #47869 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

@nv-guomingz

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59473 [ run ] triggered by Bot. Commit: 47a0e5d Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #59473 [ run ] completed with state FAILURE. Commit: 47a0e5d
/LLM/main/L0_MergeRequest_PR pipeline #47938 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

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