KEMBAR78
[None][feat] support JIT mha.cu for SPEC_DEC in runtime by jhaotingc · Pull Request #6078 · NVIDIA/TensorRT-LLM · GitHub
Skip to content

Conversation

@jhaotingc
Copy link
Collaborator

@jhaotingc jhaotingc commented Jul 16, 2025

Description

Port precompiled XQA SPEC-DEC kernel to JIT, for faster development, skipping the to-cubin step.

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 [--disable-fail-fast --skip-test --stage-list "A10-1, xxx" --gpu-type "A30, H100_PCIe" --add-multi-gpu-test --only-multi-gpu-test --disable-multi-gpu-test --post-merge --extra-stage "H100_PCIe-[Post-Merge]-1, xxx"]

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

--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-1, xxx" (OPTIONAL) : Only run the specified test stages. Examples: "A10-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.

--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. Will also run 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-[Post-Merge]-1, xxx" (OPTIONAL) : Run the ordinary L0 pre-merge pipeline and specified test stages. Examples: --extra-stage "H100_PCIe-[Post-Merge]-1, xxx".

For guidance on mapping tests to stage names, see docs/source/reference/ci-overview.md.

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.

Summary by CodeRabbit

  • New Features

    • Added support for a new specialized kernel type, enhancing compatibility and performance for additional hardware configurations.
  • Refactor

    • Improved masking logic for more explicit and portable handling of masked elements in computations.
  • Bug Fixes

    • Enhanced selection logic to ensure the most suitable implementation is chosen based on hardware and configuration parameters.

@jhaotingc
Copy link
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd
Copy link
Collaborator

PR_Github #11986 [ run ] triggered by Bot

@tensorrt-cicd
Copy link
Collaborator

PR_Github #11986 [ run ] completed with state FAILURE
/LLM/main/L0_MergeRequest_PR pipeline #8898 completed with status: 'FAILURE'

@jhaotingc jhaotingc force-pushed the enable_ampere_jit_for_spec_dec branch from 6e60be7 to bf8aaa8 Compare July 16, 2025 16:17
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 16, 2025

📝 Walkthrough

Walkthrough

The changes introduce support for a new HMMA kernel type in the decoder masked multi-head attention logic, update the kernel selection criteria to include this kernel, and refine the masking logic in the attention kernel to use a more explicit lowest floating-point value. No public interfaces or exported entity declarations were altered.

Changes

File(s) Change Summary
Masking Logic Update
cpp/kernels/xqa/mha.cu
Updated masking logic to use mha::numeric_limits<float>::lowest() instead of -INFINITY for masked accumulator values.
HMMA Kernel Support in ImplJIT
cpp/tensorrt_llm/kernels/decoderMaskedMultiheadAttention/decoderXQAImplJIT/decoderXQAImplJIT.cpp
Added support for HMMA kernel in speculative decoding, including kernel detection, parameter setup, and launch logic.
JIT Implementation Selection Update
cpp/tensorrt_llm/kernels/decoderMaskedMultiheadAttention/decoderXQARunner.cpp
Modified JIT implementation selection to include the new HMMA kernel type based on updated parameter checks.

Sequence Diagram(s)

sequenceDiagram
    participant Runner as DecoderXQARunner
    participant ImplJIT as DecoderXQAImplJIT
    participant Kernel as HMMA Kernel

    Runner->>Runner: getImplFromXQAParams(params)
    alt Supported by Hopper/MLA/Ampere XQA
        Runner->>ImplJIT: Use JIT Implementation
    else Not Supported
        Runner->>ImplJIT: Use Precompiled Implementation
    end

    ImplJIT->>ImplJIT: runImpl(...)
    alt Speculative Decoding & HMMA Kernel
        ImplJIT->>Kernel: Prepare and launch HMMA kernel with parameters
    else Other Kernel Types
        ImplJIT->>Kernel: Launch other kernel types as before
    end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Suggested reviewers

  • lucifer1004
  • symphonylyh

Note

⚡️ Unit Test Generation is now available in beta!

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


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between bf8aaa8 and 25d2b7d.

📒 Files selected for processing (3)
  • cpp/kernels/xqa/mha.cu (1 hunks)
  • cpp/tensorrt_llm/kernels/decoderMaskedMultiheadAttention/decoderXQAImplJIT/decoderXQAImplJIT.cpp (2 hunks)
  • cpp/tensorrt_llm/kernels/decoderMaskedMultiheadAttention/decoderXQARunner.cpp (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • cpp/tensorrt_llm/kernels/decoderMaskedMultiheadAttention/decoderXQARunner.cpp
  • cpp/tensorrt_llm/kernels/decoderMaskedMultiheadAttention/decoderXQAImplJIT/decoderXQAImplJIT.cpp
  • cpp/kernels/xqa/mha.cu
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Pre-commit Check
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

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

Support

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

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

CodeRabbit Commands (Invoked using PR comments)

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

Other keywords and placeholders

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

Documentation and Community

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

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

🔭 Outside diff range comments (1)
cpp/tensorrt_llm/kernels/decoderMaskedMultiheadAttention/decoderXQARunner.cpp (1)

81-90: Clarify and tighten Ampere XQA support in decoderXQARunner

The supportedByAmpereXqa condition currently only tests !xqaParams.isMLA(), enabling JIT for all non-MLA cases (regardless of spec-dec, SM version, or data type). Please:

• Update the comment above to reflect actual Ampere XQA support (pre-compiled cubins vs. JIT).
• Restrict supportedByAmpereXqa to spec-dec mode: xqaParams.multi_query_tokens.
• Limit to Ampere SM versions (e.g. 80, 86, 87): (smVersion == 80 || smVersion == 86 || smVersion == 87).
• (If needed) Restrict kv_cache_data_type similarly to Hopper’s E4M3 requirement.

Suggested diff:

- bool const supportedByAmpereXqa = (!xqaParams.isMLA());
+ bool const supportedByAmpereXqa =
+     (xqaParams.multi_query_tokens &&
+      (smVersion == 80 || smVersion == 86 || smVersion == 87) &&
+      /* optional: xqaParams.kv_cache_data_type == XQADataType::DATA_TYPE_E4M3 */);

File: cpp/tensorrt_llm/kernels/decoderMaskedMultiheadAttention/decoderXQARunner.cpp
Lines: ~84–90

🧹 Nitpick comments (2)
cpp/tensorrt_llm/kernels/decoderMaskedMultiheadAttention/decoderXQAImplJIT/decoderXQAImplJIT.cpp (2)

404-406: Fix formatting of ternary operator

The ternary operator formatting is inconsistent with the rest of the codebase. The comment placement is also confusing.

-        unsigned int maxQSeqLen = xqaParams.spec_decoding_is_generation_length_variable ? // true for ReDrafter
-            xqaParams.spec_decoding_max_generation_length
-                                                                                        : qSeqLen;
+        // true for ReDrafter
+        unsigned int maxQSeqLen = xqaParams.spec_decoding_is_generation_length_variable
+            ? xqaParams.spec_decoding_max_generation_length
+            : qSeqLen;

395-445: Consider extracting HMMA kernel launch logic into a separate method

The new code block for HMMA kernel launch is quite large and complex. For better maintainability and readability, consider extracting this logic into a separate method similar to how MLA kernel launch is handled.

This would improve:

  • Code organization and readability
  • Easier unit testing of the HMMA kernel launch logic
  • Consistency with the existing code structure
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 301b78b and bf8aaa8.

📒 Files selected for processing (3)
  • cpp/kernels/xqa/mha.cu (1 hunks)
  • cpp/tensorrt_llm/kernels/decoderMaskedMultiheadAttention/decoderXQAImplJIT/decoderXQAImplJIT.cpp (2 hunks)
  • cpp/tensorrt_llm/kernels/decoderMaskedMultiheadAttention/decoderXQARunner.cpp (1 hunks)
⏰ 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 (3)
cpp/kernels/xqa/mha.cu (1)

508-509: Change avoids NaN, but double-check soft-max semantics when an entire row is masked

Switching from the literal -INFINITY to mha::numeric_limits<float>::lowest() prevents the (-∞) – (-∞) pattern that previously produced NaN during
elem*log2e - bias (good). However, because lowest() is finite, a fully-masked row will now receive a finite maximum and will come out of the soft-max with a uniform probability mass (each element becomes exp2(0)=1). If the intent is to force such rows to contribute zero to the attention output, you will need an extra guard (e.g., detect maskFlag == false for all cols and set the row sum to zero afterwards).

Action items

  1. Confirm that “all-masked” rows cannot occur, or that the new behaviour is acceptable.
  2. Ensure mha::numeric_limits is specialised with __device__ __host__ to avoid host-only instantiation issues inside device code.

Would you like a small unit test that exercises the fully-masked-row edge case?

cpp/tensorrt_llm/kernels/decoderMaskedMultiheadAttention/decoderXQAImplJIT/decoderXQAImplJIT.cpp (2)

212-217: LGTM! Proper support for HMMA kernel in speculative decoding

The addition of isHMMAKernel detection and its inclusion in the spec dec support check is implemented correctly. This aligns with the PR objective of supporting JIT mha.cu for SPEC_DEC in runtime.


359-445: Review HMMA kernel launch parameters and dimensions

The kernel_mha definition in cpp/kernels/xqa/mha.cu (SPEC_DEC path) declares arguments in this order:

  1. qSeqLen
  2. num_k_heads
  3. headGrpSize
  4. SeqLenDataType const* qCuSeqLens
  5. (optional) uint32_t slidingWinSize
  6. float qScale
  7. OutputHead* output
  8. (optional) float const* rcpOutScale
  9. IOHead const* q
  10. MaskType const* mask
  11. KVCacheList cacheList
  12. (optional) BeamSearchParams beamSearchParams
  13. uint32_t batchSize
  14. float const* kvCacheScale
  15. uint32_t* semaphores
  16. void* scratch

In the JIT path (else if (isSpecDec && isHMMAKernel) in decoderXQAImplJIT.cpp):

  • Ensure that every appendParam(&…) call lines up exactly with one of the entries above.
  • Confirm you’re pushing exactly one pointer per non-default argument, in the same order.
  • Verify that you account for optional parameters only when their corresponding compile-time flags or runtime conditions match (e.g. slidingWindowSize, rcpOutScale, beamSearchParams).
  • The blockDim (128,1,2) yields 256 threads per CTA, matching __launch_bounds__(256,…), and gridDim {multi_block, num_kv_heads, batch_size} should mirror the device code’s use of nbCtaPerSM and CTA distribution.

Please manually cross-check the appendParam sequence in the HMMA branch against the device kernel signature to guarantee parameter count, order, and launch geometry are correct.

@jhaotingc
Copy link
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd
Copy link
Collaborator

PR_Github #12103 [ run ] triggered by Bot

@tensorrt-cicd
Copy link
Collaborator

PR_Github #12103 [ run ] completed with state FAILURE
/LLM/main/L0_MergeRequest_PR pipeline #8990 completed with status: 'FAILURE'

@jhaotingc jhaotingc force-pushed the enable_ampere_jit_for_spec_dec branch from bf8aaa8 to 25d2b7d Compare July 29, 2025 20:51
@jhaotingc jhaotingc force-pushed the enable_ampere_jit_for_spec_dec branch from 25d2b7d to 2187f6d Compare August 29, 2025 16:54
@jhaotingc jhaotingc changed the title support JIT mha.cu for SPEC_DEC in runtime [None][feat] support JIT mha.cu for SPEC_DEC in runtime Aug 29, 2025
@jhaotingc
Copy link
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd
Copy link
Collaborator

PR_Github #17019 [ run ] triggered by Bot

@tensorrt-cicd
Copy link
Collaborator

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

@jhaotingc jhaotingc force-pushed the enable_ampere_jit_for_spec_dec branch from c9a91ed to 523c539 Compare September 2, 2025 20:15
@jhaotingc
Copy link
Collaborator Author

/bot run

@tensorrt-cicd
Copy link
Collaborator

PR_Github #17402 [ run ] triggered by Bot

@tensorrt-cicd
Copy link
Collaborator

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

@jhaotingc
Copy link
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd
Copy link
Collaborator

PR_Github #17413 [ run ] triggered by Bot

@tensorrt-cicd
Copy link
Collaborator

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

@jhaotingc jhaotingc force-pushed the enable_ampere_jit_for_spec_dec branch from 523c539 to 3acdb10 Compare September 3, 2025 16:45
@jhaotingc
Copy link
Collaborator Author

/bot run

@tensorrt-cicd
Copy link
Collaborator

PR_Github #17557 [ run ] triggered by Bot

@tensorrt-cicd
Copy link
Collaborator

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

@jhaotingc jhaotingc force-pushed the enable_ampere_jit_for_spec_dec branch from 274577b to aa2f61a Compare September 18, 2025 17:53
@jhaotingc
Copy link
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd
Copy link
Collaborator

PR_Github #19216 [ run ] triggered by Bot

@tensorrt-cicd
Copy link
Collaborator

PR_Github #19216 [ run ] completed with state SUCCESS
/LLM/main/L0_MergeRequest_PR pipeline #14427 completed with status: 'SUCCESS'

@jhaotingc jhaotingc force-pushed the enable_ampere_jit_for_spec_dec branch from aa2f61a to 9d4909e Compare September 19, 2025 02:31
@jhaotingc
Copy link
Collaborator Author

/bot run --disable-fail-fast

@pengbowang-nv pengbowang-nv self-requested a review September 19, 2025 02:38
@tensorrt-cicd
Copy link
Collaborator

PR_Github #19250 [ run ] triggered by Bot

Copy link
Collaborator

@pengbowang-nv pengbowang-nv left a comment

Choose a reason for hiding this comment

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

LGTM😀

@jhaotingc jhaotingc enabled auto-merge (squash) September 19, 2025 02:42
@tensorrt-cicd
Copy link
Collaborator

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

@jhaotingc jhaotingc force-pushed the enable_ampere_jit_for_spec_dec branch from 9d4909e to 1e89b71 Compare September 19, 2025 21:58
@jhaotingc
Copy link
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd
Copy link
Collaborator

PR_Github #19385 [ run ] triggered by Bot

@tensorrt-cicd
Copy link
Collaborator

PR_Github #19385 [ run ] completed with state SUCCESS
/LLM/main/L0_MergeRequest_PR pipeline #14561 completed with status: 'SUCCESS'

Signed-off-by: Jhao-Ting Chen <jhaotingc@nvidia.com>
Signed-off-by: Jhao-Ting Chen <jhaotingc@nvidia.com>
Signed-off-by: Jhao-Ting Chen <jhaotingc@nvidia.com>
Signed-off-by: Jhao-Ting Chen <jhaotingc@nvidia.com>
Signed-off-by: Jhao-Ting Chen <jhaotingc@nvidia.com>
Signed-off-by: Jhao-Ting Chen <jhaotingc@nvidia.com>
Signed-off-by: Jhao-Ting Chen <jhaotingc@nvidia.com>
@jhaotingc jhaotingc force-pushed the enable_ampere_jit_for_spec_dec branch from 38c5c63 to ef8f199 Compare September 23, 2025 18:35
@jhaotingc
Copy link
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd
Copy link
Collaborator

PR_Github #19717 [ run ] triggered by Bot

@tensorrt-cicd
Copy link
Collaborator

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

@jhaotingc jhaotingc merged commit 220dc01 into NVIDIA:main Sep 23, 2025
5 checks passed
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