Skip to content

[3/n] attention backend refactoring: GQA FlashInfer prefill and decode - #1741

Closed
sufubao wants to merge 9 commits into
refactor/attention-backend-2-dense-tritonfrom
refactor/attention-backend-3-dense-flashinfer
Closed

sufubao wants to merge 9 commits into
refactor/attention-backend-2-dense-tritonfrom
refactor/attention-backend-3-dense-flashinfer

Conversation

@sufubao

@sufubao sufubao commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

承接 #1735。backends/gqa/flashinfer.py 用同一份 GQAInputs 接入 FP16/BF16 prefill 和 decode,并遵循 #1735 中 AttentionBackend 的 plan → forward → refresh 生命周期。调用方准备 page map 和 workspace 并管理 CUDA graph,backend 直接填写调用方的输出。

可按下面两个提交依次审阅。

提交 内容
bad062230 抽出 GQA FlashInfer backend(原 9367a75bb,随 #1735 调整包路径和类型名)。
98e274550 接入 plan/refresh。plan() 在调用方持有的 workspace 和页缓冲上创建并规划 prefill wrapper 或支持 CUDA graph 的 decode wrapper;decode 的 refresh(batch, max_kv_len=...) 在 replay 前执行 fast_decode_plan。adapter 计算页元数据,在 init_state 中规划一次,replay 状态不规划,由 copy_for_decode_cuda_graph 刷新捕获的 batch。

两点说明:

  • AttentionBackend.refresh() 改为接受各实现自定义的关键字参数。FlashInfer 需要按 replay batch 的最大 KV 长度(主机侧数值)重新规划,是第一个用到它的实现。
  • adapter 保留 prefill_wrapper/decode_wrapper,因为 FP8 FlashInfer 子类仍直接调用 wrapper。head 数和 dtype 作为 plan() 参数而非构造参数传入,因为 FP8 backend 在基类构造之后才设置 KV dtype。

验证

9367a75bb 在 H200 单卡、FlashInfer 0.6.18 上通过 32 项 GQA FlashInfer 测试,覆盖 ragged/cached prefill、不同 page size、非连续 KV、输出 identity,以及用 torch/serving allocator 捕获的 graph 在请求变长后的 replay。

98e274550 在 H200、Torch 2.11.0、FlashInfer 0.6.12 上验证:

🤖 Generated with Claude Code

@sufubao
sufubao force-pushed the refactor/attention-backend-2-dense-triton branch 2 times, most recently from 5bb04ad to 44f41a9 Compare November 18, 2026 10:09
@sufubao
sufubao force-pushed the refactor/attention-backend-3-dense-flashinfer branch 2 times, most recently from 0fbb5ef to b3b7796 Compare November 18, 2026 10:19
@sufubao
sufubao force-pushed the refactor/attention-backend-2-dense-triton branch 2 times, most recently from 7e3e005 to 55145d4 Compare November 20, 2026 07:19
@sufubao
sufubao force-pushed the refactor/attention-backend-3-dense-flashinfer branch 2 times, most recently from 19636ea to b3b7796 Compare November 20, 2026 07:26
@sufubao
sufubao force-pushed the refactor/attention-backend-2-dense-triton branch from 55145d4 to 7e3e005 Compare November 20, 2026 07:26
@sufubao
sufubao force-pushed the refactor/attention-backend-3-dense-flashinfer branch from b3b7796 to fe7e8b4 Compare November 20, 2026 08:11
@sufubao
sufubao force-pushed the refactor/attention-backend-2-dense-triton branch from f102662 to 14a4fb7 Compare November 20, 2026 08:30
@sufubao
sufubao force-pushed the refactor/attention-backend-3-dense-flashinfer branch from fe7e8b4 to 0815454 Compare November 20, 2026 08:30
@sufubao
sufubao force-pushed the refactor/attention-backend-3-dense-flashinfer branch from 0815454 to 9367a75 Compare November 20, 2026 09:24
@sufubao
sufubao force-pushed the refactor/attention-backend-2-dense-triton branch from 14a4fb7 to ee1e7ea Compare November 20, 2026 09:24
@sufubao sufubao changed the title [3/n] attention backend refactoring: introduce dense FlashInfer backends [3/n] attention backend refactoring: GQA FlashInfer prefill and decode Nov 20, 2026
sufubao and others added 6 commits November 20, 2026 18:01
The decode split schedule depended on the first layer's query and cache
strides, so it could only be prepared lazily inside decode_att, with a
separate cached schedule for CUDA graph capture.

TritonMLADecodeBackend now takes the per-rank layer configuration
(q heads, latent and rotary dims, dtype) at construction. Stage 1
occupancy is probed once with canonical operands and cached, and stage 1
launches with the planned vsm_count instead of measuring it per layer.
The adapter plans in init_state, skips planning for replay states, and
refreshes the captured schedule in copy_for_decode_cuda_graph.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
AttentionBackend now defines the lifecycle shared by all providers:
plan() builds the batch once per forward pass, outside CUDA graph
capture; forward() runs per layer; refresh() updates derived state
before graph replay and is a no-op by default.

Every GQA, MLA and sparse MLA backend implements plan(), and the
adapters plan in init_state instead of rebuilding the batch in every
layer. A GQA decode batch planned for capture bounds MHA scratch by the
graph capacity, which capture only writes into infer_state after
init_state. Speculative decode does not plan the unused GQA batch.

Batch holds state shared by all layers and Inputs holds per-layer data,
so the sparse MLA top-k indices move from Batch to Inputs. The decode
plan now creates the FlashMLA tile schedule.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
GQAInputs stores sliding_window as a tuple, so a list loaded from a JSON
model config matches the full-attention check in MHA decode. GQA decode
rejects q_heads that are not a multiple of kv_heads with a ValueError
naming both counts, instead of NotImplementedError("error") or silently
running the GQA kernel. The backend docstrings state that the caller
selects q.device as the current device.

MLA prefill derives the rotary width from k_rope instead of assuming 64.
The sparse MLA tests remove their backend singleton and its GPU buffers.
The MLA decode tuning script uses the current kernel paths and the
plan-then-decode API.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The GQA FlashInfer backends now implement the plan/refresh lifecycle
from #1735. plan() builds and plans the prefill or CUDA-graph-capable
decode wrapper over caller-owned workspace and page buffers, and the
decode refresh() runs fast_decode_plan for graph replay. The adapter
derives page metadata, plans once in init_state and refreshes the
captured batch in copy_for_decode_cuda_graph.

refresh() accepts provider-specific keyword arguments, since FlashInfer
re-plans from the replay batch's maximum KV length. The adapter keeps
prefill_wrapper and decode_wrapper because the FP8 subclass still runs
the wrapper directly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sufubao

sufubao commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator Author

FlashInfer GQA 已并入 #1735(提交 7702f03,按新的 planner/impl 接口实现),本 PR 关闭。

🤖 Generated with Claude Code

@sufubao sufubao closed this Nov 20, 2026
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.

1 participant