From 595756ed93c48cd160f47ddae45331983339125e Mon Sep 17 00:00:00 2001 From: AndreySorokin7 Date: Thu, 7 Aug 2025 18:19:00 +0300 Subject: [PATCH 1/7] first --- include/graph/graph.hpp | 6 +++ include/layers/Layer.hpp | 8 ++++ test/inference/test_inference.cpp | 66 +++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) diff --git a/include/graph/graph.hpp b/include/graph/graph.hpp index 25713611..5da27cf0 100644 --- a/include/graph/graph.hpp +++ b/include/graph/graph.hpp @@ -114,6 +114,12 @@ class Graph { weights_.push_back(layers_[i]->get_weights()); #endif inten_ = *outten_; + if (layers_[i]->ewops.countlayers > 0) { + for (unsigned int j = 0; j < layers_[i]->ewops.countlayers; j++) { + layers_[i]->ewops.layers[j]->run(inten_, *outten_); + } + inten_ = *outten_; + } #ifdef ENABLE_STATISTIC_TIME auto end = std::chrono::high_resolution_clock::now(); auto elapsed = diff --git a/include/layers/Layer.hpp b/include/layers/Layer.hpp index 9f641a94..a556c1ea 100644 --- a/include/layers/Layer.hpp +++ b/include/layers/Layer.hpp @@ -25,10 +25,18 @@ enum LayerType : uint8_t { enum ImplType : uint8_t { kDefault, kTBB, kSTL }; +class Layer; + +struct EWoperations { + std::vector layers; + unsigned int countlayers = 0; +}; + class Layer { public: Layer() = default; virtual ~Layer() = default; + EWoperations ewops; int getID() const { return id_; } void setID(int id) { id_ = id; } LayerType getName() const { return type_; } diff --git a/test/inference/test_inference.cpp b/test/inference/test_inference.cpp index 7bfc6ab0..b7daa0d4 100644 --- a/test/inference/test_inference.cpp +++ b/test/inference/test_inference.cpp @@ -138,3 +138,69 @@ TEST(bfs, check_end_to_end) { std::vector res(3, 21); ASSERT_EQ(tmp, res); } +TEST(bfs, check_struct_layer) { + Graph graph(5); + Shape sh1({1, 5, 5, 3}); + std::vector vec; + vec.reserve(75); + for (int i = 0; i < 75; ++i) { + vec.push_back(3); + } + Tensor input = make_tensor(vec, sh1); + Tensor output = make_tensor(vec, sh1); + InputLayer a1(kNhwc, kNchw, 1, 2); + a1.setName(kInput); + std::vector kernelvec = {1, 1, 1, 1, 1, 1, 1, 1, 1}; + Shape sh2({3, 3}); + Tensor kernel = make_tensor(kernelvec, sh2); + ConvolutionalLayer a2(1, 0, 1, kernel); + ConvolutionalLayer a3(1, 0, 1, kernel); + + //EWLayer a4("linear", 2.0F, 3.0F); + //a2.ewops.layers.push_back(&a4); + //a2.ewops.countlayers++; + + a2.setName(kConvolution); + a3.setName(kConvolution); + graph.setInput(a1, input); + graph.makeConnection(a1, a2); + graph.makeConnection(a2, a3); + graph.setOutput(a3, output); + graph.inference(); + std::vector tmp = *output.as(); + std::vector res = {81, 81, 81}; + ASSERT_EQ(tmp, res); +} +TEST(bfs, check_struct_layer_added) { + Graph graph(5); + Shape sh1({1, 5, 5, 3}); + std::vector vec; + vec.reserve(75); + for (int i = 0; i < 75; ++i) { + vec.push_back(3); + } + Tensor input = make_tensor(vec, sh1); + Tensor output = make_tensor(vec, sh1); + InputLayer a1(kNhwc, kNchw, 1, 2); + a1.setName(kInput); + std::vector kernelvec = {1, 1, 1, 1, 1, 1, 1, 1, 1}; + Shape sh2({3, 3}); + Tensor kernel = make_tensor(kernelvec, sh2); + ConvolutionalLayer a2(1, 0, 1, kernel); + ConvolutionalLayer a3(1, 0, 1, kernel); + + EWLayer a4("linear", 2.0F, 3.0F); + a2.ewops.layers.push_back(&a4); + a2.ewops.countlayers++; + + a2.setName(kConvolution); + a3.setName(kConvolution); + graph.setInput(a1, input); + graph.makeConnection(a1, a2); + graph.makeConnection(a2, a3); + graph.setOutput(a3, output); + graph.inference(); + std::vector tmp = *output.as(); + std::vector res = {189, 189, 189}; + ASSERT_EQ(tmp, res); +} From 47600403f33d2f946e20ca7474a8b6b2b3ffefd1 Mon Sep 17 00:00:00 2001 From: AndreySorokin7 Date: Thu, 7 Aug 2025 18:25:59 +0300 Subject: [PATCH 2/7] fix --- test/inference/test_inference.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/inference/test_inference.cpp b/test/inference/test_inference.cpp index b7daa0d4..a72de418 100644 --- a/test/inference/test_inference.cpp +++ b/test/inference/test_inference.cpp @@ -156,9 +156,9 @@ TEST(bfs, check_struct_layer) { ConvolutionalLayer a2(1, 0, 1, kernel); ConvolutionalLayer a3(1, 0, 1, kernel); - //EWLayer a4("linear", 2.0F, 3.0F); - //a2.ewops.layers.push_back(&a4); - //a2.ewops.countlayers++; + // EWLayer a4("linear", 2.0F, 3.0F); + // a2.ewops.layers.push_back(&a4); + // a2.ewops.countlayers++; a2.setName(kConvolution); a3.setName(kConvolution); @@ -189,9 +189,9 @@ TEST(bfs, check_struct_layer_added) { ConvolutionalLayer a2(1, 0, 1, kernel); ConvolutionalLayer a3(1, 0, 1, kernel); - EWLayer a4("linear", 2.0F, 3.0F); - a2.ewops.layers.push_back(&a4); - a2.ewops.countlayers++; + EWLayer a4("linear", 2.0F, 3.0F); + a2.ewops.layers.push_back(&a4); + a2.ewops.countlayers++; a2.setName(kConvolution); a3.setName(kConvolution); From 777ac3bf99c8ba2ae25bc7f9ecbb6436c3bda4ee Mon Sep 17 00:00:00 2001 From: AndreySorokin7 Date: Thu, 7 Aug 2025 18:32:05 +0300 Subject: [PATCH 3/7] fix --- include/graph/graph.hpp | 6 +++--- include/layers/Layer.hpp | 6 +++--- test/inference/test_inference.cpp | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/include/graph/graph.hpp b/include/graph/graph.hpp index 5da27cf0..1afbb7e2 100644 --- a/include/graph/graph.hpp +++ b/include/graph/graph.hpp @@ -114,9 +114,9 @@ class Graph { weights_.push_back(layers_[i]->get_weights()); #endif inten_ = *outten_; - if (layers_[i]->ewops.countlayers > 0) { - for (unsigned int j = 0; j < layers_[i]->ewops.countlayers; j++) { - layers_[i]->ewops.layers[j]->run(inten_, *outten_); + if (layers_[i]->postops.count > 0) { + for (unsigned int j = 0; j < layers_[i]->postops.count; j++) { + layers_[i]->postops.layers[j]->run(inten_, *outten_); } inten_ = *outten_; } diff --git a/include/layers/Layer.hpp b/include/layers/Layer.hpp index a556c1ea..10af9d90 100644 --- a/include/layers/Layer.hpp +++ b/include/layers/Layer.hpp @@ -27,16 +27,16 @@ enum ImplType : uint8_t { kDefault, kTBB, kSTL }; class Layer; -struct EWoperations { +struct PostOperations { std::vector layers; - unsigned int countlayers = 0; + unsigned int count = 0; }; class Layer { public: Layer() = default; virtual ~Layer() = default; - EWoperations ewops; + PostOperations postops; int getID() const { return id_; } void setID(int id) { id_ = id; } LayerType getName() const { return type_; } diff --git a/test/inference/test_inference.cpp b/test/inference/test_inference.cpp index a72de418..0134abe6 100644 --- a/test/inference/test_inference.cpp +++ b/test/inference/test_inference.cpp @@ -190,8 +190,8 @@ TEST(bfs, check_struct_layer_added) { ConvolutionalLayer a3(1, 0, 1, kernel); EWLayer a4("linear", 2.0F, 3.0F); - a2.ewops.layers.push_back(&a4); - a2.ewops.countlayers++; + a2.postops.layers.push_back(&a4); + a2.postops.count++; a2.setName(kConvolution); a3.setName(kConvolution); From 4b05f95101fdd488850336c3221aba3bb5d82f86 Mon Sep 17 00:00:00 2001 From: AndreySorokin7 Date: Sat, 9 Aug 2025 23:13:39 +0300 Subject: [PATCH 4/7] fix --- app/Graph/build.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/Graph/build.cpp b/app/Graph/build.cpp index fb514735..b45784da 100644 --- a/app/Graph/build.cpp +++ b/app/Graph/build.cpp @@ -24,6 +24,7 @@ void build_graph(Tensor& input, Tensor& output, bool comments, ImplType impl1 = parallel ? kTBB : kDefault; ImplType impl2 = parallel ? kSTL : kDefault; std::vector> layers; + std::vector layerpostop; std::string json_file = MODEL_PATH_H5; json model_data = read_json(json_file); @@ -73,12 +74,14 @@ void build_graph(Tensor& input, Tensor& output, bool comments, 1, pads, 1, tmp_values, tmp_bias, impl2); conv_layer->setName(kConvolution); layers.push_back(conv_layer); + layerpostop.push_back(0); if (comments) std::cout << "ConvLayer added to layers." << std::endl; } if (layer_type.find("relu") != std::string::npos) { auto ew_layer = std::make_shared("relu"); ew_layer->setName(kElementWise); layers.push_back(ew_layer); + layerpostop.push_back(1); if (comments) std::cout << "Element wise (relu) added to layers" << std::endl; } @@ -99,6 +102,7 @@ void build_graph(Tensor& input, Tensor& output, bool comments, auto fc_layer = std::make_shared(tensor, tmp_bias); fc_layer->setName(kFullyConnected); layers.push_back(fc_layer); + layerpostop.push_back(0); if (comments) std::cout << "DenseLayer added to layers." << std::endl; } @@ -116,6 +120,7 @@ void build_graph(Tensor& input, Tensor& output, bool comments, auto pool_layer = std::make_shared(shape, pooltype, impl1); pool_layer->setName(kPooling); layers.push_back(pool_layer); + layerpostop.push_back(0); if (comments) std::cout << "PoolingLayer added to layers." << std::endl; } @@ -124,6 +129,7 @@ void build_graph(Tensor& input, Tensor& output, bool comments, std::make_shared(std::vector({0, 3, 2, 1})); flatten_layer->setName(kFlatten); layers.push_back(flatten_layer); + layerpostop.push_back(0); if (comments) std::cout << "FlattenLayer added to layers." << std::endl; } @@ -131,6 +137,7 @@ void build_graph(Tensor& input, Tensor& output, bool comments, auto dropout_layer = std::make_shared(0.0); dropout_layer->setName(kDropout); layers.push_back(dropout_layer); + layerpostop.push_back(0); if (comments) std::cout << "DropOutLayer added to layers with probability 0.4 (turned " @@ -155,6 +162,11 @@ void build_graph(Tensor& input, Tensor& output, bool comments, << std::endl; for (size_t i = 0; i < layers.size() - 1; ++i) { + if (layerpostop[i]) { + layers[i-1]->postops.layers.push_back(layers[i].get()); + layers[i-1]->postops.count++; + graph.makeConnection(*layers[i-1], *layers[i + 1]); + } else graph.makeConnection(*layers[i], *layers[i + 1]); } From 4c93b97344ae3b70813b95808a94f62713f67ab1 Mon Sep 17 00:00:00 2001 From: AndreySorokin7 Date: Sat, 9 Aug 2025 23:14:56 +0300 Subject: [PATCH 5/7] fix --- app/Graph/build.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/Graph/build.cpp b/app/Graph/build.cpp index b45784da..a5e8ac4d 100644 --- a/app/Graph/build.cpp +++ b/app/Graph/build.cpp @@ -163,11 +163,11 @@ void build_graph(Tensor& input, Tensor& output, bool comments, for (size_t i = 0; i < layers.size() - 1; ++i) { if (layerpostop[i]) { - layers[i-1]->postops.layers.push_back(layers[i].get()); - layers[i-1]->postops.count++; - graph.makeConnection(*layers[i-1], *layers[i + 1]); + layers[i - 1]->postops.layers.push_back(layers[i].get()); + layers[i - 1]->postops.count++; + graph.makeConnection(*layers[i - 1], *layers[i + 1]); } else - graph.makeConnection(*layers[i], *layers[i + 1]); + graph.makeConnection(*layers[i], *layers[i + 1]); } graph.setOutput(*layers.back(), output); From 4df2bea8cde42c520367ce7563c7aadd378c0735 Mon Sep 17 00:00:00 2001 From: AndreySorokin7 Date: Sun, 10 Aug 2025 00:04:50 +0300 Subject: [PATCH 6/7] fix --- app/Graph/build.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Graph/build.cpp b/app/Graph/build.cpp index a5e8ac4d..bf82270c 100644 --- a/app/Graph/build.cpp +++ b/app/Graph/build.cpp @@ -166,7 +166,7 @@ void build_graph(Tensor& input, Tensor& output, bool comments, layers[i - 1]->postops.layers.push_back(layers[i].get()); layers[i - 1]->postops.count++; graph.makeConnection(*layers[i - 1], *layers[i + 1]); - } else + } else if (!layerpostop[i + 1]) graph.makeConnection(*layers[i], *layers[i + 1]); } From bf969e1aa8a2e60554f868499e19e827431bb894 Mon Sep 17 00:00:00 2001 From: AndreySorokin7 Date: Sun, 10 Aug 2025 10:14:33 +0300 Subject: [PATCH 7/7] fix tidy --- app/Graph/build.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/Graph/build.cpp b/app/Graph/build.cpp index bf82270c..bdba38a0 100644 --- a/app/Graph/build.cpp +++ b/app/Graph/build.cpp @@ -74,14 +74,14 @@ void build_graph(Tensor& input, Tensor& output, bool comments, 1, pads, 1, tmp_values, tmp_bias, impl2); conv_layer->setName(kConvolution); layers.push_back(conv_layer); - layerpostop.push_back(0); + layerpostop.push_back(false); if (comments) std::cout << "ConvLayer added to layers." << std::endl; } if (layer_type.find("relu") != std::string::npos) { auto ew_layer = std::make_shared("relu"); ew_layer->setName(kElementWise); layers.push_back(ew_layer); - layerpostop.push_back(1); + layerpostop.push_back(true); if (comments) std::cout << "Element wise (relu) added to layers" << std::endl; } @@ -102,7 +102,7 @@ void build_graph(Tensor& input, Tensor& output, bool comments, auto fc_layer = std::make_shared(tensor, tmp_bias); fc_layer->setName(kFullyConnected); layers.push_back(fc_layer); - layerpostop.push_back(0); + layerpostop.push_back(false); if (comments) std::cout << "DenseLayer added to layers." << std::endl; } @@ -120,7 +120,7 @@ void build_graph(Tensor& input, Tensor& output, bool comments, auto pool_layer = std::make_shared(shape, pooltype, impl1); pool_layer->setName(kPooling); layers.push_back(pool_layer); - layerpostop.push_back(0); + layerpostop.push_back(false); if (comments) std::cout << "PoolingLayer added to layers." << std::endl; } @@ -129,7 +129,7 @@ void build_graph(Tensor& input, Tensor& output, bool comments, std::make_shared(std::vector({0, 3, 2, 1})); flatten_layer->setName(kFlatten); layers.push_back(flatten_layer); - layerpostop.push_back(0); + layerpostop.push_back(false); if (comments) std::cout << "FlattenLayer added to layers." << std::endl; } @@ -137,7 +137,7 @@ void build_graph(Tensor& input, Tensor& output, bool comments, auto dropout_layer = std::make_shared(0.0); dropout_layer->setName(kDropout); layers.push_back(dropout_layer); - layerpostop.push_back(0); + layerpostop.push_back(false); if (comments) std::cout << "DropOutLayer added to layers with probability 0.4 (turned "