CUDA_VISIBLE_DEVICES 上的 nvprof 警告

nvprof warning on CUDA_VISIBLE_DEVICES

当我在 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

What does this actually mean?

这意味着 nvprof 开始在您通过设置 CUDA_VISIBLE_DEVICES 使其不可用的 GPU 上下文中分析您的代码。

How can I avoid this by using CUDA_VISIBLE_DEVICES (not torch.cuda.set_device())?

大概是这样的:

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

import torch
....

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

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

CUDA_VISIBLE_DEVICES=1 nvprof --analysis-metrics -o metrics python test.py