Check PyTorch version, CPU and GPU(CUDA) in PyTorch
Tags: pytorch, version, cpu, gpu
Published: 2024-05-26 01:22:20

*Memos:
- [My post](https://dev.to/hyperkai/create-a-tensor-in-pytorch-127g) explains how to create a tensor.
- [My post](https://dev.to/hyperkai/access-a-tensor-in-pytorch-1f4e) explains how to access a tensor.
- [My post](https://dev.to/hyperkai/istensor-numel-and-device-in-pytorch-2eha) explains [is_tensor()](https://pytorch.org/docs/stable/generated/torch.is_tensor.html), [numel()](https://pytorch.org/docs/stable/generated/torch.numel.html) and [device()](https://pytorch.org/docs/stable/tensor_attributes.html#torch-device).
- [My post](https://dev.to/hyperkai/type-conversion-with-type-to-and-a-tensor-in-pytorch-2a0g) explains type conversion with [type()](https://pytorch.org/docs/stable/generated/torch.Tensor.type.html), [to()](https://pytorch.org/docs/stable/generated/torch.Tensor.to.html) and a tensor.
- [My post](https://dev.to/hyperkai/type-promotion-resulttype-promotetypes-and-cancast-in-pytorch-33p8) explains type promotion, [result_type()](https://pytorch.org/docs/stable/generated/torch.result_type.html), [promote_types()](https://pytorch.org/docs/stable/generated/torch.promote_types.html) and [can_cast()](https://pytorch.org/docs/stable/generated/torch.can_cast.html).
- [My post](https://dev.to/hyperkai/device-conversion-fromnumpy-and-numpy-in-pytorch-1iih) explains device conversion, [from_numpy()](https://pytorch.org/docs/stable/generated/torch.from_numpy.html) and [numpy()](https://pytorch.org/docs/stable/generated/torch.Tensor.numpy.html).
- [My post](https://dev.to/hyperkai/setdefaultdtype-setdefaultdevice-and-setprintoptions-in-pytorch-55g8) explains [set_default_dtype()](https://pytorch.org/docs/stable/generated/torch.set_default_dtype.html), [set_default_device()](https://pytorch.org/docs/stable/generated/torch.set_default_device.html) and [set_printoptions()](https://pytorch.org/docs/stable/generated/torch.set_printoptions.html).
- [My post](https://dev.to/hyperkai/manualseed-initialseed-and-seed-in-pytorch-5gm8) explains [manual_seed()](https://pytorch.org/docs/stable/generated/torch.manual_seed.html), [initial_seed()](https://pytorch.org/docs/stable/generated/torch.initial_seed.html) and [seed()](https://pytorch.org/docs/stable/generated/torch.seed.html).

`__version__` can check PyTorch version as shown below. *`__version__` can be used with [torch](https://pytorch.org/docs/stable/torch.html) but not with a tensor:

```python
import torch

torch.__version__ # 2.2.1+cu121
```

[cpu.is_available()](https://pytorch.org/docs/stable/generated/torch.cpu.is_available.html), [cpu.device_count()](https://pytorch.org/docs/stable/generated/torch.cpu.device_count.html) or [cpu.current_device()](https://pytorch.org/docs/stable/generated/torch.cpu.current_device.html) 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](https://pytorch.org/docs/stable/torch.html) 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`:

```python
import torch

torch.cpu.is_available() # True

torch.cpu.device_count() # 1

torch.cpu.current_device() # cpu
```

[cuda.is_available()](https://pytorch.org/docs/stable/generated/torch.cuda.is_available.html) or [cuda.device_count()](https://pytorch.org/docs/stable/generated/torch.cuda.device_count.html) 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.

```python
import torch

torch.cuda.is_available() # True

torch.cuda.device_count() # 1
```

In addition, you can use [cuda.current_device()](https://pytorch.org/docs/stable/generated/torch.cuda.current_device.html), [cuda.get_device_name()](https://pytorch.org/docs/stable/generated/torch.cuda.get_device_name.html) or [cuda.get_device_properties()](https://pytorch.org/docs/stable/generated/torch.cuda.get_device_properties.html), 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-Type:(`str`, `int` or [device()](https://pytorch.org/docs/stable/tensor_attributes.html#torch-device).
  - If `device` is not given, the `device` of `cuda.current_device()` is used.
  - The number must be zero or positive.
  - Only `cuda` can be set to `device`. 
  - [My post](https://dev.to/hyperkai/istensor-numel-and-device-in-pytorch-2eha) 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()](https://pytorch.org/docs/stable/tensor_attributes.html#torch-device)).
  - The number must be zero or positive.
  - Only `cuda` can be set to `device`. 
  - [My post](https://dev.to/hyperkai/istensor-numel-and-device-in-pytorch-2eha) explains `device()`.

```python
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)
```

[!nvidia-smi](https://developer.nvidia.com/system-management-interface#:~:text=The%20NVIDIA%20System%20Management%20Interface,monitoring%20of%20NVIDIA%20GPU%20devices.) can get the information about GPUs as shown below:

```python
!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      |
|=======================================================================================|
+---------------------------------------------------------------------------------------+
```