np.array-tensor(cpu)-tensor(gpu)-np.array哪一步最耗時

AI_ape發表於2020-12-09

深度學習CPU到GPU 哪些步驟很耗時

僅以pytorch numpy.array與cpu/gpu tensor之間的轉換為例:

1.寫段程式碼用於測試時間

為了結果更準確,執行100個迴圈,求各段操作的平均時間:

import numpy as np 
import torch
import time
numpy_tensor,cpu_gpu,gpu_cpu,tensor_numpy=0.0,0.0,0.0,0.0

for i in range(100):
    a=np.random.randint(0,1000,(10000,10000),dtype=np.int16)
    t0 = time.perf_counter()
    b=torch.from_numpy(a)
    t1 = time.perf_counter()
    # print("numpy-tensor:",f"{t1-t0:.3f}")
    numpy_tensor+=t1-t0
    b=b.cuda()
    t2 = time.perf_counter()
    # print("cpu-gpu:",f"{t2-t1:.3f}")
    cpu_gpu+=t2-t1
    c=b.cpu()
    t3 = time.perf_counter()
    # print("gpu-cpu:",f"{t3-t2:.3f}")
    gpu_cpu+=t3-t2
    d=c.numpy()
    t4 = time.perf_counter()
    # print("tensor_numpy:",f"{t4-t3:.3f}")
    tensor_numpy+=t4-t3

print("numpy-tensor:",(numpy_tensor)/100,"\n")
print("cpu-gpu:"     ,(cpu_gpu)/100,"\n")
print(",gpu-cpu:",(gpu_cpu)/100,"\n")
print("tensor_numpy:",(tensor_numpy)/100,"\n")

2.每一步需要的時間

操作時間(s)
numpy-tensor3.870e-05
cpu-gpu0.05468
gpu-cpu0.08956
tensor_numpy0.006042

可見資料從CPU到GPU/GPU到CPU是非常耗時間的。

暫時未發現破解之道。

相關文章