How to check if pytorch is using the GPU?

后端 未结 10 1253
既然无缘
既然无缘 2020-12-04 05:01

I would like to know if pytorch is using my GPU. It\'s possible to detect with nvidia-smi if there is any activity from the GPU during the process,

10条回答
  •  既然无缘
    2020-12-04 05:10

    Almost all answers here reference torch.cuda.is_available(). However, that's only one part of the coin. It tells you whether the GPU (actually CUDA) is available, not whether it's actually being used. In a typical setup, you would set your device with something like this:

    device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
    

    but in larger environments (e.g. research) it is also common to give the user more options, so based on input they can disable CUDA, specify CUDA IDs, and so on. In such case, whether or not the GPU is used is not only based on whether it is available or not. After the device has been set to a torch device, you can get its type property to verify whether it's CUDA or not.

    if device.type == 'cuda':
        # do something
    

提交回复
热议问题