在查看SlowFastNet源代码https://github.com/facebookresearch/SlowFast中的model_builder时,想要采用多GPU训练;GPU大于1的话,代码自动调用torch.nn.parallel.DistributedDataParallel套在model外面。

torch.nn.parallel.DistributedDataParallel和torch.nn.parallel.DataParallel作用相似,都是pytorch调用多GPU训练;但torch.nn.parallel.DistributedDataParallel需有个初始化

在torch.nn.parallel.DistributedDataParallel前加入

torch.distributed.init_process_group('nccl',init_method='file:///home/.../my_file',world_size=1,rank=0)

这里是在单个机器上调用多张GPU,简称单机多卡,所以world_size=1;具体参考
https://github.com/pytorch/examples/tree/master/imagenet

下列代码构造了仅使用index为1,2共计两个GPU同时训练模型

os.environ['CUDA_VISIBLE_DEVICES']='1,2'
device=torch.device('cuda:0')
model=ConvNet(num_classes)
torch.distributed.init_process_group('nccl',init_method='file:///home/.../my_file',world_size=1,rank=0)
model=torch.nn.parallel.DistributedDataParallel(model.to(device)

关于pytorch多GPU训练,下面这篇文章写得有点乱,看了下就是把他放在最后的几篇文章给结合了一下
https://blog.csdn.net/m0_38008956/article/details/86559432

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐