ONE - On-device Neural Engine
Loading...
Searching...
No Matches
dalgona::Dalgona Class Reference

#include <Dalgona.h>

Public Member Functions

 Dalgona ()=default
 
 ~Dalgona ()=default
 
void initialize (const std::string &input_model_path)
 
void runAnalysisWithH5Input (const std::string &input_data_path, const std::string &analysis_path, const std::string &analysis_args)
 
void runAnalysisWithRandomInput (const std::string &analysis_path, const std::string &analysis_args)
 

Detailed Description

Definition at line 30 of file Dalgona.h.

Constructor & Destructor Documentation

◆ Dalgona()

dalgona::Dalgona::Dalgona ( )
explicitdefault

◆ ~Dalgona()

dalgona::Dalgona::~Dalgona ( )
default

Member Function Documentation

◆ initialize()

void dalgona::Dalgona::initialize ( const std::string &  input_model_path)

Definition at line 99 of file Dalgona.cpp.

100{
101 luci::ImporterEx importer;
102 _module = importer.importVerifyModule(input_model_path);
103
104 if (not _module)
105 throw std::runtime_error("ERROR: Failed to load '" + input_model_path + "'");
106
107 // Initialize interpreter
108 _interpreter = std::make_unique<luci_interpreter::Interpreter>(_module.get());
109
110 _hooks = std::make_unique<PythonHooks>(_interpreter.get());
111
112 _interpreter->attachObserver(_hooks.get());
113}
std::unique_ptr< Module > importVerifyModule(const std::string &input_path) const

References luci::ImporterEx::importVerifyModule().

◆ runAnalysisWithH5Input()

void dalgona::Dalgona::runAnalysisWithH5Input ( const std::string &  input_data_path,
const std::string &  analysis_path,
const std::string &  analysis_args 
)

Definition at line 115 of file Dalgona.cpp.

118{
119 py::object scope = py::module::import("__main__").attr("__dict__");
120 _hooks->importAnalysis(analysis_path, scope, analysis_args);
121
122 try
123 {
124 dio::hdf5::HDF5Importer importer(input_data_path);
125 importer.importGroup("value");
126
127 bool is_raw_data = importer.isRawData();
128
129 const auto num_records = importer.numData();
130 if (num_records == 0)
131 throw std::runtime_error("The input data file does not contain any record.");
132
133 const auto input_nodes = loco::input_nodes(_module->graph());
134 const auto num_inputs = input_nodes.size();
135
136 for (int32_t record_idx = 0; record_idx < num_records; record_idx++)
137 {
138 if (num_inputs != static_cast<uint32_t>(importer.numInputs(record_idx)))
139 throw std::runtime_error("Wrong number of inputs.");
140
141 std::cout << "Running " << record_idx << "'th data" << std::endl;
142
143 for (uint32_t input_idx = 0; input_idx < num_inputs; input_idx++)
144 {
145 const auto *input_node = loco::must_cast<const luci::CircleInput *>(input_nodes[input_idx]);
146 assert(input_node->index() == input_idx);
147 checkInputDimension(input_node);
148 std::vector<char> input_data(getByteSize(input_node));
149
150 if (is_raw_data)
151 {
152 // Skip type/shape check for raw data
153 importer.readTensor(record_idx, input_idx, input_data.data(), input_data.size());
154 }
155 else
156 {
158 Shape shape;
159 importer.readTensor(record_idx, input_idx, &dtype, &shape, input_data.data(),
160 input_data.size());
161
162 // Check the type and the shape of the input data is valid
163 verifyTypeShape(input_node, dtype, shape);
164 }
165
166 _interpreter->writeInputTensor(input_node, input_data.data(), input_data.size());
167 }
168
169 _hooks->startNetworkExecution(_module->graph());
170 _interpreter->interpret();
171 _hooks->endNetworkExecution(_module->graph());
172 }
173
174 std::cout << "Finished executing " << num_records << "'th data" << std::endl;
175 _hooks->endAnalysis();
176 }
177 catch (const H5::Exception &e)
178 {
179 H5::Exception::printErrorStack();
180 throw std::runtime_error("HDF5 error occurred.");
181 }
182}
void index(const loco::GraphInputIndex &index)
void verifyTypeShape(const luci::CircleInput *input_node, const DataType &dtype, const Shape &shape)
list input_data
Definition infer.py:29
std::vector< Node * > input_nodes(const Graph *)
Definition Graph.cpp:71
DataType
"scalar" value type
Definition DataType.h:27
CircleInput * input_node(loco::Graph *g, const loco::GraphInputIndex &index)
Find a Pull node with a given input index.
void checkInputDimension(const luci::CircleInput *input)
Definition Utils.cpp:29
Definition Shape.h:28

References dio::hdf5::HDF5Importer::importGroup(), luci::CircleInput::index(), loco::input_nodes(), dio::hdf5::HDF5Importer::isRawData(), dio::hdf5::HDF5Importer::numData(), dio::hdf5::HDF5Importer::numInputs(), and dio::hdf5::HDF5Importer::readTensor().

◆ runAnalysisWithRandomInput()

void dalgona::Dalgona::runAnalysisWithRandomInput ( const std::string &  analysis_path,
const std::string &  analysis_args 
)

Definition at line 184 of file Dalgona.cpp.

186{
187 py::object scope = py::module::import("__main__").attr("__dict__");
188 _hooks->importAnalysis(analysis_path, scope, analysis_args);
189
190 const auto input_nodes = loco::input_nodes(_module->graph());
191 const auto num_inputs = input_nodes.size();
192
193 for (uint32_t input_idx = 0; input_idx < num_inputs; input_idx++)
194 {
195 const auto *input_node = loco::must_cast<const luci::CircleInput *>(input_nodes[input_idx]);
196 assert(input_node->index() == input_idx);
197 checkInputDimension(input_node);
198
199 uint32_t num_elems = numElements(input_node);
200 switch (input_node->dtype())
201 {
202 case DataType::FLOAT32:
203 {
204 // Synced with record-minmax (-5,5)
206 _interpreter->writeInputTensor(input_node, input_data.data(),
207 input_data.size() * sizeof(float));
208 break;
209 }
210 case DataType::U8:
211 {
212 auto input_data = genRandomIntData<uint8_t>(num_elems, std::numeric_limits<uint8_t>::min(),
213 std::numeric_limits<uint8_t>::max());
214 _interpreter->writeInputTensor(input_node, input_data.data(),
215 input_data.size() * sizeof(uint8_t));
216 break;
217 }
218 case DataType::S16:
219 {
220 auto input_data = genRandomIntData<int16_t>(num_elems, std::numeric_limits<int16_t>::min(),
221 std::numeric_limits<int16_t>::max());
222 _interpreter->writeInputTensor(input_node, input_data.data(),
223 input_data.size() * sizeof(int16_t));
224 break;
225 }
226 case DataType::S32:
227 {
228 // Synced with record-minmax (0, 100)
229 auto input_data = genRandomIntData<int32_t>(num_elems, 0, 100);
230 _interpreter->writeInputTensor(input_node, input_data.data(),
231 input_data.size() * sizeof(int32_t));
232 break;
233 }
234 case DataType::S64:
235 {
236 // Synced with record-minmax (0, 100)
237 auto input_data = genRandomIntData<int64_t>(num_elems, 0, 100);
238 _interpreter->writeInputTensor(input_node, input_data.data(),
239 input_data.size() * sizeof(int64_t));
240 break;
241 }
242 case DataType::BOOL:
243 {
244 // Bool is represented as uint8 (0 or 1)
245 auto input_data = genRandomIntData<uint8_t>(num_elems, 0, 1);
246 _interpreter->writeInputTensor(input_node, input_data.data(),
247 input_data.size() * sizeof(uint8_t));
248 break;
249 }
250 default:
251 throw std::runtime_error("Unsupported input data type in " + input_node->name());
252 }
253 }
254
255 _hooks->startNetworkExecution(_module->graph());
256 _interpreter->interpret();
257 _hooks->endNetworkExecution(_module->graph());
258
259 std::cout << "Finished executing a random input" << std::endl;
260 _hooks->endAnalysis();
261}
std::vector< float > genRandomFloatData(uint32_t num_elements, float min, float max)
uint32_t numElements(const luci::CircleNode *node)
Definition Utils.cpp:41
uint64_t num_elems(const nnfw_tensorinfo *tensor_info)
Get the total number of elements in nnfw_tensorinfo->dims.
NodeName name(void) const

References dalgona::genRandomFloatData(), luci::CircleInput::index(), loco::input_nodes(), luci::CircleNode::name(), and num_elems().


The documentation for this class was generated from the following files: