Skip to content

refactor: inherit TRTLLMHAAttnBackend directly from AttentionBackend (#28808) - #44117

Closed
Asthenia0412 wants to merge 1 commit into
sgl-project:mainfrom
Asthenia0412:refactor/sglang-28808-trtllm-inheritance
Closed

Asthenia0412 wants to merge 1 commit into
sgl-project:mainfrom
Asthenia0412:refactor/sglang-28808-trtllm-inheritance

Conversation

@Asthenia0412

@Asthenia0412 Asthenia0412 commented Oct 13, 2026 •

Copy link
Copy Markdown

Problem

TRTLLMHAAttnBackend 继承自 FlashInferAttnBackend,但实际上只复用了少量 init 代码。这导致每次实例化 TRTLLMHAAttnBackend 时都会浪费大量资源:

  • 分配 FlashInfer prefill_wrapper_ragged, prefill_wrappers_paged, prefill_wrappers_verify, decode_wrappers (BatchPrefillWithPagedKVCacheWrapper, BatchDecodeWithPagedKVCacheWrapper 等)
  • 创建 FlashInferIndicesUpdaterPrefill / FlashInferIndicesUpdaterDecode
  • 分配 kv_indptr, kv_last_page_len, qo_indptr 等 FlashInfer 专用缓冲区
  • 设置 decode_use_tensor_cores, num_wrappers, dispatch_reason, enable_deterministic 等 TRTLLM 从不使用的标志

TRTLLMHAAttnMultiStepDraftBackend 继承自 FlashInferMultiStepDraftBackend,后者同样不是 AttentionBackend 子类,只是实现了 init_forward_metadata/init_cuda_graph_state 等接口方法。

Solution

TRTLLMHAAttnBackend

  • 改为直接继承 AttentionBackend (base class)
  • 将真正共享的池/翻译器/量化器初始化代码直接内联到 init 中
  • 将 _kv_write_scales 方法 (仅 3 行) 复制到本类
  • 在自身 _swa_kv_pool 赋值后直接设置 use_sliding_window_kv_pool
  • 删除了 flashinfer_backend.py 的 import 依赖

TRTLLMHAAttnMultiStepDraftBackend

  • 独立化 (不再继承 FlashInferMultiStepDraftBackend)
  • 内联了 init 中 kv_indptr/kv_last_page_len 的分配逻辑
  • 不再加载 generate_draft_decode_kv_indices, common_template 等 FlashInfer 专用逻辑

Changes

仅修改了一个文件: python/sglang/srt/layers/attention/trtllm_mha_backend.py (+52 / -18)

Testing

  • Python 语法检查通过 (ast.parse)
  • 所有外部调用者 (attention_registry, draft_utils, eagle_worker_v2 等) 的导入路径不变, 行为不变

Fixes: #28808
Author: yancy (Asthenia0412)


CI States

Latest PR Test (Base): ❌ Run #38461119613
Latest PR Test (Extra): ❌ Run #38461119568
Latest PR Test (AMD ROCm 10): ❌ Run #38461119645

…28808)

Remove unnecessary inheritance from FlashInferAttnBackend since
TRTLLM path only reuses a tiny portion of the parent __init__.
Now inherits directly from base AttentionBackend, avoiding wasted
allocation of FlashInfer wrappers and updaters that TRTLLM never uses.

Author: yancy (Asthenia0412)
Signed-off-by: Asthenia0412 <yancy@users.noreply.github.com>
@github-actions github-actions Bot added the blackwell SM100/SM120 label Oct 13, 2026
@Asthenia0412

Copy link
Copy Markdown
Author

Hey @reviewers! I've refactored TRTLLMHAAttnBackend and TRTLLMHAAttnMultiStepDraftBackend to inherit directly from AttentionBackend instead of FlashInferAttnBackend. The FlashInfer inheritance was a massive waste since TRTLLM overrides 95% of parent methods and never uses FlashInfer wrappers/updaters. Just inherited what TRTLLM actually needs (pool setup + kv_indptr buffers).

Could you take a quick look? The change is localized to one file and fully backward-compatible.

@Asthenia0412

Copy link
Copy Markdown
Author

Hi! The CI currently shows skipped jobs because this repo requires the run-ci label to trigger the full test suite. The failing jobs you see are just the CI gating placeholders (call-gate/pr-gate, pr-test-*-finish) — real tests haven't run yet.

Could someone please add the run-ci label to kick off the actual test matrix? Thanks so much! 🙏

@lm-sys @zhyncs @hnyls2002 @ispobock @BBuf @Fridge003 @kevin-mii

@hnyls2002

Copy link
Copy Markdown
Collaborator

Thanks for your interest in SGLang. This account opened 16 PRs in about eight hours, across unrelated subsystems (attention backends, speculative decoding, diffusion loading, scheduler IPC, KV-cache compression, docs), including two PRs for the same change (#44073 and #44117, both for #28808) and a fix for #43929 that is already on main. We can't review contributions at this volume, so we are closing the ones still open. If you want to pursue one, please pick a single change, verify it on a real run (a repro, a test, or numbers), and open a fresh PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

blackwell SM100/SM120

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor: TRTLLMHAAttnBackend should not inherit from FlashInferAttnBackend when reuse is small

3 participants