文章目录
- 0. 显卡编号(什么是主卡)
- 1. 使用所有存在的显卡
- 2. 指定编号使用显卡
- 3. os.environ["CUDA_VISIBLE_DEVICES"]详解
- 3.1. ["CUDA_VISIBLE_DEVICES"]使用
- 3.2. 关于设置["CUDA_VISIBLE_DEVICES"]无效的解决
- 4. torch.cuda主要函数
- 4.1. torch.cuda.is_available()判断GPU是否可用
- 4.2. torch.cuda.device_count()查看可用GPU数量
- 4.3. torch.cuda.current_device()查看当前使用的GPU序号
- 4.4 其他一些函数
- 5. 报错AssertionError: Invalid device id
- 6. 报错RuntimeError: module must have its parameters and buffers on device cuda:1 (device_ids[0]) but found
主要讲单机多卡(单主机多GPUs训练)
使用多卡训练的方式有很多,当然前提是我们的设备中存在多个GPU:使用命令nvidia-smi查看当前Ubuntu平台的GPU数量,其中每个GPU被编上了序号:[0,1,2,3]
0. 显卡编号(什么是主卡)
在默认情况下,标号为0的显卡为主卡,如主机中有4块显卡,那么每张显卡的默认标号为[0,1,2,3]。
如何将其他显卡设置为主卡呢?
通过
1 2 | os.environ["CUDA_VISIBLE_DEVICES"] = "3,2,0,1" model = torch.nn.DataParallel(model, device_ids=[0,2,3]).cuda() |
此时,3号显卡就变成了主卡,在使用torch.nn.DataParallel指定运算显卡时,显卡的对应关系如下:
1 2 3 4 5 | 实际显卡编号----->运算显卡编号 3 -----> 0(主卡) 2 -----> 1 0 -----> 2 1 -----> 3 |
1. 使用所有存在的显卡
在存在多卡的条件下,最简单的方法是直接使用torch.nn.DataParallel将你的模型wrap一下即可:
1 | net = torch.nn.DataParallel(model) |
这时,默认所有存在的显卡都会被使用
2. 指定编号使用显卡
如果有很多显卡(例如我们有4张显卡),但只想使用0、1、2号显卡,那么可以:
1 | net = torch.nn.DataParallel(model, device_ids=[0, 1, 2]) |
或者:
1 2 3 | os.environ["CUDA_VISIBLE_DEVICES"] = ','.join(map(str, [0,1,2])) # 一般在程序开头设置 # 等价于os.environ["CUDA_VISIBLE_DEVICES"] = '0,1,2' net = torch.nn.DataParallel(model) |
如果只需要指定一张卡,可以使用
(不建议用这个方法)
1 2 | torch.cuda.set_device(1) print(torch.cuda.device_count()) #可用GPU数量 |
(我的机器是4卡,所以print结果是:4,说明用
后面还可以用
但是必须包含
3. os.environ[“CUDA_VISIBLE_DEVICES”]详解
3.1. [“CUDA_VISIBLE_DEVICES”]使用
1 2 | os.environ["CUDA_VISIBLE_DEVICES"] = ','.join(map(str, [0,1,2])) # 一般在程序开头设置 # 等价于os.environ["CUDA_VISIBLE_DEVICES"] = '0,1,2' |
这样会修改pytorch感受的设备编号,pytorch感知的编号还是从device:0开始。如上则把1号显卡改为device:0,1号显卡改为device:1,使用时应该这么写:
1 2 | os.environ["CUDA_VISIBLE_DEVICES"] = '1,2' torch.nn.DataParallel(model, device_ids=[0,1]) |
3.2. 关于设置[“CUDA_VISIBLE_DEVICES”]无效的解决
不生效的原因是,这一行代码放置的位置不对。
一定要把
torch.cuda.device_count()
4. torch.cuda主要函数
4.1. torch.cuda.is_available()判断GPU是否可用
1 2 | import torch print(torch.cuda.is_available()) |
可用即为:True
4.2. torch.cuda.device_count()查看可用GPU数量
1 2 | os.environ["CUDA_VISIBLE_DEVICES"] = '0,1,2' print(torch.cuda.device_count()) |
结果应该为:3
4.3. torch.cuda.current_device()查看当前使用的GPU序号
1 2 | os.environ["CUDA_VISIBLE_DEVICES"] = '1,2' print(torch.cuda.current_device()) |
结果应该是:0
这样就证明了我们上面说的,
4.4 其他一些函数
查看指定GPU的容量、名称
1 2 | torch.cuda.get_device_capability(device) torch.cuda.get_device_name(device) |
清空程序占用的GPU资源
1 | torch.cuda.empty_cache() |
为GPU设置随机种子
1 2 | torch.cuda.manual_seed(seed) torch.cuda.manual_seed_all(seed) |
5. 报错AssertionError: Invalid device id
对模型进行gpu指定编号多gpu训练,必须要有所指定编号的gpu,不然会报
报错示例1:
1 2 | os.environ["CUDA_VISIBLE_DEVICES"] = '0,1' # 一般在程序开头设置 net = torch.nn.DataParallel(model,device_ids=[1, 2]) |
而使用
原因:2号显卡没有设置被可见。
报错示例2:
1 2 | os.environ["CUDA_VISIBLE_DEVICES"] = '1,2' # 一般在程序开头设置 net = torch.nn.DataParallel(model,device_ids=[1, 2]) |
这样会修改pytorch感受的设备编号,pytorch感知的编号还是从device:0开始
程序应该这么改:
1 2 | os.environ["CUDA_VISIBLE_DEVICES"] = ','.join(map(str, [1,2])) # 一般在程序开头设置 net = torch.nn.DataParallel(model,device_ids=[0, 1]) |
6. 报错RuntimeError: module must have its parameters and buffers on device cuda:1 (device_ids[0]) but found
PyTorch框架下,使用
如下代码中,主卡没有参与运算,就会导致该错误。
1 2 | os.environ["CUDA_VISIBLE_DEVICES"] = '0,1,2,3' #这里主卡就是机器0号卡 model = torch.nn.DataParallel(model, device_ids=[1,2,3]).cuda() |