Python – How to check if pytorch is using the GPU

gpumemory-managementnvidiapythonpytorch

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, but I want something written in a python script.

Is there a way to do so?

Best Solution

This should work:

import torch

torch.cuda.is_available()
>>> True

torch.cuda.current_device()
>>> 0

torch.cuda.device(0)
>>> <torch.cuda.device at 0x7efce0b03be0>

torch.cuda.device_count()
>>> 1

torch.cuda.get_device_name(0)
>>> 'GeForce GTX 950M'

This tells me CUDA is available and can be used in one of your devices (GPUs). And currently, Device 0 or the GPU GeForce GTX 950M is being used by PyTorch.