pythonRuntimeError: Cannot re-initialize CUDA in forked subprocess. To use CUDA with multiprocessing

RuntimeError: Cannot re-initialize CUDA in forked subprocess. To use CUDA with multiprocessing, you 报错如下: Traceback (most recent call last): File “E2Capsnet.py”, line 777, in main() File “E2Capsnet.py”, line 619, in main train(epoch) File “E2Capsnet.py”, line 470, in train for batch_id, (data, label) in enumerate(raf.train_loader): File “/home/nuaa301/a 阅读详情

RuntimeError: Cannot re-initialize CUDA in forked subprocess. To use CUDA with multiprocessing, you must use the ‘spawn’ start method

  • fork和spawn是构建子进程的不同方式:
forkspawn
分叉产卵
继承非继承
从头构建一个子进程,父进程的数据拷贝到子进程的空间中,拥有自己的Python解释器
变量具有与父进程中相同的id
N在每个子进程开始时导入模块
子进程获取在 name == main 块中定义的变量N

解决方案

if __name__=='__main__':
    torch.multiprocessing.set_start_method('spawn')

参考与更多

Cannot re-initialize CUDA in forked subprocess. To use CUDA with multiprocessing 问题解决 使用FastChat框架中的vllm_work部署DeepSeek-Coder-V2时遇到Cannot re-initialize CUDA in forked subprocess. To use CUDA with multiprocessing 问题解决 阅读详情

相关推荐

RuntimeError: Cannot initialize CUDA without ATen_cuda library.

RuntimeError: Cannot initialize CUDA without ATen_cuda library. PyTorch splits its backend into two shared libraries: a CPU library and a CUDA library; this error has occurred because you are trying to use some CUDA functionality, but the CUDA library has

yuuzhao的博客 1万+

RuntimeError: Cannot re-initialize CUDA in forked subprocess.

分析:multiprocessing启动方式有误。2、直接设置num_workers=0。

u013409979的博客 3423

别再让电池充不满!用CN3791芯片设计太阳能充电电路,这几个调试坑我帮你踩了

本文详细解析了使用CN3791芯片设计太阳能充电电路时的常见调试问题及解决方案。从RCS电阻的选择到MPPT电压偏差的修正,再到线路损耗和温度补偿的优化,提供了全面的避坑指南。通过实测数据和优化方案,帮助工程师提升锂电池充电效率,确保太阳能充电电路的稳定性和可靠性。

weixin_42528191的博客 240

Cannot re-initialize CUDA in forked subprocess. To use CUDA with multiprocessing, you

当然,在定义dataset,方法__getitem__ 里把张量放到device里也会产生同样的错误。但是gpu会卡住不运行数据加载。

m0_52280920的博客 2503

DP3(3D Diffusion Policy)算法详解:从点云感知到扩散动作生成

DP3 的全称是:DP3 = 3D Diffusion Policy\boxed{\text{DP3 = 3D Diffusion Policy}}DP3 = 3D Diffusion Policy​对应论文题目是:3D Diffusion Policy: Generalizable Visuomotor Policy Learning via Simple 3D Representations该工作发表于 Robotics: Science and Systems(RSS)2024。DP3 是一种面向机器

u013250861的博客 349

IMU数据处理避坑指南:从理论到实践的加速度计重力消除方案

本文详细解析了IMU数据处理中的重力消除技术,从理论基础到实践应用,涵盖了加速度计和陀螺仪的核心问题。通过ROS2实战案例,介绍了静态标定与动态校准的技术对比,以及多传感器融合的进阶策略,帮助开发者避免常见陷阱,提升IMU数据处理的精度和效率。

purple的博客 741

多进程的子进程cuda使用报错: Cannot re-initialize CUDA in forked subprocess

这个错误表明在使用CUDA时,你的代码在一个使用fork启动方法的多进程环境中,而CUDA不支持在fork的子进程中重新初始化。为了解决这个问题,你可以将多进程的启动方法改为使用spawn,这样就可以在子进程中重新初始化CUDA。在Python中,你可以使用multiprocessing模块中的Manager对象来共享资源,或者使用Pool对象来处理进程池中的任务。如果你仍然遇到问题,可能需要考虑其他方法,例如在主进程中初始化CUDA,并将相应的资源传递给子进程,以避免CUDA的重新初始化问题。

qq_43199876的博客 1万+

一文搞定学术英语写作 (斯坦福SCI论文写作课程笔记)

0.前言: 本文基于斯坦福Kristin Sainani 教授的学术论文写作公开课——SCI论文写作(Writing in the Sciences)。在此基础上进行章节顺序的重排及部分内容的删减,以方便第一次进行英语学术论文写作的同学快速上手。 时间充足和想要系统学习英文学术写作的同学,十分建议观看完整课程视频,一定会受益匪浅!课程地址如下: 课程B站地址:https://www.bilibili.com/video/BV1zv41177JQ 1.论...

Ogmx的博客 3万+

cannot re-initialize CUDA in forked subprocess.To use CUDA with multiprocessing,you must use the ‘sp

cannot re-initialize CUDA in forked subprocess.To use CUDA with multiprocessing,you must use the 'spawn' start method.

weixin_39663060的博客 3401

Python爬虫实战:使用Scrapy+Playwright高效爬取全球专利数据库

本文将详细介绍如何使用Python最新技术栈(Scrapy+Playwright)构建一个高效的专利数据库爬虫系统。我们将从爬虫基础知识讲起,逐步深入到反爬应对策略、分布式爬虫架构以及数据存储方案,最终实现一个完整的专利数据采集系统。专利数据是技术创新领域的重要信息资源,包含技术描述、权利要求、申请人信息等宝贵数据。中国国家知识产权局(CNIPA)美国专利商标局(USPTO)欧洲专利局(EPO)世界知识产权组织(WIPO)

2201_76125261的博客 1382

第12步 支持向量机(SVM)

搭建SVM

Jet4505的博客 1050

Cannot re-initialize CUDA in forked subprocess.

RuntimeError: Cannot re-initialize CUDA in forked subprocess. To use CUDA with multiprocessing, you must use the 'spawn' start methodERROR:torch.distributed.elastic.multiprocessing.api:failed (exitcode: 1) local_rank: 0 (pid: 37434) of binary: /home/lvying

qq_36955294的博客 622

高性能优化的本质

在很早之前,处理器性能的提升一般是通过减小元器件的尺寸,增加元器件的数量,提高主频来实现,这也是摩尔定律所说的,但是由于功耗的影响,主频不能一直增加。后来,由于英伟达的GPU的并行计算的出现,逐渐打破摩尔定律,人们利用多核,SIMD,SIMT来处理大量相同计算的数据,这极大的提高了效率。计算机的算力得到了巨大的提升,但是并行是困难的,对于有数据依赖的程序来说,处理并行是一件及其困难的事,但对于一些简单的数学库的计算,并行的实现相对简单。

发狂的小花的博客 273

解决PyTorch多进程CUDA初始化报错

在 Linux 中,Python 的默认多进程启动方式是 fork,它的行为是:在启动子进程时,直接复制父进程的内存空间(几乎零成本)。CUDA 上下文(context)不能被安全地复制到子进程。所以当子进程第一次调用或初始化 CUDA 时,就会出问题。建议原因✅ 使用'spawn'方式避免 fork 与 CUDA 冲突,是官方推荐方式✅ 将放在主进程中简单直接,但功能受限❌ 不要在 Dataset 中初始化 GPU 运算容易导致不可预知的错误。

handsomeboysk的博客 2114

TI硬件笔试面试题型解析上

摘要: 本专栏聚焦大厂硬件笔试面试题解析,当前第14期以德州仪器(TI)为例,涵盖模拟电路与数字电路核心考点。模拟电路部分涉及最大功率传递定理、运放设计(虚短/虚断)、共模抑制比(CMRR)及高频PCB信号完整性;数字电路包括CMOS与TTL对比、建立/保持时间、竞争冒险消除、D触发器分频及“线与”逻辑实现。文章强调通过例题掌握解题方法与知识点,助力读者应对硬件工程岗位的笔试与面试挑战,附常见考点总结与实用建议。全文注重理论与实践结合,提供典型题型解析与设计思路。(150字)

启芯硬件的博客 950

深度学习工具包 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")

RuntimeError: Cannot re-initialize CUDA in forked subprocess. To use CUDA with multiprocessing

RuntimeError: Cannot re-initialize CUDA in forked subprocess. To use CUDA with multiprocessing, you must use the

weixin_66009678的博客 600

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

CUDA 相关错误

遇到过的 CUDA 相关错误汇总。 CUDA Error: initialization error 该错误产生的较大原因为CUDA驱动和CUDA工具包版本不兼容所致,每个CUDA工具包都有对应的CUDA驱动,新的CUDA驱动可以兼容老的COUA工具包,反之则可能出现该错误。升级CUDA驱动或降低CUDA版本至两者兼容。 CUDA Error: out of memory 显存不够,调整程序显存利用方式 RuntimeError: Cannot re-initialize CUDA in for

扶摇升的IT博客 4382

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

关于RuntimeError: Cannot re-initialize CUDA in forked subprocess和CUDA error: initialization error的解决

最近小虎在cpu上试跑了detection任务的程序没什么问题,但是在gpu上运行时发生了关于cuda的一些报错,发现解决问题的精华就一句话。下面给出问题解决的例子和代码。

狂小虎/Anthony Dave的博客 KuangXiaoHu's Blogs 3238

cannot re-initialize CUDA in forked subprocess.To use CUDA with multiprocessing,you must use the ...

cannot re-initialize CUDA in forked subprocess.To use CUDA with multiprocessing,you must use the 'spawn' start method. 我选择了向邪恶势力低头,将num_workers设置为0解决此问题

qq_36321330的博客 2664

训练神经网络太慢 DataLoader设置num_workers=4报错 RuntimeError: Caught RuntimeError in DataLoader worker process

在服务器上跑网络感觉很慢,尤其是几块显卡都有人在跑的时候,尝试在train_loader=DataLoader(train_data,batch_size=trainargs.batch_size,shuffle=True,num_workers=4) 设置num_workers=4,发现报错RuntimeError: Caught RuntimeError in DataLoader worker process。 于是网上搜答案,但却没找到能成功解决的。于是放弃,就让它这么慢吧。直到有一天,心有不..

fightingfighting的博客 1万+

pytorch中使用cuda进行多任务multiprocessing

我们都知道python有自带的multiprocessing模块,但是如果要使用cuda的话会报错: RuntimeError: Cannot re-initialize CUDA in forked subprocess. To use CUDA with multiprocessing, you must use the 'spawn' start method 但是查找torch中spawn,查找到torch.multiprocessing.spawn,所得的介绍并不多而且网上搜到的都是抄这个说

YNNAD1997的博客 4万+

多进程CUDA初始化的问题 Cannot re-initialize CUDA in forked subprocess

上面这个例子会报错,主进程触发了 CUDA 初始化,fork 出来的子进程中创建 tensor 的时候报错了。这句去掉,那么主进程就没有 CUDA 初始化的操作,子进程不会报错。,它们在底层机制、内存共享、安全性、兼容性等方面有根本差异。背景:在多实例算法服务的时候,出现了 cuda 初始化错误。时,选择错误的启动方法会导致程序崩溃或未定义行为。的,每个进程使用CUDA时,都会触发。父子进程之间有 cuda 状态的。的,没有问题,不会出现二次初始化。上面 fork 的示例中。

Michael是个半路程序员 1298
上一篇: numpy np tips: numpy数组的维度squeeze等处理
下一篇: python基础: 在pycharm中使用venv虚拟环境 + 激活与去激活在干什么?+如何不激活环境使用pip
FakeOccupational
博客等级 码龄6年 2万+粉丝 1727原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值