[None][feat] Add fused T5 encoder self-attention path in bertAttentionPlugin#16211
Open
zhanghuanzj wants to merge 4 commits into
Open
[None][feat] Add fused T5 encoder self-attention path in bertAttentionPlugin#16211zhanghuanzj wants to merge 4 commits into
zhanghuanzj wants to merge 4 commits into
Conversation
Contributor
📝 WalkthroughWalkthroughAdds an optional fused T5 encoder self-attention implementation with WMMA and SIMT CUDA paths, plugin integration, environment and Python configuration controls, bucket-table management, and CUDA unit tests. ChangesFused T5 attention
Estimated code review effort: 5 (Critical) | ~120 minutes Suggested reviewers: Sequence Diagram(s)sequenceDiagram
participant PluginConfig
participant BertAttentionPlugin
participant FusedT5AttentionRunner
participant CUDAKernel
PluginConfig->>BertAttentionPlugin: enable fused T5 configuration
BertAttentionPlugin->>FusedT5AttentionRunner: check runtime support and shape
BertAttentionPlugin->>FusedT5AttentionRunner: submit QKV, bias, and bucket table
FusedT5AttentionRunner->>CUDAKernel: dispatch WMMA or SIMT attention
CUDAKernel-->>BertAttentionPlugin: write attention output
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
Contributor
There was a problem hiding this comment.
Actionable comments posted: 8
🤖 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/envUtils.cpp`:
- Around line 283-288: Unify fused T5 environment parsing so all callers use the
same semantics: update getEnvEnableFusedT5Attention() and the fused T5 kernel
logic to rely on a shared helper that recognizes the supported enabled values,
including “1”, “true”, and “TRUE”. Remove duplicated parsing from
fusedT5AttentionKernels.cu and ensure bertAttentionPlugin and
FusedT5AttentionRunner::isSupported() consume this common result.
In `@cpp/tensorrt_llm/kernels/fusedT5AttentionKernels.cu`:
- Line 2: Update the copyright year in the header comment of
fusedT5AttentionKernels.cu from 2020-2025 to 2020-2026, keeping the existing
NVIDIA copyright text unchanged.
- Around line 641-652: The local isEnabledByEnv() parser is inconsistent with
the shared configuration behavior. Replace its getenv/string comparison logic
with the shared common::getEnvEnableFusedT5Attention() accessor, or update
common::getBoolEnv() consistently if true values must be supported globally,
ensuring all fused T5 attention call sites use the same parsing rules.
- Around line 685-691: Move the cudaFuncSetAttribute call out of the
per-dispatch launchOne path and initialize the max dynamic shared-memory size
once for each fusedT5AttentionWmmaKernel<T, kHeadSize> specialization, or
protect the attribute update and kernel launch with synchronization so
concurrent dispatches cannot race. Preserve the existing kernel launch behavior
and ensure every specialization is configured before use.
In `@cpp/tensorrt_llm/kernels/fusedT5AttentionKernels.h`:
- Line 2: Update the copyright header in fusedT5AttentionKernels.h to include
the current year 2026, changing the existing 2020-2025 range to 2020-2026.
In `@cpp/tests/unit_tests/kernels/fusedT5AttentionKernelsTest.cu`:
- Line 2: Update the copyright header in fusedT5AttentionKernelsTest.cu to use
the year range 2020-2026, matching the current-year requirement used by other
new files.
- Around line 273-347: Device allocations in the test leak when ASSERT_TRUE or
ASSERT_EQ returns early. Update the test’s device-buffer ownership in the
surrounding test function to use RAII, such as std::unique_ptr with a cudaFree
deleter or a scoped cleanup guard for dQkv, dBias, dOut, dTbl, dLen, and dCu,
and remove the reliance on trailing cudaFree calls so cleanup is unconditional.
- Around line 211-218: Update runNumericParityCase and the fused T5 attention
tests to execute numeric parity cases on pre-SM80 GPUs using the SIMT fallback
instead of skipping them; retain the SM80+ WMMA coverage and add or adapt a
dedicated SIMT parity test covering the same cases for SM70–79.
🪄 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: 61e447ba-99ae-4d0d-b6f1-82ada91f940b
📒 Files selected for processing (9)
cpp/tensorrt_llm/common/envUtils.cppcpp/tensorrt_llm/common/envUtils.hcpp/tensorrt_llm/kernels/fusedT5AttentionKernels.cucpp/tensorrt_llm/kernels/fusedT5AttentionKernels.hcpp/tensorrt_llm/plugins/bertAttentionPlugin/bertAttentionPlugin.cppcpp/tensorrt_llm/plugins/bertAttentionPlugin/bertAttentionPlugin.hcpp/tests/unit_tests/kernels/CMakeLists.txtcpp/tests/unit_tests/kernels/fusedT5AttentionKernelsTest.cutensorrt_llm/plugin/plugin.py
Signed-off-by: fuyu.zh <fuyu.zh@alibaba-inc.com>
Signed-off-by: fuyu.zh <fuyu.zh@alibaba-inc.com>
Signed-off-by: fuyu.zh <fuyu.zh@alibaba-inc.com>
Signed-off-by: fuyu.zh <fuyu.zh@alibaba-inc.com>
fbe8dbc to
b3d62e2
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
New Features
[None][perf] Add fused single-kernel T5 encoder self-attention, ~2.5x faster than the unfused path
Description
GPU-side optimization for the T5 / mT5 encoder self-attention pipeline in bertAttentionPlugin. The change is gated behind an env var / build flag and is disabled by default, falling back to the original unfused path whenever the shape/arch is unsupported.
Fused T5 Attention
A new kernel family (fusedT5AttentionKernels.{h,cu}, exposed via FusedT5AttentionRunner) collapses the entire T5 encoder attention into a single launch, referencing the same "fuse everything that touches the [B,H,S,S] score matrix" idea used by fused MHA kernels.
Motivation. The current encoder self-attention runs as ~6 separate ops — QKV split → cuBLAS Q·Kᵀ → invokeAddRelativeAttentionBiasUnaligned → invokeMaskedSoftmax → cuBLAS S·V → transpose. Each step round-trips through HBM, and the dominant cost is the [B,H,S,S] intermediate score tensor plus the T5 relative-position bias. For T5-base at B=32, S=512 the score tensor alone is ~6 MB/batch written and re-read every layer.
Design. One CTA per (q_tile, batch, head); everything stays in SMEM/registers between phases:
Engineering details.
Enablement (any one of):
Expected gains.
Test Coverage
Notes
Summary by CodeRabbit
New Features
use_fused_t5_attentionplugin option, disabled by default.TRTLLM_ENABLE_FUSED_T5_ATTENTION=1.Tests