Skip to content

Conversation

DomBrown
Copy link
Collaborator

@DomBrown DomBrown commented Aug 19, 2025

Summary by CodeRabbit

  • New Features

    • Enable cross-attention during generation with XQA kernels for HALF/BF16 when using KV cache.
    • Support encoder input length propagation for cross-attention processing.
    • Generation now works with paged KV cache for cross-attention.
  • Chores

    • Improved debug logging to indicate when XQA kernels are selected or skipped during generation.
    • Internal parameter wiring consolidated to streamline preprocessing and decoder info handling.

Description

Test Coverage

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.

@DomBrown DomBrown requested a review from PerkzZheng August 19, 2025 10:19
@DomBrown DomBrown self-assigned this Aug 19, 2025
Copy link
Contributor

coderabbitai bot commented Aug 19, 2025

📝 Walkthrough

Walkthrough

Enables cross-attention support in XQA during generation by propagating encoder input lengths through AttentionOp to XQAParams, centralizing preprocessing parameter construction, gating generation behavior in unfused kernels, updating decoder info wiring, adding debug logs, and requiring paged KV cache for cross-attention.

Changes

Cohort / File(s) Summary
AttentionOp cross-attention enablement and logging
cpp/tensorrt_llm/common/attentionOp.cpp
Propagates encoder_input_lengths into XQAParams; updates XQA enablement logic (allows cross-attention when HALF/BF16 with KV cache); adds debug logs for XQA selection in generation.
XQA params: encoder input lengths
cpp/tensorrt_llm/kernels/decoderMaskedMultiheadAttention/xqaParams.h
Adds int32_t const* encoder_input_lengths member; updates toString() to include it.
Unfused kernels: generation-phase cross-attention gating
cpp/tensorrt_llm/kernels/unfusedAttentionKernels/unfusedAttentionKernels_2_template.h
Conditions decoder length/offset on generation_phase; disables encoder KV-cache writes during generation; retains non-generation caching paths with FP8/8-bit handling.
XQA dispatcher: preprocessing centralization and cross-attention wiring
cpp/tensorrt_llm/kernels/xqaDispatcher.cpp
Introduces makeQKVPreprocessingParams helper; routes encoder_input_lengths to preprocessing and runner; passes cu_kv_seqlens for cross-attention; adjusts decoder info to use encoder lengths; enforces paged KV requirement for cross-attention; minor includes and type casts.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant App as Caller
  participant Attn as AttentionOp
  participant XQAD as XQA Dispatcher
  participant Prep as QKV Preprocess
  participant DecInfo as BuildDecoderInfo
  participant Run as XQA Runner
  participant Kern as XQA/Unfused Kernels

  App->>Attn: enqueueGeneration(params)
  Attn->>Attn: Select XQA (HALF/BF16 + KV cache)
  Attn->>XQAD: runImpl(XQAParams{encoder_input_lengths,...})
  XQAD->>XQAD: shouldUse(cross_attn, paged_kv?)
  alt cross-attention without paged KV
    XQAD-->>App: error (require paged KV)
  else cross-attention with paged KV
    XQAD->>Prep: makeQKVPreprocessingParams(..., encoder_input_lengths, cu_{seq,kv}_lens, gen_phase)
    XQAD->>DecInfo: Build(..., seqKVLengths=encoder_input_lengths)
    XQAD->>Run: invoke(preprocessing, runnerParams{seqLensKvPtr=encoder_input_lengths})
    Run->>Kern: Launch XQA kernels (generation_phase)
    note over Kern: Unfused kernels skip encoder KV writes during generation
    Kern-->>Run: done
  end
  XQAD-->>Attn: status
  Attn-->>App: return
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

Suggested reviewers

  • symphonylyh
  • PerkzZheng
  • amukkara

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.


📜 Recent review details

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

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 215d5b2 and 603173d.

📒 Files selected for processing (4)
  • cpp/tensorrt_llm/common/attentionOp.cpp (3 hunks)
  • cpp/tensorrt_llm/kernels/decoderMaskedMultiheadAttention/xqaParams.h (2 hunks)
  • cpp/tensorrt_llm/kernels/unfusedAttentionKernels/unfusedAttentionKernels_2_template.h (2 hunks)
  • cpp/tensorrt_llm/kernels/xqaDispatcher.cpp (7 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • cpp/tensorrt_llm/kernels/decoderMaskedMultiheadAttention/xqaParams.h
  • cpp/tensorrt_llm/kernels/unfusedAttentionKernels/unfusedAttentionKernels_2_template.h
🧰 Additional context used
📓 Path-based instructions (4)
**/*.{cpp,cxx,cc,cu,h,hpp,hxx,hh,cuh}

📄 CodeRabbit Inference Engine (CODING_GUIDELINES.md)

**/*.{cpp,cxx,cc,cu,h,hpp,hxx,hh,cuh}: In C++, close namespaces with a comment naming the namespace (e.g., } // namespace foo)
Prefer const/constexpr variables over #define for constants
Declare variables const if not modified after initialization
Use Allman brace style in C++
C++ filenames use lowerCamelCase and must be case-insensitively unique within a build target
C++ type names use UpperCamelCase
Local variables, methods, and namespaces use lowerCamelCase
Global non-static variables not in anonymous namespace use gPrefix lowerCamelCase (e.g., gExample)
Static globals or globals in anonymous namespaces use sPrefix lowerCamelCase
Locally visible static variables start with 's' (e.g., static std::once_flag sFlag;)
Member variables use mPrefix lowerCamelCase; public members may omit but are encouraged to use 'm'
Constants (enums, global/static/function-scope magic numbers) use kPREFIXED_UPPER_SNAKE (e.g., kDIGIT_NUM)
If macros are unavoidable, use UPPER_SNAKE_CASE (prefer constants over #define)
Constructor parameter that conflicts with a public member name gets trailing underscore (foo_)
Literal suffixes should be uppercase (e.g., 1234L not 1234l)
C++: use spaces only; indent 4 spaces
Run clang-format (LLVM style) before submitting; wrap lines at 120 characters
If formatting must be bypassed, use // clang-format off/on around the section
Prefer smart pointers; use unique_ptr for sole ownership, shared_ptr for shared; weak_ptr only in exceptional cases
Do not use deprecated pre-C++11 smart pointers
Use C++ style comments; avoid C comments except special inline cases; prefer // single-line
Capitalize and punctuate full-sentence comments
Follow Doxygen rules: use //! for comments and //!< for members in C++
Disable code with #if/#endif and mnemonic conditions; avoid commented-out code; avoid dead code
Do not throw exceptions across library boundaries
Use least-forceful casts; avoid removing const/volatile; avoid C-style and functional casts (except constructors); p...

Files:

  • cpp/tensorrt_llm/common/attentionOp.cpp
  • cpp/tensorrt_llm/kernels/xqaDispatcher.cpp
**/*.{cpp,cxx,cc,cu}

📄 CodeRabbit Inference Engine (CODING_GUIDELINES.md)

**/*.{cpp,cxx,cc,cu}: Avoid literal values except for 0, nullptr, true, false; use named constexpr for other literals
Place semicolon of empty for/while loop on a new line
Always use brace-delimited bodies for switch/while/do-for/if/else
Use inline C comments in argument lists when parameter meaning is unclear (e.g., /* checkForErrors = */ false)
Do not use assignment in subexpressions (e.g., if (x = y) ... is forbidden)
Switch on enums should enumerate all values and omit default to catch new values at compile time
Structure switch statements; prohibit fallthrough except between empty cases; each case ends with break or throw; return at end of case not allowed; put break inside braces for compound case
Prefer anonymous namespaces over static for internal linkage of functions
Every defined function must be called at least once (no unused methods)

Files:

  • cpp/tensorrt_llm/common/attentionOp.cpp
  • cpp/tensorrt_llm/kernels/xqaDispatcher.cpp
**/*.{h,hpp,hxx,hh,cuh,cpp,cxx,cc,cu}

📄 CodeRabbit Inference Engine (CODING_GUIDELINES.md)

Parameter names must be consistent between declarations and definitions

Files:

  • cpp/tensorrt_llm/common/attentionOp.cpp
  • cpp/tensorrt_llm/kernels/xqaDispatcher.cpp
**/*.{cpp,cxx,cc,cu,h,hpp,hxx,hh,cuh,py}

📄 CodeRabbit Inference Engine (CODING_GUIDELINES.md)

Prepend NVIDIA copyright header (current year) to all source files

Files:

  • cpp/tensorrt_llm/common/attentionOp.cpp
  • cpp/tensorrt_llm/kernels/xqaDispatcher.cpp
🧠 Learnings (2)
📚 Learning: 2025-08-15T06:46:53.780Z
Learnt from: eopXD
PR: NVIDIA/TensorRT-LLM#6767
File: cpp/tensorrt_llm/batch_manager/kvCacheManager.cpp:0-0
Timestamp: 2025-08-15T06:46:53.780Z
Learning: In the TensorRT-LLM KV cache manager, SWA (Sliding Window Attention) combined with beam search is currently in a broken/non-functional state and is planned for future rework. During preparatory refactoring phases, code related to SWA+beam search may intentionally remain in a non-working state until the broader rework is completed.

Applied to files:

  • cpp/tensorrt_llm/kernels/xqaDispatcher.cpp
📚 Learning: 2025-08-14T21:04:50.208Z
Learnt from: thorjohnsen
PR: NVIDIA/TensorRT-LLM#6910
File: cpp/tensorrt_llm/batch_manager/kvCacheManager.cpp:0-0
Timestamp: 2025-08-14T21:04:50.208Z
Learning: In KV cache onboarding logic during prefill in cpp/tensorrt_llm/batch_manager/kvCacheManager.cpp, when calculating which blocks fall within the attention window, use getTokensPerBlock() to advance token indices rather than block->getUniqueTokens().size(), because the calculation needs to consider the post-prefill state where blocks will be filled to capacity, not their current token count.

Applied to files:

  • cpp/tensorrt_llm/kernels/xqaDispatcher.cpp
⏰ 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 (9)
cpp/tensorrt_llm/common/attentionOp.cpp (3)

286-290: LGTM! Cross-attention parameter propagation is properly implemented.

The addition of encoder_input_lengths to XQAParams enables proper cross-attention support. The implementation correctly propagates the encoder input lengths from generation parameters to XQAParams.


2210-2238: Good addition of debug logging for XQA kernel selection.

The addition of both success and fallback debug log messages improves observability when debugging XQA kernel selection during generation. This will help with troubleshooting cross-attention support in production.


2759-2761: Proper extension of XQA enablement logic for cross-attention.

The removal of the cross-attention exclusion (&& !isCrossAttention()) is correct. The XQA dispatcher now properly handles cross-attention cases with the paged KV cache requirement, as validated in the dispatcher's shouldUse method.

cpp/tensorrt_llm/kernels/xqaDispatcher.cpp (6)

19-21: LGTM! Added necessary includes for cross-attention support.

The addition of decoderXQAImplCommon.h and <cstdint> includes is appropriate for supporting the new XQA common utilities and fixed-width integer types used in the cross-attention implementation.


43-122: Well-structured helper function for QKV preprocessing parameters.

The new makeQKVPreprocessingParams helper function effectively centralizes the construction of preprocessing parameters, improving code maintainability. The function properly handles:

  • Cross-attention-specific encoder sequence lengths (line 117)
  • Generation phase settings (line 110)
  • All necessary buffer and scalar parameters

This centralization reduces duplication and potential errors in parameter setup.


339-340: Correct wiring of encoder input lengths for decoder info.

The conditional assignment of seqKVLengths correctly uses encoder_input_lengths when cross-attention is active, ensuring proper sequence length handling in the decoder info construction.


223-227: Appropriate validation for cross-attention paged KV requirement.

The check correctly ensures that cross-attention requires paged KV cache in TRTLLM-GEN kernels. The error message clearly communicates the requirement.


427-427: Proper K/V sequence length selection for cross-attention.

The conditional assignment correctly selects encoder_input_lengths for cross-attention cases, ensuring the TRTLLM-GEN runner receives the appropriate sequence lengths.


360-377: Ensure removePadding is false for cross-attention so invokeBuildDecoderInfo populates cu_kv_seq_lens

  • buildXQALaunchParams unconditionally allocates
    launchParams.cu_kv_seq_lens (decoderXQAImplCommon.h around line 269).
  • The buffer is actually filled by the CUDA kernel in invokeBuildDecoderInfo, which only runs when
    decoder_params.isBuildDecoderInfoKernelNeeded() returns true (gptKernels.h:isBuildDecoderInfoKernelNeeded()).
  • In xqaDispatcher.cpp (lines 359–365), if that check is false, local cu_kv_seqlens remains nullptr and is passed as such to makeQKVPreprocessingParams.

Please verify that for cross-attention (where you need KV offsets) the removePadding flag inside BuildDecoderInfoParams is set to false so that invokeBuildDecoderInfo actually runs and populates launchParams.cu_kv_seq_lens before use. If skipping the kernel is intentional in some cross-attention scenarios, add a comment to document why cu_kv_seqlens == nullptr is acceptable.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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
🪧 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.
    • 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.
  • 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 the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

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

CodeRabbit Commands (Invoked using PR/Issue comments)

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

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.

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • 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.

@DomBrown DomBrown changed the title [TRTLLM-7348] feat: Enable Cross-Attention to use XQA kernels for Whisper [TRTLLM-7348] [feat] Enable Cross-Attention to use XQA kernels for Whisper Aug 19, 2025
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: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
cpp/tensorrt_llm/common/attentionOp.cpp (1)

1431-1471: Potential OOB write: encoderPaddingOffsets may point to a zero-sized buffer under FMHA

When mEnableContextFMHA is true, encoder_padding_offset_size is computed as 0, but decoder_params.encoderPaddingOffsets is still set to encoder_padding_offset for cross-attn. If BuildDecoderInfo writes into encoderPaddingOffsets, this can corrupt memory.

Safer is to pass nullptr for encoderPaddingOffsets when FMHA is enabled (consistent with the generation path in XQA). Apply this change:

-    int* encoder_padding_offset = (mEnableContextFMHA && !isCrossAttention()) ? nullptr
-        : reinterpret_cast<int*>(nextWorkspacePtr(workspace_byte_ptr, offset, encoder_padding_offset_size));
+    int* encoder_padding_offset = (mEnableContextFMHA && !isCrossAttention()) ? nullptr
+        : reinterpret_cast<int*>(nextWorkspacePtr(workspace_byte_ptr, offset, encoder_padding_offset_size));

     // ...
-    decoder_params.encoderPaddingOffsets
-        = isCrossAttention() ? encoder_padding_offset : nullptr; // cross attention takes offsets from encoder inputs
+    // Only needed in unfused cross-attention; FMHA path should pass nullptr.
+    decoder_params.encoderPaddingOffsets = (isCrossAttention() && !mEnableContextFMHA)
+        ? encoder_padding_offset
+        : nullptr;

Optional: For clarity, also set the new flag so logs/kernel gates reflect intent:

+    decoder_params.isCrossAttention = isCrossAttention();
🧹 Nitpick comments (3)
cpp/tensorrt_llm/kernels/gptKernels.h (1)

225-274: Add isCrossAttention to toString() for better debuggability

toString() logs many fields but omits the new isCrossAttention flag. Including it will simplify debugging and log triage when cross-attn paths are active.

Apply this diff to include the flag:

         ss << "removePadding: " << removePadding << std::endl;
+        ss << "isCrossAttention: " << (isCrossAttention ? "true" : "false") << std::endl;
         ss << "attentionMaskType: " << static_cast<int>(attentionMaskType) << std::endl;
cpp/tensorrt_llm/common/attentionOp.cpp (1)

1498-1529: Unfused cross-attn mask build: verify host-side T initialization for half/bfloat16

The host mask is built with std::vector h_attention_mask(..., 1.). For half/bfloat16, implicit host-side initialization via a double literal may not be portable across toolchains. If you’ve seen sporadic build issues, consider constructing via a typed constant or filling via a small helper that writes the correct bit-pattern for T.

If everything builds cleanly across supported compilers, feel free to ignore.

cpp/tensorrt_llm/kernels/xqaDispatcher.cpp (1)

43-131: Helper extraction is good; remove duplicated assignments in preprocessing params

makeQKVPreprocessingParams centralizes construction well. There’s a small duplication: rotary_embedding_inv_freq, rotary_coef_cache_buffer, kvScaleOrigQuant, spec_decoding_position_offsets, and logn_scaling are assigned twice (first in the common section, then again in the cross-attention-only section). This is benign but confusing.

Trim the duplicates in the cross-attention section to reduce maintenance overhead.

     // Cross-attention only.
-    preprocessingParms.cu_kv_seq_lens = cu_kv_seqlens;
-    preprocessingParms.encoder_seq_lens = params.encoder_input_lengths;
-    preprocessingParms.rotary_embedding_inv_freq = rotary_inv_freq_buf;
-    preprocessingParms.rotary_coef_cache_buffer = params.rotary_cos_sin;
-
-    preprocessingParms.kvScaleOrigQuant = params.kv_scale_orig_quant;
-    preprocessingParms.spec_decoding_position_offsets = nullptr;
-    preprocessingParms.logn_scaling = params.logn_scaling_ptr;
+    preprocessingParms.cu_kv_seq_lens = cu_kv_seqlens;
+    preprocessingParms.encoder_seq_lens = params.encoder_input_lengths;
 
     // Not available in generation phase
     preprocessingParms.mrope_rotary_cos_sin = nullptr;
📜 Review details

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

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 953f4fd and b8918e5.

📒 Files selected for processing (5)
  • cpp/tensorrt_llm/common/attentionOp.cpp (3 hunks)
  • cpp/tensorrt_llm/kernels/decoderMaskedMultiheadAttention/xqaParams.h (2 hunks)
  • cpp/tensorrt_llm/kernels/gptKernels.h (2 hunks)
  • cpp/tensorrt_llm/kernels/unfusedAttentionKernels/unfusedAttentionKernels_2_template.h (2 hunks)
  • cpp/tensorrt_llm/kernels/xqaDispatcher.cpp (7 hunks)
🧰 Additional context used
📓 Path-based instructions (5)
**/*.{cpp,cxx,cc,cu,h,hpp,hxx,hh,cuh}

📄 CodeRabbit Inference Engine (CODING_GUIDELINES.md)

**/*.{cpp,cxx,cc,cu,h,hpp,hxx,hh,cuh}: In C++, close namespaces with a comment naming the namespace (e.g., } // namespace foo)
Prefer const/constexpr variables over #define for constants
Declare variables const if not modified after initialization
Use Allman brace style in C++
C++ filenames use lowerCamelCase and must be case-insensitively unique within a build target
C++ type names use UpperCamelCase
Local variables, methods, and namespaces use lowerCamelCase
Global non-static variables not in anonymous namespace use gPrefix lowerCamelCase (e.g., gExample)
Static globals or globals in anonymous namespaces use sPrefix lowerCamelCase
Locally visible static variables start with 's' (e.g., static std::once_flag sFlag;)
Member variables use mPrefix lowerCamelCase; public members may omit but are encouraged to use 'm'
Constants (enums, global/static/function-scope magic numbers) use kPREFIXED_UPPER_SNAKE (e.g., kDIGIT_NUM)
If macros are unavoidable, use UPPER_SNAKE_CASE (prefer constants over #define)
Constructor parameter that conflicts with a public member name gets trailing underscore (foo_)
Literal suffixes should be uppercase (e.g., 1234L not 1234l)
C++: use spaces only; indent 4 spaces
Run clang-format (LLVM style) before submitting; wrap lines at 120 characters
If formatting must be bypassed, use // clang-format off/on around the section
Prefer smart pointers; use unique_ptr for sole ownership, shared_ptr for shared; weak_ptr only in exceptional cases
Do not use deprecated pre-C++11 smart pointers
Use C++ style comments; avoid C comments except special inline cases; prefer // single-line
Capitalize and punctuate full-sentence comments
Follow Doxygen rules: use //! for comments and //!< for members in C++
Disable code with #if/#endif and mnemonic conditions; avoid commented-out code; avoid dead code
Do not throw exceptions across library boundaries
Use least-forceful casts; avoid removing const/volatile; avoid C-style and functional casts (except constructors); p...

Files:

  • cpp/tensorrt_llm/kernels/decoderMaskedMultiheadAttention/xqaParams.h
  • cpp/tensorrt_llm/kernels/gptKernels.h
  • cpp/tensorrt_llm/kernels/unfusedAttentionKernels/unfusedAttentionKernels_2_template.h
  • cpp/tensorrt_llm/common/attentionOp.cpp
  • cpp/tensorrt_llm/kernels/xqaDispatcher.cpp
**/*.{h,hpp,hxx,hh,cuh,cpp,cxx,cc,cu}

📄 CodeRabbit Inference Engine (CODING_GUIDELINES.md)

Parameter names must be consistent between declarations and definitions

Files:

  • cpp/tensorrt_llm/kernels/decoderMaskedMultiheadAttention/xqaParams.h
  • cpp/tensorrt_llm/kernels/gptKernels.h
  • cpp/tensorrt_llm/kernels/unfusedAttentionKernels/unfusedAttentionKernels_2_template.h
  • cpp/tensorrt_llm/common/attentionOp.cpp
  • cpp/tensorrt_llm/kernels/xqaDispatcher.cpp
**/*.{h,hpp,hxx,hh,cuh}

📄 CodeRabbit Inference Engine (CODING_GUIDELINES.md)

Header files must use include guards named TRTLLM__H without underscores prefix/suffix (e.g., TRTLLM_FOO_BAR_HELLO_H)

Files:

  • cpp/tensorrt_llm/kernels/decoderMaskedMultiheadAttention/xqaParams.h
  • cpp/tensorrt_llm/kernels/gptKernels.h
  • cpp/tensorrt_llm/kernels/unfusedAttentionKernels/unfusedAttentionKernels_2_template.h
**/*.{cpp,cxx,cc,cu,h,hpp,hxx,hh,cuh,py}

📄 CodeRabbit Inference Engine (CODING_GUIDELINES.md)

Prepend NVIDIA copyright header (current year) to all source files

Files:

  • cpp/tensorrt_llm/kernels/decoderMaskedMultiheadAttention/xqaParams.h
  • cpp/tensorrt_llm/kernels/gptKernels.h
  • cpp/tensorrt_llm/kernels/unfusedAttentionKernels/unfusedAttentionKernels_2_template.h
  • cpp/tensorrt_llm/common/attentionOp.cpp
  • cpp/tensorrt_llm/kernels/xqaDispatcher.cpp
**/*.{cpp,cxx,cc,cu}

📄 CodeRabbit Inference Engine (CODING_GUIDELINES.md)

**/*.{cpp,cxx,cc,cu}: Avoid literal values except for 0, nullptr, true, false; use named constexpr for other literals
Place semicolon of empty for/while loop on a new line
Always use brace-delimited bodies for switch/while/do-for/if/else
Use inline C comments in argument lists when parameter meaning is unclear (e.g., /* checkForErrors = */ false)
Do not use assignment in subexpressions (e.g., if (x = y) ... is forbidden)
Switch on enums should enumerate all values and omit default to catch new values at compile time
Structure switch statements; prohibit fallthrough except between empty cases; each case ends with break or throw; return at end of case not allowed; put break inside braces for compound case
Prefer anonymous namespaces over static for internal linkage of functions
Every defined function must be called at least once (no unused methods)

Files:

  • cpp/tensorrt_llm/common/attentionOp.cpp
  • cpp/tensorrt_llm/kernels/xqaDispatcher.cpp
🧠 Learnings (1)
📚 Learning: 2025-08-14T21:04:50.208Z
Learnt from: thorjohnsen
PR: NVIDIA/TensorRT-LLM#6910
File: cpp/tensorrt_llm/batch_manager/kvCacheManager.cpp:0-0
Timestamp: 2025-08-14T21:04:50.208Z
Learning: In KV cache onboarding logic during prefill in cpp/tensorrt_llm/batch_manager/kvCacheManager.cpp, when calculating which blocks fall within the attention window, use getTokensPerBlock() to advance token indices rather than block->getUniqueTokens().size(), because the calculation needs to consider the post-prefill state where blocks will be filled to capacity, not their current token count.

Applied to files:

  • cpp/tensorrt_llm/kernels/unfusedAttentionKernels/unfusedAttentionKernels_2_template.h
  • cpp/tensorrt_llm/kernels/xqaDispatcher.cpp
🔇 Additional comments (10)
cpp/tensorrt_llm/kernels/gptKernels.h (1)

189-221: Good addition: explicit cross-attention gating for BuildDecoderInfo

Adding isCrossAttention with a default initializer and short-circuiting isBuildDecoderInfoKernelNeeded() on it is the right call to ensure decoder-info is built for cross-attention scenarios, regardless of other gates.

cpp/tensorrt_llm/kernels/unfusedAttentionKernels/unfusedAttentionKernels_2_template.h (1)

1351-1460: Cross-attn generation gating looks correct; confirm decoder offset semantics

  • Setting decoder_seq_len=1 and decoder_seq_offset=batch_idx under generation_phase aligns with the remove-padding layout used in generation paths.
  • Gating encoder KV-cache writes with if (!params.generation_phase) prevents redundant writes during gen and is consistent with “encoder KV is static” semantics for cross-attn.

Please double-check that qkv_input layout for generation is indeed [batch_beam, ...] when remove_padding is true so that decoder_seq_offset=batch_idx addresses the correct Q token in all gen modes (including beam search). If not, we should derive the offset from cu_seq_lens for robustness.

cpp/tensorrt_llm/kernels/decoderMaskedMultiheadAttention/xqaParams.h (2)

109-111: Propagating encoder_input_lengths into XQAParams is appropriate

This field is necessary for cross-attention with XQA and aligns with the updates in attentionOp and xqaDispatcher. Good placement and default nullptr.


181-181: Nice: toString() now logs encoder_input_lengths

Logging encoder_input_lengths will help diagnose sequence length mismatches in cross-attention scenarios.

cpp/tensorrt_llm/common/attentionOp.cpp (3)

288-290: Correct propagation of encoder_input_lengths to XQA

Mapping generationsParams.encoder_input_lengths into xqaParams ensures XQA sees the encoder seqlens for cross-attention. LGTM.


2216-2219: Helpful debug: log when XQA is not selected in generation

Non-intrusive and useful for runtime diagnostics.


2738-2740: Relaxed XQA enablement is sensible

Enabling XQA when type is HALF/BF16 and KV cache is used (independent of cross-attn) matches the dispatcher’s capability checks. The final gate still depends on shouldUse() and isSupported(), so low risk.

cpp/tensorrt_llm/kernels/xqaDispatcher.cpp (3)

349-371: Correct: feed encoder lengths and cross-attn metadata into decoder-info

Using encoder_input_lengths for seqKVLengths and setting isCrossAttention, maxEncoderQSeqLength, and encoderPaddingOffsets=nullptr is consistent with the cross-attention generation path and avoids building a square mask.


392-395: Good reuse: centralized preprocessing param construction

Swapping in makeQKVPreprocessingParams reduces duplication and keeps cross-attn wiring in one place.


444-445: Right choice: pass encoder lengths to TLLM-GEN runner

tllmRunnerParams.seqLensKvPtr = encoder_input_lengths in cross-attn ensures the runner sees correct KV lengths.

@DomBrown DomBrown force-pushed the dev/xqa_cross_attention branch from 3aad9b2 to 215d5b2 Compare August 19, 2025 10:45
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: 0

🧹 Nitpick comments (6)
cpp/tensorrt_llm/kernels/gptKernels.h (3)

189-189: Good addition: explicit cross-attention flag in params

isCrossAttention in BuildDecoderInfoParams provides a clear hook to gate decoder-info building for cross-attn paths. Consider adding a short comment to document when it should be set (e.g., generation with encoder KV).

Apply this diff to document intent:

-    bool isCrossAttention{false};
+    // True when running cross-attention (e.g., decoder attends to encoder KV); triggers decoder-info build for KV paths.
+    bool isCrossAttention{false};

217-220: Ensures decoder-info kernel runs for cross-attention

Unconditionally returning true when isCrossAttention is set is the right call; cu_kv_seqlens and related buffers are required in XQA cross-attn paths. If you want to make this more obvious to future readers, consider moving this check to the top of the method.


272-272: toString: consider readability for booleans

isCrossAttention will print as 0/1 unless boolalpha is enabled. If logs are for human consumption (and not parsed), consider enabling std::boolalpha once at the start of toString().

Apply this diff if you prefer human-readable booleans:

     std::string toString() const
     {
         std::stringstream ss;
+        ss << std::boolalpha;
         ss << "BuildDecoderInfoParams ====================" << std::endl;
cpp/tensorrt_llm/kernels/xqaDispatcher.cpp (3)

80-83: Ambiguous duplicate: kvScaleOrigQuant vs kv_scale_quant_orig

Both preprocessingParms.kv_scale_quant_orig (set above) and preprocessingParms.kvScaleOrigQuant (set here) are being populated from params.kv_scale_orig_quant/params.kv_scale_quant_orig (note the swapped word order). This looks like either:

  • Two different fields that mean different things (INT8 vs FP8 scales), or
  • A naming inconsistency/duplication.

Please confirm the intended fields and unify naming/assignment. Mismatched names tend to compile but wire the wrong buffer at runtime.


372-380: Use gated cu_kv_seqlens consistently to avoid stale/unused pointers

You correctly gate cu_kv_seqlens to nullptr unless the decoder-info kernel runs. However, tllmRunnerParams.cumSeqLensKvPtr unconditionally uses launchParams.cu_kv_seq_lens, which may not be built/populated when the kernel is skipped. For consistency with Q, pass the gated cu_kv_seqlens.

This avoids propagating an uninitialized or unnecessary pointer to the runner.

Apply this diff:

-        tllmRunnerParams.cumSeqLensKvPtr = reinterpret_cast<int const*>(launchParams.cu_kv_seq_lens);
+        tllmRunnerParams.cumSeqLensKvPtr = cu_kv_seqlens;

Also applies to: 430-433


387-390: Centralized preprocessing param construction improves maintainability

Replacing inline construction with makeQKVPreprocessingParams is a solid refactor. Once the scale wiring is verified (see earlier comments), this will simplify future changes.

📜 Review details

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

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between b8918e5 and 3aad9b2.

📒 Files selected for processing (2)
  • cpp/tensorrt_llm/kernels/gptKernels.h (3 hunks)
  • cpp/tensorrt_llm/kernels/xqaDispatcher.cpp (7 hunks)
🧰 Additional context used
📓 Path-based instructions (5)
**/*.{cpp,cxx,cc,cu,h,hpp,hxx,hh,cuh}

📄 CodeRabbit Inference Engine (CODING_GUIDELINES.md)

**/*.{cpp,cxx,cc,cu,h,hpp,hxx,hh,cuh}: In C++, close namespaces with a comment naming the namespace (e.g., } // namespace foo)
Prefer const/constexpr variables over #define for constants
Declare variables const if not modified after initialization
Use Allman brace style in C++
C++ filenames use lowerCamelCase and must be case-insensitively unique within a build target
C++ type names use UpperCamelCase
Local variables, methods, and namespaces use lowerCamelCase
Global non-static variables not in anonymous namespace use gPrefix lowerCamelCase (e.g., gExample)
Static globals or globals in anonymous namespaces use sPrefix lowerCamelCase
Locally visible static variables start with 's' (e.g., static std::once_flag sFlag;)
Member variables use mPrefix lowerCamelCase; public members may omit but are encouraged to use 'm'
Constants (enums, global/static/function-scope magic numbers) use kPREFIXED_UPPER_SNAKE (e.g., kDIGIT_NUM)
If macros are unavoidable, use UPPER_SNAKE_CASE (prefer constants over #define)
Constructor parameter that conflicts with a public member name gets trailing underscore (foo_)
Literal suffixes should be uppercase (e.g., 1234L not 1234l)
C++: use spaces only; indent 4 spaces
Run clang-format (LLVM style) before submitting; wrap lines at 120 characters
If formatting must be bypassed, use // clang-format off/on around the section
Prefer smart pointers; use unique_ptr for sole ownership, shared_ptr for shared; weak_ptr only in exceptional cases
Do not use deprecated pre-C++11 smart pointers
Use C++ style comments; avoid C comments except special inline cases; prefer // single-line
Capitalize and punctuate full-sentence comments
Follow Doxygen rules: use //! for comments and //!< for members in C++
Disable code with #if/#endif and mnemonic conditions; avoid commented-out code; avoid dead code
Do not throw exceptions across library boundaries
Use least-forceful casts; avoid removing const/volatile; avoid C-style and functional casts (except constructors); p...

Files:

  • cpp/tensorrt_llm/kernels/gptKernels.h
  • cpp/tensorrt_llm/kernels/xqaDispatcher.cpp
**/*.{h,hpp,hxx,hh,cuh,cpp,cxx,cc,cu}

📄 CodeRabbit Inference Engine (CODING_GUIDELINES.md)

Parameter names must be consistent between declarations and definitions

Files:

  • cpp/tensorrt_llm/kernels/gptKernels.h
  • cpp/tensorrt_llm/kernels/xqaDispatcher.cpp
**/*.{h,hpp,hxx,hh,cuh}

📄 CodeRabbit Inference Engine (CODING_GUIDELINES.md)

Header files must use include guards named TRTLLM__H without underscores prefix/suffix (e.g., TRTLLM_FOO_BAR_HELLO_H)

Files:

  • cpp/tensorrt_llm/kernels/gptKernels.h
**/*.{cpp,cxx,cc,cu,h,hpp,hxx,hh,cuh,py}

📄 CodeRabbit Inference Engine (CODING_GUIDELINES.md)

Prepend NVIDIA copyright header (current year) to all source files

Files:

  • cpp/tensorrt_llm/kernels/gptKernels.h
  • cpp/tensorrt_llm/kernels/xqaDispatcher.cpp
**/*.{cpp,cxx,cc,cu}

📄 CodeRabbit Inference Engine (CODING_GUIDELINES.md)

**/*.{cpp,cxx,cc,cu}: Avoid literal values except for 0, nullptr, true, false; use named constexpr for other literals
Place semicolon of empty for/while loop on a new line
Always use brace-delimited bodies for switch/while/do-for/if/else
Use inline C comments in argument lists when parameter meaning is unclear (e.g., /* checkForErrors = */ false)
Do not use assignment in subexpressions (e.g., if (x = y) ... is forbidden)
Switch on enums should enumerate all values and omit default to catch new values at compile time
Structure switch statements; prohibit fallthrough except between empty cases; each case ends with break or throw; return at end of case not allowed; put break inside braces for compound case
Prefer anonymous namespaces over static for internal linkage of functions
Every defined function must be called at least once (no unused methods)

Files:

  • cpp/tensorrt_llm/kernels/xqaDispatcher.cpp
🧠 Learnings (2)
📓 Common learnings
Learnt from: thorjohnsen
PR: NVIDIA/TensorRT-LLM#6910
File: cpp/tensorrt_llm/batch_manager/kvCacheManager.cpp:0-0
Timestamp: 2025-08-14T21:04:50.208Z
Learning: In KV cache onboarding logic during prefill in cpp/tensorrt_llm/batch_manager/kvCacheManager.cpp, when calculating which blocks fall within the attention window, use getTokensPerBlock() to advance token indices rather than block->getUniqueTokens().size(), because the calculation needs to consider the post-prefill state where blocks will be filled to capacity, not their current token count.
📚 Learning: 2025-08-14T21:04:50.208Z
Learnt from: thorjohnsen
PR: NVIDIA/TensorRT-LLM#6910
File: cpp/tensorrt_llm/batch_manager/kvCacheManager.cpp:0-0
Timestamp: 2025-08-14T21:04:50.208Z
Learning: In KV cache onboarding logic during prefill in cpp/tensorrt_llm/batch_manager/kvCacheManager.cpp, when calculating which blocks fall within the attention window, use getTokensPerBlock() to advance token indices rather than block->getUniqueTokens().size(), because the calculation needs to consider the post-prefill state where blocks will be filled to capacity, not their current token count.

Applied to files:

  • cpp/tensorrt_llm/kernels/xqaDispatcher.cpp
🔇 Additional comments (7)
cpp/tensorrt_llm/kernels/xqaDispatcher.cpp (7)

19-22: Includes look correct for new helper and types

Adding decoderXQAImplCommon.h and <cstdint> is appropriate for the new preprocessing helper and fixed-width ints.


63-71: Potential mix-up between Q and KV FP8 scale sources

q_scale_quant_orig is initialized from params.kv_scale_quant_orig. That looks suspicious: Q’s FP8 quantization scale should typically come from a Q-specific source (e.g., params.q_scale_quant_orig) rather than KV’s. Also, q_scale_quant_orig is only set inside hasFp8KvCache(), which couples Q quantization to KV cache FP8 mode.

Please verify:

  • Whether there’s a distinct Q scale in XQAParams for FP8 Q preprocessing.
  • Whether Q’s scale should be independent from KV’s FP8 mode.

If a Q-specific scale exists, route it here to avoid applying KV’s scale to Q.


99-103: Double-check fmha bmm1 scale formula

fmha_host_bmm1_scale = 1 / (sqrt(head_size) * q_scaling) divides by q_scaling. In most FMHA paths, the BMM1 scale is (q_scaling / sqrt(head_size)). If q_scaling here is the usual multiplier, the current formula inverts it.

Please verify against your existing MMHA/XQA codepaths to ensure consistency; a wrong scale will silently degrade output quality.


115-123: Cross-attention specifics wired correctly in preprocessing

Setting cu_kv_seq_lens and encoder_seq_lens, and nulling non-generation-phase fields is aligned with the generation-only cross-attn path.


227-231: Good guard: cross-attn requires paged KV cache in TRTLLM-GEN

The fallback with a clear debug message helps avoid unsupported configurations.


343-365: Decoder-info params correctly adapted for cross-attention

  • seqKVLengths sourced from encoder_input_lengths under cross-attn is correct.
  • isCrossAttention flag and maxEncoderQSeqLength set from max_past_kv_length align with the new gating in BuildDecoderInfo.
  • encoderPaddingOffsets = nullptr for generation-phase makes sense.

LGTM.


439-440: KV length source selection looks correct

Using encoder_input_lengths under cross-attn for seqLensKvPtr matches the rest of the wiring in this PR.

@DomBrown
Copy link
Collaborator Author

/bot run

@tensorrt-cicd
Copy link
Collaborator

PR_Github #15772 [ run ] triggered by Bot

Copy link
Collaborator

@PerkzZheng PerkzZheng left a comment

Choose a reason for hiding this comment

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

Others LGTM. Thanks for the great work!

@PerkzZheng
Copy link
Collaborator

@wu6u3tw do we have model tests (whisper) to guard this ? just want that we won't break it in the future. Thanks.

@DomBrown
Copy link
Collaborator Author

/bot kill

@DomBrown DomBrown force-pushed the dev/xqa_cross_attention branch from 215d5b2 to b36910c Compare August 19, 2025 14:23
@DomBrown DomBrown force-pushed the dev/xqa_cross_attention branch from b36910c to 603173d Compare August 19, 2025 14:24
@DomBrown
Copy link
Collaborator Author

/bot kill

@tensorrt-cicd
Copy link
Collaborator

PR_Github #15795 [ kill ] triggered by Bot

@tensorrt-cicd
Copy link
Collaborator

PR_Github #15772 [ run ] completed with state ABORTED

@tensorrt-cicd
Copy link
Collaborator

PR_Github #15795 [ kill ] completed with state SUCCESS
Successfully killed previous jobs for commit 603173d

@DomBrown
Copy link
Collaborator Author

/bot run

@tensorrt-cicd
Copy link
Collaborator

PR_Github #15797 [ run ] triggered by Bot

@DomBrown DomBrown enabled auto-merge (squash) August 19, 2025 21:41
@tensorrt-cicd
Copy link
Collaborator

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

@DomBrown DomBrown merged commit 92daec1 into NVIDIA:main Aug 20, 2025
5 checks passed
zhou-yuxin pushed a commit to zhou-yuxin/TensorRT-LLM that referenced this pull request Aug 21, 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