Skip to content

Commit

Permalink
Added move_to_device and options to make cudnn deterministic
Browse files Browse the repository at this point in the history
  • Loading branch information
ArneNx committed Jun 23, 2020
1 parent 7c5188a commit 63496c7
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions nnfabrik/utility/nn_helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# helper functions concerning the ANN architecture

import torch
from torch import nn
from torch.backends import cudnn

from mlutils.training import eval_state
import numpy as np
import random
Expand Down Expand Up @@ -46,8 +49,8 @@ def get_module_output(model, input_shape):
:return: output dimensions of the core
"""
initial_device = 'cuda' if next(iter(model.parameters())).is_cuda else 'cpu'
device = 'cuda' if torch.cuda.is_available() else 'cpu'
initial_device = "cuda" if next(iter(model.parameters())).is_cuda else "cpu"
device = "cuda" if torch.cuda.is_available() else "cpu"
with eval_state(model):
with torch.no_grad():
input = torch.zeros(1, *input_shape[1:]).to(device)
Expand All @@ -64,4 +67,23 @@ def set_random_seed(seed):
random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
cudnn.benchmark = False
cudnn.deterministic = True
torch.cuda.manual_seed(seed)


def move_to_device(model, gpu=True, multi_gpu=True):
"""
Moves given model to GPU(s) if they are available
:param model: (torch.nn.Module) model to move
:param gpu: (bool) if True attempt to move to GPU
:param multi_gpu: (bool) if True attempt to use multi-GPU
:return: torch.nn.Module, str
"""
device = "cuda" if torch.cuda.is_available() and gpu else "cpu"
if multi_gpu and torch.cuda.device_count() > 1:
print("Using ", torch.cuda.device_count(), "GPUs")
model = nn.DataParallel(model)
model = model.to(device)
return model, device

0 comments on commit 63496c7

Please sign in to comment.