pytorch-styleguide icon indicating copy to clipboard operation
pytorch-styleguide copied to clipboard

Use multiple GPUs for training

Open cenjinglun opened this issue 3 years ago • 0 comments

def multi_gpu(model):
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    if device != torch.device('cpu') and torch.cuda.device_count() > 1:
        model = nn.DataParallel(model)
        print(f'Using {torch.cuda.device_count()} GPUs!')
    elif device == torch.device('cuda'):
        print(f'Using 1 GPU!')
    else:
        print('Using CPU!')
    model.to(device)
    return model, device

I do by this.

cenjinglun avatar Oct 14 '21 08:10 cenjinglun