已关闭
[Bug-Report|缺陷反馈]: grouped_matmul 示例算子 acl_call 异步捕获 TensorList 致悬垂引用 SIGSEGV #4482
liulinxiang创建于 16 天前关闭于 12 天前
16 天前 添加了label:bug-report
16 天前 将 kknan 设为负责人
16 天前 将 cc-z 设为负责人,移除负责人 kknan
15 天前 关联了pull request:Fix the issue of gmm examples
12 天前 关闭了 issue
12 天前 添加了label:resolved


Describe the current behavior / 问题描述
examples/fast_kernel_launch_example中 grouped_matmul 算子(ascend910b)存在异步执行悬垂引用缺陷:grouped_matmul.cpp #L125-130 的
grouped_matmul_npu接收torch::TensorList/c10::optional<torch::TensorList>/c10::IntArrayRef参数;#L169-183 的acl_calllambda 以[=]按值捕获上述参数后,经OpCommand::RunOpApi交由acl_thread异步执行。TensorList/IntArrayRef(ArrayRef)不持有底层数据所有权:pybind 将 Python list 转换为 C++ 参数时创建的底层临时数组,在grouped_matmul_npu返回后即析构。当调用频率高于 acl_thread 消费速度(队列积压)时,acl_thread在GroupedMatmulCommonTiling中访问已释放的 TensorImpl → SIGSEGV(exit 139)。单次调用/低频调用不崩溃(队列空、立即消费时 tensor 仍存活),因此常规 pytest 单测无法暴露。
Environment / 环境信息
Steps to reproduce the issue / 重现步骤
import torch, torch_npu, ascend_ops x = [torch.rand(16, 16, dtype=torch.bfloat16).npu()] w = [torch.rand(16, 16, dtype=torch.bfloat16).npu()] gl = torch.tensor([16, 16, 16, 16], dtype=torch.int64).npu() for _ in range(101): # 快速入队使 acl_thread 队列积压 torch.ops.ascend_ops.grouped_matmul( x, w, None, None, None, None, None, gl, None, 0, 0, 0, 0, None) torch.npu.synchronize() # acl_thread 消费积压任务时访问悬垂引用 -> SIGSEGVDescribe the expected behavior / 预期结果
连续多次调用 +
torch.npu.synchronize()不崩溃,结果正确。Related log / screenshot / 日志 / 截图
gdb 关键栈(Thread "acl_thread"):
修复验证:将参数在 lambda 前实体化为
std::vector<at::Tensor>再按值捕获后,101 次调用 + sync 不再崩溃,msprof / NPUGraph capture 均恢复正常。Special notes for this issue / 备注
参考修复(与仓内
ops_common.h的ConvertTypes同步转换范式一致——进入异步 lambda 前完成 ArrayRef 实体化):std::vector<at::Tensor> x_vec(x.begin(), x.end()); std::vector<at::Tensor> weight_vec(weight.begin(), weight.end()); // optional 各字段同理实体化... auto acl_call = [=, &matched_combo]() -> int { groupedmatmul_api(..., x_vec, weight_vec, ...); // vector 活在 lambda 拷贝内, 安全 };注:与 #4442(示例 test cpp 显存欠分配)为不同问题,本条为 torch 绑定层 host 侧异步悬垂。