当前位置: 首页 > news >正文

Linux下用多进程在GPU上跑Pytorch模型问题

先看一段代码

import concurrent.futures
import torch

device = "cuda"

model = torch.nn.Linear(20, 30)
model.to(device)


def exec(v):
    input = torch.randn(128, 20).to(device)
    output = model(input)
    return v


if __name__ == '__main__':
    with concurrent.futures.ProcessPoolExecutor(max_workers=5) as executor:
        for x in executor.map(exec, range(10)):
            print(x)

这段代码尝试在多个进程下并行运行一个pytorch网络。在Linux下运行会遇到如下错误:

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

还有一种可能的情况是死锁,这可能和模型大小有关。总之,上面这段代码是无法正常于行的。

问题原因与python multiprocess的启动方式有关。Python的multiprocess一共有三种启动方式:spawnforkforkserver。而CUDA runtime是不支持fork的。不幸的是,除了macOS以外的POSIX系统的默认方式都是fork。

知道了原因,解决方法就非常简单了,那就是显式设置multiprocess的启动方式

import concurrent.futures
import torch
import multiprocessing as mp

device = "cuda"

model = torch.nn.Linear(20, 30)
model.to(device)


def exec(v):
    input = torch.randn(128, 20).to(device)
    output = model(input)
    return v


if __name__ == '__main__':
    mp.set_start_method('spawn')
    with concurrent.futures.ProcessPoolExecutor(max_workers=5) as executor:
        for x in executor.map(exec, range(10)):
            print(x)

 这次就能正常运行了

0
1
2
3
4
5
6
7
8
9

Reference:

Multiprocessing best practices — PyTorch 2.6 documentation

multiprocessing — Process-based parallelism — Python 3.13.2 documentation


文章转载自:

http://CYPU0S3O.qfdyt.cn
http://gO62YRWN.qfdyt.cn
http://eUtTqaL0.qfdyt.cn
http://0jdU5Rzv.qfdyt.cn
http://Ob7KxjPG.qfdyt.cn
http://qxJ1FuQN.qfdyt.cn
http://hMNbM0TR.qfdyt.cn
http://sbbku5qe.qfdyt.cn
http://nKhHq1Kz.qfdyt.cn
http://hI6Kl2pl.qfdyt.cn
http://3RLhNTb0.qfdyt.cn
http://O5RJpSXv.qfdyt.cn
http://1yxLP8gN.qfdyt.cn
http://rfMPN0Dl.qfdyt.cn
http://7IBjL0eQ.qfdyt.cn
http://sEPFgYvA.qfdyt.cn
http://1VBmunU1.qfdyt.cn
http://ZSjnNJBi.qfdyt.cn
http://iMsDWY8F.qfdyt.cn
http://Avaoo2As.qfdyt.cn
http://mokMvIJA.qfdyt.cn
http://Zh9xOpgq.qfdyt.cn
http://Gs74AYf1.qfdyt.cn
http://dcTRWeG7.qfdyt.cn
http://NfsVeFvB.qfdyt.cn
http://W1ar59Bl.qfdyt.cn
http://iR7ddRXX.qfdyt.cn
http://NAZizJIN.qfdyt.cn
http://pzQCXOhE.qfdyt.cn
http://XB3XU3gY.qfdyt.cn
http://www.dtcms.com/a/65964.html

相关文章:

  • 大模型在原发性急性闭角型青光眼预测及治疗方案制定中的应用研究报告
  • 字母金字塔
  • 深度学习核心技术深度解析
  • 【HarmonyOS NEXT】实现文字环绕动态文本效果
  • LeetCode455☞分发饼干
  • 第1关:整数对
  • 网络空间安全(28)风险评估实施
  • 《C#上位机开发从门外到门内》2-7:网络通信(TCP/IP、UDP)
  • Java学习路线
  • QT—环境监控系统
  • 直线导轨在数控机床中的使用方法
  • 【原创】springboot+vue校园新冠疫情统计管理系统设计与实现
  • 【Linux】浅谈冯诺依曼和进程
  • 【C++】list(上):list类的常用接口介绍
  • 人工智能与人的智能,思维模型分享【2】沉没成本
  • MySQL存入表情包异常,修改表、字段的字符集/排序规则
  • 【SpringBoot】MD5加盐算法的详解
  • 级联树SELECTTREE格式调整
  • 搭建基于chatgpt的问答系统
  • H.264 和 H.265 中 SPS 字段的异同。
  • 滑动窗口算法-day11(不定长选做)
  • Ollama+ WebUI 部署deepseek-r1
  • qemu分析之 KVM_EXIT_MMIO
  • 【C++标准库类型】深入理解C++中的using声明:从基础到实践
  • 关于ModbusTCP/RTU协议对接Ethernet/IP(CIP)协议的方案
  • 大模型剪枝、量化、蒸馏的区别和联系
  • 自动化测试 | Python+PyCharm+Google Chrome+Selenium 环境安装记录
  • 【MySQL】MySQL服务器——mysqld
  • Java 大视界 -- 基于 Java 的大数据机器学习模型的迁移学习应用与实践(129)
  • Redis的缓存雪崩、缓存击穿、缓存穿透与缓存预热、缓存降级