GPU計算
默認情況下,pytorch將數據保存在內存,而不是顯存.
查看顯卡信息
nvidia-smi
我的機器輸出如下:
Fri Jan 3 16:20:51 2020
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 418.67 Driver Version: 418.67 CUDA Version: 10.1 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|===============================+======================+======================|
| 0 GeForce GTX 105... Off | 00000000:01:00.0 Off | N/A |
| N/A 42C P0 N/A / N/A | 1670MiB / 4042MiB | 0% Default |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: GPU Memory |
| GPU PID Type Process name Usage |
|=============================================================================|
| 0 1572 G /usr/lib/xorg/Xorg 601MiB |
| 0 4508 G compiz 231MiB |
| 0 4935 G ...equest-channel-token=592189694510481540 486MiB |
| 0 5574 G ...quest-channel-token=4527142888685015556 328MiB |
| 0 10049 G ...passed-by-fd --v8-snapshot-passed-by-fd 21MiB |
+-----------------------------------------------------------------------------+
單卡,gtx 1050,4g顯存.
查看gpu是否可用
torch.cuda.is_available()
查看gpu數量
torch.cuda.device_count()
查看當前gpu號
torch.cuda.current_device()
查看設備名
torch.cuda.get_device_name(device_id)
把tensor復制到顯存
使用.cuda()
可以將CPU上的Tensor
轉換(復制)到GPU上。如果有多塊GPU,我們用.cuda(i)
來表示第 \(i\) 塊GPU及相應的顯存(\(i\)從0開始)且cuda(0)
和cuda()
等價。
x=x.cuda()
直接在顯存上存儲數據
device = torch.device('cuda')
x = torch.tensor([1, 2, 3], device=device)
或者
x = torch.tensor([1,2,3]).to(device)
如果對在GPU上的數據進行運算,那么結果還是存放在GPU上。
y = x**2
y
輸出:
tensor([1, 4, 9], device='cuda:0')
需要注意的是,存儲在不同位置中的數據是不可以直接進行計算的。即存放在CPU上的數據不可以直接與存放在GPU上的數據進行運算,位於不同GPU上的數據也是不能直接進行計算的。
z = y + x.cpu()
會報錯:
z=y+x.cpu()
RuntimeError: expected device cuda:0 and dtype Long but got device cpu and dtype Long
完整代碼
import torch
from torch import nn
is_gpu = torch.cuda.is_available()
gpu_nums = torch.cuda.device_count()
gpu_index = torch.cuda.current_device()
print(is_gpu,gpu_nums,gpu_index)
device_name = torch.cuda.get_device_name(gpu_index)
print(device_name)
x=torch.Tensor([1,2,3])
print(x)
x=x.cuda(gpu_index)
print(x)
print(x.device)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
x = torch.tensor([1, 2, 3], device=device)
x = torch.tensor([1,2,3]).to(device)
print(x)
y=x**2
print(y)
#z=y+x.cpu()
模型的gpu計算
同Tensor
類似,PyTorch模型也可以通過.cuda
轉換到GPU上。我們可以通過檢查模型的參數的device
屬性來查看存放模型的設備。
檢查模型參數存放設備:
net = nn.Linear(3,1)
print(type(net.parameters()))
print(list(net.parameters())[0].device)
輸出
<class 'generator'>
cpu
在gpu上做運算.通過.cuda()將模型計算放到gpu.相應的,傳給模型的輸入也必須是gpu顯存上的數據.
net = nn.Linear(3,1)
print(type(net.parameters()))
print(list(net.parameters())[0].device)
net=net.cuda()
x=torch.tensor([1,2,3]).cuda()
net(x)
總結:
- PyTorch可以指定用來存儲和計算的設備,如使用內存的CPU或者使用顯存的GPU。在默認情況下,PyTorch會將數據創建在內存,然后利用CPU來計算。
- PyTorch要求計算的所有輸入數據都在內存或同一塊顯卡的顯存上。