HySparse MQA sparse attention (FA4 block scoring + SWA MQA + block-sparse DSA gather)#1470
Closed
ForFishes wants to merge 2 commits into
Closed
HySparse MQA sparse attention (FA4 block scoring + SWA MQA + block-sparse DSA gather)#1470ForFishes wants to merge 2 commits into
ForFishes wants to merge 2 commits into
Conversation
…arse DSA gather) - FA4-fused block scoring (block_score_fa4) with PyLayer grads - SWA sliding-window MQA (swa_attn) replacing standalone block_sparse_attn_mqa - block-sparse DSA gather via cuDNN (block_sparse_mqa_dsa) with d_v<512 value-region padding to satisfy FlashMLA sparse kernel (d_v==512) and learnable per-head attn_sink - MLA/transformer wiring in multi_latent_attention, transformer_layer, gpt_layer_specs; enable_hy_sparse_attention config plumbing - runtime guards: fusion_layer_utils fp8-scales ImportError fallback for older paddlefleet_ops; transformer_config MTP-must-be-full-attention check
- FA4 block-score grad / topk-consistency / multidoc tests - MQA gather DSA, windowed SWA, pipeline, whole-flow precision tests - MQA self-attention module test, build_hysparse_valid_range test - remove obsolete block-attn test superseded by the above
Member
Author
Contributor
There was a problem hiding this comment.
Pull request overview
该 PR 在 PaddleFleet 中引入 HySparse MQA 稀疏注意力(包含 FA4 block scoring + SWA MQA + cuDNN/FlashMLA DSA gather 三个协作结构),并补充了覆盖算子精度、梯度、packed-multidoc 语义与端到端 whole-flow 的单卡测试,用于在 enable_hy_sparse_attention 配置开关下启用新路径、保持对现有 develop 功能的隔离。
Changes:
- 新增 FA4 融合的 block-score(输出 scaled block_logit)与 host 侧 TopK block 选择流水线,并在 MLA 路径中接入(HySparse full layer 产出 shared KV + block indices)。
- 新增 SWA windowed MQA TileLang kernel(支持 Dk≠Dv、可选 attn_sink)及其 PyLayer 封装,并接入 HySparse SWA 层主分支。
- 新增 DSA(FlashMLA sparse fwd + cuDNN DSA bwd)block-sparse gather 后端与 padding/sink 逻辑,并增加多组单卡测试覆盖。
PR 标题与描述检查
- 标题目前不符合仓库要求的
[CLASS]Title格式;建议按类别补前缀,例如:[Feature] HySparse MQA sparse attention (FA4 + SWA + DSA gather)或按项目约定选择更合适的 CLASS。 - 描述信息较完整(What/核心改动/约束/兼容性/测试计划均有),符合最低要求。
Reviewed changes
Copilot reviewed 28 out of 28 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
src/paddlefleet/transformer/transformer_layer.py |
引入 HySparseTransformerLayer 以支持跨层 KV sharing,并在线程/重计算路径中维护 shared KV 的传递。 |
src/paddlefleet/transformer/transformer_config.py |
增加 HySparse 配置开关与 block/topk 参数,并在 __post_init__ 增加 MTP + SWA 的 fail-fast 约束。 |
src/paddlefleet/models/gpt/gpt_layer_specs.py |
HySparse 开关下切换 attention/layer 实现并对不兼容组合显式报错。 |
src/paddlefleet/tilelang_ops/hysparse/windowed_mqa_attn*.py |
windowed MQA kernel fwd/bwd(Dk≠Dv、attn_sink、ragged seqlen bwd guard 等)。 |
src/paddlefleet/tilelang_ops/hysparse/swa_attn.py |
sliding-window MQA attention 的 PyLayer 封装与默认 sm_scale 逻辑。 |
src/paddlefleet/tilelang_ops/hysparse/block_score_fa4.py |
FA4 融合 block scoring 的 PyLayer 封装与 scaled block_logit 语义。 |
src/paddlefleet/tilelang_ops/hysparse/pipeline.py |
TopK block 选择与 block score 恢复(exp(block_logit - lse))及 TopK 前的 detach 防护。 |
src/paddlefleet/cudnn_ops/block_sparse_mqa_dsa.py |
DSA gather 后端:block->token 展开、sinkless/learnable sink、kv_lora_rank<512 padding、analytic sink grad。 |
src/paddlefleet/transformer/moe/fusion_layer_utils.py |
为旧版 paddlefleet_ops 增加 deepep_topk_to_sonic_metadata_with_scales 的 ImportError fallback。 |
tests/single_card_tests/** |
新增/调整多组 HySparse 单卡测试,覆盖 grad、一致性、multidoc、pipeline、whole-flow precision 等。 |
Comment on lines
+42
to
+51
| try: | ||
| from paddlefleet_ops.sonicmoe.ernie_compat.deepep_metadata import ( | ||
| deepep_topk_to_sonic_metadata_with_scales, | ||
| ) | ||
| except ImportError: | ||
| # Older installed paddlefleet_ops binaries (pre-#1348) do not export | ||
| # the fp8-scales variant; the sibling optional imports below use the | ||
| # same guard. Only the fp8 + fp8_scale MoE path calls it, which this | ||
| # config does not exercise. | ||
| deepep_topk_to_sonic_metadata_with_scales = None |
PaddleFleet Log Analysis
日志分析报告
失败的测试case: 根本原因分析: 修复建议:
🔄 每次 Re-run 后自动更新 |
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.
What
Add HySparse MQA sparse attention (NSA-style, three cooperating structures) to PaddleFleet, plus test coverage.
Core (
b9f3145)tilelang_ops/hysparse/block_score_fa4.py) with PyLayer-routed gradients.tilelang_ops/hysparse/swa_attn.py), replacing the standalone block-sparse MQA fwd/bwd kernels.cudnn_ops/block_sparse_mqa_dsa.py) via cuDNN / FlashMLA sparse, including:d_v < 512value-region padding to 512 so the FlashMLA sparse kernel constraint (d_v == 512) is satisfied transparently — zeros injected between the value region and RoPE region are transparent to q·k, output sliced back tokv_lora_rank; pad/slice sit outside the PyLayer so autograd routesdq/dkvcorrectly.attn_sink(analytic gradient in backward).gpt_layer_specswiring;enable_hy_sparse_attentionconfig plumbing intransformer_config.fusion_layer_utils:deepep_topk_to_sonic_metadata_with_scalesImportError fallback so olderpaddlefleet_opsbinaries (pre-Supports Sonicmoe bf16 z and wgrad, optimize sonicmoe performance #1348) still load.transformer_config: whenenable_hy_sparse_attention, the MTP portion ofwindow_attn_skip_freqmust be all-zero (SWA MTP layers have no shared KV) — fail fast.Tests (
da832e0)FA4 block-score grad / topk-consistency / multidoc, MQA gather DSA, windowed SWA, pipeline, whole-flow precision, MQA self-attention module test, and
build_hysparse_valid_rangetest. Obsoletetest_hysparse_block_attn.pyremoved (superseded).FlashMLA sparse kernel constraints
d_qk ∈ {512, 576},d_v == 512,h_q == 64. Absorbed-MLA MQA layout:Dk = kv_lora_rank + qk_rope_head_dim,Dv = kv_lora_rank.Compatibility
Rebased on latest
develop. No changes to existing develop features — HySparse is gated behindenable_hy_sparse_attention. Net diff: 28 files, +4975 / −1979.Test plan
tests/single_card_tests/.