CUDA kernel errors might be asynchronously reported at some other API call
目录
CUDA kernel errors might be asynchronously reported at some other API call
背景
在训练 mmseg 工程时遇到报错 “CUDA kernel errors might be asynchronously reported at some other API call, so the stacktrace below might be incorrect.”,该报错信息模糊,难以直接定位问题。
详细报错
报错提示 CUDA 内核错误可能在其他 API 调用中异步报告,导致堆栈跟踪可能不准确。
原因分析
报错涉及 CUDA 内核错误,但由于异步报告机制,难以直接确定具体错误原因和代码位置。
解决步骤
定位报错详细信息:在代码起始位置添加import os
和os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
两行代码,使报错定位到实际位置,便于针对性解决问题。
检查 num_class 设置:查看数据集中 label 对应的值是否超出 num_class 的设定范围。例如,若 num_class 设为 3,label 值应在 [0, 1, 2] 内,不能有其他值。(因为工程里的label默认是从0开始计算的)
检查模型输出尺寸:模型输入尺寸的通道数一般为 3,而输出尺寸的通道数应与 num_class 大小一致,需确保模型的 out_channel 已正确设置为 num_class。
总结
Python 代码报错通常较易理解,但遇到模糊报错时,可通过设置os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
打印详细报错信息来定位和修改代码。此次经验表明,CUDA kernel errors 报错很可能是 out_channel 和 num_class 不对应导致的。
【常见模块错误】
如果出现模块错误
进入控制台输入:建议使用国内镜像源
pip install 模块名称 -i https://mirrors.aliyun.com/pypi/simple
我大致罗列了以下几种国内镜像源:
清华大学
https://pypi.tuna.tsinghua.edu.cn/simple
阿里云
https://mirrors.aliyun.com/pypi/simple/
豆瓣
https://pypi.douban.com/simple/
百度云
https://mirror.baidu.com/pypi/simple/
中科大
https://pypi.mirrors.ustc.edu.cn/simple/
华为云
https://mirrors.huaweicloud.com/repository/pypi/simple/
腾讯云
https://mirrors.cloud.tencent.com/pypi/simple/