Pull Request has been successfully merged by CANN-robot.
(Thank you for F0rty-tw0's contribution.)变更摘要
本 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:


代码审查
审查结论
- [建议]
torch_extension/cann_ops_nn/csrc/quant/anti_hi_f4_quant.cpp:51fix: scale 的 dtype 未校验,却被强制标记为 ACL_HIFLOAT4_SCALE - [建议]
torch_extension/cann_ops_nn/csrc/quant/grouped_dynamic_hi_f4_quant.cpp:62fix: grouped 量化 scale 形状用 floor 除法推导,与实际 block 数不一致 - [建议]
torch_extension/cann_ops_nn/ops/quant/anti_hi_f4_quant/graph_convert_anti_hi_f4_quant.py:87fix: graph 模式 Bitcast scale 可能改变元素个数,与 eager 路径不一致 - [建议]
torch_extension/cann_ops_nn/csrc/quant/dynamic_block_hi_f4_quant.cpp:52fix: 最后一维为奇数时 y 的 acl 逻辑 shape 与 x 不再相等 - [建议]
torch_extension/cann_ops_nn/ops/quant/anti_hi_f4_quant/anti_hi_f4_quant.py:46fix: 反量化输出形状按 2 倍放大时未校验/处理最后一维奇偶 - [建议]
torch_extension/cann_ops_nn/csrc/quant/dynamic_hi_f4_quant.cpp:78fix: yScale 以 float32 分配却按 ACL_HIFLOAT4_SCALE 声明给 ACLNN - [建议]
torch_extension/cann_ops_nn/ops/quant/dynamic_hi_f4_quant/graph_convert_dynamic_hi_f4_quant.py:96fix: 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:119fix: block 图模式下 scale1/scale2 输出 dtype 与 meta 的 float32 不一致
| 类型 | 数量 |
|---|---|
| 🔴 阻塞 | 0 |
| 🟡 建议 | 8 |
💬 仅评论
🧩 本次 Review 已使用自定义 Skill:


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
/approveor/lgtm- Commenting
/approveimplies 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. 👍


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.


Pull Request 已合并或已关闭。
If you want to solve this problem, you can click here to do it in the FAQs.


描述
为已有 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。
测试
git diff --check通过。文档更新
新增四个接口的说明和示例,明确 torch 存储 dtype 与 ACL 逻辑 dtype 的关系。纠正 grouped 量化方向、补全
scale_alg=0原型,并说明 grouped scale 布局不能直接用于当前反量化接口。类型标签
AI/Agent生成声明