1+ #include < algorithm>
2+ #include < random>
13#include < vector>
24
35#include " graph/graph.hpp"
68#include " layers/EWLayer.hpp"
79#include " layers/FCLayer.hpp"
810#include " layers/InputLayer.hpp"
11+ #include " perf/benchmarking.hpp"
912
1013using namespace it_lab_ai ;
1114
@@ -224,8 +227,9 @@ TEST(graph_transformations, check_subgraphs_search) {
224227
225228 subgraph.setInput (fcLayer, input);
226229 subgraph.makeConnection (fcLayer, fcLayer2);
227-
228- ASSERT_EQ (find_subgraphs (graph, subgraph), std::vector<int >({1 }));
230+ auto res = find_subgraphs (graph, subgraph);
231+ auto it = std::find (res.begin (), res.end (), std::vector<int >({1 , 2 }));
232+ ASSERT_NE (it, res.end ());
229233}
230234
231235TEST (graph_transformations, check_subgraphs_search1) {
@@ -241,19 +245,20 @@ TEST(graph_transformations, check_subgraphs_search1) {
241245 FCLayer fcLayer2 (weights, bias);
242246 FCLayer fcLayer3 (weights, bias);
243247 FCLayer fcLayer4 (weights, bias);
244- FCLayer fcLayer5 (weights, bias );
248+ EWLayer ewLayer5 ( " relu " );
245249
246250 graph.setInput (fcLayer, input);
247251 graph.makeConnection (fcLayer, fcLayer2);
248252 graph.makeConnection (fcLayer2, fcLayer3);
249253 graph.makeConnection (fcLayer, fcLayer4);
250- graph.makeConnection (fcLayer4, fcLayer5 );
251- graph.setOutput (fcLayer5 , output);
254+ graph.makeConnection (fcLayer4, ewLayer5 );
255+ graph.setOutput (ewLayer5 , output);
252256
253257 subgraph.setInput (fcLayer, input);
254- subgraph.makeConnection (fcLayer, fcLayer2);
255-
256- ASSERT_EQ (find_subgraphs (graph, subgraph), std::vector<int >({1 , 3 }));
258+ subgraph.makeConnection (fcLayer, ewLayer5);
259+ auto res = find_subgraphs (graph, subgraph);
260+ auto it = std::find (res.begin (), res.end (), std::vector<int >({3 , 4 }));
261+ ASSERT_NE (it, res.end ());
257262}
258263
259264TEST (graph_transformations, check_subgraphs_search2) {
@@ -281,7 +286,9 @@ TEST(graph_transformations, check_subgraphs_search2) {
281286 subgraph.makeConnection (fcLayer, fcLayer2);
282287 subgraph.makeConnection (fcLayer2, fcLayer3);
283288
284- ASSERT_EQ (find_subgraphs (graph, subgraph), std::vector<int >({0 }));
289+ auto res = find_subgraphs (graph, subgraph);
290+ auto it = std::find (res.begin (), res.end (), std::vector<int >({0 , 1 , 2 }));
291+ ASSERT_NE (it, res.end ());
285292}
286293
287294TEST (graph_transformations, check_subgraphs_search3) {
@@ -309,7 +316,9 @@ TEST(graph_transformations, check_subgraphs_search3) {
309316 subgraph.makeConnection (fcLayer, fcLayer2);
310317 subgraph.makeConnection (fcLayer2, fcLayer3);
311318
312- ASSERT_EQ (find_subgraphs (graph, subgraph), std::vector<int >({2 }));
319+ auto res = find_subgraphs (graph, subgraph);
320+ auto it = std::find (res.begin (), res.end (), std::vector<int >({2 , 0 , 1 }));
321+ ASSERT_NE (it, res.end ());
313322}
314323
315324TEST (graph_transformations, check_subgraphs_search4) {
@@ -337,5 +346,82 @@ TEST(graph_transformations, check_subgraphs_search4) {
337346 subgraph.makeConnection (fcLayer, fcLayer2);
338347 subgraph.makeConnection (fcLayer2, fcLayer3);
339348
340- ASSERT_EQ (find_subgraphs (graph, subgraph), std::vector<int >({1 }));
349+ auto res = find_subgraphs (graph, subgraph);
350+ auto it = std::find (res.begin (), res.end (), std::vector<int >({1 , 2 , 0 }));
351+ ASSERT_NE (it, res.end ());
352+ }
353+
354+ TEST (graph_transformations, check_subgraphs_search5) {
355+ const std::vector<float > vec1 = {2 .0F , 1 .5F , 0 .1F , 1 .9F , 0 .0F , 5 .5F };
356+ Tensor weights = make_tensor<float >(vec1, {3 , 2 });
357+ Tensor bias = make_tensor<float >({0 .5F , 0 .5F , 1 .0F });
358+ Tensor input = make_tensor<float >({1 .0F , 2 .0F }, {2 });
359+ Tensor output;
360+
361+ Graph graph (5 );
362+ Graph subgraph (2 );
363+ FCLayer fcLayer (weights, bias);
364+ FCLayer fcLayer2 (weights, bias);
365+ FCLayer fcLayer3 (weights, bias);
366+ FCLayer fcLayer4 (weights, bias);
367+ EWLayer ewLayer5 (" relu" );
368+
369+ graph.setInput (fcLayer, input);
370+ graph.makeConnection (fcLayer, fcLayer2);
371+ graph.makeConnection (fcLayer, fcLayer4);
372+ graph.makeConnection (fcLayer2, fcLayer3);
373+ graph.makeConnection (fcLayer4, ewLayer5);
374+ graph.setOutput (ewLayer5, output);
375+
376+ subgraph.setInput (fcLayer, input);
377+ subgraph.makeConnection (fcLayer, fcLayer2);
378+ subgraph.addSingleLayer (fcLayer3);
379+ subgraph.makeConnection (fcLayer3, ewLayer5);
380+
381+ auto res = find_subgraphs (graph, subgraph);
382+ auto it = std::find (res.begin (), res.end (), std::vector<int >({1 , 3 , 2 , 4 }));
383+ ASSERT_NE (it, res.end ());
384+ }
385+
386+ TEST (graph_transformations, check_subgraphs_big_random) {
387+ const int num_vertices = 1000 ;
388+ const std::vector<float > vec1 = {2 .0F , 1 .5F , 0 .1F , 1 .9F , 0 .0F , 5 .5F };
389+ Tensor weights = make_tensor<float >(vec1, {3 , 2 });
390+ Tensor bias = make_tensor<float >({0 .5F , 0 .5F , 1 .0F });
391+ Tensor input = make_tensor<float >({1 .0F , 2 .0F }, {2 });
392+ Tensor output;
393+ Graph graph (num_vertices);
394+ Graph subgraph (3 );
395+ std::vector<std::shared_ptr<Layer>> layers;
396+ for (int i = 0 ; i < num_vertices / 2 ; i++) {
397+ layers.push_back (std::make_shared<FCLayer>(weights, bias));
398+ }
399+ for (int i = 0 ; i < num_vertices / 2 ; i++) {
400+ layers.push_back (std::make_shared<EWLayer>(" relu" ));
401+ }
402+ graph.setInput (*layers[0 ], input);
403+ for (int i = 0 ; i < num_vertices; i++) {
404+ int rFirst = rand () % (num_vertices - 1 );
405+ int rSecond = 1 + rand () % (num_vertices - 1 );
406+ if ((rFirst == rSecond) ||
407+ ((*layers[rFirst]).getID () == (*layers[rSecond]).getID ()) &&
408+ ((*layers[rFirst]).getID () != 0 )) {
409+ continue ;
410+ }
411+ if (((*layers[rFirst]).getID () >= graph.getLayersCount ()) ||
412+ (rFirst != 0 && (*layers[rFirst]).getID () == 0 )) {
413+ graph.addSingleLayer (*layers[rFirst]);
414+ }
415+ graph.makeConnection (*layers[rFirst], *layers[rSecond]);
416+ }
417+ graph.setOutput (*layers[num_vertices - 1 ], output);
418+
419+ subgraph.setInput (*layers[0 ], input);
420+ subgraph.makeConnection (*layers[0 ], *layers[50 ]);
421+ subgraph.makeConnection (*layers[50 ], *layers[1 ]);
422+
423+ std::vector<std::vector<int >> res1 = find_subgraphs (graph, subgraph);
424+ double res1_time =
425+ elapsed_time_avg<double , std::milli>(10 , find_subgraphs, graph, subgraph);
426+ std::cerr << " Find subgraphs time in ms " << res1_time << std::endl;
341427}
0 commit comments