Skip to content

Commit 1c8cf2e

Browse files
NeiroYTNeiroYT
andauthored
Parallel implementations, update layers API (Pooling, Convolution) (#174)
Closes #125, #150, #173 --------- Co-authored-by: NeiroYT <[email protected]>
1 parent ab14c52 commit 1c8cf2e

27 files changed

+549
-204
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
.vscode
22
build
3+
build*
34
3rdparty/tensorflow
45
app/AccuracyImgNet/imgs
56
docs/model_data_alexnet_1.json
7+
docs/input
8+
docs/mnist

3rdparty/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ add_subdirectory(googletest)
33
file(MAKE_DIRECTORY "${CMAKE_SOURCE_DIR}/3rdparty/TBB/build")
44

55
execute_process(
6-
COMMAND ${CMAKE_COMMAND} -S "${CMAKE_SOURCE_DIR}/3rdparty/TBB" -B "${CMAKE_SOURCE_DIR}/3rdparty/TBB/build" -DCMAKE_C_COMPILER_LAUNCHER=${CMAKE_C_COMPILER_LAUNCHER} -DCMAKE_CXX_COMPILER_LAUNCHER=${CMAKE_CXX_COMPILER_LAUNCHER} -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
6+
COMMAND ${CMAKE_COMMAND} -S "${CMAKE_SOURCE_DIR}/3rdparty/TBB" -B "${CMAKE_SOURCE_DIR}/3rdparty/TBB/build" -DCMAKE_C_COMPILER_LAUNCHER=${CMAKE_C_COMPILER_LAUNCHER} -DCMAKE_CXX_COMPILER_LAUNCHER=${CMAKE_CXX_COMPILER_LAUNCHER} -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} -DTBB_OUTPUT_DIR_BASE=tbb
77
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/3rdparty/TBB/build"
88
)
99
execute_process(
@@ -12,4 +12,4 @@ execute_process(
1212
)
1313

1414
option(TBB_TEST OFF)
15-
add_subdirectory(TBB)
15+
add_subdirectory(TBB)

app/Graph/acc_check_mnist.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77

88
using namespace itlab_2023;
99

10-
int main() {
10+
int main(int argc, char* argv[]) {
11+
bool parallel = false;
12+
if (argc > 1 && std::string(argv[1]) == "--parallel") {
13+
std::cout << "Parallel mode" << std::endl;
14+
parallel = true;
15+
}
1116
std::vector<size_t> counts = {979, 1134, 1031, 1009, 981,
1217
891, 957, 1027, 973, 1008};
1318
int stat = 0;
@@ -50,7 +55,7 @@ int main() {
5055
Shape sh({static_cast<size_t>(count_pic), 1, 28, 28});
5156
Tensor t = make_tensor<float>(res, sh);
5257
input = t;
53-
build_graph(input, output, false);
58+
build_graph(input, output, false, parallel);
5459
std::vector<std::vector<float>> tmp_output =
5560
softmax<float>(*output.as<float>(), 10);
5661
std::vector<size_t> indices;

app/Graph/build.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "build.hpp"
22

3-
void build_graph(Tensor& input, Tensor& output, bool comments) {
3+
void build_graph(Tensor& input, Tensor& output, bool comments,
4+
bool parallel = false) {
45
if (comments) {
56
for (size_t i = 0; i < input.get_shape().dims(); i++) {
67
std::cout << input.get_shape()[i] << ' ';
@@ -20,6 +21,8 @@ void build_graph(Tensor& input, Tensor& output, bool comments) {
2021
std::cout << std::endl << std::endl;
2122
}
2223
}
24+
ImplType impl1 = parallel ? kTBB : kDefault;
25+
ImplType impl2 = parallel ? kSTL : kDefault;
2326
std::vector<std::shared_ptr<Layer>> layers;
2427

2528
std::string json_file = MODEL_PATH;
@@ -68,7 +71,7 @@ void build_graph(Tensor& input, Tensor& output, bool comments) {
6871
Tensor tmp_values = tensor;
6972
Tensor tmp_bias = make_tensor(tensor.get_bias());
7073
auto conv_layer = std::make_shared<ConvolutionalLayer>(
71-
1, pads, 1, tmp_values, tmp_bias);
74+
1, pads, 1, tmp_values, tmp_bias, impl2);
7275
conv_layer->setName(kConvolution);
7376
layers.push_back(conv_layer);
7477
if (comments) std::cout << "ConvLayer added to layers." << std::endl;
@@ -111,7 +114,7 @@ void build_graph(Tensor& input, Tensor& output, bool comments) {
111114
if (comments)
112115
std::cout << "PoolingLayer shape: " << shape[0] << "x" << shape[1]
113116
<< std::endl;
114-
auto pool_layer = std::make_shared<PoolingLayer>(shape, pooltype);
117+
auto pool_layer = std::make_shared<PoolingLayer>(shape, pooltype, impl1);
115118
pool_layer->setName(kPooling);
116119
layers.push_back(pool_layer);
117120
if (comments) std::cout << "PoolingLayer added to layers." << std::endl;
@@ -161,6 +164,17 @@ void build_graph(Tensor& input, Tensor& output, bool comments) {
161164

162165
if (comments) std::cout << "Starting inference..." << std::endl;
163166
graph.inference();
167+
#ifdef ENABLE_STATISTIC_TIME
168+
std::vector<std::string> times = graph.getTimeInfo();
169+
std::cout << "!INFERENCE TIME INFO START!" << std::endl;
170+
for (size_t i = 0; i < times.size(); i++) {
171+
std::cout << times[i] << std::endl;
172+
}
173+
std::vector<int> elps_time = graph.getTime();
174+
int sum = std::accumulate(elps_time.begin(), elps_time.end(), 0);
175+
std::cout << "Elapsed inference time:" << sum << std::endl;
176+
std::cout << "!INFERENCE TIME INFO END!" << std::endl;
177+
#endif
164178
if (comments) std::cout << "Inference completed." << std::endl;
165179
if (comments) {
166180
std::vector<float> tmp_output = softmax<float>(*output.as<float>());

app/Graph/build.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616
#include "layers/OutputLayer.hpp"
1717
#include "layers/PoolingLayer.hpp"
1818

19-
void build_graph(Tensor& input, Tensor& output, bool comments);
19+
void build_graph(Tensor& input, Tensor& output, bool comments, bool parallel);

app/Graph/graph_build.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44
namespace fs = std::filesystem;
55
using namespace itlab_2023;
66

7-
int main() {
7+
int main(int argc, char* argv[]) {
88
std::string image_folder = IMAGE1_PATH;
99
std::vector<std::string> image_paths;
10+
bool parallel = false;
11+
if (argc > 1 && std::string(argv[1]) == "--parallel") {
12+
std::cout << "Parallel mode" << std::endl;
13+
parallel = true;
14+
}
1015

1116
for (const auto& entry : fs::directory_iterator(image_folder)) {
1217
if (entry.path().extension() == ".png") {
@@ -43,7 +48,7 @@ int main() {
4348
std::vector<float> vec(75, 3);
4449
Tensor output = make_tensor(vec, sh1);
4550

46-
build_graph(input, output, true);
51+
build_graph(input, output, true, parallel);
4752

4853
std::vector<float> tmp_output = softmax<float>(*output.as<float>());
4954
for (size_t i = 0; i < tmp_output.size(); i++) {

include/graph/graph.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Graph {
2727
#endif
2828
#ifdef ENABLE_STATISTIC_TIME
2929
std::vector<int> time_;
30+
std::vector<LayerType> time_layer_;
3031
#endif
3132
#ifdef ENABLE_STATISTIC_WEIGHTS
3233
std::vector<Tensor> weights_;
@@ -118,6 +119,7 @@ class Graph {
118119
auto elapsed =
119120
std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
120121
time_.push_back(static_cast<int>(elapsed.count()));
122+
time_layer_.push_back(layers_[i]->getName());
121123
#endif
122124
}
123125
}
@@ -129,6 +131,17 @@ class Graph {
129131
std::vector<Tensor> getTensors() { return tensors_; }
130132
#endif
131133
#ifdef ENABLE_STATISTIC_TIME
134+
std::vector<std::string> getTimeInfo() {
135+
std::vector<std::string> res;
136+
std::vector<std::string> labels = {
137+
"Input", "Pooling", "Normalization", "Dropout", "Element-wise",
138+
"Convolution", "Dense", "Flatten", "Output"};
139+
for (size_t i = 0; i < time_.size(); i++) {
140+
res.push_back(labels[static_cast<size_t>(time_layer_[i])] + ':' +
141+
std::to_string(time_[i]));
142+
}
143+
return res;
144+
}
132145
std::vector<int> getTime() { return time_; }
133146
#endif
134147
#ifdef ENABLE_STATISTIC_WEIGHTS

0 commit comments

Comments
 (0)