ONE - On-device Neural Engine
Loading...
Searching...
No Matches
dalgona Namespace Reference

Data Structures

class  Dalgona
 
class  PostOperatorHook
 
class  PreOperatorHook
 
class  PythonHooks
 

Functions

std::vector< float > genRandomFloatData (uint32_t num_elements, float min, float max)
 
template<typename T >
std::vector< T > genRandomIntData (uint32_t num_elements, T min, T max)
 
const std::string toString (luci::CircleOpcode opcode)
 
const std::string toString (luci::FusedActFunc fused_act)
 
py::object none ()
 
std::vector< py::dict > inputsPyArray (const luci::CircleNode *node, luci_interpreter::Interpreter *interpreter)
 
std::vector< py::dict > outputsPyArray (const luci::CircleNode *node, luci_interpreter::Interpreter *interpreter)
 
py::dict outputPyArray (const luci::CircleNode *node, luci_interpreter::Interpreter *interpreter)
 
bool multi_out_node (const luci::CircleNode *node)
 
template<typename... Args>
void pySafeCall (py::object func, Args... args)
 

Function Documentation

◆ genRandomFloatData()

std::vector< float > dalgona::genRandomFloatData ( uint32_t  num_elements,
float  min,
float  max 
)

Definition at line 26 of file RandomUtils.cpp.

27{
28 if (min > max)
29 throw std::invalid_argument("min is greater than max");
30
31 std::random_device rd;
32 std::mt19937 gen(rd());
33 std::uniform_real_distribution<> dist(min, max);
34 std::vector<float> buffer(num_elements);
35
36 // Write random data
37 for (auto &iter : buffer)
38 iter = static_cast<float>(dist(gen));
39
40 return buffer;
41}

Referenced by dalgona::Dalgona::runAnalysisWithRandomInput().

◆ genRandomIntData()

template<typename T >
std::vector< T > dalgona::genRandomIntData ( uint32_t  num_elements,
min,
max 
)

Definition at line 28 of file RandomUtils.h.

29{
30 if (min > max)
31 throw std::invalid_argument("min is greater than max");
32
33 std::random_device rd;
34 std::mt19937 gen(rd());
35 std::uniform_int_distribution<T> dist(min, max);
36 std::vector<T> buffer(num_elements);
37
38 // Write random data
39 for (auto &iter : buffer)
40 iter = dist(gen);
41
42 return buffer;
43}

◆ inputsPyArray()

std::vector< py::dict > dalgona::inputsPyArray ( const luci::CircleNode node,
luci_interpreter::Interpreter interpreter 
)

Definition at line 109 of file Utils.cpp.

111{
112 assert(node != nullptr); // FIX_CALLER_UNLESS
113 assert(interpreter != nullptr); // FIX_CALLER_UNLESS
114
115 std::vector<py::dict> inputs;
116 for (uint32_t i = 0; i < node->arity(); ++i)
117 {
118 const auto input_tensor = interpreter->getTensor(node->arg(i));
119 auto circle_node = static_cast<luci::CircleNode *>(node->arg(i));
120
121 // skip invalid inputs (e.g., non-existing bias in TCONV)
122 if (circle_node->opcode() == luci::CircleOpcode::CIRCLEOUTPUTEXCLUDE)
123 continue;
124
125 auto py_input =
126 py::dict("name"_a = circle_node->name(), "data"_a = numpyArray(input_tensor),
127 "quantparam"_a = quantparam(input_tensor),
128 "is_const"_a = circle_node->opcode() == luci::CircleOpcode::CIRCLECONST);
129 inputs.push_back(py_input);
130 }
131 return inputs;
132}
virtual Node * arg(uint32_t N) const =0
Access N-th argument node.
virtual uint32_t arity(void) const =0
Return the number of arguments.

References loco::Node::arg(), and loco::Node::arity().

Referenced by dalgona::PostOperatorHook::visit(), and dalgona::PreOperatorHook::visit().

◆ multi_out_node()

bool dalgona::multi_out_node ( const luci::CircleNode node)

Definition at line 175 of file Utils.cpp.

176{
177 switch (node->opcode())
178 {
179 // TODO Update this list when new Op is added
180 // Tip: grep "public GraphBuilderMultiOutput" in luci/import
181 case luci::CircleOpcode::BIDIRECTIONAL_SEQUENCE_LSTM:
182 case luci::CircleOpcode::CUSTOM:
183 case luci::CircleOpcode::IF:
184 case luci::CircleOpcode::NON_MAX_SUPPRESSION_V4:
185 case luci::CircleOpcode::NON_MAX_SUPPRESSION_V5:
186 case luci::CircleOpcode::SPLIT:
187 case luci::CircleOpcode::SPLIT_V:
188 case luci::CircleOpcode::TOPK_V2:
189 case luci::CircleOpcode::UNIQUE:
190 case luci::CircleOpcode::UNPACK:
191 return true;
192 default:
193 return false;
194 }
195}
virtual CircleOpcode opcode(void) const =0

References luci::CircleNode::opcode().

Referenced by dalgona::PostOperatorHook::visit().

◆ none()

py::object dalgona::none ( )

Definition at line 107 of file Utils.cpp.

107{ return py::none(); }

Referenced by dalgona::PostOperatorHook::visit(), and dalgona::PreOperatorHook::visit().

◆ outputPyArray()

py::dict dalgona::outputPyArray ( const luci::CircleNode node,
luci_interpreter::Interpreter interpreter 
)

Definition at line 160 of file Utils.cpp.

161{
162 assert(node != nullptr); // FIX_CALLER_UNLESS
163 assert(interpreter != nullptr); // FIX_CALLER_UNLESS
164
165 const auto tensor = interpreter->getTensor(node);
166
167 THROW_UNLESS(tensor != nullptr, "Null tensor detected in " + node->name());
168
169 auto py_output = py::dict("name"_a = node->name(), "data"_a = numpyArray(tensor),
170 "quantparam"_a = quantparam(tensor),
171 "is_const"_a = node->opcode() == luci::CircleOpcode::CIRCLECONST);
172 return py_output;
173}
#define THROW_UNLESS(COND, MSG)
Definition Utils.cpp:33
NodeName name(void) const

References luci::CircleNode::name(), luci::CircleNode::opcode(), and THROW_UNLESS.

Referenced by dalgona::PythonHooks::endNetworkExecution(), dalgona::PythonHooks::startNetworkExecution(), and dalgona::PostOperatorHook::visit().

◆ outputsPyArray()

std::vector< py::dict > dalgona::outputsPyArray ( const luci::CircleNode node,
luci_interpreter::Interpreter interpreter 
)

Definition at line 134 of file Utils.cpp.

136{
137 std::vector<py::dict> outputs;
138 for (auto succ : loco::succs(node))
139 {
140 const auto output_tensor = interpreter->getTensor(succ);
141 auto circle_node = static_cast<luci::CircleNode *>(succ);
142
143 auto opcode_str = toString(circle_node->opcode());
144 // Check if node is a multi-output node
145 // Assumption: Multi-output virtual nodes have 'Out' prefix
146 // TODO Fix this if the assumption changes
147 THROW_UNLESS(opcode_str.substr(opcode_str.length() - 3) == "Out",
148 "Invalid output detected in " + node->name());
149
150 auto py_output =
151 py::dict("name"_a = circle_node->name(), "data"_a = numpyArray(output_tensor),
152 "quantparam"_a = quantparam(output_tensor),
153 "is_const"_a = circle_node->opcode() == luci::CircleOpcode::CIRCLECONST);
154 outputs.push_back(py_output);
155 }
156 return outputs;
157}
const std::string toString(luci::CircleOpcode opcode)

References luci::CircleNode::name(), loco::succs(), THROW_UNLESS, and toString().

Referenced by dalgona::PostOperatorHook::visit().

◆ pySafeCall()

◆ toString() [1/2]

const std::string dalgona::toString ( luci::CircleOpcode  opcode)

Definition at line 26 of file StringUtils.cpp.

27{
28 static const char *names[] = {
29#define CIRCLE_NODE(OPCODE, CIRCLE_CLASS) #CIRCLE_CLASS,
30#define CIRCLE_VNODE(OPCODE, CIRCLE_CLASS) #CIRCLE_CLASS,
31#include <luci/IR/CircleNodes.lst>
32#undef CIRCLE_NODE
33#undef CIRCLE_VNODE
34 };
35
36 auto const node_name = names[static_cast<int>(opcode)];
37
38 assert(std::string(node_name).substr(0, 6) == "Circle"); // FIX_ME_UNLESS
39
40 // Return substring of class name ("Circle" is sliced out)
41 // Ex: Return "Conv2D" for "CircleConv2D" node
42 return std::string(node_name).substr(6);
43}

Referenced by outputsPyArray(), dalgona::PostOperatorHook::visit(), dalgona::PreOperatorHook::visit(), dalgona::PostOperatorHook::visit(), dalgona::PreOperatorHook::visit(), dalgona::PostOperatorHook::visit(), dalgona::PreOperatorHook::visit(), dalgona::PostOperatorHook::visit(), dalgona::PreOperatorHook::visit(), dalgona::PostOperatorHook::visit(), dalgona::PreOperatorHook::visit(), dalgona::PostOperatorHook::visit(), and dalgona::PreOperatorHook::visit().

◆ toString() [2/2]

const std::string dalgona::toString ( luci::FusedActFunc  fused_act)

Definition at line 45 of file StringUtils.cpp.

46{
47 switch (fused_act)
48 {
50 return std::string("undefined");
52 return std::string("none");
54 return std::string("relu");
56 return std::string("relu_n1_to_1");
58 return std::string("relu6");
60 return std::string("tanh");
62 return std::string("sign_bit");
63 default:
64 throw std::runtime_error("Unsupported activation function");
65 }
66}

References luci::NONE, luci::RELU, luci::RELU6, luci::RELU_N1_TO_1, luci::SIGN_BIT, luci::TANH, and luci::UNDEFINED.