From 46645df6bfdb53897c0f2e695a11c297cb7411f4 Mon Sep 17 00:00:00 2001 From: Jiyoung Yun Date: Mon, 22 Jan 2024 18:14:33 +0900 Subject: [PATCH] [onert] Add compilation apis This commit adds compliation APIs to nnfw_experimental.h file. ONE-DCO-1.0-Signed-off-by: Jiyoung Yun --- runtime/onert/api/include/nnfw_experimental.h | 45 +++++++++++++ runtime/onert/api/src/nnfw_api.cc | 18 +++++ runtime/onert/api/src/nnfw_api_internal.cc | 65 +++++++++++++++++++ runtime/onert/api/src/nnfw_api_internal.h | 4 ++ 4 files changed, 132 insertions(+) diff --git a/runtime/onert/api/include/nnfw_experimental.h b/runtime/onert/api/include/nnfw_experimental.h index dd7aeb5e4f7..1bf0fa93a9d 100644 --- a/runtime/onert/api/include/nnfw_experimental.h +++ b/runtime/onert/api/include/nnfw_experimental.h @@ -448,6 +448,51 @@ NNFW_STATUS nnfw_set_quantized_model_path(nnfw_session *session, const char *pat */ NNFW_STATUS nnfw_quantize(nnfw_session *session); +/** + * @brief Preference for compilation + */ +typedef enum +{ + /** Use the default configuration */ + NNFW_COMPILE_PREF_DEFAULT, + /** Do best efforts to compile model for performance */ + NNFW_COMPILE_PREF_PERFORMANCE_FIRST, + /** Do best efforts to compile model for reducing host memory usage */ + NNFW_COMPILE_PREF_MEMORY_FIRST, + /** Do best efforts to compile model for reducing compilation time */ + NNFW_COMPILE_PREF_COMPILE_TIME_FIRST, +} NNFW_COMPILE_PREF; + +/** + * @brief Set preference for backend compilation + * + * @param[in] session nnfw_session the session to set preference + * @param[in] pref @c NNFW_COMPILE_PREF + * @return @c NNFW_STATUS_NO_ERROR if successful, otherwise return @c NNFW_STATUS_ERROR + */ +NNFW_STATUS nnfw_set_compile_preference(nnfw_session *session, NNFW_COMPILE_PREF pref); + +/** + * @brief Set exported compiled model path + * + * This function should be called before {@link nnfw_compile} is invoked. + * + * TODO: If this function is not called, compiled model will not be exported + * + * @param[in] session nnfw_session to set compiled model path + * @param[in] path Compiled model path + * @return @c NNFW_STATUS_NO_ERROR if successful, otherwise return @c NNFW_STATUS_ERROR + */ +NNFW_STATUS nnfw_set_compiled_model_path(nnfw_session *session, const char *path); + +/** + * @brief Compile the model + * + * @param[in] session nnfw_session the session which contains information about compilation + * @return @c NNFW_STATUS_NO_ERROR if successful, otherwise return @c NNFW_STATUS_ERROR + */ +NNFW_STATUS nnfw_compile(nnfw_session *session); + #ifdef __cplusplus } #endif diff --git a/runtime/onert/api/src/nnfw_api.cc b/runtime/onert/api/src/nnfw_api.cc index dd2da19eb07..169983996b9 100644 --- a/runtime/onert/api/src/nnfw_api.cc +++ b/runtime/onert/api/src/nnfw_api.cc @@ -478,3 +478,21 @@ NNFW_STATUS nnfw_quantize(nnfw_session *session) NNFW_RETURN_ERROR_IF_NULL(session); return session->quantize(); } + +NNFW_STATUS nnfw_set_compile_preference(nnfw_session *session, NNFW_COMPILE_PREF pref) +{ + NNFW_RETURN_ERROR_IF_NULL(session); + return session->set_compile_preference(pref); +} + +NNFW_STATUS nnfw_set_compiled_model_path(nnfw_session *session, const char *path) +{ + NNFW_RETURN_ERROR_IF_NULL(session); + return session->set_compiled_model_path(path); +} + +NNFW_STATUS nnfw_compile(nnfw_session *session) +{ + NNFW_RETURN_ERROR_IF_NULL(session); + return session->compile(); +} diff --git a/runtime/onert/api/src/nnfw_api_internal.cc b/runtime/onert/api/src/nnfw_api_internal.cc index 6e21eaa4969..2160b8558f1 100644 --- a/runtime/onert/api/src/nnfw_api_internal.cc +++ b/runtime/onert/api/src/nnfw_api_internal.cc @@ -1833,3 +1833,68 @@ NNFW_STATUS nnfw_session::quantize() return NNFW_STATUS_NO_ERROR; } + +NNFW_STATUS nnfw_session::set_compile_preference(NNFW_COMPILE_PREF pref) +{ + try + { + if (!isStateModelLoaded()) + { + std::cerr << "invalid state" << std::endl; + return NNFW_STATUS_INVALID_STATE; + } + + // TODO Set preference to compile manager + UNUSED_RELEASE(pref); + } + catch (const std::exception &e) + { + std::cerr << "Error during nnfw_session::set_compile_preference : " << e.what() << std::endl; + return NNFW_STATUS_ERROR; + } + + return NNFW_STATUS_NO_ERROR; +} + +NNFW_STATUS nnfw_session::set_compiled_model_path(const char *path) +{ + try + { + if (!isStateModelLoaded()) + { + std::cerr << "invalid state" << std::endl; + return NNFW_STATUS_INVALID_STATE; + } + + // TODO Set model path for compiled file + UNUSED_RELEASE(path); + } + catch (const std::exception &e) + { + std::cerr << "Error during nnfw_session::set_compiled_model_path : " << e.what() << std::endl; + return NNFW_STATUS_ERROR; + } + + return NNFW_STATUS_NO_ERROR; +} + +NNFW_STATUS nnfw_session::compile() +{ + try + { + if (!isStateModelLoaded()) + { + std::cerr << "invalid state" << std::endl; + return NNFW_STATUS_INVALID_STATE; + } + + // TODO Compile model + } + catch (const std::exception &e) + { + std::cerr << "Error during nnfw_session::compile : " << e.what() << std::endl; + return NNFW_STATUS_ERROR; + } + + return NNFW_STATUS_NO_ERROR; +} diff --git a/runtime/onert/api/src/nnfw_api_internal.h b/runtime/onert/api/src/nnfw_api_internal.h index 93d61c2cb09..aadfc9c8641 100644 --- a/runtime/onert/api/src/nnfw_api_internal.h +++ b/runtime/onert/api/src/nnfw_api_internal.h @@ -188,6 +188,10 @@ struct nnfw_session NNFW_STATUS set_quantized_model_path(const char *path); NNFW_STATUS quantize(); + NNFW_STATUS set_compile_preference(NNFW_COMPILE_PREF pref); + NNFW_STATUS set_compiled_model_path(const char *path); + NNFW_STATUS compile(); + private: const onert::ir::IGraph *primary_subgraph(); uint32_t getInputSize();