已关闭
[Bug]: triton_experimental 后端 min/max reduce相关 #4183
创建于  19 天前关闭于  6 天前
伦
成员
19 天前 创建

在提交新问题之前,请确保您已经在社区中搜索过相关问题,并使用了社区中提供的资源/工具后,仍未找到满意的解决方式。

⚠️ 安全信息提醒:请仔细检查提供的文本内容,确保其不包含敏感数据信息,包括但不限于:

  • API 令牌或密钥
  • 密码或身份验证凭证
  • 私有网址或接口地址
  • 个人或机密数据
  • ...

在分享配置信息或代码示例时,请将敏感信息脱敏处理,或使用 <TOKEN> 等占位符替代原有内容。

环境信息

-- CANN 版本: 9.1.0-beta.1
-- Pytorch/Torch_npu 版本: PyTorch 2.13.0 / torch_npu 2.13.0
-- Python 版本: Python 3.11.15
-- 操作系统版本: EulerOS 24.03 (aarch64)
-- NPU 型号: Ascend 910B2
-- triton-ascend 版本: 3.2.2
-- 后端开关: TORCHINDUCTOR_NPU_BACKEND=triton_experimental

🐛 问题描述

现象:torch.compile 走 triton_experimental 后端时,min/max 归约(含元素级 minimum/maximum 与按维度归约)会丢弃 NaN——输入含 NaN 时输出不含 NaN,与 eager / cuda 后端语义不一致。NaN
语义错误会导致上游训练 loss 变成 nan 时被静默掩盖。

根因:triton_experimental 的 min/max lowering 使用裸 tl.minimum/maximum,落盘为 MLIR arith.minnumf(NaN-dropping);arith.minnumf 遇到 NaN 会丢弃它。正确做法是 tl.minimum(a, b,
tl.PropagateNan.ALL) → arith.minimumf(NaN-propagating)。

最小复现(伪代码,供定位):

import torch
import torch_npu
import torch._inductor.config as config

config.npu_backend = "triton_experimental"

x = torch.randn(8, 8, device="npu")
x[0] = float("nan") # 让第一行全 NaN

元素级 minimum:期望输出保留 NaN,实际被丢弃

ref = torch.minimum(x, x + 1)
got = torch.compile(torch.minimum)(x, x + 1)
assert torch.equal(ref, got) # ❌ 失败:got[0] 无 NaN

按维度归约:同理,min(dim=0) 期望 NaN 传播

期望行为:min/max 语义与 PyTorch 一致,NaN 传播(torch.minimum / torch.min(dim) 遇 NaN 输出 NaN);cuda inductor 后端即为合体 NaN 传播形式。

实际行为:NaN 被丢弃,输出不含 NaN。

影响:任何含 NaN 的输入(训练发散的常见信号)经 triton_experimental 后端后 NaN 被抹掉,正确性错误且难以察觉。

欢迎加入社区,感谢您对社区的贡献 🎉!

likedislike
伦成员
19 天前 关联了看板:FrameworkPTAdapter 版本issue看板
TorchNPU-BotTorchNPU-Bot成员
19 天前 添加了label:triage-review
ascend-robotascend-robot成员
19 天前 添加了label:bug
TorchNPU-Bot
TorchNPU-Bot成员
19 天前 评论:

issue待分派,添加triage-review标签

likedislike
TorchNPU-BotTorchNPU-Bot成员
19 天前 添加了label:bot-triaged;删除了label:triage-review
TorchNPU-Bot
TorchNPU-Bot成员
19 天前 评论:

检测到当前 issue 已关联 PR !44773,自动添加标签:bot-triaged

likedislike
ascend-robotascend-robot成员
6 天前 关闭了 issue
ascend-robotascend-robot成员
6 天前 添加了label:resolved