PyTorch torch.cuda function
PyTorch torch reference manual
torch.cuda is a PyTorch module for CUDA management. It provides features such as CUDA device management, memory management, and synchronization.
Function definition
torch.cuda
The torch.cuda module contains the following commonly used functions:
torch.cuda.is_available()- Check whether CUDA is availabletorch.cuda.device_count()- Get the number of CUDA devicestorch.cuda.current_device()- Get the current devicetorch.cuda.synchronize()- Synchronize CUDA operationstorch.cuda.empty_cache()- Clear the cache
Usage examples
Example
import
torch
Check whether CUDA is available
(
f
"CUDA available: {torch.cuda.is_available()}"
)
Get the number of CUDA devices
if
torch.
cuda
.
is_available
(
)
:
(
f
"CUDA device count: {torch.cuda.device_count()}"
)
(
f
"CUDA device name: {torch.cuda.get_device_name(0)}"
)