[release/0.4] Fuse HCA inverse RoPE into the VHA postmix, and the sparse-attn KV index remap - #1843
Merged
DanielSun11 merged 1 commit intoAug 24, 2026
Conversation
…rse-attention KV index remap (PaddlePaddle#1772) Cherry-pick of PaddlePaddle#1772 from develop. Conflicts resolved (release/0.4 keeps the padded-head / sub-512-latent sparse-attn path that develop does not have): - src/paddlefleet/fusions/csa_sparse_attn.py: kept release's kernel-side head count `kh` for `lse_flat` and release's `compacted_idxs`-driven `topk_length` recount, while taking develop's `local_to_global_flat(..., fused=ctx.global_kv_idx_remap_fusion)` dispatch; docstring keeps both the new arg doc and release's head-tile / latent-padding note. - tests/.../test_csa_sparse_attn_backends.py: kept release's `compacted_idxs` field on the fake ctx (the `global_kv_idx_remap_fusion=False` field develop adds was already present on release). (cherry picked from commit 99e7118)
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## release/0.4 #1843 +/- ##
===============================================
Coverage ? 100.00%
===============================================
Files ? 8
Lines ? 42
Branches ? 9
===============================================
Hits ? 42
Misses ? 0
Partials ? 0
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
LiYuRio
approved these changes
Aug 24, 2026
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.
PR Category
Performance Optimization
PR Types
New features, Performance
Description
Merged in dev: #1772
将已合入 develop 的 1772(commit
99e711822a70f8e338a4bfae7b28f750a541fff9)cherry-pick 到 release/0.4。两个融合开关默认均为False,未开启时 release/0.4 的行为与本 PR 之前完全一致。一、带来的改动
fuse_inv_rope_into_vha_postmix:把 HCA 层的 inverse RoPE 折进 ungrouped VHA postmix 的[nh,nh]GEMM(triton_ops/inv_rope_vha_postmix_fusion.py),不再物化全宽的inv_rope(O)。DSv4HybridAttention._can_fuse_inv_rope_postmix对所有不适用的组合(开关关闭、vha_postmix_grouped=True、apply_rope_fusion=False、high_precision_rope=True、postmix 自带 selective recompute 包装)一律回退而不是报错。sparse_attn_global_kv_idx_remap_fusion:把「per-batch-local → flat-global」KV 列索引重映射(idx + b * seqlen_kv)的七个 elementwise kernel 合成一个 Triton kernel(triton_ops/local_to_global_idxs_fusion.py),结果逐位相同。覆盖CompressedSparseAttention与MQALatentAttention在"cudnn"后端下的前向与反向;"tilelang"/"unfused"后端不受影响。_derive_csa_doc_boundaries去 host-sync:is_boundary[0] = True的 1 字节 pageablecudaMemcpy换成concat(原本会阻塞 host 直到 device queue 排空,在 layer43 配置上落在 DeepEP combine 之后,每个-2层约 2.9 ms);单行长序列上的paddle.cummax换成cumsum + gather(seqlen 131072 时 5.3 ms → 约 0.04 ms)。二、冲突解决
release/0.4 上有 develop 尚未合入的 padded-head / sub-512-latent 稀疏 attention 路径,因此产生两处冲突。两处都是保留 release 独有逻辑、只把 dev 的改动叠加上去:
src/paddlefleet/fusions/csa_sparse_attn.pykh用于lse_flat(dev 那边仍是np_heads),保留 release 由ctx.compacted_idxs驱动的topk_length重算;local_to_global_flat(topk_idxs, s_kv, fused=ctx.global_kv_idx_remap_fusion)分发;tests/single_card_tests/ai_edited_test/fusions/test_csa_sparse_attn_backends.pyctx上的compacted_idxs字段。dev 在这个文件里新增的global_kv_idx_remap_fusion=False在 release 上已经存在,所以该文件最终没有改动(本 PR 是 19 个文件,dev 侧是 20 个)。三、验证
CSASparseAttention.forward的形参顺序与apply的位置传参一致,global_kv_idx_remap_fusion在末位。SimpleNamespace伪造ctx直接调 backward 的测试,没有遗漏global_kv_idx_remap_fusion属性的(否则新增的ctx.global_kv_idx_remap_fusion读取会抛AttributeError)。release 上另外两个伪 ctx 测试本来就带了这个字段。py_compile通过;pre-commit run --files <changed>全绿(ruff check / ruff format / typos / copyright / whitespace)。首轮 CI 里
Unit test (multi-card)与Integration test (H20, single card)的失败发生在 Clone / Install 阶段,日志为You are using GPU version Paddle, but your CUDA device is not set properly且paddlefleet_ops/__init__.py:114的paddle.cuda.get_device_capability()抛Place(cpu),即 runner 上没有可见 GPU,与本次改动无关,需重跑。是否引起精度变化
否
两个开关默认关闭;开启后前向、激活梯度以及 postmix
U/V的梯度都与不融合版本逐位一致,由 dev 侧新增的单测钉住。第 3 项_derive_csa_doc_boundaries的改写是等价重写(concat与cumsum + gather产出的 boundary 与doc_start_per_pos与原实现相同),不涉及数值。