已开启
[RFC]: 上库 eagle_quant_block_sparse_attention 算子和 plugin 适配 #197
lanwangli创建于  6月27日
lanwangli成员
6月27日 创建

状态 (Status): Draft
作者 (Authors): lanwangli
创建日期 (Created): 2026-06-27
更新日期 (Updated): 2026-06-27
相关 Issue/PR: 待关联


1. 概述

1.1 简介

本 RFC 提议在 MindIE-SD 中上库 eagle_quant_block_sparse_attention 算子及对应 plugin 适配,提供面向 Ascend 950 / arch35 的量化块稀疏 Attention 能力。该算子将量化 Attention 与 Block Sparse Attention 两类优化叠加:在计算路径上对 Q/K 采用 INT8 量化,对 P/V 相关路径采用 FP8 低精度表示,并通过 block sparse mask 或 index 表达稀疏块选择,从而降低长序列 Attention 的计算量、访存压力和中间张量开销。

MindIE-SD 侧通过 CANN 自定义算子目录交付 op_host、op_api、op_kernel 和 tiling 实现;通过 libPTAExtensionOPS.so 注册 torch.ops.mindiesd.eagle_quant_block_sparse_attention;并在 mindiesd.layers._custom_ops 提供 Python wrapper 与 fake op,供上层模型按稳定接口调用。

1.2 动机

图像/视频生成模型中,Attention 是端到端推理的核心耗时来源之一。随着分辨率、帧数和序列长度增长,全量 Attention 的计算和访存成本快速上升。已有块稀疏 Attention 可以跳过低价值 block,量化 Attention 可以降低矩阵计算和数据搬运成本,但单独使用任一方案仍难以覆盖长序列、高吞吐场景的性能诉求。

eagle_quant_block_sparse_attention 的目标是把两类收益叠加:

优化方向 设计点 预期收益
块稀疏 支持 int8 mask 与 int32 index 两种 block sparse 表达 减少无效 QK/PV block 计算
QK 量化 Q/K 使用 INT8 量化输入及 scale 降低 QK 矩阵计算与访存开销
PV 低精度 P/V 相关路径采用 FP8 低精度表示 降低 PV 阶段数据搬运和计算压力
Plugin 适配 暴露 torch.ops.mindiesd 与 Python wrapper 屏蔽底层 aclnn 调用细节,便于模型接入

不做该适配的影响包括:

影响项 说明
长序列 Attention 性能收益不足 仅依赖 dense 或非量化路径,难以同时获得稀疏和量化收益。
上层模型接入成本高 缺少 MindIE-SD plugin wrapper 时,需要直接处理底层 aclnn 参数和输出约束。
950 / arch35 能力无法沉淀 已验证的 QK INT8、PV FP8 块稀疏 Attention 能力无法通过统一工程上库复用。

1.3 目标

目标 设计要求
算子上库 新增 csrc/ops/eagle_quant_block_sparse_attention,包含 op_host、op_api、op_kernel、tiling 和 arch35 kernel。
量化稀疏能力 支持 Q/K INT8、PV FP8 的量化块稀疏 Attention 路径。
稀疏表达 支持 block sparse mask 和 index 两种模式。
Plugin 适配 注册 torch.ops.mindiesd.eagle_quant_block_sparse_attention,并提供 Python wrapper / fake op。
构建适配 在 CANN 9.x 自定义算子构建流程中启用该算子,并兼容 CANN 9.0/9.1 msprof include 布局。
测试验证 提供单算子 golden 测试,覆盖 mask / index 模式,并与 CPU reference 比较。

非目标

非目标 说明
不支持训练反向 当前设计聚焦推理前向。
不替代现有 BSA 路径 原有 block_sparse_attention bridge 保持不变,本算子作为新增 eagle quant BSA 路径。
不承诺所有 SOC 通用 当前核心目标是 Ascend 950 / arch35,其他 SOC 以 op_host AddConfig 与实际 kernel 支持为准。
不在本 RFC 中完成模型级调参 本 RFC 关注算子与 plugin 上库,模型端参数选择和端到端收益需在模型适配中验证。

2. 方案拆分

为降低单 PR 评审和合入风险,本 RFC 对应的改动按互不重叠的 5 个 PR 拆分:

顺序 内容 合入安全性
eqbsa-01 构建兼容性:补充 CANN msprof include 搜索路径 仅增加 include 路径,不启用新算子。
eqbsa-02 算子源码:新增 eagle_quant_block_sparse_attention 源码目录 只新增源码,不进入构建列表。
eqbsa-03 构建启用:加入 build_ops.sh 的 CANN 9.x 算子列表 依赖源码目录已合入后启用构建。
eqbsa-04 API 暴露:新增 plugin bridge、schema、Python wrapper/fake op 依赖算子源码和构建启用后对外暴露接口。
eqbsa-05 测试验证:新增单算子 golden 测试 依赖 API 暴露后运行验证脚本。

3. 接口说明

新增接口:

torch.ops.mindiesd.eagle_quant_block_sparse_attention(
    query, key, value, block_sparse_mask=None, block_shape=[128, 128],
    q_input_layout="BNSD", kv_input_layout="BNSD",
    num_key_value_heads=1, scale_value=1.0, inner_precise=4,
    actual_seq_lengths=None, actual_seq_lengths_kv=None,
    softmax_lse_flag=0,
    q_dequant_scale=None, k_dequant_scale=None, v_dequant_scale=None,
) -> (attention_out, softmax_lse)

核心约束:

  • 支持 BNSD / TND 布局,Q 和 KV layout 需一致。
  • TND 模式需要显式传入 actual_seq_lengthsactual_seq_lengths_kv
  • Q/K INT8 + PV FP8 量化路径需要传入对应 dequant scale。
  • Ascend 950 路径当前要求 inner_precise=4
  • block_sparse_mask 支持 int8 mask 或 int32 index。

4. 测试计划

测试项 说明
构建检查 验证 CANN 9.x 自定义算子构建可找到 msprof/profiling/toolchain 头文件。
单算子精度 使用 golden 脚本验证 mask / index 模式,与 CPU float reference 比较。
NaN/Inf 检查 golden 脚本对 CPU reference 与 NPU 输出执行 NaN/Inf 检查。
950 实机验证 原完整改动已在 Ascend 950 上完成验证。
pre-commit 各拆分 PR 需通过基础格式、ruff/pylint/bandit/typos 等门禁。

5. 风险与约束

风险 缓解措施
单 PR 过大影响评审 拆为 5 个互不重叠 PR,按构建兼容性、源码、构建、API、测试顺序合入。
构建顺序错误 eqbsa-03 依赖 eqbsa-02,eqbsa-04 依赖 eqbsa-02/03,eqbsa-05 依赖 eqbsa-04。
SOC/精度路径约束 PR 描述和 op_host 校验中明确 950 / inner_precise / dtype 约束。
格式门禁失败 按 pre-commit hook 格式化测试脚本和新增代码。
likedislike
Llanwangli成员
6月27日 添加了label:rfc
lanwangli成员
6月29日 评论:

感谢提交功能建议!本 Issue 已标记为 enhancement 并补充 ai-triaged 标签。\n评估状态:已记录,维护团队将在本周评估实现复杂度。\n\n该 RFC 涉及 eagle_quant_block_sparse_attention 算子的上库方案,建议后续在 PR 中补充设计评审记录和测试覆盖。\n\n---\n关联分析:发现 PR !394 ([Feature][op][eqbsa-05] add golden precision test) 可能与本 Issue 相关。\n该 PR 状态:open,目标分支:dev。\n建议维护者确认关联关系。

likedislike
ascend-robotascend-robot成员
7月3日 关联了看板:MindStudio ISSUE管理
Cchangetheway成员
7月14日 关联了看板:MindIE-SD
yjy_acyjy_ac成员
8 天前 关联了pull request:[Feature][op][eqbsa-01] support CANN msprof include layout
yjy_acyjy_ac成员
8 天前 关联了pull request:[Feature][op][eqbsa-02] add operator source tree
yjy_acyjy_ac成员
8 天前 关联了pull request:[Feature][op][eqbsa-03] enable operator build registration
yjy_acyjy_ac成员
8 天前 关联了pull request:[Feature][op][eqbsa-04] expose plugin and Python API
yjy_acyjy_ac成员
8 天前 关联了pull request:[Feature][op][eqbsa-05] add golden precision test
yjy_acyjy_ac成员
8 天前 关联了pull request:[Feature][op][eqbsa-01] support CANN msprof include layout
yjy_acyjy_ac成员
8 天前 关联了pull request:[Feature][op][eqbsa-02] add attention infra scaffolding
yjy_acyjy_ac成员
8 天前 关联了pull request:[Feature][op][eqbsa-03] add operator source and enable build
yjy_acyjy_ac成员
8 天前 关联了pull request:[Feature][op][eqbsa-04] expose plugin API and golden test
Llanwangli成员
8 天前 关联了pull request:[Feature][op][eqbsa-01] support CANN msprof include layout
Llanwangli成员
8 天前 关联了pull request:[Feature][op][eqbsa-02] add attention infra scaffolding
Llanwangli成员
8 天前 关联了pull request:[Feature][op][eqbsa-03] add operator source and enable build
Llanwangli成员
8 天前 关联了pull request:[Feature][op][eqbsa-04] expose plugin API and golden test