ONE - On-device Neural Engine
Loading...
Searching...
No Matches
onert::api::Session Struct Reference

#include <Session.h>

Public Member Functions

 ~Session ()
 
NNFW_STATUS load_model_from_path (const char *path)
 
NNFW_STATUS prepare ()
 
NNFW_STATUS run ()
 
NNFW_STATUS run_async ()
 
NNFW_STATUS await ()
 
NNFW_STATUS set_input (uint32_t index, NNFW_TYPE type, const void *buffer, size_t length)
 
NNFW_STATUS set_output (uint32_t index, NNFW_TYPE type, void *buffer, size_t length)
 
NNFW_STATUS input_size (uint32_t *number)
 
NNFW_STATUS output_size (uint32_t *number)
 
NNFW_STATUS set_input_layout (uint32_t index, NNFW_LAYOUT layout)
 
NNFW_STATUS set_output_layout (uint32_t index, NNFW_LAYOUT layout)
 
NNFW_STATUS set_input_type (uint32_t index, NNFW_TYPE type)
 
NNFW_STATUS set_output_type (uint32_t index, NNFW_TYPE type)
 
NNFW_STATUS set_input_tensorinfo (uint32_t index, const nnfw_tensorinfo *ti)
 
NNFW_STATUS input_tensorinfo (uint32_t index, nnfw_tensorinfo *ti)
 
NNFW_STATUS output_tensorinfo (uint32_t index, nnfw_tensorinfo *ti)
 
NNFW_STATUS set_available_backends (const char *backends)
 
NNFW_STATUS set_workspace (const char *dir)
 
NNFW_STATUS configure_signature (const char *signature)
 
NNFW_STATUS set_signature_run (const char *signature)
 
NNFW_STATUS get_last_error_message (char *buffer, size_t length) const
 
NNFW_STATUS deprecated (const char *msg)
 
NNFW_STATUS set_config (const char *key, const char *value)
 
NNFW_STATUS get_config (const char *key, char *value, size_t value_size)
 
NNFW_STATUS load_circle_from_buffer (uint8_t *buffer, size_t size)
 
NNFW_STATUS get_output (uint32_t index, nnfw_tensorinfo *out_info, const void **out_buffer)
 
NNFW_STATUS register_custom_operation (const std::string &id, nnfw_custom_eval eval_func)
 
NNFW_STATUS input_tensorindex (const char *tensorname, uint32_t *index)
 
NNFW_STATUS output_tensorindex (const char *tensorname, uint32_t *index)
 
NNFW_STATUS run_with_auto_compilation (const char *target, NNFW_CODEGEN_PREF pref)
 
NNFW_STATUS set_odc_param_minmax_records_count (int minmax_records_count)
 
NNFW_STATUS delete_odc_minmax_file ()
 
NNFW_STATUS set_backends_per_operation (const char *backend_settings)
 Set backends with string-encoded mapping from operation index to backend type (cpu, acl_cl)
 
NNFW_STATUS train_get_traininfo (nnfw_train_info *info)
 
NNFW_STATUS train_set_traininfo (const nnfw_train_info *info)
 
NNFW_STATUS train_prepare ()
 
NNFW_STATUS train_input_tensorinfo (uint32_t index, nnfw_tensorinfo *ti)
 
NNFW_STATUS train_expected_tensorinfo (uint32_t index, nnfw_tensorinfo *ti)
 
NNFW_STATUS train_set_input (uint32_t index, const void *input, const nnfw_tensorinfo *input_tensorinfo)
 
NNFW_STATUS train_set_expected (uint32_t index, const void *expected, const nnfw_tensorinfo *expected_tensorinfo)
 
NNFW_STATUS train_set_output (uint32_t index, NNFW_TYPE type, void *buffer, size_t length)
 
NNFW_STATUS train_run (bool update_weights)
 
NNFW_STATUS train_get_loss (uint32_t index, float *loss)
 
NNFW_STATUS train_export_circle (const char *path)
 
NNFW_STATUS train_export_circleplus (const char *path)
 
NNFW_STATUS train_import_checkpoint (const char *path)
 
NNFW_STATUS train_export_checkpoint (const char *path)
 
NNFW_STATUS set_quantization_type (NNFW_QUANTIZE_TYPE qtype)
 
NNFW_STATUS set_quantized_model_path (const char *path)
 
NNFW_STATUS quantize ()
 
NNFW_STATUS set_codegen_model_path (const char *path)
 
NNFW_STATUS codegen (const char *target, NNFW_CODEGEN_PREF pref)
 
NNFW_STATUS set_prepare_config (const NNFW_PREPARE_CONFIG key, const char *value)
 
NNFW_STATUS reset_prepare_config ()
 
NNFW_STATUS set_execute_config (const NNFW_RUN_CONFIG key, const char *value)
 
NNFW_STATUS reset_execute_config ()
 

Static Public Member Functions

static NNFW_STATUS create (Session **session)
 Factory method. It creates and initialize Session.
 

Detailed Description

Definition at line 41 of file Session.h.

Constructor & Destructor Documentation

◆ ~Session()

onert::api::Session::~Session ( )
default

Member Function Documentation

◆ await()

NNFW_STATUS onert::api::Session::await ( )

Definition at line 522 of file Session.cc.

523{
524 if (!isStateRunning())
525 {
526 setLastErrorMessage(
527 "Error during Session::run_await : run_await should be called after run_async");
528 return NNFW_STATUS_ERROR;
529 }
530
531 _execution->waitFinish();
532
533 _state = State::FINISHED_RUN;
535}
@ NNFW_STATUS_NO_ERROR
Definition onert-micro.h:88
@ NNFW_STATUS_ERROR
Definition onert-micro.h:93

References NNFW_STATUS_ERROR, and NNFW_STATUS_NO_ERROR.

◆ codegen()

NNFW_STATUS onert::api::Session::codegen ( const char *  target,
NNFW_CODEGEN_PREF  pref 
)

Definition at line 2235 of file Session.cc.

2236{
2237 try
2238 {
2239 if (isStateInitialized() || isStateRunning())
2240 {
2241 setLastErrorMessage("Error during Session::codegen : Invalid state");
2243 }
2244
2245 std::string target_str{target};
2246 if (target_str.empty() || target_str.size() < 5 ||
2247 target_str.substr(target_str.size() - 4) != "-gen")
2248 {
2249 setLastErrorMessage("Error during Session::codegen : Invalid target");
2250 return NNFW_STATUS_ERROR;
2251 }
2252
2253 onert::odc::CodegenPreference codegen_pref;
2254 switch (pref)
2255 {
2258 break;
2261 break;
2264 break;
2267 break;
2268 default:
2269 setLastErrorMessage("Error during Session::codegen : Invalid preference");
2270 return NNFW_STATUS_ERROR;
2271 }
2272
2273 assert(_codegen_manager != nullptr);
2274 auto export_model_path = std::filesystem::path(_codegen_manager->exportModelPath());
2275 const auto model_type = target_str.substr(0, target_str.size() - 4);
2276 // If the export_model_path is not set, it generates a compiled model path
2277 // automatically.
2278 if (export_model_path.empty())
2279 {
2280 // The compiled model path is the same directory of the original model/package with
2281 // target backend extension.
2282 export_model_path = _model_path.replace_extension(model_type);
2283 _codegen_manager->exportModelPath(export_model_path.string());
2284 }
2285
2286 _codegen_manager->codegen(_model_path, target, codegen_pref);
2287
2288 // Replace model
2289 // TODO Support buffer replace, not file reload
2290 return loadModelFile(export_model_path, model_type);
2291 }
2292 catch (const std::exception &e)
2293 {
2294 setLastErrorMessage("Error during Session::codegen : " + std::string(e.what()));
2295 return NNFW_STATUS_ERROR;
2296 }
2297}
Option< std::string > target(optname("--target"), overview("select target language to emit for given architecture." "Valid values are '" NNC_TARGET_ARM_CPP "', '" NNC_TARGET_X86_CPP "', '" NNC_TARGET_ARM_GPU_CPP "', '" NNC_TARGET_INTERPRETER "'"), std::string(), optional(false), optvalues(NNC_TARGET_ARM_CPP "," NNC_TARGET_X86_CPP "," NNC_TARGET_ARM_GPU_CPP "," NNC_TARGET_INTERPRETER), nullptr, separators("="))
Definition Options.h:47
@ NNFW_CODEGEN_PREF_DEFAULT
@ NNFW_CODEGEN_PREF_MEMORY_FIRST
@ NNFW_CODEGEN_PREF_COMPILE_TIME_FIRST
@ NNFW_CODEGEN_PREF_PERFORMANCE_FIRST
@ NNFW_STATUS_INVALID_STATE
Definition onert-micro.h:97

References onert::odc::CODEGEN_PREF_COMPILE_TIME_FIRST, onert::odc::CODEGEN_PREF_DEFAULT, onert::odc::CODEGEN_PREF_MEMORY_FIRST, onert::odc::CODEGEN_PREF_PERFORMANCE_FIRST, NNFW_CODEGEN_PREF_COMPILE_TIME_FIRST, NNFW_CODEGEN_PREF_DEFAULT, NNFW_CODEGEN_PREF_MEMORY_FIRST, NNFW_CODEGEN_PREF_PERFORMANCE_FIRST, NNFW_STATUS_ERROR, and NNFW_STATUS_INVALID_STATE.

◆ configure_signature()

NNFW_STATUS onert::api::Session::configure_signature ( const char *  signature)

Definition at line 1073 of file Session.cc.

1074{
1075 if (!signature)
1076 {
1077 setLastErrorMessage("Error during Session::configure_signature : signature is NULL");
1079 }
1080
1081 if (!isStateModelLoaded())
1082 {
1083 setLastErrorMessage("Error during Session::configure_signature : Invalid state");
1085 }
1086
1087 for (const auto &[subg_idx, sig_str] : _signature_map)
1088 {
1089 if (sig_str == std::string(signature))
1090 {
1091 _selected_signature = subg_idx;
1092
1093 return NNFW_STATUS_NO_ERROR;
1094 }
1095 }
1096
1097 setLastErrorMessage("Error during Session::configure_signature : Cannot find signature \"" +
1098 std::string(signature) + "\"");
1099 return NNFW_STATUS_ERROR;
1100}
@ NNFW_STATUS_UNEXPECTED_NULL
Definition onert-micro.h:95

References NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, NNFW_STATUS_NO_ERROR, and NNFW_STATUS_UNEXPECTED_NULL.

◆ create()

NNFW_STATUS onert::api::Session::create ( Session **  session)
static

Factory method. It creates and initialize Session.

Note
Use factory instead of constructor to get status

Definition at line 231 of file Session.cc.

232{
233 if (session == nullptr)
235 try
236 {
237 auto new_session = std::unique_ptr<Session>(new Session());
238 new_session->_kernel_registry = std::make_shared<onert::api::CustomKernelRegistry>();
239 *session = new_session.release();
240 }
241 catch (const std::bad_alloc &e)
242 {
243 // TODO: Do not write to std::cerr in library code
244 std::cerr << "Error during session creation" << std::endl;
245 *session = nullptr; // Set nullptr on error to keep the old behavior
247 }
248 catch (const std::exception &e)
249 {
250 // TODO: Do not write to std::cerr in library code
251 std::cerr << "Error during session initialization : " << e.what() << std::endl;
252 *session = nullptr; // Set nullptr on error to keep the old behavior
253 return NNFW_STATUS_ERROR;
254 }
256}
SessionID session(const coco::Module *m)
Definition Session.cpp:48
@ NNFW_STATUS_OUT_OF_MEMORY
Definition onert-micro.h:99

References NNFW_STATUS_ERROR, NNFW_STATUS_NO_ERROR, NNFW_STATUS_OUT_OF_MEMORY, and NNFW_STATUS_UNEXPECTED_NULL.

Referenced by nnfw_create_session().

◆ delete_odc_minmax_file()

NNFW_STATUS onert::api::Session::delete_odc_minmax_file ( )

Definition at line 2398 of file Session.cc.

2399{
2400 if (isStateRunning())
2401 {
2402 setLastErrorMessage("Error during Session::delete_odc_minmax_file : Invalid state");
2404 }
2405
2406 if (_quant_manager->deleteMinMaxFile())
2407 return NNFW_STATUS_NO_ERROR;
2408
2409 setLastErrorMessage("Error during Session::delete_odc_minmax_file : Could not delete file");
2410 return NNFW_STATUS_ERROR;
2411}

References NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, and NNFW_STATUS_NO_ERROR.

◆ deprecated()

NNFW_STATUS onert::api::Session::deprecated ( const char *  msg)

Definition at line 1145 of file Session.cc.

1146{
1147 setLastErrorMessage(msg);
1149}
@ NNFW_STATUS_DEPRECATED_API

References NNFW_STATUS_DEPRECATED_API.

◆ get_config()

NNFW_STATUS onert::api::Session::get_config ( const char *  key,
char *  value,
size_t  value_size 
)

Definition at line 1281 of file Session.cc.

1282{
1283 if (!isStateModelLoaded())
1284 {
1285 setLastErrorMessage("Error during Session::get_config : Model is not loaded");
1287 }
1288
1289 if (!key)
1290 {
1291 setLastErrorMessage("Error during Session::get_config : key is NULL");
1293 }
1294
1295 if (!value)
1296 {
1297 setLastErrorMessage("Error during Session::get_config : value is NULL");
1299 }
1300
1301 auto check_boundary = [](size_t dest_size, std::string &src) {
1302 if (dest_size < src.length() + 1 /* for '\0' */)
1303 return false;
1304 return true;
1305 };
1306
1307 const std::string skey = key;
1308
1309 if (skey == onert::util::config::BACKENDS)
1310 {
1311 if (_coptions->backend_list.size() == 0)
1312 return NNFW_STATUS_NO_ERROR; // no setting backend is not an error of get_config_str()
1313
1314 auto str =
1315 nnfw::misc::join(_coptions->backend_list.begin(), _coptions->backend_list.end(), ";");
1316
1317 if (!check_boundary(value_size, str))
1318 {
1319 setLastErrorMessage(
1320 "Error during Session::get_config : Buffer is too small to copy backends");
1321 return NNFW_STATUS_ERROR;
1322 }
1323
1324 strncpy(value, str.c_str(), value_size);
1325 }
1326 else if (skey == onert::util::config::EXECUTOR)
1327 {
1328 if (!check_boundary(value_size, _coptions->executor))
1329 {
1330 setLastErrorMessage(
1331 "Error during Session::get_config : Buffer is too small to copy executor");
1332 return NNFW_STATUS_ERROR;
1333 }
1334
1335 strncpy(value, _coptions->executor.c_str(), _coptions->executor.length());
1336 }
1337 else
1338 {
1339 setLastErrorMessage("Error during Session::get_config : Unknown config key");
1340 return NNFW_STATUS_ERROR;
1341 }
1342
1343 return NNFW_STATUS_NO_ERROR;
1344}
str
Definition infer.py:18
std::string join(InputIt first, InputIt last, const std::string &concat)

References nnfw::misc::join(), NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, NNFW_STATUS_NO_ERROR, and NNFW_STATUS_UNEXPECTED_NULL.

◆ get_last_error_message()

NNFW_STATUS onert::api::Session::get_last_error_message ( char *  buffer,
size_t  length 
) const

Definition at line 1131 of file Session.cc.

1132{
1133 if (!buffer)
1134 {
1136 }
1137 if (length < _last_error_message.size() + 1)
1138 {
1140 }
1141 strncpy(buffer, _last_error_message.c_str(), length);
1142 return NNFW_STATUS_NO_ERROR;
1143}
@ NNFW_STATUS_INSUFFICIENT_OUTPUT_SIZE

References NNFW_STATUS_INSUFFICIENT_OUTPUT_SIZE, NNFW_STATUS_NO_ERROR, and NNFW_STATUS_UNEXPECTED_NULL.

◆ get_output()

NNFW_STATUS onert::api::Session::get_output ( uint32_t  index,
nnfw_tensorinfo out_info,
const void **  out_buffer 
)

Definition at line 963 of file Session.cc.

964{
965 if (ti == nullptr)
966 {
967 setLastErrorMessage("Error during Session::get_output : tensorinfo is NULL");
969 }
970
971 if (out_buffer == nullptr)
972 {
973 setLastErrorMessage("Error during Session::get_output : output buffer is NULL");
975 }
976
977 if (!isStateFinishedRun())
978 {
979 setLastErrorMessage("Error during Session::get_output : Invalid state");
981 }
982
983 try
984 {
985 if (index >= getOutputSize())
986 {
987 setLastErrorMessage("Error during Session::get_output : index is out of range : " +
988 std::to_string(index) + " >= " + std::to_string(getOutputSize()));
989 return NNFW_STATUS_ERROR;
990 }
991
992 if (!_coptions->internal_output_alloc)
993 {
994 setLastErrorMessage(
995 "Error during Session::get_output : internal output allocation is not enabled : Call "
996 "nnfw_set_prepare_config(session, NNFW_PREPARE_CONFIG_ENABLE_INTERNAL_OUTPUT_ALLOC, "
997 "\"true\") before nnfw_prepare()");
998 return NNFW_STATUS_ERROR;
999 }
1000
1001 auto io_index = onert::ir::IOIndex{index};
1002 const auto &info = _execution->outputInfo(io_index);
1003 const auto &shape = info.shape();
1004 const auto &dtype = info.typeInfo().type();
1005 fillTensorInfo(ti, shape, dtype);
1006
1007 *out_buffer = _execution->outputBuffer(io_index);
1008 }
1009 catch (const std::exception &e)
1010 {
1011 setLastErrorMessage("Error during Session::get_output : " + std::string(e.what()));
1012 return NNFW_STATUS_ERROR;
1013 }
1014
1015 return NNFW_STATUS_NO_ERROR;
1016}
A wrapper class for unsigned integral Index NOTE : Max value of the underlying type is used as the in...
Definition Index.h:37
volatile const char info[]
loco::GraphInputIndex index(const TFPlaceholder *node)
Definition TFNode.cpp:54

References info, NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, NNFW_STATUS_NO_ERROR, and NNFW_STATUS_UNEXPECTED_NULL.

◆ input_size()

NNFW_STATUS onert::api::Session::input_size ( uint32_t *  number)

Definition at line 590 of file Session.cc.

591{
592 if (isStateInitialized())
593 {
594 setLastErrorMessage("Error during Session::input_size : Model is not loaded");
596 }
597
598 if (number == nullptr)
599 {
600 setLastErrorMessage("Error during Session::input_size : number is NULL");
602 }
603
604 try
605 {
606 *number = getInputSize();
607 }
608 catch (const std::exception &e)
609 {
610 setLastErrorMessage("Error during Session::input_size : " + std::string(e.what()));
611 return NNFW_STATUS_ERROR;
612 }
614}
int number
Definition jpeg2hdf5.py:87

References NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, NNFW_STATUS_NO_ERROR, and NNFW_STATUS_UNEXPECTED_NULL.

◆ input_tensorindex()

NNFW_STATUS onert::api::Session::input_tensorindex ( const char *  tensorname,
uint32_t *  index 
)

Definition at line 1447 of file Session.cc.

1448{
1449 return getTensorIndexImpl(*primary_subgraph(), tensorname, index, true);
1450}

◆ input_tensorinfo()

NNFW_STATUS onert::api::Session::input_tensorinfo ( uint32_t  index,
nnfw_tensorinfo ti 
)

Definition at line 869 of file Session.cc.

870{
871 if (isStateInitialized())
872 {
873 setLastErrorMessage("Error during Session::input_tensorinfo : Model is not loaded");
875 }
876
877 try
878 {
879 if (ti == nullptr)
880 {
881 setLastErrorMessage("Error during Session::input_tensorinfo : tensorinfo is NULL");
883 }
884
885 if (index >= getInputSize())
886 {
887 setLastErrorMessage("Error during Session::input_tensorinfo : index is out of range");
888 return NNFW_STATUS_ERROR;
889 }
890
892 if (isStateModelLoaded())
893 {
894 const auto &info = _selected_signature.valid() ? _nnpkg->inputInfo(_selected_signature, index)
895 : _nnpkg->inputInfo(input_index);
896 fillTensorInfo(ti, info.shape(), info.typeInfo().type());
897 }
898 else
899 {
900 const auto &info = _execution->inputInfo(input_index);
901 fillTensorInfo(ti, info.shape(), info.typeInfo().type());
902 }
903 }
904 catch (const std::exception &e)
905 {
906 setLastErrorMessage("Error during Session::input_tensorinfo : " + std::string(e.what()));
907 return NNFW_STATUS_ERROR;
908 }
910}
bool valid() const
Check whether the value is valid or not.
Definition Index.h:125

References info, NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, NNFW_STATUS_NO_ERROR, and NNFW_STATUS_UNEXPECTED_NULL.

◆ load_circle_from_buffer()

NNFW_STATUS onert::api::Session::load_circle_from_buffer ( uint8_t *  buffer,
size_t  size 
)

Definition at line 260 of file Session.cc.

261{
262 if (!isStateInitialized())
263 {
264 setLastErrorMessage("Invalid state : " + std::to_string(static_cast<int>(_state)));
266 }
267
268 if (!buffer)
269 {
270 setLastErrorMessage("Invalid argument : buffer is NULL");
272 }
273
274 if (size == 0)
275 {
276 setLastErrorMessage("Invalid argument : size is 0");
277 return NNFW_STATUS_ERROR;
278 }
279
280 try
281 {
283 // TODO: Update _model_path if necessary
284 _nnpkg = std::make_unique<onert::ir::NNPkg>(std::move(model));
285 _train_info = loadTrainingInfo(_nnpkg->primary_model());
286 _state = State::MODEL_LOADED;
287 }
288 catch (const std::exception &e)
289 {
290 setLastErrorMessage("Error during model loading : " + std::string(e.what()));
291 return NNFW_STATUS_ERROR;
292 }
294}
std::unique_ptr< ir::train::TrainingInfo > loadTrainingInfo(const uint8_t *buffer, const size_t size)
std::unique_ptr< ir::Model > loadCircleModel(const std::string &filename)
int32_t size[5]
Definition Slice.cpp:35

References onert::loader::loadCircleModel(), NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, NNFW_STATUS_NO_ERROR, NNFW_STATUS_UNEXPECTED_NULL, and size.

◆ load_model_from_path()

NNFW_STATUS onert::api::Session::load_model_from_path ( const char *  path)

Definition at line 296 of file Session.cc.

297{
298 if (!isStateInitialized())
299 {
300 setLastErrorMessage("Invalid state : " + std::to_string(static_cast<int>(_state)));
302 }
303
304 if (!path)
305 {
306 setLastErrorMessage("Invalid argument : path is NULL");
308 }
309
310 if (!null_terminating(path, MAX_PATH_LENGTH))
311 {
312 setLastErrorMessage("Invalid argument : path is too long");
313 return NNFW_STATUS_ERROR;
314 }
315
316 try
317 {
318 std::filesystem::path filename{path};
319 if (!std::filesystem::is_directory(filename))
320 {
321 std::string model_type = inferModelType(filename);
322 if (model_type.empty())
323 {
324 setLastErrorMessage("Cannot determine model type from file name extension '" +
325 filename.string() + "'");
326 return NNFW_STATUS_ERROR;
327 }
328 else
329 return loadModelFile(filename, model_type);
330 }
331
332 const auto &package_dir = filename;
333
334 // TODO : add support for zipped package file load
335 if (!std::filesystem::is_directory(package_dir))
336 {
337 setLastErrorMessage("Invalid argument : path '" + package_dir.string() +
338 "' is not a directory");
339 return NNFW_STATUS_ERROR;
340 }
341
342 const auto manifest_file_name = package_dir / "metadata/MANIFEST";
343 std::ifstream mfs(manifest_file_name);
344
345 // extract the filename of the first(index 0) model
346 // e.g. In MANIFEST file, { "models" : [ "firstmodel.tflite", "2nd.tflite" ] }
347 Json::Value root;
348 mfs >> root;
349 const Json::Value &models = root["models"];
350 const Json::Value &model_types = root["model-types"];
351 const Json::Value &configs = root["configs"];
352
353 if (!configs.empty() && !configs[0].empty())
354 {
355 const auto filepath = package_dir / "metadata" / configs[0].asString();
356
358 if (loadConfigure(filepath.string(), keyValues))
359 {
361 }
362 }
363 _nnpkg = std::make_unique<onert::ir::NNPkg>();
364 auto num_models = models.size();
365 if (num_models == 0 || (num_models - 1) > onert::ir::ModelIndex::max())
366 {
367 setLastErrorMessage("Invalid model size : " + std::to_string(num_models));
368 return NNFW_STATUS_ERROR;
369 }
370
371 // Not support backend mapping to operator index for multiple models yet
372 // TODO Support this
373 if (num_models > 1 && _coptions->manual_scheduler_options.index_to_backend.size() != 0)
374 {
375 setLastErrorMessage("Cannot set backend to operator index for multiple models");
376 return NNFW_STATUS_ERROR;
377 }
378
379 for (uint16_t i = 0; i < num_models; ++i)
380 {
381 const auto model_file_name = std::filesystem::path(models[i].asString());
382 const auto model_file_path = package_dir / model_file_name;
383 std::string model_type;
384
385 // Use model-types if available and not empty, otherwise infer from file extension
386 if (!model_types.empty() && i < model_types.size())
387 model_type = model_types[i].asString();
388 else
389 model_type = inferModelType(model_file_name);
390 if (model_type.empty())
391 {
392 setLastErrorMessage(
393 "Cannot determine model type for '" + models[i].asString() +
394 "' : Please specify model-types in MANIFEST or use a file with valid extension");
395 return NNFW_STATUS_ERROR;
396 }
397
398 auto model = loadModel(model_file_path.string(), model_type);
399 _model_path = model_file_path; // TODO Support multiple models
400 model->bindKernelBuilder(_kernel_registry->getBuilder());
401 _nnpkg->push(onert::ir::ModelIndex{i}, std::move(model));
402 }
403
404 _train_info = loadTrainingInfo(_nnpkg->primary_model());
405
406 auto toIODesc = [](std::string str) {
407 auto indices = nnfw::misc::split(str, ':');
408 if (indices.size() != 3)
409 {
410 // TODO: Do not write to std::cerr in library code
411 std::cerr << "IODesc should be 3-tuple." << std::endl;
412 return onert::ir::IODesc{};
413 }
414 auto model_idx = static_cast<uint32_t>(std::stoi(indices.at(0)));
415 auto subgraph_idx = static_cast<uint32_t>(std::stoi(indices.at(1)));
416 auto operand_idx = static_cast<uint32_t>(std::stoi(indices.at(2)));
417 return onert::ir::IODesc{model_idx, subgraph_idx, operand_idx};
418 };
419 // read pkg-inputs and pkg-outputs
420 const Json::Value &pkg_inputs = root["pkg-inputs"];
421 for (uint32_t i = 0; i < pkg_inputs.size(); ++i)
422 _nnpkg->addInput(toIODesc(pkg_inputs[i].asString()));
423 const Json::Value &pkg_outputs = root["pkg-outputs"];
424 for (uint32_t i = 0; i < pkg_outputs.size(); ++i)
425 _nnpkg->addOutput(toIODesc(pkg_outputs[i].asString()));
426 // read model-connect
427 const Json::Value &fromtos = root["model-connect"];
428 for (uint32_t i = 0; i < fromtos.size(); ++i)
429 {
430 const Json::Value &tos = fromtos[i]["to"];
431 for (uint32_t j = 0; j < tos.size(); ++j)
432 _nnpkg->addEdge(toIODesc(fromtos[i]["from"].asString()), toIODesc(tos[j].asString()));
433 }
434
435 _nnpkg->verify();
436 _state = State::MODEL_LOADED;
437 }
438 catch (const std::exception &e)
439 {
440 setLastErrorMessage("Failed to load model : " + std::string(e.what()));
441 return NNFW_STATUS_ERROR;
442 }
444}
#define MAX_PATH_LENGTH
Definition Session.cc:48
static uint16_t max()
Return max index value.
Definition Index.h:146
Op * root(Op *)
Return the root Op from a given Op node.
Definition Op.cpp:144
std::unique_ptr< mir::Graph > loadModel(std::string predict_net, std::string init_net, const std::vector< std::vector< int > > &input_shapes)
std::vector< std::string > split(const std::string &s, char delim)
std::tuple< ModelIndex, SubgraphIndex, IOIndex > IODesc
Definition NNPkg.h:30
std::unordered_map< std::string, std::string > CfgKeyValues
void setConfigKeyValues(const CfgKeyValues &keyValues)

References onert::util::Index< uint16_t, ModelIndexTag >::max(), MAX_PATH_LENGTH, NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, NNFW_STATUS_NO_ERROR, NNFW_STATUS_UNEXPECTED_NULL, onert::util::setConfigKeyValues(), and nnfw::misc::split().

◆ output_size()

NNFW_STATUS onert::api::Session::output_size ( uint32_t *  number)

Definition at line 616 of file Session.cc.

617{
618 if (isStateInitialized())
619 {
620 setLastErrorMessage("Error during Session::output_size : Model is not loaded");
622 }
623
624 if (number == nullptr)
625 {
626 setLastErrorMessage("Error during Session::output_size : number is NULL");
628 }
629
630 try
631 {
632 *number = getOutputSize();
633 }
634 catch (const std::exception &e)
635 {
636 setLastErrorMessage("Error during Session::output_size : " + std::string(e.what()));
637 return NNFW_STATUS_ERROR;
638 }
640}

References NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, NNFW_STATUS_NO_ERROR, and NNFW_STATUS_UNEXPECTED_NULL.

◆ output_tensorindex()

NNFW_STATUS onert::api::Session::output_tensorindex ( const char *  tensorname,
uint32_t *  index 
)

Definition at line 1452 of file Session.cc.

1453{
1454 return getTensorIndexImpl(*primary_subgraph(), tensorname, index, false);
1455}

◆ output_tensorinfo()

NNFW_STATUS onert::api::Session::output_tensorinfo ( uint32_t  index,
nnfw_tensorinfo ti 
)

Definition at line 912 of file Session.cc.

913{
914 if (isStateInitialized())
915 {
916 setLastErrorMessage("Error during Session::output_tensorinfo : Model is not loaded");
918 }
919
920 if (ti == nullptr)
921 {
922 setLastErrorMessage("Error during Session::output_tensorinfo : tensorinfo is NULL");
924 }
925
926 try
927 {
928 if (index >= getOutputSize())
929 {
930 setLastErrorMessage("Error during Session::output_tensorinfo : index is out of range");
931 return NNFW_STATUS_ERROR;
932 }
933
934 const auto output_index = onert::ir::IOIndex{index};
935 if (isStateModelLoaded())
936 {
937 const auto &info = _selected_signature.valid()
938 ? _nnpkg->outputInfo(_selected_signature, index)
939 : _nnpkg->outputInfo(output_index);
940 fillTensorInfo(ti, info.shape(), info.typeInfo().type());
941 }
942 else
943 {
944 auto info = _execution->outputInfo(output_index);
945 fillTensorInfo(ti, info.shape(), info.typeInfo().type());
946 }
947 }
948 catch (const std::exception &e)
949 {
950 setLastErrorMessage("Error during Session::output_tensorinfo : " + std::string(e.what()));
951 return NNFW_STATUS_ERROR;
952 }
953
955}

References info, NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, NNFW_STATUS_NO_ERROR, and NNFW_STATUS_UNEXPECTED_NULL.

◆ prepare()

NNFW_STATUS onert::api::Session::prepare ( )

Definition at line 446 of file Session.cc.

447{
448 // NOTE. If users want to run prepare() more than one time, this could be removed.
449 if (!isStateModelLoaded())
450 {
451 if (isStateInitialized())
452 {
453 setLastErrorMessage("Error during Session::prepare : prepare should be called once");
454 }
455 else
456 {
457 setLastErrorMessage("Error during Session::prepare : Invalid state");
458 }
460 }
461
462 try
463 {
464 auto compiler =
465 onert::compiler::CompilerFactory::get().create(std::move(_nnpkg), _coptions.get());
466 _compiler_artifact = compiler->compile();
467 _execution = std::make_unique<onert::exec::Execution>(_compiler_artifact->_executors);
468 }
469 catch (const std::exception &e)
470 {
471 setLastErrorMessage("Error during Session::prepare : " + std::string(e.what()));
472 return NNFW_STATUS_ERROR;
473 }
474
475 _state = State::PREPARED;
477}
static CompilerFactory & get()
std::unique_ptr< ICompiler > create(std::unique_ptr< ir::NNPkg > nnpkg, CompilerOptions *copts, const ir::train::TrainingInfo *training_info=nullptr)
Create ICompiler instance. Ownership of nnpkg is moved to ICompiler instance.

References onert::compiler::CompilerFactory::create(), onert::compiler::CompilerFactory::get(), NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, and NNFW_STATUS_NO_ERROR.

◆ quantize()

NNFW_STATUS onert::api::Session::quantize ( )

Definition at line 2185 of file Session.cc.

2186{
2187 try
2188 {
2189 if (isStateInitialized() || isStateRunning())
2190 {
2191 setLastErrorMessage("Error during Session::quantize : Invalid state");
2193 }
2194
2195 auto result = _quant_manager->quantize(_model_path.string());
2196 if (!result)
2197 {
2198 setLastErrorMessage("Error during Session::quantize : Quantization failed");
2200 }
2201
2202 // Replace model
2203 // TODO Support buffer replace, not file reload
2204 return loadModelFile(_quant_manager->exportModelPath(), "circle");
2205 }
2206 catch (const std::exception &e)
2207 {
2208 setLastErrorMessage("Error during Session::quantize : " + std::string(e.what()));
2209 return NNFW_STATUS_ERROR;
2210 }
2211}
result
Definition infer.py:103

References NNFW_STATUS_ERROR, and NNFW_STATUS_INVALID_STATE.

◆ register_custom_operation()

NNFW_STATUS onert::api::Session::register_custom_operation ( const std::string &  id,
nnfw_custom_eval  eval_func 
)

Definition at line 957 of file Session.cc.

958{
959 _kernel_registry->registerKernel(id, eval_func);
961}

References NNFW_STATUS_NO_ERROR.

◆ reset_execute_config()

NNFW_STATUS onert::api::Session::reset_execute_config ( )

Definition at line 2367 of file Session.cc.

2368{
2369 if (!isStatePreparedOrFinishedRun())
2370 {
2371 setLastErrorMessage("Error during Session::set_execution_config : Invalid state");
2373 }
2374
2375 _execution->executionOptions().dump_minmax = false;
2376 _execution->executionOptions().trace = false;
2377 _execution->executionOptions().profile = false;
2378
2379 return NNFW_STATUS_NO_ERROR;
2380}

References NNFW_STATUS_INVALID_STATE, and NNFW_STATUS_NO_ERROR.

◆ reset_prepare_config()

NNFW_STATUS onert::api::Session::reset_prepare_config ( )

Definition at line 2323 of file Session.cc.

2324{
2325 if (!isStateModelLoaded())
2326 {
2327 setLastErrorMessage("Error during Session::reset_prepare_config : Invalid state");
2329 }
2330
2331 _coptions->he_profiling_mode = false;
2332
2333 return NNFW_STATUS_NO_ERROR;
2334}

References NNFW_STATUS_INVALID_STATE, and NNFW_STATUS_NO_ERROR.

◆ run()

NNFW_STATUS onert::api::Session::run ( )

Definition at line 479 of file Session.cc.

480{
481 if (!isStatePreparedOrFinishedRun())
482 {
483 setLastErrorMessage("Error during Session::run : run should be called after prepare");
485 }
486
487 try
488 {
489 _execution->execute();
490 }
492 {
493 // Currently insufficient buffer always means output buffer.
494 setLastErrorMessage("Error during Session::run : " + std::string(e.what()));
496 }
497 catch (const std::exception &e)
498 {
499 setLastErrorMessage("Error during Session::run : " + std::string(e.what()));
500 return NNFW_STATUS_ERROR;
501 }
502
503 _state = State::FINISHED_RUN;
505}
const char * what() const noexcept override
Definition Exceptions.h:31

References NNFW_STATUS_ERROR, NNFW_STATUS_INSUFFICIENT_OUTPUT_SIZE, NNFW_STATUS_INVALID_STATE, NNFW_STATUS_NO_ERROR, and onert::Exception::what().

◆ run_async()

NNFW_STATUS onert::api::Session::run_async ( )

Definition at line 507 of file Session.cc.

508{
509 if (!isStatePreparedOrFinishedRun())
510 {
511 setLastErrorMessage(
512 "Error during Session::run_async : run_async should be called after prepare");
514 }
515
516 _execution->startExecute();
517
518 _state = State::RUNNING;
520}

References NNFW_STATUS_INVALID_STATE, and NNFW_STATUS_NO_ERROR.

◆ run_with_auto_compilation()

NNFW_STATUS onert::api::Session::run_with_auto_compilation ( const char *  target,
NNFW_CODEGEN_PREF  pref 
)

Definition at line 2414 of file Session.cc.

2415{
2416
2417 if (!isStatePreparedOrFinishedRun())
2418 {
2419 setLastErrorMessage("Error during Session::run_with_auto_compilation : Run should be after "
2420 "preparation");
2422 }
2423
2424 // Check quantization and code-generation parameters
2425 std::string target_str{target};
2426 if (_quant_manager->exportModelPath().empty() || _codegen_manager->exportModelPath().empty() ||
2427 target_str.empty() || target_str.substr(target_str.size() - 4) != "-gen")
2428 {
2429 setLastErrorMessage("Error during Session::run_with_auto_compilation : Quantization and "
2430 "code generation parameters should be set");
2432 }
2433
2434 // Odc: auto compilation with hidden switching mechanizm
2435 // Check is model already quantized or compiled
2436 std::ifstream file_quantized_model(_quant_manager->exportModelPath());
2437 std::ifstream file_compiled_model(_codegen_manager->exportModelPath());
2438
2439 if (!file_quantized_model.good() && !file_compiled_model.good())
2440 {
2441 // Run float model and try to quantize it
2442 {
2443 // Save execution options
2444 auto saved_options = _execution->executionOptions();
2445 // turn on minmax recording
2446 _execution->executionOptions().dump_minmax = true;
2447
2448 try
2449 {
2450 _execution->execute();
2451 }
2453 {
2454 // Currently insufficient buffer always means output buffer.
2455 setLastErrorMessage("Error during Session::run_with_auto_compilation : " +
2456 std::string(e.what()));
2458 }
2459 catch (const std::exception &e)
2460 {
2461 setLastErrorMessage("Error during Session::run_with_auto_compilation : " +
2462 std::string(e.what()));
2463 return NNFW_STATUS_ERROR;
2464 }
2465
2466 _state = State::FINISHED_RUN;
2467
2468 // restore min_max option to user defined state
2469 _execution->executionOptions().dump_minmax = saved_options.dump_minmax;
2470
2471 // if enough statistics are collected, then run the quantization
2472 if (_quant_manager->readyForQuantize())
2473 {
2474 try
2475 {
2476 if (isStateInitialized() || isStateRunning())
2477 {
2478 setLastErrorMessage("Error during Session::run_with_auto_compilation : Invalid state");
2480 }
2481
2482 auto result = _quant_manager->quantize(_model_path);
2483 if (!result)
2484 {
2485 setLastErrorMessage(
2486 "Error during Session::run_with_auto_compilation : Quantization failed");
2488 }
2489
2490 // remove minmax file
2491 result = _quant_manager->deleteMinMaxFile();
2492 if (!result)
2493 {
2494 setLastErrorMessage(
2495 "Error during Session::run_with_auto_compilation : Could not delete minmax file");
2497 }
2498 }
2499 catch (const std::exception &e)
2500 {
2501 setLastErrorMessage("Error during Session::run_with_auto_compilation : " +
2502 std::string(e.what()));
2503 return NNFW_STATUS_ERROR;
2504 }
2505 }
2506 }
2507 }
2508 else
2509 {
2510 // run compiled or quantized model
2511 NNFW_STATUS status;
2512
2513 // turn off minmax recording
2514 _execution->executionOptions().dump_minmax = false;
2515
2516 // save initial buffers if quantized model or compiled model is not loaded
2517 if (_autoCompilationState == Session::AutoCompilationState::INITIAL_STATE)
2518 {
2519 auto dotidx = _codegen_manager->exportModelPath().rfind('.');
2520 if (dotidx == std::string::npos)
2521 {
2522 setLastErrorMessage("Error during Session::run_with_auto_compilation : Invalid compiled "
2523 "model path. Please use a path that includes the extension.");
2524 return NNFW_STATUS_ERROR;
2525 }
2526
2527 std::string compiled_model_type =
2528 _codegen_manager->exportModelPath().substr(dotidx + 1); // + 1 to exclude dot
2529
2530 dotidx = _quant_manager->exportModelPath().rfind('.');
2531 if (dotidx == std::string::npos)
2532 {
2533 setLastErrorMessage("Error during Session::run_with_auto_compilation : Invalid quantized "
2534 "model path. Please use a path that includes the extension.");
2535 return NNFW_STATUS_ERROR;
2536 }
2537 std::string quantized_model_type =
2538 _quant_manager->exportModelPath().substr(dotidx + 1); // + 1 to exclude dot
2539
2540 // Save initial (float) input and output buffers
2541 auto input_size = _execution->inputSize();
2542 auto output_size = _execution->outputSize();
2543
2544 std::vector<const void *> _input_buffers;
2545 std::vector<void *> _output_buffers;
2546
2547 using namespace onert::ir;
2548 // Copy execution context for backup: I/O buffer, shape, and execution options
2549 const onert::exec::ExecutionContext ctx_backup = _execution->context();
2550
2551 // Set compile option to use float type
2552 for (auto input_index = IOIndex{0}; input_index < IOIndex{input_size}; input_index++)
2553 _coptions->input_type.insert_or_assign(IODesc{ModelIndex{0}, SubgraphIndex{0}, input_index},
2554 TypeInfo(DataType::FLOAT32));
2555
2556 // Save Outputs buffers
2557 for (auto output_index = IOIndex{0}; output_index < IOIndex{output_size}; output_index++)
2558 _coptions->output_type.insert_or_assign(
2559 IODesc{ModelIndex{0}, SubgraphIndex{0}, output_index}, TypeInfo(DataType::FLOAT32));
2560
2561 // if there is compiled model - try to load it
2562 if (file_compiled_model.good())
2563 {
2564 // load compiled model
2565 status = loadModelFile(_codegen_manager->exportModelPath(), compiled_model_type);
2566 if (status == NNFW_STATUS_NO_ERROR)
2567 {
2568 _autoCompilationState = Session::AutoCompilationState::COMPILED_MODEL_LOADED;
2569 }
2570 }
2571 else // there is no compiled model - try to compile and load it
2572 {
2573
2574 // avoiding code duplication use existing "codegen" function. Set up _model_path for the
2575 // codegen function.
2576 // TODO: change it if codegen function will be generalized
2577 _model_path = _quant_manager->exportModelPath();
2578
2579 // try to compile and load compiled model
2580 status = codegen(target, pref);
2581 if (status == NNFW_STATUS_NO_ERROR)
2582 {
2583 _autoCompilationState = Session::AutoCompilationState::COMPILED_MODEL_LOADED;
2584 // TODO delete quantized model
2585 }
2586 }
2587
2588 // loading compiled model is fail - try to load quantized model
2589 if (_autoCompilationState != Session::AutoCompilationState::COMPILED_MODEL_LOADED)
2590 {
2591 // load quantized model
2592 status = loadModelFile(_quant_manager->exportModelPath(), quantized_model_type);
2593 if (status != NNFW_STATUS_NO_ERROR)
2594 return status;
2595 else
2596 _autoCompilationState = Session::AutoCompilationState::QUANTIZED_MODEL_LOADED;
2597 }
2598
2599 status = prepare();
2600 if (status != NNFW_STATUS_NO_ERROR)
2601 return status;
2602
2603 // Restore execution context: I/O buffer, shape, and execution options
2604 _execution->restoreContext(ctx_backup);
2605 }
2606
2607 // Run quantized model
2608 if (!isStatePreparedOrFinishedRun())
2609 {
2610 setLastErrorMessage("Error during Session::run_with_auto_compilation : Run should be after "
2611 "preparation");
2613 }
2614
2615 try
2616 {
2617 _execution->execute();
2618 }
2620 {
2621 // Currently insufficient buffer always means output buffer.
2622 setLastErrorMessage("Error during Session::run_with_auto_compilation : " +
2623 std::string(e.what()));
2625 }
2626 catch (const std::exception &e)
2627 {
2628 setLastErrorMessage("Error during Session::run_with_auto_compilation : " +
2629 std::string(e.what()));
2630 return NNFW_STATUS_ERROR;
2631 }
2632
2633 _state = State::FINISHED_RUN;
2634 }
2635
2636 return NNFW_STATUS_NO_ERROR;
2637}
NNFW_STATUS
Result values returned from a call to an API function.
Definition onert-micro.h:86
NNFW_STATUS prepare()
Definition Session.cc:446
NNFW_STATUS input_size(uint32_t *number)
Definition Session.cc:590
NNFW_STATUS output_size(uint32_t *number)
Definition Session.cc:616

References NNFW_STATUS_ERROR, NNFW_STATUS_INSUFFICIENT_OUTPUT_SIZE, NNFW_STATUS_INVALID_STATE, and onert::Exception::what().

◆ set_available_backends()

NNFW_STATUS onert::api::Session::set_available_backends ( const char *  backends)

Definition at line 1018 of file Session.cc.

1019{
1020 if (!isStateModelLoaded())
1021 {
1022 setLastErrorMessage("Error during Session::set_available_backends : Model is not loaded");
1024 }
1025
1026 if (!backends)
1027 {
1028 setLastErrorMessage("Error during Session::set_available_backends : backends is NULL");
1030 }
1031
1032 if (null_terminating(backends, MAX_BACKEND_NAME_LENGTH) == false)
1033 {
1034 setLastErrorMessage("Error during Session::set_available_backends : backends is too long");
1035 return NNFW_STATUS_ERROR;
1036 }
1037
1038 try
1039 {
1040 using namespace onert::util;
1041
1042 _coptions->backend_list = nnfw::misc::split(std::string{backends}, ';');
1043 }
1044 catch (const std::exception &e)
1045 {
1046 setLastErrorMessage("Error during Session::set_available_backends : " + std::string(e.what()));
1047 return NNFW_STATUS_ERROR;
1048 }
1049 return NNFW_STATUS_NO_ERROR;
1050}
#define MAX_BACKEND_NAME_LENGTH
Definition Session.cc:46

References MAX_BACKEND_NAME_LENGTH, NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, NNFW_STATUS_NO_ERROR, NNFW_STATUS_UNEXPECTED_NULL, and nnfw::misc::split().

◆ set_backends_per_operation()

NNFW_STATUS onert::api::Session::set_backends_per_operation ( const char *  backend_settings)

Set backends with string-encoded mapping from operation index to backend type (cpu, acl_cl)

Definition at line 1457 of file Session.cc.

1458{
1459 if (backend_settings == NULL)
1460 {
1461 setLastErrorMessage(
1462 "Error during Session::set_backends_per_operation : backend_settings is NULL");
1463 return NNFW_STATUS_ERROR;
1464 }
1465
1466 if (!isStateModelLoaded())
1467 {
1468 setLastErrorMessage("Error during Session::set_backends_per_operation : Model is not loaded");
1470 }
1471
1472 // Not supported multiple model
1473 // TODO Support this
1474 if (_nnpkg->model_count() > 1)
1475 {
1476 setLastErrorMessage(
1477 "Error during Session::set_backends_per_operation : Multiple model is not supported");
1478 return NNFW_STATUS_ERROR;
1479 }
1480
1481 try
1482 {
1483 // Backend for all
1484 auto &ms_options = _coptions->manual_scheduler_options;
1485 ms_options.setBackendMap(std::string{backend_settings});
1486 }
1487 catch (const std::exception &e)
1488 {
1489 setLastErrorMessage("Error during Session::set_backends_per_operation : " +
1490 std::string(e.what()));
1491 return NNFW_STATUS_ERROR;
1492 }
1493
1494 return NNFW_STATUS_NO_ERROR;
1495}

References NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, and NNFW_STATUS_NO_ERROR.

◆ set_codegen_model_path()

NNFW_STATUS onert::api::Session::set_codegen_model_path ( const char *  path)

Definition at line 2213 of file Session.cc.

2214{
2215 try
2216 {
2217 if (isStateInitialized() || isStateRunning())
2218 {
2219 setLastErrorMessage("Error during Session::set_codegen_model_path : Invalid state");
2221 }
2222
2223 assert(_codegen_manager != nullptr);
2224 _codegen_manager->exportModelPath(std::string(path));
2225 }
2226 catch (const std::exception &e)
2227 {
2228 setLastErrorMessage("Error during Session::set_codegen_model_path : " + std::string(e.what()));
2229 return NNFW_STATUS_ERROR;
2230 }
2231
2232 return NNFW_STATUS_NO_ERROR;
2233}

References NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, and NNFW_STATUS_NO_ERROR.

◆ set_config()

NNFW_STATUS onert::api::Session::set_config ( const char *  key,
const char *  value 
)

Definition at line 1151 of file Session.cc.

1152{
1153 if (!isStateModelLoaded())
1154 {
1155 setLastErrorMessage("Error during Session::set_config : Model is not loaded");
1157 }
1158
1159 if (!key)
1160 {
1161 setLastErrorMessage("Error during Session::set_config : key is NULL");
1163 }
1164 if (!value)
1165 {
1166 setLastErrorMessage("Error during Session::set_config : value is NULL");
1168 }
1169
1170 using namespace onert::util;
1171
1172 const std::string skey = key;
1173
1174 if (skey == config::GRAPH_DOT_DUMP)
1175 {
1176 _coptions->graph_dump_level = toInt(value);
1177 }
1178 else if (skey == config::EXECUTOR)
1179 {
1180 _coptions->executor = value;
1181 }
1182 else if (skey == config::USE_SCHEDULER)
1183 {
1184 _coptions->he_scheduler = toBool(value);
1185 }
1186 else if (skey == config::PROFILING_MODE)
1187 {
1188 _coptions->he_profiling_mode = toBool(value);
1189 }
1190 else if (skey == config::ENABLE_LOG || skey == config::NUM_THREADS)
1191 {
1192 onert::util::CfgKeyValues keyValues;
1193 keyValues[skey] = std::string(value);
1195
1196 if (skey == config::ENABLE_LOG)
1197 {
1199 }
1200 }
1201 else
1202 {
1203 setLastErrorMessage("Error during Session::set_config : Unknown config key");
1204 return NNFW_STATUS_ERROR;
1205 }
1206 return NNFW_STATUS_NO_ERROR;
1207}
int toInt(const std::string &val)
bool toBool(const std::string &val)
#define UPDATE_VERBOSE_CONFIG()
Definition logging.h:81

References NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, NNFW_STATUS_NO_ERROR, NNFW_STATUS_UNEXPECTED_NULL, onert::util::setConfigKeyValues(), and UPDATE_VERBOSE_CONFIG.

◆ set_execute_config()

NNFW_STATUS onert::api::Session::set_execute_config ( const NNFW_RUN_CONFIG  key,
const char *  value 
)

Definition at line 2336 of file Session.cc.

2337{
2338 if (!isStatePreparedOrFinishedRun())
2339 {
2340 setLastErrorMessage("Error during Session::set_execute_config : Invalid state");
2342 }
2343
2344 switch (key)
2345 {
2347 if (_coptions->workspace_dir.empty())
2348 return NNFW_STATUS_ERROR;
2349 _execution->executionOptions().dump_minmax = true;
2350 break;
2352 if (_coptions->workspace_dir.empty())
2353 return NNFW_STATUS_ERROR;
2354 _execution->executionOptions().trace = true;
2355 break;
2357 _execution->executionOptions().profile = true;
2358 break;
2359 default:
2360 setLastErrorMessage("Error during Session::set_execute_config : Invalid config key");
2361 return NNFW_STATUS_ERROR;
2362 }
2363
2364 return NNFW_STATUS_NO_ERROR;
2365}
@ NNFW_RUN_CONFIG_PROFILE
@ NNFW_RUN_CONFIG_TRACE
@ NNFW_RUN_CONFIG_DUMP_MINMAX

References NNFW_RUN_CONFIG_DUMP_MINMAX, NNFW_RUN_CONFIG_PROFILE, NNFW_RUN_CONFIG_TRACE, NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, and NNFW_STATUS_NO_ERROR.

◆ set_input()

NNFW_STATUS onert::api::Session::set_input ( uint32_t  index,
NNFW_TYPE  type,
const void *  buffer,
size_t  length 
)

Definition at line 537 of file Session.cc.

538{
539 if (!isStatePreparedOrFinishedRun())
540 {
541 setLastErrorMessage("Error during Session::set_input : Invalid state");
543 }
544
545 if (!buffer && length != 0)
546 {
547 setLastErrorMessage("Error during Session::set_input : buffer is NULL but the length is not 0");
548 return NNFW_STATUS_ERROR;
549 }
550
551 try
552 {
553 _execution->setInput(onert::ir::IOIndex(index), buffer, length);
554 }
555 catch (const std::exception &e)
556 {
557 setLastErrorMessage("Error during Session::set_input : " + std::string(e.what()));
558 return NNFW_STATUS_ERROR;
559 }
561}

References NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, and NNFW_STATUS_NO_ERROR.

◆ set_input_layout()

NNFW_STATUS onert::api::Session::set_input_layout ( uint32_t  index,
NNFW_LAYOUT  layout 
)

Definition at line 642 of file Session.cc.

643{
644 if (!isStateModelLoaded())
645 {
646 setLastErrorMessage("Error during Session::set_input_layout : Model is not loaded");
648 }
649
650 try
651 {
652 if (layout != NNFW_LAYOUT_NONE && layout != NNFW_LAYOUT_CHANNELS_FIRST &&
654 {
655 setLastErrorMessage("Error during Session::set_input_layout : Not supported layout");
656 return NNFW_STATUS_ERROR;
657 }
658
659 if (_selected_signature.valid())
660 {
661 // TODO Support this
662 setLastErrorMessage("Error during Session::set_input_layout : set_input_layout after "
663 "signature selection is not supported yet");
664 return NNFW_STATUS_ERROR;
665 }
666
667 const auto io_index = onert::ir::IOIndex{index};
668 // Signature is supported on single model only
669 assert(!_selected_signature.valid() || _nnpkg->model_count() != 1);
670 const auto io_desc =
671 _selected_signature.valid()
672 ? onert::ir::IODesc{onert::ir::ModelIndex{0}, _selected_signature, io_index}
673 : _nnpkg->input(io_index);
674 // Insert if not exists, otherwise update the value
675 _coptions->input_layout[io_desc] = convertLayout(layout);
676 }
677 catch (const std::exception &e)
678 {
679 setLastErrorMessage("Error during Session::set_input_layout : " + std::string(e.what()));
680 return NNFW_STATUS_ERROR;
681 }
683}
@ NNFW_LAYOUT_CHANNELS_LAST
Definition nnfw.h:141
@ NNFW_LAYOUT_CHANNELS_FIRST
Definition nnfw.h:146
@ NNFW_LAYOUT_NONE
Definition nnfw.h:136

References NNFW_LAYOUT_CHANNELS_FIRST, NNFW_LAYOUT_CHANNELS_LAST, NNFW_LAYOUT_NONE, NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, and NNFW_STATUS_NO_ERROR.

◆ set_input_tensorinfo()

NNFW_STATUS onert::api::Session::set_input_tensorinfo ( uint32_t  index,
const nnfw_tensorinfo ti 
)

Definition at line 817 of file Session.cc.

818{
819 // sanity check
820 {
821 if (isStateInitialized())
822 {
823 setLastErrorMessage("Error during Session::set_input_tensorinfo : Model is not loaded");
825 }
826
827 if (ti == nullptr)
828 {
829 setLastErrorMessage("Error during Session::set_input_tensorinfo : tensorinfo is NULL");
831 }
832
833 if (ti->rank < 0 || ti->rank > NNFW_MAX_RANK)
834 {
835 setLastErrorMessage("Error during Session::set_input_tensorinfo : Unsupported rank : " +
836 std::to_string(ti->rank));
837 return NNFW_STATUS_ERROR;
838 }
839
840 for (int32_t i = 0; i < ti->rank; ++i)
841 {
842 if (ti->dims[i] <= 0)
843 {
844 setLastErrorMessage(
845 "Error during Session::set_input_tensorinfo : dim must be positive integer but was " +
846 std::to_string(ti->dims[i]));
847 return NNFW_STATUS_ERROR;
848 }
849 }
850 }
851
852 onert::ir::Shape new_shape(ti->rank);
853 for (int32_t i = 0; i < ti->rank; i++)
854 new_shape.dim(i) = ti->dims[i];
855
856 const auto input_index = onert::ir::IOIndex(index);
857 if (!isStatePreparedOrFinishedRun())
858 {
859 // In this case, if we apply input shape, it will propagate after compilation and excution
860 _selected_signature.valid() ? _nnpkg->changeInputShape(_selected_signature, index, new_shape)
861 : _nnpkg->changeInputShape(input_index, new_shape);
862 }
863 else // when called after Session::prepare()
864 _execution->changeInputShape(input_index, new_shape);
865
867}
::onert::util::Index< uint32_t, IOIndexTag > IOIndex
Definition Index.h:36
#define NNFW_MAX_RANK
Maximum rank expressible with nnfw.
int32_t dims[NNFW_MAX_RANK]

References nnfw_tensorinfo::dims, NNFW_MAX_RANK, NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, NNFW_STATUS_NO_ERROR, NNFW_STATUS_UNEXPECTED_NULL, and nnfw_tensorinfo::rank.

◆ set_input_type()

NNFW_STATUS onert::api::Session::set_input_type ( uint32_t  index,
NNFW_TYPE  type 
)

Definition at line 729 of file Session.cc.

730{
731 if (!isStateModelLoaded())
732 {
733 setLastErrorMessage("Error during Session::set_input_type : Model is not loaded");
735 }
736
737 try
738 {
740 {
741 setLastErrorMessage("Error during Session::set_input_type : Not supported type");
742 return NNFW_STATUS_ERROR;
743 }
744
745 if (_selected_signature.valid())
746 {
747 // TODO Support this
748 setLastErrorMessage("Error during Session::set_input_type : set_input_type after signature "
749 "selection is not supported yet");
750 return NNFW_STATUS_ERROR;
751 }
752
753 const auto io_index = onert::ir::IOIndex{index};
754 // Signature is supported on single model only
755 assert(!_selected_signature.valid() || _nnpkg->model_count() != 1);
756 const auto io_desc =
757 _selected_signature.valid()
758 ? onert::ir::IODesc{onert::ir::ModelIndex{0}, _selected_signature, io_index}
759 : _nnpkg->input(io_index);
760 // Insert if not exists, otherwise update the value
761 _coptions->input_type.insert_or_assign(io_desc,
762 onert::ir::TypeInfo(onert::ir::DataType::FLOAT32));
763 }
764 catch (const std::exception &e)
765 {
766 setLastErrorMessage("Error during Session::set_input_type : " + std::string(e.what()));
767 return NNFW_STATUS_ERROR;
768 }
769
771}
int32_t type
@ NNFW_TYPE_TENSOR_FLOAT32
Definition onert-micro.h:77

References NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, NNFW_STATUS_NO_ERROR, NNFW_TYPE_TENSOR_FLOAT32, and type.

◆ set_odc_param_minmax_records_count()

NNFW_STATUS onert::api::Session::set_odc_param_minmax_records_count ( int  minmax_records_count)

Definition at line 2382 of file Session.cc.

2383{
2384 if (isStateInitialized() || isStateRunning())
2385 {
2386 setLastErrorMessage("Error during Session::set_odc_param_minmax_records_count : Invalid state");
2388 }
2389
2390 if (_quant_manager->setMinMaxRecordsThreshold(minmax_records_count))
2391 return NNFW_STATUS_NO_ERROR;
2392
2393 setLastErrorMessage(
2394 "Error during Session::set_odc_param_minmax_records_count : Could not set value");
2395 return NNFW_STATUS_ERROR;
2396}

References NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, and NNFW_STATUS_NO_ERROR.

◆ set_output()

NNFW_STATUS onert::api::Session::set_output ( uint32_t  index,
NNFW_TYPE  type,
void *  buffer,
size_t  length 
)

Definition at line 563 of file Session.cc.

564{
565 if (!isStatePreparedOrFinishedRun())
566 {
567 setLastErrorMessage("Error during Session::set_output : Invalid state");
569 }
570
571 if (!buffer && length != 0)
572 {
573 setLastErrorMessage(
574 "Error during Session::set_output : buffer is NULL but the length is not 0");
575 return NNFW_STATUS_ERROR;
576 }
577
578 try
579 {
580 _execution->setOutput(onert::ir::IOIndex(index), buffer, length);
581 }
582 catch (const std::exception &e)
583 {
584 setLastErrorMessage("Error during Session::set_output : " + std::string(e.what()));
585 return NNFW_STATUS_ERROR;
586 }
588}

References NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, and NNFW_STATUS_NO_ERROR.

◆ set_output_layout()

NNFW_STATUS onert::api::Session::set_output_layout ( uint32_t  index,
NNFW_LAYOUT  layout 
)

Definition at line 685 of file Session.cc.

686{
687 if (!isStateModelLoaded())
688 {
689 setLastErrorMessage("Error during Session::set_output_layout : Model is not loaded");
691 }
692
693 try
694 {
695 if (layout != NNFW_LAYOUT_NONE && layout != NNFW_LAYOUT_CHANNELS_FIRST &&
697 {
698 setLastErrorMessage("Error during Session::set_output_layout : Not supported layout");
699 return NNFW_STATUS_ERROR;
700 }
701
702 if (_selected_signature.valid())
703 {
704 // TODO Support this
705 setLastErrorMessage("Error during Session::set_output_layout : set_output_layout after "
706 "signature selection is not supported yet");
707 return NNFW_STATUS_ERROR;
708 }
709
710 const auto io_index = onert::ir::IOIndex{index};
711 // Signature is supported on single model only
712 assert(!_selected_signature.valid() || _nnpkg->model_count() != 1);
713 const auto io_desc =
714 _selected_signature.valid()
715 ? onert::ir::IODesc{onert::ir::ModelIndex{0}, _selected_signature, io_index}
716 : _nnpkg->output(io_index);
717
718 // Insert if not exists, otherwise update the value
719 _coptions->output_layout[io_desc] = convertLayout(layout);
720 }
721 catch (const std::exception &e)
722 {
723 setLastErrorMessage("Error during Session::set_output_layout : " + std::string(e.what()));
724 return NNFW_STATUS_ERROR;
725 }
727}

References NNFW_LAYOUT_CHANNELS_FIRST, NNFW_LAYOUT_CHANNELS_LAST, NNFW_LAYOUT_NONE, NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, and NNFW_STATUS_NO_ERROR.

◆ set_output_type()

NNFW_STATUS onert::api::Session::set_output_type ( uint32_t  index,
NNFW_TYPE  type 
)

Definition at line 773 of file Session.cc.

774{
775 if (!isStateModelLoaded())
776 {
777 setLastErrorMessage("Error during Session::set_output_type : Model is not loaded");
779 }
780
781 try
782 {
784 {
785 setLastErrorMessage("Error during Session::set_output_type : Not supported type");
786 return NNFW_STATUS_ERROR;
787 }
788
789 if (_selected_signature.valid())
790 {
791 // TODO Support this
792 setLastErrorMessage("Error during Session::set_output_type : set_output_type after signature "
793 "selection is not supported yet");
794 return NNFW_STATUS_ERROR;
795 }
796
797 const auto io_index = onert::ir::IOIndex{index};
798 // Signature is supported on single model only
799 assert(!_selected_signature.valid() || _nnpkg->model_count() != 1);
800 const auto io_desc =
801 _selected_signature.valid()
802 ? onert::ir::IODesc{onert::ir::ModelIndex{0}, _selected_signature, io_index}
803 : _nnpkg->output(io_index);
804 // Insert if not exists, otherwise update the value
805 _coptions->output_type.insert_or_assign(io_desc,
806 onert::ir::TypeInfo(onert::ir::DataType::FLOAT32));
807 }
808 catch (const std::exception &e)
809 {
810 setLastErrorMessage("Error during Session::set_output_type : " + std::string(e.what()));
811 return NNFW_STATUS_ERROR;
812 }
813
815}

References NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, NNFW_STATUS_NO_ERROR, NNFW_TYPE_TENSOR_FLOAT32, and type.

◆ set_prepare_config()

NNFW_STATUS onert::api::Session::set_prepare_config ( const NNFW_PREPARE_CONFIG  key,
const char *  value 
)

Definition at line 2299 of file Session.cc.

2300{
2301 if (!isStateModelLoaded())
2302 {
2303 setLastErrorMessage("Error during Session::set_prepare_config : Invalid state");
2305 }
2306
2307 switch (key)
2308 {
2310 _coptions->he_profiling_mode = true;
2311 break;
2313 _coptions->internal_output_alloc = true;
2314 break;
2315 default:
2316 setLastErrorMessage("Error during Session::set_prepare_config : Invalid config key");
2317 return NNFW_STATUS_ERROR;
2318 }
2319
2320 return NNFW_STATUS_NO_ERROR;
2321}
@ NNFW_PREPARE_CONFIG_PROFILE
@ NNFW_ENABLE_INTERNAL_OUTPUT_ALLOC

References NNFW_ENABLE_INTERNAL_OUTPUT_ALLOC, NNFW_PREPARE_CONFIG_PROFILE, NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, and NNFW_STATUS_NO_ERROR.

◆ set_quantization_type()

NNFW_STATUS onert::api::Session::set_quantization_type ( NNFW_QUANTIZE_TYPE  qtype)

Definition at line 2121 of file Session.cc.

2122{
2124 try
2125 {
2126 if (isStateInitialized() || isStateRunning())
2127 {
2128 setLastErrorMessage("Error during Session::set_quantization_type : Invalid state");
2130 }
2131
2133 switch (qtype)
2134 {
2137 break;
2140 break;
2143 break;
2146 break;
2147 default:
2148 setLastErrorMessage(
2149 "Error during Session::set_quantization_type : Invalid quantization type");
2151 }
2152 _quant_manager->quantizeType(odc_qtype);
2153 }
2154 catch (const std::exception &e)
2155 {
2156 setLastErrorMessage("Error during Session::set_quantization_type : " + std::string(e.what()));
2157 return NNFW_STATUS_ERROR;
2158 }
2159
2160 return NNFW_STATUS_NO_ERROR;
2161}
@ ODC_QTYPE_WO_I8_SYM
@ ODC_QTYPE_WO_I16_SYM
@ NNFW_QUANTIZE_TYPE_WO_I16_SYM
@ NNFW_QUANTIZE_TYPE_U8_ASYM
@ NNFW_QUANTIZE_TYPE_I16_SYM
@ NNFW_QUANTIZE_TYPE_WO_I8_SYM

References NNFW_QUANTIZE_TYPE_I16_SYM, NNFW_QUANTIZE_TYPE_U8_ASYM, NNFW_QUANTIZE_TYPE_WO_I16_SYM, NNFW_QUANTIZE_TYPE_WO_I8_SYM, NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, NNFW_STATUS_NO_ERROR, onert::odc::ODC_QTYPE_I16_SYM, onert::odc::ODC_QTYPE_NOT_SET, onert::odc::ODC_QTYPE_U8_ASYM, onert::odc::ODC_QTYPE_WO_I16_SYM, and onert::odc::ODC_QTYPE_WO_I8_SYM.

◆ set_quantized_model_path()

NNFW_STATUS onert::api::Session::set_quantized_model_path ( const char *  path)

Definition at line 2163 of file Session.cc.

2164{
2165 try
2166 {
2167 if (isStateInitialized() || isStateRunning())
2168 {
2169 setLastErrorMessage("Error during Session::set_quantized_model_path : Invalid state");
2171 }
2172
2173 _quant_manager->exportModelPath(std::string(path));
2174 }
2175 catch (const std::exception &e)
2176 {
2177 setLastErrorMessage("Error during Session::set_quantized_model_path : " +
2178 std::string(e.what()));
2179 return NNFW_STATUS_ERROR;
2180 }
2181
2182 return NNFW_STATUS_NO_ERROR;
2183}

References NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, and NNFW_STATUS_NO_ERROR.

◆ set_signature_run()

NNFW_STATUS onert::api::Session::set_signature_run ( const char *  signature)

Definition at line 1102 of file Session.cc.

1103{
1104 if (!signature)
1105 {
1106 setLastErrorMessage("Error during Session::set_signature_run : signature is NULL");
1108 }
1109
1110 if (!isStatePreparedOrFinishedRun())
1111 {
1112 setLastErrorMessage("Error during Session::set_signature_run : Invalid state");
1114 }
1115
1116 for (const auto &[subg_idx, sig_str] : _signature_map)
1117 {
1118 if (sig_str == std::string(signature))
1119 {
1120 _execution =
1121 std::make_unique<onert::exec::Execution>(_compiler_artifact->_executors, subg_idx);
1122 return NNFW_STATUS_NO_ERROR;
1123 }
1124 }
1125
1126 setLastErrorMessage("Error during Session::set_signature_run : Cannot find signature \"" +
1127 std::string(signature) + "\"");
1128 return NNFW_STATUS_ERROR;
1129}

References NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, NNFW_STATUS_NO_ERROR, and NNFW_STATUS_UNEXPECTED_NULL.

◆ set_workspace()

NNFW_STATUS onert::api::Session::set_workspace ( const char *  dir)

Definition at line 1052 of file Session.cc.

1053{
1054 // TODO Check dir read & write permission
1055
1056 if (!dir)
1057 {
1058 setLastErrorMessage("Error during Session::set_workspace : dir is NULL");
1060 }
1061
1062 if (!isStateInitialized())
1063 {
1064 setLastErrorMessage("Error during Session::set_workspace : Invalid state");
1066 }
1067
1068 _coptions->workspace_dir = std::string(dir);
1069
1070 return NNFW_STATUS_NO_ERROR;
1071}

References NNFW_STATUS_INVALID_STATE, NNFW_STATUS_NO_ERROR, and NNFW_STATUS_UNEXPECTED_NULL.

◆ train_expected_tensorinfo()

NNFW_STATUS onert::api::Session::train_expected_tensorinfo ( uint32_t  index,
nnfw_tensorinfo ti 
)

Definition at line 1774 of file Session.cc.

1775{
1776 if (!isStatePreparedOrFinishedTraining())
1777 {
1778 setLastErrorMessage("Error during Session::train_expected_tensorinfo : Invalid state");
1780 }
1781
1782 // Check index is valid: [0, getExpectedSize())
1783
1784 // NYI
1785 (void)index;
1786 (void)ti;
1787 setLastErrorMessage("Error during Session::train_expected_tensorinfo : Not implemented yet");
1788 return NNFW_STATUS_ERROR;
1789}

References NNFW_STATUS_ERROR, and NNFW_STATUS_INVALID_STATE.

◆ train_export_checkpoint()

NNFW_STATUS onert::api::Session::train_export_checkpoint ( const char *  path)

Definition at line 2064 of file Session.cc.

2065{
2066 if (path == nullptr)
2067 {
2068 setLastErrorMessage("Error during Session::train_export_checkpoint : path is NULL");
2070 }
2071
2072 // Check training mode is enabled
2073 if (!isStateFinishedTraining())
2074 {
2075 setLastErrorMessage("Error during Session::train_export_checkpoint : Invalid state");
2077 }
2078
2079 try
2080 {
2081 onert::exporter::train::exportCheckpoint(path, _train_info, _execution);
2082 }
2083 catch (const std::exception &e)
2084 {
2085 setLastErrorMessage("Error during Session::train_export_checkpoint : " + std::string(e.what()));
2086 return NNFW_STATUS_ERROR;
2087 }
2088
2089 return NNFW_STATUS_NO_ERROR;
2090}
void exportCheckpoint(const std::string &filename, const std::unique_ptr< ir::train::TrainingInfo > &train_info, const std::unique_ptr< exec::Execution > &exec)

References onert::exporter::train::exportCheckpoint(), NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, NNFW_STATUS_NO_ERROR, and NNFW_STATUS_UNEXPECTED_NULL.

◆ train_export_circle()

NNFW_STATUS onert::api::Session::train_export_circle ( const char *  path)

Definition at line 1979 of file Session.cc.

1980{
1981 if (path == nullptr)
1982 {
1983 setLastErrorMessage("Error during Session::train_export_circle : path is NULL");
1985 }
1986
1987 // Check training mode is enabled
1988 if (!isStateFinishedTraining())
1989 {
1990 setLastErrorMessage("Error during Session::train_export_circle : Invalid state");
1992 }
1993
1994 try
1995 {
1996 onert::exporter::CircleExporter exporter(_model_path.string(), std::string{path});
1997 exporter.updateWeight(_execution);
1998 }
1999 catch (const std::exception &e)
2000 {
2001 setLastErrorMessage("Error during Session::train_export_circle : " + std::string(e.what()));
2002 return NNFW_STATUS_ERROR;
2003 }
2004
2005 return NNFW_STATUS_NO_ERROR;
2006}

References NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, NNFW_STATUS_NO_ERROR, NNFW_STATUS_UNEXPECTED_NULL, and onert::exporter::CircleExporter::updateWeight().

◆ train_export_circleplus()

NNFW_STATUS onert::api::Session::train_export_circleplus ( const char *  path)

Definition at line 2008 of file Session.cc.

2009{
2010 if (path == nullptr)
2011 {
2012 setLastErrorMessage("Error during Session::train_export_circleplus : path is NULL");
2014 }
2015
2016 if (!isStatePreparedOrFinishedTraining())
2017 {
2018 setLastErrorMessage("Error during Session::train_export_circleplus : Invalid state");
2020 }
2021
2022 try
2023 {
2024 onert::exporter::CircleExporter exporter(_model_path.string(), std::string{path});
2025 exporter.updateWeight(_execution);
2026 exporter.updateMetadata(_train_info);
2027 }
2028 catch (const std::exception &e)
2029 {
2030 setLastErrorMessage("Error during Session::train_export_circleplus : " + std::string(e.what()));
2031 return NNFW_STATUS_ERROR;
2032 }
2033
2034 return NNFW_STATUS_NO_ERROR;
2035}

References NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, NNFW_STATUS_NO_ERROR, NNFW_STATUS_UNEXPECTED_NULL, onert::exporter::CircleExporter::updateMetadata(), and onert::exporter::CircleExporter::updateWeight().

◆ train_get_loss()

NNFW_STATUS onert::api::Session::train_get_loss ( uint32_t  index,
float *  loss 
)

Definition at line 1945 of file Session.cc.

1946{
1947 if (loss == nullptr)
1948 {
1949 setLastErrorMessage("Error during Session::train_get_loss : loss is NULL");
1951 }
1952
1953 if (!isStateFinishedTraining())
1954 {
1955 setLastErrorMessage("Error during Session::train_get_loss : Invalid state");
1957 }
1958
1959 if (index >= getOutputSize())
1960 {
1961 setLastErrorMessage("Error during Session::train_get_loss : index is out of range");
1962 return NNFW_STATUS_ERROR;
1963 }
1964
1965 try
1966 {
1967 auto ind = onert::ir::IOIndex(index);
1968 *loss = _execution->getLoss(ind);
1969 }
1970 catch (const std::exception &e)
1971 {
1972 setLastErrorMessage("Error during Session::train_get_loss : " + std::string(e.what()));
1973 return NNFW_STATUS_ERROR;
1974 }
1975
1976 return NNFW_STATUS_NO_ERROR;
1977}

References NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, NNFW_STATUS_NO_ERROR, and NNFW_STATUS_UNEXPECTED_NULL.

◆ train_get_traininfo()

NNFW_STATUS onert::api::Session::train_get_traininfo ( nnfw_train_info info)

Definition at line 1497 of file Session.cc.

1498{
1499 if (isStateInitialized())
1500 {
1501 // There is no _train_info in INITIALIZED, since _train_info is set when a model loaded
1502 setLastErrorMessage("Error during Session::train_get_traininfo : Model is not loaded");
1504 }
1505
1506 if (info == nullptr)
1507 {
1508 setLastErrorMessage("Error during Session::train_get_traininfo : info is NULL");
1510 }
1511
1512 // after model loaded, it ensures that _train_info is not nullptr
1513 assert(_train_info != nullptr);
1514
1515 auto convertLossCode = [](const onert::ir::train::LossCode &code) -> NNFW_TRAIN_LOSS {
1516 switch (code)
1517 {
1524 default:
1525 throw std::runtime_error{"fail to convert ir::train::LossCode"};
1526 }
1527 };
1528
1529 auto convertLossReduction =
1531 switch (type)
1532 {
1539 default:
1540 throw std::runtime_error{"fail to convert from ir::train::LossReductionType"};
1541 break;
1542 }
1543 };
1544
1545 auto convertOptimizerCode =
1547 switch (code)
1548 {
1555 default:
1556 throw std::runtime_error{"fail to convert from ir::train::OptimizerCode"};
1557 }
1558 };
1559
1560 const auto &loss = _train_info->lossInfo();
1561 const auto &optim = _train_info->optimizerInfo();
1562
1563 try
1564 {
1565 info->learning_rate = optim.learning_rate;
1566 info->batch_size = _train_info->batchSize();
1567 info->loss_info.loss = convertLossCode(loss.loss_code);
1568 info->loss_info.reduction_type = convertLossReduction(loss.reduction_type);
1569 info->opt = convertOptimizerCode(optim.optim_code);
1570
1571 if (_train_info->getTrainableOps().size() > 0)
1572 {
1573 const uint32_t first_trainable_idx = _train_info->getTrainableOps().cbegin()->value();
1574 const uint32_t last_trainable_idx = _train_info->getTrainableOps().crbegin()->value();
1575 const uint32_t ops_size = primary_subgraph()->operations().size();
1576 const uint32_t trainable_indexes_range = last_trainable_idx - first_trainable_idx + 1;
1577
1578 // check if trainable ops set contains continuous indexes on the back of the set
1579 if (last_trainable_idx == ops_size - 1 &&
1580 trainable_indexes_range == _train_info->getTrainableOps().size())
1581 {
1582 // check if all ops are trainable
1583 if (0 == first_trainable_idx)
1584 {
1585 info->num_of_trainable_ops = NNFW_TRAIN_TRAINABLE_ALL;
1586 }
1587 else
1588 {
1589 info->num_of_trainable_ops = trainable_indexes_range;
1590 }
1591 }
1592 else
1593 {
1594 info->num_of_trainable_ops = NNFW_TRAIN_TRAINABLE_INCORRECT_STATE;
1595 setLastErrorMessage("Error during Session::train_get_traininfo : Conversion from set of "
1596 "trainable ops to num_of_trainable_ops is impossible");
1598 }
1599 }
1600 else
1601 {
1602 // no layer will be trained
1603 info->num_of_trainable_ops = NNFW_TRAIN_TRAINABLE_NONE;
1604 }
1605 }
1606 catch (const std::exception &e)
1607 {
1608 setLastErrorMessage("Error during Session::train_get_traininfo : " + std::string(e.what()));
1609 return NNFW_STATUS_ERROR;
1610 }
1611
1612 return NNFW_STATUS_NO_ERROR;
1613}
size_t size() const
Return the number of objects that the manager contains.
Code * code(const SessionID &sess)
Definition Session.cpp:54
@ NNFW_TRAIN_TRAINABLE_NONE
@ NNFW_TRAIN_TRAINABLE_ALL
@ NNFW_TRAIN_TRAINABLE_INCORRECT_STATE
NNFW_TRAIN_LOSS_REDUCTION
@ NNFW_TRAIN_LOSS_REDUCTION_UNDEFINED
@ NNFW_TRAIN_LOSS_REDUCTION_SUM
@ NNFW_TRAIN_LOSS_REDUCTION_SUM_OVER_BATCH_SIZE
NNFW_TRAIN_OPTIMIZER
@ NNFW_TRAIN_OPTIMIZER_ADAM
@ NNFW_TRAIN_OPTIMIZER_SGD
@ NNFW_TRAIN_OPTIMIZER_UNDEFINED
NNFW_TRAIN_LOSS
@ NNFW_TRAIN_LOSS_MEAN_SQUARED_ERROR
@ NNFW_TRAIN_LOSS_UNDEFINED
@ NNFW_TRAIN_LOSS_CATEGORICAL_CROSSENTROPY
virtual const Operations & operations() const =0

References onert::ir::train::Adam, onert::ir::train::CategoricalCrossentropy, info, onert::ir::train::MeanSquaredError, NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, NNFW_STATUS_NO_ERROR, NNFW_STATUS_UNEXPECTED_NULL, NNFW_TRAIN_LOSS_CATEGORICAL_CROSSENTROPY, NNFW_TRAIN_LOSS_MEAN_SQUARED_ERROR, NNFW_TRAIN_LOSS_REDUCTION_SUM, NNFW_TRAIN_LOSS_REDUCTION_SUM_OVER_BATCH_SIZE, NNFW_TRAIN_LOSS_REDUCTION_UNDEFINED, NNFW_TRAIN_LOSS_UNDEFINED, NNFW_TRAIN_OPTIMIZER_ADAM, NNFW_TRAIN_OPTIMIZER_SGD, NNFW_TRAIN_OPTIMIZER_UNDEFINED, NNFW_TRAIN_TRAINABLE_ALL, NNFW_TRAIN_TRAINABLE_INCORRECT_STATE, NNFW_TRAIN_TRAINABLE_NONE, onert::ir::train::SGD, onert::ir::train::Sum, onert::ir::train::SumOverBatchSize, type, and onert::ir::train::Undefined.

◆ train_import_checkpoint()

NNFW_STATUS onert::api::Session::train_import_checkpoint ( const char *  path)

Definition at line 2037 of file Session.cc.

2038{
2039 if (path == nullptr)
2040 {
2041 setLastErrorMessage("Error during Session::train_import_checkpoint : path is NULL");
2043 }
2044
2045 if (!isStatePreparedOrFinishedTraining())
2046 {
2047 setLastErrorMessage("Error during Session::train_import_checkpoint : Invalid state");
2049 }
2050
2051 try
2052 {
2053 onert::loader::train::loadCheckpoint(path, _train_info, _execution);
2054 }
2055 catch (const std::exception &e)
2056 {
2057 setLastErrorMessage("Error during Session::train_import_checkpoint : " + std::string(e.what()));
2058 return NNFW_STATUS_ERROR;
2059 }
2060
2061 return NNFW_STATUS_NO_ERROR;
2062}
void loadCheckpoint(const std::string &filename, const std::unique_ptr< ir::train::TrainingInfo > &train_info, const std::unique_ptr< exec::Execution > &exec)

References onert::loader::train::loadCheckpoint(), NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, NNFW_STATUS_NO_ERROR, and NNFW_STATUS_UNEXPECTED_NULL.

◆ train_input_tensorinfo()

NNFW_STATUS onert::api::Session::train_input_tensorinfo ( uint32_t  index,
nnfw_tensorinfo ti 
)

Definition at line 1757 of file Session.cc.

1758{
1759 if (!isStatePreparedOrFinishedTraining())
1760 {
1761 setLastErrorMessage("Error during Session::train_input_tensorinfo : Invalid state");
1763 }
1764
1765 // Check index is valid: [0, getInputSize())
1766
1767 // NYI
1768 (void)index;
1769 (void)ti;
1770 setLastErrorMessage("Error during Session::train_input_tensorinfo : Not implemented yet");
1771 return NNFW_STATUS_ERROR;
1772}

References NNFW_STATUS_ERROR, and NNFW_STATUS_INVALID_STATE.

◆ train_prepare()

NNFW_STATUS onert::api::Session::train_prepare ( )

Definition at line 1719 of file Session.cc.

1720{
1721 // We may need different state to represent training model is loaded
1722 if (!isStateModelLoaded())
1723 {
1724 if (_state == State::PREPARED_TRAINING)
1725 setLastErrorMessage("Error during Session::train_prepare : Training is already prepared");
1726 else
1727 setLastErrorMessage("Error during Session::train_prepare : Invalid state");
1729 }
1730
1731 // after model loaded, it ensures that _train_info is not nullptr
1732 assert(_train_info != nullptr);
1733
1734 try
1735 {
1736 if (not _train_info->isValid())
1737 throw std::runtime_error{"training info is not valid"};
1738
1739 // initialize trainingStep count
1740 _train_info->trainingStep() = 0;
1741
1743 std::move(_nnpkg), _coptions.get(), _train_info.get());
1744 _compiler_artifact = compiler->compile();
1745 _execution = std::make_unique<onert::exec::Execution>(_compiler_artifact->_executors);
1746 }
1747 catch (const std::exception &e)
1748 {
1749 setLastErrorMessage("Error during Session::train_prepare : " + std::string(e.what()));
1750 return NNFW_STATUS_ERROR;
1751 }
1752
1753 _state = State::PREPARED_TRAINING;
1754 return NNFW_STATUS_NO_ERROR;
1755}

References onert::compiler::CompilerFactory::create(), onert::compiler::CompilerFactory::get(), NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, and NNFW_STATUS_NO_ERROR.

◆ train_run()

NNFW_STATUS onert::api::Session::train_run ( bool  update_weights)

Definition at line 1911 of file Session.cc.

1912{
1913 if (!isStatePreparedOrFinishedTraining())
1914 {
1915 setLastErrorMessage("Error during Session::train_run : Invalid state");
1917 }
1918
1919 try
1920 {
1921 if (update_weights)
1922 {
1923 auto &training_step = _train_info->trainingStep();
1924 _execution->train(training_step++);
1925 }
1926 else
1927 _execution->execute();
1928 }
1930 {
1931 // Currently insufficient buffer always means output buffer.
1932 setLastErrorMessage("Error during Session::train_run : " + std::string(e.what()));
1934 }
1935 catch (const std::exception &e)
1936 {
1937 setLastErrorMessage("Error during Session::train_run : " + std::string(e.what()));
1938 return NNFW_STATUS_ERROR;
1939 }
1940
1941 _state = State::FINISHED_TRAINING;
1942 return NNFW_STATUS_NO_ERROR;
1943}

References NNFW_STATUS_ERROR, NNFW_STATUS_INSUFFICIENT_OUTPUT_SIZE, NNFW_STATUS_INVALID_STATE, NNFW_STATUS_NO_ERROR, and onert::Exception::what().

◆ train_set_expected()

NNFW_STATUS onert::api::Session::train_set_expected ( uint32_t  index,
const void *  expected,
const nnfw_tensorinfo expected_tensorinfo 
)

Definition at line 1834 of file Session.cc.

1836{
1837 if (expected == nullptr)
1838 {
1839 setLastErrorMessage("Error during Session::train_set_expected : expected is NULL");
1841 }
1842
1843 if (!isStatePreparedOrFinishedTraining())
1844 {
1845 setLastErrorMessage("Error during Session::train_set_expected : Invalid state");
1847 }
1848
1849 if (index >= getOutputSize())
1850 {
1851 setLastErrorMessage("Error during Session::train_set_expected : index is out of range");
1852 return NNFW_STATUS_ERROR;
1853 }
1854
1855 try
1856 {
1857 const auto ind = onert::ir::IOIndex{index};
1858 auto size = _execution->outputInfo(ind).total_size();
1859 if (expected_tensorinfo && getBufSize(expected_tensorinfo) != size)
1860 {
1861 setLastErrorMessage(
1862 "Error during Session::train_set_expected : Changing tensorinfo is not supported");
1863 return NNFW_STATUS_ERROR;
1864 }
1865
1866 // NOTE Find the loss input index
1867 // Input is added as many as the number of outputs.
1868 // The loss index is calculated from the value obtained by subtracting the
1869 // total output(added loss input) from the total input size.
1870 auto input_index = getInputSize() - getOutputSize() + index;
1871 auto input_ind = onert::ir::IOIndex(input_index);
1872 _execution->setInput(input_ind, expected, size);
1873 }
1874 catch (const std::exception &e)
1875 {
1876 setLastErrorMessage("Error during Session::train_set_expected : " + std::string(e.what()));
1877 return NNFW_STATUS_ERROR;
1878 }
1879
1880 return NNFW_STATUS_NO_ERROR;
1881}

References NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, NNFW_STATUS_NO_ERROR, NNFW_STATUS_UNEXPECTED_NULL, and size.

◆ train_set_input()

NNFW_STATUS onert::api::Session::train_set_input ( uint32_t  index,
const void *  input,
const nnfw_tensorinfo input_tensorinfo 
)

Definition at line 1791 of file Session.cc.

1793{
1794 if (input == nullptr)
1795 {
1796 setLastErrorMessage("Error during Session::train_set_input : input is NULL");
1798 }
1799
1800 if (!isStatePreparedOrFinishedTraining())
1801 {
1802 setLastErrorMessage("Error during Session::train_set_input : Invalid state");
1804 }
1805
1806 if (index >= getInputSize())
1807 {
1808 setLastErrorMessage("Error during Session::train_set_input : index is out of range");
1809 return NNFW_STATUS_ERROR;
1810 }
1811
1812 try
1813 {
1814 auto ind = onert::ir::IOIndex(index);
1815 auto size = _execution->inputInfo(ind).total_size();
1816 if (input_tensorinfo && getBufSize(input_tensorinfo) != size)
1817 {
1818 setLastErrorMessage(
1819 "Error during Session::train_set_input : Changing tensorinfo is not supported");
1820 return NNFW_STATUS_ERROR;
1821 }
1822
1823 _execution->setInput(ind, input, size);
1824 }
1825 catch (const std::exception &e)
1826 {
1827 setLastErrorMessage("Error during Session::train_set_input : " + std::string(e.what()));
1828 return NNFW_STATUS_ERROR;
1829 }
1830
1831 return NNFW_STATUS_NO_ERROR;
1832}
NNFW_STATUS input_tensorinfo(uint32_t index, nnfw_tensorinfo *ti)
Definition Session.cc:869

References NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, NNFW_STATUS_NO_ERROR, NNFW_STATUS_UNEXPECTED_NULL, and size.

◆ train_set_output()

NNFW_STATUS onert::api::Session::train_set_output ( uint32_t  index,
NNFW_TYPE  type,
void *  buffer,
size_t  length 
)

Definition at line 1883 of file Session.cc.

1885{
1886 if (!isStatePreparedOrFinishedTraining())
1887 {
1888 setLastErrorMessage("Error during Session::train_set_output : Invalid state");
1890 }
1891
1892 if (!buffer && length != 0)
1893 {
1894 setLastErrorMessage(
1895 "Error during Session::train_set_output : buffer is NULL but the length is not 0");
1896 return NNFW_STATUS_ERROR;
1897 }
1898
1899 try
1900 {
1901 _execution->setOutput(onert::ir::IOIndex(index), buffer, length);
1902 }
1903 catch (const std::exception &e)
1904 {
1905 setLastErrorMessage("Error during Session::train_set_output : " + std::string(e.what()));
1906 return NNFW_STATUS_ERROR;
1907 }
1908 return NNFW_STATUS_NO_ERROR;
1909}

References NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, and NNFW_STATUS_NO_ERROR.

◆ train_set_traininfo()

NNFW_STATUS onert::api::Session::train_set_traininfo ( const nnfw_train_info info)

Definition at line 1615 of file Session.cc.

1616{
1617 if (!isStateModelLoaded())
1618 {
1619 setLastErrorMessage("Error during Session::train_set_traininfo : Model is not loaded");
1621 }
1622
1623 if (info == nullptr)
1624 {
1625 setLastErrorMessage("Error during Session::train_set_traininfo : info is NULL");
1627 }
1628
1629 // after model loaded, it ensures that _train_info is not nullptr
1630 assert(_train_info != nullptr);
1631
1632 auto convertLossType = [](const int &type) {
1637 else
1638 throw std::runtime_error("not supported loss type");
1639 };
1640
1641 auto convertLossReductionType = [](const int &type) {
1646 else
1647 throw std::runtime_error("not supported loss reduction type");
1648 };
1649
1650 auto convertOptType = [](const int &type) {
1653 else if (type == NNFW_TRAIN_OPTIMIZER_ADAM)
1655 else
1656 throw std::runtime_error("not supported optimizer type");
1657 };
1658
1659 try
1660 {
1662 loss_info.loss_code = convertLossType(info->loss_info.loss);
1663 loss_info.reduction_type = convertLossReductionType(info->loss_info.reduction_type);
1664
1666 opt_info.learning_rate = info->learning_rate;
1667 opt_info.optim_code = convertOptType(info->opt);
1668
1669 _train_info->setBatchSize(info->batch_size);
1670 _train_info->setLossInfo(loss_info);
1671 _train_info->setOptimizerInfo(opt_info);
1672
1673 if (info->num_of_trainable_ops < -1)
1674 {
1675 setLastErrorMessage(
1676 "Error during Session::train_set_traininfo : Provided num_of_trainable_ops "
1677 "has incorrect value : " +
1678 std::to_string(info->num_of_trainable_ops));
1679 return NNFW_STATUS_ERROR;
1680 }
1681
1682 const uint32_t ops_size = primary_subgraph()->operations().size();
1683 std::set<onert::ir::OperationIndex> trainable_ops;
1684
1685 if (NNFW_TRAIN_TRAINABLE_ALL == info->num_of_trainable_ops)
1686 {
1687 for (uint32_t idx = 0; idx < ops_size; ++idx)
1688 {
1689 trainable_ops.emplace(idx);
1690 }
1691 }
1692 else
1693 {
1694 if (static_cast<uint32_t>(info->num_of_trainable_ops) > ops_size)
1695 {
1696 setLastErrorMessage(
1697 "Error during Session::train_set_traininfo : Provided num_of_trainable_ops "
1698 "is out of operators range : " +
1699 std::to_string(info->num_of_trainable_ops) + " > " + std::to_string(ops_size));
1700 return NNFW_STATUS_ERROR;
1701 }
1702 for (uint32_t i = 1; i <= static_cast<uint32_t>(info->num_of_trainable_ops); ++i)
1703 {
1704 trainable_ops.emplace(ops_size - i);
1705 }
1706 }
1707 // Note that possible setting an empty trainable_ops set (for NNFW_TRAIN_TRAINABLE_NONE value)
1708 _train_info->setTrainableOps(trainable_ops);
1709 }
1710 catch (const std::exception &e)
1711 {
1712 setLastErrorMessage("Error during Session::train_set_traininfo : " + std::string(e.what()));
1713 return NNFW_STATUS_ERROR;
1714 }
1715
1716 return NNFW_STATUS_NO_ERROR;
1717}
nnfw::cker::train::LossReductionType convertLossReductionType(ir::train::LossReductionType type)
convert loss reduction type
LossReductionType reduction_type
Definition LossInfo.h:44

References onert::ir::train::Adam, onert::ir::train::CategoricalCrossentropy, info, onert::ir::train::OptimizerInfo::learning_rate, onert::ir::train::LossInfo::loss_code, onert::ir::train::MeanSquaredError, NNFW_STATUS_ERROR, NNFW_STATUS_INVALID_STATE, NNFW_STATUS_NO_ERROR, NNFW_STATUS_UNEXPECTED_NULL, NNFW_TRAIN_LOSS_CATEGORICAL_CROSSENTROPY, NNFW_TRAIN_LOSS_MEAN_SQUARED_ERROR, NNFW_TRAIN_LOSS_REDUCTION_SUM, NNFW_TRAIN_LOSS_REDUCTION_SUM_OVER_BATCH_SIZE, NNFW_TRAIN_OPTIMIZER_ADAM, NNFW_TRAIN_OPTIMIZER_SGD, NNFW_TRAIN_TRAINABLE_ALL, onert::ir::train::OptimizerInfo::optim_code, onert::ir::train::LossInfo::reduction_type, onert::ir::train::SGD, onert::ir::train::Sum, onert::ir::train::SumOverBatchSize, and type.


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