網(wǎng)站首頁 編程語言 正文
前言
CUDA(Compute Unified Device Architecture)是NVIDIA推出的異構(gòu)計(jì)算平臺(tái),PyTorch中有專門的模塊torch.cuda來設(shè)置和運(yùn)行CUDA相關(guān)操作。本地安裝環(huán)境為Windows10,Python3.7.8和CUDA 11.6,安裝PyTorch最新穩(wěn)定版本1.12.1如下:
pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu116
一.常見CPU和GPU操作命令
1.查看PyTorch版本
print(torch.__version__) 1.12.1+cu116
2.查看GPU設(shè)備是否可用
print(torch.cuda.is_available()) True
3.PyTorch默認(rèn)使用設(shè)備是CPU
print("default device: {}".format(torch.Tensor([4,5,6]).device)) default device: cpu
4.查看所有可用的cpu設(shè)備的數(shù)量
print("available cpu devices: {}".format(torch.cuda.os.cpu_count())) available cpu devices: 20
這里CPU設(shè)備數(shù)量指的是邏輯處理器的數(shù)量。
5.查看所有可用的gpu設(shè)備的數(shù)量
print("available gpu devices: {}".format(torch.cuda.device_count())) available gpu devices: 1
6.獲取gpu設(shè)備的名稱
print("gpu device name: {}".format(torch.cuda.get_device_name(torch.device("cuda:0")))) gpu device name: NVIDIA GeForce GTX 1080 Ti
7.通過device="cpu:0"指定cpu:0設(shè)備
device = torch.Tensor([1,2,3], device="cpu:0").device print("device type: {}".format(device)) device type: cpu
8.通過torch.device指定cpu:0設(shè)備
cpu1 = torch.device("cpu:0") print("cpu device: {}:{}".format(cpu1.type, cpu1.index)) cpu device: cpu:0
9.使用索引的方式,默認(rèn)使用CUDA設(shè)備
gpu = torch.device(0) print("gpu device: {}:{}".format(gpu.type, gpu.index)) gpu device: cuda:0
10.通過torch.device("cuda:0)指定cuda:0設(shè)備
gpu = torch.device("cuda:0") print("gpu device: {}:{}".format(gpu.type, gpu.index)) gpu device: cuda:0
二.CPU和GPU設(shè)備上的Tensor
默認(rèn)情況下創(chuàng)建Tensor是在CPU設(shè)備上的,但是可以通過copy_、to、cuda等方法將CPU設(shè)備中的Tensor轉(zhuǎn)移到GPU設(shè)備上。當(dāng)然也是可以直接在GPU設(shè)備上創(chuàng)建Tensor的。torch.tensor和torch.Tensor的區(qū)別是,torch.tensor可以通過device指定gpu設(shè)備,而torch.Tensor只能在cpu上創(chuàng)建,否則報(bào)錯(cuò)。
1.Tensor從CPU拷貝到GPU上
# 默認(rèn)創(chuàng)建的tensor是在cpu上創(chuàng)建的 cpu_tensor = torch.Tensor([[1,4,7],[3,6,9],[2,5,8]]) print(cpu_tensor.device) # 通過to方法將cpu_tensor拷貝到gpu上 gpu_tensor1 = cpu_tensor.to(torch.device("cuda:0")) print(gpu_tensor1.device) # 通過cuda方法將cpu_tensor拷貝到gpu上 gpu_tensor2 = cpu_tensor.cuda(torch.device("cuda:0")) print(gpu_tensor2.device) # 將gpu_tensor2拷貝到cpu上 gpu_tensor3 = cpu_tensor.copy_(gpu_tensor2) print(gpu_tensor3.device) print(gpu_tensor3)
輸出結(jié)果如下:
cpu
cuda:0
cuda:0
cpu
tensor([[1., 4., 7.],
? ? ? ? [3., 6., 9.],
? ? ? ? [2., 5., 8.]])
主要說明下這個(gè)copy_()方法,實(shí)現(xiàn)如下:
def copy_(self, src, non_blocking=False): ...... return _te.Tensor(*(), **{})
就是從src中拷貝元素到self的tensor中,然后返回self。以gpu_tensor3 = cpu_tensor.copy_(gpu_tensor2)
為例,就是把gpu中的gpu_tensor2拷貝到cpu中的cpu_tensor中。
2.直接在GPU上創(chuàng)建Tensor
gpu_tensor1 = torch.tensor([[2,5,8],[1,4,7],[3,6,9]], device=torch.device("cuda:0")) print(gpu_tensor1.device) # 在gpu設(shè)備上創(chuàng)建隨機(jī)數(shù)tensor print(torch.rand((3,4), device=torch.device("cuda:0"))) # 在gpu設(shè)備上創(chuàng)建0值tensor print(torch.zeros((2,5), device=torch.device("cuda:0")))
輸出結(jié)果,如下:
cuda:0
tensor([[0.7061, 0.2161, 0.8219, 0.3354],
? ? ? ? [0.1697, 0.1730, 0.1400, 0.2825],
? ? ? ? [0.1771, 0.0473, 0.8411, 0.2318]], device='cuda:0')
tensor([[0., 0., 0., 0., 0.],
? ? ? ? [0., 0., 0., 0., 0.]], device='cuda:0')
3.CUDA Streams
Steam是CUDA命令線性執(zhí)行的抽象形式,分配給設(shè)備的CUDA命令按照入隊(duì)序列的順序執(zhí)行。每個(gè)設(shè)備都有一個(gè)默認(rèn)的Steam,也可以通過torch.cuda.Stream()創(chuàng)建新的Stream。如果不同Stream中的命令交互執(zhí)行,那么就不能保證命令絕對(duì)按順序執(zhí)行。下面的這個(gè)例子不同的Stream就可能會(huì)產(chǎn)生錯(cuò)誤。
cuda = torch.device("cuda") # 創(chuàng)建默認(rèn)的stream,A就是使用的默認(rèn)stream s = torch.cuda.Stream() A = torch.randn((1,10), device=cuda) for i in range(100): # 在新的stream上對(duì)默認(rèn)的stream上創(chuàng)建的tensor進(jìn)行求和 with torch.cuda.stream(s): # 存在的問題是:torch.sum()可能會(huì)在torch.randn()之前執(zhí)行 B = torch.sum(A) print(B)
這個(gè)例子存在的問題是torch.sum()可能會(huì)在torch.randn()之前就執(zhí)行。為了保證Stream中的命令絕對(duì)按順序執(zhí)行,接下來使用Synchronize同步方法解決上面例子的問題:
cuda = torch.device("cuda") s = torch.cuda.Stream() A = torch.randn((1,10), device=cuda) default_stream = torch.cuda.current_stream() print("Default Stream: {}".format(default_stream)) # 等待創(chuàng)建A的stream執(zhí)行完畢 torch.cuda.Stream.synchronize(default_stream) for i in range(100): # 在新的stream上對(duì)默認(rèn)的stream上創(chuàng)建的tensor進(jìn)行求和 with torch.cuda.stream(s): print("current stream: {}".format(torch.cuda.current_stream())) B = torch.sum(A) print(B)
解決問題的思路就是通過torch.cuda.Stream.synchronize(default_stream)
等待創(chuàng)建A的stream執(zhí)行完畢,然后再執(zhí)行新的Stream中的指令。
除此之外,使用memory_cached方法獲取緩存內(nèi)存的大小,使用max_memory_cached方法獲取最大緩存內(nèi)存的大小,使用max_memory_allocated方法獲取最大分配內(nèi)存的大小。可以使用empty_cache方法釋放無用的緩存內(nèi)存。
三.固定緩沖區(qū)
緩存就是當(dāng)計(jì)算機(jī)內(nèi)存不足的時(shí)候,就會(huì)把內(nèi)存中的數(shù)據(jù)存儲(chǔ)到硬盤上。固定緩沖區(qū)就是說常駐內(nèi)存,不能把這部分?jǐn)?shù)據(jù)緩存到硬盤上。可以直接使用pin_memory方法或在Tensor上直接調(diào)用pin_memory方法將Tensor復(fù)制到固定緩沖區(qū)。為什么要做固定緩沖區(qū)呢?目的只有一個(gè),就是把CPU上的固定緩沖區(qū)拷貝到GPU上時(shí)速度快。Tensor上的is_pinned方法可以查看該Tensor是否加載到固定緩沖區(qū)中。
from torch.utils.data._utils.pin_memory import pin_memory x = torch.Tensor([[1,2,4], [5, 7, 9], [3, 7, 10]]) # 通過pin_memory()方法將x復(fù)制到固定緩沖區(qū) y = pin_memory(x) # 在tensor上直接調(diào)用pin_memory()方法將tensor復(fù)制到固定緩沖區(qū) z = x.pin_memory() # id()方法返回tensor的內(nèi)存地址,pin_memory()返回tensor對(duì)象的拷貝,因此內(nèi)存地址是不同的 print("id: {}".format(id(x))) print("id: {}".format(id(y))) print("id: {}".format(id(z))) # 當(dāng)tensor放入固定緩沖區(qū)后,就可以異步將數(shù)據(jù)復(fù)制到gpu設(shè)備上了 a = z.cuda(non_blocking=True) print(a) print("is_pinned: {}/{}".format(x.is_pinned(), z.is_pinned()))
輸出結(jié)果如下所示:
id: 1605289350472
id: 1605969660408
id: 1605969660248
tensor([[ 1., ?2., ?4.],
? ? ? ? [ 5., ?7., ?9.],
? ? ? ? [ 3., ?7., 10.]], device='cuda:0')
is_pinned: False/True
說明:通過id()查看對(duì)象的內(nèi)存地址。
四.自動(dòng)設(shè)備感知
1.適配CPU和GPU設(shè)備
自動(dòng)設(shè)備感知本質(zhì)上就是有GPU時(shí)就使用GPU,沒有GPU時(shí)就使用CPU,即一套代碼適配CPU和GPU設(shè)備。GPU是否存在是通過torch.cuda.is_available()判斷的。
常見的寫法如下:
device = torch.device("cpu") if torch.cuda.is_available(): device = torch.device("cuda") a = torch.tensor([1,2,3], device=device) print(a)
輸出結(jié)果如下所示:
tensor([1, 2, 3], device='cuda:0')
2.模型遷移到GPU設(shè)備
在Module對(duì)象上調(diào)用to()方法可以把模型也遷移到GPU設(shè)備上,如下所示:
class LinearRegression(torch.nn.Module): def __init__(self): super(LinearRegression, self).__init__() self.linear = torch.nn.Linear(1, 1) def forward(self, x): return self.linear(x) regression = LinearRegression().to(device=device) for param in regression.parameters(): print(param)
從上述輸出參數(shù)中可以看到param都是device='cuda:0’上的tensor,所以可以說模型通過to()遷移到GPU設(shè)備上了。
原文鏈接:https://blog.csdn.net/ssw_1990/article/details/126496887
相關(guān)推薦
- 2022-06-01 如何利用Python將字典轉(zhuǎn)為成員變量_python
- 2022-05-22 Nginx的基本概念和原理_nginx
- 2022-11-19 React實(shí)現(xiàn)多個(gè)場(chǎng)景下鼠標(biāo)跟隨提示框詳解_React
- 2022-07-06 pandas實(shí)現(xiàn)一行拆分成多行_python
- 2022-04-01 SQL?Server?數(shù)據(jù)庫基礎(chǔ)編程詳解_MsSql
- 2022-11-19 Golang切片Slice功能操作詳情_Golang
- 2023-02-15 Go語言中節(jié)省內(nèi)存技巧方法示例_Golang
- 2022-06-12 QT?.pro文件使用解析_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支