已关闭
[Requirement|需求建议]: InplaceRsqrt 新增整型数据类型支持 #2156
Nice try创建于 7月7日关闭于 7月30日
7月7日 关联了pull request:【CANN开源开放社区任务】【社区任务】InplaceRsqrt 新增整型数据类型支持
7月7日 将 Nice_try 设为负责人
7月7日 修改了issue 的描述
7月7日 关联了pull request:【CANN开源开放社区任务】【社区任务】InplaceRsqrt 新增整型数据类型支持


Thanks for sending an requirement! Please fill in the following template to help quickly solve your problem.
一、背景信息 (必填)
当前 Rsqrt / InplaceRsqrt 算子仅支持浮点数据类型(float16、float、bfloat16),不支持整型和布尔类型。在 AscendC 算子的通用生态中,越来越多的模型和场景需要 Rsqrt 算子能够处理 int8、int16、int32、uint8、bool 等数据类型,以实现端到端的整型推理链路,避免频繁的类型转换开销。
二、价值/作用 (必填)
本需求为 Rsqrt / InplaceRsqrt 算子新增 int8、int16、int32、uint8、bool 五种数据类型的支持,带来以下价值:
三、设计方案 (必填)
总体思路
Rsqrt 算子的核心计算为
y = 1 / sqrt(x),对于浮点类型直接通过 AscendC 内置的Sqrt+Div原语完成;对于整型/布尔类型,通过类型提升(promotion)到 half/float → 执行 Rsqrt 计算 → CAST_RINT 截断回原类型的方式实现,同时对 int16/int8 引入分段线性校正保证结果精度。改动范围
涉及 5 个层次的修改:
1. 算子定义层(op_def / op_proto)
在 Input 和 Output 的 DataType 列表中新增
DT_INT8、DT_INT16、DT_INT32、DT_UINT8、DT_BOOL,注册格式为 ND。2. Tiling 层(op_host/rsqrt_tiling.cpp)
新增各数据类型在 UB 空间中的 Buffer 分配常量:
Tiling 函数
RsqrtTilingFunc的 switch 分支扩展至上述类型,其余逻辑(coreNum 计算、buffer 数量选择、tile 切分)保持通用。3. ACLNN API 层(op_api/aclnn_rsqrt.cpp)
DTYPE_SUPPORT_LIST覆盖全部 8 种类型(移除旧版独立仅浮点的DTYPE_OUT_LIST)CheckDtypeValid校验:self 和 out 的数据类型必须一致,避免 ViewCopy 出现未定义行为4. Kernel 层(op_kernel/rsqrt.h / rsqrt.cpp)
KernelRsqrt<DTYPE_X, BUFFER_NUM>模板类按TYPE_X分发至以下计算路径:Sqrt→Div(1, result)(保持原逻辑)Sqrt→Div→ Cast 回 bfloat16Rsqrt→Cast(CAST_RINT)写回 int32Rsqrt→ComputeIntCorrection(分段校正)→Cast(CAST_RINT)Rsqrt→Cast(CAST_RINT)Duplicate(0x0101)填充 int16_t 模式,每个输入元素输出 true其中
ComputeIntCorrection用于修正 int16/int8 的 Rsqrt 结果:此校正将
[0, 2]范围的 Rsqrt 结果压缩到正确范围,对齐 pytorch/tbe 的语义。rsqrt_tiling_data.h中将 tile/tail 相关字段从uint32_t升级为uint64_t,避免大 shape 下溢出。5. 测试层(tests/ut)
op_api 测试(16 个用例):覆盖全部支持类型的正常入参、空 tensor、非连续、高维、dtype 不匹配、shape 不一致、nullptr 等场景。
op_host 测试(8 个用例):infershape 推导 + tiling 参数计算验证(float、float16、bf16)。
op_kernel 测试(7 个用例):每个新增类型独立的端到端 ICPU 仿真测试(int8/int16/int32/uint8/bool 各 1 个,float32 覆盖单流水 + 双流水)。使用
rsqrt_test_entry.h共享入口模板通过宏拼接生成唯一函数名(如rsqrt_int8),避免多DTYPE_X文件的 ODR 冲突。数据生成脚本gen_data.py和对比脚本compare_data.py同时支持 8 种数据类型。伪代码示意
def rsqrt_kernel(x: Tensor[AnyDType], y: Tensor[AnyDType]): tiling = compute_tiling(x.shape, x.dtype) if x.dtype in {float16, float32}: y = 1.0 / sqrt(x) elif x.dtype == bfloat16: x_f32 = cast(x, float32) y_f32 = 1.0 / sqrt(x_f32) y = cast(y_f32, bfloat16, round_mode=CAST_RINT) elif x.dtype == int32: x_f32 = cast(x, float32) y_f32 = rsqrt(x_f32) y = cast(y_f32, int32, round_mode=CAST_RINT) elif x.dtype in {int16, int8}: x_f16 = cast(x, float16) y_f16 = rsqrt(x_f16) y_f16 = int_correction(y_f16) # min(2)→add(-1)→max(0)→mul(3)→sub y = cast(y_f16, original_dtype, round_mode=CAST_RINT) elif x.dtype == uint8: x_f16 = cast(x, float16) y_f16 = rsqrt(x_f16) y = cast(y_f16, uint8, round_mode=CAST_RINT) elif x.dtype == bool: y = fill(0x0101) # map to int16_t pattern, output True💡 备注(选填)