ONE - On-device Neural Engine
Loading...
Searching...
No Matches
circle-input-names.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 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 <iostream>
18#include <crew/PConfigJson.h>
20
21using namespace luci;
22
23// clang-format off
24// For Variadic Arity Nodes which have multiple inputs.
25template <typename T> struct is_VariadicArity : std::false_type {};
26template <> struct is_VariadicArity<CircleConcatenation> : std::true_type {};
27template <> struct is_VariadicArity<CircleCustom> : std::true_type {};
28template <> struct is_VariadicArity<CirclePack> : std::true_type {};
29template <> struct is_VariadicArity<CircleAddN> : std::true_type {};
30template <> struct is_VariadicArity<CircleIf> : std::true_type {};
31template <> struct is_VariadicArity<CircleWhile> : std::true_type {};
32
33// For Variadic Outputs Nodes which have multiple outputs.
34template <typename T> struct is_VariadicOut : std::false_type {};
35template <> struct is_VariadicOut<CircleCustom> : std::true_type{};
36template <> struct is_VariadicOut<CircleIf> : std::true_type {};
37template <> struct is_VariadicOut<CircleWhile> : std::true_type {};
38// clang-format on
39
40// For Circle Nodes which have variadic arity and variadic outputs
41template <typename CircleOp, typename std::enable_if_t<is_VariadicArity<CircleOp>::value &&
44{
45 return CircleOp(1, 1);
46}
47
48// For Circle Nodes which have variadic arity but single output
49template <typename CircleOp,
50 typename std::enable_if_t<is_VariadicArity<CircleOp>::value &&
53{
54 return CircleOp(1);
55}
56
57// For Circle Nodes which have fixed arity
58template <typename CircleOp,
59 typename std::enable_if_t<!is_VariadicArity<CircleOp>::value> * = nullptr>
61{
62 return CircleOp();
63}
64
65// Add fused activation function option to CircleNode if it supports FusedActFunc traits
67{
68 auto node_ = dynamic_cast<CircleNodeMixin<CircleNodeTrait::FusedActFunc> *>(node);
69 if (node_)
70 {
71 node_->fusedActivationFunction(luci::FusedActFunc::RELU);
72 }
73}
74
75// Add padding option to AVERAGE_POOL_2D, CONV_2D, DEPTHWISE_CONV_2D, L2_POOL_2D, MAX_POOL_2D,
76// TRANSPOSE_CONV nodes
78{
79 switch (node->opcode())
80 {
81#define CIRCLE_NODE(OPCODE, CLASS) \
82 case luci::CircleOpcode::OPCODE: \
83 { \
84 auto node_ = dynamic_cast<CLASS *>(node); \
85 node_->padding(Padding::SAME); \
86 break; \
87 }
93 CIRCLE_NODE(TRANSPOSE_CONV, CircleTransposeConv)
94#undef CIRCLE_NODE
95 default:
96 {
97 break;
98 }
99 }
100 return;
101}
102
103// Add mode option to MIRROR_PAD, ROPE nodes
105{
106 switch (node->opcode())
107 {
108#define CIRCLE_NODE(OPCODE, CLASS) \
109 case luci::CircleOpcode::OPCODE: \
110 { \
111 auto node_ = dynamic_cast<CLASS *>(node); \
112 auto mode_ = node_->mode(); \
113 node_->mode(decltype(mode_)(1)); \
114 break; \
115 }
116 CIRCLE_NODE(MIRROR_PAD, CircleMirrorPad)
118#undef CIRCLE_NODE
119 default:
120 {
121 break;
122 }
123 }
124 return;
125}
126
127// Fill dummy values to CircleNode for creating NodeSummary
134
135// Mock Symbol Table for CircleNodeSummaryBuilder
137{
138 std::string lookup(const loco::Node *) const override { return ""; }
139};
140
141// Create NodeSummary using CircleNodeSummaryBuilder and MockSymbolTable
143{
145 MockSymbolTable tbl;
147
148 builder.build(node, &tbl, s);
149 return s;
150}
151
152// Get input names of CircleNode and export as JSON format
154 crew::JsonExport &json_export)
155{
156 std::vector<std::string> arg_names;
157 for (int i = 0; i < node->arity(); i++)
158 {
159 auto args = s.args().at(i);
160 // args : pair(name, value)
161 arg_names.emplace_back(args.first);
162 }
163
164 // s.opname() : "Circle.Opname""
165 auto opname = s.opname().substr(7);
166 // "Opname" : ["arg1", "arg2",...],"
167 json_export.key_val(opname, arg_names, true);
168}
169
170int main(void)
171{
172 std::stringstream ss;
173 crew::JsonExport json_export(ss);
174 // "{"
175 json_export.open_brace();
176#define CIRCLE_NODE(OP, CIRCLE_OP) \
177 { \
178 auto node = CircleNodeCreator<CIRCLE_OP>(); \
179 fill_dummies_for_summary_creation(&node); \
180 auto summary = create_circle_node_summary(&node); \
181 get_input_names_from_summary(&node, summary, json_export); \
182 }
183#define CIRCLE_VNODE(_1, _2)
184#include <luci/IR/CircleNodes.lst>
185#undef CIRCLE_NODE
186#undef CIRCLE_VNODE
187 // Remove last comma from stringstream 'ss'
188 ss.seekp(-2, std::ios_base::end) << '\n';
189 // "}"
190 json_export.close_brace(false);
191 std::cout << ss.str();
192
193 return 0;
194}
#define CIRCLE_NODE(OPCODE, CLASS)
void add_padding_option(CircleNode *node)
auto CircleNodeCreator()
void fill_dummies_for_summary_creation(CircleNode *node)
locop::NodeSummary create_circle_node_summary(CircleNode *node)
int main(void)
void add_fused_actfn_option(CircleNode *node)
void add_mode_option(CircleNode *node)
void get_input_names_from_summary(CircleNode *node, locop::NodeSummary &s, crew::JsonExport &json_export)
void key_val(const std::string &key, const std::string &value, bool cont)
void open_brace(void)
void close_brace(bool cont)
Logical unit of computation.
Definition Node.h:54
virtual uint32_t arity(void) const =0
Return the number of arguments.
ADD_N in Circle.
Definition CircleAddN.h:32
AVERAGE_POOL_2D in Circle.
CONCATENATION in Circle.
CONV_2D in Circle.
CUSTOM in Circle.
DEPTHWISE_CONV_2D in Circle.
IF in Circle.
Definition CircleIf.h:34
L2_POOL_2D in Circle.
MAX_POOL_2D in Circle.
MIRROR_PAD in Circle.
bool build(const loco::Node *node, const locop::SymbolTable *tbl, locop::NodeSummary &s)
PACK in Circle.
Definition CirclePack.h:34
ROPE in Circle.
Definition CircleRoPE.h:33
TRANSPOSE_CONV in Circle.
WHILE in Circle.
Definition CircleWhile.h:34
Symbol Table Interface.
Definition SymbolTable.h:33
virtual CircleOpcode opcode(void) const =0