Closed
[Bug-Report|缺陷反馈]: aclnnSWhere self/other 均为 BOOL 且 out 非 BOOL 时第一段返回 561103(实测 561103,资料未声明该限制) #4286
liuhanwen8Add on 18 days agoClosed at 5 days ago
18 days ago Added label: bug-report
陈思
18 days ago Comments:
18 days ago Comments:
您好,感谢反馈!
问题分析
本次读取版本中,CheckParams 分别检查各 tensor 的 dtype 白名单与广播 shape;BOOL/BOOL 分支将输入创建为 INT8 view,调用 SelectV2 后把结果 view 恢复为 BOOL,随后直接 ViewCopy 到 out。该分支缺少通用分支中按 out->GetDataType() 执行的 Cast。因此 BOOL 结果与非 BOOL out 的写回路径存在类型衔接缺口,与您报告的 BOOL out 正常、INT8/FLOAT/INT32 out 返回 561103 的对照一致。具体 561103 运行结果来自您的 CANN 9.2.0 探针,本次未在该环境复跑。
核查源码为 cann/ops-math@8c1f633e3679f83b55a237c552f842cdcf80462f:math/select/op_api/aclnn_s_where.cpp。
处理方向
一个可行方向是在 BOOL 分支写回前按 out dtype 显式 Cast,并保留 BOOL out 的既有路径;还需核对非连续 out 和广播输出写回,避免仅修改 tensor view 的类型而没有实际转换数据。若产品约束确实不支持部分转换,则应在参数检查阶段明确拒绝并同步文档。
我们继续跟踪该问题。


18 days ago Assign chen_0715 as responsible person
18 days ago Added label: pending-confirmation
18 days ago Assign gcw_SUaZx3UQ as responsible person
18 days ago Removed responsible person chen_0715
15 days ago Associated pull request
5 days ago changed custom state from 进行中 to 已解决
5 days ago Close issue
现象
aclnnSWhere在 self 与 other 同为 BOOL、out 取 非 BOOL(INT8 / FLOAT / INT32)时,第一段接口返回 561103(内部错误),合法组合无法执行;而同样的 self/other 配 out=BOOL 正常返回 0,资料也未把这种组合列为不支持。对照(设备 npu:0,CANN 9.2.0,shape 均为 4×1):
三组缺陷用例的返回码完全一致,且与 out 的取值无关,只与 out 是否为 BOOL 有关。
期望
资料
math/select/docs/aclnnSWhere.md参数表三处 dtype 列的原文(A2 / A3 / 950 分组)都是同一份 15 类清单:self、other、out 三者各自都在支持面内,且资料只对 out 提了 shape 要求(「shape需要是self与other和condition broadcast之后的shape」),未要求 out 的 dtype 必须等于 self/other 的推导结果。实现内部对 out dtype 与推导结果不同的组合(FLOAT/FLOAT→BOOL 等)是支持的,因此 BOOL/BOOL→INT8 也应执行成功;若确实不支持,至少应返回 161002(参数非法),不应返回内部错误码 561103。
环境
Ascend910_9362),npu-smi26.1.1,固件 7.8.0.6.201Ascend-cann-toolkitV100R001C12SPC001B237),openEuler 22.03 LTS-SP4 / aarch64,g++ 10.3.1cann/ops-math3e0a1f15d7ce1bc163b862c0e29af1f7410b9bb6(git ls-remote origin refs/heads/master与本地一致)复现步骤
repro.cppg++ -std=c++17 -O2 repro.cpp -o repro -I$ASCEND_HOME_PATH/include -L$ASCEND_HOME_PATH/lib64 -lopapi_math -lopapi -lascendcl -lnnopbase./reproret=0,后三行应执行成功,实测均为ret=561103复现脚本
#include <cstdio> #include <vector> #include <unistd.h> #include "acl/acl.h" #include "aclnnop/aclnn_s_where.h" static size_t ESize(aclDataType dt) { switch (dt) { case ACL_FLOAT: case ACL_INT32: case ACL_UINT32: return 4; case ACL_DOUBLE: case ACL_INT64: case ACL_UINT64: return 8; case ACL_FLOAT16: case ACL_BF16: case ACL_INT16: case ACL_UINT16: return 2; default: return 1; } } static aclTensor* Mk(const std::vector<int64_t>& shape, aclDataType dt, void** dev) { int64_t n = 1; for (auto d : shape) n *= d; size_t bytes = static_cast<size_t>(n) * ESize(dt); aclrtMalloc(dev, bytes, ACL_MEM_MALLOC_HUGE_FIRST); aclrtMemset(*dev, bytes, 1, bytes); std::vector<int64_t> stride(shape.size(), 1); for (int64_t i = static_cast<int64_t>(shape.size()) - 2; i >= 0; i--) stride[i] = shape[i + 1] * stride[i + 1]; return aclCreateTensor(shape.data(), shape.size(), dt, stride.data(), 0, ACL_FORMAT_ND, shape.data(), shape.size(), *dev); } int main() { aclInit(nullptr); aclrtSetDevice(0); aclrtStream stream = nullptr; aclrtCreateStream(&stream); const aclDataType outs[] = {ACL_BOOL, ACL_INT8, ACL_FLOAT, ACL_INT32}; const char* names[] = {"BOOL", "INT8", "FLOAT", "INT32"}; for (int k = 0; k < 4; k++) { std::vector<int64_t> sh = {4, 1}; void *dc = nullptr, *ds = nullptr, *do_ = nullptr, *dout = nullptr; aclTensor* cond = Mk(sh, ACL_BOOL, &dc); aclTensor* self = Mk(sh, ACL_BOOL, &ds); aclTensor* other = Mk(sh, ACL_BOOL, &do_); aclTensor* out = Mk(sh, outs[k], &dout); uint64_t ws = 0; aclOpExecutor* exec = nullptr; int ret = static_cast<int>( aclnnSWhereGetWorkspaceSize(cond, self, other, out, &ws, &exec)); printf("self=BOOL other=BOOL out=%-5s -> ret=%d\n", names[k], ret); } fflush(stdout); _exit(0); }实测输出:
影响
PyTorch 语义下
torch.where(cond, a, b)允许目标 dtype 与 a/b 不同(由调用方的 out 决定),把布尔掩码结果直接写进 int8/float 缓冲区是常见写法。该组合现在直接失败且错误码是内部错误,调用方无法区分「参数非法」与「框架故障」,只能改写成两步 Cast 绕过,多一次 HBM 往返。建议
math/select/op_api/aclnn_s_where.cpp第 145-163 行的「self/other 同为 BOOL」快路径把两个输入SetDataType(DT_INT8)后调用SelectV2,再把结果 View 回 BOOL 直接ViewCopy到 out——该分支没有按 out 的实际 dtype 补一次 Cast。建议在selRes = l0op::ViewCopy(resView, out, ...)之前判断out->GetDataType() != DT_BOOL时先l0op::Cast到 out dtype;或统一去掉快路径,让 BOOL+BOOL 也走通用分支(通用分支已支持二次 Cast)。