Imported Upstream version 1.8.0
[platform/core/ml/nnfw.git] / compiler / locop / src / FormattedGraph.test.cpp
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "locop/FormattedGraph.h"
18 #include "ExampleGraph.h"
19
20 #include <stdex/Memory.h>
21
22 #include <gtest/gtest.h>
23
24 TEST(LinearV1FormatterTest, simple)
25 {
26   auto bundle = make_bundle<PullPush>();
27   auto g = bundle->graph();
28
29   // TODO Validate the output (when the implementation becomes stable)
30   std::cout << locop::fmt<locop::LinearV1>(g) << std::endl;
31
32   SUCCEED();
33 }
34
35 TEST(LinearV1FormatterTest, user_defined_node_summary_builder)
36 {
37   struct MyAnnotation final : public loco::NodeAnnotation
38   {
39     // DO NOTHING
40   };
41
42   auto bundle = make_bundle<PullPush>();
43   auto g = bundle->graph();
44   {
45     bundle->push->annot(stdex::make_unique<MyAnnotation>());
46   }
47
48   struct MyBuilder final : public locop::NodeSummaryBuilder
49   {
50     bool build(const loco::Node *node, locop::NodeSummary &s) const final
51     {
52       s.opname("my.op");
53       if (node->annot<MyAnnotation>())
54       {
55         s.comments().append("annotated");
56       }
57       s.state(locop::NodeSummary::State::PartiallyKnown);
58       return true;
59     }
60   };
61
62   struct MyFactory final : public locop::NodeSummaryBuilderFactory
63   {
64     std::unique_ptr<locop::NodeSummaryBuilder> create(const locop::SymbolTable *) const final
65     {
66       return stdex::make_unique<MyBuilder>();
67     }
68   };
69
70   std::cout << locop::fmt<locop::LinearV1>(g).with(stdex::make_unique<MyFactory>()) << std::endl;
71
72   // TODO Check whether MyBuilder actually sees all the nodes in a graph
73   SUCCEED();
74 }
75
76 // This test shows how to compose two node summary builders.
77 TEST(LinearV1FormatterTest, node_summary_builder_composition)
78 {
79   struct MyNode : public loco::FixedArity<0>::Mixin<loco::Node>
80   {
81     uint32_t opnum(void) const final { return 0; }
82     const loco::Dialect *dialect(void) const final { return nullptr; };
83   };
84
85   auto g = loco::make_graph();
86   {
87     auto user = g->nodes()->create<MyNode>();
88
89     auto push = g->nodes()->create<loco::Push>();
90
91     push->from(user);
92   }
93
94   // TODO Reuse MyBuilder above
95   struct MyBuilder final : public locop::NodeSummaryBuilder
96   {
97     bool build(const loco::Node *node, locop::NodeSummary &s) const final
98     {
99       s.opname("my.op");
100       s.state(locop::NodeSummary::State::PartiallyKnown);
101       return true;
102     }
103   };
104
105   class CompositeBuilder final : public locop::NodeSummaryBuilder
106   {
107   public:
108     CompositeBuilder(const locop::SymbolTable *tbl) : _tbl{tbl}
109     {
110       // DO NOTHING
111     }
112
113   public:
114     bool build(const loco::Node *node, locop::NodeSummary &s) const final
115     {
116       if (locop::CanonicalNodeSummaryBuilder(_tbl).build(node, s))
117       {
118         return true;
119       }
120
121       if (MyBuilder().build(node, s))
122       {
123         return true;
124       }
125
126       return false;
127     }
128
129   private:
130     const locop::SymbolTable *_tbl;
131   };
132
133   struct MyFactory final : public locop::NodeSummaryBuilderFactory
134   {
135     std::unique_ptr<locop::NodeSummaryBuilder> create(const locop::SymbolTable *tbl) const final
136     {
137       return stdex::make_unique<CompositeBuilder>(tbl);
138     }
139   };
140
141   std::cout << locop::fmt<locop::LinearV1>(g).with(stdex::make_unique<MyFactory>()) << std::endl;
142
143   // TODO Check whether MyBuilder actually sees all the nodes in a graph
144   SUCCEED();
145 }