Repository navigation
Conversation
sufubao
force-pushed
the
refactor/attention-backend-2-dense-triton
branch
from
November 17, 2026 07:27
2716162 to
00592ad
Compare
sufubao
force-pushed
the
refactor/attention-backend-1-kernel-layout
branch
from
November 17, 2026 07:27
ce7e093 to
24a2f1e
Compare
sufubao
force-pushed
the
refactor/attention-backend-1-kernel-layout
branch
from
November 18, 2026 09:24
24a2f1e to
33c5627
Compare
sufubao
force-pushed
the
refactor/attention-backend-2-dense-triton
branch
2 times, most recently
from
November 18, 2026 10:09
5bb04ad to
44f41a9
Compare
sufubao
force-pushed
the
refactor/attention-backend-1-kernel-layout
branch
from
November 18, 2026 10:09
33c5627 to
2861a14
Compare
sufubao
force-pushed
the
refactor/attention-backend-2-dense-triton
branch
from
November 18, 2026 10:19
44f41a9 to
7e3e005
Compare
sufubao
force-pushed
the
refactor/attention-backend-1-kernel-layout
branch
from
November 18, 2026 10:19
2861a14 to
4e8f3a1
Compare
sufubao
force-pushed
the
refactor/attention-backend-2-dense-triton
branch
from
November 20, 2026 07:19
7e3e005 to
55145d4
Compare
sufubao
force-pushed
the
refactor/attention-backend-1-kernel-layout
branch
from
November 20, 2026 07:26
b5bcf3c to
4e8f3a1
Compare
sufubao
force-pushed
the
refactor/attention-backend-2-dense-triton
branch
from
November 20, 2026 07:26
55145d4 to
7e3e005
Compare
Base automatically changed from
refactor/attention-backend-1-kernel-layout
to
main
November 20, 2026 08:09
sufubao
force-pushed
the
refactor/attention-backend-2-dense-triton
branch
2 times, most recently
from
November 20, 2026 09:24
14a4fb7 to
ee1e7ea
Compare
sufubao
added a commit
that referenced
this pull request
Nov 20, 2026
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
force-pushed
the
refactor/attention-backend-2-dense-triton
branch
from
November 20, 2026 13:55
9f780d8 to
f303ea4
Compare
sufubao
force-pushed
the
refactor/attention-backend-2-dense-triton
branch
from
November 20, 2026 15:29
f303ea4 to
4605284
Compare
sufubao
force-pushed
the
refactor/attention-backend-2-dense-triton
branch
2 times, most recently
from
November 21, 2026 07:57
b6d4ead to
0e710cf
Compare
sufubao
force-pushed
the
refactor/attention-backend-2-dense-triton
branch
2 times, most recently
from
November 23, 2026 02:21
adad5b4 to
7126ab1
Compare
--att_backend takes FAMILY.PHASE=BACKEND entries (gqa.prefill=fa3
gdn.prefill=flashqla kda.prefill=flashkda ...) for the families gqa,
mla, sparse_mla, gdn and kda. Omitted keys stay auto. The parser checks
keys and names against the selector registry, so a typo or a name the
family does not offer fails at startup with the valid choices.
Each registry Family now carries its name. selector.requested_backend
reads --att_backend first and falls back to the deprecated positional
--llm_{prefill,decode}_att_backend flags (first value for full
attention, second for linear attention), which keep working with a
warning. GLM-5's KDA prefill kernel and the EP-MoE flashinfer check use
the same lookup. The unused enable_triton_mtp_kernel helper is removed.
Docs, cookbooks and start scripts use the new flag; GLM-4.7-Flash uses
the mla keys.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
get_gpu_workspace_buffer now keys storage by (device, name, dtype): a request no larger than a buffer the name already has gets a prefix view of it. Main and draft models prefill one after the other, so a name may be shared as long as it has one user at a time. The model-side prefill workspaces were named per role, so every MTP draft reserved its own copy (up to 2 GiB of FP8 gather space each for GLM-5). GLM-5 now names its FP8 prefill regions per microbatch, which keeps region i of every model on the same stream, and shares one read-only cu_seqlens_q arange across regions. DeepSeek V4 names its SWA staging buffer by its per-layer layout, so a one-layer draft stages into the main model's first layer slice and a draft with another layout gets its own buffer. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…orts The PR's own rule test (lightllm.common.attention must not depend on models) failed: every backend imported InferStateInfo from lightllm.common.basemodel at runtime. Backends now name InferStateInfo only under TYPE_CHECKING, the one runtime need, GraphMode, is defined in lightllm/common/attention/backend.py and re-exported by infer_struct, and the rule test skips TYPE_CHECKING blocks, since annotations create no runtime dependency. The caller-glob test no longer expects the deleted layer_infer/gqa_attention.py. The graph-storage helper is public as keep_for_graph: the shared FA3 and recurrent decode builders call it on another backend, and backend authors are meant to use it. The test helper no longer writes the private set-up flag. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The FlashKDA prefill tests built bare type(...)() stubs. Copying InferStateInfo's instance fields onto them misses properties such as uses_cuda_graph, which the KDA prefill backend reads, so 8 cases failed. They now start from InferStateInfo(). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…plate Llama and Bloom each carried the same GQA wiring: the cache-state lookup, the layer config, the GQALayerInputs assembly and the backend call. It now lives once in TransformerLayerInferTpl, which already writes the layer's full-attention KV cache, as the default attention_config and attention kernels. Models override only what is theirs: extra config fields through _gqa_config(model, **fields), per-layer sinks or ALiBi slopes, and the KV view. Bloom keeps just its ALiBi config and slopes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
setup already runs on a phase's first prepare, so taking the workspace there is as lazy as the deferred lookups it replaces: the MLA module's one-line _workspace helper and the GQA backend's stored lambda. Each backend now keeps the tensor as self.workspace, and MLA no longer keeps the resources. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- requires_cpu_cache_sync is a backend class attribute again; the FA3 backends set it from FA3_CPU_CACHE_SYNC instead of three classmethods calling a one-line env wrapper. The manager exposes it as a property and the CPU cache module reads it there, without a model-level alias. - The manager's backends(phase) only re-listed state_groups[phase]; callers read state_groups directly. - The selector imports the library validator instead of wrapping it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nels The ALiBi prefill and decode kernels were the last attention kernels outside lightllm/common/attention/kernels. They now live in kernels/gqa/alibi next to the int8kv and int4kv variants, so the call-site rule needs no special case for basemodel/triton_kernel/alibi_att. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…plate 803f899 made TransformerLayerInferTpl default to GQA, but DeepSeek V4 and GLM-5-Next also derive from it and are not GQA. The generic template is back to declaring no attention type (it keeps only the prefill_backend and decode_backend slots every layer gets), and the shared GQA wiring lives in GQATransformerLayerInferTpl, which Llama and Bloom derive from. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tion_state does The main model now creates one AttentionBackendManager next to its AttentionStateManager and its drafts share it. Backends are registered per model role (register_backends(model)), and layers look theirs up on each call with get_backend(role, phase, logical_layer_id), mirroring get_state(role, type, logical_layer_id). Layer configs still come from each layer's attention_config(model). Backend instances are shared within a role but never across roles, since each owns its role's cross-step and CUDA graph storage. The bound prefill_backend / decode_backend slots on the layer template are gone, prepare is renamed create_att_state on both the manager and the backends, has_attention takes the role, and requires_cpu_cache_sync covers every registered role. GDN/KDA layers always pass replay_state, which only replay backends read. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
hiworldwzj
force-pushed
the
refactor/attention-backend-2-dense-triton
branch
from
November 30, 2026 10:12
1f771ba to
b060935
Compare
These types describe one attention call, separate from the transformer layer that invokes it. Co-authored-by: Cursor <cursoragent@cursor.com>
Docs and helpers still called the same object "att config" or "config" after the rename. Co-authored-by: Cursor <cursoragent@cursor.com>
Give each state layout its own directory so replay prefill inherits the native class, and register FlashKDA as its own prefill backend. Co-authored-by: Cursor <cursoragent@cursor.com>
Machine and config checks decide the implementation at load time, so a selected FlashKDA prefill always runs FlashKDA and an explicit backend fails before the first step. Co-authored-by: Cursor <cursoragent@cursor.com>
The selected FlashKDA prefill loads its kernel directly. Availability stays in the machine check instead of a second loader that could still return no kernel. Co-authored-by: Cursor <cursoragent@cursor.com>
KDA prefill metadata no longer chooses a kernel. Each backend calls its own prefill function, and the selected FlashKDA kernel stays on the backend. Co-authored-by: Cursor <cursoragent@cursor.com>
Triton and FlashKDA pack the same state slots. Each backend still supplies its own attn_cu_seqlens dtype. Co-authored-by: Cursor <cursoragent@cursor.com>
Prefill and decode both reject an AttConfig whose native or replay layout does not match the backend, through one check. Co-authored-by: Cursor <cursoragent@cursor.com>
Selection and cache sync read these from the backend itself, so each implementation sets them as instance properties and keeps the base method order. Co-authored-by: Cursor <cursoragent@cursor.com>
The step mode belongs with the infer state. Drop the allocator protocol and keep alloc_func as the forward argument it already was. Co-authored-by: Cursor <cursoragent@cursor.com>
Layers no longer pin an implementation. metadata_config_key reads the backend's own config, and the command line plus config checks select it. Co-authored-by: Cursor <cursoragent@cursor.com>
Each distinct config gets one backend and one metadata entry, so the backend no longer publishes a separate grouping key. Co-authored-by: Cursor <cursoragent@cursor.com>
Pass the model's fixed capacities when a backend is constructed, and keep only the scalars that backend uses. One shared scratch buffer is reserved at registration and handed to create_att_metadata. Co-authored-by: Cursor <cursoragent@cursor.com>
Registration of a model's backends belongs with the attention component, next to the selector and the backend interface. Co-authored-by: Cursor <cursoragent@cursor.com>
Move the backend manager to attention/manager.py. Register every role before warmup, then allocate the shared scratch once so CUDA graph capture keeps that address. Co-authored-by: Cursor <cursoragent@cursor.com>
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.
概述
所有 attention 路径(GQA、MLA、稀疏 MLA、GDN、KDA)改由
lightllm/common/attention/下的 backend 层执行,原lightllm/common/basemodel/attention/中的模型侧 adapter 全部删除。模型层只声明每层的静态配置、组装本层张量并调用 backend,不再直接调用 attention kernel(由test_attention_call_sites.py检查)。改动规模:生产代码 138 个文件(+5856/−6584),测试 86 个文件(+8220/−2515)。完整设计见
docs/CN/source/framework/attention_backend_design.md。设计
每步由
AttentionBackendManager.create_att_metadata_by_group生成按组索引的元数据;各组执行InferStateInfo → backend.create_att_metadata → att_metadata → backend.forward。*LayerConfigAttentionBackendname、phase、metadata_config_key,实现init_runtime、build_att_metadata、forwardInferStateInfoinit_runtime与build_att_metadata读取att_metadata*LayerInputsselector.py按 layer config 类型找到算法族,按名称或auto优先级构造 backend。backend 在构造时拒绝不支持的配置(NotImplementedError),auto跳过它,显式指定则在模型加载时报错并列出原因。各 backend 用phase声明运行阶段,选择器只把它绑定到该阶段。AttentionBackendManager与AttentionStateManager组织方式一致:主模型创建、草稿模型共用,按模型角色(main、draft0…)登记。register_backends(model)显式遍历每层的 prefill/decode 配置,将选择规则和实例复用交给本次注册独有的BackendSelector(args);selector 统一处理层指定名称、CLI、MTP 优先级和 EP 的 auto 排除规则,manager 保存层绑定与状态分组。层每次调用时用get_backend(角色, 阶段, 逻辑层号)取用,与get_state(角色, 类型, 逻辑层号)对应。同一角色内阶段、配置和最终请求名称相同的层共用实例,不同角色不共用(实例持有本角色的跨步与 graph 存储)。每个阶段按(backend 类, metadata_config_key)分组,组内第一个实例在首次create_att_metadata时调用init_runtime(infer_state)初始化跨步存储,之后每个 microbatchbuild_att_metadata一次状态,组内各层共用。metadata_config_key是参与准备的配置子集,metadata_group_key是包含 backend 类的完整键;manager 的group_backends保存各组代表实例,_layer_backends保存层绑定,selector 的_selection_cache复用选择结果。_create_inferstate直接从MtpManager、模型参数和当前角色的 recurrent State 准备 page size、模型序列容量、graph batch 容量、最大 decode 行数和 diverse 开关,以及本步的 decode 行数、dynamic verify 标记、投机状态索引和 graph KV 长度容量,删除step_configs与configure_attention_step。manager 只按角色和阶段构建 attention 状态;循环状态唯一性在模型初始化时检查,纯 FFN 草稿与 KV-only 步骤跳过 attention 准备。backend 选择日志在登记结束后输出。AttentionBuildResources,create_att_metadata只接收infer_state。backend 首次初始化只保留需要的标量和缓冲区,不持有整份推理状态。AttentionBackendManager.get_workspace(name, numel, dtype)管理缓存,替代独立的workspace_utils.py;main 与 draft 共用,同名同尺寸请求地址固定,独立 manager 之间隔离。FlashInfer GQA 自行读取并校验flashinfer_workspace_size,私有配置不放入通用状态。update_graph_tensor按(microbatch, batch size)固定地址并原地改写;第三方有状态对象(FlashInfer wrapper、FlashMLA scheduler metadata)同样按该键保存,replay 时原地重新规划。*Metadata,与 KV/SSM 持久状态区分。use_ssm_replay、state_draft_capacity、max_draft_tokens和max_decode_rows_per_request明确模式与容量;logical_layer_id、head 维度、卷积宽度、sink 字段及测试辅助函数同步统一。稀疏输入拆分物理缓存槽位topk_cache_indices、请求内位置topk_token_positions和拼接位置topk_ragged_indices;SWA 压缩缓存使用compressed_indices/compressed_lengths。forward写入调用方提供的out,只有 kernel 不接受out、需要补齐 head 或需要 FP32 合并时才复制。GQATransformerLayerInferTpl(Llama、Bloom 的基类)提供 GQA 的attention_config与 prefill/decode 调用,通用的TransformerLayerInferTpl不假定 attention 类型;模型只通过_gqa_config(model, **fields)、_gqa_sinks、_gqa_alibi_slopes、_gqa_kv_cache提供自身差异。lightllm/common/attention在运行时不依赖模型代码;InferStateInfo只在TYPE_CHECKING下用于注解,GraphMode定义在backend.py。覆盖范围
gqaGQALayerConfigmlaMLAPrefillLayerConfig、MLADecodeLayerConfigsparse_mlaSparseMLALayerConfig、SWASparseMLALayerConfiggdnGDNLayerConfigkdaKDALayerConfig线性注意力的 backend 原地更新调用方的 conv/SSM 状态,replay 状态通过
LayerInputs.replay_state追加历史。用户可见的变化
--att_backend FAMILY.PHASE=BACKEND,例如--att_backend gqa.prefill=fa3 gqa.decode=flashinfer gdn.prefill=flashqla;算法族为gqa、mla、sparse_mla、gdn、kda,未指定的项为auto,解析时按注册表校验。--llm_prefill_att_backend/--llm_decode_att_backend仍可使用但已弃用(启动时警告),--att_backend的项优先。文档、cookbook 和启动脚本已改用新参数。sparse_mla.prefill/decode=flashmla或fa3,不支持该格式时直接报错;flashmla_sparse和auto保留原来的按 cache 格式选择行为。GLM 层直接声明各阶段所需 provider。混合 Triton/FlashKDA 的 prefill 类命名为KDAPrefillBackend,FP8 稀疏 decode(含小 graph 的 Triton 路径)命名为FP8SparseMLADecodeBackend。topk的 FA3 稀疏 MLA decode。修复
6cefb3054softmax_scale(YaRN mscale 下结果错误)53b8776b36ddbca4c1b_image_token_start)b8585c12ac882436da254ebcb28out;GDN gate 多一次拷贝b3abf361d85cb10bbe1655e5f4d、c33728061out参数)的 FP8 与 BF16 prefill测试
(microbatch, batch size)的捕获与 replay 保持地址,值等于 eager 构造,不同 microbatch 不共享存储;并通过CudaGraph.capture_decode/replay在长度变化后验证。--att_backend解析、workspace 共享和分层规则各有单元测试。run_modefixture 失败排除 1 项。实验记录:261128-002615-data6-exp-logs-artifacts-pr1735-attention-famili。扩展回归发现的另外 11 项旧 fixture 失败已在干净c6b21fd78复现并修正(workspace manager 与投影 mock 参数名),上述通过数包含修正后的用例。run_modefixture 失败排除 1 项。实验记录:261127-202124-data6-exp-logs-artifacts-pr1735-attention-famili。test_fp8_state_preserves_kv_and_indexer_across_cpu_and_pd因 fixture 缺少run_mode失败,已在未修改的43c7613c9上复现。unit_tests/common、unit_tests/utils、unit_tests/models与多级 KV cache 测试:common 18 项失败、5186 项通过,models 27 项失败、1622 项通过,各 1 项收集错误。剩余失败在main(85ba752f0)上同样失败,属于环境(kernel 版本、数值容差、显存)或已有问题,例如 DeepSeek V4 kernel、GDN 数值、CPU cache 控制流,以及deepseek_v4/test_kernels.py与glm5_next/test_kernels.py同名导致的收集错误。git diff --check通过。已知限制
main新增的 DeepSeek V4.1 支持(Add DeepSeek V4.1 Flash model support #1758,58e18b323)尚未合入本分支,涉及 6 个文件冲突;更新前的999283385也有相同冲突。GitHub 当前显示CONFLICTING,本次提交未触发远端 CI;上述验证为本地记录。InferStateInfo.copy_for_cuda_graph仍按属性复制请求张量(跳过req_to_mtp_state_index);GDN/KDA 共用RecurrentDecodeMetadata及其构造函数。评审指南
提交按以下顺序分组,可逐组阅读:
backend 与迁移(
cf729774b…58684eb86):接口、各算法族 backend,以及 GQA 变体、MLA、稀疏 MLA、GDN、KDA 的迁移。修复与测试(
830cd6146…5f2ec2857):见上表;backend 数值、graph 约定和调用位置测试。移除 adapter(
753837a19…24f9efbdc):选择器、模型侧绑定,删除basemodel/attention。状态准备收敛(
2af81a98d…c33728061):每步状态在_create_inferstate中一次构建、输出写入out、manager 与命名整理,以及 FlashMLA 兼容修复。整理(
4d099a87f…c6b21fd78):跨步存储放到 backend 实例上、统一模块写法、phase、--att_backend、workspace 共享、分层规则与测试修复,以及 GQA 层调用收敛到GQATransformerLayerInferTpl(Llama、Bloom 不再各写一份),ALiBi kernel 移入common/attention/kernels/gqa/alibi,backend manager 按角色共享并按逻辑层查找;每步输入在模型侧直接准备,移除中间配置字典和转发函数;选择策略和角色内的实例复用集中到BackendSelector,注册循环只保留显式阶段遍历、层绑定和状态分组;模型容量直接放入InferStateInfo,共享 workspace 归 manager 持有,首次初始化命名为init_runtime。命名统一(
1f771ba30):统一 metadata 生命周期、分组键、配置字段、provider 与稀疏索引坐标;同步调用、测试和设计文档,移除残留的 StepBuilder 命名和测试包装。