Pull Request has been successfully merged by CANN-robot.
(Thank you for zsq_888's contribution.)变更摘要
本 PR 为 segsum 算子在 arch35 平台新增了一个批处理模板,专门处理尾维长度为 2 的场景:当批次数量足够大时,把大量互相独立、长度为 2 的序列打平做向量化批量计算,从而提升该形状下的性能。改动涵盖 tiling 侧的模板选择与 batchTile 计算、新增的 SegsumBatchTwo kernel 模板类、新的 TilingKey 与模板参数声明、tiling data 常量与字段扩展,以及对应的 host UT 与离线 Python 模型校验。
主要改动
- 新增长度二维批量模板
SegsumBatchTwo:新增math/segsum/op_kernel/arch35/segsum_batch_two_arch35.h,用ArithProgression、Duplicate构造输入/输出 gather 索引表与常量值表,通过密集DataCopyPad搬运、Gather+Adds(0.0f)+Exp计算以及非 float 类型的Cast转换完成批量输出,各 batch 由不同向量 lane 独立处理。 - 新增 TilingKey 与模板调度分支:
segsum_struct.h增加SEGSUM_TPL_SCH_MODE_BATCH_TWO = 2并将schMode模板声明宽度扩到 2 位;segsum.cpp通过if constexpr (schMode == SEGSUM_TPL_SCH_MODE_BATCH_TWO)实例化新模板,其余分支保持原Segsum模板不变。 - tiling 侧增加批量模板的选择与容量计算:
segsum_tiling_arch35.cpp新增BATCH_TWO_TILING_KEY = 2,在tailDimLength == 2且averageBatches >= SEGSUM_MIN_BATCHES_ARCH35时按 UB 预算、批数上限与 64 对齐计算batchTile,条件成立则直接返回该 TilingKey,否则回退原有模板;FillTilingData同步写入batchTile。 - 扩展 tiling data 结构与常量:
segsum_tiling_data.h新增SEGSUM_BATCH_ALIGN_ARCH35、SEGSUM_MIN_BATCHES_ARCH35、SEGSUM_MAX_BATCH_ARCH35、SEGSUM_BATCH_CONSTANTS_ARCH35常量及SegsumTilingDataArch35::batchTile字段,segsum_tiling_arch35.h增加同名成员。 - 补充验证用例:新增 host UT 覆盖大数据量 T=2 走批量模板、阈值边界回退与原模板保持、其他尾维长度不受影响;新增
test_segsum_batch_model.py作为离线 NumPy 模型,校验索引生成、尾部归属、类型量化语义与形状/UB 门限。
🧩 本次 Review 已使用自定义 Skill:


代码审查
已完成全部 9 个变更文件的审查,3 条已上报。以下为最终结论。
逐文件审查结论
| 文件 | 结论 |
|---|---|
math/segsum/op_host/arch35/segsum_tiling_arch35.cpp |
无阻断问题。gate(tailDimLength == 2 && averageBatches >= 256)、UB 预算 6*dt+24+(非float?16:0) 与 kernel 实际 InitBuffer 逐项吻合(float 48B/序列、half/bf16 52B/序列 + 64B 常量池),std::max(int64_t{0}, …) 已挡住负值,bytesPerBatch ≥ 36 无除零,batchTile 回退时显式置 0,全部算术为 int64 |
math/segsum/op_host/arch35/segsum_tiling_arch35.h |
无问题(新增成员 batchTile = 0 已初始化) |
math/segsum/op_kernel/arch35/segsum.cpp |
无问题。if constexpr (schMode == 2) 条件依赖模板参数,discarded 分支不会实例化 SegsumBatchTwo,原 0/1 路径逐字保留 |
math/segsum/op_kernel/arch35/segsum_batch_two_arch35.h |
3 条 P3(见下);核心逻辑核对为正确:索引表 byte 偏移(4+8i 取 x[1],62+4b+2=(16+b)*4)、mask 0x99…(p%4∈{0,3}→1.0)/0x22…(p%4==1→0.0)、repeats=batchTile/16(arch35 fp32 一次 repeat = 64 元素 = 256B,high word=0 正确)、尾 tile count<batchTile 全部按 count 截断、GM 偏移 2*start/4*start 为 int64 无溢出、输出 [1,0,exp(x[1]),1] 与既有 T=2 语义(现有 case00011~ 走 row 模板)一致 |
math/segsum/op_kernel/arch35/segsum_struct.h |
无问题。ASCENDC_TPL_UINT_DECL(schMode, 1→2) 是位宽且为唯一字段,历史 key 0/1 数值不变(对照 dense_bincount/eltwise 等多值用例),未破坏 TilingKey 兼容性 |
math/segsum/op_kernel/arch35/segsum_tiling_data.h |
无问题(batchTile 追加在结构体末尾,符合追加不改序) |
math/segsum/tests/st/arch35/ttk_kernel_segsum_generalization.csv |
无问题。字段数与既有行一致('ND',)","{6}" 校验通过);_infnan/_negzero 命名沿用既有 golden 注入约定(case00985_infnan / case00992_negzero);16320/16321、69568/69632 等正是 avg=255→256 与 1024+64 满/尾 tile 边界 |
math/segsum/tests/ut/op_host/arch35/test_segsum_tiling_arch35.cpp |
无问题(新增 3 个用例的期望值经手算与实现一致,含 batchTile 对齐/上限、ownership 覆盖与 T=1/3/16/40000 回退) |
math/segsum/tests/ut/op_kernel/arch35/test_segsum_batch_model.py |
1 条 P3(常量池布局在模型内硬编码) |
发现汇总
- P0/P1/P2:0 条
- P3:3 条
test_segsum_batch_model.py:114-117—— 模型的常量池布局硬编码,索引表却从源码抽取;kernel 侧常量填充写反/写偏时模型仍通过(可选加固)segsum_batch_two_arch35.h:84-95—— 62/32/8 与共享常量SEGSUM_BATCH_CONSTANTS_ARCH35耦合但以裸字面量写死,未来改动会静默越界读(可选重构)segsum_batch_two_arch35.h:116-117——Adds(..., 0.0f, ...)经 IEEE 语义核对为可移除空操作(可选简化)
总体风险判断
低风险、可合入。 新增 T=2 批处理模板的 host/kernel 契约(batchTile、UB 预算、每核区间、索引表布局、尾块处理)经逐项核对自洽;GM 偏移与字节长度均为 int64/uint32 安全范围;原有 TilingKey 0/1 的数值与代码路径未被改动,回退边界(avg<256、T≠2、UB 不足、空张量)都有显式路径与 UT/ST 覆盖。我未发现可证的逻辑、安全或精度缺陷;3 条 P3 均为可选的可维护性/测试加固项。唯一无法在只读环境内确认的外部前提是 Duplicate 的 (repeatTimes, 1, 8) 与 Gather 的 byte-offset/元素计数语义——仓库内同类经典 API 用法(hans_encode 先乘 sizeof(int32_t) 再 Gather、hypot 用元素数作 repeatTimes、arch35 向量宽 64 元素/256B)与该写法一致,故不作为问题上报。
| 类型 | 数量 |
|---|---|
| 🔴 阻塞 | 0 |
| 🟡 建议 | 1 |
💬 仅评论
🧩 本次 Review 已使用自定义 Skill:


Thanks for your pull-request.
The full list of commands accepted by me can be found at here.
You can get sig-info at here.
You can self-configure the PR merge rules for this repository. For more details, please refer to Here.
For more, you also can visit HICANN.
PR Approval Progress
✅ Congratulations! All modules have met the lgtm and approve requirements.
Module Approval Details
| module | lgtm status | approve status |
|---|---|---|
| math/segsum | ✅ 汤平川, 张磊 (2/2) | ✅ 汤平川, 张磊 (2/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)
CLA Signature Pass
qq_57796320, thanks for your pull request. All authors of the commits have signed the CLA. 👍


描述
优化 Ascend 950 平台 Segsum 算子的大 batch、短序列场景性能。
原实现处理粒度较小,小块数据搬运、循环和同步开销较大,核内向量利用率不足。本次根据 UB 容量规划批处理大小,一次并行处理多条序列,减少重复搬运和同步;对序列长度为 1 的场景直接生成结果。同时改善 FP32 指数计算精度,保持每条序列原有的累加顺序。
关联的Issue
关联 Issue #4754:https://gitcode.com/cann/ops-math/issues/4754
测试
性能集:300 条精度全部 PASS。结合异常耗时用例的同包复测,298 条有效计时用例均达到单 case 性能要求,另外 2 条为空张量,无有效设备耗时。
0.4 × GPU耗时 / NPU耗时,性能比均值为 12.429×,最低为 0.133×,满足均值 > 0.5×、单 case ≥ 0.1× 的要求。文档更新
类型标签
AI/Agent生成声明