-
Notifications
You must be signed in to change notification settings - Fork 0
/
lesson-05.cpp
88 lines (76 loc) · 3.2 KB
/
lesson-05.cpp
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
#include "tensor.h"
#include "nn.h"
#include <chrono>
#include <iostream>
#include <random>
int main() {
auto engine = std::default_random_engine(std::random_device{}());
int iterations = 100;
auto large = randn({1000, 100}, engine);
auto target = randint(0, 100, {1000}, engine);
// Implemented optimized backpropagation for cross_entropy and BatchNorm1d.
// cross_entropy backprop has ~85x performance boost
// BatchNorm2d backprop has ~15x performance boost
{
auto loss = cross_entropy_unoptimized(large, target);
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < iterations; i++) {
cross_entropy_unoptimized(large, target);
}
std::chrono::duration<double> duration = std::chrono::high_resolution_clock::now() - start;
std::cout << "cross_entropy_unoptimized forward pass: " << duration.count() << " seconds" << std::endl;
start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < iterations; i++) {
loss->backward();
}
duration = std::chrono::high_resolution_clock::now() - start;
std::cout << "cross_entropy_unoptimized backward pass: " << duration.count() << " seconds" << std::endl;
}
{
auto loss = cross_entropy(large, target);
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < iterations; i++) {
cross_entropy(large, target);
}
std::chrono::duration<double> duration = std::chrono::high_resolution_clock::now() - start;
std::cout << "cross_entropy forward pass: " << duration.count() << " seconds" << std::endl;
start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < iterations; i++) {
loss->backward();
}
duration = std::chrono::high_resolution_clock::now() - start;
std::cout << "cross_entropy backward pass: " << duration.count() << " seconds" << std::endl;
}
{
BatchNorm1dUnoptimized bn(100);
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < iterations; i++) {
bn(large);
}
std::chrono::duration<double> duration = std::chrono::high_resolution_clock::now() - start;
std::cout << "BatchNorm1dUnoptimized forward pass: " << duration.count() << " seconds" << std::endl;
auto loss = sum(bn(large));
start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < iterations; i++) {
loss->backward();
}
duration = std::chrono::high_resolution_clock::now() - start;
std::cout << "BatchNorm1dUnoptimized backward pass: " << duration.count() << " seconds" << std::endl;
}
{
BatchNorm1d bn(100);
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < iterations; i++) {
bn(large);
}
std::chrono::duration<double> duration = std::chrono::high_resolution_clock::now() - start;
std::cout << "BatchNorm1d forward pass: " << duration.count() << " seconds" << std::endl;
auto loss = sum(bn(large));
start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < iterations; i++) {
loss->backward();
}
duration = std::chrono::high_resolution_clock::now() - start;
std::cout << "BatchNorm1d backward pass: " << duration.count() << " seconds" << std::endl;
}
}