Skip to content

support mqa indexer backward determine - #1683

Merged
From00 merged 2 commits into
PaddlePaddle:release/0.4from
LiYuRio:fix_mqa_determined
Aug 19, 2026
Merged

support mqa indexer backward determine#1683
From00 merged 2 commits into
PaddlePaddle:release/0.4from
LiYuRio:fix_mqa_determined

Conversation

@LiYuRio

@LiYuRio LiYuRio commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

PR Category

Execute Infrastructure

PR Types

New features

Description

增加mqa tilelang的backend

是否引起精度变化

Merged in dev: #1776

@LiYuRio
LiYuRio force-pushed the fix_mqa_determined branch 2 times, most recently from a72eff8 to c0572fb Compare August 9, 2026 10:20

@risemeup1111 risemeup1111 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

序号 位置 优先级 状态
1 transformer_config.py P1
2 mqa_latent_sparse_bwd.py P1
3 mqa_latent_attention.py P1
4 mqa_latent_sparse_bwd.py P1
5 mqa_latent_sparse_bwd.py P1

当前 head 7339b4a 已对小于 kernel tile 的 head 维度进行一致 padding,并在返回前裁剪;H=8 和 H=24 的新增测试覆盖了实际 kernel 与参考实现的梯度对齐。

Powered by Nyanpasu with gpt-5.5 xhigh, please check the suggestions carefully.

"""

stage1_overlap: bool = False
mqa_sparse_attn_backward_backend: str = "cudnn"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1

这里把已有的 stage1_overlap: bool = False 直接替换掉了。core_transformer_config_from_args 只遍历 dataclasses.fields,所以现有 YAML/CLI 的 stage1_overlap 会被静默丢弃,而直接 TransformerConfig(stage1_overlap=...) 也会变成 TypeErrortransformer_layer.py:87 仍按该字段决定共享 MTP 的 sharding overlap。请保留原字段,并在其后新增 mqa_sparse_attn_backward_backend

be reused across all chunks (``bwd_det`` overwrites every slot, so the buffer
needs no clearing between launches).
"""
row_bytes = topk * dk * 4

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1

row_bytes 少计算了 batch 维度:实际在下方分配的 dkv_buf 形状是 [b, sc, topk, dk],预算应按 b * sc * topk * dk * 4 计算。以默认预算和文档中的 S=8192, L=640, Dk=576 为例,B=1 会选择 sc=8192 并占用约 11.25 GiB;同样合法的 B=2 仍选择这个 chunk,却分配约 22.5 GiB,直接违背 12 GiB 上限并可能 OOM。请将 b 纳入 _pick_chunk 的计算(或在分配前按实际 shape 重新限制 chunk)。

topk_probs,
loss_coeff,
"cudnn",
self.indexer_backend,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1

这里把 self.indexer_backend 传给了 MQA sparse loss scaler,但配置校验允许 csa_indexer_backend="unfused"。该 scaler 的 backward 只处理 "cudnn""tilelang",其它值会在 csa_attention.pyNotImplementedError 分支失败;因此 hybrid_mla_attention="mqa_dsa" 的合法 unfused 配置会在第一次 sparse backward 时崩溃。请为该路径实现 unfused backward,或在 MQA sparse 模式下明确拒绝/改用受支持的后端。

_, s_kv, kv_dk = kv.shape
dv = grad_out.shape[-1]

assert kv_dk == dk, f"kv width {kv_dk} != query width {dk}"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1

这组输入形状和 kernel 约束使用了 Python assert(同一函数的 196-204 行)。在 python -O 下这些检查会被完全移除,非法的 Dk/Dv、head 数或 block_h 会继续进入 TileLang JIT 和显存分配,导致更晚的 opaque 错误甚至越界风险。请改为显式 ValueError/参数校验;前向新增的同类 assert 也应一并处理。

@LiYuRio
LiYuRio force-pushed the fix_mqa_determined branch from c0572fb to 70fe9ef Compare August 18, 2026 03:09

@risemeup1111 risemeup1111 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

当前 head 仍有以下未解决的 P1,请按首评行间评论修复后再请求复核:

首评 Board 已更新:stage1_overlap 已解决,其余三项仍需处理。

@LiYuRio
LiYuRio force-pushed the fix_mqa_determined branch 2 times, most recently from 93ee790 to f20f558 Compare August 18, 2026 07:25
@LiYuRio
LiYuRio force-pushed the fix_mqa_determined branch from f20f558 to 1e1ad70 Compare August 19, 2026 02:53

@risemeup1111 risemeup1111 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

当前 head 新增的 TileLang backward 在每卡 H<16 时仍有未解决的 P1:bwd_det 会把 head 数向上 pad 到 16,但调用侧仍传入未 padding 的 8-head 张量。详情见对应行间评论。

Comment thread src/paddlefleet/tilelang_ops/attn/mqa_latent_sparse_bwd.py
``bwd_det`` derives its own tile from the ``H`` it is handed --
``padded_H = max(next_power_of_2(H), 16)``, ``block_H = min(64, padded_H)``
-- and then copies ``Q[b, s, 0:block_H, :]`` and stores
``dQ[b, s, 0:block_H, :]``. Handing it a head group narrower than 16 does
not make it move fewer heads: at ``block_h = 8`` it still moves 16, i.e. it
reads and writes 8 heads past the end of an 8-head tensor.

Restrict the group width to the two values for which the tile matches
(16 / 32) and zero-pad the head axis up to a multiple of it instead. Pad
heads are inert: q and dO are zero there, so dP and both dKV terms vanish
and Delta is zero, so d_sink is too; their dq rows are sliced off before
returning. ``h = 8`` -- the per-rank count of the hybrid-MLA fixtures and of
any TP > 4 split -- therefore keeps working without touching the kernel.
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.31818% with 10 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (release/0.4@7b0d2df). Learn more about missing BASE report.

Files with missing lines Patch % Lines
src/paddlefleet/fusions/mqa_sparse_attn.py 85.41% 6 Missing and 1 partial ⚠️
...lefleet/tilelang_ops/attn/mqa_latent_sparse_bwd.py 97.52% 1 Missing and 2 partials ⚠️
Additional details and impacted files

Impacted file tree graph

@@              Coverage Diff               @@
##             release/0.4    #1683   +/-   ##
==============================================
  Coverage               ?   94.31%           
==============================================
  Files                  ?        4           
  Lines                  ?      176           
  Branches               ?       34           
==============================================
  Hits                   ?      166           
  Misses                 ?        7           
  Partials               ?        3           
Flag Coverage Δ
coverage_combine 94.31% <94.31%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...rc/paddlefleet/transformer/mqa_latent_attention.py 100.00% <100.00%> (ø)
src/paddlefleet/transformer/transformer_config.py 100.00% <100.00%> (ø)
...lefleet/tilelang_ops/attn/mqa_latent_sparse_bwd.py 97.52% <97.52%> (ø)
src/paddlefleet/fusions/mqa_sparse_attn.py 85.41% <85.41%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@From00 From00 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@From00
From00 merged commit fdee6e9 into PaddlePaddle:release/0.4 Aug 19, 2026
25 of 27 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.

4 participants