diff --git a/app/Accuracy/accuracy_check.cpp b/app/Accuracy/accuracy_check.cpp index 4a3c3333..934b4841 100644 --- a/app/Accuracy/accuracy_check.cpp +++ b/app/Accuracy/accuracy_check.cpp @@ -47,29 +47,23 @@ int main() { } Tensor input = t; Tensor output = make_tensor(vec, sh1); - auto a1 = std::make_unique(kNchw, kNchw, 1, 2); - Layer* a1_ptr = a1.get(); + 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); - auto a2 = std::make_unique(1, 0, 0, kernel); - Layer* a2_ptr = a2.get(); + auto a2 = std::make_shared(1, 0, 0, kernel); Shape poolshape = {2, 2}; - auto a3 = std::make_unique("linear", 2.0F, 3.0F); - Layer* a3_ptr = a3.get(); - auto a4 = std::make_unique(poolshape, "average"); - Layer* a4_ptr = a4.get(); - auto a5 = std::make_unique(); - Layer* a5_ptr = a5.get(); - auto a6 = std::make_unique(); - Layer* a6_ptr = a6.get(); - graph.setInput(a1_ptr, input); - graph.makeConnection(a1_ptr, a2_ptr); - graph.makeConnection(a2_ptr, a3_ptr); - graph.makeConnection(a3_ptr, a4_ptr); - graph.makeConnection(a4_ptr, a5_ptr); - graph.makeConnection(a5_ptr, a6_ptr); - graph.setOutput(a5_ptr, output); + 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); + graph.makeConnection(a3, a4); + graph.makeConnection(a4, a5); + graph.makeConnection(a5, a6); + graph.setOutput(a5, output); graph.inference(options); std::vector tmp = *output.as(); std::vector tmp_output = softmax(*output.as()); diff --git a/app/Graph/CMakeLists.txt b/app/Graph/CMakeLists.txt index 7ba68416..66612643 100644 --- a/app/Graph/CMakeLists.txt +++ b/app/Graph/CMakeLists.txt @@ -22,6 +22,11 @@ target_link_libraries(Graph_Build BuildGraph) add_executable(ACC acc_check.cpp) target_link_libraries(ACC BuildGraph) +add_executable(onnx_subgraphs onnx_subgraphs.cpp) +target_link_libraries(onnx_subgraphs BuildGraph) +target_link_libraries(onnx_subgraphs OpenMP::OpenMP_CXX) +target_link_libraries(onnx_subgraphs graphT_lib) + file(DOWNLOAD "https://raw.githubusercontent.com/DeepTrackAI/MNIST_dataset/main/mnist/test/1_000008.png" "${CMAKE_SOURCE_DIR}/docs/input/28/test1.png" diff --git a/app/Graph/build.cpp b/app/Graph/build.cpp index f6b33ef0..ebf27e48 100644 --- a/app/Graph/build.cpp +++ b/app/Graph/build.cpp @@ -18,23 +18,8 @@ void build_graph_linear(it_lab_ai::Graph& graph, it_lab_ai::Tensor& input, for (size_t i = 0; i < input.get_shape().dims(); i++) { std::cout << input.get_shape()[i] << ' '; } - std::cout << '\n'; - if (input.get_shape().dims() == 4) { - for (size_t n = 0; n < input.get_shape()[0]; n++) { - for (size_t h = 0; h < input.get_shape()[2]; h++) { - for (size_t w = 0; w < input.get_shape()[3]; w++) { - for (size_t c = 0; c < input.get_shape()[1]; c++) { - std::cout << input.get({n, c, h, w}) << ' '; - } - } - std::cerr << '\n'; - } - } - std::cout << '\n' << '\n'; - } } - std::vector> layers; - std::vector layer_ptrs; + std::vector> layers; std::vector layerpostop; std::string json_file = MODEL_PATH_H5; @@ -81,17 +66,15 @@ void build_graph_linear(it_lab_ai::Graph& graph, it_lab_ai::Tensor& input, 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_unique( + auto conv_layer = std::make_shared( 1, pads, 1, tmp_values, tmp_bias, 1, true); - layer_ptrs.push_back(conv_layer.get()); - layers.push_back(std::move(conv_layer)); + layers.push_back(conv_layer); layerpostop.push_back(false); if (comments) std::cout << "ConvLayer added to layers." << '\n'; } if (layer_type.find("relu") != std::string::npos) { auto ew_layer = LayerFactory::createEwLayer("relu", options); - layer_ptrs.push_back(ew_layer.get()); - layers.push_back(std::move(ew_layer)); + layers.push_back(ew_layer); layerpostop.push_back(true); if (comments) { std::cout << "Element wise (relu) added to layers" << '\n'; @@ -99,9 +82,8 @@ void build_graph_linear(it_lab_ai::Graph& graph, it_lab_ai::Tensor& input, } if (layer_type.find("Dense") != std::string::npos) { it_lab_ai::Tensor tmp_bias = it_lab_ai::make_tensor(tensor.get_bias()); - auto fc_layer = std::make_unique(tensor, tmp_bias); - layer_ptrs.push_back(fc_layer.get()); - layers.push_back(std::move(fc_layer)); + auto fc_layer = std::make_shared(tensor, tmp_bias); + layers.push_back(fc_layer); layerpostop.push_back(false); if (comments) std::cout << "DenseLayer added to layers." << '\n'; } @@ -119,26 +101,23 @@ void build_graph_linear(it_lab_ai::Graph& graph, it_lab_ai::Tensor& input, << '\n'; } auto pool_layer = - std::make_unique(shape, pooltype); - layer_ptrs.push_back(pool_layer.get()); - layers.push_back(std::move(pool_layer)); + std::make_shared(shape, pooltype); + layers.push_back(pool_layer); layerpostop.push_back(false); if (comments) std::cout << "PoolingLayer added to layers." << '\n'; } if (layer_type.find("Flatten") != std::string::npos) { - auto flatten_layer = std::make_unique( + auto flatten_layer = std::make_shared( std::vector({0, 3, 2, 1})); - layer_ptrs.push_back(flatten_layer.get()); - layers.push_back(std::move(flatten_layer)); + layers.push_back(flatten_layer); layerpostop.push_back(false); if (comments) std::cout << "FlattenLayer added to layers." << '\n'; } if (layer_type.find("Dropout") != std::string::npos) { - auto dropout_layer = std::make_unique(0.0); - layer_ptrs.push_back(dropout_layer.get()); - layers.push_back(std::move(dropout_layer)); + auto dropout_layer = std::make_shared(0.0); + layers.push_back(dropout_layer); layerpostop.push_back(false); if (comments) { std::cout @@ -148,39 +127,29 @@ void build_graph_linear(it_lab_ai::Graph& graph, it_lab_ai::Tensor& input, } } } - if (comments) { - std::cout << "number of layers - " << layers.size() + 1 << '\n'; - } - auto a1 = std::make_unique(it_lab_ai::kNchw, + if (comments) std::cout << "number of layers - " << layers.size() + 1 << '\n'; + auto a1 = std::make_shared(it_lab_ai::kNchw, it_lab_ai::kNchw); - Layer* a1_ptr = a1.get(); if (comments) std::cout << "InputLayer created." << '\n'; - graph.setInput(a1_ptr, input); + graph.setInput(a1, input); if (comments) std::cout << "Input set in graph." << '\n'; - graph.makeConnection(a1_ptr, layer_ptrs[0]); - if (comments) { + graph.makeConnection(a1, layers[0]); + if (comments) std::cout << "Connection made between InputLayer and first layer." << '\n'; - } for (size_t i = 0; i < layers.size() - 1; ++i) { if (layerpostop[i]) { - layer_ptrs[i - 1]->postops.layers.push_back(layer_ptrs[i]); - layer_ptrs[i - 1]->postops.count++; - graph.makeConnection(layer_ptrs[i - 1], layer_ptrs[i + 1]); - } else if (!layerpostop[i + 1]) { - graph.makeConnection(layer_ptrs[i], layer_ptrs[i + 1]); - } + layers[i - 1]->postops.layers.push_back(layers[i]); + layers[i - 1]->postops.count++; + graph.makeConnection(layers[i - 1], layers[i + 1]); + } else if (!layerpostop[i + 1]) + graph.makeConnection(layers[i], layers[i + 1]); } - graph.setOutput(layer_ptrs.back(), output); - - graph.addOwnedLayer(std::move(a1)); - for (auto& layer : layers) { - graph.addOwnedLayer(std::move(layer)); - } + graph.setOutput(layers.back(), output); } namespace { @@ -198,7 +167,7 @@ void build_graph(it_lab_ai::Graph& graph, it_lab_ai::Tensor& input, auto parse_result = parse_json_model(options, json_path, comments); auto& layers = parse_result.layers; - auto& name_to_layer_ptr = parse_result.name_to_layer_ptr; + auto& name_to_layer = parse_result.name_to_layer; auto& connections = parse_result.connections; auto& concat_connections = parse_result.concat_connections; auto& concat_orders = parse_result.concat_orders; @@ -211,7 +180,7 @@ void build_graph(it_lab_ai::Graph& graph, it_lab_ai::Tensor& input, [](const auto& layer) { return layer->getName() == it_lab_ai::kInput; }); if (input_layer_it != layers.end()) { - graph.setInput(input_layer_it->get(), input); + graph.setInput(*input_layer_it, input); } std::vector> connection_list; @@ -225,24 +194,23 @@ void build_graph(it_lab_ai::Graph& graph, it_lab_ai::Tensor& input, } try { - std::sort(connection_list.begin(), connection_list.end(), - [&](const auto& a, const auto& b) { - if (!name_to_layer_ptr.count(a.first) || - !name_to_layer_ptr.count(b.first)) { - return false; - } - return name_to_layer_ptr[a.first]->getID() < - name_to_layer_ptr[b.first]->getID(); - }); + std::sort( + connection_list.begin(), connection_list.end(), + [&](const auto& a, const auto& b) { + if (!name_to_layer.count(a.first) || !name_to_layer.count(b.first)) { + return false; + } + return name_to_layer[a.first]->getID() < + name_to_layer[b.first]->getID(); + }); } catch (const std::exception& e) { std::cerr << "ERROR during sorting: " << e.what() << '\n'; } for (const auto& [source_name, target_name] : connection_list) { - if (name_to_layer_ptr.count(source_name) && - name_to_layer_ptr.count(target_name)) { + if (name_to_layer.count(source_name) && name_to_layer.count(target_name)) { if (target_name.find("Concat") != std::string::npos || - name_to_layer_ptr[target_name]->getName() == it_lab_ai::kConcat) { + name_to_layer[target_name]->getName() == it_lab_ai::kConcat) { if (concat_connections.find(target_name) != concat_connections.end()) { const auto& expected_inputs = concat_connections[target_name]; auto it = std::find(expected_inputs.begin(), expected_inputs.end(), @@ -256,8 +224,9 @@ void build_graph(it_lab_ai::Graph& graph, it_lab_ai::Tensor& input, if (concat_connected_inputs[target_name].size() == concat_connections[target_name].size()) { - auto* concat_layer = dynamic_cast( - name_to_layer_ptr[target_name]); + auto concat_layer = + std::dynamic_pointer_cast( + name_to_layer[target_name]); if (concat_layer) { concat_layer->setInputOrder(concat_orders[target_name]); } @@ -267,8 +236,9 @@ void build_graph(it_lab_ai::Graph& graph, it_lab_ai::Tensor& input, } try { - graph.makeConnection(name_to_layer_ptr[source_name], - name_to_layer_ptr[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 << " : " << e.what() << '\n'; @@ -278,7 +248,7 @@ void build_graph(it_lab_ai::Graph& graph, it_lab_ai::Tensor& input, for (auto& split_dist : split_distribution) { for (auto& connection : split_dist) { - for (const auto& [name, layer] : name_to_layer_ptr) { + for (const auto& [name, layer] : name_to_layer) { if (original_ids[name] == connection.first) { connection.first = layer->getID(); break; @@ -286,17 +256,9 @@ void build_graph(it_lab_ai::Graph& graph, it_lab_ai::Tensor& input, } } } - graph.setSplitDistribution(split_distribution); - - if (!layers.empty()) { - auto* output_layer = layers.back().get(); - graph.setOutput(output_layer, output); - } - - for (auto& layer : layers) { - graph.addOwnedLayer(std::move(layer)); - } + auto output_layer = layers.back(); + graph.setOutput(output_layer, output); } ParseResult parse_json_model(RuntimeOptions options, @@ -304,7 +266,7 @@ ParseResult parse_json_model(RuntimeOptions options, ParseResult result; auto& layers = result.layers; - auto& name_to_layer_ptr = result.name_to_layer_ptr; + auto& name_to_layer = result.name_to_layer; auto& connections = result.connections; auto& concat_connections = result.concat_connections; auto& concat_connected_inputs = result.concat_connected_inputs; @@ -333,14 +295,12 @@ ParseResult parse_json_model(RuntimeOptions options, if (comments) std::cout << "Loaded model data from JSON." << '\n'; - int current_id = 0; - - auto input_layer = std::make_unique(it_lab_ai::kNchw, + auto input_layer = std::make_shared(it_lab_ai::kNchw, it_lab_ai::kNchw); + layers.push_back(input_layer); + name_to_layer[input_layer_name] = input_layer; + int current_id = 0; input_layer->setID(current_id++); - name_to_layer_ptr[input_layer_name] = input_layer.get(); - layers.push_back(std::move(input_layer)); - original_ids[input_layer_name] = 0; for (const auto& layer_data : model_data) { try { @@ -354,7 +314,7 @@ ParseResult parse_json_model(RuntimeOptions options, << " (" << layer_type << ")" << '\n'; } - std::unique_ptr layer; + std::shared_ptr layer; if (layer_type.find("Conv") != std::string::npos) { it_lab_ai::Tensor tensor = it_lab_ai::create_tensor_from_json( @@ -408,11 +368,12 @@ ParseResult parse_json_model(RuntimeOptions options, } it_lab_ai::Tensor tmp_tensor = tensor; + it_lab_ai::Tensor tmp_bias = it_lab_ai::make_tensor(tensor.get_bias()); - auto conv_layer = std::make_unique( + auto conv_layer = std::make_shared( stride, pads, dilations, tmp_tensor, tmp_bias, group); - layer = std::move(conv_layer); + layer = conv_layer; } else if (layer_type.find("Relu") != std::string::npos || layer_type.find("relu") != std::string::npos) { layer = LayerFactory::createEwLayer("relu", options); @@ -435,11 +396,11 @@ ParseResult parse_json_model(RuntimeOptions options, it_lab_ai::Tensor tmp_bias = it_lab_ai::make_tensor(tensor.get_bias()); auto fc_layer = - std::make_unique(tmp_tensor, tmp_bias); - layer = std::move(fc_layer); + std::make_shared(tmp_tensor, tmp_bias); + layer = fc_layer; } else if (layer_type.find("Dropout") != std::string::npos) { - auto dropout_layer = std::make_unique(0.0); - layer = std::move(dropout_layer); + auto dropout_layer = std::make_shared(0.0); + layer = dropout_layer; if (comments) { std::cout << "DropOutLayer added to layers with probability 0.4 (turned " @@ -447,9 +408,9 @@ ParseResult parse_json_model(RuntimeOptions options, << '\n'; } } else if (layer_type == "GlobalAveragePool") { - auto pool_layer = std::make_unique( + auto pool_layer = std::make_shared( it_lab_ai::Shape({0, 0}), "average"); - layer = std::move(pool_layer); + layer = pool_layer; if (comments) { std::cout << "GlobalAveragePool layer added (will use input spatial " "dimensions as kernel)" @@ -510,7 +471,7 @@ ParseResult parse_json_model(RuntimeOptions options, } auto pool_layer = - std::make_unique(shape, pooltype); + std::make_shared(shape, pooltype); try { if (strides[0] != 2 || strides[1] != 2) { @@ -533,7 +494,7 @@ ParseResult parse_json_model(RuntimeOptions options, << e.what() << '\n'; } } - layer = std::move(pool_layer); + layer = pool_layer; } else if (layer_type.find("Flatten") != std::string::npos) { int axis = 1; @@ -543,8 +504,8 @@ ParseResult parse_json_model(RuntimeOptions options, axis = attributes["axis"].get(); } } - auto flatten_layer = std::make_unique(axis); - layer = std::move(flatten_layer); + auto flatten_layer = std::make_shared(axis); + layer = flatten_layer; } else if (layer_type == "Concat") { int axis = 0; if (layer_data["attributes"].contains("axis")) { @@ -557,8 +518,8 @@ ParseResult parse_json_model(RuntimeOptions options, concat_connections[layer_name].push_back(base_input_name); } } - auto concat_layer = std::make_unique(axis); - layer = std::move(concat_layer); + auto concat_layer = std::make_shared(axis); + layer = concat_layer; concat_connected_inputs[layer_name] = std::unordered_set(); } else if (layer_type == "Split") { int axis = 0; @@ -589,8 +550,13 @@ ParseResult parse_json_model(RuntimeOptions options, } auto split_layer = - std::make_unique(axis, splits); - layer = std::move(split_layer); + std::make_shared(axis, splits); + layer = split_layer; + + split_layers[layer_name] = split_layer; + split_name_to_index[layer_name] = + static_cast(split_distribution.size()); + split_distribution.emplace_back(); } else if (layer_type == "Add" || layer_type == "Mul" || layer_type == "Sub" || layer_type == "Div") { bool has_scalar_constant = false; @@ -671,8 +637,8 @@ ParseResult parse_json_model(RuntimeOptions options, continue; } - auto bin_layer = std::make_unique(op); - layer = std::move(bin_layer); + auto bin_layer = std::make_shared(op); + layer = bin_layer; } } else if (layer_type == "Gemm") { it_lab_ai::Tensor tensor = it_lab_ai::create_tensor_from_json( @@ -734,8 +700,8 @@ ParseResult parse_json_model(RuntimeOptions options, } auto fc_layer = - std::make_unique(tmp_tensor, tmp_bias); - layer = std::move(fc_layer); + std::make_shared(tmp_tensor, tmp_bias); + layer = fc_layer; } else if (layer_type == "Transpose" || layer_type.find("transpose") != std::string::npos) { std::vector perm; @@ -750,8 +716,8 @@ ParseResult parse_json_model(RuntimeOptions options, } auto transpose_layer = - std::make_unique(perm); - layer = std::move(transpose_layer); + std::make_shared(perm); + layer = transpose_layer; if (comments) { std::cout << "TransposeLayer added with perm: ["; @@ -795,8 +761,8 @@ ParseResult parse_json_model(RuntimeOptions options, } auto reshape_layer = - std::make_unique(allowzero, shape); - layer = std::move(reshape_layer); + std::make_shared(allowzero, shape); + layer = reshape_layer; } else if (layer_type == "ReduceMean") { std::vector axes; @@ -814,9 +780,9 @@ ParseResult parse_json_model(RuntimeOptions options, keepdims = attributes["keepdims"].get(); } } - auto reduce_layer = std::make_unique( + auto reduce_layer = std::make_shared( it_lab_ai::ReduceLayer::Operation::kMean, keepdims, axes); - layer = std::move(reduce_layer); + layer = reduce_layer; } else if (layer_type == "ReduceSum") { int64_t keepdims = 0; if (layer_data.contains("attributes")) { @@ -841,9 +807,9 @@ ParseResult parse_json_model(RuntimeOptions options, } } } - auto reduce_layer = std::make_unique( + auto reduce_layer = std::make_shared( it_lab_ai::ReduceLayer::Operation::kSum, keepdims, axes); - layer = std::move(reduce_layer); + layer = reduce_layer; } else if (layer_type == "Constant") { if (layer_data.contains("attributes")) { const auto& attributes = layer_data["attributes"]; @@ -865,8 +831,8 @@ ParseResult parse_json_model(RuntimeOptions options, continue; } else if (layer_type == "MatMul") { - auto matmul_layer = std::make_unique(); - layer = std::move(matmul_layer); + auto matmul_layer = std::make_shared(); + layer = matmul_layer; } else if (layer_type == "Softmax") { int axis = -1; @@ -877,8 +843,8 @@ ParseResult parse_json_model(RuntimeOptions options, axis = attributes["axis"].get(); } } - auto softmax_layer = std::make_unique(axis); - layer = std::move(softmax_layer); + auto softmax_layer = std::make_shared(axis); + layer = softmax_layer; } else if (layer_type == "BatchNormalization") { float epsilon = 1e-5F; @@ -942,34 +908,18 @@ ParseResult parse_json_model(RuntimeOptions options, it_lab_ai::Tensor var = it_lab_ai::make_tensor(var_data, it_lab_ai::Shape({num_channels})); - auto bn_layer = std::make_unique( + auto bn_layer = std::make_shared( scale, bias, mean, var, epsilon, momentum, training_mode); - layer = std::move(bn_layer); + layer = bn_layer; } else { continue; } - if (layer) { int original_id = current_id; layer->setID(current_id++); - - Layer* layer_ptr = layer.get(); - name_to_layer_ptr[layer_name] = layer_ptr; + layers.push_back(layer); + name_to_layer[layer_name] = layer; original_ids[layer_name] = original_id; - - layers.push_back(std::move(layer)); - - if (layer_type == "Split") { - auto* split_ptr = dynamic_cast(layer_ptr); - if (split_ptr) { - split_layers[layer_name] = - std::make_unique(*split_ptr); - split_name_to_index[layer_name] = - static_cast(split_distribution.size()); - split_distribution.emplace_back(); - } - } - if (layer_data.contains("inputs")) { for (const auto& input_name : layer_data["inputs"]) { std::string input_tensor = input_name.get(); @@ -983,7 +933,7 @@ ParseResult parse_json_model(RuntimeOptions options, int output_index = std::stoi(matches[2].str()); if (split_layers.find(split_layer_name) != split_layers.end()) { - int target_layer_id = layer_ptr->getID(); + int target_layer_id = layer->getID(); int split_index = split_name_to_index[split_layer_name]; @@ -1001,7 +951,6 @@ ParseResult parse_json_model(RuntimeOptions options, split_distribution[split_index].emplace_back(target_layer_id, output_index); } - bool connection_in_list = false; for (const auto& existing_target : connections[split_layer_name]) { diff --git a/app/Graph/build.hpp b/app/Graph/build.hpp index bbba3318..e9aae0e2 100644 --- a/app/Graph/build.hpp +++ b/app/Graph/build.hpp @@ -37,14 +37,15 @@ extern std::unordered_map model_paths; struct ParseResult { - std::vector> layers; - std::unordered_map name_to_layer_ptr; + std::vector> layers; + std::unordered_map> + name_to_layer; std::unordered_map> connections; std::unordered_map> concat_connections; std::unordered_map> concat_orders; std::unordered_map> concat_connected_inputs; - std::unordered_map> + std::unordered_map> split_layers; std::unordered_map split_name_to_index; std::vector>> split_distribution; @@ -75,15 +76,15 @@ void print_time_stats(it_lab_ai::Graph& graph); namespace it_lab_ai { class LayerFactory { public: - static std::unique_ptr createEwLayer(const std::string& function, + static std::shared_ptr createEwLayer(const std::string& function, const RuntimeOptions& options, float alpha = 1.0F, float beta = 0.0F) { if (options.backend == Backend::kOneDnn && EwLayerOneDnn::is_function_supported(function)) { - return std::make_unique(function, alpha, beta); + return std::make_shared(function, alpha, beta); } - return std::make_unique(function, alpha, beta); + return std::make_shared(function, alpha, beta); } }; diff --git a/app/Graph/onnx_subgraphs.cpp b/app/Graph/onnx_subgraphs.cpp new file mode 100644 index 00000000..1647206f --- /dev/null +++ b/app/Graph/onnx_subgraphs.cpp @@ -0,0 +1,91 @@ +#include +#include +#include +#include +#include +#include + +#include "build.hpp" +#include "graph_transformations/graph_transformations.hpp" +#include "perf/benchmarking.hpp" + +using namespace it_lab_ai; + +int main() { + int type = 2; + Tensor input = make_tensor(std::vector({0})); + RuntimeOptions options; + if (type == 0) { + Graph graph1; + build_graph(graph1, input, input, MODEL_PATH_DENSENET_ONNX, options, false); + + Graph subgraph; + Tensor scale = make_tensor(std::vector({1.0})); + std::shared_ptr layer_0 = + std::make_shared(scale, scale, scale, scale); + std::shared_ptr layer_1 = std::make_shared("relu"); + std::shared_ptr layer_2 = std::make_shared(); + std::shared_ptr layer_3 = std::make_shared("relu"); + std::shared_ptr layer_4 = std::make_shared(); + subgraph.setInput(layer_0, input); + subgraph.makeConnection(layer_0, layer_1); + subgraph.makeConnection(layer_1, layer_2); + subgraph.makeConnection(layer_2, layer_3); + subgraph.makeConnection(layer_3, layer_4); + + Graph subgraph2; + std::shared_ptr layer_5 = std::make_shared(); + std::shared_ptr layer_6 = + std::make_shared(Shape({1, 1, 1}), "max"); + std::shared_ptr layer_7 = std::make_shared(); + subgraph2.setInput(layer_6, input); + subgraph2.makeConnection(layer_6, layer_5); + subgraph2.addSingleLayer(layer_7); + subgraph2.makeConnection(layer_7, layer_5); + + auto vec = find_subgraphs(graph1, subgraph); + auto vec2 = find_subgraphs(graph1, subgraph2); + } else if (type == 1) { + Graph graph1; + build_graph(graph1, input, input, MODEL_PATH_RESNET_ONNX, options, false); + + Graph subgraph; + std::shared_ptr layer_0 = std::make_shared(); + std::shared_ptr layer_1 = std::make_shared(); + std::shared_ptr layer_2 = std::make_shared(); + std::shared_ptr layer_3 = std::make_shared(); + std::shared_ptr layer_4 = std::make_shared(); + subgraph.setInput(layer_0, input); + subgraph.makeConnection(layer_0, layer_1); + subgraph.makeConnection(layer_1, layer_2); + subgraph.makeConnection(layer_2, layer_3); + subgraph.makeConnection(layer_3, layer_4); + + auto vec = find_subgraphs(graph1, subgraph); + } else if (type == 2) { + Graph graph1; + build_graph(graph1, input, input, MODEL_PATH_GOOGLENET_ONNX, options, + false); + + Graph subgraph; + Shape shape(2); + std::shared_ptr layer_0 = std::make_shared(); + std::shared_ptr layer_1 = std::make_shared(); + std::shared_ptr layer_2 = std::make_shared("relu"); + std::shared_ptr layer_3 = std::make_shared("relu"); + std::shared_ptr layer_4 = std::make_shared(); + std::shared_ptr layer_5 = std::make_shared(); + std::shared_ptr layer_6 = + std::make_shared(shape, "max"); + subgraph.setInput(layer_0, input); + subgraph.makeConnection(layer_0, layer_1); + subgraph.makeConnection(layer_0, layer_4); + subgraph.makeConnection(layer_0, layer_5); + subgraph.makeConnection(layer_0, layer_6); + subgraph.makeConnection(layer_4, layer_2); + subgraph.makeConnection(layer_5, layer_3); + + auto vec = find_subgraphs(graph1, subgraph); + } + return 0; +} diff --git a/include/graph/graph.hpp b/include/graph/graph.hpp index 07d86c0e..cc60bd06 100644 --- a/include/graph/graph.hpp +++ b/include/graph/graph.hpp @@ -26,8 +26,7 @@ struct BranchState { class Graph { int BiggestSize_; int V_; // amount of ids - std::vector> owned_layers_; - std::vector layers_; + std::vector> layers_; std::vector arrayV_; // vertices (id -> vertex number) std::vector arrayE_; // edges (vertex number -> id) std::vector inten_; @@ -78,25 +77,6 @@ class Graph { split_distribution_ = std::move(split_dist); } - void addOwnedLayer(std::unique_ptr layer) { - if (!layer) return; - - for (const auto& existing_layer : owned_layers_) { - if (existing_layer.get() == layer.get()) { - return; - } - } - - owned_layers_.push_back(std::move(layer)); - } - - Layer* addLayer(std::unique_ptr layer) { - if (!layer) return nullptr; - Layer* raw_ptr = layer.get(); - addOwnedLayer(std::move(layer)); - return raw_ptr; - } - [[nodiscard]] int getVertexValue(size_t layerID) const { if (layerID >= arrayV_.size()) { throw std::invalid_argument("ArrayV does not contain this ID."); @@ -118,22 +98,28 @@ class Graph { return in_edges_[layerID].size(); } + [[nodiscard]] std::vector getInLayers(size_t layerID) const { + if (layerID >= in_edges_.size()) { + throw std::invalid_argument("Input edges array do not contain this ID."); + } + return in_edges_[layerID]; + } + [[nodiscard]] int getLayersCount() const { return V_; } - [[nodiscard]] const Layer& getLayerFromID(size_t layerID) const { + [[nodiscard]] std::shared_ptr getLayerFromID(size_t layerID) const { if (layerID >= layers_.size()) { throw std::invalid_argument("Layers do not contain this ID."); } - return *layers_[layerID]; + return layers_[layerID]; } - void setInput(Layer* layer, Tensor& vec) { + void setInput(const std::shared_ptr& layer, Tensor& vec) { if (!layer) { throw std::invalid_argument("Layer cannot be null"); } - bool layer_exists = false; - for (const auto* existing_layer : layers_) { + for (std::shared_ptr& existing_layer : layers_) { if (existing_layer == layer) { layer_exists = true; break; @@ -142,7 +128,7 @@ class Graph { if (!layer_exists) { layer->setID(V_); - layers_.push_back(layer); + layers_.emplace_back(layer); arrayV_.push_back(static_cast(arrayE_.size())); if (V_ >= static_cast(in_edges_.size())) { @@ -156,11 +142,10 @@ class Graph { start_ = layer->getID(); } - void addSingleLayer(Layer* layer) { + void addSingleLayer(const std::shared_ptr& layer) { if (!layer) return; - bool layer_exists = false; - for (const auto* existing_layer : layers_) { + for (const std::shared_ptr& existing_layer : layers_) { if (existing_layer == layer) { layer_exists = true; break; @@ -180,7 +165,8 @@ class Graph { } } - void makeConnection(Layer* layPrev, Layer* layNext) { + void makeConnection(const std::shared_ptr& layPrev, + const std::shared_ptr& layNext) { if (!layPrev || !layNext) { throw std::invalid_argument("Layers cannot be null"); } @@ -213,9 +199,79 @@ class Graph { in_edges_[layNext->getID()].push_back(layPrev->getID()); } - bool areLayerNext(Layer* layPrev, Layer* layNext) { + void removeConnection(int idPrev, int idNext) { + if (idPrev >= V_ || idNext >= V_ || idPrev < 0 || idNext < 0) { + throw std::out_of_range("Layer ID out of range"); + } + auto it = + std::find(in_edges_[idNext].begin(), in_edges_[idNext].end(), idPrev); + if (it == in_edges_[idNext].end()) { + throw std::invalid_argument( + (std::string("No such edge ") + std::to_string(idPrev)) + " " + + std::to_string(idNext)); + } + in_edges_[idNext].erase(it); + auto array_e_it = std::find(arrayE_.begin() + arrayV_[idPrev], + arrayE_.begin() + arrayV_[idPrev + 1], idNext); + if (array_e_it == arrayE_.begin() + arrayV_[idPrev + 1]) { + throw std::invalid_argument( + (std::string("No such edge ") + std::to_string(idPrev)) + " " + + std::to_string(idNext)); + } + arrayE_.erase(array_e_it); + for (size_t i = static_cast(idPrev) + 1; i < arrayV_.size(); ++i) { + arrayV_[i]--; + } + } + void removeSingleLayer(int id) { + if (id >= V_ || id < 0) { + throw std::out_of_range("Layer ID out of range"); + } + // remove inputs + for (int i = 0; i < V_; i++) { + if (arrayV_[i] != arrayV_[i + 1]) { + auto array_e_it = std::find(arrayE_.begin() + arrayV_[i], + arrayE_.begin() + arrayV_[i + 1], id); + if (array_e_it != arrayE_.begin() + arrayV_[i + 1]) { + removeConnection(i, id); + } + } + } + // remove outputs + int amount_connected = arrayV_[id + 1] - arrayV_[id]; + for (int i = 0; i < amount_connected; i++) { + removeConnection(id, arrayE_[arrayV_[id] + i]); + } + // remove vertex + in_edges_.erase(in_edges_.begin() + id); + arrayV_.erase(arrayV_.begin() + id); + for (int& i : arrayE_) { + if (i > id) { + i -= 1; + } + } + for (std::vector& i : in_edges_) { + for (int& j : i) { + if (j > id) { + j--; + } + } + } + for (size_t i = id + 1; i < layers_.size(); i++) { + layers_[i]->setID(layers_[i]->getID() - 1); + } + layers_[id]->setID(-1); + layers_.erase(layers_.begin() + id); + V_--; + } + bool areLayerNext(const std::shared_ptr& layPrev, + const std::shared_ptr& layNext) { if (!layPrev || !layNext) return false; + if (layPrev->getID() >= V_ || layPrev->getID() < 0) { + throw std::invalid_argument("No such layer in graph"); + } + for (int i = arrayV_[layPrev->getID()]; i < arrayV_[layPrev->getID() + 1]; i++) { if (arrayE_[i] == layNext->getID()) { @@ -336,13 +392,12 @@ class Graph { } } - void setOutput(Layer* layer, Tensor& vec) { + void setOutput(const std::shared_ptr& layer, Tensor& vec) { if (!layer) { throw std::invalid_argument("Layer cannot be null"); } end_ = layer->getID(); outtenres_ = &vec; - if (outten_.empty()) { std::vector vec1 = {1, 7, 1, 0}; Tensor start = make_tensor(vec1); diff --git a/include/layers/ConvLayer.hpp b/include/layers/ConvLayer.hpp index b7fb56c0..720f68f3 100644 --- a/include/layers/ConvLayer.hpp +++ b/include/layers/ConvLayer.hpp @@ -132,7 +132,7 @@ class ConvImpl : public LayerImpl { } auto kercol = static_cast(kercol_index); color += - matrix[(i + coloms + str) * input_flow_ + x] * + matrix.at((i + coloms + str) * input_flow_ + x) * kernel[kercol * kernel_size + static_cast(str + 1)]; } } diff --git a/include/layers/Layer.hpp b/include/layers/Layer.hpp index 00ddeb7c..0139912f 100644 --- a/include/layers/Layer.hpp +++ b/include/layers/Layer.hpp @@ -43,7 +43,7 @@ using ParBackend = parallel::Backend; class Layer; struct PostOperations { - std::vector layers; + std::vector> layers; unsigned int count = 0; }; diff --git a/src/graph_transformations/graph_transformations.cpp b/src/graph_transformations/graph_transformations.cpp index cb1b1c04..93122a6d 100644 --- a/src/graph_transformations/graph_transformations.cpp +++ b/src/graph_transformations/graph_transformations.cpp @@ -4,8 +4,9 @@ namespace it_lab_ai { namespace { -bool layer_conditions(const Layer& layer, const Layer& layer_sub) { - return layer.getName() == layer_sub.getName(); +bool layer_conditions(const std::shared_ptr& layer, + const std::shared_ptr& layer_sub) { + return layer->getName() == layer_sub->getName(); } } // namespace @@ -44,13 +45,12 @@ bool run_search(const Graph& graph, const Graph& subgraph, std::vector& assignments, std::vector>& results) { size_t cur_size = assignments.size(); - for (int prev_id = 0; prev_id < subgraph.getLayersCount(); prev_id++) { + for (int prev_id = 0; prev_id < static_cast(cur_size); prev_id++) { int amount_connected_s = subgraph.getVertexValue(prev_id + 1) - subgraph.getVertexValue(prev_id); for (int j = 0; j < amount_connected_s; j++) { int next_id = subgraph.getEdgeValue(subgraph.getVertexValue(prev_id) + j); - if (prev_id < static_cast(cur_size) && - next_id < static_cast(cur_size)) { + if (next_id < static_cast(cur_size)) { if (!has_edge(graph, assignments[prev_id], assignments[next_id])) { return false; } diff --git a/test/graph/test_graph.cpp b/test/graph/test_graph.cpp index 4057a318..d4608409 100644 --- a/test/graph/test_graph.cpp +++ b/test/graph/test_graph.cpp @@ -22,76 +22,175 @@ TEST(graph, check_connection) { Tensor output; Graph graph; - auto fcLayer = std::make_unique(weights, bias); - auto inputLayer = std::make_unique(); - auto ewLayer = std::make_unique(); + auto fcLayer = std::make_shared(weights, bias); + auto inputLayer = std::make_shared(); + auto ewLayer = std::make_shared(); - Layer* fcLayer_ptr = fcLayer.get(); - Layer* inputLayer_ptr = inputLayer.get(); - Layer* ewLayer_ptr = ewLayer.get(); + graph.setInput(inputLayer, input); + graph.makeConnection(inputLayer, fcLayer); + graph.makeConnection(fcLayer, ewLayer); - graph.setInput(inputLayer_ptr, input); - graph.makeConnection(inputLayer_ptr, fcLayer_ptr); - graph.makeConnection(fcLayer_ptr, ewLayer_ptr); + ASSERT_EQ(graph.areLayerNext(inputLayer, fcLayer), 1); +} - ASSERT_EQ(graph.areLayerNext(inputLayer_ptr, fcLayer_ptr), 1); +TEST(graph, check_connection_remove) { + const std::vector vec1 = {2.0F, 1.5F, 0.1F, 1.9F, 0.0F, 5.5F}; + Tensor weights = make_tensor(vec1, {3, 2}); + Tensor bias = make_tensor({0.5F, 0.5F, 1.0F}); + Tensor input = make_tensor({1.0F, 2.0F}, {2}); + Tensor output; + Graph graph; + 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); + graph.makeConnection(fcLayer, ewLayer); + graph.removeConnection(fcLayer->getID(), ewLayer->getID()); + graph.removeConnection(inputLayer->getID(), fcLayer->getID()); + + ASSERT_EQ(graph.areLayerNext(fcLayer, ewLayer), 0); + ASSERT_EQ(graph.areLayerNext(inputLayer, fcLayer), 0); } -TEST(graph, check_connection1) { +TEST(graph, check_connection_remove_out_of_range) { const std::vector vec1 = {2.0F, 1.5F, 0.1F, 1.9F, 0.0F, 5.5F}; Tensor weights = make_tensor(vec1, {3, 2}); Tensor bias = make_tensor({0.5F, 0.5F, 1.0F}); Tensor input = make_tensor({1.0F, 2.0F}, {2}); Tensor output; Graph graph; + 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); + graph.makeConnection(fcLayer, ewLayer); + ASSERT_ANY_THROW(graph.removeConnection(999, -1)); +} + +TEST(graph, check_connection_remove_no_edge) { + const std::vector vec1 = {2.0F, 1.5F, 0.1F, 1.9F, 0.0F, 5.5F}; + Tensor weights = make_tensor(vec1, {3, 2}); + Tensor bias = make_tensor({0.5F, 0.5F, 1.0F}); + Tensor input = make_tensor({1.0F, 2.0F}, {2}); + Tensor output; + Graph graph; + 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); + graph.makeConnection(fcLayer, ewLayer); + ASSERT_ANY_THROW(graph.removeConnection(0, 2)); +} - auto fcLayer = std::make_unique(weights, bias); - auto inputLayer = std::make_unique(); - auto ewLayer = std::make_unique(); - auto fcLayer2 = std::make_unique(weights, bias); +TEST(graph, check_connection_double_remove_throw) { + const std::vector vec1 = {2.0F, 1.5F, 0.1F, 1.9F, 0.0F, 5.5F}; + Tensor weights = make_tensor(vec1, {3, 2}); + Tensor bias = make_tensor({0.5F, 0.5F, 1.0F}); + Tensor input = make_tensor({1.0F, 2.0F}, {2}); + Tensor output; + Graph graph; + 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); + graph.makeConnection(fcLayer, ewLayer); + graph.removeConnection(fcLayer->getID(), ewLayer->getID()); + ASSERT_ANY_THROW(graph.removeConnection(fcLayer->getID(), ewLayer->getID())); +} - Layer* fcLayer_ptr = fcLayer.get(); - Layer* inputLayer_ptr = inputLayer.get(); - Layer* ewLayer_ptr = ewLayer.get(); - Layer* fcLayer2_ptr = fcLayer2.get(); +TEST(graph, check_layer_remove) { + const std::vector vec1 = {2.0F, 1.5F, 0.1F, 1.9F, 0.0F, 5.5F}; + Tensor weights = make_tensor(vec1, {3, 2}); + Tensor bias = make_tensor({0.5F, 0.5F, 1.0F}); + Tensor input = make_tensor({1.0F, 2.0F}, {2}); + Tensor output; + Graph graph; + auto fcLayer = std::make_shared(weights, bias); + auto inputLayer = std::make_shared(); + auto ewLayer = std::make_shared(); - graph.setInput(inputLayer_ptr, input); - graph.makeConnection(inputLayer_ptr, fcLayer_ptr); - graph.makeConnection(fcLayer_ptr, ewLayer_ptr); - graph.makeConnection(fcLayer_ptr, fcLayer2_ptr); - graph.setOutput(fcLayer2_ptr, output); + graph.setInput(inputLayer, input); + graph.makeConnection(inputLayer, fcLayer); + graph.makeConnection(fcLayer, ewLayer); + graph.removeSingleLayer(fcLayer->getID()); - ASSERT_EQ(graph.areLayerNext(fcLayer_ptr, fcLayer2_ptr), 1); + ASSERT_EQ(graph.areLayerNext(inputLayer, fcLayer), 0); + ASSERT_ANY_THROW(graph.areLayerNext(fcLayer, ewLayer)); } -TEST(graph, check_connection_when_not_connection) { +TEST(graph, check_layer_remove_out_of_range) { const std::vector vec1 = {2.0F, 1.5F, 0.1F, 1.9F, 0.0F, 5.5F}; Tensor weights = make_tensor(vec1, {3, 2}); Tensor bias = make_tensor({0.5F, 0.5F, 1.0F}); Tensor input = make_tensor({1.0F, 2.0F}, {2}); Tensor output; Graph graph; + 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); + graph.makeConnection(fcLayer, ewLayer); + ASSERT_ANY_THROW(graph.removeSingleLayer(999)); + ASSERT_ANY_THROW(graph.removeSingleLayer(-1)); +} - auto fcLayer = std::make_unique(weights, bias); - auto inputLayer = std::make_unique(); - auto ewLayer = std::make_unique(); - auto fcLayer2 = std::make_unique(weights, bias); +TEST(graph, check_connection1) { + const std::vector vec1 = {2.0F, 1.5F, 0.1F, 1.9F, 0.0F, 5.5F}; + Tensor weights = make_tensor(vec1, {3, 2}); + Tensor bias = make_tensor({0.5F, 0.5F, 1.0F}); + Tensor input = make_tensor({1.0F, 2.0F}, {2}); + Tensor output; - Layer* fcLayer_ptr = fcLayer.get(); - Layer* inputLayer_ptr = inputLayer.get(); - Layer* ewLayer_ptr = ewLayer.get(); - Layer* fcLayer2_ptr = fcLayer2.get(); + Graph graph; + + 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); + graph.makeConnection(fcLayer, ewLayer); + graph.makeConnection(fcLayer, fcLayer2); + graph.setOutput(fcLayer2, output); + + ASSERT_EQ(graph.areLayerNext(fcLayer, fcLayer2), 1); +} - graph.setInput(inputLayer_ptr, input); - graph.makeConnection(inputLayer_ptr, fcLayer_ptr); - graph.makeConnection(fcLayer_ptr, fcLayer2_ptr); - graph.setOutput(fcLayer2_ptr, output); +TEST(graph, check_connection_when_not_connection) { + const std::vector vec1 = {2.0F, 1.5F, 0.1F, 1.9F, 0.0F, 5.5F}; + Tensor weights = make_tensor(vec1, {3, 2}); + Tensor bias = make_tensor({0.5F, 0.5F, 1.0F}); + Tensor input = make_tensor({1.0F, 2.0F}, {2}); + Tensor output; + + Graph graph; - ASSERT_EQ(graph.areLayerNext(fcLayer_ptr, ewLayer_ptr), false); + 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.makeConnection(fcLayer_ptr, ewLayer_ptr); + graph.setInput(inputLayer, input); + graph.makeConnection(inputLayer, fcLayer); + graph.makeConnection(fcLayer, fcLayer2); + graph.setOutput(fcLayer2, output); - ASSERT_EQ(graph.areLayerNext(fcLayer_ptr, ewLayer_ptr), true); + ASSERT_EQ(graph.areLayerNext(fcLayer, ewLayer), false); + + graph.makeConnection(fcLayer, ewLayer); + + ASSERT_EQ(graph.areLayerNext(fcLayer, ewLayer), true); } TEST(graph, check_connection_when_not_connection1) { @@ -100,25 +199,21 @@ TEST(graph, check_connection_when_not_connection1) { Tensor bias = make_tensor({0.5F, 0.5F, 1.0F}); Tensor input = make_tensor({1.0F, 2.0F}, {2}); Tensor output; - Graph graph; - auto fcLayer = std::make_unique(weights, bias); - auto fcLayer2 = std::make_unique(weights, bias); - auto fcLayer3 = std::make_unique(weights, bias); - auto fcLayer4 = std::make_unique(weights, bias); + Graph graph; - Layer* fcLayer_ptr = fcLayer.get(); - Layer* fcLayer2_ptr = fcLayer2.get(); - Layer* fcLayer3_ptr = fcLayer3.get(); - Layer* fcLayer4_ptr = fcLayer4.get(); + 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_ptr, input); - graph.makeConnection(fcLayer_ptr, fcLayer2_ptr); - graph.makeConnection(fcLayer2_ptr, fcLayer3_ptr); - graph.makeConnection(fcLayer_ptr, fcLayer4_ptr); - graph.setOutput(fcLayer4_ptr, output); + graph.setInput(fcLayer, input); + graph.makeConnection(fcLayer, fcLayer2); + graph.makeConnection(fcLayer2, fcLayer3); + graph.makeConnection(fcLayer, fcLayer4); + graph.setOutput(fcLayer4, output); - ASSERT_EQ(graph.areLayerNext(fcLayer_ptr, fcLayer_ptr), 0); + ASSERT_EQ(graph.areLayerNext(fcLayer, fcLayer), 0); } TEST(graph, check_connection_when_not_connection2) { @@ -127,25 +222,21 @@ TEST(graph, check_connection_when_not_connection2) { Tensor bias = make_tensor({0.5F, 0.5F, 1.0F}); Tensor input = make_tensor({1.0F, 2.0F}, {2}); Tensor output; - Graph graph; - auto fcLayer = std::make_unique(weights, bias); - auto fcLayer2 = std::make_unique(weights, bias); - auto fcLayer3 = std::make_unique(weights, bias); - auto fcLayer4 = std::make_unique(weights, bias); + Graph graph; - Layer* fcLayer_ptr = fcLayer.get(); - Layer* fcLayer2_ptr = fcLayer2.get(); - Layer* fcLayer3_ptr = fcLayer3.get(); - Layer* fcLayer4_ptr = fcLayer4.get(); + 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_ptr, input); - graph.makeConnection(fcLayer_ptr, fcLayer2_ptr); - graph.makeConnection(fcLayer2_ptr, fcLayer3_ptr); - graph.makeConnection(fcLayer_ptr, fcLayer4_ptr); - graph.setOutput(fcLayer4_ptr, output); + graph.setInput(fcLayer, input); + graph.makeConnection(fcLayer, fcLayer2); + graph.makeConnection(fcLayer2, fcLayer3); + graph.makeConnection(fcLayer, fcLayer4); + graph.setOutput(fcLayer4, output); - ASSERT_EQ(graph.areLayerNext(fcLayer2_ptr, fcLayer4_ptr), 0); + ASSERT_EQ(graph.areLayerNext(fcLayer2, fcLayer4), 0); } TEST(graph, set_split_distribution) { @@ -162,34 +253,25 @@ TEST(graph, set_input_null_layer) { EXPECT_THROW(graph.setInput(nullptr, input), std::invalid_argument); } -TEST(graph, add_owned_layer_null_check) { - Graph graph; - std::unique_ptr null_layer = nullptr; - graph.addOwnedLayer(std::move(null_layer)); - SUCCEED(); -} - TEST(graph, make_connection_null_layers) { Graph graph; Tensor input = make_tensor({1.0F, 2.0F}, {2}); - auto valid_layer = std::make_unique(); - Layer* valid_ptr = valid_layer.get(); - graph.addOwnedLayer(std::move(valid_layer)); + auto valid_layer = std::make_shared(); - EXPECT_THROW(graph.makeConnection(nullptr, valid_ptr), std::invalid_argument); - EXPECT_THROW(graph.makeConnection(valid_ptr, nullptr), std::invalid_argument); + EXPECT_THROW(graph.makeConnection(nullptr, valid_layer), + std::invalid_argument); + EXPECT_THROW(graph.makeConnection(valid_layer, nullptr), + std::invalid_argument); EXPECT_THROW(graph.makeConnection(nullptr, nullptr), std::invalid_argument); } TEST(graph, make_connection_same_layer) { Graph graph; Tensor input = make_tensor({1.0F, 2.0F}, {2}); - auto layer = std::make_unique(); - Layer* layer_ptr = layer.get(); - graph.addOwnedLayer(std::move(layer)); - graph.setInput(layer_ptr, input); + auto layer = std::make_shared(); + graph.setInput(layer, input); - EXPECT_THROW(graph.makeConnection(layer_ptr, layer_ptr), std::out_of_range); + EXPECT_THROW(graph.makeConnection(layer, layer), std::out_of_range); } TEST(graph, set_output_null_layer) { @@ -229,38 +311,26 @@ TEST(graph, complex_graph_with_split_distribution) { Tensor input = make_tensor({1.0F, 2.0F, 3.0F, 4.0F}, {2, 2}); Tensor output; - auto input_layer = std::make_unique(); - auto split_layer = std::make_unique(1, 2); - auto ew_layer1 = std::make_unique("relu"); - auto ew_layer2 = std::make_unique("sigmoid"); - auto concat_layer = std::make_unique(0); - - Layer* input_ptr = input_layer.get(); - Layer* split_ptr = split_layer.get(); - Layer* ew1_ptr = ew_layer1.get(); - Layer* ew2_ptr = ew_layer2.get(); - Layer* concat_ptr = concat_layer.get(); + auto input_layer = std::make_shared(); + auto split_layer = std::make_shared(1, 2); + auto ew_layer1 = std::make_shared("relu"); + auto ew_layer2 = std::make_shared("sigmoid"); + auto concat_layer = std::make_shared(0); graph.setSplitDistribution(split_dist); - graph.addOwnedLayer(std::move(input_layer)); - graph.addOwnedLayer(std::move(split_layer)); - graph.addOwnedLayer(std::move(ew_layer1)); - graph.addOwnedLayer(std::move(ew_layer2)); - graph.addOwnedLayer(std::move(concat_layer)); - - graph.setInput(input_ptr, input); - graph.makeConnection(input_ptr, split_ptr); - graph.makeConnection(split_ptr, ew1_ptr); - graph.makeConnection(split_ptr, ew2_ptr); - graph.makeConnection(ew1_ptr, concat_ptr); - graph.makeConnection(ew2_ptr, concat_ptr); - graph.setOutput(concat_ptr, output); - - ASSERT_TRUE(graph.areLayerNext(input_ptr, split_ptr)); - ASSERT_TRUE(graph.areLayerNext(split_ptr, ew1_ptr)); - ASSERT_TRUE(graph.areLayerNext(split_ptr, ew2_ptr)); - ASSERT_TRUE(graph.areLayerNext(ew1_ptr, concat_ptr)); - ASSERT_TRUE(graph.areLayerNext(ew2_ptr, concat_ptr)); + graph.setInput(input_layer, input); + graph.makeConnection(input_layer, split_layer); + graph.makeConnection(split_layer, ew_layer1); + graph.makeConnection(split_layer, ew_layer2); + graph.makeConnection(ew_layer1, concat_layer); + graph.makeConnection(ew_layer2, concat_layer); + graph.setOutput(concat_layer, output); + + ASSERT_TRUE(graph.areLayerNext(input_layer, split_layer)); + ASSERT_TRUE(graph.areLayerNext(split_layer, ew_layer1)); + ASSERT_TRUE(graph.areLayerNext(split_layer, ew_layer2)); + ASSERT_TRUE(graph.areLayerNext(ew_layer1, concat_layer)); + ASSERT_TRUE(graph.areLayerNext(ew_layer2, concat_layer)); } TEST(graph, vertex_out_of_range) { @@ -269,24 +339,19 @@ TEST(graph, vertex_out_of_range) { Tensor bias = make_tensor({0.5F, 0.5F, 1.0F}); Tensor input = make_tensor({1.0F, 2.0F}, {2}); Tensor output; - Graph graph; - - auto fcLayer = std::make_unique(weights, bias); - auto fcLayer2 = std::make_unique(weights, bias); - auto fcLayer3 = std::make_unique(weights, bias); - auto fcLayer4 = std::make_unique(weights, bias); - Layer* fcLayer_ptr = fcLayer.get(); - Layer* fcLayer2_ptr = fcLayer2.get(); - Layer* fcLayer3_ptr = fcLayer3.get(); - Layer* fcLayer4_ptr = fcLayer4.get(); + Graph graph; - graph.setInput(fcLayer_ptr, input); - graph.makeConnection(fcLayer_ptr, fcLayer2_ptr); - graph.makeConnection(fcLayer2_ptr, fcLayer3_ptr); - graph.makeConnection(fcLayer_ptr, fcLayer4_ptr); - graph.setOutput(fcLayer4_ptr, output); + 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); + graph.makeConnection(fcLayer2, fcLayer3); + graph.makeConnection(fcLayer, fcLayer4); + graph.setOutput(fcLayer4, output); ASSERT_ANY_THROW(static_cast(graph.getVertexValue(5))); } @@ -296,24 +361,19 @@ TEST(graph, edges_out_of_range) { Tensor bias = make_tensor({0.5F, 0.5F, 1.0F}); Tensor input = make_tensor({1.0F, 2.0F}, {2}); Tensor output; - Graph graph; - - auto fcLayer = std::make_unique(weights, bias); - auto fcLayer2 = std::make_unique(weights, bias); - auto fcLayer3 = std::make_unique(weights, bias); - auto fcLayer4 = std::make_unique(weights, bias); - Layer* fcLayer_ptr = fcLayer.get(); - Layer* fcLayer2_ptr = fcLayer2.get(); - Layer* fcLayer3_ptr = fcLayer3.get(); - Layer* fcLayer4_ptr = fcLayer4.get(); + Graph graph; - graph.setInput(fcLayer_ptr, input); - graph.makeConnection(fcLayer_ptr, fcLayer2_ptr); - graph.makeConnection(fcLayer2_ptr, fcLayer3_ptr); - graph.makeConnection(fcLayer_ptr, fcLayer4_ptr); - graph.setOutput(fcLayer4_ptr, output); + 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); + graph.makeConnection(fcLayer2, fcLayer3); + graph.makeConnection(fcLayer, fcLayer4); + graph.setOutput(fcLayer4, output); ASSERT_ANY_THROW(static_cast(graph.getEdgeValue(999))); } @@ -323,24 +383,19 @@ TEST(graph, inputs_out_of_range) { Tensor bias = make_tensor({0.5F, 0.5F, 1.0F}); Tensor input = make_tensor({1.0F, 2.0F}, {2}); Tensor output; - Graph graph; - auto fcLayer = std::make_unique(weights, bias); - auto fcLayer2 = std::make_unique(weights, bias); - auto fcLayer3 = std::make_unique(weights, bias); - auto fcLayer4 = std::make_unique(weights, bias); - - Layer* fcLayer_ptr = fcLayer.get(); - Layer* fcLayer2_ptr = fcLayer2.get(); - Layer* fcLayer3_ptr = fcLayer3.get(); - Layer* fcLayer4_ptr = fcLayer4.get(); + Graph graph; - graph.setInput(fcLayer_ptr, input); - graph.makeConnection(fcLayer_ptr, fcLayer2_ptr); - graph.makeConnection(fcLayer2_ptr, fcLayer3_ptr); - graph.makeConnection(fcLayer_ptr, fcLayer4_ptr); - graph.setOutput(fcLayer4_ptr, output); + 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); + graph.makeConnection(fcLayer2, fcLayer3); + graph.makeConnection(fcLayer, fcLayer4); + graph.setOutput(fcLayer4, output); ASSERT_ANY_THROW(static_cast(graph.getInputsSize(999))); } @@ -350,55 +405,87 @@ TEST(graph, get_layer_out_of_range) { Tensor bias = make_tensor({0.5F, 0.5F, 1.0F}); Tensor input = make_tensor({1.0F, 2.0F}, {2}); Tensor output; + Graph graph; - auto fcLayer = std::make_unique(weights, bias); - auto fcLayer2 = std::make_unique(weights, bias); - auto fcLayer3 = std::make_unique(weights, bias); - auto fcLayer4 = std::make_unique(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); - Layer* fcLayer_ptr = fcLayer.get(); - Layer* fcLayer2_ptr = fcLayer2.get(); - Layer* fcLayer3_ptr = fcLayer3.get(); - Layer* fcLayer4_ptr = fcLayer4.get(); + graph.setInput(fcLayer, input); + graph.makeConnection(fcLayer, fcLayer2); + graph.makeConnection(fcLayer2, fcLayer3); + graph.makeConnection(fcLayer, fcLayer4); + graph.setOutput(fcLayer4, output); + ASSERT_ANY_THROW(static_cast(graph.getLayerFromID(999))); +} - graph.setInput(fcLayer_ptr, input); - graph.makeConnection(fcLayer_ptr, fcLayer2_ptr); - graph.makeConnection(fcLayer2_ptr, fcLayer3_ptr); - graph.makeConnection(fcLayer_ptr, fcLayer4_ptr); - graph.setOutput(fcLayer4_ptr, output); +TEST(graph, get_in_layers_out_of_range) { + const std::vector vec1 = {2.0F, 1.5F, 0.1F, 1.9F, 0.0F, 5.5F}; + Tensor weights = make_tensor(vec1, {3, 2}); + Tensor bias = make_tensor({0.5F, 0.5F, 1.0F}); + Tensor input = make_tensor({1.0F, 2.0F}, {2}); + Tensor output; - ASSERT_ANY_THROW(static_cast(graph.getLayerFromID(999))); + Graph graph; + 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); + graph.makeConnection(fcLayer2, fcLayer3); + graph.makeConnection(fcLayer, fcLayer4); + graph.setOutput(fcLayer4, output); + ASSERT_ANY_THROW(static_cast(graph.getInLayers(999))); } -TEST(graph_transformations, check_subgraphs_search) { +TEST(graph, get_in_layers) { const std::vector vec1 = {2.0F, 1.5F, 0.1F, 1.9F, 0.0F, 5.5F}; Tensor weights = make_tensor(vec1, {3, 2}); Tensor bias = make_tensor({0.5F, 0.5F, 1.0F}); Tensor input = make_tensor({1.0F, 2.0F}, {2}); Tensor output; + Graph graph; - Graph subgraph; + 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); + graph.makeConnection(fcLayer2, fcLayer3); + graph.makeConnection(fcLayer, fcLayer4); + graph.setOutput(fcLayer4, output); + ASSERT_NO_THROW(static_cast(graph.getInLayers(0))); +} - auto fcLayer = std::make_unique(weights, bias); - auto fcLayer2 = std::make_unique(weights, bias); - auto fcLayer3 = std::make_unique(weights, bias); - auto fcLayer4 = std::make_unique(weights, bias); +TEST(graph_transformations, check_subgraphs_search) { + const std::vector vec1 = {2.0F, 1.5F, 0.1F, 1.9F, 0.0F, 5.5F}; + Tensor weights = make_tensor(vec1, {3, 2}); + Tensor bias = make_tensor({0.5F, 0.5F, 1.0F}); + Tensor input = make_tensor({1.0F, 2.0F}, {2}); + Tensor output; - Layer* fcLayer_ptr = fcLayer.get(); - Layer* fcLayer2_ptr = fcLayer2.get(); - Layer* fcLayer3_ptr = fcLayer3.get(); - Layer* fcLayer4_ptr = fcLayer4.get(); + Graph graph; + Graph subgraph; - graph.setInput(fcLayer_ptr, input); - graph.makeConnection(fcLayer_ptr, fcLayer2_ptr); - graph.makeConnection(fcLayer2_ptr, fcLayer3_ptr); - graph.makeConnection(fcLayer_ptr, fcLayer4_ptr); - graph.setOutput(fcLayer4_ptr, output); + 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); - subgraph.setInput(fcLayer_ptr, input); - subgraph.makeConnection(fcLayer_ptr, fcLayer2_ptr); + graph.setInput(fcLayer, input); + graph.makeConnection(fcLayer, fcLayer2); + graph.makeConnection(fcLayer2, fcLayer3); + graph.makeConnection(fcLayer, fcLayer4); + graph.setOutput(fcLayer4, output); + subgraph.setInput(fcLayer, input); + subgraph.makeConnection(fcLayer, fcLayer2); auto res = find_subgraphs(graph, subgraph); auto it = std::find(res.begin(), res.end(), std::vector({1, 2})); ASSERT_NE(it, res.end()); @@ -410,31 +497,24 @@ TEST(graph_transformations, check_subgraphs_search1) { Tensor bias = make_tensor({0.5F, 0.5F, 1.0F}); Tensor input = make_tensor({1.0F, 2.0F}, {2}); Tensor output; + Graph graph; Graph subgraph; - - auto fcLayer = std::make_unique(weights, bias); - auto fcLayer2 = std::make_unique(weights, bias); - auto fcLayer3 = std::make_unique(weights, bias); - auto fcLayer4 = std::make_unique(weights, bias); - auto ewLayer5 = std::make_unique("relu"); - - Layer* fcLayer_ptr = fcLayer.get(); - Layer* fcLayer2_ptr = fcLayer2.get(); - Layer* fcLayer3_ptr = fcLayer3.get(); - Layer* fcLayer4_ptr = fcLayer4.get(); - Layer* ewLayer5_ptr = ewLayer5.get(); - - graph.setInput(fcLayer_ptr, input); - graph.makeConnection(fcLayer_ptr, fcLayer2_ptr); - graph.makeConnection(fcLayer2_ptr, fcLayer3_ptr); - graph.makeConnection(fcLayer_ptr, fcLayer4_ptr); - graph.makeConnection(fcLayer4_ptr, ewLayer5_ptr); - graph.setOutput(ewLayer5_ptr, output); - - subgraph.setInput(fcLayer_ptr, input); - subgraph.makeConnection(fcLayer_ptr, ewLayer5_ptr); - + 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); + graph.makeConnection(fcLayer2, fcLayer3); + graph.makeConnection(fcLayer, fcLayer4); + graph.makeConnection(fcLayer4, ewLayer5); + graph.setOutput(ewLayer5, output); + + subgraph.setInput(fcLayer, input); + subgraph.makeConnection(fcLayer, ewLayer5); auto res = find_subgraphs(graph, subgraph); auto it = std::find(res.begin(), res.end(), std::vector({3, 4})); ASSERT_NE(it, res.end()); @@ -446,29 +526,24 @@ TEST(graph_transformations, check_subgraphs_search2) { Tensor bias = make_tensor({0.5F, 0.5F, 1.0F}); Tensor input = make_tensor({1.0F, 2.0F}, {2}); Tensor output; + Graph graph; Graph subgraph; - - auto fcLayer = std::make_unique(weights, bias); - auto fcLayer2 = std::make_unique(weights, bias); - auto fcLayer3 = std::make_unique(weights, bias); - auto fcLayer4 = std::make_unique(weights, bias); - - Layer* fcLayer_ptr = fcLayer.get(); - Layer* fcLayer2_ptr = fcLayer2.get(); - Layer* fcLayer3_ptr = fcLayer3.get(); - Layer* fcLayer4_ptr = fcLayer4.get(); - - graph.setInput(fcLayer_ptr, input); - graph.makeConnection(fcLayer_ptr, fcLayer2_ptr); - graph.makeConnection(fcLayer2_ptr, fcLayer3_ptr); - graph.makeConnection(fcLayer3_ptr, fcLayer_ptr); - graph.makeConnection(fcLayer3_ptr, fcLayer4_ptr); - graph.setOutput(fcLayer4_ptr, output); - - subgraph.setInput(fcLayer_ptr, input); - subgraph.makeConnection(fcLayer_ptr, fcLayer2_ptr); - subgraph.makeConnection(fcLayer2_ptr, fcLayer3_ptr); + 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); + graph.makeConnection(fcLayer2, fcLayer3); + graph.makeConnection(fcLayer3, fcLayer); + graph.makeConnection(fcLayer3, fcLayer4); + graph.setOutput(fcLayer4, output); + + subgraph.setInput(fcLayer, input); + subgraph.makeConnection(fcLayer, fcLayer2); + subgraph.makeConnection(fcLayer2, fcLayer3); auto res = find_subgraphs(graph, subgraph); auto it = std::find(res.begin(), res.end(), std::vector({0, 1, 2})); @@ -481,29 +556,24 @@ TEST(graph_transformations, check_subgraphs_search3) { Tensor bias = make_tensor({0.5F, 0.5F, 1.0F}); Tensor input = make_tensor({1.0F, 2.0F}, {2}); Tensor output; + Graph graph; Graph subgraph; - - auto fcLayer = std::make_unique(weights, bias); - auto fcLayer2 = std::make_unique(weights, bias); - auto fcLayer3 = std::make_unique(weights, bias); - auto fcLayer4 = std::make_unique(weights, bias); - - Layer* fcLayer_ptr = fcLayer.get(); - Layer* fcLayer2_ptr = fcLayer2.get(); - Layer* fcLayer3_ptr = fcLayer3.get(); - Layer* fcLayer4_ptr = fcLayer4.get(); - - graph.setInput(fcLayer_ptr, input); - graph.makeConnection(fcLayer_ptr, fcLayer2_ptr); - graph.makeConnection(fcLayer2_ptr, fcLayer3_ptr); - graph.makeConnection(fcLayer3_ptr, fcLayer_ptr); - graph.makeConnection(fcLayer2_ptr, fcLayer4_ptr); - graph.setOutput(fcLayer4_ptr, output); - - subgraph.setInput(fcLayer_ptr, input); - subgraph.makeConnection(fcLayer_ptr, fcLayer2_ptr); - subgraph.makeConnection(fcLayer2_ptr, fcLayer3_ptr); + 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); + graph.makeConnection(fcLayer2, fcLayer3); + graph.makeConnection(fcLayer3, fcLayer); + graph.makeConnection(fcLayer2, fcLayer4); + graph.setOutput(fcLayer4, output); + + subgraph.setInput(fcLayer, input); + subgraph.makeConnection(fcLayer, fcLayer2); + subgraph.makeConnection(fcLayer2, fcLayer3); auto res = find_subgraphs(graph, subgraph); auto it = std::find(res.begin(), res.end(), std::vector({2, 0, 1})); @@ -516,29 +586,24 @@ TEST(graph_transformations, check_subgraphs_search4) { Tensor bias = make_tensor({0.5F, 0.5F, 1.0F}); Tensor input = make_tensor({1.0F, 2.0F}, {2}); Tensor output; + Graph graph; Graph subgraph; - - auto fcLayer = std::make_unique(weights, bias); - auto fcLayer2 = std::make_unique(weights, bias); - auto fcLayer3 = std::make_unique(weights, bias); - auto fcLayer4 = std::make_unique(weights, bias); - - Layer* fcLayer_ptr = fcLayer.get(); - Layer* fcLayer2_ptr = fcLayer2.get(); - Layer* fcLayer3_ptr = fcLayer3.get(); - Layer* fcLayer4_ptr = fcLayer4.get(); - - graph.setInput(fcLayer_ptr, input); - graph.makeConnection(fcLayer_ptr, fcLayer2_ptr); - graph.makeConnection(fcLayer2_ptr, fcLayer3_ptr); - graph.makeConnection(fcLayer3_ptr, fcLayer_ptr); - graph.makeConnection(fcLayer_ptr, fcLayer4_ptr); - graph.setOutput(fcLayer4_ptr, output); - - subgraph.setInput(fcLayer_ptr, input); - subgraph.makeConnection(fcLayer_ptr, fcLayer2_ptr); - subgraph.makeConnection(fcLayer2_ptr, fcLayer3_ptr); + 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); + graph.makeConnection(fcLayer2, fcLayer3); + graph.makeConnection(fcLayer3, fcLayer); + graph.makeConnection(fcLayer, fcLayer4); + graph.setOutput(fcLayer4, output); + + subgraph.setInput(fcLayer, input); + subgraph.makeConnection(fcLayer, fcLayer2); + subgraph.makeConnection(fcLayer2, fcLayer3); auto res = find_subgraphs(graph, subgraph); auto it = std::find(res.begin(), res.end(), std::vector({1, 2, 0})); @@ -551,32 +616,26 @@ TEST(graph_transformations, check_subgraphs_search5) { Tensor bias = make_tensor({0.5F, 0.5F, 1.0F}); Tensor input = make_tensor({1.0F, 2.0F}, {2}); Tensor output; + Graph graph; Graph subgraph; - - auto fcLayer = std::make_unique(weights, bias); - auto fcLayer2 = std::make_unique(weights, bias); - auto fcLayer3 = std::make_unique(weights, bias); - auto fcLayer4 = std::make_unique(weights, bias); - auto ewLayer5 = std::make_unique("relu"); - - Layer* fcLayer_ptr = fcLayer.get(); - Layer* fcLayer2_ptr = fcLayer2.get(); - Layer* fcLayer3_ptr = fcLayer3.get(); - Layer* fcLayer4_ptr = fcLayer4.get(); - Layer* ewLayer5_ptr = ewLayer5.get(); - - graph.setInput(fcLayer_ptr, input); - graph.makeConnection(fcLayer_ptr, fcLayer2_ptr); - graph.makeConnection(fcLayer_ptr, fcLayer4_ptr); - graph.makeConnection(fcLayer2_ptr, fcLayer3_ptr); - graph.makeConnection(fcLayer4_ptr, ewLayer5_ptr); - graph.setOutput(ewLayer5_ptr, output); - - subgraph.setInput(fcLayer_ptr, input); - subgraph.makeConnection(fcLayer_ptr, fcLayer2_ptr); - subgraph.addSingleLayer(fcLayer3_ptr); - subgraph.makeConnection(fcLayer3_ptr, ewLayer5_ptr); + 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); + graph.makeConnection(fcLayer, fcLayer4); + graph.makeConnection(fcLayer2, fcLayer3); + graph.makeConnection(fcLayer4, ewLayer5); + graph.setOutput(ewLayer5, output); + + subgraph.setInput(fcLayer, input); + subgraph.makeConnection(fcLayer, fcLayer2); + subgraph.addSingleLayer(fcLayer3); + subgraph.makeConnection(fcLayer3, ewLayer5); auto res = find_subgraphs(graph, subgraph); auto it = std::find(res.begin(), res.end(), std::vector({1, 3, 2, 4})); @@ -593,48 +652,93 @@ TEST(graph_transformations, check_subgraphs_big_random) { Graph graph; Graph subgraph; - std::vector> layers; - std::vector layer_ptrs; - + std::vector> layers; for (int i = 0; i < num_vertices / 2; i++) { - layers.push_back(std::make_unique(weights, bias)); - layer_ptrs.push_back(layers.back().get()); + layers.push_back(std::make_shared(weights, bias)); } for (int i = 0; i < num_vertices / 2; i++) { - layers.push_back(std::make_unique("relu")); - layer_ptrs.push_back(layers.back().get()); + layers.push_back(std::make_shared("relu")); } - graph.setInput(layer_ptrs[0], input); - std::mt19937 rng(42); - std::uniform_int_distribution first_dist(0, num_vertices - 2); - std::uniform_int_distribution second_dist(1, num_vertices - 1); + graph.setInput(layers[0], input); for (int i = 0; i < num_vertices; i++) { - int rFirst = first_dist(rng); - int rSecond = second_dist(rng); + int rFirst = rand() % (num_vertices - 1); + int rSecond = 1 + rand() % (num_vertices - 1); if ((rFirst == rSecond) || - ((layer_ptrs[rFirst]->getID() == layer_ptrs[rSecond]->getID()) && - (layer_ptrs[rFirst]->getID() != 0))) { + ((layers[rFirst]->getID() == layers[rSecond]->getID()) && + (layers[rFirst]->getID() != 0))) { continue; } - if ((layer_ptrs[rFirst]->getID() >= graph.getLayersCount()) || - (rFirst != 0 && layer_ptrs[rFirst]->getID() == 0)) { - graph.addSingleLayer(layer_ptrs[rFirst]); + if ((layers[rFirst]->getID() >= graph.getLayersCount()) || + (rFirst != 0 && layers[rFirst]->getID() == 0)) { + graph.addSingleLayer(layers[rFirst]); } - graph.makeConnection(layer_ptrs[rFirst], layer_ptrs[rSecond]); - } - graph.setOutput(layer_ptrs[num_vertices - 1], output); - - for (auto& layer : layers) { - graph.addOwnedLayer(std::move(layer)); + graph.makeConnection(layers[rFirst], layers[rSecond]); } + graph.setOutput(layers[num_vertices - 1], output); - subgraph.setInput(layer_ptrs[0], input); - subgraph.makeConnection(layer_ptrs[0], layer_ptrs[50]); - subgraph.makeConnection(layer_ptrs[50], layer_ptrs[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 = elapsed_time_avg(10, find_subgraphs, graph, subgraph); - std::cerr << "Find subgraphs time in ms " << res1_time << '\n'; + std::cerr << "Find subgraphs time in ms " << res1_time << std::endl; +} + +class SubgraphTestsParameterized + : public ::testing::TestWithParam>> {}; + +TEST_P(SubgraphTestsParameterized, check_subgraphs_big_random_lines) { + auto data = GetParam(); + for (size_t m = 0; m < data.size(); m++) { + std::cerr << "(" << std::get<1>(data[m]) << ") "; + int num_vertices = std::get<0>(data[m]); + int num_vertices_sub = std::get<1>(data[m]); + const std::vector vec1 = {2.0F, 1.5F, 0.1F, 1.9F, 0.0F, 5.5F}; + Tensor weights = make_tensor(vec1, {3, 2}); + Tensor bias = make_tensor({0.5F, 0.5F, 1.0F}); + Tensor input = make_tensor({1.0F, 2.0F}, {2}); + Tensor output; + Graph graph; + Graph subgraph; + std::vector> layers; + for (int i = 0; i < num_vertices; i++) { + layers.push_back(std::make_shared(weights, bias)); + } + graph.setInput(layers[0], input); + for (int i = 0; i < num_vertices - 1; i++) { + graph.makeConnection(layers[i], layers[i + 1]); + } + graph.setOutput(layers[num_vertices - 1], output); + + std::shared_ptr temp_layer = + std::make_shared(weights, bias); + subgraph.setInput(temp_layer, input); + std::shared_ptr temp_layer2 = + std::make_shared(weights, bias); + for (int i = 0; i < num_vertices_sub; i++) { + subgraph.makeConnection(temp_layer, temp_layer2); + temp_layer = temp_layer2; + temp_layer2 = std::make_shared(weights, bias); + } + + double res1_time = elapsed_time_avg(1, find_subgraphs, + graph, subgraph); + std::cerr << "Find subgraphs time in ms " + << res1_time / (100 * num_vertices_sub * num_vertices_sub) + << std::endl; + } } + +std::vector> genVector() { + std::vector> results(10); + for (size_t i = 0; i < results.size(); i++) { + results[i] = std::tuple(105, 2 + 2 * static_cast(i)); + } + return results; +} + +INSTANTIATE_TEST_SUITE_P(graph_transformations, SubgraphTestsParameterized, + ::testing::Values(genVector())); diff --git a/test/inference/test_inference.cpp b/test/inference/test_inference.cpp index f6b201a2..7574ebc3 100644 --- a/test/inference/test_inference.cpp +++ b/test/inference/test_inference.cpp @@ -33,73 +33,60 @@ TEST(bfs, check_struct_graph) { Tensor input = make_tensor(vec, sh1); Tensor output = make_tensor(vec, sh1); - auto a1 = std::make_unique(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); - auto a2 = std::make_unique(1, 0, 1, kernel); - auto a3_1 = std::make_unique(1, 0, 1, kernel); - auto a3_1_1 = std::make_unique("relu"); - auto a3_2 = std::make_unique(1, 0, 1, kernel); - auto a3_2_1 = std::make_unique("relu"); - auto a4 = std::make_unique(0); - auto a5 = std::make_unique("relu"); - auto a6_1 = std::make_unique("relu"); - auto a6_2 = std::make_unique("relu"); - auto a7 = std::make_unique(0); - auto a8 = std::make_unique(1, 3); - auto a9_1 = std::make_unique("relu"); - auto a9_2 = std::make_unique("relu"); - auto a9_3 = std::make_unique("relu"); - auto a10 = std::make_unique(0); - auto a11_1 = std::make_unique("relu"); - auto a12 = std::make_unique(0); - - Layer* a1_ptr = a1.get(); - Layer* a2_ptr = a2.get(); - Layer* a3_1_ptr = a3_1.get(); - Layer* a3_1_1_ptr = a3_1_1.get(); - Layer* a3_2_ptr = a3_2.get(); - Layer* a3_2_1_ptr = a3_2_1.get(); - Layer* a4_ptr = a4.get(); - Layer* a5_ptr = a5.get(); - Layer* a6_1_ptr = a6_1.get(); - Layer* a6_2_ptr = a6_2.get(); - Layer* a7_ptr = a7.get(); - Layer* a8_ptr = a8.get(); - Layer* a9_1_ptr = a9_1.get(); - Layer* a9_2_ptr = a9_2.get(); - Layer* a9_3_ptr = a9_3.get(); - Layer* a10_ptr = a10.get(); - Layer* a11_1_ptr = a11_1.get(); - Layer* a12_ptr = a12.get(); - - graph.setInput(a1_ptr, input); - graph.makeConnection(a1_ptr, a2_ptr); - graph.makeConnection(a2_ptr, a3_1_ptr); - graph.makeConnection(a2_ptr, a3_2_ptr); - graph.makeConnection(a3_1_ptr, a3_1_1_ptr); - graph.makeConnection(a3_1_1_ptr, a4_ptr); - graph.makeConnection(a3_2_ptr, a3_2_1_ptr); - graph.makeConnection(a3_2_1_ptr, a4_ptr); - graph.makeConnection(a4_ptr, a5_ptr); - graph.makeConnection(a5_ptr, a7_ptr); - graph.makeConnection(a5_ptr, a6_1_ptr); - graph.makeConnection(a5_ptr, a6_2_ptr); - graph.makeConnection(a6_1_ptr, a7_ptr); - graph.makeConnection(a6_2_ptr, a7_ptr); - graph.makeConnection(a7_ptr, a8_ptr); - graph.makeConnection(a8_ptr, a9_1_ptr); - graph.makeConnection(a8_ptr, a9_2_ptr); - graph.makeConnection(a8_ptr, a9_3_ptr); - graph.makeConnection(a9_1_ptr, a10_ptr); - graph.makeConnection(a9_2_ptr, a10_ptr); - graph.makeConnection(a9_3_ptr, a10_ptr); - graph.makeConnection(a10_ptr, a11_1_ptr); - graph.makeConnection(a11_1_ptr, a12_ptr); - graph.makeConnection(a10_ptr, a12_ptr); - graph.setOutput(a12_ptr, output); - + 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); + graph.makeConnection(a2, a3_1); + graph.makeConnection(a2, a3_2); + graph.makeConnection(a3_1, a3_1_1); + graph.makeConnection(a3_1_1, a4); + graph.makeConnection(a3_2, a3_2_1); + graph.makeConnection(a3_2_1, a4); + graph.makeConnection(a4, a5); + graph.makeConnection(a5, a7); + graph.makeConnection(a5, a6_1); + graph.makeConnection(a5, a6_2); + graph.makeConnection(a6_1, a7); + graph.makeConnection(a6_2, a7); + graph.makeConnection(a7, a8); + graph.makeConnection(a8, a9_1); + graph.makeConnection(a8, a9_2); + graph.makeConnection(a8, a9_3); + graph.makeConnection(a9_1, a10); + graph.makeConnection(a9_2, a10); + graph.makeConnection(a9_3, a10); + graph.makeConnection(a10, a11_1); + graph.makeConnection(a11_1, a12); + graph.makeConnection(a10, a12); + graph.setOutput(a12, output); graph.inference(options); std::vector tmp = *output.as(); std::vector res(36, 81); @@ -126,46 +113,34 @@ TEST(bfs, check_struct_graph_not_used_yolo) { Shape sh2({3, 3}); Tensor kernel = make_tensor(kernelvec, sh2); - auto a2 = std::make_unique(1, 4); - auto a3_1 = std::make_unique("relu"); - auto a3_1_1 = std::make_unique("relu"); - auto a3_2 = std::make_unique(0); - auto a3_2_1 = std::make_unique("relu"); - auto a3_3 = std::make_unique("relu"); - auto a3_3_1 = std::make_unique(0); - auto a3_3_2 = std::make_unique("relu"); - auto a3_3_3 = std::make_unique("relu"); - auto a3_3_4 = std::make_unique("relu"); - auto a4 = std::make_unique(0); - - Layer* a2_ptr = a2.get(); - Layer* a3_1_ptr = a3_1.get(); - Layer* a3_1_1_ptr = a3_1_1.get(); - Layer* a3_2_ptr = a3_2.get(); - Layer* a3_2_1_ptr = a3_2_1.get(); - Layer* a3_3_ptr = a3_3.get(); - Layer* a3_3_1_ptr = a3_3_1.get(); - Layer* a3_3_2_ptr = a3_3_2.get(); - Layer* a3_3_3_ptr = a3_3_3.get(); - Layer* a3_3_4_ptr = a3_3_4.get(); - Layer* a4_ptr = a4.get(); - - graph.setInput(a2_ptr, input); - graph.makeConnection(a2_ptr, a3_1_ptr); - graph.makeConnection(a2_ptr, a3_2_ptr); - graph.makeConnection(a2_ptr, a3_3_ptr); - graph.makeConnection(a3_1_ptr, a3_1_1_ptr); - graph.makeConnection(a3_1_1_ptr, a4_ptr); - graph.makeConnection(a3_2_ptr, a3_2_1_ptr); - graph.makeConnection(a3_2_1_ptr, a4_ptr); - graph.makeConnection(a3_3_ptr, a3_3_1_ptr); - graph.makeConnection(a2_ptr, a3_3_1_ptr); - graph.makeConnection(a3_3_1_ptr, a3_3_2_ptr); - graph.makeConnection(a3_3_2_ptr, a3_3_3_ptr); - graph.makeConnection(a3_3_3_ptr, a3_3_4_ptr); - graph.makeConnection(a3_3_4_ptr, a3_2_ptr); - graph.setOutput(a4_ptr, output); - + 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); + graph.makeConnection(a2, a3_2); + graph.makeConnection(a2, a3_3); + graph.makeConnection(a3_1, a3_1_1); + graph.makeConnection(a3_1_1, a4); + graph.makeConnection(a3_2, a3_2_1); + graph.makeConnection(a3_2_1, a4); + graph.makeConnection(a3_3, a3_3_1); + graph.makeConnection(a2, a3_3_1); + graph.makeConnection(a3_3_1, a3_3_2); + graph.makeConnection(a3_3_2, a3_3_3); + graph.makeConnection(a3_3_3, a3_3_4); + graph.makeConnection(a3_3_4, a3_2); + + graph.setOutput(a4, output); graph.inference(options); std::vector tmp = *output.as(); std::vector res(16, 3); @@ -188,42 +163,30 @@ TEST(bfs, check_struct_graph_resnet1) { Tensor input = make_tensor(vec, sh1); Tensor output = make_tensor(vec, sh1); - auto a2 = std::make_unique(1, 2); - auto a2_1 = std::make_unique("relu"); - auto a2_1_1 = std::make_unique("relu"); - auto a2_1_1_1 = std::make_unique("relu"); - auto a2_1_1_2 = std::make_unique("relu"); - auto a2_1_2 = std::make_unique(BinaryOpLayer::Operation::kMul); - auto a2_1_3 = std::make_unique("relu"); - auto a2_2 = std::make_unique("relu"); - auto a3 = std::make_unique(BinaryOpLayer::Operation::kAdd); - auto a4 = std::make_unique("relu"); - - Layer* a2_ptr = a2.get(); - Layer* a2_1_ptr = a2_1.get(); - Layer* a2_1_1_ptr = a2_1_1.get(); - Layer* a2_1_1_1_ptr = a2_1_1_1.get(); - Layer* a2_1_1_2_ptr = a2_1_1_2.get(); - Layer* a2_1_2_ptr = a2_1_2.get(); - Layer* a2_1_3_ptr = a2_1_3.get(); - Layer* a2_2_ptr = a2_2.get(); - Layer* a3_ptr = a3.get(); - Layer* a4_ptr = a4.get(); - - graph.setInput(a2_ptr, input); - graph.makeConnection(a2_ptr, a2_1_ptr); - graph.makeConnection(a2_ptr, a2_2_ptr); - graph.makeConnection(a2_1_ptr, a2_1_1_ptr); - graph.makeConnection(a2_1_1_ptr, a2_1_1_1_ptr); - graph.makeConnection(a2_1_1_1_ptr, a2_1_1_2_ptr); - graph.makeConnection(a2_1_1_2_ptr, a2_1_2_ptr); - graph.makeConnection(a2_1_1_ptr, a2_1_2_ptr); - graph.makeConnection(a2_1_2_ptr, a2_1_3_ptr); - graph.makeConnection(a2_1_3_ptr, a3_ptr); - graph.makeConnection(a2_2_ptr, a3_ptr); - graph.makeConnection(a3_ptr, a4_ptr); - graph.setOutput(a4_ptr, output); - + 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); + graph.makeConnection(a2, a2_2); + graph.makeConnection(a2_1, a2_1_1); + graph.makeConnection(a2_1_1, a2_1_1_1); + graph.makeConnection(a2_1_1_1, a2_1_1_2); + graph.makeConnection(a2_1_1_2, a2_1_2); + graph.makeConnection(a2_1_1, a2_1_2); + graph.makeConnection(a2_1_2, a2_1_3); + graph.makeConnection(a2_1_3, a3); + graph.makeConnection(a2_2, a3); + graph.makeConnection(a3, a4); + graph.setOutput(a4, output); graph.inference(options); std::vector tmp = *output.as(); std::vector res(4, 12); @@ -246,39 +209,28 @@ TEST(bfs, check_struct_graph_resnet2) { Tensor input = make_tensor(vec, sh1); Tensor output = make_tensor(vec, sh1); - auto a2 = std::make_unique(1, 2); - auto a2_1 = std::make_unique("relu"); - auto a2_1_1 = std::make_unique("relu"); - auto a2_1_1_1 = std::make_unique("relu"); - auto a2_1_1_2 = std::make_unique("relu"); - auto a2_1_2 = std::make_unique(BinaryOpLayer::Operation::kMul); - auto a2_1_3 = std::make_unique("relu"); - auto a3 = std::make_unique(BinaryOpLayer::Operation::kAdd); - auto a4 = std::make_unique("relu"); - - Layer* a2_ptr = a2.get(); - Layer* a2_1_ptr = a2_1.get(); - Layer* a2_1_1_ptr = a2_1_1.get(); - Layer* a2_1_1_1_ptr = a2_1_1_1.get(); - Layer* a2_1_1_2_ptr = a2_1_1_2.get(); - Layer* a2_1_2_ptr = a2_1_2.get(); - Layer* a2_1_3_ptr = a2_1_3.get(); - Layer* a3_ptr = a3.get(); - Layer* a4_ptr = a4.get(); - - graph.setInput(a2_ptr, input); - graph.makeConnection(a2_ptr, a2_1_ptr); - graph.makeConnection(a2_1_ptr, a2_1_1_ptr); - graph.makeConnection(a2_1_1_ptr, a2_1_1_1_ptr); - graph.makeConnection(a2_1_1_1_ptr, a2_1_1_2_ptr); - graph.makeConnection(a2_1_1_2_ptr, a2_1_2_ptr); - graph.makeConnection(a2_1_1_ptr, a2_1_2_ptr); - graph.makeConnection(a2_1_2_ptr, a2_1_3_ptr); - graph.makeConnection(a2_1_3_ptr, a3_ptr); - graph.makeConnection(a2_ptr, a3_ptr); - graph.makeConnection(a3_ptr, a4_ptr); - graph.setOutput(a4_ptr, output); - + 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); + graph.makeConnection(a2_1, a2_1_1); + graph.makeConnection(a2_1_1, a2_1_1_1); + graph.makeConnection(a2_1_1_1, a2_1_1_2); + graph.makeConnection(a2_1_1_2, a2_1_2); + graph.makeConnection(a2_1_1, a2_1_2); + graph.makeConnection(a2_1_2, a2_1_3); + graph.makeConnection(a2_1_3, a3); + graph.makeConnection(a2, a3); + graph.makeConnection(a3, a4); + graph.setOutput(a4, output); graph.inference(options); std::vector tmp = *output.as(); std::vector res(4, 12); @@ -301,37 +253,27 @@ TEST(bfs, check_struct_graph_google1) { Tensor input = make_tensor(vec, sh1); Tensor output = make_tensor(vec, sh1); - auto a2 = std::make_unique("relu"); - auto a2_1 = std::make_unique("relu"); - auto a2_2 = std::make_unique("relu"); - auto a2_3 = std::make_unique("relu"); - auto a2_4 = std::make_unique("relu"); - auto a2_2_1 = std::make_unique("linear", 2.0F, 3.0F); - auto a2_3_1 = std::make_unique("linear", 2.0F, 3.0F); - auto a3 = std::make_unique(0); - - Layer* a2_ptr = a2.get(); - Layer* a2_1_ptr = a2_1.get(); - Layer* a2_2_ptr = a2_2.get(); - Layer* a2_3_ptr = a2_3.get(); - Layer* a2_4_ptr = a2_4.get(); - Layer* a2_2_1_ptr = a2_2_1.get(); - Layer* a2_3_1_ptr = a2_3_1.get(); - Layer* a3_ptr = a3.get(); - - graph.setInput(a2_ptr, input); - graph.makeConnection(a2_ptr, a2_1_ptr); - graph.makeConnection(a2_ptr, a2_2_ptr); - graph.makeConnection(a2_ptr, a2_3_ptr); - graph.makeConnection(a2_ptr, a2_4_ptr); - graph.makeConnection(a2_2_ptr, a2_2_1_ptr); - graph.makeConnection(a2_3_ptr, a2_3_1_ptr); - graph.makeConnection(a2_4_ptr, a3_ptr); - graph.makeConnection(a2_3_1_ptr, a3_ptr); - graph.makeConnection(a2_2_1_ptr, a3_ptr); - graph.makeConnection(a2_1_ptr, a3_ptr); - graph.setOutput(a3_ptr, output); - + 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); + graph.makeConnection(a2, a2_2); + graph.makeConnection(a2, a2_3); + graph.makeConnection(a2, a2_4); + graph.makeConnection(a2_2, a2_2_1); + graph.makeConnection(a2_3, a2_3_1); + graph.makeConnection(a2_4, a3); + graph.makeConnection(a2_3_1, a3); + graph.makeConnection(a2_2_1, a3); + graph.makeConnection(a2_1, a3); + graph.setOutput(a3, output); graph.inference(options); std::vector tmp = *output.as(); std::vector res(32, 3); @@ -357,23 +299,18 @@ TEST(bfs, check_result_vec) { Tensor input = make_tensor(vec, sh1); Tensor output = make_tensor(vec, sh1); - auto a1 = std::make_unique(kNhwc, kNchw, 1, 2); - auto a3 = std::make_unique(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); - auto a2 = std::make_unique(1, 0, 1, kernel); - auto a4 = std::make_unique(1, 0, 1, kernel); - - Layer* a1_ptr = a1.get(); - Layer* a2_ptr = a2.get(); - Layer* a4_ptr = a4.get(); - - graph.setInput(a1_ptr, input); - graph.makeConnection(a1_ptr, a2_ptr); - graph.makeConnection(a2_ptr, a4_ptr); - graph.setOutput(a4_ptr, output); + 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); + graph.setOutput(a4, output); graph.inference(options); std::vector tmp = *output.as(); std::vector res = {81, 81, 81}; @@ -424,7 +361,6 @@ TEST(bfs, check_result_vec) { #endif ASSERT_EQ(tmp, res); } - TEST(bfs, check_end_to_end) { RuntimeOptions options; options.backend = Backend::kNaive; @@ -441,7 +377,7 @@ TEST(bfs, check_end_to_end) { Tensor input = make_tensor(vec, sh1); Tensor output = make_tensor(vec, sh1); - auto a1 = std::make_unique(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) { @@ -449,26 +385,19 @@ TEST(bfs, check_end_to_end) { } Shape sh2({3, 3, 3, 3}); Tensor kernel = make_tensor(kernelvec, sh2); - auto a2 = std::make_unique(1, 0, 1, kernel); + auto a2 = std::make_shared(1, 0, 1, kernel); Shape poolshape = {2, 2}; - auto a3 = std::make_unique("linear", 2.0F, 3.0F); - auto a4 = std::make_unique(poolshape, "average"); - auto a6 = std::make_unique(); - auto a5 = std::make_unique(); - - Layer* a1_ptr = a1.get(); - Layer* a2_ptr = a2.get(); - Layer* a3_ptr = a3.get(); - Layer* a4_ptr = a4.get(); - Layer* a5_ptr = a5.get(); - - graph.setInput(a1_ptr, input); - graph.makeConnection(a1_ptr, a2_ptr); - graph.makeConnection(a2_ptr, a3_ptr); - graph.makeConnection(a3_ptr, a4_ptr); - graph.makeConnection(a4_ptr, a5_ptr); - graph.setOutput(a5_ptr, output); - + 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); + graph.makeConnection(a3, a4); + graph.makeConnection(a4, a5); + graph.setOutput(a5, output); graph.inference(options); std::vector tmp = *output.as(); @@ -494,22 +423,17 @@ TEST(bfs, check_struct_layer) { Tensor input = make_tensor(vec, sh1); Tensor output = make_tensor(vec, sh1); - auto a1 = std::make_unique(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); - auto a2 = std::make_unique(1, 0, 1, kernel); - auto a3 = std::make_unique(1, 0, 1, kernel); - - Layer* a1_ptr = a1.get(); - Layer* a2_ptr = a2.get(); - Layer* a3_ptr = a3.get(); - - graph.setInput(a1_ptr, input); - graph.makeConnection(a1_ptr, a2_ptr); - graph.makeConnection(a2_ptr, a3_ptr); - graph.setOutput(a3_ptr, output); + auto a2 = std::make_shared(1, 0, 1, kernel); + auto a3 = std::make_shared(1, 0, 1, kernel); + graph.setInput(a1, input); + graph.makeConnection(a1, a2); + graph.makeConnection(a2, a3); + graph.setOutput(a3, output); graph.inference(options); std::vector tmp = *output.as(); std::vector res = {81, 81, 81}; @@ -532,27 +456,20 @@ TEST(bfs, check_struct_layer_added) { Tensor input = make_tensor(vec, sh1); Tensor output = make_tensor(vec, sh1); - auto a1 = std::make_unique(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); - auto a2 = std::make_unique(1, 0, 1, kernel); - auto a3 = std::make_unique(1, 0, 1, kernel); - auto a4 = std::make_unique("linear", 2.0F, 3.0F); - - Layer* a1_ptr = a1.get(); - Layer* a2_ptr = a2.get(); - Layer* a3_ptr = a3.get(); - Layer* a4_ptr = a4.get(); - - a2->postops.layers.push_back(a4_ptr); + 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); a2->postops.count++; - graph.setInput(a1_ptr, input); - graph.makeConnection(a1_ptr, a2_ptr); - graph.makeConnection(a2_ptr, a3_ptr); - graph.setOutput(a3_ptr, output); - + graph.setInput(a1, input); + graph.makeConnection(a1, a2); + graph.makeConnection(a2, a3); + graph.setOutput(a3, output); graph.inference(options); std::vector tmp = *output.as(); std::vector res = {189, 189, 189}; @@ -577,76 +494,56 @@ FLAKY_TEST(bfs, check_struct_graph_split) { Tensor input = make_tensor(vec, sh1); Tensor output = make_tensor(vec, sh1); - auto a1 = std::make_unique(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); - auto a2 = std::make_unique(1, 0, 1, kernel); - auto a3_1 = std::make_unique(1, 0, 1, kernel); - auto a3_1_1 = std::make_unique("relu"); - auto a3_2 = std::make_unique(1, 0, 1, kernel); - auto a3_2_1 = std::make_unique("relu"); - auto a4 = std::make_unique(0); - auto a5 = std::make_unique("relu"); - auto a6_1 = std::make_unique("relu"); - auto a6_2 = std::make_unique("relu"); - auto a7 = std::make_unique(0); - auto a8 = std::make_unique(1, 3); - auto a9_1 = std::make_unique("relu"); - auto a9_2 = std::make_unique("relu"); - auto a9_3 = std::make_unique("relu"); - auto a10 = std::make_unique(0); - auto a11_1 = std::make_unique("relu"); - auto a12 = std::make_unique(0); - - Layer* a1_ptr = a1.get(); - Layer* a2_ptr = a2.get(); - Layer* a3_1_ptr = a3_1.get(); - Layer* a3_1_1_ptr = a3_1_1.get(); - Layer* a3_2_ptr = a3_2.get(); - Layer* a3_2_1_ptr = a3_2_1.get(); - Layer* a4_ptr = a4.get(); - Layer* a5_ptr = a5.get(); - Layer* a6_1_ptr = a6_1.get(); - Layer* a6_2_ptr = a6_2.get(); - Layer* a7_ptr = a7.get(); - Layer* a8_ptr = a8.get(); - Layer* a9_1_ptr = a9_1.get(); - Layer* a9_2_ptr = a9_2.get(); - Layer* a9_3_ptr = a9_3.get(); - Layer* a10_ptr = a10.get(); - Layer* a11_1_ptr = a11_1.get(); - Layer* a12_ptr = a12.get(); - - graph.setInput(a1_ptr, input); - graph.makeConnection(a1_ptr, a2_ptr); - graph.makeConnection(a2_ptr, a3_1_ptr); - graph.makeConnection(a2_ptr, a3_2_ptr); - graph.makeConnection(a3_1_ptr, a3_1_1_ptr); - graph.makeConnection(a3_1_1_ptr, a4_ptr); - graph.makeConnection(a3_2_ptr, a3_2_1_ptr); - graph.makeConnection(a3_2_1_ptr, a4_ptr); - graph.makeConnection(a4_ptr, a5_ptr); - graph.makeConnection(a5_ptr, a7_ptr); - graph.makeConnection(a5_ptr, a6_1_ptr); - graph.makeConnection(a5_ptr, a6_2_ptr); - graph.makeConnection(a6_1_ptr, a7_ptr); - graph.makeConnection(a6_2_ptr, a7_ptr); - graph.makeConnection(a7_ptr, a8_ptr); - graph.makeConnection(a8_ptr, a9_1_ptr); - graph.makeConnection(a8_ptr, a9_2_ptr); - graph.makeConnection(a8_ptr, a9_3_ptr); - graph.makeConnection(a9_1_ptr, a10_ptr); - graph.makeConnection(a9_2_ptr, a10_ptr); - graph.makeConnection(a9_3_ptr, a10_ptr); - graph.makeConnection(a10_ptr, a11_1_ptr); - graph.makeConnection(a11_1_ptr, a12_ptr); - graph.makeConnection(a10_ptr, a12_ptr); - graph.setOutput(a12_ptr, output); - + 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); + graph.makeConnection(a2, a3_1); + graph.makeConnection(a2, a3_2); + graph.makeConnection(a3_1, a3_1_1); + graph.makeConnection(a3_1_1, a4); + graph.makeConnection(a3_2, a3_2_1); + graph.makeConnection(a3_2_1, a4); + graph.makeConnection(a4, a5); + graph.makeConnection(a5, a7); + graph.makeConnection(a5, a6_1); + graph.makeConnection(a5, a6_2); + graph.makeConnection(a6_1, a7); + graph.makeConnection(a6_2, a7); + graph.makeConnection(a7, a8); + graph.makeConnection(a8, a9_1); + graph.makeConnection(a8, a9_2); + graph.makeConnection(a8, a9_3); + graph.makeConnection(a9_1, a10); + graph.makeConnection(a9_2, a10); + graph.makeConnection(a9_3, a10); + graph.makeConnection(a10, a11_1); + graph.makeConnection(a11_1, a12); + graph.makeConnection(a10, a12); + graph.setOutput(a12, output); graph.inference(options); std::vector tmp = *output.as(); std::vector res(36, 81); ASSERT_EQ(tmp, res); } -FLAKY_END_TEST \ No newline at end of file +FLAKY_END_TEST