PyTorch中的CUDA的操作方法_第1頁(yè)
PyTorch中的CUDA的操作方法_第2頁(yè)
PyTorch中的CUDA的操作方法_第3頁(yè)
PyTorch中的CUDA的操作方法_第4頁(yè)
PyTorch中的CUDA的操作方法_第5頁(yè)
已閱讀5頁(yè),還剩4頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第PyTorch中的CUDA的操作方法目錄前言一.常見(jiàn)CPU和GPU操作命令二.CPU和GPU設(shè)備上的Tensor1.Tensor從CPU拷貝到GPU上2.直接在GPU上創(chuàng)建Tensor3.CUDAStreams三.固定緩沖區(qū)四.自動(dòng)設(shè)備感知1.適配CPU和GPU設(shè)備2.模型遷移到GPU設(shè)備

前言

CUDA(ComputeUnifiedDeviceArchitecture)是NVIDIA推出的異構(gòu)計(jì)算平臺(tái),PyTorch中有專(zhuān)門(mén)的模塊torch.cuda來(lái)設(shè)置和運(yùn)行CUDA相關(guān)操作。本地安裝環(huán)境為Windows10,Python3.7.8和CUDA11.6,安裝PyTorch最新穩(wěn)定版本1.12.1如下:

pip3installtorchtorchvisiontorchaudio--extra-index-url/whl/cu116

一.常見(jiàn)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("defaultdevice:{}".format(torch.Tensor([4,5,6]).device))

defaultdevice:cpu

4.查看所有可用的cpu設(shè)備的數(shù)量

print("availablecpudevices:{}".format(torch.cuda.os.cpu_count()))

availablecpudevices:20

這里CPU設(shè)備數(shù)量指的是邏輯處理器的數(shù)量。

5.查看所有可用的gpu設(shè)備的數(shù)量

print("availablegpudevices:{}".format(torch.cuda.device_count()))

availablegpudevices:1

6.獲取gpu設(shè)備的名稱(chēng)

print("gpudevicename:{}".format(torch.cuda.get_device_name(torch.device("cuda:0"))))

gpudevicename:NVIDIAGeForceGTX1080Ti

7.通過(guò)device=cpu:0指定cpu:0設(shè)備

device=torch.Tensor([1,2,3],device="cpu:0").device

print("devicetype:{}".format(device))

devicetype:cpu

8.通過(guò)torch.device指定cpu:0設(shè)備

cpu1=torch.device("cpu:0")

print("cpudevice:{}:{}".format(cpu1.type,cpu1.index))

cpudevice:cpu:0

9.使用索引的方式,默認(rèn)使用CUDA設(shè)備

gpu=torch.device(0)

print("gpudevice:{}:{}".format(gpu.type,gpu.index))

gpudevice:cuda:0

10.通過(guò)torch.device(cuda:0)指定cuda:0設(shè)備

gpu=torch.device("cuda:0")

print("gpudevice:{}:{}".format(gpu.type,gpu.index))

gpudevice:cuda:0

二.CPU和GPU設(shè)備上的Tensor

默認(rèn)情況下創(chuàng)建Tensor是在CPU設(shè)備上的,但是可以通過(guò)copy_、to、cuda等方法將CPU設(shè)備中的Tensor轉(zhuǎn)移到GPU設(shè)備上。當(dāng)然也是可以直接在GPU設(shè)備上創(chuàng)建Tensor的。torch.tensor和torch.Tensor的區(qū)別是,torch.tensor可以通過(guò)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)

#通過(guò)to方法將cpu_tensor拷貝到gpu上

gpu_tensor1=cpu_tensor.to(torch.device("cuda:0"))

print(gpu_tensor1.device)

#通過(guò)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.]])

主要說(shuō)明下這個(gè)copy_()方法,實(shí)現(xiàn)如下:

defcopy_(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.CUDAStreams

Steam是CUDA命令線(xiàn)性執(zhí)行的抽象形式,分配給設(shè)備的CUDA命令按照入隊(duì)序列的順序執(zhí)行。每個(gè)設(shè)備都有一個(gè)默認(rèn)的Steam,也可以通過(guò)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)

foriinrange(100):

#在新的stream上對(duì)默認(rèn)的stream上創(chuàng)建的tensor進(jìn)行求和

withtorch.cuda.stream(s):

#存在的問(wèn)題是:torch.sum()可能會(huì)在torch.randn()之前執(zhí)行

B=torch.sum(A)

print(B)

這個(gè)例子存在的問(wèn)題是torch.sum()可能會(huì)在torch.randn()之前就執(zhí)行。為了保證Stream中的命令絕對(duì)按順序執(zhí)行,接下來(lái)使用Synchronize同步方法解決上面例子的問(wèn)題:

cuda=torch.device("cuda")

s=torch.cuda.Stream()

A=torch.randn((1,10),device=cuda)

default_stream=torch.cuda.current_stream()

print("DefaultStream:{}".format(default_stream))

#等待創(chuàng)建A的stream執(zhí)行完畢

torch.cuda.Stream.synchronize(default_stream)

foriinrange(100):

#在新的stream上對(duì)默認(rèn)的stream上創(chuàng)建的tensor進(jìn)行求和

withtorch.cuda.stream(s):

print("currentstream:{}".format(torch.cuda.current_stream()))

B=torch.sum(A)

print(B)

解決問(wèn)題的思路就是通過(guò)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)存的大小??梢允褂胑mpty_cache方法釋放無(wú)用的緩存內(nèi)存。

三.固定緩沖區(qū)

緩存就是當(dāng)計(jì)算機(jī)內(nèi)存不足的時(shí)候,就會(huì)把內(nèi)存中的數(shù)據(jù)存儲(chǔ)到硬盤(pán)上。固定緩沖區(qū)就是說(shuō)常駐內(nèi)存,不能把這部分?jǐn)?shù)據(jù)緩存到硬盤(pán)上。可以直接使用pin_memory方法或在Tensor上直接調(diào)用pin_memory方法將Tensor復(fù)制到固定緩沖區(qū)。為什么要做固定緩沖區(qū)呢?目的只有一個(gè),就是把CPU上的固定緩沖區(qū)拷貝到GPU上時(shí)速度快。Tensor上的is_pinned方法可以查看該Tensor是否加載到固定緩沖區(qū)中。

fromtorch.utils.data._utils.pin_memoryimportpin_memory

x=torch.Tensor([[1,2,4],[5,7,9],[3,7,10]])

#通過(guò)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

說(shuō)明:通過(guò)id()查看對(duì)象的內(nèi)存地址。

四.自動(dòng)設(shè)備感知

1.適配CPU和GPU設(shè)備

自動(dòng)設(shè)備感知本質(zhì)上就是有GPU時(shí)就使用GPU,沒(méi)有GPU時(shí)就使用CPU,即一套代碼適配CPU和GPU設(shè)備。GPU是否存在是通過(guò)torch.cuda.is_available()判斷的。

常見(jiàn)的寫(xiě)法如下:

device=torch.dev

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論