Closed
[Documentation|文档反馈]: aclnnForeachRoundOffNumber/V2 资料未声明 roundMode=7(实测=取小数部分,资料按「其他值→RNE」理解会算错) #8008
liuhanwen8Add on 19 days agoClosed at 10 days ago
19 days ago Added label: documentation
18 days ago Assign tramp-ll as responsible person
yuning_chen
18 days ago Comments:
18 days ago Comments:
初步分析
感谢反馈,我们已开始核对这个问题。
现象理解:aclnnForeachRoundOffNumber/V2 的资料未声明 roundMode=7 的语义,实测为取小数部分;按资料「其他值按 RNE 处理」的描述理解会算错。
当前判断:初步判断是资料对 roundMode 取值表的覆盖不全,需要对照 kernel 实现确认 7 的实际语义是否为对外承诺行为,待确认后再定是补资料还是收窄取值。
涉及范围:foreach/foreach_round_off_number 的 kernel roundMode 分支(含 arch35 SIMT 实现)与 aclnnForeachRoundOffNumber/V2 资料。
下一步:我们会对照实现列出 roundMode 全部取值的实际语义,再补齐资料或增加取值校验。


yuning_chen
18 days ago Comments:
18 days ago Comments:
您好,该问题已收到,当前问题 @tramp-ll 正在处理中


11 days ago Assign east_yang as responsible person, remove original responsible person: tramp-ll
11 days ago Assign east_yang as responsible person, remove original responsible person: tramp-ll
11 days ago Assign Tian_1122 as responsible person
11 days ago Removed responsible person east_yang
10 days ago Assign tramp-ll as responsible person
tramp-ll
10 days ago Comments:
10 days ago Comments:
最新文档资料已经更新roundMode=7说明


10 days ago changed custom state from 进行中 to 已完成
10 days ago Close issue
8 days ago Added label: resolved
现象
aclnnForeachRoundOffNumber/aclnnForeachRoundOffNumberV2的资料在roundMode取值说明里只列了 1~6,并写「当 roundMode 为其他时,如果精度损失会进行四舍六入五成双舍入操作,不涉及精度损失时则不进行舍入操作」。但实现(kernel 与 aclnn 两侧一致)把 7 定义为「取小数部分」,这是一个独立语义,不是「其他值走 RNE」。按资料理解
roundMode=7会得到完全不同的结果。同机同构造实测(输入
x = {0.5, 1.5, 2.5}):roundMode{0, 2, 2}{0, 1, 2}{1, 2, 3}{1, 2, 3}{0, 1, 2}{0, 2, 2}{0.5, 0.5, 0.5}x - trunc(x)根因定位(仓内源码,逐行)
foreach/foreach_round_off_number/op_kernel/foreach_round_off_number.h:33constexpr int8_t CAST_FRAC = 7;同文件
:106-111,ComputerPerCast对 7 走独立分支:if (roundModeValue == CAST_FRAC) { Cast(float32Tensor[maxCastDataCount], float32Tensor, RoundMode::CAST_TRUNC, dataCount); PipeBarrier<PIPE_V>(); Sub(float32Tensor[maxCastDataCount], float32Tensor, float32Tensor[maxCastDataCount], dataCount); PipeBarrier<PIPE_V>(); }即先
trunc再相减 = 取小数部分,与 RNE 无关。arch35 的 SIMT 实现同样如此,
arch35/foreach_round_off_number_simt.h:76-77:case CAST_FRAC: return value - truncf(value);两侧实现语义一致,说明「7 = 取小数部分」是有意设计的扩展能力,只是资料没写。
期望
二选一:
aclnnForeachRoundOffNumber.md/aclnnForeachRoundOffNumberV2.md的roundMode取值列表里补一条「当 roundMode=7,表示取输入的小数部分(
x - trunc(x))」,并明确它与「其他值走 RNE」的边界;default(RNE)并与资料对齐。附带的同类缺口(同一接口,一并核对)
同一份资料对
x与out的 dtype 列表内一致性一项,把错误码归在 561002 下:而
V2把同样的四项(shape 约束 / 列表内 dtype / 超 8 维 / roundMode 元素数)归到 161002:实测(同机同构造
x=fp32[2x3]ND|x_1=fp16[2x3]ND|out=fp32[2x3]ND):V1 返 561002、V2 返 161002,各自与自己的资料一致,但 V1/V2 这两份资料的归属互相矛盾 —— 同一个 l0 kernel,两个入口的文档给了两种错误码。建议一并统一(同族aclnnForeachAsin/Atan的同类问题见 #7945 / #7946)。环境
opp/version.info→Version=9.2.0,文件时间 2026-10-20)cann/ops-nnmaster12ad726c(2026-11-03 fetch)-std=c++17复现步骤
probe.cpp。g++ -O1 -std=c++17 -o probe probe.cpp \ -I${ASCEND_HOME_PATH}/include -I${ASCEND_HOME_PATH}/include/aclnnop \ -L${ASCEND_HOME_PATH}/lib64 -lascendcl -lnnopbase -lopapi -lpthread./probe <device_id> 7out = 0.5 0.5 0.5,而资料「其他值」口径应为0 2 2。复现脚本
实测输出(A2 / CANN 9.2.0 /
12ad726c,device 7):对照组(同二进制、同输入、只改 roundMode):
// probe.cpp —— aclnnForeachRoundOffNumber:roundMode=7 的实际语义 // 用法: ./probe <device_id> <roundMode> [n] #include <acl/acl.h> #include <aclnnop/aclnn_foreach_round_off_number.h> #include <cstdio> #include <cstdlib> #include <vector> int main(int argc, char **argv) { int dev = (argc > 1) ? atoi(argv[1]) : 0; int mode = (argc > 2) ? atoi(argv[2]) : 7; int n = (argc > 3) ? atoi(argv[3]) : 3; aclInit(nullptr); aclrtSetDevice(dev); aclrtStream stream = nullptr; aclrtCreateStream(&stream); // 输入全是平局点:0.5, 1.5, 2.5, ... std::vector<float> hostIn(n); for (int i = 0; i < n; ++i) { hostIn[i] = 0.5f + static_cast<float>(i); } int64_t dims[1] = {n}; const size_t bytes = n * sizeof(float); void *inDev = nullptr, *outDev = nullptr; aclrtMalloc(&inDev, bytes, ACL_MEM_MALLOC_HUGE_FIRST); aclrtMalloc(&outDev, bytes, ACL_MEM_MALLOC_HUGE_FIRST); aclrtMemcpy(inDev, bytes, hostIn.data(), bytes, ACL_MEMCPY_HOST_TO_DEVICE); aclTensor *xT = aclCreateTensor(dims, 1, ACL_FLOAT, nullptr, 0, ACL_FORMAT_ND, dims, 1, inDev); aclTensor *outT = aclCreateTensor(dims, 1, ACL_FLOAT, nullptr, 0, ACL_FORMAT_ND, dims, 1, outDev); aclTensorList *xList = aclCreateTensorList(&xT, 1); aclTensorList *outList = aclCreateTensorList(&outT, 1); int8_t modeI8 = static_cast<int8_t>(mode); int64_t rmDims[1] = {1}; void *rmDev = nullptr; aclrtMalloc(&rmDev, sizeof(int8_t), ACL_MEM_MALLOC_HUGE_FIRST); aclrtMemcpy(rmDev, sizeof(int8_t), &modeI8, sizeof(int8_t), ACL_MEMCPY_HOST_TO_DEVICE); aclTensor *rmT = aclCreateTensor(rmDims, 1, ACL_INT8, nullptr, 0, ACL_FORMAT_ND, rmDims, 1, rmDev); uint64_t wsSize = 0; aclOpExecutor *executor = nullptr; int ret = aclnnForeachRoundOffNumberGetWorkspaceSize(xList, rmT, outList, &wsSize, &executor); printf("mode=%d n=%2d RET_GWS=%d ws=%llu\n", mode, n, ret, static_cast<unsigned long long>(wsSize)); if (ret != 0) { return ret; } void *ws = nullptr; if (wsSize > 0) { aclrtMalloc(&ws, wsSize, ACL_MEM_MALLOC_HUGE_FIRST); } ret = aclnnForeachRoundOffNumber(ws, wsSize, executor, stream); printf("RET_EXEC=%d\n", ret); aclrtSynchronizeStream(stream); aclrtMemcpy(hostIn.data(), bytes, outDev, bytes, ACL_MEMCPY_DEVICE_TO_HOST); printf("out ="); for (int i = 0; i < n; ++i) { printf(" %.6g", hostIn[i]); } printf("\n"); aclDestroyTensorList(xList); aclDestroyTensorList(outList); aclrtFree(inDev); aclrtFree(outDev); aclrtFree(rmDev); aclrtFree(ws); aclrtDestroyStream(stream); aclrtResetDevice(dev); aclFinalize(); return 0; }