reference
4.6 GPU computing
So far, we have been using the CPU for calculations. For complex neural networks and large-scale data, using CPU to calculate may not be efficient enough.
In this section, I will introduce how to use a single NIVIDA GPU for calculations.
4.6.1 Computing equipment
PyTorch can specify the device used for storage and calculation, if you use a memory CPU or a graphics memory GPU. By default, PyTorch will create data in memory, and then use GPU to calculate.
Use to torch.cuda.is_available()
check whether the GPU is available:
import torch
from torch import nn
torch.cuda.is_available()
# 查看GPU数量
torch.cuda.current_device()
PS: The number sequence number of the GPU is calculated from 0
# 根据索引查看GPU名字
torch.cuda.get_device_name(0)
4.6.2 Tensor
GPU computing
By default, it Tensor
will be stored in memory. Therefore, before we Tensor
could not see the GPU related logo every time we printed
x = torch.tensor([1,2,3])
x
Use .cuda()
can Tensor
convert (copy) the CPU on the GPU. If you have multiple GPU, we use .cuda(i)
to represent the i-th block GPU
x = x.cuda()
We can view the device by Tensor
the device
propertiesTensor
x.device
We can specify the device when we create it
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
x = torch.tensor([1, 2, 3], device=device)
print(x)
x= torch.tensor([1,2,3]).to(device)
print(x)
If you perform operations on the data on the GPU, the result is still stored on the GPU
y = x**2
y
It should be noted that the data stored in different locations cannot be directly calculated. That is, the data stored on the CPU cannot be directly calculated with the data stored on the GPU, and the data on different GPUs cannot be directly calculated.
4.6.3 GPU calculation of the model
With Tensor
similar, PyTorch model can also .cuda
switch to the GPU. We can device
view the device storing the model through the properties
net = nn.Linear(3, 1)
list(net.parameters())[0].device
Transfer the model to the GPU
net.cuda()
list(net.parameters())[0].device
The model and parameters need to be calculated on the GPU at the same time
x =torch.rand(2, 3).cuda()
net(x)