Pull Request已成功合入, 合并人@CANN-robot
(感谢 tianqiguang 的贡献)变更摘要
此 PR 主要更新了三个算子(deep_norm_grad、apply_adam_w_quant、dynamic_quant_update_scatter / dynamic_quant_update_scatter_v2)测试资产中的 golden 参考实现,使其适配新的测试框架接口(如额外的位置参数 *unused_outputs / *attr_values),同时调整了部分容差标准,并增强了 golden 函数对 PyTorch tensor 与 NumPy 混合输入场景的兼容性。
主要改动
-
deep_norm_gradgolden 新增纯 Torch 参考实现:新增_deep_norm_grad_torch_reference函数,提供基于原生 PyTorch 运算的完整参考实现;_DeepNormGradCompose.__call__改为调用该实现以替代原先的deep_norm_grad_aclnn_golden;同时deep_norm_grad_input和deep_norm_grad_aclnn_golden签名新增*unused_outputs以兼容新测试框架传入的额外输出参数。 -
apply_adam_w_quantgolden 增强 tensor/numpy 互操作与属性传参:新增_to_f32、_shape_of、_dtype_of、_cast_like等辅助函数,统一处理 tensor 与 numpy 的转换;apply_adam_w_quant_golden签名新增*attr_values以按位置接收超参(lr、beta1、beta2、weightDecay、eps、gnormScale、blockSize),并支持驼峰与下划线两种 key 名;customize_inputs拆分为 tensor(in-place)和 numpy 两条路径;uint8数据类型容差标准从"binary_equal"改为"quant"。 -
dynamic_quant_update_scattergolden 容差与参数处理调整:int8的 kernel 容差标准从"quant"改为"binary_equal";_DynamicQuantUpdateScatterCompose.__call__中对reduce和axis的获取由kwargs.get改为kwargs.pop,避免多余参数向下传递。 -
dynamic_quant_update_scatter_v2golden 修复 int4 检测与输出类型:_to_supported_torch_tensor中 int4 dtype 判断从精确匹配"int4"扩展为"int4" in str(arr.dtype) or arr.dtype.kind == "V",兼容更多 dtype 表示形式;_DynamicQuantUpdateScatterV2Compose.__call__返回值中第一个输出保持为 NumPy 数组(np.asarray),其余输出仍通过_to_supported_torch_tensor转为 tensor。


代码审查
现在我已经完整阅读了所有 4 个变更文件及其上下文。让我逐一总结每个文件的审查结果。
文件 1: norm/deep_norm_grad/tests/assets/golden.py
变更内容:
deep_norm_grad_input和deep_norm_grad_aclnn_golden增加了*unused_outputs参数以吸收框架可能传入的额外输出张量- 新增
_deep_norm_grad_torch_reference函数——纯 Torch 实现的金色参考 _DeepNormGradCompose.__call__从调用deep_norm_grad_aclnn_golden(NumPy 路径)切换为调用_deep_norm_grad_torch_reference(Torch 路径)
审查结论: 无问题。新函数 _deep_norm_grad_torch_reference 正确复刻了 deep_norm_grad_golden 的数学逻辑,使用了 Torch 原生算子(torch.sum、torch.where、torch.all),适合在 GPU 上运行。*unused_outputs 的加入是正确的兼容性改动,且 alpha/epsilon 在该签名中本来就有关键字参数的默认值,实际调用不受影响。kwargs.pop("alpha", self.alpha) 允许调用方覆盖 alpha 值,行为合理。
文件 2: optim/apply_adam_w_quant/tests/assets/golden.py
变更内容:
- 新增
import ml_dtypes以支持 bfloat16 的 NumPy 注册 uint8的 tolerance standard 从"binary_equal"改为"quant"(量化感知比较,更合理)_to_f32和_to_numpy_for_golden增加了对 Torch 张量和 bfloat16 的支持- 新增辅助函数
_shape_of、_dtype_of、_cast_like apply_adam_w_quant_golden签名增加*attr_values,支持灵活的属性名称映射(驼峰/下划线兼容)customize_inputs重写为同时支持 Torch 和 NumPy 的就地修改路径
审查结论: 无问题。属性名映射通过 kwargs.setdefault 实现(关键词参数优先级高于位置参数),驼峰/下划线双兼容的 fallback 设计合理。customize_inputs 的就地修改方式与旧代码的重新赋值方式在 TTK 框架中行为等价。_cast_like 正确区分了 Torch/NumPy 两种输出类型。
文件 3: quant/dynamic_quant_update_scatter/tests/assets/golden.py
变更内容:
_KERNEL_TOLERANCE中int8的 standard 从"quant"改为"binary_equal"(精确逐位比对)_DynamicQuantUpdateScatterCompose.__call__中kwargs.get改为kwargs.pop,防止reduce/axis重复传入 golden 函数
审查结论: 无问题。kwargs.get → kwargs.pop 是正确的 bug 修复——旧代码在 **kwargs 中包含 reduce/axis 时会导致 TypeError: got multiple values for argument,因为 dynamic_quant_update_scatter_golden 已将这些作为显式参数。int8 tolerance 改为 binary_equal 是合理的(int8 量化输出应为逐位精确)。
文件 4: quant/dynamic_quant_update_scatter_v2/tests/assets/golden.py
变更内容:
_to_supported_torch_tensor的 dtype 检测从arr.dtype.name == "int4"改为"int4" in str(arr.dtype) or arr.dtype.kind == "V"(更健壮的 int4 检测)_DynamicQuantUpdateScatterV2Compose.__call__的返回值从统一_to_supported_torch_tensor列表推导改为:第一个输出保持np.asarray(int4),后两个用_to_supported_torch_tensor转为 Torch 张量
审查结论: 无问题。第一个输出(int4 打包的 var)保持为 NumPy 数组是合理的——int4 类型在 PyTorch 中没有良好支持,转为 int8 会丢失精度语义。"int4" in str(arr.dtype) 比 arr.dtype.name == "int4" 更健壮(兼容不同的 dtype 字符串表示),arr.dtype.kind == "V" 作为额外的防御性检查覆盖了自定义 void dtype 的情况。
关闭总结
| 优先级 | 数量 |
|---|---|
| P0 | 0 |
| P1 | 0 |
| P2 | 0 |
| P3 | 0 |
总体风险判断:低。 四个文件的变更均为金色引用文件的维护性更新,包含:
- 签名兼容性改动(
*unused_outputs/*attr_values)以适配 TTK 框架的新参数传递方式; - 更健壮的 dtype 处理和 Torch/NumPy 双路径支持;
- 一个 bug 修复(
kwargs.get→kwargs.pop防止重复参数错误); - tolerance 策略调整。
所有变更逻辑一致,未发现正确性、安全性、可靠性或兼容性方面的问题。
| 类型 | 数量 |
|---|---|
| 🔴 阻塞 | 0 |
| 🟡 建议 | 2 |
💬 仅评论


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
PR Approval Progress
✅ Congratulations! All modules have met the lgtm and approve requirements.
Module Approval Details
| module | lgtm status | approve status |
|---|---|---|
| norm | ✅ 汤平川, 王星 (2/2) | ✅ 汤平川, 王星 (2/1) |
| optim | ✅ 王星, 汤平川 (2/2) | ✅ 汤平川, 王星 (2/1) |
| quant | ✅ 汤平川, 王星 (2/2) | ✅ 汤平川, 王星 (2/1) |
💡 Tip:
- Committer can comment
/approveor/lgtm- Commenting
/approveimplies both code review (lgtm) and intent to merge (approve)
CLA Signature Pass
tianqiguang, thanks for your pull request. All authors of the commits have signed the CLA. 👍


compile


流水线任务触发成功
任务链接 [6396558c363b422daef52c1bf18daec6][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| Compile_Ascend_X86_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_X86_mobile_station | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_single | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_experimental | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_X86_950 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM_950 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Pre | ✅ SUCCESS | >>>>> | |
| pre_comment | ✅ SUCCESS | >>>>> | |
| Compile_Ascend_X86 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_X86_mobile_station_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_X86_950_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_single_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_experimental_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_Ascend_ARM_950_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_X86_monitor_910b | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_X86_monitor_910c | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_X86_monitor_950 | ✅ SUCCESS | >>>>> | >>>>> |
| Compile_classify | ✅ SUCCESS | >>>>> | |
| Compile_Ascend_X86_mobile_station_9030_ubuntu24 | ✅ SUCCESS | >>>>> | >>>>> |
| UT_Test_ophost | ✅ SUCCESS | ||
| UT_Test_opapi | ✅ SUCCESS | ||
| UT_Test_kernel | ✅ SUCCESS | ||
| UT_Test_opgraph | ✅ SUCCESS | ||
| PreSmoke_A900 | ✅ SUCCESS | >>>>> | |
| API_Check | ✅ SUCCESS | >>>>> | |
| PreSmoke_ATK_Test_A2 | ✅ SUCCESS | >>>>> | |
| UT_Test_harmony-infer-chs-nn | ✅ SUCCESS | ||
| UT_Test_harmony-infer-chs-nn-1 | ✅ SUCCESS | ||
| UT_Test_harmony-infer-cpro-nn | ✅ SUCCESS | ||
| UT_Test_report_lcov | ⚠️ WARNING | >>>>> |
[2026-08-12 09:34:47] CI执行结束


流水线任务触发成功
任务链接 [2a019ba676b440df88b9679db936be2c][流水线指导]
| 任务名称 | 状态 | 日志 | 下载链接 |
|---|---|---|---|
| antipoison | ✅ SUCCESS | >>>>> | |
| codecheck_checkpr | ✅ SUCCESS | ||
| StaticCheck_codespell | ✅ SUCCESS | ||
| StaticCheck_link_validity | ✅ SUCCESS | ||
| StaticCheck_resource_existence | ✅ SUCCESS | ||
| StaticCheck_tag_closed | ✅ SUCCESS | ||
| StaticCheck_markdownlint | ✅ SUCCESS | ||
| codecheck_precommit | ✅ SUCCESS | >>>>> | |
| SCA | ✅ SUCCESS | >>>>> |
[2026-08-12 09:20:34] CI执行结束


🟡 Medium Priority
changed line 159–163: _DynamicQuantUpdateScatterV2Compose.__call__ 的返回值从统一使用 _to_supported_torch_tensor(out, device) 改为对 outputs[0] 仅调用 np.asarray(outputs[0]),而 outputs[1] 和 outputs[2] 仍通过 _to_supported_torch_tensor 转为 torch tensor 并放到正确 device。
受影响行为/契约:third-party compose 负责将 CPU golden 结果转为与输入一致的 torch tensor(参考同仓库 _ApplyAdamWQuantCompose 和 _DynamicQuantUpdateScatterCompose 的实现模式)。旧代码对所有输出统一转换;新代码让 outputs[0](int4 var 数组)保留为 CPU numpy 数组,与 outputs[1]/outputs[2](torch tensor,可能在 GPU 上)类型不一致。
失败模式:如果测试对比框架遍历所有输出并统一做 .cpu().numpy() 或 .to(device) 操作,outputs[0](已是 numpy)会因缺少这些方法而报错;或者对比时因 device 不一致导致隐式传输失败。
同时,_to_supported_torch_tensor 在本 diff 中已修复为可正确识别 int4 类型("int4" in str(arr.dtype) or arr.dtype.kind == "V"),但 compose 中却绕过它处理 outputs[0],这看起来是遗漏。
建议:将 outputs[0] 也通过 _to_supported_torch_tensor 转换,保持三个输出类型和 device 一致。
|
163 | + return [ |
|
164 | + _to_supported_torch_tensor(outputs[0], device), |
|
165 | + _to_supported_torch_tensor(outputs[1], device), |
|
166 | + _to_supported_torch_tensor(outputs[2], device), |
| 163
167 | ] |


🟡 Medium Priority
customize_inputs 在 tensor 分支中直接返回原地修改后的 tensor(可能位于 GPU),随后 apply_adam_w_quant_golden 接收这些 tensor 作为输入。第 151–152 行使用 np.asarray(m) / np.asarray(v) 直接转换,当 m / v 为 GPU tensor 时会抛出 TypeError: can't convert cuda:0 device type tensor to numpy。
同一函数中,其他输入(var, grad, qmap_m, qmap_v, absmax_m, absmax_v)在第 149–156 行均通过 _to_f32() 转换,该函数内部已通过 .cpu() 处理了 GPU tensor 场景。唯独 m 和 v 缺失 .cpu() 保护,构成不一致的遗漏。
触发条件: 测试框架提供 GPU tensor 输入 → customize_inputs tensor 分支返回 GPU tensor → apply_adam_w_quant_golden 执行到第 151 行即崩溃。
建议:为 m / v 增加 GPU-safe 路径:先用 .detach().cpu().numpy() 处理 tensor 输入,再构造 m_codes / v_codes。与同一函数中 _to_f32 的模式保持一致。
|
152 | + m_arr = m.detach().cpu().numpy() if torch.is_tensor(m) else np.asarray(m) |
|
153 | + m_codes = torch.as_tensor(m_arr.reshape(-1).copy(), dtype=torch.int64) |
|
154 | + v_arr = v.detach().cpu().numpy() if torch.is_tensor(v) else np.asarray(v) |
| 152
| - v_codes = torch.as_tensor( |
|
155 | + v_codes = torch.as_tensor(v_arr.reshape(-1).copy(), dtype=torch.int64) |


/lgtm
/approve


/lgtm
/approve


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


描述
更新 4 个算子的 golden 测试数据。
类型标签
关联的Issue