已关闭
[Bug-Report|缺陷反馈]: grouped_matmul 示例算子 acl_call 异步捕获 TensorList 致悬垂引用 SIGSEGV #4482
liulinxiang创建于  16 天前关闭于  12 天前
liulinxiang
16 天前 创建

Describe the current behavior / 问题描述

examples/fast_kernel_launch_example 中 grouped_matmul 算子(ascend910b)存在异步执行悬垂引用缺陷:

grouped_matmul.cpp #L125-130grouped_matmul_npu 接收 torch::TensorList / c10::optional<torch::TensorList> / c10::IntArrayRef 参数;#L169-183acl_call lambda 以 [=] 按值捕获上述参数后,经 OpCommand::RunOpApi 交由 acl_thread 异步执行。

TensorList/IntArrayRefArrayRef)不持有底层数据所有权:pybind 将 Python list 转换为 C++ 参数时创建的底层临时数组,在 grouped_matmul_npu 返回后即析构。当调用频率高于 acl_thread 消费速度(队列积压)时,acl_threadGroupedMatmulCommonTiling 中访问已释放的 TensorImpl → SIGSEGV(exit 139)。

单次调用/低频调用不崩溃(队列空、立即消费时 tensor 仍存活),因此常规 pytest 单测无法暴露。

Environment / 环境信息

  • 硬件:Ascend 910_93(A3)
  • CANN:9.1.0;torch 2.x + torch_npu(OpCommand 异步任务队列生效)
  • 代码:master 最新(eaaad12, 2026-08-21)

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 消费积压任务时访问悬垂引用 -> SIGSEGV

Describe the expected behavior / 预期结果

连续多次调用 + torch.npu.synchronize() 不崩溃,结果正确。

gdb 关键栈(Thread "acl_thread"):

SIGSEGV in GroupedMatmulNs::GroupedMatmulTiling::GroupedMatmulCommonTiling<...>::RunTiling()
  <- groupedmatmul_api(...)  <- grouped_matmul_npu::<lambda()>  <- acl_thread

修复验证:将参数在 lambda 前实体化为 std::vector<at::Tensor> 再按值捕获后,101 次调用 + sync 不再崩溃,msprof / NPUGraph capture 均恢复正常。

Special notes for this issue / 备注

参考修复(与仓内 ops_common.hConvertTypes 同步转换范式一致——进入异步 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 侧异步悬垂。

likedislike
Lliulinxiang
16 天前 添加了label:bug-report
weihao18成员
16 天前 评论:

/assign @cc-z

likedislike
CANN-robotCANN-robot成员
16 天前 将 kknan 设为负责人
CANN-robotCANN-robot成员
16 天前 将 cc-z 设为负责人,移除负责人 kknan
Llijiamin2025成员
15 天前 关联了pull request:Fix the issue of gmm examples
CANN-robotCANN-robot成员
12 天前 关闭了 issue
CANN-robotCANN-robot成员
12 天前 添加了label:resolved