| 17 | |
| 18 | class Net(nn.Module): |
| 19 | def __init__(self, num_gpus=0): |
| 20 | super(Net, self).__init__() |
| 21 | print(f"Using {num_gpus} GPUs to train") |
| 22 | self.num_gpus = num_gpus |
| 23 | if torch.accelerator.is_available() and self.num_gpus > 0: |
| 24 | acc = torch.accelerator.current_accelerator() |
| 25 | device = torch.device(f'{acc}:0') |
| 26 | else: |
| 27 | device = torch.device("cpu") |
| 28 | print(f"Putting first 2 convs on {str(device)}") |
| 29 | # Put conv layers on the first accelerator device |
| 30 | self.conv1 = nn.Conv2d(1, 32, 3, 1).to(device) |
| 31 | self.conv2 = nn.Conv2d(32, 64, 3, 1).to(device) |
| 32 | # Put rest of the network on the 2nd accelerator device, if there is one |
| 33 | if torch.accelerator.is_available() and self.num_gpus > 0: |
| 34 | acc = torch.accelerator.current_accelerator() |
| 35 | device = torch.device(f'{acc}:1') |
| 36 | |
| 37 | print(f"Putting rest of layers on {str(device)}") |
| 38 | self.dropout1 = nn.Dropout2d(0.25).to(device) |
| 39 | self.dropout2 = nn.Dropout2d(0.5).to(device) |
| 40 | self.fc1 = nn.Linear(9216, 128).to(device) |
| 41 | self.fc2 = nn.Linear(128, 10).to(device) |
| 42 | |
| 43 | def forward(self, x): |
| 44 | x = self.conv1(x) |