[Feature] Add Triton unified attention kernel for deterministic inference#6795
Open
gongweibao wants to merge 2 commits intoPaddlePaddle:developfrom
Open
[Feature] Add Triton unified attention kernel for deterministic inference#6795gongweibao wants to merge 2 commits intoPaddlePaddle:developfrom
gongweibao wants to merge 2 commits intoPaddlePaddle:developfrom
Conversation
…ence Add a Triton-based unified extend attention kernel that processes both prefix (cached) and extend (new) KV tokens through a single kernel with unified kv_indices, ensuring identical accumulation order regardless of cache hit/miss patterns. Key components: - _fwd_kernel_unified: Triton JIT kernel with online softmax, paged KV cache support, and causal masking for prefix+extend - Index building utilities: triton_cumsum_with_zero_prefix, build_kv_indices_from_block_tables, build_unified_kv_indices, _scatter_extend_kv_indices_kernel (all CUDA Graph compatible) - pre_cache_len_concat_triton: GPU-only replacement for C++ op - Reference implementations (_ref variants) for correctness validation - Comprehensive tests: kernel correctness, split invariance, determinism, production-scale, cross-validation Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
Thanks for your contribution! |
|
gongweibao seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
Replace triple Python for-loop with paddle.where vectorized mask in naive_attention and _build_causal_mask. seq4096 test: 2m39s -> 6s. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #6795 +/- ##
==========================================
Coverage ? 72.36%
==========================================
Files ? 396
Lines ? 54956
Branches ? 8635
==========================================
Hits ? 39770
Misses ? 12362
Partials ? 2824
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
Motivation
为确定性推理模式提供 Triton unified attention kernel。该 kernel 通过 unified kv_indices 将 prefix(cached)和 extend(new)KV tokens 统一处理,确保无论 prefix cache 命中与否,累加顺序完全一致,从而实现 split-invariant 的确定性 attention 输出。
本 PR 是纯算子层面的实现,不涉及推理流程集成(流程集成见后续 PR)。
Modifications
新增文件:
fastdeploy/model_executor/layers/attention/triton_ops/__init__.py— 包导出fastdeploy/model_executor/layers/attention/triton_ops/unified_extend_attention.py— 核心实现:_fwd_kernel_unified: Triton JIT kernel,online softmax + paged KV cache + causal masktriton_cumsum_with_zero_prefix,build_kv_indices_from_block_tables,build_unified_kv_indices,_scatter_extend_kv_indices_kernel(均 CUDA Graph 兼容)pre_cache_len_concat_triton: GPU-only Triton 替代 C++ op_ref后缀)用于正确性验证tests/deterministic/test_unified_extend_attention.py— kernel 正确性单测tests/deterministic/test_build_triton_indices.py— indices 构建逻辑单测Usage or Command
Accuracy Tests
测试覆盖:
Checklist
pre-commitbefore commit.releasebranch, make sure the PR has been submitted to thedevelopbranch, then cherry-pick it to thereleasebranch with the[Cherry-Pick]PR tag.🤖 Generated with Claude Code