Pytorch在1.9版本的官方文档中,明确说明了不再建议使用nn.DataParallel
,或者是multiprocessin
g,而是推荐使用nn.parallel.DistributedDataParllel
。即使只有一个GPU核,同样也推荐使用nn.paralle.DistributeDataParalle
。官方文档给出的理由是:
The difference between
DistributedDataParallel
andDataParallel
is:DistributedDataParallel
uses multiprocessing where a process is created for each GPU, whileDataParallel
uses multithreading. By using multiprocessing, each GPU has its dedicated process, this avoids the performance overhead caused by GIL of Python interpreter.
大意是,DistributedDataParallel
比较优秀,是因为他对每个GPU分配一个固定的进程;而DataParallel
不推荐,因为使用的是多线程的方法,这可能会导致来自于GIL或者Python解释器的性能开销。
另一个Basic
文档里面提到,对于torch.multiprocessing
或者是torch.nn.DataParallel
来说,用户必须要显式的对每个进程创建一个独立的、关于主训练脚本的副本。这不方便。