pytorch中多stream加速应用

1. 多stream下的代码如下:

from timeit import default_timer as timer
import sys
import torch


class ProfilerTest(torch.nn.Module) :
    def __init__(self):
        super(ProfilerTest, self).__init__()
        self.conv2 = torch.nn.Conv2d(in_channels = 3, out_channels = 32, kernel_size = 1, stride = 1, padding = 0)
        self.conv5 = torch.nn.Conv2d(in_channels = 32, out_channels = 64, kernel_size = 3, stride = 1, padding = 1)

        self.conv1 = torch.nn.Conv2d(in_channels = 3, out_channels = 32, kernel_size = 3, stride = 1, padding = 1)
        self.conv3 = torch.nn.Conv2d(in_channels = 32, out_channels = 32, kernel_size = 1, stride = 1, padding = 0)
        self.conv4 = torch.nn.Conv2d(in_channels = 32, out_channels = 64, kernel_size = 3, stride = 1, padding = 1)


        self.stream_conv1 = torch.cuda.Stream()

    def forward(self, x, multi_stream):
        #
        o2 = self.conv2(x)
        o2 = self.conv5(o2)
        #
        if multi_stream:
            with torch.cuda.stream(self.stream_conv1):
                o1 = self.conv1(x)
                o1 = self.conv3(o1)
                o1 = self.conv4(o1)
            torch.cuda.current_stream().wait_stream(self.stream_conv1)
        else:
                o1 = self.conv1(x)
                o1 = self.conv3(o1)
                o1 = self.conv4(o1)
        #    
        output = o1 + o2
        #
        return output

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

model = ProfilerTest()

model = model.to(device)


input = torch.randn(1,3,224,224).to(device)

default_stream = torch.cuda.current_stream()

torch.cuda.Stream.synchronize(default_stream)

start = timer()

for i in range(10):
    output = model(input, sys.argv[1])

end = timer()

print ("using time: %.1f ms" % (1000 * (end - start)))

2. 测试结果:

3. nsight下的行为观察:

 profiler脚本如下:

nsys profile --trace=cuda,cudnn,cublas --stats=true python test_torch.py True

kernel的执行行为如下(可发现multi stream下的kernel的并行执行concurrent kernel execution):

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

seasermy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值