ONE - On-device Neural Engine
Loading...
Searching...
No Matches
prunner::PModelsRunner Class Reference

PModelsRunner runs partitioned models from input data file and stores output data to a file. More...

#include <PModelsRunner.h>

Public Member Functions

 PModelsRunner ()=default
 
bool load_config (const std::string &filename)
 
void load_inputs (const std::string &input_prefix, int32_t num_inputs)
 
bool run (void)
 
void save_outputs (const std::string &output_file)
 

Detailed Description

PModelsRunner runs partitioned models from input data file and stores output data to a file.

Definition at line 41 of file PModelsRunner.h.

Constructor & Destructor Documentation

◆ PModelsRunner()

prunner::PModelsRunner::PModelsRunner ( )
default

Member Function Documentation

◆ load_config()

bool prunner::PModelsRunner::load_config ( const std::string &  filename)

Definition at line 92 of file PModelsRunner.cpp.

93{
94 if (!crew::read_ini(filename, _pconfig))
95 {
96 std::cerr << "ERROR: Invalid config ini file: '" << filename << "'" << std::endl;
97 return false;
98 }
99
100 for (auto &part : _pconfig.parts)
101 {
102 _models_to_run.push_back(part.model_file);
103 }
104 return true;
105}
bool read_ini(const std::string &path, PConfig &config)
Read config as ini file, return false if failed.
Definition PConfig.cpp:121

References crew::PConfig::parts, and crew::read_ini().

Referenced by entry().

◆ load_inputs()

void prunner::PModelsRunner::load_inputs ( const std::string &  input_prefix,
int32_t  num_inputs 
)

Definition at line 107 of file PModelsRunner.cpp.

108{
109 LOGGER(l);
110
111 auto its = _pconfig.source.inputs.begin();
112 for (int32_t i = 0; i < num_inputs; ++i, ++its)
113 {
114 std::string filename = input_prefix + std::to_string(i);
115
116 INFO(l) << "Load input data: " << filename << std::endl;
117 foder::FileLoader file_loader{filename};
118
119 std::string input_name = *its;
120 _data_stage[input_name] = file_loader.load();
121
122 INFO(l) << "Input: [" << input_name << "], size " << _data_stage[input_name].size()
123 << std::endl;
124 }
125}
#define LOGGER(name)
Definition Log.h:65
#define INFO(name)
Definition Log.h:68
Source source
Definition PConfig.h:39
std::vector< std::string > inputs
Definition PConfig.h:30

References INFO, crew::Part::inputs, LOGGER, and crew::PConfig::source.

Referenced by entry().

◆ run()

bool prunner::PModelsRunner::run ( void  )

Definition at line 147 of file PModelsRunner.cpp.

148{
149 LOGGER(l);
150
151 // for each partitioned model, if the inputs of the model are ready, run the model
152 do
153 {
154 bool found_model = false;
155
156 for (auto it = _models_to_run.begin(); it != _models_to_run.end(); ++it)
157 {
158 auto model_fname = *it;
159
160 INFO(l) << "Check model input ready: " << model_fname << std::endl;
161 if (is_input_ready(model_fname))
162 {
163 found_model = true;
164
165 INFO(l) << "Run model: " << model_fname << std::endl;
166 auto module = import_circle(model_fname);
167
169
170 // Set input
171 const auto input_nodes = loco::input_nodes(module->graph());
172 int32_t num_inputs = static_cast<int32_t>(input_nodes.size());
173 for (int32_t i = 0; i < num_inputs; i++)
174 {
175 const auto *input_node = loco::must_cast<const luci::CircleInput *>(input_nodes[i]);
176
177 auto input_name = input_node->name();
178 assert(_data_stage.find(input_name) != _data_stage.end());
179
180 auto input_data = _data_stage[input_name];
181
182 interpreter.writeInputTensor(input_node, input_data.data(), input_data.size());
183 }
184
185 // Run interpreter
186 interpreter.interpret();
187 INFO(l) << "Run model: " << model_fname << " done" << std::endl;
188
189 // Get output.
190 const auto output_nodes = loco::output_nodes(module->graph());
191 for (uint32_t i = 0; i < module->graph()->outputs()->size(); i++)
192 {
193 const auto *output_node = loco::must_cast<const luci::CircleOutput *>(output_nodes[i]);
194 auto output_name = output_node->name();
195
196 Buffer output_data(tensor_size(output_node));
197
198 interpreter.readOutputTensor(output_node, output_data.data(), output_data.size());
199
200 // There should not exist same output names
201 // TODO check with multiple virtual outputs
202 assert(_data_stage.find(output_name) == _data_stage.end());
203 _data_stage[output_name] = output_data;
204 }
205
206 // We've ran this model, remove from the model list
207 _models_to_run.erase(it);
208 break;
209 }
210 }
211
212 if (not found_model)
213 {
214 std::cerr << "ERROR: model partition or configuration has problems" << std::endl;
215 return false;
216 }
217 } while (not _models_to_run.empty());
218
219 return true;
220}
list input_data
Definition infer.py:29
std::vector< Node * > input_nodes(const Graph *)
Definition Graph.cpp:71
std::vector< Node * > output_nodes(Graph *)
Definition Graph.cpp:101
CircleOutput * output_node(loco::Graph *g, const loco::GraphOutputIndex &index)
Find a CircleOutput node with a given output index.
CircleInput * input_node(loco::Graph *g, const loco::GraphInputIndex &index)
Find a Pull node with a given input index.
std::vector< char > Buffer
NodeName name(void) const

References INFO, loco::input_nodes(), LOGGER, luci::CircleNode::name(), and loco::output_nodes().

Referenced by entry(), and package.infer.session::inference().

◆ save_outputs()

void prunner::PModelsRunner::save_outputs ( const std::string &  output_file)

Definition at line 222 of file PModelsRunner.cpp.

223{
224 LOGGER(l);
225
226 // load source model as we need to get both shape and node name
227 // TODO check for unknown shape
228 auto source_fname = _pconfig.source.model_file;
229
230 INFO(l) << "save_outputs() loading file: " << source_fname << std::endl;
231 auto module = import_circle(source_fname);
232
233 const auto output_nodes = loco::output_nodes(module->graph());
234 for (uint32_t i = 0; i < module->graph()->outputs()->size(); i++)
235 {
236 const auto *output_node = loco::must_cast<const luci::CircleOutput *>(output_nodes[i]);
237
238 auto output_name = output_node->name();
239 INFO(l) << "save_outputs() save output node: " << output_name << std::endl;
240 assert(_data_stage.find(output_name) != _data_stage.end());
241
242 auto tensor_data = _data_stage[output_name];
243 auto output_filename = output_file + std::to_string(i);
244
245 write_file(output_filename, tensor_data.data(), tensor_data.size());
246 save_shape(output_filename + ".shape", output_node);
247 }
248}
std::string model_file
Definition PConfig.h:29

References INFO, LOGGER, crew::Part::model_file, luci::CircleNode::name(), loco::output_nodes(), and crew::PConfig::source.

Referenced by entry().


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