已开启
[PROMPT-CONSERVATIVE][source #27] [泛化测试 G02] [Bug-Report|缺陷反馈]: im2col 的 div_rtn 模板以 int 承接 int64 商/余数,窄化截断 #63
xujiachen8创建于  9月16日
xujiachen8成员
9月16日 创建

本条为响应质量测试副本,来源 https://gitcode.com/xujiachen8/ops-math/issues/27 。

[泛化测试 G02] 测试副本,仅用于 gitcode-issue-handler 泛化测试。
来源:https://gitcode.com/cann/ops-math/issues/3190
原状态:open
原标题:[Bug-Report|缺陷反馈]: im2col 的 div_rtn 模板以 int 承接 int64 商/余数,窄化截断

Thanks for sending an issue! Please fill in the following template to help quickly solve your problem.

一、问题描述 (必填)

文件:行号:conversion/im2col/op_api/aclnn_im2col.cpp:47-60(定义)、:106-113(调用)
对照:experimental/conversion/im2col/op_api/aclnn_im2col.cpp:51-79(已用 __int128/SafeMul 做溢出防护)
代码片段(原样引用):
// aclnn_im2col.cpp:46-60
46 // Integer division rounding to -Infinity
47 template
48 static inline auto div_rtn(T x, T y) -> T
49 {
50 if (y == 0) {
51 OP_LOGE(ACL_ERROR_INVALID_PARAM, "Division by zero!");
52 return -1;
53 }
54 int q = x / y;
55 int r = x % y;
56 if ((r != 0) && ((r < 0) != (y < 0))) {
57 --q;
58 };
59 return q;
60 }
// aclnn_im2col.cpp:106-113(调用,T 实参为 int64_t)
106 int64_t outputHeight = div_rtn<int64_t>(
107 (inputHeight + 2 * (*padding)[0] - ((*dilation)[0] * ((*kernelSize)[0] - 1) + 1)),
108 (*stride)[0]) +
109 1;
110 int64_t outputWidth = div_rtn<int64_t>(
111 (inputWidth + 2 * (*padding)[1] - ((*dilation)[1] * ((*kernelSize)[1] - 1) + 1)),
112 (*stride)[1]) +
113 1;
缺陷描述:模板返回类型声明为 T(此处 int64_t),但函数体内 q、r 用 int(32 位)声明,对 64 位商/余数做隐式窄化;且 (*dilation)[i] * ((kernelSize)[i]-1)、2 * (padding)[i] 等乘法无溢出保护。当分子超过 INT32 范围时结果错误。对照实现(experimental 版本)已改用 __int128/SafeMul,证明该缺陷真实且修复方案已知。
触发条件:用户传入较大的 padding/kernelSize/dilation(仅校验 >0,未做上界约束),使 inputHeight + 2
padding - (dilation
(kernelSize-1)+1) 超出 INT32 范围(约 2.1e9)。
潜在影响:outputHeight/Width 计算错误 → 后续 outShape 与实际 out 比对错乱,出现「本应报错却通过」或「合法输入被误拒」;属静默数值/形状偏差。
修复建议:将 :54-55 的 q、r 改为 T;并参照 experimental 版本对分子乘法做溢出防护。

二、环境信息 (可选)

白盒检视不涉及

三、重现步骤 (可选)

白盒检视不涉及

四、预期结果 (可选)

无溢出问题

💡 备注(选填)

likedislike
xujiachen8成员
9月16日 评论:

您好,感谢反馈!

问题分析

代码核查确认,conversion/im2col/op_api/aclnn_im2col.cpp 的 div_rtn<T> 在调用点使用 T = int64_t,但商和余数仍分别保存到 int,存在 64 位结果窄化。该函数参与 outputHeight/outputWidth 计算;当 padding、kernelSize 或 dilation 使中间分子超出 32 位范围时,窄化会导致形状计算错误。当前实现中的相关乘加也没有像 experimental 版本那样使用更宽的中间类型进行溢出防护。

处理方向

一个可行方案是让商和余数使用模板类型 T,并参考 experimental/conversion/im2col/op_api/aclnn_im2col.cpp 的 __int128/边界检查方式保护有效 kernel、padding 和输入尺寸的中间计算;同时保留除零、正 stride 和输出尺寸为正等参数校验。修正后应覆盖大数参数、除零及正常尺寸的 shape 校验,维护团队将继续确认。

likedislike