-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathdistributed.py
51 lines (40 loc) · 1.38 KB
/
distributed.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from __future__ import division
from __future__ import print_function
import time
import argparse
import torch
import torch.nn as nn
from torch.nn import Module, Parameter
from torch.nn import functional as F
from parallel.train import RingAllReduce
from torch.utils.data import TensorDataset
parser = argparse.ArgumentParser()
parser.add_argument('-m', '--mode', choices=['py', 'cpp', 'cuda'])
parser.add_argument('-e', '--epoch', type=int, default=100)
parser.add_argument('-s', '--size', type=int, default=100)
options = parser.parse_args()
if options.mode == 'py':
from python.dense import Dense
elif options.mode == 'cpp':
from cpp.dense import Dense
elif options.mode == 'cuda':
from cuda.dense import Dense
inputs = torch.randn(options.size, 256)
labels = torch.rand(options.size).mul(10).long()
dataset = TensorDataset(inputs, labels)
class Model(Module):
def __init__(self):
super(Model, self).__init__()
self.dense1 = Dense(256, 64)
self.dense2 = Dense(64, 16)
self.dense3 = Dense(16, 10)
def forward(self, x):
x = self.dense1(x)
x = self.dense2(x)
x = self.dense3(x)
return F.log_softmax(x, dim=1)
model = Model()
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=1e-4)
handler = RingAllReduce(model, criterion, optimizer, dataset, epoch=options.epoch)
handler.train()