已合并
fix(foreach): AddListV2 整数溢出改为二进制补码回绕语义 #9151
Tian_1122创建于 8月25日
fix(foreach): AddListV2 整数溢出改为二进制补码回绕语义 #9151
已合并
Tian_1122创建于 8月25日
Tian_1122
Tian_1122
8月25日

描述

本 PR 修复 foreach_add_list(AddListV2)在 int16/int8/uint8 数据类型下的整数溢出语义,将其从原先经 AddListFloatAdapter 的浮点中间态计算改为二进制补码回绕(wrap-around)语义,以对齐 PyTorch CPU/GPU 行为。核心改动是新增内核模板类 ForeachAddListWrap(位于新增文件 foreach_add_list_wrap.h),并在 foreach_add_list.cpp 中将 TILING_KEY_IS(5)、(7)、(8) 三个分支的算子从 ForeachOneScalarTernary 切换为 ForeachAddListWrap。

主要改动

  • 新增 ForeachAddListWrap 内核类:在 foreach_add_list_wrap.h 中新增按数据块处理的 int16/int8/uint8 加法内核,包含 Init/Process/CopyIn/Compute/CopyOut 流水线,双缓冲队列及余量(remainder)数据处理逻辑。
  • int16 路径直接计算:T=int16_t 时在 int16 域内直接执行 Muls(inLocal2, inLocal2, alphaVal, dataCount) 与 Add,利用硬件二进制补码回绕语义,无需中间转换。
  • int8/uint8 提升计算后掩码回绕:T=int8_t/uint8_t 时先经 half 提升为 int16 精确计算 a + alpha * b,再回绕到低 8 位:uint8_t 通过 & 0x00FF 保留低 8 位,int8_t 通过 (v & 0x7F) - (v & 0x80) 映射到 [-128, 127]。
  • 算子入口替换:foreach_add_list.cpp 中 TILING_KEY_IS(5)/(7)/(8) 分支由 ForeachOneScalarTernary<int16_t, float, AddListFloatAdapter>、ForeachOneScalarTernary<int8_t, half, ...>、ForeachOneScalarTernary<uint8_t, half, ...> 分别替换为 ForeachAddListWrap<int16_t>、ForeachAddListWrap<int8_t>、ForeachAddListWrap<uint8_t>,并新增对 foreach_add_list_wrap.h 头文件的引用。

关联的Issue

#5169

测试

文档更新

类型标签

AI/Agent生成声明

likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 Tian_1122 的贡献)
Tian_1122Tian_1122
8月25日 创建了 pull request,commit a68c6f5e
atomgit-bot
atomgit-bot
8月25日 评论:

变更摘要

本 PR 修复 foreach_add_list(AddListV2)在 int16/int8/uint8 数据类型下的整数溢出语义,将其从原先经 AddListFloatAdapter 的浮点中间态计算改为二进制补码回绕(wrap-around)语义,以对齐 PyTorch CPU/GPU 行为。核心改动是新增内核模板类 ForeachAddListWrap<T>(位于新增文件 foreach_add_list_wrap.h),并在 foreach_add_list.cpp 中将 TILING_KEY_IS(5)、(7)、(8) 三个分支的算子从 ForeachOneScalarTernary 切换为 ForeachAddListWrap。

主要改动

  • 新增 ForeachAddListWrap<T> 内核类:在 foreach_add_list_wrap.h 中新增按数据块处理的 int16/int8/uint8 加法内核,包含 Init/Process/CopyIn/Compute/CopyOut 流水线,双缓冲队列及余量(remainder)数据处理逻辑。
  • int16 路径直接计算:T=int16_t 时在 int16 域内直接执行 Muls(inLocal2, inLocal2, alphaVal, dataCount) 与 Add,利用硬件二进制补码回绕语义,无需中间转换。
  • int8/uint8 提升计算后掩码回绕:T=int8_t/uint8_t 时先经 half 提升为 int16 精确计算 a + alpha * b,再回绕到低 8 位:uint8_t 通过 & 0x00FF 保留低 8 位,int8_t 通过 (v & 0x7F) - (v & 0x80) 映射到 [-128, 127]。
  • 算子入口替换:foreach_add_list.cpp 中 TILING_KEY_IS(5)/(7)/(8) 分支由 ForeachOneScalarTernary<int16_t, float, AddListFloatAdapter<float>>、ForeachOneScalarTernary<int8_t, half, ...>、ForeachOneScalarTernary<uint8_t, half, ...> 分别替换为 ForeachAddListWrap<int16_t>、ForeachAddListWrap<int8_t>、ForeachAddListWrap<uint8_t>,并新增对 foreach_add_list_wrap.h 头文件的引用。
likedislike
不准确?
atomgit-bot
atomgit-bot
8月25日 评论:

代码审查

✅ 未发现问题

likedislike
不准确?
CANN-robotCANN-robot成员
8月25日 添加了label:cann-cla/yes
CANN-robot
CANN-robot成员
8月25日 评论:

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
foreach ✅ 查建青, 唐玮玮, 任如海 (3/2) ✅ 查建青 (1/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

Tian_1122, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
此处折叠了117条消息 查看更多
zhajianqing123成员
8月26日 评论:

/approve

likedislike
CANN-robotCANN-robot成员
8月26日 添加了label:approved
CANN-robotCANN-robot成员
8月26日 关闭了关联的issue
CANN-robotCANN-robot成员
8月26日 合入了pull request
Chen_HaoWen成员
8月26日 评论:

/approve

likedislike