pytorch nn.conv2d中的不同运行时间

时间:2019-04-19 06:16:51

标签: deep-learning computer-vision pytorch

我遇到了有关正向传播的问题,完整代码如下:

我想准确记录nn.conv2d函数的运行时间,其中time1大约为0.00009901s,而time2大约为0.00011235s。从代码中可以看出,卷积触发器是相同的,我不知道是什么导致了time1和time2的时差。

import os
os.environ["CUDA_VISIBLE_DEVICES"] = '7'

import torch
import torch.nn as nn
from torch.autograd import Variable

import time
import numpy as np
import random

def main():

    test_input1 = Variable(torch.rand(1,36,56,56)).cuda()
    test_input2 = Variable(torch.rand(1,64,56,56)).cuda()
    m1 = torch.nn.Conv2d(36, 36, kernel_size=3, stride=1, padding=1).cuda()

    time1_list = []
    for i in range(10000):
        torch.cuda.synchronize()
        t1 =time.time()
        temp_out = m1(test_input1)
        torch.cuda.synchronize()
        t2=time.time()
        time1_list.append(t2-t1)
    print('time1:%.8f' %(sum(time1_list[50:])/len(time1_list[50:])))



    output = Variable(torch.zeros(1, 64, 56, 56)).cuda()
    k_in_mask = torch.from_numpy(np.array(random.sample(range(0,64), 36))).cuda()
    k_out_mask = torch.from_numpy(np.array(random.sample(range(0,64), 36))).cuda()

    time2_list = []
    for i in range(10000):
        temp_in = torch.index_select(test_input2, 1, Variable(k_in_mask))

        torch.cuda.synchronize()
        t1=time.time()
        temp_out = m1(temp_in)
        torch.cuda.synchronize()
        t2=time.time()
        time2_list.append(t2-t1)

        output.index_copy(1, Variable(k_out_mask), temp_out)
    print('time2:%.8f' %(sum(time2_list[50:])/len(time2_list[50:])))

if __name__=='__main__':
    main()

0 个答案:

没有答案