Repository navigation
[4/n] attention backend refactoring: migrate every model to AttentionGroups and remove the adapter layer - #1756
Merged
sufubao merged 5 commits intoNov 23, 2026
Conversation
Move the Llama layer's attention plumbing into GQAAttentionLayerMixin (lightllm/common/basemodel/layer_infer/gqa_attention.py). Layers describe their own attention through hooks: _gqa_layer_config_fields (sliding window, sinks, ALiBi), _gqa_layer_weights (sinks, ALiBi slopes) and _gqa_kv_cache (the cache view). Bloom, Starcoder, Gemma4, GPT-OSS, Neo and the DFlash/DSpark drafts now create their Attention when the model loads instead of selecting adapters: - Bloom (ALiBi) and Gemma4 always run on Triton, now through the new TpPartBaseModel.att_implementation. Gemma4's sliding and full-attention layers share one group, so the step args are built once per step instead of once per slot. - GPT-OSS and Neo drop their priority lists: the impls reject sinks and image spans they cannot run, so auto-selection reaches the same implementation. - DFlash layers only write KV in prefill. A layer can now pass no prefill config to create_attention, and its group then builds no prefill step args. The non-causal decode config leaves FA3 as the only implementation, which replaces the model's FA3 check. - Starcoder inherits Bloom's layer, minus ALiBi, instead of binding the Llama kernels. That binding broke when the Llama kernels began reading attributes Bloom's layer does not define. Every attention output of these models (prefill, eager decode, CUDA graph capture and replays) is bit-identical to the adapters'. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Deepseek2TransformerLayerInfer.init_attention creates the layer's Attention from an MLAPrefillLayerConfig and an MLADecodeLayerConfig, whose softmax scale (including the YaRN mscale) the layer already computes. Its kernels build MLAPrefillLayerInputs and MLADecodeLayerInputs directly. This covers DeepSeek V2/V3, their MTP draft, GLM-4 MoE Lite and InternVL-DeepSeek2. DeepSeek V3.2 stays on the adapters until sparse MLA migrates. The MLA backends create their step builders from a layer config: - Triton keys its decode builder by the fields its split schedule depends on. - FA3 decode reuses the FA3 GQA decode step builder and page tables, and both FA3 MLA backends honour LIGHTLLM_FA3_CPU_CACHE_SYNC. - FlashInfer keys by the layer config, so the softmax scale that the adapter recomputed from the model config now comes from the layers. Its builder config leaves the other phase's fields None. The selector now registers a Family per layer config type: its backends per phase, its auto-selection order, and which --llm_*_att_backend value names its implementation. auto_priority replaces decode_priority. MLA uses the GQA priorities, as the adapters did. Every attention output (Triton/FA3/FlashInfer/auto, GLM's 256-dim prefill, fixed and dynamic MTP decode, CUDA graph replays) is bit-identical to the adapters'. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…4) through AttentionGroups The sparse-MLA layers create their Attention when the model loads and build SparseMLA*LayerInputs / SWASparseMLALayerInputs themselves: - DeepSeek V3.2 and GLM-5 describe the indexer in SparseMLALayerConfig. The new fields are topk, kpool and causal: GLM-5's indexer restricts the selection to visible keys, so its layers are non-causal. The step builders derive their parameters from these fields, where the adapters read the model config. That covers FA3 decode's top-k and pool width and FlashMLA decode's direct-decode bound. - DeepSeek V4's layers build their FlashMLA cache views from SWASparseMLALayerConfig and slice off the padded heads. The model's _init_attention keeps the adapter's setup: the fp8kv_dsa check, the 64-head padding and the shared prefill-q buffer. Layers group by compress ratio, whose scheduler metadata they already shared. - GLM-5's FP8 prefill workspace moves into lightllm/models/glm5_next/prefill_workspace.py. The model still reserves it before sizing the KV cache, and step builders now receive it through StepBuilderContext.prefill_workspace. The selector registers both sparse families under the one name the adapters used, "flashmla_sparse". Each phase takes the first backend that accepts the cache format, which reproduces the adapters' pairings: FlashMLA prefill with FA3 decode on a bf16 cache, FlashMLA for both on DeepSeek V3.2's FP8 cache, FA3 prefill with FlashMLA decode on GLM-5's FP8 cache. GLM-5 keeps ignoring --llm_*_att_backend through att_implementation, which now applies only to full-attention families. Every attention output (bf16 and FP8 caches, cached and uncached prefill, eager decode, CUDA graph replays, DeepSeek V4 compress ratios 0/4/128) is bit-identical to the adapters'. The DeepSeek V4 FlashMLA tests move to unit_tests/models/deepseek_v4/test_sparse_attention.py and now drive the layer kernels. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nGroups
Qwen3-Next/Qwen3.5/SenseNova Loop GDN layers and GLM-5 KDA layers
create their Attention when the model loads and build GDNLayerInputs /
KDALayerInputs themselves. Models drop their linear-attention slot.
_init_attention now also covers roles whose only attention is linear.
- The layer configs gain replay, chosen by --ssm_state_mode. Each impl
accepts one state kind, and the step builders derive the SSM slots
per request from the layer config.
- The replay decode impls advance the replay cursors at the end of
forward, where the adapters did it after each call (design doc 10.6).
- Speculative decode batches carry the role's recurrent
req_to_mtp_state_index: AttentionGroups looks it up once and
build_attention_batch sets it when a request has several rows.
- KDALayerConfig gains ssm_dtype and prefill_kernel: the second
--llm_prefill_att_backend value, which picks the FlashKDA fast path
("triton", "flashkda", or "auto" with its validated length cap). Its
prefill step builder loads FlashKDA as the adapter did. An explicitly
requested FlashKDA must import when the model loads.
- The selector registers the GDN and KDA families on the second backend
value with the adapters' auto orders: FlashQLA, then Triton, for GDN
prefill, and Triton otherwise. A new Family.unvalidated lets auto
take Triton without validation, as before.
- An impl whose optional library fails to import, for any reason,
rejects its layer config. Auto-selection then moves on (FlashQLA to
Triton) instead of failing the load.
Every output and final conv/SSM state (GDN Triton prefill, KDA Triton,
FlashKDA and auto prefill, native and replay state, MTP 0/1 with fixed
and dynamic verification) is bit-identical to the adapters'. The
adapters stay until the next commit; they now set replay and leave the
cursor advance to the impls.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…dapters Every model now runs attention through AttentionGroups, so the adapter package and the plumbing that kept both paths alive are removed: - Delete lightllm/common/basemodel/attention (BaseAttBackend and its subclasses, AttControl, the attention states, create_utils and create_linear_utils). - TpPartBaseModel creates AttentionGroups for every model with attention state; InferStateInfo keeps only att_groups and att_step_args, and the four attention-state fields, check_att_states and copy_for_decode_cuda_graph are gone. - Drop uses_attention_groups and the now-unused ENABLE_DIVERSE_MODE_GQA_DECODE_FAST_KERNEL switch. - An explicitly requested FlashKDA that cannot be loaded is rejected at model load with the install command, as the adapter reported it. - Port the tests that drove the adapters to layer configs, AttentionGroups and the step builders, and move the remaining ones next to the code they cover. - Rewrite the attention backend design doc for the finished structure. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
sufubao
changed the base branch from
refactor/attention-backend-3-groups
to
refactor/attention-backend-2-dense-triton
November 23, 2026 05:23
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.
基于 #1735(base 为其分支,#1735 合入后改为
main;原 #1754 已并入 #1735)。完成设计文档第 10.8 节迁移步骤的第 2–5 步:剩余模型全部改走AttentionGroups,然后整体删除适配层lightllm/common/basemodel/attention/。每个迁移提交都与适配层逐位一致,最后一个提交删除适配层及其过渡代码。结果
lightllm/common/basemodel/attention/整个包删除:BaseAttBackend及各库的子类、AttControl、attention state、create_utils.py、create_linear_utils.py、mla_layer.py、sparse_mla_layer.py。create_attention拿到Attention;每次调用直接构造族的LayerInputs。不支持的配置在 warmup 之前报错。InferStateInfo只保留att_groups与att_step_args,删除四个 attention state 字段、check_att_states、copy_for_decode_cuda_graph;模型删除 slot 0/1(prefill_att_backend、*_att_backend1、_init_att_backend*)和uses_attention_groups。提交
12534d326GQAAttentionLayerMixin,各层通过钩子描述滑窗、sinks、ALiBi 和 cache 视图。Bloom、Starcoder、Gemma4、GPT-OSS、Neo、DFlash/DSpark 迁移。Bloom 与 Gemma4 通过新的TpPartBaseModel.att_implementation固定 Triton;Gemma4 的滑窗层与全注意力层共用一组,每步少 build 一次。DFlash 层只在 decode 计算 attention,create_attention的 prefill config 可以为None。03bd38c12Family。ffa59ffc2SparseMLALayerConfig新增topk、kpool、causal,step builder 从中取参数而不再读模型配置。名称仍为flashmla_sparse,各阶段取第一个接受该 cache 格式的 backend,与适配层的组合相同。GLM-5 FP8 prefill workspace 移到models/glm5_next/prefill_workspace.py,经StepBuilderContext.prefill_workspace交给 step builder。DeepSeek V4 按压缩比分组。b11f9d4dereplay(KDA 另有ssm_dtype、prefill_kernel)。replay 游标由 replay decode impl 在forward末尾推进;投机解码的 recurrent 状态索引由AttentionGroups取得一次。第二个--llm_*_att_backend值仍选择 FlashQLA/FlashKDA。47d1a8ec6行为变化
迁移提交都与适配层逐位一致。以下变化都在错误处理或已有 bug 上:
main不受影响)。现在 Starcoder 继承 Bloom 层(去掉 ALiBi),输出与main一致。ValueError,并列出各 backend 拒绝的原因。以前可能在第一次前向时才报错,或抛出映射表的KeyError。ValueError时,auto不再中断加载,而是退回 Triton。显式要求 FlashKDA 但无法加载时,仍在加载时报错并给出安装命令。ENABLE_DIVERSE_MODE_GQA_DECODE_FAST_KERNEL。验证(H200,按 requirements/Dockerfile 固定版本的环境)
逐位一致。 每个迁移提交都用相同输入比较模型层经适配层与经
AttentionGroups的输出(Starcoder 与main中 801e930 之前的结果比较):以上每种都包括 prefill、eager decode、CUDA graph 捕获和 replay。
新增与移植的测试。 驱动适配层的测试都移植到 layer config、
AttentionGroups和 step builder。新增的测试覆盖:step builder 资源(workspace、page size)、InferStateInfo不持有 attention state、GLM-5 的nsa_step由init_att_state构造、多级 KV cache 按AttentionGroups判断 CPU cache 同步、FlashKDA 按命令行选择与安装提示。全量单测。 前四个提交压缩 fixup 之前已逐个运行
unit_tests/common、unit_tests/utils、unit_tests/models、unit_tests/server,失败集合与 [2/n] attention backend refactoring: all attention paths (GQA, MLA, sparse MLA, GDN, KDA) and removal of the model-side adapters #1735 相同(均为环境相关)。最终提交47d1a8ec6重跑全部四个目录:共 38 failed + 7 errors(common 12+1、models 20+1、utils 0、server 6+5),失败集合与 [2/n] attention backend refactoring: all attention paths (GQA, MLA, sparse MLA, GDN, KDA) and removal of the model-side adapters #1735 完全相同,无新增、无减少。压缩 fixup 后改变了树的两个提交(12534d326、ffa59ffc2)也各自重跑了四个目录,失败集合同样与 [2/n] attention backend refactoring: all attention paths (GQA, MLA, sparse MLA, GDN, KDA) and removal of the model-side adapters #1735 完全相同。Black 与 Flake8 按仓库配置通过。尚未覆盖:本机没有本仓库支持的模型权重,没有做端到端推理精度检查。设计要求对每个迁移的模型各做一次,覆盖 CUDA graph、microbatch overlap 和 MTP。其他硬件型号也没有实测。
🤖 Generated with Claude Code