diff --git a/nn/learn.py b/nn/learn.py index a77ebb4..575072a 100644 --- a/nn/learn.py +++ b/nn/learn.py @@ -150,7 +150,7 @@ def train_on_gpu(program_dir: str, board_size: int, batch_size: int, \ weight_decay=WEIGHT_DECAY, nesterov=True) - scaler = torch.cuda.amp.GradScaler() + scaler = torch.amp.GradScaler(device.type) current_lr = SL_LEARNING_RATE @@ -166,7 +166,7 @@ def train_on_gpu(program_dir: str, board_size: int, batch_size: int, \ dual_net.train() epoch_time = time.time() for i in range(0, len(value_data) - batch_size + 1, batch_size): - with torch.cuda.amp.autocast(enabled=True): + with torch.amp.autocast(device.type, enabled=True): plane = torch.tensor(plane_data[i:i+batch_size]).to(device) policy = torch.tensor(policy_data[i:i+batch_size]).to(device) value = torch.tensor(value_data[i:i+batch_size]).to(device) @@ -339,7 +339,7 @@ def train_with_gumbel_alphazero_on_gpu(program_dir: str, board_size: int, \ weight_decay=WEIGHT_DECAY, nesterov=True) - scaler = torch.cuda.amp.GradScaler() + scaler = torch.amp.GradScaler(device.type) num_trained_batches = 0 @@ -368,7 +368,7 @@ def train_with_gumbel_alphazero_on_gpu(program_dir: str, board_size: int, \ dual_net.train() epoch_time = time.time() for i in range(0, len(value_data) - batch_size + 1, batch_size): - with torch.cuda.amp.autocast(enabled=True): + with torch.amp.autocast(device.type, enabled=True): plane = torch.tensor(plane_data[i:i+batch_size]).to(device) policy = torch.tensor(policy_data[i:i+batch_size]).to(device) value = torch.tensor(value_data[i:i+batch_size]).to(device) diff --git a/nn/utility.py b/nn/utility.py index 00a6af1..aa4e043 100644 --- a/nn/utility.py +++ b/nn/utility.py @@ -19,8 +19,11 @@ def get_torch_device(use_gpu: bool) -> torch.device: torch.device: デバイス情報。 """ if use_gpu: - torch.cuda.set_device(0) - return torch.device("cuda") + if torch.cuda.is_available(): + torch.cuda.set_device(0) + return torch.device("cuda") + if torch.backends.mps.is_available(): + return torch.device("mps") return torch.device("cpu")