0

当我在 pytorch 中使用 os.environ['CUDA_VISIBLE_DEVICES'] 时,我收到以下消息

Warning: Device on which events/metrics are configured are different than the device on which it is being profiled. One of the possible reason is setting CUDA_VISIBLE_DEVICES inside the application.

这实际上意味着什么?如何通过使用“CUDA_VISIBLE_DEVICES”(不是 torch.cuda.set_device())来避免这种情况?

这是 pytorch test.py 中的代码

import torch
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '1'
g = 1
c1 = 512
c2 = 512
input = torch.randn(64, c1, 28, 28).cuda()
model = nn.Sequential(
      nn.Conv2d(c1,c2,1,groups=g),
      nn.ReLU(),
      nn.Conv2d(c1,c2,1,groups=g),
      nn.ReLU(),
      nn.Conv2d(c1,c2,1,groups=g),
      nn.ReLU(),
      nn.Conv2d(c1,c2,1,groups=g),
      nn.ReLU(),
      nn.Conv2d(c1,c2,1,groups=g),
      nn.ReLU(),
    ).cuda()
out = model(input)

和命令:

nvprof --analysis-metrics -o metrics python test.py
4

1 回答 1

3

这实际上意味着什么?

这意味着 nvprof 开始在您通过设置CUDA_VISIBLE_DEVICES.

如何通过使用CUDA_VISIBLE_DEVICES(not torch.cuda.set_device()) 来避免这种情况?

大概是这样的:

import os
os.environ['CUDA_VISIBLE_DEVICES'] = '1'

import torch
....

我对 pytorch 一无所知,但我猜想导入库会触发很多你看不到的 CUDA 活动。如果在 set 之后导入库CUDA_VISIBLE_DEVICES,我怀疑整个问题都会消失。

如果这不起作用,那么您别无选择,只能根本不在CUDA_VISIBLE_DEVICESpython 代码中设置,而是这样做:

CUDA_VISIBLE_DEVICES=1 nvprof --analysis-metrics -o metrics python test.py
于 2019-12-20T07:34:42.857 回答