How do I check if PyTorch is using the GPU? The nvidia-smi
command can detect GPU activity, but I want to check it directly from inside a Python script.
Advertisement
Answer
These functions should help:
JavaScript
x
17
17
1
>>> import torch
2
3
>>> torch.cuda.is_available()
4
True
5
6
>>> torch.cuda.device_count()
7
1
8
9
>>> torch.cuda.current_device()
10
0
11
12
>>> torch.cuda.device(0)
13
<torch.cuda.device at 0x7efce0b03be0>
14
15
>>> torch.cuda.get_device_name(0)
16
'GeForce GTX 950M'
17
This tells us:
- CUDA is available and can be used by one device.
Device 0
refers to the GPUGeForce GTX 950M
, and it is currently chosen by PyTorch.