Skip to content

[4/n] attention backend refactoring: migrate every model to AttentionGroups and remove the adapter layer - #1756

Merged
sufubao merged 5 commits into
refactor/attention-backend-2-dense-tritonfrom
refactor/attention-backend-4-remove-adapters
Nov 23, 2026
Merged

sufubao merged 5 commits into
refactor/attention-backend-2-dense-tritonfrom
refactor/attention-backend-4-remove-adapters

Conversation

@sufubao

@sufubao sufubao commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

基于 #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。
  • 模型层在加载时从自己的配置构造 layer config,通过 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。
  • 设计文档按最终结构重写:第 2–9 节描述现状,迁移计划(原第 10 节)删除,选择规则与分组、时序分别成为第 5、6 节。

提交

提交 内容
12534d326 GQA 变体:Llama 层的 attention 代码移入 GQAAttentionLayerMixin,各层通过钩子描述滑窗、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。
03bd38c12 MLA(DeepSeek V2/V3、其 MTP、GLM-4 MoE Lite、InternVL-DeepSeek2):softmax scale(含 YaRN mscale)由层计算一次写入 layer config,FlashInfer MLA 不再从模型配置重复计算。选择器按 layer config 类型注册 Family。
ffa59ffc2 稀疏 MLA(DeepSeek V3.2、GLM-5、DeepSeek V4):SparseMLALayerConfig 新增 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 按压缩比分组。
b11f9d4de 线性注意力(Qwen3-Next/Qwen3.5 的 GDN、GLM-5 的 KDA):layer config 新增 replay(KDA 另有 ssm_dtype、prefill_kernel)。replay 游标由 replay decode impl 在 forward 末尾推进;投机解码的 recurrent 状态索引由 AttentionGroups 取得一次。第二个 --llm_*_att_backend 值仍选择 FlashQLA/FlashKDA。
47d1a8ec6 删除适配层与过渡代码、移植测试、重写设计文档。

行为变化

迁移提交都与适配层逐位一致。以下变化都在错误处理或已有 bug 上:

验证(H200,按 requirements/Dockerfile 固定版本的环境)

尚未覆盖:本机没有本仓库支持的模型权重,没有做端到端推理精度检查。设计要求对每个迁移的模型各做一次,覆盖 CUDA graph、microbatch overlap 和 MTP。其他硬件型号也没有实测。

🤖 Generated with Claude Code

sufubao and others added 5 commits November 22, 2026 21:20
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
sufubao changed the base branch from refactor/attention-backend-3-groups to refactor/attention-backend-2-dense-triton November 23, 2026 05:23
@sufubao
sufubao merged commit 47d1a8e into refactor/attention-backend-2-dense-triton Nov 23, 2026
@sufubao
sufubao deleted the refactor/attention-backend-4-remove-adapters branch November 23, 2026 05:30
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