DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

1

Check PyTorch version, CPU and GPU(CUDA) in PyTorch

Buy Me a Coffee

*My post explains how to create and acceess a tensor.

__version__ can check PyTorch version as shown below. *__version__ can be used with torch but not with a tensor:

import torch

torch.__version__ # 2.2.1+cu121
Enter fullscreen mode Exit fullscreen mode

cpu.is_available(), cpu.device_count() or cpu.current_device() can check if CPU is available, getting a scalar as shown below:

*Memos:

  • cpu.is_available(), cpu.device_count() or cpu.current_device() can be used with torch but not with a tensor.
  • cpu.device_count() can get the number of CPUs. *It always gets 1:
  • cpu.current_device() can get the index of a currently selected CPU. *It always gets cpu:
import torch

torch.cpu.is_available() # True

torch.cpu.device_count() # 1

torch.cpu.current_device() # cpu
Enter fullscreen mode Exit fullscreen mode

cuda.is_available() or cuda.device_count() can check if GPU(CUDA) is available, getting a scalar as shown below:

*Memos:

  • cuda.is_available() or cuda.device_count() can be used with torch but not with a tensor.
  • cuda.device_count() can get the number of GPUs.
import torch

torch.cuda.is_available() # True

torch.cuda.device_count() # 1
Enter fullscreen mode Exit fullscreen mode

In addition, you can use cuda.current_device(), cuda.get_device_name() or cuda.get_device_properties(), getting a scalar as shown below:

*Memos:

  • cuda.current_device(), cuda.get_device_name() or cuda.get_device_properties() can be used with torch but not with a tensor.
  • cuda.current_device() can get the index of a currently selected GPU.
  • cuda.get_device_name() can get the name of a GPU. *Memos:
    • The 1st argument with torch is device(Optional-Default:None-Type:(str, int or device().
    • If it's None, cuda.current_device() is used.
    • The number must be zero or positive.
    • Only cuda can be set to device.
    • My post explains device().
  • cuda.get_device_properties() can get the properties of a GPU. *Memos:
    • The 1st argument with torch is device(Required-Type:str, int or device()).
    • The number must be zero or positive.
    • Only cuda can be set to device.
    • My post explains device().
torch.cuda.current_device() # 0

torch.cuda.get_device_name()
torch.cuda.get_device_name(device='cuda:0')
torch.cuda.get_device_name(device='cuda')
torch.cuda.get_device_name(device=0)
torch.cuda.get_device_name(device=torch.device(device='cuda:0'))
torch.cuda.get_device_name(device=torch.device(device='cuda'))
torch.cuda.get_device_name(device=torch.device(device=0))
torch.cuda.get_device_name(device=torch.device(type='cuda'))
torch.cuda.get_device_name(device=torch.device(type='cuda', index=0))
# Tesla T4

torch.cuda.get_device_properties(device='cuda:0')
torch.cuda.get_device_properties(device='cuda')
torch.cuda.get_device_properties(device=0)
torch.cuda.get_device_properties(device=torch.device(device='cuda:0'))
torch.cuda.get_device_properties(device=torch.device(device='cuda'))
torch.cuda.get_device_properties(device=torch.device(device=0))
torch.cuda.get_device_name(device=torch.device(type='cuda'))
torch.cuda.get_device_name(device=torch.device(type='cuda', index=0))
# _CudaDeviceProperties(name='Tesla T4', major=7, minor=5, 
# total_memory=15102MB, multi_processor_count=40)
Enter fullscreen mode Exit fullscreen mode

!nvidia-smi can get the information about GPUs as shown below:

!nvidia-smi

Wed May 15 13:18:15 2024       
+---------------------------------------------------------------------------------------+
| NVIDIA-SMI 535.104.05             Driver Version: 535.104.05   CUDA Version: 12.2     |
|-----------------------------------------+----------------------+----------------------+
| GPU  Name                 Persistence-M | Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp   Perf          Pwr:Usage/Cap |         Memory-Usage | GPU-Util  Compute M. |
|                                         |                      |               MIG M. |
|=========================================+======================+======================|
|   0  Tesla T4                       Off | 00000000:00:04.0 Off |                    0 |
| N/A   56C    P0              28W /  70W |    105MiB / 15360MiB |      0%      Default |
|                                         |                      |                  N/A |
+-----------------------------------------+----------------------+----------------------+

+---------------------------------------------------------------------------------------+
| Processes:                                                                            |
|  GPU   GI   CI        PID   Type   Process name                            GPU Memory |
|        ID   ID                                                             Usage      |
|=======================================================================================|
+---------------------------------------------------------------------------------------+
Enter fullscreen mode Exit fullscreen mode

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (0)

Retry later
Retry later