-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
262 lines (202 loc) · 8.58 KB
/
Copy pathmain.cpp
File metadata and controls
262 lines (202 loc) · 8.58 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// MNIST model trainig example
#include <iostream>
#include <torch/csrc/api/include/torch/torch.h>
#ifndef SERIALISED_VERSION
# define USE_C10D_MPI
# include <torch/csrc/distributed/c10d/ProcessGroup.hpp>
# include <torch/csrc/distributed/c10d/ProcessGroupMPI.hpp>
# include <torch/csrc/distributed/c10d/Work.hpp>
# define RUN_MODE_STRING "DISTRIBUTED"
#else
# define RUN_MODE_STRING "SERIALISED "
#endif
#define PROVIDE_TIMING
#if (defined(PROVIDE_TIMING))
# include <chrono>
using namespace std::chrono_literals;
#endif
#define TENSOR_NORMALISE_OP_CST_1 0.1307
#define TENSOR_NORMALISE_OP_CST_2 0.3081
#define CHRONO_SECONDS_TO_DOUBLE(t) \
(double)((double)std::chrono::duration_cast<std::chrono::seconds>(t).count() + \
1e-3 * (double)std::chrono::duration_cast<std::chrono::milliseconds>(t % 1s).count())
//
// Define a Convolutional Module
struct Model : torch::nn::Module {
Model()
: conv1(torch::nn::Conv2dOptions(1, 10, 5)), conv2(torch::nn::Conv2dOptions(10, 20, 5)),
fc1(320, 50), fc2(50, 10)
{
register_module("conv1", conv1);
register_module("conv2", conv2);
register_module("conv2_drop", conv2_drop);
register_module("fc1", fc1);
register_module("fc2", fc2);
}
torch::Tensor forward(torch::Tensor x)
{
x = torch::relu(torch::max_pool2d(conv1->forward(x), 2));
x = torch::relu(torch::max_pool2d(conv2_drop->forward(conv2->forward(x)), 2));
x = x.view({-1, 320});
x = torch::relu(fc1->forward(x));
x = torch::dropout(x, 0.5, is_training());
x = fc2->forward(x);
return torch::log_softmax(x, 1);
}
torch::nn::Conv2d conv1;
torch::nn::Conv2d conv2;
torch::nn::Dropout2d conv2_drop;
torch::nn::Linear fc1;
torch::nn::Linear fc2;
};
int main(int argc, char *argv[])
{
#ifndef SERIALISED_VERSION
// Creating MPI Process Group
auto pg = c10d::ProcessGroupMPI::createProcessGroupMPI();
// Retrieving MPI environment variables
const auto numranks = pg->getSize();
const auto rank = pg->getRank();
#else
constexpr auto numranks = 1;
constexpr auto rank = 0;
#endif
if (0 == rank) {
fprintf(stdout, "\n **** " RUN_MODE_STRING " MNIST toy model ****\n\n");
}
// TRAINING
// Read train dataset
const char *kDataRoot = "./dataset/dataset/";
auto train_dataset = torch::data::datasets::MNIST(kDataRoot)
.map(torch::data::transforms::Normalize<>(TENSOR_NORMALISE_OP_CST_1,
TENSOR_NORMALISE_OP_CST_2))
.map(torch::data::transforms::Stack<>());
#ifndef SERIALISED_VERSION
// Distributed Random Sampler
auto data_sampler = torch::data::samplers::DistributedRandomSampler(
train_dataset.size().value(), numranks, rank, false);
#else
auto data_sampler = torch::data::samplers::RandomSampler(train_dataset.size().value());
#endif
auto num_train_samples_per_proc = train_dataset.size().value() / numranks;
// Generate dataloader
constexpr auto total_batch_size = 64;
auto batch_size_per_proc =
total_batch_size / numranks; // effective batch size in each processor
auto data_loader =
torch::data::make_data_loader(std::move(train_dataset), data_sampler, batch_size_per_proc);
torch::manual_seed(0);
auto model = std::make_shared<Model>();
constexpr auto learning_rate = 1e-2;
torch::optim::SGD optimizer(model->parameters(), learning_rate);
#if (defined(PROVIDE_TIMING))
auto hr_clock = std::chrono::high_resolution_clock();
auto init_time = hr_clock.now();
auto init_train_time = init_time;
auto end_train_time = init_train_time;
auto end_time = end_train_time;
# ifndef SERIALISED_VERSION
std::chrono::duration<double> time_allreduce_plus_wait;
std::chrono::duration<double> time_allreduce;
std::chrono::duration<double> time_dummy_compute;
std::chrono::duration<double> time_waitall;
# endif
#endif
constexpr size_t num_epochs = 10;
for (size_t epoch = 1; epoch <= num_epochs; ++epoch) {
size_t num_correct = 0;
for (auto &batch : *data_loader) {
auto ip = batch.data;
auto op = batch.target.squeeze();
// convert to required formats
ip = ip.to(torch::kF32);
op = op.to(torch::kLong);
// Reset gradients
model->zero_grad();
// Execute forward pass
auto prediction = model->forward(ip);
auto loss = torch::nll_loss(torch::log_softmax(prediction, 1), op);
// Backpropagation
loss.backward();
#ifndef SERIALISED_VERSION
// Averaging the gradients of the parameters in all the processors
// Note: This may lag behind DistributedDataParallel (DDP) in performance
// since this synchronizes parameters after backward pass while DDP
// overlaps synchronizing parameters and computing gradients in backward pass.
auto ts = hr_clock.now();
std::vector<c10::intrusive_ptr<::c10d::Work>> works;
for (auto ¶m : model->named_parameters()) {
std::vector<torch::Tensor> tmp = {param.value().grad()};
auto ti = hr_clock.now();
auto work = pg->allreduce(tmp);
time_allreduce += hr_clock.now() - ti;
works.push_back(std::move(work));
}
auto ti = hr_clock.now();
for (auto &work : works) {
try {
work->wait();
} catch (const std::exception &ex) {
std::cerr << "Exception received: " << ex.what() << std::endl;
pg->abort();
}
}
auto te = hr_clock.now();
time_waitall += te - ti;
time_allreduce_plus_wait += te - ts;
for (auto ¶m : model->named_parameters()) {
param.value().grad().data() = param.value().grad().data() / numranks;
}
#endif
// Update parameters
optimizer.step();
auto guess = prediction.argmax(1);
num_correct += torch::sum(guess.eq_(op)).item<int64_t>();
} // end batch loader
auto accuracy = 100.0 * num_correct / num_train_samples_per_proc;
std::cout << "Accuracy "
#ifndef SERIALISED_VERSION
<< "in rank " << rank
#endif
<< " in epoch " << epoch << " - " << accuracy << std::endl;
} // end epoch
#if (defined(PROVIDE_TIMING))
if (0 == rank) {
end_train_time = hr_clock.now();
}
#endif
// TESTING ONLY IN RANK 0
if (rank == 0) {
auto test_dataset =
torch::data::datasets::MNIST(kDataRoot, torch::data::datasets::MNIST::Mode::kTest)
.map(torch::data::transforms::Normalize<>(TENSOR_NORMALISE_OP_CST_1,
TENSOR_NORMALISE_OP_CST_2))
.map(torch::data::transforms::Stack<>());
auto num_test_samples = test_dataset.size().value();
auto test_loader = torch::data::make_data_loader(std::move(test_dataset), num_test_samples);
model->eval(); // enable eval mode to prevent backprop
size_t num_correct = 0;
for (auto &batch : *test_loader) {
auto ip = batch.data;
auto op = batch.target.squeeze();
// convert to required format
ip = ip.to(torch::kF32);
op = op.to(torch::kLong);
auto prediction = model->forward(ip);
auto loss = torch::nll_loss(torch::log_softmax(prediction, 1), op);
std::cout << "Test loss - " << loss.item<float>() << std::endl;
auto guess = prediction.argmax(1);
num_correct += torch::sum(guess.eq_(op)).item<int64_t>();
} // end test loader
std::cout << "Num correct - " << num_correct << std::endl;
std::cout << "Test Accuracy - " << 100.0 * num_correct / num_test_samples << std::endl;
} // end rank 0
#if (defined(PROVIDE_TIMING))
if (0 == rank) {
auto end_train_time = hr_clock.now();
auto train_time = end_train_time - init_train_time;
fprintf(stdout, "\nTiming report for %ld epochs:\n", num_epochs);
fprintf(stdout, "\t%s %7.3lf s\n", "training:", CHRONO_SECONDS_TO_DOUBLE(train_time));
}
#endif
}