| Files | Last commit | Last update |
|---|---|---|
rsqrt add support int datatype in A2/A3 Co-authored-by: Nice_try<nicetryzzw@163.com> # message auto-generated for no-merge-commit merge: !3863 merge submit-InplaceSqrt into master 【CANN开源开放社区任务】【社区任务】InplaceRsqrt 新增整型数据类型支持 Created-by: Nice_try Commit-by: Nice_try Merged-by: cann-robot Description: ## 描述 ### 改动背景 Rsqrt/InplaceRsqrt 算子原仅支持浮点类型(float16、float、bfloat16),本次 PR 将其数据类型支持扩展至全部整型和布尔类型(int8、int16、int32、uint8、bool),对齐 AscendC 算子的通用数据覆盖范围。 ### 改动内容 #### 1. 算子定义层(op_def / op_proto) - **op_host/rsqrt_def.cpp**:Input/Output 的 DataType 列表新增 DT_INT8、DT_INT16、DT_INT32、DT_UINT8、DT_BOOL - **op_graph/rsqrt_proto.h**:INPUT/OUTPUT 的 TensorType 列表新增上述整型和布尔类型 #### 2. 算子 Tiling 层(op_host/rsqrt_tiling.cpp) - 新增 DATA_NUM_INT32 = 2、DATA_NUM_INT16 = 2、DATA_NUM_INT8 = 6、DATA_NUM_UINT8 = 4、DATA_NUM_BOOL = 2 五个常量,用于各数据类型在 UB 空间中的 Buffer 分配计算 - 在 switch 分支中新增对应数据类型到 ubDataNumber 的映射 - tiling 函数已扩展覆盖全部新增类型,无需额外修改注册逻辑 #### 3. 算子 ACLNN API 层(op_api/aclnn_rsqrt.cpp) - DTYPE_SUPPORT_LIST 移除旧版的独立 DTYPE_OUT_LIST(仅浮点),统一为一个支持全部 8 种类型的列表 - 移除 CheckDtypeValid 中不必要的 OUTPUT 类型检查重复逻辑 - 移除多余的 Cast 中间转换序列(由 rsqrt.cpp 内部统一处理) - **新增关键校验**:CheckDtypeValid 增加 self->GetDataType() != out->GetDataType() 检查,确保 self 和 out 的数据类型必须一致,避免 ViewCopy 在 dtype 不匹配时出现未定义行为 #### 4. 算子 Kernel 层(op_kernel/rsqrt.h / rsqrt.cpp) - rsqrt_tiling_data.h:RsqrtTilingData 结构体将 tile/tail 相关字段从 uint32_t 升级为 uint64_t,避免大 shape 下溢出 - rsqrt.h:KernelRsqrt 模板类新增 ComputeImpl 分派函数,按 TYPE_X 类型分发至以下计算函数: - **int32**:Cast 到 float → Rsqrt → Cast(CAST_RINT) 写回 int32 - **int16**:Cast 到 half → Rsqrt → ComputeIntCorrection(Mins/Adds/Maxs/Muls/Sub 校正)→ Cast(CAST_RINT) 写回 int16 - **int8**:Cast 到 half → Rsqrt → ComputeIntCorrection → Cast(CAST_RINT) 写回 int8 - **uint8**:Cast 到 half → Rsqrt → Cast(CAST_RINT) 写回 uint8 - **bool**:直接 Duplicate(0x0101) 填充 int16_t 模式,每个输入元素输出 true - ComputeIntCorrection 为 int16/int8 类型提供分段线性校正:将 [0, 2] 范围的 Rsqrt 结果通过 min(2)→add(-1)→max(0)→mul(3)→sub 的校正公式压缩到正确范围 - rsqrt.cpp:扩展 REGISTER_TILING_DEFAULT 和 DTYPE_X 宏路径,单入口 rsqrt<schMode> 模板支持 schMode=0(双缓冲)和 schMode=1(单缓冲) #### 5. 测试层(tests/ut) **op_api 测试**(test_aclnn_rsqrt.cpp): - 16 个测试用例,覆盖 int16、float16、bf16、float、int32、uint8 等类型的正常入参 - 覆盖不同 shape、空 tensor、非连续、低维/高维、inplace 等场景 - 覆盖 dtype 不匹配(ACLNN_ERR_PARAM_INVALID)、shape 不一致、nullptr 等错误路径 **op_host 测试**: - test_rsqrt_infershape.cpp:3 个测试用例覆盖 float、float16、bf16 的 shape 推导 - test_rsqrt_tiling.cpp:5 个测试用例覆盖不同 shape(8x8、8x2048、1023x2047)和数据类型(float、float16、bf16)的 tiling 参数计算 **op_kernel 测试**: - test_rsqrt.cpp:float32 单缓冲(单流水)测试 + float32 双缓冲(双流水)测试,均检查 system() 返回值 - test_rsqrt_int8.cpp:int8 类型 kernel 端到端测试 - test_rsqrt_int16.cpp:int16 类型 kernel 端到端测试 - test_rsqrt_int32.cpp:int32 类型 kernel 端到端测试 - test_rsqrt_uint8.cpp:uint8 类型 kernel 端到端测试 - test_rsqrt_bool.cpp:bool 类型 kernel 端到端测试 - 使用 rsqrt_test_entry.h 共享入口模板(宏拼接生成唯一函数名 rsqrt_int8、rsqrt_int16 等),避免多 DTYPE_X 文件的 ODR 冲突 - gen_data.py / compare_data.py:同时支持 8 种数据类型的 golden 数据生成与结果比对,整型使用 float32 精度计算对齐 AscendC Rsqrt 结果 ## 关联的Issue [#2156](https://gitcode.com/cann/ops-math/issues/2156) ## 测试 - **UT 测试**(全部通过): - --opapi:16/16 ✅ - --ophost:8/8 ✅ - --opkernel:7/7 ✅ - **AscendOpTest 测试**:已通过算子泛化测试 - **测试覆盖的数据类型**:float16、float、bfloat16、int8、int16、int32、uint8、bool ## 文档更新 - 更新了 README.md 文件,补充新增的整型/布尔类型支持说明 - 更新了 docs/aclnnRsqrt&aclnnInplaceRsqrt.md 文件 ## 类型标签 - [ ] Bug修复 - [x] 新特性 - [ ] 性能优化 - [x] 文档更新 - [ ] 其他,请描述: See merge request: cann/ops-math!3863 | 3 months ago |