ONE - On-device Neural Engine
Loading...
Searching...
No Matches
nnkit-run.cpp File Reference
#include <nnkit/CmdlineArguments.h>
#include <nnkit/VectorArguments.h>
#include <nnkit/BackendPlugin.h>
#include <nnkit/Action.h>
#include <memory>
#include <dlfcn.h>
#include <assert.h>
#include <map>
#include <iostream>

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 148 of file nnkit-run.cpp.

149{
150 // Usage:
151 // [Command] --backend [Backend module path] --backend-arg ... --backend-arg ...
152 // --pre [Action module path] --pre-arg ... --pre-arg ...
153 // --post [Action module path] --post-arg ... --post-arg ...
154
155 // Argument sections
156 //
157 // NOTE Command-line arguments should include one backend section, and may include multiple
158 // pre/post action sections.
159 struct Sections
160 {
161 std::unique_ptr<BackendSection> backend;
162 std::vector<ActionSection> pre;
163 std::vector<ActionSection> post;
164 };
165
166 Sections sections;
167
168 // Simple argument parser (based on map)
169 std::map<std::string, std::function<void(const std::string &arg)>> argparse;
170
171 argparse["--backend"] = [&sections](const std::string &tag) {
172 sections.backend = std::make_unique<BackendSection>(tag);
173 };
174
175 argparse["--backend-arg"] = [&sections](const std::string &arg) {
176 sections.backend->append(arg);
177 };
178
179 argparse["--pre"] = [&sections](const std::string &tag) { sections.pre.emplace_back(tag); };
180
181 argparse["--pre-arg"] = [&sections](const std::string &arg) { sections.pre.back().append(arg); };
182
183 argparse["--post"] = [&sections](const std::string &tag) { sections.post.emplace_back(tag); };
184
185 argparse["--post-arg"] = [&sections](const std::string &arg) {
186 sections.post.back().append(arg);
187 };
188
189 if (argc < 2)
190 {
191 std::cerr << "Usage:" << std::endl
192 << "[Command] --backend [Backend module path] "
193 << "--backend-arg [Backend argument] ..." << std::endl
194 << " --pre [Pre-Action module path] "
195 << "--pre-arg [Pre-Action argument] ..." << std::endl
196 << " --post [Post-Action module path] "
197 << "--post-arg [Post-Action argument] ..." << std::endl;
198 return 255;
199 }
200
201 for (int n = 1; n < argc; n += 2)
202 {
203 const std::string tag{argv[n]};
204 const std::string arg{argv[n + 1]};
205
206 auto it = argparse.find(tag);
207
208 if (it == argparse.end())
209 {
210 std::cerr << "Option '" << tag << "' is not supported" << std::endl;
211 return 255;
212 }
213
214 it->second(arg);
215 }
216
217 // we need a backend
218 if (sections.backend == nullptr)
219 {
220 std::cerr << "Error: Backend is required. Provide with [--backend]" << std::endl;
221 return 255;
222 }
223
224 // Initialize a backend
225 auto backend = sections.backend->initialize();
226
227 // Initialize pre actions
228 std::vector<std::unique_ptr<nnkit::Action>> pre_actions;
229
230 for (const auto &section : sections.pre)
231 {
232 pre_actions.emplace_back(section.initialize());
233 }
234
235 // Initialize post actions
236 std::vector<std::unique_ptr<nnkit::Action>> post_actions;
237
238 for (const auto &section : sections.post)
239 {
240 post_actions.emplace_back(section.initialize());
241 }
242
243 //
244 // Run inference
245 //
246 backend->prepare([&pre_actions](nnkit::TensorContext &ctx) {
247 // Run pre-actions on prepared tensor context
248 for (auto &action : pre_actions)
249 {
250 action->run(ctx);
251 }
252 });
253
254 backend->run();
255
256 backend->teardown([&post_actions](nnkit::TensorContext &ctx) {
257 // Run post-actions before teardown
258 for (auto &action : post_actions)
259 {
260 action->run(ctx);
261 }
262 });
263
264 return 0;
265}
std::vector< Section > Sections
Definition PConfigIni.h:36