已关闭
[Requirement|需求建议]: ForeachNeg算子支持DT_INT16/DT_INT8/DT_UINT8数据类型 #3020
hth810创建于  6月1日关闭于  27 天前
hth810
hth810
6月1日 创建

Thanks for sending an requirement! Please fill in the following template to help quickly solve your problem.

Backgroud(背景信息)

本需求为 foreach/foreach_neg 算子新增 DT_INT16DT_INT8DT_UINT8 数据类型支持,并保证 ACLNN 接口、算子原型、host 注册、tiling、AICore kernel、配置文件和文档中的支持范围一致。

算子功能仍是对输入 TensorList 做逐元素取负:

out[i] = 0 - x[i]

新增后,在 A2/A3 上,输入/输出 TensorList 除原有 FLOAT16FLOAT32BFLOAT16INT32 外,也可以使用 INT16INT8UINT8。Kirin 配置保持原支持范围,不扩展新增整数类型。

Origin(信息来源)

社区任务

Benefit / Necessity (价值/作用)

支持低位整数数据,减少模型中额外 cast、拆算子或回退路径,提升执行链路完整性。

该能力对量化/低精度场景更友好,尤其是 INT8UINT8 常用于量化模型输入、中间结果或轻量化计算。

foreach_neg 是 foreach 系列中的基础一元算子,常作为优化器、参数更新、量化前后处理等链路中的基础算子。补齐新增整数类型后,可以避免同一 TensorList 计算链路中因类型不支持而拆分执行。

Design(设计方案)

总体思路如下:

  • host 侧放开类型校验和配置;
  • tiling 侧按新增 dtype 生成正确 tiling key;
  • kernel 侧根据 dtype 选择合适的中间计算类型,并复用隐式输出一元模板完成 0 - x
  • 针对新增小整数类型处理 UB 对齐问题。

伪代码如下:

// host dtype 支持
support_tensor_dtype = {
    FLOAT16, FLOAT32, BFLOAT16, INT32,
    INT16, INT8, UINT8
}

// soc 支持范围
if soc in {ascend910_93, ascend910b, ascend950}:
    support_tensor_dtype includes {INT16, INT8, UINT8}
else if soc in {kirinx90, kirin9030}:
    keep original dtype set {FLOAT16, FLOAT32, INT32}

kernel 计算策略:

if dtype == INT16:
    // AICore 可 cast 到 float
    cast x: int16 -> float
    compute: float_out = 0.0 - x_float
    cast result: float -> int16

else if dtype == INT8 or UINT8:
    // AICore 不支持 int8/uint8 直接 cast 到 float
    cast x: int8/uint8 -> half
    compute: half_out = half(0) - x_half
    cast result: half -> int8/uint8

else if dtype == BFLOAT16:
    cast bf16 -> float
    compute: float_out = 0.0 - x_float
    cast back bf16

else:
    use original normal path:
    compute out = 0 - x

foreach_neg 使用 ForeachImplictOutputLevelZeroApi<T, P, Sub> 模板,其中 T 为输入/输出数据类型,P 为中间计算类型。新增类型对应关系如下:

INT16 -> T=int16_t, P=float
INT8  -> T=int8_t,  P=half
UINT8 -> T=uint8_t, P=half

同时针对新增小整数类型修复 UB 对齐和隐式输出模板中的标量块类型问题:

if solo_neg_op && dtype needs cast compute:
    reserve extra cast-compute temp UB
    inputsTensorUbSize align to 64B
else:
    inputsTensorUbSize align to 32B

// scalar zero block is allocated by compute type P
scalar_zero_ub = scalar_queue.AllocTensor<P>()
scalar_queue.EnQue(scalar_zero_ub)
scalar_queue.DeQue<P>()

cast-compute 路径会在 UB 内再切分临时计算区。如果只保证 32B 对齐,INT16INT8UINT8 在大 shape 分片场景下可能出现中间偏移未 32B 对齐,导致 device kernel 同步失败。foreach_neg 还需要额外维护一个值为 0 的标量块,该标量块按中间计算类型 P 分配和出队,避免 T != P 时 UB 队列类型不一致。

likedislike
hth810
hth810
6月1日 评论:

/assign

likedislike
CANN-robotCANN-robot成员
6月1日 将 hth810 设为负责人
hth810hth810
6月1日 关联了pull request:【社区任务】ForeachNeg算子支持DT_INT16/DT_INT8/DT_UINT8数据类型实现贡献
tangweiwei2成员
7月27日 评论:

@hth810 您好,该任务自 6 月 1 日以来已有一段时间未更新,想跟进一下当前进展:

  1. ForeachNeg 的 INT16/INT8/UINT8 数据类型扩展,host 侧配置和 kernel 侧计算路径开发是否已完成?
  2. 新增小整数类型的 UB 对齐处理是否有需要协助的地方?

欢迎同步最新状态,方便跟踪推进。感谢!

likedislike
hth810
hth810
7月28日 评论:

@hth810 您好,该任务自 6 月 1 日以来已有一段时间未更新,想跟进一下当前进展:

  1. ForeachNeg 的 INT16/INT8/UINT8 数据类型扩展,host 侧配置和 kernel 侧计算路径开发是否已完成?
  2. 新增小整数类型的 UB 对齐处理是否有需要协助的地方?

欢迎同步最新状态,方便跟踪推进。感谢!

@tangweiwei2

已完成并提交pr,待合并

likedislike
CANN-robotCANN-robot成员
27 天前 关闭了 issue
CANN-robotCANN-robot成员
27 天前 添加了label:resolved