是否可以将数据存储在MultiGPU中,并使用Pytork计算GPU上的平均值?

2024-06-28 22:06:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我最近在学习Pytork,我想到了这个问题

当提到多GPU时,我可以看到许多关于“DataParallel”的教程。它为不同的GPU拆分计算,然后合并结果

这个问题怎么样?我希望数据存储在不同的GPU中,然后计算它们在GPU上的平均值。例如,如果我有4个形状相同的张量:

tensor_gpu1=tensor_c1.to(device1)
tensor_gpu2=tensor_c2.to(device2)
tensor_gpu3=tensor_c3.to(device3)
tensor_gpu4=tensor_c4.to(device4)
##then I want the average still calculated on GPU (which means no communication with CPU)
##Will it work if I just sum up and divide?
result=(tensor_gpu1+tensor_gpu2+tensor_gpu3+tensor_gpu4)/4 ##Try to add tensors on different GPU.

在读了更多的文章之后,现在我想知道这是否有效。据说,尝试添加CPU张量和GPU张量会导致错误。不同GPU上的张量如何?这将是几天之前,我可以访问一个可用的GPU,所以我不能尝试它现在


Tags: to数据gpuon教程cpu平均值形状