加快Python算法的四个方法:PyTorch

1.png

相信大家在做一些算法经常会被庞大的数据量所造成的超多计算量需要的时间而折磨的痛苦不已,接下来我们围绕四个方法来帮助大家加快一下Python的计算时间,减少大家在算法上的等待时间

1.介绍:

在PyTorch模块中,我将展示如何使用torch和检查、初始化GPU设备pycuda,以及如何使算法更快。

PyTorch是建立在torch的机器学习库。它得到了Facebook AI研究小组的支持。在最近开发之后,由于它的简单性,动态图形以及它本质上是Python,它在被开发出来之后变得非常流行。它的速度仍然没有落后,在很多情况下可以说是表现的非常好的。

pycuda允许你从python访问Nvidia的CUDA并行计算API。

2.如何检查cuda的可用性?

要检查是否有cuda可用的设备Torch,可以简单地运行一下下面的代码:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="Python" cid="n38" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import torch
torch.cuda.is_available()

True</pre>

3.如何获取有关你的cuda设备的更多信息?

要获取设备的基本信息,可以使用torch.cuda。但是,要获取有关设备的更多信息,可以使用pycuda,这是一个围绕CUDA库开发的python包装器。你可以使用:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="Python" cid="n41" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import torch
import pycuda.driver as cuda
cuda.init()

获取默认设备的Id

torch.cuda.current_device()

0

cuda.Device(0).name() # '0'是你的GPU的id

Tesla K80</pre>

或者你可以这么用:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n43" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">torch.cuda.get_device_name(0)#获取ID为'0'的名称设备
#'Tesla K80'</pre>

我编写了一个简单的类来获取有关cuda兼容GPU的信息:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="Python" cid="n45" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#一个简单的类来了解你的cuda设备
import pycuda.driver as cuda
import pycuda.autoinit #必须使用它的功能
cuda.init() # 需要使用它的功能

class aboutCudaDevices():
def init(self):
pass

def num_devices(self):
"""返回连接的设备数量."""
return cuda.Device.count()

def devices(self):
"""获取所有连接设备的信息."""
num = cuda.Device.count()
print("%d device(s) found:"%num)
for i in range(num):
print(cuda.Device(i).name(), "(Id: %d)"%i)

def mem_info(self):
"""获得所有设备的可用内存和总内存."""
available, total = cuda.mem_get_info()
print("Available: %.2f GB\nTotal: %.2f GB"%(available/1e9, total/1e9))

def attributes(self, device_id=0):
"""获取设备Id的属性 = device_id"""
return cuda.Device(device_id).get_attributes()

def repr(self):
"""类表示为连接到它们的设备的数量."""
num = cuda.Device.count()
string = ""
string += ("%d device(s) found:\n"%num)
for i in range(num):
string += ( " %d) %s (Id: %d)\n"%((i+1),cuda.Device(i).name(),i))
string += (" Memory: %.2f GB\n"%(cuda.Device(i).total_memory()/1e9))
return string

你可以通过输入它的名字来打印输出(repr):

aboutCudaDevices()

1 设备(年代):

1) Tesla K80 (Id: 0)

Memory: 12.00 GB</pre>

要获取当前的内存使用情况,你可以使用pyTorch的函数:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="Python" cid="n47" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">import torch

返回当前的GPU内存使用

一个给定设备的容量(以字节为单位)

torch.cuda.memory_allocated()

函数管理的当前的GPU内存

以字节为给定设备缓存的分配器

torch.cuda.memory_cached()</pre>

在运行应用程序后,可以使用一个简单的命令来清除缓存:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="Python" cid="n49" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># 释放缓存分配器当前持有的所有为占用的缓存内存

以便这些内存可以在其他GPU应用程序中可以使用

并且可以在NVIDIA-SMI中可以进行查看

torch.cuda.empty_cache()</pre>

但是,使用这个命令不会通过张量释放占用的GPU内存,因此它无法增加可用于PyTorch的GPU内存量。

这些内存方法仅适用于GPU。所以这才是真正需要它的地方。

4.如何存储张量并在GPU上运行模型?

使用.cuda函数。

如果你想要在CPU上存储一些内容,可以简单地编写代码:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="Python" cid="n56" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">a = torch.DoubleTensor([1., 2.])</pre>

这个向量是存储在CPU上的,你对它执行的任何操作都是在CPU上执行完成的。而要将其转移到GPU上,你只需要执行以下操作.cuda

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="Python" cid="n58" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">a = torch.FloatTensor([1., 2.]).cuda()</pre>

或者,

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="Python" cid="n60" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">a = torch.cuda.FloatTensor([1., 2.])</pre>

这将为它选择默认设备,该默认设备可通过以下命令查看:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="Python" cid="n62" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">torch.cuda.current_device()
#0</pre>

或者,你也可以执行以下操作:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="Python" cid="n64" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">a.get_device()
#0</pre>

你也可以将一个模型发送到GPU设备。例如,考虑一个简单的模块nn.Sequential

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="Python" cid="n66" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">sq = nn.Sequential(
nn.Linear(20,20),
nn.ReLU(),
nn.Linear(
20,4 ),nn.Softmax()
)</pre>

要将其发送到GPU设备,只需执行以下操作:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="Python" cid="n68" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">model = sq.cuda()</pre>

你可以检查它是否在GPU设备上,为此,你必须检查它的参数是否在GPU设备上,例如:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="Python" cid="n70" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#可以在这里进行讨论: discuss.pytorch.org/t/how-to-check-if-model-is-on-cuda
next(model.parameters()).is_cuda

True</pre>

5.如果有多个GPU,如何选择和使用GPU?

你可以为当前应用/存储选择一个GPU,该GPU可以与你上一个应用/存储选择的GPU不同。

正如在第(2)部分中已经看到的那样,我们可以获得所有与cuda兼容的设备及其Id使用pycuda的情况,在此我们就不再赘述了。

考虑到你有3个cuda兼容的设备,你可以初始化和分配tensors到一个特定的设备,就像这样。

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="Python" cid="n75" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">cuda0 = torch.device('cuda:0')
cuda1 = torch.device('cuda:1')
cuda2 = torch.device('cuda:2')

如果你只使用 'cuda' , 张量/型号将会被发送到默认(当前)设备。(默认值= 0)

x = torch.Tensor([1., 2.], device=cuda1)

或者

x = torch.Tensor([1., 2.]).to(cuda1)

或者

x = torch.Tensor([1., 2.]).cuda(cuda1)

笔记:

如果你想改变默认设备,那么请使用:

torch.cuda.set_device(2) # 设备Id为'2'的地方

如果你只想使用3个GPU中的2个,那么你必须设置环境变量CUDA_VISIBLE_DEVICES 等于"0,2",如果你只想使用第一个和第三个GPU的话

现在如果你想要检查有多少个GPU时,它将显示两个(0,1)

import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0,2"</pre>

当你对这些Tensors 进行任何操作时,无论选择什么设备,都可以执行该操作,结果会和Tensor保存在同一个设备上。

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="Python" cid="n77" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">x = torch.Tensor([1., 2.]).to(cuda2)
y = torch.Tensor([3., 4.]).to(cuda2)

这个Tensor(张量)只会保存在'cuda2' 中

z = x + y</pre>

如果你有多个GPU,则可以在其中划分应用程序的工作,但是他们之间的通信会带来开销。但是,如果你不需要过多的进行传递信息,那你可以尝试一下。

实际上还有一个问题。在PyTorch中的所有GPU操作中,默认情况下都是异步的。尽管在CPU和GPU或两个GPU之间复制数据时确实进行了必要的同步,但是,如果你在命令torch.cuda.Stream()的帮助下创建自己的数据流,那么你讲不得不处理指令的同步

举一个PyTorch文档中的例子,这是不正确的:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n81" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">cuda = torch.device('cuda')
s = torch.cuda.Stream() # 创建一个新的数据流.
A = torch.empty((100, 100), device=cuda).normal_(0.0, 1.0)
with torch.cuda.stream(s):

因为 sum() 可能会在normal_()结束之前开始执行!

B = torch.sum(A)</pre>

如果你想充分利用多个GPU,那么你可以:

  1. 将所有GPU用于不同的任务/应用程序

  2. 将每个GPU用于集合或堆栈中的一个模型,每个GPU都有数据副本(如果可能的话),因为大多数处理是在拟合模型期间完成的,

  3. 在每个GPU中使用带有切片输入的每个GPU和模型副本。每个GPU都会分别计算结果,并将其结果发送到目标GPU,然后再进行进一步的计算等。

6.数据并行性

在数据并行性中,我们将数据(从数据生成器中获得的一个批次的数据)分割为较小的小型批次的数据,然后将其发送到多个GPU进行并行计算。

PyTorch中,数据并行中是使用torch.nn.DataParallel实现的

我们将看到一个简单的例子来了解实际情况。为此,我们将必须使用nn.parallel的某些功能:

  1. Replicate(复制):Module在多个设备上复制。

  2. Scatter(分散):input将第一个维度分配到这些设备中。

  3. Gather(聚集):input收集并连接这些设备的第一个维度。

  4. parallel_apply:将我们从我们从Scatter中得到的一套分布式的input,输入到相应的分布式Module中,我们是通过复制得到的这些模块。

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="Python" cid="n103" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#将模块复制到设备id中的设备
replicas = nn.parallel.replicate(module, device_ids)

将输入分配到设备id中的设备

inputs = nn.parallel.scatter(input, device_ids)

将模型应用于相应的输入

outputs = nn.parallel.parallel_apply(replicas, inputs)

收集所有设备的结果到output_device

result = nn.parallel.gather(outputs, output_device)</pre>

或者,只需要简单地:

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n105" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">model = nn.DataParallel(model, device_ids=device_ids)
result = model(input)</pre>

7.数据并行比较

训练集数据+Val w/数据加载器+SSD中对真实数据的数据扩充

方式1*V100/CUDA 9/CuDNN 74*V100/CUDA 9/CuDNN 7
Pytorch27分钟10分钟
Keras(TF)38分钟18分钟
Tensorflow33分钟22分钟
Chainer29分钟8分钟
MXNet(Gluon)29分钟10分钟

训练集W/在内存中的综合数据

方式1*V100/CUDA 9/CuDnn 74*V100/CUDA 9 / CuDNN 7
Pytorch25分钟8分钟
Keras(TF)36分钟15分钟
Tensorflow25分钟14分钟
Chainer27分钟7分钟
MxNet(Gluon)28分钟8分钟

现在,你可以清楚的看到,即使必须在开始和结束时与主设备进行通信,并行处理也绝对是有帮助的。并且仅在多GPU情况下,PyTorch比所有结果提供结果的时间更快仅仅略低于ChainerPytorch只需要通过对DataParallel的一次调用,就会使其变得简单。

8.torch.multiprocessing

torch.multiprocessing是Python multiprocessing模块的包装,其API与原始模块100%兼容。因此,你可以在此处使用Python的多处理模块中的Queue',Pipe',Array'等。此外,为了使其更快,他们添加了一个方法,share_memory_()该方法允许数据进入一个状态,任何进程都可以直接使用它,因此将该数据作为参数传递给不同的进程将不会复制该数据。

你可以共享Tensors,模型的parameters,也可以根据需要在CPU或GPU上共享它们。

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="" cid="n164" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">来自Pytorch的警告:(关于GPU上的共享)
CUDA API要求导出到其他进程的分配只要在被其他进程使用时就保持有效。你应该小心并确保你共享的CUDA Tensors在必要时不会超出范围。这对于共享模型参数应该不是问题,但是传递其他类型的数据时应格外小心。请注意,此限制不适用于共享CPU内存。</pre>

你可以在此处的“池和进程”部分中使用上面的方法,并且要获得更快的速度,可以使用share_memory_()方法Tensor在所有进程之间共享(例如)而不被复制。

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="Python" cid="n166" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#
import torch.multiprocessing as mp
def train(model):
for data, labels in data_loader:
optimizer.zero_grad()
loss_fn(model(data), labels).backward()
optimizer.step() # 这一步将更新共享参数
model = nn.Sequential(nn.Linear(n_in, n_h1),
nn.ReLU(),
nn.Linear(n_h1, n_out))
model.share_memory() # 需要 'fork' 方法工作
processes = []
for i in range(4): # No. 的过程
p = mp.Process(target=train, args=(model,))
p.start()
processes.append(p)
for p in processes: p.join()</pre>

下一期我们继续看加快Python计算的另一种方法——Numba~

好的,下面是一个使用Python编写的基于PyTorch的强化学习模型示例: 首先,需要安装PyTorch和其他必要的库。可以使用以下命令在Python中安装PyTorch: ``` pip install torch ``` 接下来,导入必要的库: ```python import torch import torch.nn as nn import torch.optim as optim import gym ``` 定义一个神经网络模型,该模型将接收环境状态,并输出每个可能的行动的值。在这个例子中,我们使用两个全连接层和ReLU激活函数。最后一层输出的值是行动的Q值: ```python class DQN(nn.Module): def __init__(self, state_dim, action_dim, hidden_dim): super().__init__() self.fc1 = nn.Linear(state_dim, hidden_dim) self.fc2 = nn.Linear(hidden_dim, hidden_dim) self.fc3 = nn.Linear(hidden_dim, action_dim) def forward(self, x): x = nn.functional.relu(self.fc1(x)) x = nn.functional.relu(self.fc2(x)) x = self.fc3(x) return x ``` 定义一个代理类,该类将实现强化学习代理的行为。在这个例子中,我们使用Deep Q-Network (DQN)算法: ```python class DQNAgent: def __init__(self, env, state_dim, action_dim, hidden_dim, lr, gamma, epsilon): self.env = env self.state_dim = state_dim self.action_dim = action_dim self.gamma = gamma self.epsilon = epsilon self.net = DQN(state_dim, action_dim, hidden_dim) self.optimizer = optim.Adam(self.net.parameters(), lr=lr) self.loss_fn = nn.MSELoss() def act(self, state): if torch.rand(1) < self.epsilon: return self.env.action_space.sample() else: q_values = self.net(torch.FloatTensor(state)) _, action = torch.max(q_values, dim=0) return action.item() def learn(self, batch): states, actions, rewards, next_states, dones = batch q_values = self.net(torch.FloatTensor(states)) next_q_values = self.net(torch.FloatTensor(next_states)) target_q_values = q_values.clone() for i in range(len(actions)): target_q_values[i][actions[i]] = rewards[i] + self.gamma * torch.max(next_q_values[i]) * (1 - dones[i]) loss = self.loss_fn(q_values, target_q_values.detach()) self.optimizer.zero_grad() loss.backward() self.optimizer.step() ``` 最后,我们可以创建一个环境,初始化代理,并开始训练: ```python env = gym.make('CartPole-v1') agent = DQNAgent(env, env.observation_space.shape[0], env.action_space.n, 128, 1e-3, 0.99, 0.1) for episode in range(1000): state = env.reset() done = False total_reward = 0 while not done: action = agent.act(state) next_state, reward, done, info = env.step(action) agent.learn((state, action, reward, next_state, done)) state = next_state total_reward += reward
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值