PyTorch:在多个GPU上运行推理

bttbmeg0  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(183)

我有一个接受两个输入的模型。我想在多个GPU上运行推理,其中一个输入是固定的,而另一个是变化的。所以,假设我使用n个GPU,每个GPU都有一个模型的副本。首先,GPU处理输入对(a_1,B),第二个进程(a_2,b)等等。所有的输出都保存为文件,所以我不需要对输出执行连接操作。我如何使用DDP或其他方法来执行此操作?

ftf50wuq

ftf50wuq1#

我已经想出了如何使用torch.multiprocessing.Queue来实现这一点:

import torch
import torch.multiprocessing as mp
from absl import app, flags
from torchvision.models import AlexNet

FLAGS = flags.FLAGS

flags.DEFINE_integer("num_processes", 2, "Number of subprocesses to use")

def infer(rank, queue):
    """Each subprocess will run this function on a different GPU which is indicated by the parameter `rank`."""
    model = AlexNet()
    device = torch.device(f"cuda:{rank}")
    model.to(device)
    while True:
        a, b = queue.get()
        if a is None:  # check for sentinel value
            break
        x = a + b
        x = x.to(device)
        model(x)
        del a, b  # free memory
        print(f"Inference on process {rank}")

def main(argv):
    queue = mp.Queue()
    processes = []
    for rank in range(FLAGS.num_processes):
        p = mp.Process(target=infer, args=(rank, queue))
        p.start()
        processes.append(p)
    for _ in range(10):
        a_1 = torch.randn(1, 3, 224, 224)
        a_2 = torch.randn(1, 3, 224, 224)
        b = torch.randn(1, 3, 224, 224)
        queue.put((a_1, b))
        queue.put((a_2, b))
    for _ in range(FLAGS.num_processes):
        queue.put((None, None))  # sentinel value to signal subprocesses to exit
    for p in processes:
        p.join()  # wait for all subprocesses to finish

if __name__ == "__main__":
    app.run(main)

相关问题