Skip to content

Example 77 add blackwell fmha bwd for MLA shape#2466

Merged
hwu36 merged 5 commits into
NVIDIA:mainfrom
uchihatmtkinu:fmha_blackwell_bwd_mla
Jul 24, 2025
Merged

Example 77 add blackwell fmha bwd for MLA shape#2466
hwu36 merged 5 commits into
NVIDIA:mainfrom
uchihatmtkinu:fmha_blackwell_bwd_mla

Conversation

@uchihatmtkinu

Copy link
Copy Markdown
Contributor

Description

Add MLA shape($d_{qk}=192$,$d_{vo}=128$) of attention bwd kernels in example 77. Both fp8 and fp16 are supported.

Design of MLA bwd kernel

fmha_bwd_mla has several changes to handle the larger $d_{qk} size:

  • Q_TILE=64 and KV_TILE=128
  • Calculate $S^{T}=QK$(instead of$S=KQ$),$dP^{T}=dOV$(instead of $dP=VdO$). $S^{T}$ and $dP^{T}$ are stored in TMEM with dim=64
  • TMEM layout change: * $dK$ (128x192) | $dV$ (128x128) | $dQ$ (64x192)= $dP$ (64x128) & $S$ (64x128)
    • $dQ$ and $dP$ share the same address with 64dim and $S$ uses another 64dim
    • $P$ will be stored to SMEM instead of TMEM since the space is available.
  • Shared Memory
    • Store $P$ in the smem
  • Corresponding layout adaption of cute

Examples

The bwd MLA also uses 77_blackwell_fmha_bwd_fp(type), to trigger it, using --d=192 --d_vo=128.

Performance Data

setting $d_{qk}$=$d_{vo}$=128 $d_{qk}$=192, $d_{vo}$=128
b=16,h=10,q=1024,k=1024 690T 576T
b=4,h=128,q=4096,k=4096 1015T 874T

@uchihatmtkinu uchihatmtkinu changed the title example 77 add blackwell fmha bwd for MLA shape Example 77 add blackwell fmha bwd for MLA shape Jul 14, 2025
Comment thread examples/77_blackwell_fmha/device/fmha_device_bwd.hpp Outdated
Comment thread examples/77_blackwell_fmha/kernel/fmha_kernel_bwd_convert.hpp Outdated
@uchihatmtkinu

uchihatmtkinu commented Jul 21, 2025

Copy link
Copy Markdown
Contributor Author

@thakkarV Hi, I just add some new commits which fix the casual mask when IsQBegin==false, which means the Q and K are aligned at tail when seqlen_q != seqlen_kv. Please have a look.

@thakkarV

Copy link
Copy Markdown
Contributor

@IonThruster @hwu36 please fwd to Richard?

@thakkarV

Copy link
Copy Markdown
Contributor

@v0i0 too in case you have time to review

@uchihatmtkinu
uchihatmtkinu force-pushed the fmha_blackwell_bwd_mla branch from d4416b8 to b468b12 Compare July 22, 2025 03:32
@hwu36
hwu36 merged commit 0e02698 into NVIDIA:main Jul 24, 2025
Albresky pushed a commit to Albresky/cutlass that referenced this pull request Oct 11, 2025
* Update examples/77_blackwell_fmha/device/fmha_device_bwd.hpp

Co-authored-by: Vijay Thakkar <vijaythakkar@me.com>

* bug fix & use existing value rather than pass one more argument to support different dim in bwd_convert

* Fix casual mask cnt when IsQBegin==false

* bug fix in casual mask backward

* code sync

---------

Co-authored-by: Vijay Thakkar <vijaythakkar@me.com>
guocuimi pushed a commit to vectorch-ai/cutlass that referenced this pull request Nov 6, 2025
* Update examples/77_blackwell_fmha/device/fmha_device_bwd.hpp

Co-authored-by: Vijay Thakkar <vijaythakkar@me.com>

* bug fix & use existing value rather than pass one more argument to support different dim in bwd_convert

* Fix casual mask cnt when IsQBegin==false

* bug fix in casual mask backward

* code sync

---------

Co-authored-by: Vijay Thakkar <vijaythakkar@me.com>
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.

3 participants