Skip to main content
 首页 » 编程设计

Python之在 GPU 工作时将数据从存储传输到 RAM

2024年10月01日7mfryf

这篇文章与深度学习无关,但围绕它进行了一些繁重的工作。

在训练神经网络时,尤其是在处理高分辨率图像时,有一个重复的过程,即从存储设备 (SSD/HDD) 加载图像并将其加载到 RAM,然后将其输入 GPU 进行训练。

有很多时间 GPU 正在做所有的工作,而 CPU 却很空闲,所以我在想,有没有办法在 GPU 工作时将下一批图像加载到 RAM?因为如果我没记错的话,现在发生的是 CPU 从存储中加载图像,将它们传输到 GPU,GPU 做它的事情,然后 GPU 必须等待 CPU 从存储中加载新图像。

我们如何编写生成器,在 GPU 工作时将新图像检索到 RAM?

请您参考如下方法:

好的,假设您有这两个任务:

import time 
 
 
def cpu_operation(n): 
    print('Start CPU', n) 
    for x in range(100): 
        time.sleep(0.01) 
    print('End CPU', n) 
    return n 
 
 
def expensive_gpu_operation(n): 
    print('Start GPU', n) 
    time.sleep(0.3) 
    print('Stop GPU', n) 
    return n 

以下是您现在如何运行它们:

def slow(): 
    results = [] 
    for task in range(5): 
        cpu_result = cpu_operation(task) 
        gpu_result = expensive_gpu_operation(cpu_result) 
        results.append(gpu_result) 
    return results 

我们按顺序运行它们 - CPU、GPU、CPU、GPU...输出如下:

Start CPU 0 
End CPU 0 
Start GPU 0 
Stop GPU 0 
Start CPU 1 
End CPU 1 
Start GPU 1 
Stop GPU 1 
Start CPU 2 
End CPU 2 
Start GPU 2 
Stop GPU 2 
Start CPU 3 
End CPU 3 
Start GPU 3 
Stop GPU 3 
Start CPU 4 
End CPU 4 
Start GPU 4 
Stop GPU 4 

假设我们可以通过在 GPU 任务 X 完成之前启动 CPU 任务 X+1 来节省一些时间,这样 CPU X+1 和 GPU X 并行运行,对吗?

(我们不能并行运行 CPU X 和 GPU X,因为 GPU X 需要来自 CPU X 输出的输入,因此需要 +1。)

让我们使用线程吧!基本上我们想做这样的事情:

  • 启动CPU N,等待它完成
  • 等待 GPU N-1 完成,在后台启动 GPU N

所以我们得到了一些并行性。实现它的最简单方法是具有 1 个线程的线程池 - 它可以像队列一样工作。在每个循环中,我们只安排一个任务并存储 async_result。完成后,我们将能够检索所有结果。

Incidentally, Python has a thread pool implementation in the multiprocessing module.

from multiprocessing.pool import ThreadPool 
 
def quick(): 
    pool = ThreadPool(processes=1) 
    results = [] 
    for task in range(5): 
        cpu_result = cpu_operation(task) 
        # schedule next GPU operation in background, 
        # store the async_result instance for this operation 
        async_result = pool.apply_async(expensive_gpu_operation, (cpu_result, )) 
        results.append(async_result) 
 
    # The results are ready! (Well, the last one probably isn't yet, 
    # but get() will wait for it 
    return [x.get() for x in results] 

现在输出变成:

Start CPU 0 
End CPU 0 
Start CPU 1 
Start GPU 0 
Stop GPU 0 
End CPU 1 
Start CPU 2 
Start GPU 1 
Stop GPU 1 
End CPU 2 
Start CPU 3 
Start GPU 2 
Stop GPU 2 
End CPU 3 
Start CPU 4 
Start GPU 3 
Stop GPU 3 
End CPU 4 
Start GPU 4 
Stop GPU 4 

我们可以观察到并行性!


请注意,当 expensive_gpu_operation 被调度时,它实际上不会运行,直到 time.sleep 进入下一个 CPU 操作。这是由于全局解释器锁 - 在工作线程有机会做某事之前,主线程必须放弃 GIL,这里发生在 time.sleep() 上,在你的情况下我预计它会在您进行一些 i/o 时发生 - 开始阅读下一批图像。