已关闭
[Bug]: 8_QuantScatter case indices 未保证合法,随机化初始部分用例越界导致验证非确定性失败 #8
bingbing_____创建于 5月19日关闭于 5月30日
5月19日 添加了label:bug
5月20日 添加了label:triaged
Zzhaolinlin
5月30日 关联了pull request:【benchmark】修复quantscatter indices 未保证合法问题、HybridAttentionMaskPreparation 未设置device问题、GroupNorm分布问题
5月30日 关联了pull request:【benchmark】修复quantscatter indices 未保证合法问题、HybridAttentionMaskPreparation 未设置device问题、GroupNorm分布问题
zhaolinlin
5月30日 评论:
5月30日 评论:
已和用户确认最新代码已修复


zhaolinlin
5月30日 评论:
5月30日 评论:
/close


ascend-robot
5月30日 评论:
5月30日 评论:
Notice
@zhaolinlin , this issue is currently under CVE service control and cannot be closed directly. Please comment /check-issue.


zhaolinlin
5月30日 评论:
5月30日 评论:
/check-issue


5月30日 issue状态由 TODO 改变为 DONE
5月30日 关闭了 issue
zhaolinlin
5月30日 评论:
5月30日 评论:
/close


5月30日 添加了label:resolved
在提交新问题之前,请确保您已经在社区中搜索过相关问题,并使用了社区中提供的资源/工具后,仍未找到满意的解决方式。
⚠️ 安全信息提醒:请仔细检查提供的文本内容,确保其不包含敏感数据信息,包括但不限于:
在分享配置信息或代码示例时,请将敏感信息脱敏处理,或使用
<TOKEN>等占位符替代原有内容。环境信息
🐛 问题描述
问题概述
8_QuantScatter benchmark 在 M < 1000 的测试 case 上,因 indices 张量的生成范围与 input 张量的 M 维不匹配,产生大量 out-of-bounds(OOB) indices。在这些 case 上,framework 参考实现
torch_npu.npu_quant_scatter 与任何正确的 Triton 实现都无法稳定匹配,验证结果呈现非确定性(同一份代码、同一种子、同一 NPU 设备下,重复运行结果不同)。
复现环境
复现步骤
ASCEND_RT_VISIBLE_DEVICES=14 python3 verify.py --op_name 8_QuantScatter --verify_dir . --non-compute
2. 同一份 kernel 代码 / 同一份 JSON / 同一种子(torch.manual_seed(0))反复运行,结果在 56/57(case 10 失败)和 57/57 之间波动。
根本原因
indices 生成范围与 input M 维解耦。
在 8_QuantScatter_torch.py:110-113 的 get_input_groups() 中:
elif dtype_str in ('int32', 'int64', 'int8'):
max_val = {'int32': 1000, 'int64': 1000, 'int8': 127}.get(dtype_str, 100)
tensors[name] = torch.randint(0, max_val, shape, dtype=dtype)
所有 int32 张量(包括 scatter 的 indices)一律用 randint(0, 1000) 生成,与 input 的 M 维无关。
把 57 个 case 的 M 与期望 OOB 比例汇总:
┌────────┬───────────────┬─────────────────────────────────────┐
│ M 大小 │ case 编号 │ 期望 OOB 比例 │
├────────┼───────────────┼─────────────────────────────────────┤
│ ≥ 1024 │ 多数 │ 0%(1000 < 1024,所有 randint 都合法) │
├────────┼───────────────┼─────────────────────────────────────┤
│ 512 │ case 6, 16 等 │ ~49% │
├────────┼───────────────┼─────────────────────────────────────┤
│ 256 │ case 8, 18 等 │ ~74% │
├────────┼───────────────┼─────────────────────────────────────┤
│ 128 │ case 10, 20 │ ~87% │
└────────┴───────────────┴─────────────────────────────────────┘
case 10 的 input shape 为 [256, 128, 64],M=128,实测 indices 中 213/256 越界,例如 indices[13] = 522(522 > M=128)。
为什么 OOB 会让验证非确定
行,但具体行为不稳定)
这不是 kernel bug
证据:
的参考算子位精确匹配
影响范围
建议修复
修改 get_input_groups(),对 scatter / gather 类算子的索引张量,根据上下文 tensor 的对应维度限制生成范围。
验证kernel:
import torch
import torch.nn as nn
import triton
import triton.language as tl
import torch_npu
@triton.jit
def quant_scatter_kernel(
input_ptr,
indices_ptr,
updates_ptr,
scales_ptr,
zp_ptr,
output_ptr,
B,
M,
D,
stride_input_b,
stride_input_m,
stride_input_d,
stride_updates_b,
stride_updates_m,
stride_updates_d,
stride_output_b,
stride_output_m,
stride_output_d,
stride_scales_d,
stride_zp_d,
has_zp: tl.constexpr,
BLOCK_D: tl.constexpr,
replicate_npu_bug: tl.constexpr,
):
pid = tl.program_id(0)
num_blocks_d = tl.cdiv(D, BLOCK_D)
total_blocks = B * num_blocks_d
class ModelNew(nn.Module):
def init(self):
super(ModelNew, self).init()
try:
self.VEC_CORE_NUM = torch_npu.npu.npu_config.get_device_limit(0).get("vector_core_num", 48)
except Exception:
self.VEC_CORE_NUM = 48
验证verify脚本
https://github.com/ElleElleWu/AscendOpGenAgent/blob/main/skills/triton/kernel-verifier/scripts/verify.py
欢迎加入社区,感谢您对社区的贡献 🎉!