torch tensor调用detach时出现RuntimeError: CUDA error: an illegal memory access was encountered解决

深度学习27个核心概念实战解析:从调试故障到训练闭环 深度学习不是术语背诵,而是可调试、可验证、可落地的工程实践。理解batch size如何平衡显存与梯度质量、activation function如何解决梯度消失、batch normalization如何缓解内部协变量偏移,是构建稳定训练流程的基础。这些概念共同支撑模型收敛性、泛化能力与部署鲁棒性,在图像分类、语义分割、时序建模等任务中直接影响F1、mIoU、ECE等关键指标。本文聚焦真实项目中的27个高频术语,覆盖数据加载、模型结构、优化策略与故障排查全链路,提供可复现的代码验证方式、内存估算公式与错误 阅读详情

代码大致如下:

a = torch.tensor(b) #b是bool类型
c = a.float()
d = torch.tensor([0], dtype=torch.float32, device="cuda")
e = render(c, d) #得到torch([3,1920,1080])
e.detach().cpu().numpy() #报错

在最后一步,d.detach()时报错:
RuntimeError: CUDA error: an illegal memory access was encountered

上网查了一些原因:
有pytorch, cuda版本不匹配的,这点确认过没问题。
cuda内存耗尽的,检查了一下发现也没用多少内存啊,这点也排除。

后来发现问题出在没有保证所有变量都是cuda,
可以看到a 是cpu, c 是cpu,d是cuda, 即有的变量是cpu, 有的变量是cuda。
为了保证一致性,只需要把a改为cuda类型即可。

a = torch.tensor(b).cuda() #b是bool类型

总结一下,要确保所有涉及的张量都在相同的设备上(GPU 或 CPU)。

成功解决RuntimeError:Unable to find a valid cuDNN algorithm to run convolution Unable to find a valid cuDNN algorithm to run convolution 阅读详情

相关推荐

一次惨痛的debug的经历-RuntimeError: CUDA error: an illegal memory access was encountered

之所以说惨痛是有原因的。这个错误有人严重怀疑是显卡和pytorch二者之一有一个是有问题的,也曾经想一度放弃,最后还是分享我的解决方法是啥,不确定对大家都适用。 一开始遇到这个错误,报的是我写的一个模块内的: File "/gpfs/share/home/1601111331/uda_for_convex/convex_mirror_block/convex_mirror_block.py", ...

u013517182的博客 1万+

RuntimeError: CUDA error: an illegal memory access was encountered

问题解决

啷个哩个啷 5万+

pytorch 训练 RuntimeError Unable to find a valid cuDNN algorithm to run convolution

pytorch 训练 RuntimeError: Unable to find a valid cuDNN algorithm to run convolution pytorch 训练 RuntimeError: Unable to find a valid cuDNN algorithm to run convolution # 问题描述: python:3.95 pytorch:1.10.2 python train.py --img 640 --batch 64 --epochs 600 --d

mtl1994的博客 1万+

RuntimeError:unable to find a valid cuDNN algorithm to convolution

由于虚拟环境都已经创建完成,目前是在Linux上进行安装,根据“链接2”中的安装代码,直接进行pytorch和cuda的安装。如果环境中已经装过其他版本的pytorch,应该能够自动卸载原版本,再装新的指定版本。经过验证,RTX3090安装上述版本的pytorch和cuda后,能够正常运行,不会报出上面的那个错误。2.其实问题更加简单,是模型的训练的batch-size训练过大了,调整更小,就可以了。解决办法:安装对应的cudnn,以及cuda,找到对应的torch框架,进行安装。

m0_46630689的博客 1万+

基于STM32的INA226电压电流检测仪

该功率检测装置以STM32F103C8T6微控制器为核心,集成功率检测、数据交互、状态显示和用户提示功能,通过模块化设计实现稳定运行。系统包括按键、LED指示灯、OLED屏幕、INA226电压电流检测、复位电路、晶振电路、蜂鸣器驱动、输入输出接口、调试接口和3.3V降压电路等模块。微控制器通过按键检测用户输入,启动功率检测,采集数据并通过OLED显示,异常情况触发蜂鸣器报警,LED状态指示同步反馈。3.3V降压电路为所有模块提供稳定电源,晶振电路确保时序精度。INA226芯片通过I2C接口与微控制器通信,实

a2784288164的博客 1921

使用LaTex写IEEE trans 论文的参考文献(bib)

(1)在.TeX文件的最下方添加这几行内容,主要为生成tran形式的参考文献,第二行为参考文献名称,我命名为reference.bib,所以写的是reference,要根据自己的命名改写。(5)如果要在文中引用的话,就在.tex文件中添加\cite{},中括号里面内容为方框圈出来部分,但引用之后,在参考文献后面会出现[1]这样的标识,不知道为什么。(4)完成(3)以后,点击工具-命令-LaTeX,编译,然后按照此步骤LaTeX-bibtex-LaTeX-LaTeX,就可以完成参考文献的导入。

qq_45884215的博客 6263

PyTorch Tensor布局与拷贝:GPU高性能训练的底层核心

Tensor布局与拷贝是深度学习框架中影响GPU计算效率的基础机制,其本质源于内存连续性(contiguous)、stride步长定义及Storage共享策略。理解这些概念,才能解释为何非连续Tensor会导致GPU利用率骤降、显存带宽瓶颈和隐式跨设备拷贝。在PyTorch中,布局错误常引发DDP静默失败、TensorRT导出报错、ONNX兼容性问题等典型工程故障;而拷贝类型(零拷贝/浅拷贝/深拷贝)直接决定CUDA kernel访存效率与显存碎片风险。本文聚焦Tensor物理结构(Storage-Layo

weixin_33158887的博客 256

AFSim插件开发实战:从环境搭建到功能扩展

本文详细介绍了AFSim插件开发的完整流程,从环境搭建到功能扩展,涵盖了SDK配置、插件创建、核心代码解析、功能扩展实战、调试优化以及高级开发技巧。通过实战案例和实用技巧,帮助开发者快速掌握AFSim插件开发,提升仿真系统的扩展性和灵活性。

weixin_29309475的博客 586

PyTorch Tensor工程解剖:从DataPtr到Storage的内存治理

Tensor在深度学习框架中常被视作多维数组,但其本质是融合内存管理、设备抽象与生命周期控制的工程实体。其核心原理在于分层设计:Tensor为轻量接口,TensorImpl承载元数据与状态机逻辑,Storage则封装物理存储与资源契约。关键技术价值体现在内存策略与计算逻辑的彻底解耦——通过DataPtr的Deleter+Context机制和Storage的allocator可插拔设计,实现跨CPU/GPU/NPU的无缝迁移与定制化内存治理。典型应用场景包括模型部署中的显存泄漏排查、Android Scope

weixin_29045001的博客 256

笔记:使用Materials Studio计算聚合物的玻璃化转变温度

使用Materials Studio计算聚合物的玻璃化转变温度。

FXJgogogo的博客 1952

dToF直方图之美_deadtime死区时间

IMX459的死区时间可以在6ns,这个时间是什么级别,车载激光雷达激光器的光脉宽一般都在4ns以上,也就是这个时间贴近去SPAD的死区时间,这就导致了pile up会非常弱,不会有太多数量的光子计数堆积,加上某些SPAD设计,对TDC有一些更深入的考量,所以不需要去校正pile up算法。Sensor层面决定了pile up的复杂程度,如果一个dToF的SPAD一致性非常好,那么pile up校正可以简单到极致,也就是一句代码的事,比如IMX611,优秀到不需要后端去考虑校正pile up。

致力于lidar应用开发 2154

FlexGen 仓库中的 Hugging Face transformers 混合 INT8 量化测试指南:基于 bitsandbytes 的 8-bit 模型加载、调试与验证

本文以 [benchmark/third_party/transformers/tests/mixed_int8/README.md](https://link.gitcode.com/i/0d1c99daa634a37e3fc48b8d52e3a035) 为骨架,完整梳理如何在 Hugging Face `transformers` 生态中有效调试 `bitsandbytes` 的 INT8 混

gitblog_00475的博客 172

ArcGIS Pro符号化教程

本文详细介绍了ArcGIS Pro的软件设置和符号化操作步骤。主要包括:1)设置默认工程目录和关闭地图自动加载;2)创建新工程的基本流程;3)点要素的单一符号化方法,包括样式修改和图层调整;4)图表符号显示过滤器的使用,涉及字段添加、图表类型选择和背景设置。文章还提供了操作截图和数据下载链接,适合GIS初学者学习ArcGIS Pro的基础符号化操作。

WwLK123的博客 555

DeepSeek-V4架构创新:面向数据流重排的GPU微架构优化

大语言模型性能瓶颈已从算力转向数据搬运效率,核心在于显存带宽与计算单元间的协同失配。理解Transformer微架构优化,需回归硬件本质:减少显存访问、提升带宽利用率、实现计算与数据流对齐。FP8混合精度调度、动态稀疏注意力编译、分层KV缓存压缩等技术,本质是针对GPU内存层级(寄存器/L2/HBM/NVMe)与计算单元(Tensor Core/SRAM)特性的系统性适配。这类优化显著提升长上下文推理吞吐与训练稳定性,广泛应用于百万token级RAG、实时对话引擎与低成本边缘部署场景。DeepSeek-V4

weixin_33739627的博客 357

02-Redis 核心特性与数据结构详解:揭秘高性能的技术内核

本文深度拆解 Redis 高性能的技术内核:揭秘其凭“内存存储 + C 语言开发” 实现微秒级延迟、10 万 + QPS 的性能优势;详解 5 种基础数据类型(String/Hash 等)与 4 种扩展数据类型(Stream/Geospatial 等)的特性及场景;还介绍多语言支持、RDB/AOF 持久化、主从复制 / 哨兵 / 集群等高可用方案,助读者掌握 Redis 技术核心与实践逻辑。

蜗牛老师,专注编程领域 1002

三态门Multisim电路仿真——硬件工程师笔记

三态门原理及应用摘要 三态门是具有高电平(1)、低电平(0)和高阻态(Z)三种输出的逻辑电路,通过使能端(EN)控制状态切换。高阻态时输出端等效断路,避免信号冲突。其核心应用包括: 总线系统:多设备共享总线时,仅使能选中设备的三态门,其余保持高阻态,防止干扰。 数据选择器:利用三态门切换多路输入信号,简化电路设计。 实现方式分两种: TTL三态门(三极管构成):低电平使能有效,仿真验证输出逻辑与高阻态。 MOS三态门(MOS管构成):高电平使能有效,仿真显示使能信号控制输出状态。 文中还附硬件工程师笔试面试

XU157303764的博客 2018

RuntimeError: Unable to find a valid cuDNN algorithm to run convolution

如果你的GPU内存不足以处理当前的卷积操作,也可能会出现这个错误。:确保你安装的CUDA和cuDNN版本是相互兼容的。有时,新版本的cuDNN可能不支持旧版本的CUDA,或者反之。:确保你的CUDA和cuDNN版本是兼容的。:检查你的卷积层配置,确保它们是合理的,并且不会导致cuDNN找不到合适的算法。如果问题依然存在,你可能需要更详细地检查你的代码和配置,或者寻求更专业的帮助。:尝试减小训练时的batch size,以减少GPU内存的使用。:确保你的GPU驱动程序是最新的,并且与你的CUDA版本兼容。

得塔云的博客 5232

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

【bug解决】RuntimeError: Unable to find a valid cuDNN algorithm to run convolution

2.其实问题更加简单,是模型的训练的batch-size训练过大了,调整更小,就可以了。解决办法:安装对应的cudnn,以及cuda,找到对应的torch框架,进行安装。1.模型训练的环境中cudnn,CUDA的版本号不匹配。

千与编程 2万+

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

RuntimeError: CUDA error: an illegal memory access was encountered 解决思路

RuntimeError: CUDA error: an illegal memory access was encountered GPU负载

yyywxk的博客 1万+

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

解决RuntimeError: CUDA error: an illegal memory access was encountered问题

用pytorch在多卡训练transformers的时候出现了以下问题: RuntimeError: CUDA error: an illegal memory access was encountered terminate called after throwing an instance of 'c10::Error' what(): CUDA error: an illegal memory access was encountered Exception raised from create

chenf1995的博客 1万+

1:RuntimeError: CUDA error: an illegal memory access was encounteredCompile with TORCH_USE_CUDA_DSA`

报错显示不精确,按照软件推荐方法设置set TORCH_USE_CUDA_DSA=1,设置之后就会报错详细的问题,从而进行修改。

qq_44770238的博客 7350

RuntimeError: CUDA error: an illegal memory access was encountered 错误解决方案

RuntimeError: CUDA error: an illegal memory access was encountered 首先,大家先检查自己的网络的参数是否有问题,如果参数有问题会导致此问题。 其次,博主遇到一个情况。在单GPU下开启时,eval阶段会报这种错误。 torch.nn.DataParallel(net,device_ids=[0]) 在net eval之前加...

slamdunkofkd的博客 3万+

报错解决:CUDA Runtime error(an illegal memory access was encountered, cudaErrorIllegalAddress = 700)

报错解决:CUDA Runtime Error(cudaErrorIllegalAddress = 700)

博学而笃志,切问而近思。 8589
上一篇: 3D分割之SAGA训练流程解读
下一篇: colmap的几种相机类型和内外参取得方法
蓝羽飞鸟
博客等级 码龄8年 1万+粉丝 727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝羽飞鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址