MCPcopy Create free account
hub / github.com/pytorch/examples / Net

Class Net

distributed/rpc/parameter_server/rpc_parameter_server.py:18–60  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16
17
18class 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)
45 x = F.relu(x)
46 x = self.conv2(x)
47 x = F.max_pool2d(x, 2)
48
49 x = self.dropout1(x)
50 x = torch.flatten(x, 1)
51 # Move tensor to next device if necessary
52 next_device = next(self.fc1.parameters()).device
53 x = x.to(next_device)
54
55 x = self.fc1(x)
56 x = F.relu(x)
57 x = self.dropout2(x)
58 x = self.fc2(x)
59 output = F.log_softmax(x, dim=1)
60 return output
61
62
63# --------- Helper Methods --------------------

Callers 1

__init__Method · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected