diff --git a/app/Accuracy/accuracy_check.cpp b/app/Accuracy/accuracy_check.cpp index d97648dd..542d5595 100644 --- a/app/Accuracy/accuracy_check.cpp +++ b/app/Accuracy/accuracy_check.cpp @@ -43,16 +43,16 @@ int main() { } Tensor input = t; Tensor output = make_tensor(vec, sh1); - InputLayer a1(kNhwc, kNchw, 1, 2); + auto a1 = std::make_shared(kNchw, kNchw, 1, 2); 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, 0, kernel); + auto a2 = std::make_shared(1, 0, 0, kernel); Shape poolshape = {2, 2}; - EWLayer a3("linear", 2.0F, 3.0F); - PoolingLayer a4(poolshape, "average"); - FCLayer a6; - OutputLayer a5; + auto a3 = std::make_shared("linear", 2.0F, 3.0F); + auto a4 = std::make_shared(poolshape, "average"); + auto a5 = std::make_shared(); + auto a6 = std::make_shared(); graph.setInput(a1, input); graph.makeConnection(a1, a2); graph.makeConnection(a2, a3); diff --git a/app/AccuracyImgNet/accimgnet.cpp b/app/AccuracyImgNet/accimgnet.cpp index 87008f2d..9f3fbe84 100644 --- a/app/AccuracyImgNet/accimgnet.cpp +++ b/app/AccuracyImgNet/accimgnet.cpp @@ -118,11 +118,11 @@ void check_accuracy(const std::string& neural_network_path, Graph a1 = open_network(neural_network_path); Tensor input; Tensor output; - InputLayer inlayer; - OutputLayer outlayer; + auto inlayer = std::make_shared(); + auto outlayer = std::make_shared(); // ?? warning from linux - outlayer.setID(1); - inlayer.setID(0); + outlayer->setID(1); + inlayer->setID(0); // size_t k = 5; for (size_t i = 0; i < imgs_size; i++) { diff --git a/app/Graph/acc_check.cpp b/app/Graph/acc_check.cpp index dbd3d16b..e367d516 100644 --- a/app/Graph/acc_check.cpp +++ b/app/Graph/acc_check.cpp @@ -13,18 +13,15 @@ using namespace it_lab_ai; int main(int argc, char* argv[]) { std::string model_name = "alexnet_mnist"; - bool parallel = false; bool onednn = false; for (int i = 1; i < argc; ++i) { - if (std::string(argv[i]) == "--parallel") { - parallel = true; - } else if (std::string(argv[i]) == "--model" && i + 1 < argc) { + if (std::string(argv[i]) == "--model" && i + 1 < argc) { model_name = argv[++i]; } else if (std::string(argv[i]) == "--onednn") { onednn = true; } } - + it_lab_ai::LayerFactory::configure(onednn); std::string dataset_path; if (model_name == "alexnet_mnist") { dataset_path = MNIST_PATH; @@ -80,7 +77,9 @@ int main(int argc, char* argv[]) { Shape sh({static_cast(count_pic), 1, 28, 28}); Tensor t = make_tensor(res, sh); input = t; - build_graph_linear(input, output, false, parallel, onednn); + auto graph = build_graph_linear(input, output, false); + graph.inference(); + print_time_stats(graph); std::vector> tmp_output = softmax(*output.as(), 10); std::vector indices; @@ -187,7 +186,9 @@ int main(int argc, char* argv[]) { it_lab_ai::Tensor output = it_lab_ai::Tensor(output_shape, it_lab_ai::Type::kFloat); - build_graph(input, output, json_path, false, parallel, onednn); + auto graph = build_graph(input, output, json_path, false); + graph.inference(); + print_time_stats(graph); std::vector> processed_outputs; const std::vector& raw_output = *output.as(); diff --git a/app/Graph/build.cpp b/app/Graph/build.cpp index addd71f2..25e92bb3 100644 --- a/app/Graph/build.cpp +++ b/app/Graph/build.cpp @@ -4,8 +4,8 @@ using namespace it_lab_ai; -void build_graph_linear(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output, - bool comments, bool parallel, bool onednn) { +Graph build_graph_linear(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output, + bool comments) { if (comments) { for (size_t i = 0; i < input.get_shape().dims(); i++) { std::cout << input.get_shape()[i] << ' '; @@ -25,8 +25,6 @@ void build_graph_linear(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output, std::cout << std::endl << std::endl; } } - it_lab_ai::ImplType impl1 = parallel ? it_lab_ai::kTBB : it_lab_ai::kDefault; - it_lab_ai::ImplType impl2 = parallel ? it_lab_ai::kSTL : it_lab_ai::kDefault; std::vector> layers; std::vector layerpostop; @@ -74,18 +72,13 @@ void build_graph_linear(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output, it_lab_ai::Tensor tmp_values = tensor; it_lab_ai::Tensor tmp_bias = it_lab_ai::make_tensor(tensor.get_bias()); auto conv_layer = std::make_shared( - 1, pads, 1, tmp_values, tmp_bias, impl2, 1, true); + 1, pads, 1, tmp_values, tmp_bias, kDefault, 1, true); layers.push_back(conv_layer); layerpostop.push_back(false); if (comments) std::cout << "ConvLayer added to layers." << std::endl; } if (layer_type.find("relu") != std::string::npos) { - std::shared_ptr ew_layer; - if (onednn) { - ew_layer = std::make_shared("relu"); - } else { - ew_layer = std::make_shared("relu"); - } + auto ew_layer = LayerFactory::createEwLayer("relu"); layers.push_back(ew_layer); layerpostop.push_back(true); if (comments) @@ -111,7 +104,7 @@ void build_graph_linear(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output, std::cout << "PoolingLayer shape: " << shape[0] << "x" << shape[1] << std::endl; auto pool_layer = - std::make_shared(shape, pooltype, impl1); + std::make_shared(shape, pooltype, kDefault); layers.push_back(pool_layer); layerpostop.push_back(false); if (comments) std::cout << "PoolingLayer added to layers." << std::endl; @@ -139,14 +132,15 @@ void build_graph_linear(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output, if (comments) std::cout << "number of layers - " << layers.size() + 1 << std::endl; it_lab_ai::Graph graph(static_cast(layers.size())); - it_lab_ai::InputLayer a1(it_lab_ai::kNchw, it_lab_ai::kNchw); + auto a1 = std::make_shared(it_lab_ai::kNchw, + it_lab_ai::kNchw); if (comments) std::cout << "InputLayer created." << std::endl; graph.setInput(a1, input); if (comments) std::cout << "Input set in graph." << std::endl; - graph.makeConnection(a1, *layers[0]); + graph.makeConnection(a1, layers[0]); if (comments) std::cout << "Connection made between InputLayer and first layer." << std::endl; @@ -155,39 +149,17 @@ void build_graph_linear(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output, 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]); + graph.makeConnection(layers[i - 1], layers[i + 1]); } else if (!layerpostop[i + 1]) - graph.makeConnection(*layers[i], *layers[i + 1]); + graph.makeConnection(layers[i], layers[i + 1]); } - graph.setOutput(*layers.back(), output); - if (comments) std::cout << "Output set in graph." << std::endl; + graph.setOutput(layers.back(), output); - if (comments) std::cout << "Starting inference..." << std::endl; - graph.inference(); -#ifdef ENABLE_STATISTIC_TIME - std::vector times = graph.getTimeInfo(); - std::cout << "!INFERENCE TIME INFO START!" << std::endl; - for (size_t i = 0; i < times.size(); i++) { - std::cout << times[i] << std::endl; - } - std::vector elps_time = graph.getTime(); - int sum = std::accumulate(elps_time.begin(), elps_time.end(), 0); - std::cout << "Elapsed inference time:" << sum << std::endl; - std::cout << "!INFERENCE TIME INFO END!" << std::endl; -#endif - if (comments) std::cout << "Inference completed." << std::endl; - if (comments) { - std::vector tmp_output = - it_lab_ai::softmax(*output.as()); - for (size_t i = 0; i < tmp_output.size(); i++) { - if (tmp_output[i] < 1e-6) { - std::cout << i << ": 0" << std::endl; - } else { - std::cout << i << ": " << tmp_output[i] << std::endl; - } - } + for (auto& layer : layers) { + graph.addOwnedLayer(layer); } + return graph; } std::string get_base_layer_name(const std::string& tensor_name) { @@ -234,9 +206,8 @@ std::string layerTypeToString(it_lab_ai::LayerType type) { } } -void build_graph(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output, - const std::string& json_path, bool comments, bool parallel, - bool onednn) { +Graph build_graph(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output, + const std::string& json_path, bool comments) { if (comments) { for (size_t i = 0; i < input.get_shape().dims(); i++) { std::cout << input.get_shape()[i] << ' '; @@ -257,7 +228,7 @@ void build_graph(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output, } } - auto parse_result = parse_json_model(json_path, comments, parallel, onednn); + auto parse_result = parse_json_model(json_path, comments); auto& layers = parse_result.layers; auto& name_to_layer = parse_result.name_to_layer; @@ -275,7 +246,7 @@ void build_graph(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output, [](const auto& layer) { return layer->getName() == it_lab_ai::kInput; }); if (input_layer_it != layers.end()) { - graph.setInput(**input_layer_it, input); + graph.setInput(*input_layer_it, input); } std::vector> connection_list; @@ -331,8 +302,8 @@ void build_graph(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output, } try { - graph.makeConnection(*name_to_layer[source_name], - *name_to_layer[target_name]); + graph.makeConnection(name_to_layer[source_name], + name_to_layer[target_name]); } catch (const std::exception& e) { std::cerr << "Failed: " << source_name << " -> " << target_name << " : " @@ -353,31 +324,15 @@ void build_graph(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output, } graph.setSplitDistribution(split_distribution); auto output_layer = layers.back(); - graph.setOutput(*output_layer, output); - - if (comments) std::cout << "Starting inference..." << std::endl; - try { - graph.inference(); - if (comments) std::cout << "Inference completed successfully." << std::endl; - } catch (const std::exception& e) { - std::cerr << "ERROR during inference: " << e.what() << std::endl; + graph.setOutput(output_layer, output); + for (auto& layer : layers) { + graph.addOwnedLayer(layer); } -#ifdef ENABLE_STATISTIC_TIME - std::vector times = graph.getTimeInfo(); - std::cout << "!INFERENCE TIME INFO START!" << std::endl; - for (size_t i = 0; i < times.size(); i++) { - std::cout << times[i] << std::endl; - } - std::vector elps_time = graph.getTime(); - int sum = std::accumulate(elps_time.begin(), elps_time.end(), 0); - std::cout << "Elapsed inference time:" << sum << std::endl; - std::cout << "!INFERENCE TIME INFO END!" << std::endl; -#endif + return graph; } -ParseResult parse_json_model(const std::string& json_path, bool comments, - bool parallel, bool onednn) { +ParseResult parse_json_model(const std::string& json_path, bool comments) { ParseResult result; auto& layers = result.layers; @@ -390,9 +345,6 @@ ParseResult parse_json_model(const std::string& json_path, bool comments, auto& split_distribution = result.split_distribution; auto& original_ids = result.original_ids; - it_lab_ai::ImplType impl1 = parallel ? it_lab_ai::kTBB : it_lab_ai::kDefault; - it_lab_ai::ImplType impl2 = parallel ? it_lab_ai::kSTL : it_lab_ai::kDefault; - std::unordered_map> layer_parameters; std::unordered_map float_parameters; std::string last_constant_name; @@ -490,25 +442,13 @@ ParseResult parse_json_model(const std::string& json_path, bool comments, it_lab_ai::Tensor tmp_bias = it_lab_ai::make_tensor(tensor.get_bias()); auto conv_layer = std::make_shared( - stride, pads, dilations, tmp_tensor, tmp_bias, impl2, group); + stride, pads, dilations, tmp_tensor, tmp_bias, kDefault, group); layer = conv_layer; } else if (layer_type.find("Relu") != std::string::npos || layer_type.find("relu") != std::string::npos) { - std::shared_ptr ew_layer; - if (onednn) { - ew_layer = std::make_shared("relu"); - } else { - ew_layer = std::make_shared("relu"); - } - layer = ew_layer; + layer = LayerFactory::createEwLayer("relu"); } else if (layer_type.find("Sigmoid") != std::string::npos) { - std::shared_ptr ew_layer; - if (onednn) { - ew_layer = std::make_shared("sigmoid"); - } else { - ew_layer = std::make_shared("sigmoid"); - } - layer = ew_layer; + layer = LayerFactory::createEwLayer("sigmoid"); } else if (layer_type.find("Dense") != std::string::npos || layer_type.find("FullyConnected") != std::string::npos) { it_lab_ai::Tensor tensor = it_lab_ai::create_tensor_from_json( @@ -538,7 +478,7 @@ ParseResult parse_json_model(const std::string& json_path, bool comments, << std::endl; } else if (layer_type == "GlobalAveragePool") { auto pool_layer = std::make_shared( - it_lab_ai::Shape({0, 0}), "average", impl1); + it_lab_ai::Shape({0, 0}), "average", kDefault); layer = pool_layer; if (comments) { std::cout << "GlobalAveragePool layer added (will use input spatial " @@ -599,8 +539,8 @@ ParseResult parse_json_model(const std::string& json_path, bool comments, } } - auto pool_layer = - std::make_shared(shape, pooltype, impl1); + auto pool_layer = std::make_shared( + shape, pooltype, kDefault); try { if (strides[0] != 2 || strides[1] != 2) { @@ -732,39 +672,13 @@ ParseResult parse_json_model(const std::string& json_path, bool comments, if (layer_type == "Mul") { ew_operation = "linear"; - std::shared_ptr ew_layer; - if (onednn) { - ew_layer = std::make_shared( - ew_operation, value, 0.0F); - } else { - ew_layer = std::make_shared(ew_operation, - value, 0.0F); - } - layer = ew_layer; + layer = LayerFactory::createEwLayer(ew_operation, value, 0.0F); } else if (layer_type == "Add") { ew_operation = "linear"; - std::shared_ptr ew_layer; - if (onednn && - it_lab_ai::EwLayerOneDnn::is_function_supported("linear")) { - ew_layer = std::make_shared( - ew_operation, 1.0F, value); - } else { - ew_layer = std::make_shared(ew_operation, - 1.0F, value); - } - layer = ew_layer; + layer = LayerFactory::createEwLayer(ew_operation, 1.0F, value); } else if (layer_type == "Sub") { ew_operation = "linear"; - std::shared_ptr ew_layer; - if (onednn && - it_lab_ai::EwLayerOneDnn::is_function_supported("linear")) { - ew_layer = std::make_shared( - ew_operation, 1.0F, -value); - } else { - ew_layer = std::make_shared(ew_operation, - 1.0F, -value); - } - layer = ew_layer; + layer = LayerFactory::createEwLayer(ew_operation, 1.0F, -value); } else { continue; } @@ -1292,4 +1206,20 @@ it_lab_ai::Tensor prepare_mnist_image(const cv::Mat& image) { Shape sh({1, 1, 28, 28}); return it_lab_ai::make_tensor(res, sh); +} + +void print_time_stats(Graph& graph) { +#ifdef ENABLE_STATISTIC_TIME + std::vector times = graph.getTimeInfo(); + std::cout << "!INFERENCE TIME INFO START!" << std::endl; + for (size_t i = 0; i < times.size(); i++) { + std::cout << times[i] << std::endl; + } + std::vector elps_time = graph.getTime(); + int sum = std::accumulate(elps_time.begin(), elps_time.end(), 0); + std::cout << "Elapsed inference time:" << sum << std::endl; + std::cout << "!INFERENCE TIME INFO END!" << std::endl; +#else + (void)graph; +#endif } \ No newline at end of file diff --git a/app/Graph/build.hpp b/app/Graph/build.hpp index 5d4d651e..7d557ac8 100644 --- a/app/Graph/build.hpp +++ b/app/Graph/build.hpp @@ -56,17 +56,15 @@ struct ParseResult { std::unordered_map original_ids; }; -void build_graph(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output, - const std::string& json_path, bool comments, - bool parallel = false, bool onednn = false); -void build_graph_linear(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output, - bool comments, bool parallel = false, - bool onednn = false); +it_lab_ai::Graph build_graph(it_lab_ai::Tensor& input, + it_lab_ai::Tensor& output, + const std::string& json_path, bool comments); +it_lab_ai::Graph build_graph_linear(it_lab_ai::Tensor& input, + it_lab_ai::Tensor& output, bool comments); std::unordered_map load_class_names( const std::string& filename); -ParseResult parse_json_model(const std::string& json_path, bool comments, - bool parallel, bool onednn); +ParseResult parse_json_model(const std::string& json_path, bool comments); std::vector get_input_shape_from_json(const std::string& json_path); std::vector process_model_output(const std::vector& output, @@ -75,3 +73,27 @@ it_lab_ai::Tensor prepare_image(const cv::Mat& image, const std::vector& input_shape, const std::string& model_name = ""); it_lab_ai::Tensor prepare_mnist_image(const cv::Mat& image); + +void print_time_stats(it_lab_ai::Graph& graph); + +namespace it_lab_ai { +class LayerFactory { + private: + static bool onednn_; + + public: + static void configure(bool onednn) { onednn_ = onednn; } + + static std::shared_ptr createEwLayer(const std::string& function, + float alpha = 1.0F, + float beta = 0.0F) { + if (onednn_ && EwLayerOneDnn::is_function_supported(function)) { + return std::make_shared(function, alpha, beta); + } + return std::make_shared(function, alpha, beta); + } +}; + +bool LayerFactory::onednn_ = false; + +} // namespace it_lab_ai diff --git a/app/Graph/graph_build.cpp b/app/Graph/graph_build.cpp index d2a50d3e..28fffa47 100644 --- a/app/Graph/graph_build.cpp +++ b/app/Graph/graph_build.cpp @@ -10,18 +10,17 @@ using namespace it_lab_ai; int main(int argc, char* argv[]) { std::string model_name = "alexnet_mnist"; - bool parallel = false; bool onednn = false; for (int i = 1; i < argc; ++i) { - if (std::string(argv[i]) == "--parallel") { - parallel = true; - } else if (std::string(argv[i]) == "--model" && i + 1 < argc) { + if (std::string(argv[i]) == "--model" && i + 1 < argc) { model_name = argv[++i]; } else if (std::string(argv[i]) == "--onednn") { onednn = true; } } + it_lab_ai::LayerFactory::configure(onednn); + std::string json_path = model_paths[model_name]; std::vector input_shape; @@ -64,7 +63,16 @@ int main(int argc, char* argv[]) { std::vector vec(75, 3); it_lab_ai::Tensor output = it_lab_ai::make_tensor(vec, sh1); - build_graph_linear(input, output, true, parallel, onednn); + Graph graph = build_graph_linear(input, output, true); + + std::cout << "Starting inference..." << std::endl; + try { + graph.inference(); + std::cout << "Inference completed successfully." << std::endl; + } catch (const std::exception& e) { + std::cerr << "ERROR during inference: " << e.what() << std::endl; + } + print_time_stats(graph); std::vector tmp_output = softmax(*output.as()); int top_n = std::min(3, static_cast(tmp_output.size())); std::vector indices(tmp_output.size()); @@ -94,7 +102,16 @@ int main(int argc, char* argv[]) { size_t output_classes = 1000; it_lab_ai::Tensor output({1, output_classes}, it_lab_ai::Type::kFloat); - build_graph(input, output, json_path, false, parallel, onednn); + Graph graph = build_graph(input, output, json_path, false); + + std::cout << "Starting inference..." << std::endl; + try { + graph.inference(); + std::cout << "Inference completed successfully." << std::endl; + } catch (const std::exception& e) { + std::cerr << "ERROR during inference: " << e.what() << std::endl; + } + print_time_stats(graph); std::vector tmp_output = process_model_output(*output.as(), model_name); diff --git a/include/graph/graph.hpp b/include/graph/graph.hpp index b72e7bc2..9dfb567e 100644 --- a/include/graph/graph.hpp +++ b/include/graph/graph.hpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -23,10 +24,11 @@ struct BranchState { class Graph { int BiggestSize_; - int V_; // amount of ids - std::vector layers_; // layers vector with some ids - std::vector arrayV_; // vertices (id -> vertex number) - std::vector arrayE_; // edges (vertex number -> id) + int V_; // amount of ids + std::vector> owned_layers_; + std::vector layers_; + std::vector arrayV_; // vertices (id -> vertex number) + std::vector arrayE_; // edges (vertex number -> id) std::vector inten_; std::vector outten_; Tensor* outtenres_; @@ -72,6 +74,17 @@ class Graph { split_distribution_ = split_dist; } + void addOwnedLayer(const std::shared_ptr& layer) { + if (!layer) return; + for (const auto& existing_layer : owned_layers_) { + if (existing_layer.get() == layer.get()) { + return; + } + } + + owned_layers_.push_back(layer); + } + int getVertexValue(size_t layerID) const { if (layerID >= arrayV_.size()) { throw std::invalid_argument("ArrayV does not contain this ID."); @@ -94,6 +107,7 @@ class Graph { } int getLayersCount() const { return V_; } + const Layer& getLayerFromID(size_t layerID) const { if (layerID >= layers_.size()) { throw std::invalid_argument("Layers do not contain this ID."); @@ -101,28 +115,31 @@ class Graph { return *layers_[layerID]; } - void setInput(Layer& lay, Tensor& vec) { - lay.setID(0); - layers_.push_back(&lay); + void setInput(const std::shared_ptr& layer, Tensor& vec) { + addOwnedLayer(layer); + layer->setID(0); + layers_.push_back(layer.get()); arrayV_.push_back(0); inten_ = {vec}; - start_ = lay.getID(); + start_ = layer->getID(); V_++; in_edges_.resize(1); } - void addSingleLayer(Layer& lay) { + void addSingleLayer(const std::shared_ptr& layer) { + addOwnedLayer(layer); + bool layer_exists = false; - for (const auto* layer : layers_) { - if (layer == &lay) { + for (const auto* existing_layer : layers_) { + if (existing_layer == layer.get()) { layer_exists = true; break; } } if (!layer_exists) { - lay.setID(V_); - layers_.push_back(&lay); + layer->setID(V_); + layers_.push_back(layer.get()); arrayV_.push_back(static_cast(arrayE_.size())); if (V_ >= static_cast(in_edges_.size())) { @@ -133,18 +150,22 @@ class Graph { } } - void makeConnection(const Layer& layPrev, Layer& layNext) { + void makeConnection(const std::shared_ptr& layPrev, + const std::shared_ptr& layNext) { + addOwnedLayer(layPrev); + addOwnedLayer(layNext); + bool layer_exists = false; for (const auto* layer : layers_) { - if (layer == &layNext) { + if (layer == layNext.get()) { layer_exists = true; break; } } if (!layer_exists) { - layNext.setID(V_); - layers_.push_back(&layNext); + layNext->setID(V_); + layers_.push_back(layNext.get()); arrayV_.push_back(static_cast(arrayE_.size())); if (V_ >= static_cast(in_edges_.size())) { @@ -154,31 +175,35 @@ class Graph { V_++; } - if (layPrev.getID() == layNext.getID()) { + if (layPrev->getID() == layNext->getID()) { throw std::out_of_range("i=j cant add edge"); } - for (int i = layPrev.getID() + 1; i < V_; ++i) { + for (int i = layPrev->getID() + 1; i < V_; ++i) { arrayV_[i]++; } - arrayE_.insert(arrayE_.begin() + arrayV_[layPrev.getID()], layNext.getID()); + arrayE_.insert(arrayE_.begin() + arrayV_[layPrev->getID()], + layNext->getID()); arrayV_[V_] = static_cast(arrayE_.size()); - if (layNext.getID() >= static_cast(in_edges_.size())) { - in_edges_.resize(layNext.getID() + 1); + if (layNext->getID() >= static_cast(in_edges_.size())) { + in_edges_.resize(layNext->getID() + 1); } - in_edges_[layNext.getID()].push_back(layPrev.getID()); + in_edges_[layNext->getID()].push_back(layPrev->getID()); } - bool areLayerNext(const Layer& layPrev, const Layer& layNext) { - for (int i = arrayV_[layPrev.getID()]; i < arrayV_[layPrev.getID() + 1]; + + bool areLayerNext(const std::shared_ptr& layPrev, + const std::shared_ptr& layNext) { + for (int i = arrayV_[layPrev->getID()]; i < arrayV_[layPrev->getID() + 1]; i++) { - if (arrayE_[i] == layNext.getID()) { + if (arrayE_[i] == layNext->getID()) { return true; } } return false; } + void inference() { std::vector> countinout = getInOutDegrees(); std::vector traversal = getTraversalOrder(); @@ -276,13 +301,15 @@ class Graph { *outtenres_ = outten_[0]; } - void setOutput(const Layer& lay, Tensor& vec) { - end_ = lay.getID(); + + void setOutput(const std::shared_ptr& layer, Tensor& vec) { + end_ = layer->getID(); outtenres_ = &vec; std::vector vec1 = {1, 7, 1, 0}; Tensor start = make_tensor(vec1); outten_.push_back(start); } + #ifdef ENABLE_STATISTIC_TENSORS std::vector getTensors() { return tensors_; } #endif @@ -320,6 +347,7 @@ class Graph { #ifdef ENABLE_STATISTIC_WEIGHTS std::vector getWEIGHTS() { return weights_; } #endif + std::vector> getInOutDegrees() const { std::vector in_degree(V_, 0); @@ -340,6 +368,7 @@ class Graph { return result; } + std::vector getTraversalOrder() const { auto in_out_degrees = getInOutDegrees(); std::vector in_degree(V_); diff --git a/test/graph/test_graph.cpp b/test/graph/test_graph.cpp index 5aa42890..ce84dcca 100644 --- a/test/graph/test_graph.cpp +++ b/test/graph/test_graph.cpp @@ -19,9 +19,10 @@ TEST(graph, check_connection) { Tensor input = make_tensor({1.0F, 2.0F}, {2}); Tensor output; Graph graph(5); - FCLayer fcLayer(weights, bias); - InputLayer inputLayer; - EWLayer ewLayer; + + auto fcLayer = std::make_shared(weights, bias); + auto inputLayer = std::make_shared(); + auto ewLayer = std::make_shared(); graph.setInput(inputLayer, input); graph.makeConnection(inputLayer, fcLayer); @@ -38,10 +39,11 @@ TEST(graph, check_connection1) { Tensor output; Graph graph(5); - FCLayer fcLayer(weights, bias); - InputLayer inputLayer; - EWLayer ewLayer; - FCLayer fcLayer2(weights, bias); + + auto fcLayer = std::make_shared(weights, bias); + auto inputLayer = std::make_shared(); + auto ewLayer = std::make_shared(); + auto fcLayer2 = std::make_shared(weights, bias); graph.setInput(inputLayer, input); graph.makeConnection(inputLayer, fcLayer); @@ -60,10 +62,11 @@ TEST(graph, check_connection_when_not_connection) { Tensor output; Graph graph(5); - FCLayer fcLayer(weights, bias); - InputLayer inputLayer; - EWLayer ewLayer; - FCLayer fcLayer2(weights, bias); + + auto fcLayer = std::make_shared(weights, bias); + auto inputLayer = std::make_shared(); + auto ewLayer = std::make_shared(); + auto fcLayer2 = std::make_shared(weights, bias); graph.setInput(inputLayer, input); graph.makeConnection(inputLayer, fcLayer); @@ -85,10 +88,11 @@ TEST(graph, check_connection_when_not_connection1) { Tensor output; Graph graph(5); - FCLayer fcLayer(weights, bias); - FCLayer fcLayer2(weights, bias); - FCLayer fcLayer3(weights, bias); - FCLayer fcLayer4(weights, bias); + + auto fcLayer = std::make_shared(weights, bias); + auto fcLayer2 = std::make_shared(weights, bias); + auto fcLayer3 = std::make_shared(weights, bias); + auto fcLayer4 = std::make_shared(weights, bias); graph.setInput(fcLayer, input); graph.makeConnection(fcLayer, fcLayer2); @@ -107,10 +111,11 @@ TEST(graph, check_connection_when_not_connection2) { Tensor output; Graph graph(5); - FCLayer fcLayer(weights, bias); - FCLayer fcLayer2(weights, bias); - FCLayer fcLayer3(weights, bias); - FCLayer fcLayer4(weights, bias); + + auto fcLayer = std::make_shared(weights, bias); + auto fcLayer2 = std::make_shared(weights, bias); + auto fcLayer3 = std::make_shared(weights, bias); + auto fcLayer4 = std::make_shared(weights, bias); graph.setInput(fcLayer, input); graph.makeConnection(fcLayer, fcLayer2); @@ -129,10 +134,11 @@ TEST(graph, vertex_out_of_range) { Tensor output; Graph graph(5); - FCLayer fcLayer(weights, bias); - FCLayer fcLayer2(weights, bias); - FCLayer fcLayer3(weights, bias); - FCLayer fcLayer4(weights, bias); + + auto fcLayer = std::make_shared(weights, bias); + auto fcLayer2 = std::make_shared(weights, bias); + auto fcLayer3 = std::make_shared(weights, bias); + auto fcLayer4 = std::make_shared(weights, bias); graph.setInput(fcLayer, input); graph.makeConnection(fcLayer, fcLayer2); @@ -150,10 +156,11 @@ TEST(graph, edges_out_of_range) { Tensor output; Graph graph(5); - FCLayer fcLayer(weights, bias); - FCLayer fcLayer2(weights, bias); - FCLayer fcLayer3(weights, bias); - FCLayer fcLayer4(weights, bias); + + auto fcLayer = std::make_shared(weights, bias); + auto fcLayer2 = std::make_shared(weights, bias); + auto fcLayer3 = std::make_shared(weights, bias); + auto fcLayer4 = std::make_shared(weights, bias); graph.setInput(fcLayer, input); graph.makeConnection(fcLayer, fcLayer2); @@ -171,10 +178,11 @@ TEST(graph, inputs_out_of_range) { Tensor output; Graph graph(5); - FCLayer fcLayer(weights, bias); - FCLayer fcLayer2(weights, bias); - FCLayer fcLayer3(weights, bias); - FCLayer fcLayer4(weights, bias); + + auto fcLayer = std::make_shared(weights, bias); + auto fcLayer2 = std::make_shared(weights, bias); + auto fcLayer3 = std::make_shared(weights, bias); + auto fcLayer4 = std::make_shared(weights, bias); graph.setInput(fcLayer, input); graph.makeConnection(fcLayer, fcLayer2); @@ -192,10 +200,11 @@ TEST(graph, get_layer_out_of_range) { Tensor output; Graph graph(5); - FCLayer fcLayer(weights, bias); - FCLayer fcLayer2(weights, bias); - FCLayer fcLayer3(weights, bias); - FCLayer fcLayer4(weights, bias); + + auto fcLayer = std::make_shared(weights, bias); + auto fcLayer2 = std::make_shared(weights, bias); + auto fcLayer3 = std::make_shared(weights, bias); + auto fcLayer4 = std::make_shared(weights, bias); graph.setInput(fcLayer, input); graph.makeConnection(fcLayer, fcLayer2); @@ -214,10 +223,11 @@ TEST(graph_transformations, check_subgraphs_search) { Graph graph(5); Graph subgraph(2); - FCLayer fcLayer(weights, bias); - FCLayer fcLayer2(weights, bias); - FCLayer fcLayer3(weights, bias); - FCLayer fcLayer4(weights, bias); + + auto fcLayer = std::make_shared(weights, bias); + auto fcLayer2 = std::make_shared(weights, bias); + auto fcLayer3 = std::make_shared(weights, bias); + auto fcLayer4 = std::make_shared(weights, bias); graph.setInput(fcLayer, input); graph.makeConnection(fcLayer, fcLayer2); @@ -241,11 +251,11 @@ TEST(graph_transformations, check_subgraphs_search1) { Graph graph(5); Graph subgraph(2); - FCLayer fcLayer(weights, bias); - FCLayer fcLayer2(weights, bias); - FCLayer fcLayer3(weights, bias); - FCLayer fcLayer4(weights, bias); - EWLayer ewLayer5("relu"); + auto fcLayer = std::make_shared(weights, bias); + auto fcLayer2 = std::make_shared(weights, bias); + auto fcLayer3 = std::make_shared(weights, bias); + auto fcLayer4 = std::make_shared(weights, bias); + auto ewLayer5 = std::make_shared("relu"); graph.setInput(fcLayer, input); graph.makeConnection(fcLayer, fcLayer2); @@ -270,10 +280,10 @@ TEST(graph_transformations, check_subgraphs_search2) { Graph graph(5); Graph subgraph(2); - FCLayer fcLayer(weights, bias); - FCLayer fcLayer2(weights, bias); - FCLayer fcLayer3(weights, bias); - FCLayer fcLayer4(weights, bias); + auto fcLayer = std::make_shared(weights, bias); + auto fcLayer2 = std::make_shared(weights, bias); + auto fcLayer3 = std::make_shared(weights, bias); + auto fcLayer4 = std::make_shared(weights, bias); graph.setInput(fcLayer, input); graph.makeConnection(fcLayer, fcLayer2); @@ -300,10 +310,10 @@ TEST(graph_transformations, check_subgraphs_search3) { Graph graph(5); Graph subgraph(2); - FCLayer fcLayer(weights, bias); - FCLayer fcLayer2(weights, bias); - FCLayer fcLayer3(weights, bias); - FCLayer fcLayer4(weights, bias); + auto fcLayer = std::make_shared(weights, bias); + auto fcLayer2 = std::make_shared(weights, bias); + auto fcLayer3 = std::make_shared(weights, bias); + auto fcLayer4 = std::make_shared(weights, bias); graph.setInput(fcLayer, input); graph.makeConnection(fcLayer, fcLayer2); @@ -330,10 +340,10 @@ TEST(graph_transformations, check_subgraphs_search4) { Graph graph(5); Graph subgraph(2); - FCLayer fcLayer(weights, bias); - FCLayer fcLayer2(weights, bias); - FCLayer fcLayer3(weights, bias); - FCLayer fcLayer4(weights, bias); + auto fcLayer = std::make_shared(weights, bias); + auto fcLayer2 = std::make_shared(weights, bias); + auto fcLayer3 = std::make_shared(weights, bias); + auto fcLayer4 = std::make_shared(weights, bias); graph.setInput(fcLayer, input); graph.makeConnection(fcLayer, fcLayer2); @@ -360,11 +370,11 @@ TEST(graph_transformations, check_subgraphs_search5) { Graph graph(5); Graph subgraph(2); - FCLayer fcLayer(weights, bias); - FCLayer fcLayer2(weights, bias); - FCLayer fcLayer3(weights, bias); - FCLayer fcLayer4(weights, bias); - EWLayer ewLayer5("relu"); + auto fcLayer = std::make_shared(weights, bias); + auto fcLayer2 = std::make_shared(weights, bias); + auto fcLayer3 = std::make_shared(weights, bias); + auto fcLayer4 = std::make_shared(weights, bias); + auto ewLayer5 = std::make_shared("relu"); graph.setInput(fcLayer, input); graph.makeConnection(fcLayer, fcLayer2); @@ -392,6 +402,7 @@ TEST(graph_transformations, check_subgraphs_big_random) { Tensor output; Graph graph(num_vertices); Graph subgraph(3); + std::vector> layers; for (int i = 0; i < num_vertices / 2; i++) { layers.push_back(std::make_shared(weights, bias)); @@ -399,26 +410,27 @@ TEST(graph_transformations, check_subgraphs_big_random) { for (int i = 0; i < num_vertices / 2; i++) { layers.push_back(std::make_shared("relu")); } - graph.setInput(*layers[0], input); + + graph.setInput(layers[0], input); for (int i = 0; i < num_vertices; i++) { int rFirst = rand() % (num_vertices - 1); int rSecond = 1 + rand() % (num_vertices - 1); if ((rFirst == rSecond) || - (((*layers[rFirst]).getID() == (*layers[rSecond]).getID()) && - ((*layers[rFirst]).getID() != 0))) { + ((layers[rFirst]->getID() == layers[rSecond]->getID()) && + (layers[rFirst]->getID() != 0))) { continue; } - if (((*layers[rFirst]).getID() >= graph.getLayersCount()) || - (rFirst != 0 && (*layers[rFirst]).getID() == 0)) { - graph.addSingleLayer(*layers[rFirst]); + if ((layers[rFirst]->getID() >= graph.getLayersCount()) || + (rFirst != 0 && layers[rFirst]->getID() == 0)) { + graph.addSingleLayer(layers[rFirst]); } - graph.makeConnection(*layers[rFirst], *layers[rSecond]); + graph.makeConnection(layers[rFirst], layers[rSecond]); } - graph.setOutput(*layers[num_vertices - 1], output); + graph.setOutput(layers[num_vertices - 1], output); - subgraph.setInput(*layers[0], input); - subgraph.makeConnection(*layers[0], *layers[50]); - subgraph.makeConnection(*layers[50], *layers[1]); + subgraph.setInput(layers[0], input); + subgraph.makeConnection(layers[0], layers[50]); + subgraph.makeConnection(layers[50], layers[1]); std::vector> res1 = find_subgraphs(graph, subgraph); double res1_time = diff --git a/test/inference/test_inference.cpp b/test/inference/test_inference.cpp index a297b220..4d6d6569 100644 --- a/test/inference/test_inference.cpp +++ b/test/inference/test_inference.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -26,35 +27,35 @@ TEST(bfs, check_struct_graph) { } Tensor input = make_tensor(vec, sh1); Tensor output = make_tensor(vec, sh1); - InputLayer a1(kNhwc, kNchw, 1, 2); + + auto a1 = std::make_shared(kNhwc, kNchw, 1, 2); 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); + auto a2 = std::make_shared(1, 0, 1, kernel); - ConvolutionalLayer a3_1(1, 0, 1, kernel); - EWLayer a3_1_1("relu"); - ConvolutionalLayer a3_2(1, 0, 1, kernel); - EWLayer a3_2_1("relu"); + auto a3_1 = std::make_shared(1, 0, 1, kernel); + auto a3_1_1 = std::make_shared("relu"); + auto a3_2 = std::make_shared(1, 0, 1, kernel); + auto a3_2_1 = std::make_shared("relu"); - ConcatLayer a4(0); - EWLayer a5("relu"); + auto a4 = std::make_shared(0); + auto a5 = std::make_shared("relu"); - EWLayer a6_1("relu"); - EWLayer a6_2("relu"); + auto a6_1 = std::make_shared("relu"); + auto a6_2 = std::make_shared("relu"); - ConcatLayer a7(0); - // EWLayer a8("relu"); - SplitLayer a8(1, 3); + auto a7 = std::make_shared(0); + auto a8 = std::make_shared(1, 3); - EWLayer a9_1("relu"); - EWLayer a9_2("relu"); - EWLayer a9_3("relu"); + auto a9_1 = std::make_shared("relu"); + auto a9_2 = std::make_shared("relu"); + auto a9_3 = std::make_shared("relu"); - ConcatLayer a10(0); - EWLayer a11_1("relu"); + auto a10 = std::make_shared(0); + auto a11_1 = std::make_shared("relu"); - ConcatLayer a12(0); + auto a12 = std::make_shared(0); graph.setInput(a1, input); graph.makeConnection(a1, a2); @@ -101,22 +102,18 @@ TEST(bfs, check_struct_graph_not_used_yolo) { std::vector kernelvec = {1, 1, 1, 1, 1, 1, 1, 1, 1}; Shape sh2({3, 3}); Tensor kernel = make_tensor(kernelvec, sh2); - // EWLayer a2("relu"); //split 1 , 4 - SplitLayer a2(1, 4); - - EWLayer a3_1("relu"); - EWLayer a3_1_1("relu"); - - ConcatLayer a3_2(0); - EWLayer a3_2_1("relu"); - - EWLayer a3_3("relu"); - ConcatLayer a3_3_1(0); - EWLayer a3_3_2("relu"); - EWLayer a3_3_3("relu"); - EWLayer a3_3_4("relu"); - ConcatLayer a4(0); + auto a2 = std::make_shared(1, 4); + auto a3_1 = std::make_shared("relu"); + auto a3_1_1 = std::make_shared("relu"); + auto a3_2 = std::make_shared(0); + auto a3_2_1 = std::make_shared("relu"); + auto a3_3 = std::make_shared("relu"); + auto a3_3_1 = std::make_shared(0); + auto a3_3_2 = std::make_shared("relu"); + auto a3_3_3 = std::make_shared("relu"); + auto a3_3_4 = std::make_shared("relu"); + auto a4 = std::make_shared(0); graph.setInput(a2, input); graph.makeConnection(a2, a3_1); @@ -151,19 +148,16 @@ TEST(bfs, check_struct_graph_resnet1) { Tensor input = make_tensor(vec, sh1); Tensor output = make_tensor(vec, sh1); - SplitLayer a2(1, 2); - - EWLayer a2_1("relu"); - EWLayer a2_1_1("relu"); - - EWLayer a2_1_1_1("relu"); - EWLayer a2_1_1_2("relu"); - - BinaryOpLayer a2_1_2(BinaryOpLayer::Operation::kMul); - EWLayer a2_1_3("relu"); - EWLayer a2_2("relu"); - BinaryOpLayer a3(BinaryOpLayer::Operation::kAdd); - EWLayer a4("relu"); + auto a2 = std::make_shared(1, 2); + auto a2_1 = std::make_shared("relu"); + auto a2_1_1 = std::make_shared("relu"); + auto a2_1_1_1 = std::make_shared("relu"); + auto a2_1_1_2 = std::make_shared("relu"); + auto a2_1_2 = std::make_shared(BinaryOpLayer::Operation::kMul); + auto a2_1_3 = std::make_shared("relu"); + auto a2_2 = std::make_shared("relu"); + auto a3 = std::make_shared(BinaryOpLayer::Operation::kAdd); + auto a4 = std::make_shared("relu"); graph.setInput(a2, input); graph.makeConnection(a2, a2_1); @@ -195,18 +189,15 @@ TEST(bfs, check_struct_graph_resnet2) { Tensor input = make_tensor(vec, sh1); Tensor output = make_tensor(vec, sh1); - SplitLayer a2(1, 2); - - EWLayer a2_1("relu"); - EWLayer a2_1_1("relu"); - - EWLayer a2_1_1_1("relu"); - EWLayer a2_1_1_2("relu"); - - BinaryOpLayer a2_1_2(BinaryOpLayer::Operation::kMul); - EWLayer a2_1_3("relu"); - BinaryOpLayer a3(BinaryOpLayer::Operation::kAdd); - EWLayer a4("relu"); + auto a2 = std::make_shared(1, 2); + auto a2_1 = std::make_shared("relu"); + auto a2_1_1 = std::make_shared("relu"); + auto a2_1_1_1 = std::make_shared("relu"); + auto a2_1_1_2 = std::make_shared("relu"); + auto a2_1_2 = std::make_shared(BinaryOpLayer::Operation::kMul); + auto a2_1_3 = std::make_shared("relu"); + auto a3 = std::make_shared(BinaryOpLayer::Operation::kAdd); + auto a4 = std::make_shared("relu"); graph.setInput(a2, input); graph.makeConnection(a2, a2_1); @@ -237,17 +228,14 @@ TEST(bfs, check_struct_graph_google1) { Tensor input = make_tensor(vec, sh1); Tensor output = make_tensor(vec, sh1); - EWLayer a2("relu"); - - EWLayer a2_1("relu"); - EWLayer a2_2("relu"); - EWLayer a2_3("relu"); - EWLayer a2_4("relu"); - - EWLayer a2_2_1("linear", 2.0F, 3.0F); - EWLayer a2_3_1("linear", 2.0F, 3.0F); - - ConcatLayer a3(0); + auto a2 = std::make_shared("relu"); + auto a2_1 = std::make_shared("relu"); + auto a2_2 = std::make_shared("relu"); + auto a2_3 = std::make_shared("relu"); + auto a2_4 = std::make_shared("relu"); + auto a2_2_1 = std::make_shared("linear", 2.0F, 3.0F); + auto a2_3_1 = std::make_shared("linear", 2.0F, 3.0F); + auto a3 = std::make_shared(0); graph.setInput(a2, input); graph.makeConnection(a2, a2_1); @@ -280,13 +268,15 @@ TEST(bfs, check_result_vec) { } Tensor input = make_tensor(vec, sh1); Tensor output = make_tensor(vec, sh1); - InputLayer a1(kNhwc, kNchw, 1, 2); - InputLayer a3(kNhwc, kNhwc, 1, 1); + + auto a1 = std::make_shared(kNhwc, kNchw, 1, 2); + auto a3 = std::make_shared(kNhwc, kNhwc, 1, 1); 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 a4(1, 0, 1, kernel); + auto a2 = std::make_shared(1, 0, 1, kernel); + auto a4 = std::make_shared(1, 0, 1, kernel); + graph.setInput(a1, input); graph.makeConnection(a1, a2); graph.makeConnection(a2, a4); @@ -351,7 +341,8 @@ TEST(bfs, check_end_to_end) { } Tensor input = make_tensor(vec, sh1); Tensor output = make_tensor(vec, sh1); - InputLayer a1(kNhwc, kNchw, 1, 2); + + auto a1 = std::make_shared(kNhwc, kNchw, 1, 2); std::vector kernelvec; kernelvec.reserve(3 * 3 * 3 * 3); for (int i = 0; i < 81; ++i) { @@ -359,12 +350,13 @@ TEST(bfs, check_end_to_end) { } Shape sh2({3, 3, 3, 3}); Tensor kernel = make_tensor(kernelvec, sh2); - ConvolutionalLayer a2(1, 0, 1, kernel); + auto a2 = std::make_shared(1, 0, 1, kernel); Shape poolshape = {2, 2}; - EWLayer a3("linear", 2.0F, 3.0F); - PoolingLayer a4(poolshape, "average"); - FCLayer a6; - OutputLayer a5; + auto a3 = std::make_shared("linear", 2.0F, 3.0F); + auto a4 = std::make_shared(poolshape, "average"); + auto a6 = std::make_shared(); + auto a5 = std::make_shared(); + graph.setInput(a1, input); graph.makeConnection(a1, a2); graph.makeConnection(a2, a3); @@ -373,41 +365,13 @@ TEST(bfs, check_end_to_end) { graph.setOutput(a5, output); graph.inference(); -#ifdef ENABLE_STATISTIC_WEIGHTS - std::vector weights = graph.getWEIGHTS(); - for (size_t i = 0; i < weights.size(); i++) { - switch (weights[i].get_type()) { - case Type::kInt: { - std::vector ten = *weights[i].as(); - for (size_t j = 0; j < ten.size(); j++) { - std::cout << ten[j] << ' '; - } - std::cout << '\n'; - break; - } - case Type::kFloat: { - std::vector ten = *weights[i].as(); - for (size_t j = 0; j < ten.size(); j++) { - std::cout << ten[j] << ' '; - } - std::cout << '\n'; - break; - } - case Type::kUnknown: - default: { - throw std::runtime_error("Unknown tensor type encountered"); - break; - } - } - } -#endif - std::vector tmp = *output.as(); ASSERT_GT(tmp.size(), 0); for (size_t i = 0; i < tmp.size(); ++i) { ASSERT_GE(tmp[i], 0); } } + TEST(bfs, check_struct_layer) { Graph graph(5); Shape sh1({1, 5, 5, 3}); @@ -418,16 +382,14 @@ TEST(bfs, check_struct_layer) { } Tensor input = make_tensor(vec, sh1); Tensor output = make_tensor(vec, sh1); - InputLayer a1(kNhwc, kNchw, 1, 2); + + auto a1 = std::make_shared(kNhwc, kNchw, 1, 2); 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); + auto a2 = std::make_shared(1, 0, 1, kernel); + auto a3 = std::make_shared(1, 0, 1, kernel); - // EWLayer a4("linear", 2.0F, 3.0F); - // a2.ewops.layers.push_back(&a4); - // a2.ewops.countlayers++; graph.setInput(a1, input); graph.makeConnection(a1, a2); graph.makeConnection(a2, a3); @@ -437,6 +399,7 @@ TEST(bfs, check_struct_layer) { 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}); @@ -447,16 +410,16 @@ TEST(bfs, check_struct_layer_added) { } Tensor input = make_tensor(vec, sh1); Tensor output = make_tensor(vec, sh1); - InputLayer a1(kNhwc, kNchw, 1, 2); + + auto a1 = std::make_shared(kNhwc, kNchw, 1, 2); 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.postops.layers.push_back(&a4); - a2.postops.count++; + auto a2 = std::make_shared(1, 0, 1, kernel); + auto a3 = std::make_shared(1, 0, 1, kernel); + auto a4 = std::make_shared("linear", 2.0F, 3.0F); + a2->postops.layers.push_back(a4.get()); + a2->postops.count++; graph.setInput(a1, input); graph.makeConnection(a1, a2); @@ -480,35 +443,28 @@ FLAKY_TEST(bfs, check_struct_graph_split) { } Tensor input = make_tensor(vec, sh1); Tensor output = make_tensor(vec, sh1); - InputLayer a1(kNhwc, kNchw, 1, 2); + + auto a1 = std::make_shared(kNhwc, kNchw, 1, 2); 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(1, 0, 1, kernel); - EWLayer a3_1_1("relu"); - ConvolutionalLayer a3_2(1, 0, 1, kernel); - EWLayer a3_2_1("relu"); - - ConcatLayer a4(0); - EWLayer a5("relu"); - - EWLayer a6_1("relu"); - EWLayer a6_2("relu"); - - ConcatLayer a7(0); - // EWLayer a8("relu"); - SplitLayer a8(1, 3); - - EWLayer a9_1("relu"); - EWLayer a9_2("relu"); - EWLayer a9_3("relu"); - - ConcatLayer a10(0); - EWLayer a11_1("relu"); - - ConcatLayer a12(0); + auto a2 = std::make_shared(1, 0, 1, kernel); + auto a3_1 = std::make_shared(1, 0, 1, kernel); + auto a3_1_1 = std::make_shared("relu"); + auto a3_2 = std::make_shared(1, 0, 1, kernel); + auto a3_2_1 = std::make_shared("relu"); + auto a4 = std::make_shared(0); + auto a5 = std::make_shared("relu"); + auto a6_1 = std::make_shared("relu"); + auto a6_2 = std::make_shared("relu"); + auto a7 = std::make_shared(0); + auto a8 = std::make_shared(1, 3); + auto a9_1 = std::make_shared("relu"); + auto a9_2 = std::make_shared("relu"); + auto a9_3 = std::make_shared("relu"); + auto a10 = std::make_shared(0); + auto a11_1 = std::make_shared("relu"); + auto a12 = std::make_shared(0); graph.setInput(a1, input); graph.makeConnection(a1, a2);