-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.cpp
131 lines (110 loc) · 5.87 KB
/
main.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
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
#include <iostream>
#include "include/utils.hpp"
#include "include/py_object.hpp"
#include "include/layers.hpp"
#include "include/inference_engine.hpp"
int main() {
// Set up data - this just creates random tensors in the specified shapes
pycpp::python_home = "../scripts";
pycpp::py_object test ("test");
test("save_array", {pycpp::to_python(16), // out_filters
pycpp::to_python(3), // in_filters
pycpp::to_python(3),
pycpp::to_python(3),
pycpp::to_python("filts.dat")});
test("save_array", {pycpp::to_python(1),
pycpp::to_python(16), // out_filters
pycpp::to_python(1),
pycpp::to_python(1),
pycpp::to_python("bias.dat")});
test("save_array", {pycpp::to_python(1),
pycpp::to_python(16),
pycpp::to_python(1),
pycpp::to_python(1),
pycpp::to_python("gamma.dat")});
test("save_array", {pycpp::to_python(1),
pycpp::to_python(16),
pycpp::to_python(1),
pycpp::to_python(1),
pycpp::to_python("beta.dat")});
test("save_array", {pycpp::to_python(1),
pycpp::to_python(16),
pycpp::to_python(1),
pycpp::to_python(1),
pycpp::to_python("rm.dat")});
test("save_array", {pycpp::to_python(1),
pycpp::to_python(16),
pycpp::to_python(1),
pycpp::to_python(1),
pycpp::to_python("rv.dat")});
test("save_array", {pycpp::to_python(2), // batch
pycpp::to_python(3), // in_filters
pycpp::to_python(224), // change back to 224
pycpp::to_python(224), // change back to 224
pycpp::to_python("img.dat")});
test("save_array", {pycpp::to_python(1), // no batch
pycpp::to_python(1), // no filters
pycpp::to_python(3), // output size
pycpp::to_python(56*56*16), // input size
pycpp::to_python("lin_weight.dat")});
test("save_array", {pycpp::to_python(1), // no batch
pycpp::to_python(1), // no filters
pycpp::to_python(3), // output size
pycpp::to_python(1), // vector
pycpp::to_python("lin_bias.dat")});
PyObject *i = test("load_array", {pycpp::to_python("img.dat")}, {});
af::timer::start();
PyObject *pto = test("test_conv", {pycpp::to_python("filts.dat"),
pycpp::to_python("bias.dat"), pycpp::to_python("img.dat"),
pycpp::to_python("lin_weight.dat"), pycpp::to_python("lin_bias.dat"),
pycpp::to_python("gamma.dat"), pycpp::to_python("beta.dat"),
pycpp::to_python("rm.dat"), pycpp::to_python("rv.dat")});
std::cout << "pytorch forward took (s): " << af::timer::stop() << std::endl;
// Initialize the engine (sets up the backend)
pytorch::inference_engine engine;
// Load up the image and target
pytorch::tensor image = pytorch::internal::from_numpy((PyArrayObject *)i, 4, {2, 3, 224, 224});
pytorch::tensor pytorch_out = pytorch::internal::from_numpy((PyArrayObject *)pto, 4, {2, 3, 1, 1});
// need to reorder if it's coming out of a linear layer
pytorch_out = af::array(pytorch_out.data(), 3, 1, 1, 2); // Get the output from pytorch - this is the result we
// want to replicate
pytorch::conv_params_t params = {3, 3, 1, 1, 1, 1}; // filter_x, filter_y, stride_x, stride_y, pad_x, pad_y
pytorch::pooling_params_t poolparams = {2, 2, 2, 2, 0, 0};
// Set up the layers of our network
// pytorch::Conv2d conv(params, "filts.dat", {1, 3, 3, 5}, "bias.dat", {1, 1, 1, 1});
// pytorch::Linear lin("lin_weight.dat", {1, 1, 3, 8}, "lin_bias.dat", {1, 1, 3, 1});
// pytorch::Hardtanh hardtanh(-1, 1);
// Can set up layers like above (commented) or this way, both have the same effect.
engine.add_layer(new pytorch::Conv2d(params, "filts.dat", {16, 3, 3, 3}, "bias.dat", {1, 16, 1, 1}));
engine.add_layer(new pytorch::BatchNorm2d("gamma.dat", {1, 16, 1, 1},
"beta.dat", {1, 16, 1, 1},
"rm.dat", {1, 16, 1, 1},
"rv.dat", {1, 16, 1, 1}, 1e-5));
engine.add_layer(new pytorch::Tanh);
engine.add_layer(new pytorch::MaxPool2d(poolparams));
engine.add_layer(new pytorch::Sigmoid);
engine.add_layer(new pytorch::Slice(pytorch::k, 2));
engine.add_layer({new pytorch::Skip, new pytorch::Tanh});
engine.add_layer(new pytorch::Concat(pytorch::k));
// engine.add_layer(new pytorch::MaxUnpool2d(poolparams,
// reinterpret_cast<pytorch::MaxPool2d *>(engine.get_layer_ptr(3, 0))));
// engine.add_layer(new pytorch::AvgPool2d(poolparams));
engine.add_layer(new pytorch::MaxPool2d(poolparams));
engine.add_layer(new pytorch::Hardtanh(-0.1f, 0.1f));
engine.add_layer(new pytorch::Linear("lin_weight.dat", {1, 1, 3, 56*56*16}, "lin_bias.dat", {1, 1, 3, 1}));
engine.add_layer(new pytorch::ReLU);
engine.add_layer(new pytorch::Softmax);
// Check speed
int iters = 10;
af::timer::start();
for (int i = 0; i < iters; i++){
pytorch::tensor output = engine.forward({image});
output.eval();
}
af::sync();
std::cout << "forward took: " << af::timer::stop()/iters << " s (on average)" << std::endl;
// Check correctness
pytorch::tensor output = engine.forward({image});
af_print((pytorch_out.data() - output.data()));
return 0;
}