Merged
feat(torch_extension): 新增 HiF4 量化与反量化接口 #13394
feat(torch_extension): 新增 HiF4 量化与反量化接口 #13394
Merged
F0rty-tw0Add on 19 days ago
F0rty-tw0
F0rty-tw0Collaborators
19 days ago

描述

为已有 HiF4 算子新增 PyTorch 调用入口,提供 dynamic_hi_f4_quant、dynamic_block_hi_f4_quant、grouped_dynamic_hi_f4_quant 和 anti_hi_f4_quant 四个接口,包含 C++ ACLNN 封装、Python 注册、Meta 推导及 TorchAir GE converter。所有交付文件集中放在顶层 torch_extension/cann_ops_nn/。

公共封装增加 ACL_HIFLOAT4 的 4-bit 判断,以及 DType::HIFLOAT4、DType::HIFLOAT4_SCALE 枚举,使打包 HiF4 Tensor 进入已有逻辑 shape、stride 和 storage offset 转换。修正 Builder 构造函数和 __all__ 导出声明。本次不修改算子 kernel、tiling 或计算算法。

关联的Issue

关联 Issue #8000。

测试

  • 13 个 Python 文件静态 AST 解析通过。
  • Ruff 0.14.14 check、format 检查通过。
  • 4 个 C++ 文件和 1 个公共头文件通过 clang-format 18.1.8 检查。
  • git diff --check 通过。
  • 本地仅完成静态检查;未编译 C++、构建 wheel、导入执行本次接口或运行 NPU/TorchAir 测试。

文档更新

新增四个接口的说明和示例,明确 torch 存储 dtype 与 ACL 逻辑 dtype 的关系。纠正 grouped 量化方向、补全 scale_alg=0 原型,并说明 grouped scale 布局不能直接用于当前反量化接口。

类型标签

AI/Agent生成声明

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for F0rty-tw0's contribution.)
F0rty-tw0F0rty-tw0Collaborators
19 days ago Created pull request, commit f606bb58
atomgit-bot
atomgit-bot
19 days ago Comments:

变更摘要

本 PR 为已有 HiF4 算子补齐 PyTorch 侧调用入口,在 torch_extension/cann_ops_nn/ 下新增 dynamic_hi_f4_quant、dynamic_block_hi_f4_quant、grouped_dynamic_hi_f4_quant 与 anti_hi_f4_quant 四个接口,每个接口均包含 C++ ACLNN 封装、OpBuilder 注册 + Meta 推导 + PrivateUse1 实现、以及 TorchAir GE converter。同时扩展公共封装 aclnn_common.h,使 HiF4 打包 Tensor 能复用既有的 shape/stride/storage offset 处理逻辑。本次仅增加接口与注册,不涉及 kernel、tiling 或计算算法变更。

主要改动

  • 公共数据类型扩展: 在 aclnn_common.h 的 DType 枚举中新增 HIFLOAT4、HIFLOAT4_SCALE 偏移项,并把 ACL_HIFLOAT4 纳入 Is4BitDtype 的 4-bit 判定,使打包 HiF4 张量进入已有 4-bit 数据类型转换路径。
  • 四个 C++ ACLNN 接口封装: 新增 csrc/quant/ 下四个源文件,分别以 ACLNN_CMD 调用 aclnnDynamicHiF4Quant、aclnnDynamicBlockHiF4Quant、aclnnGroupedDynamicHiF4Quant、aclnnAntiHiF4Quant,并通过 TensorWrapper{y, ACL_HIFLOAT4} / {scale, ACL_HIFLOAT4_SCALE} 显式指定 ACL 数据类型;dynamic_block 输出额外传入 packedDim。
  • 输出 shape 与参数校验约定: C++ 与 Python 两侧统一按 64 元素 block、每字节 2 个 FP4 计算输出维度(CeilDiv(..., 2)、CeilDiv(..., 64) 并追加尾维 1),并对 axis 范围、round_mode(rint/trunc)、scale_alg(0/1)、dst_type(1/27)、group_index 的 rank/非空/int32 等做前置校验。
  • PyTorch 注册与 Meta 推导: 每个接口目录下新增 OpBuilder 子类,通过 schema、sources、register_meta 声明函数签名、编译源文件与 Meta 实现,并以 @impl(..., "PrivateUse1") 转发到已加载的扩展模块;ops/quant/__init__.py 汇总导出四个接口。
  • TorchAir GE converter 与 HiF4 打包/解包: 各 graph_convert_* 文件在 torchair 可用时注册 GE converter,构造对应 ge_op(IR 中声明 DT_HIFLOAT4 / DT_HIFLOAT4_SCALE),并通过 _pack_hif4_to_uint8 / _unpack_uint8_to_hif4 用 Bitcast + Reshape 在 uint8 打包视图与 HIFLOAT4 视图间转换;torchair 不可用时退化为抛出 RuntimeError。

🧩 本次 Review 已使用自定义 Skill:

likedislike
Incorrect?
atomgit-bot
atomgit-bot
19 days ago Comments:

代码审查

审查结论

  • [建议] torch_extension/cann_ops_nn/csrc/quant/anti_hi_f4_quant.cpp:51 fix: scale 的 dtype 未校验,却被强制标记为 ACL_HIFLOAT4_SCALE
  • [建议] torch_extension/cann_ops_nn/csrc/quant/grouped_dynamic_hi_f4_quant.cpp:62 fix: grouped 量化 scale 形状用 floor 除法推导,与实际 block 数不一致
  • [建议] torch_extension/cann_ops_nn/ops/quant/anti_hi_f4_quant/graph_convert_anti_hi_f4_quant.py:87 fix: graph 模式 Bitcast scale 可能改变元素个数,与 eager 路径不一致
  • [建议] torch_extension/cann_ops_nn/csrc/quant/dynamic_block_hi_f4_quant.cpp:52 fix: 最后一维为奇数时 y 的 acl 逻辑 shape 与 x 不再相等
  • [建议] torch_extension/cann_ops_nn/ops/quant/anti_hi_f4_quant/anti_hi_f4_quant.py:46 fix: 反量化输出形状按 2 倍放大时未校验/处理最后一维奇偶
  • [建议] torch_extension/cann_ops_nn/csrc/quant/dynamic_hi_f4_quant.cpp:78 fix: yScale 以 float32 分配却按 ACL_HIFLOAT4_SCALE 声明给 ACLNN
  • [建议] torch_extension/cann_ops_nn/ops/quant/dynamic_hi_f4_quant/graph_convert_dynamic_hi_f4_quant.py:96 fix: graph 模式下 scale 输出被标为 HIFLOAT4_SCALE,与 meta/单算子模式的 float32 不一致
  • [建议] torch_extension/cann_ops_nn/ops/quant/dynamic_block_hi_f4_quant/graph_convert_dynamic_block_hi_f4_quant.py:119 fix: block 图模式下 scale1/scale2 输出 dtype 与 meta 的 float32 不一致
类型 数量
🔴 阻塞 0
🟡 建议 8

💬 仅评论


🧩 本次 Review 已使用自定义 Skill:

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
19 days ago Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
19 days ago Comments:

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
quant ✅ 汤平川, 苏跃明, 唐玮玮, 陈展熹, 钱泽洪 (5/2) ✅ 汤平川, 陈展熹, 钱泽洪 (3/1)
torch_extension ✅ 唐玮玮, 汤平川 (2/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

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

likedislike
75 messages are collapsed here。 See more
CANN-robot
CANN-robotCollaborators
18 days ago Comments:

The MR can not be merged, because of CodeReview discussion not resolved

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
焦一鸣焦一鸣Collaborators
18 days ago Resolved final issue
CANN-robotCANN-robotCollaborators
18 days ago Closed associated issue
CANN-robotCANN-robotCollaborators
18 days ago Merged pull request
CANN-robot
CANN-robotCollaborators
18 days ago Comments:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike