Closed
[Bug-Report|缺陷反馈]: [aclnnMseLossOut] 空 tensor 输入返回成功但 out 未被写出(整块残留未初始化显存) #7542
liuhanwen8Add on 24 days agoClosed at 11 days ago
24 days ago Added label: bug-report
yuning_chen
24 days ago Comments:
24 days ago Comments:
24 days ago Assign ji-songyuan as responsible person
yuning_chen
24 days ago Comments:
24 days ago Comments:
您好,问题已收到,当前 @ji-songyuan 正在处理中。


11 days ago Associated pull request
佐助大王
11 days ago Comments:
11 days ago Comments:
该接口没有竞品实现,只有aclnnMseLoss有对应的实现,所以不存在污染现象


11 days ago changed custom state from 进行中 to 已完成
11 days ago Close issue
6 days ago Added label: resolved
3 days ago Assign chenjiayi_ as responsible person
chenjiayi_
3 days ago Comments:
3 days ago Comments:
补充说明本问题的后续处理:
已修复并随关联 PR 合入。
处理结果:
- !14080:fix(mse_loss): MseLossOut空tensor输入按reduction语义填充输出
验证:
- 关联 PR(2026-11-16)已合入,变更可从 PR 记录追溯。
- 本次以 PR 记录与 issue 讨论核对为主,未重新执行复现或真机验证。
说明:合入分支与版本以 PR 记录为准;如仍有问题,请重新提 issue 并附版本、shape 与日志。
如果还有后续需要,请重新提 issue。


18 hours ago Removed responsible person chenjiayi_
现象
aclnnMseLossOut在 self/target 含 0 元素、reduction=1(mean) 或 2(sum) 时,两段接口都返回 0,但 out 一个字节都没被改写,用户拿到的是 device 上未初始化的旧内容。实测(Ascend 910B2 / CANN 9.2.0 / ops-nn
711a4f5d,out 在 device 上预填 -777.0 当金丝雀):canary_left = n/n表示 out 的 n 个元素全部仍是预填值。同仓aclnnMseLoss在同一组入参下 out 得到NaN(reduction=1) /0(reduction=2)。成因:
loss/mse_loss/op_api/aclnn_mse_loss_out.cpp的早退分支if (self->IsEmpty() || target->IsEmpty())里只有一行注释// 根据实际支持情况补充,没有填充动作;而aclnn_mse_loss.cpp同一位置调用MseLossEmptyTensorCompute,用loss/common/level2_base_loss.h的CheckFillScalarLoss按 reduction 填 NaN/0(aclnnL1Loss、aclnnSoftMarginLoss同)。另一个独立入口(valid-fuzz 冒烟,out 预填 0xA5)也判出输出未写出(仍是预填金丝雀):out(len=4, a5a5a5a5)。期望
reduction=1 → out 全 NaN;reduction=2 → out 全 0;reduction=0 → out 本身是空张量,不写是正确行为。与同仓
aclnnMseLoss及 PyTorchmse_loss的空输入语义一致(资料未声明空 tensor 语义,另见 #7466)。环境
cann-9.2.0);算子仓 cann/ops-nn711a4f5d(loss/mse_loss)复现步骤
aclnnMseLossOutGetWorkspaceSize,记录返回码与 workspaceSizeaclnnMseLossOut+aclrtSynchronizeStream,记录返回码复现脚本
实测输出(本脚本跑出的就是下面五行,与「现象」一致):
// g++ -std=c++17 -O1 -o repro repro.cpp -I$ASCEND_TOOLKIT_HOME/include -L$ASCEND_TOOLKIT_HOME/lib64 \ // -lascendcl -lnnopbase -lopapi -Wl,--allow-shlib-undefined && ./repro 0 #include <cstdio> #include <vector> #include "acl/acl.h" #include "aclnnop/aclnn_mse_loss_out.h" static aclTensor* Make(const std::vector<int64_t>& shape, void** dev, float fill) { int64_t n = 1; for (auto v : shape) n *= v; std::vector<int64_t> strides(shape.size(), 1); for (int64_t i = (int64_t)shape.size() - 2; i >= 0; --i) strides[i] = shape[i + 1] * strides[i + 1]; size_t bytes = (size_t)(n > 0 ? n : 1) * 4; aclrtMalloc(dev, bytes, ACL_MEM_MALLOC_HUGE_FIRST); std::vector<float> h((size_t)(n > 0 ? n : 1), fill); aclrtMemcpy(*dev, bytes, h.data(), bytes, ACL_MEMCPY_HOST_TO_DEVICE); std::vector<int64_t> storage{n > 0 ? n : 1}; return aclCreateTensor(shape.data(), shape.size(), ACL_FLOAT, strides.data(), 0, ACL_FORMAT_ND, storage.data(), storage.size(), *dev); } static void Run(const char* name, std::vector<int64_t> ssh, std::vector<int64_t> tsh, std::vector<int64_t> osh, int64_t red) { void *sp = nullptr, *tp = nullptr, *op = nullptr; aclTensor* self = Make(ssh, &sp, 1.0f); aclTensor* target = Make(tsh, &tp, 0.0f); aclTensor* out = Make(osh, &op, -777.0f); uint64_t ws = 0; aclOpExecutor* ex = nullptr; aclnnStatus r1 = aclnnMseLossOutGetWorkspaceSize(self, target, red, out, &ws, &ex); aclnnStatus r2 = -1; if (r1 == 0) { void* wsp = nullptr; if (ws > 0) aclrtMalloc(&wsp, ws, ACL_MEM_MALLOC_HUGE_FIRST); aclrtStream st = nullptr; aclrtCreateStream(&st); r2 = aclnnMseLossOut(wsp, ws, ex, st); aclrtSynchronizeStream(st); if (wsp) aclrtFree(wsp); aclrtDestroyStream(st); } int64_t on = 1; for (auto v : osh) on *= v; std::vector<float> h((size_t)(on > 0 ? on : 1), 0.0f); aclrtMemcpy(h.data(), (size_t)on * 4, op, (size_t)on * 4, ACL_MEMCPY_DEVICE_TO_HOST); int canary = 0; for (int64_t i = 0; i < on; ++i) if (h[(size_t)i] == -777.0f) canary++; printf("[%s] ret1=%d ret2=%d workspace=%llu out_n=%lld canary_left=%d/%lld\n", name, (int)r1, (int)r2, (unsigned long long)ws, (long long)on, canary, (long long)on); aclDestroyTensor(self); aclDestroyTensor(target); aclDestroyTensor(out); aclrtFree(sp); aclrtFree(tp); aclrtFree(op); } int main(int argc, char** argv) { setvbuf(stdout, nullptr, _IONBF, 0); int dev = (argc > 1) ? atoi(argv[1]) : 0; if (aclInit(nullptr) != ACL_SUCCESS || aclrtSetDevice(dev) != ACL_SUCCESS) { printf("init failed\n"); return 1; } Run("empty_1d_mean", {0}, {0}, {}, 1); Run("empty_1d_sum", {0}, {0}, {}, 2); Run("empty_2d_mean", {0, 4}, {0, 4}, {4}, 1); Run("empty_2d_sum", {0, 4}, {0, 4}, {4}, 2); Run("nonempty_mean_control", {2, 4}, {2, 4}, {4}, 1); aclrtResetDevice(dev); aclFinalize(); _Exit(0); }影响
aclnnMseLoss给 NaN/0,本接口给垃圾)。建议
早退分支复用
loss/common/level2_base_loss.h的CheckFillScalarLoss:reduction=1 填NAN、reduction=2 填0.0f,reduction=0 保持不写;并补 UT(fp32/fp16/bf16 × reduction 1/2 × rank 1/2)断言 out 全 NaN / 全 0。