From 61315a074fb5db9ebe4483c6db3c33d1c8a3f744 Mon Sep 17 00:00:00 2001 From: Jihoon Lee Date: Tue, 22 Jun 2021 20:33:36 +0900 Subject: [PATCH] [CAPI] Propose save/load api **Motivations** 1. Fine grained api required to save and load **Changes proposed in this PR:** - Add function to model.h / `save`, `load` - Mark `readModel()` deprecated (later remove...) - Mark `saveModel()` to change `exportTo` - Add `ModelSaveLoadFormat` - Create capi-machine-learning-training-tizen-internal for internal api support **Self evaluation:** 1. Build test: [X]Passed [ ]Failed [ ]Skipped 2. Run test: [X]Passed [ ]Failed [ ]Skipped Signed-off-by: Jihoon Lee --- api/capi/include/nntrainer-tizen-internal.h | 77 +++++++++++++++++++++++++++++ api/capi/meson.build | 4 ++ api/ccapi/include/model.h | 40 ++++++++++++++- api/nntrainer-api-common.h | 10 ++++ packaging/nntrainer.spec | 10 ++++ 5 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 api/capi/include/nntrainer-tizen-internal.h diff --git a/api/capi/include/nntrainer-tizen-internal.h b/api/capi/include/nntrainer-tizen-internal.h new file mode 100644 index 0000000..e2e3302 --- /dev/null +++ b/api/capi/include/nntrainer-tizen-internal.h @@ -0,0 +1,77 @@ +// SPDX-License-Identifier: Apache-2.0 +/** + * Copyright (C) 2021 Jihoon Lee + * + * @file nntrainer-tizen-internal.h + * @date 30 June 2021 + * @brief NNTrainer CAPI header for tizen interanl api. + * @note This header is designed to be used only in Tizen + * @see https://github.com/nnstreamer/nntrainer + * @author Jihoon Lee + * @bug No known bugs except for NYI items + * + */ + +#ifndef __NNTRAINER_TIZEN_INTERNAL_H__ +#define __NNTRAINER_TIZEN_INTERNAL_H__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/** + * @brief Save the model + * @details Use this function to save the current model. @a format + * describes various formats in which various selections of the + * parameters of the models can be saved. Some formats may save + * parameters required for training. Some other formats may save model + * configurations. Unless stated otherwise, @a ml_train_model_compile() has to + * be called upon the @a model before calling this function. + * @see @a ml_train_model_format_e to check which part of the model is + * saved + * @note This function overrides the existing file without any notification. + * + * @since_tizen 6.5 + * @param[in] model The NNTrainer model handle to save + * @param[in] file_path File path to save the file. + * @param[in] format Format flag to determine which format should be used to + * save + * @return @c 0 on success, Otherwise a negative error value + * @retval #ML_ERROR_NONE Successful. + * @retval #ML_ERROR_INVALID_PARAMETER The given @a file_path is + * invalid or @a model is not compiled. + */ +int ml_train_model_save(ml_train_model_h model, const char *file_path, + ml_train_model_format_e format); + +/** + * @brief Load the model + * @details Use this function to load the current model. @a format + * describes various formats in which various selections of the + * parameters of the models can be loaded. Some formats may load + * parameters required for training. Some other formats may load model + * configurations. Unless stated otherwise, @a ml_train_model_compile() has to + * be called upon the @a model before calling this function. + * + * @see @a ml_train_model_format_e to check which part of the model is + * loaded + * + * @since_tizen 6.5 + * @param[in] model The NNTrainer model handle to load. + * @param[in] file_path File path to load the file. + * @param[in] format Format flag to determine which format should be used to + * loaded + * @return @c 0 on success, Otherwise a negative error value + * @retval #ML_ERROR_NONE Successful. + * @retval #ML_ERROR_INVALID_PARAMETER The given @a file_path is + * invalid or @a model is not in valid state to load. + */ +int ml_train_model_load(ml_train_model_h model, const char *file_path, + ml_train_model_format_e format); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif // __NNTRAINER_TIZEN_INTERNAL_H__ diff --git a/api/capi/meson.build b/api/capi/meson.build index 6e52d8d..eee567e 100644 --- a/api/capi/meson.build +++ b/api/capi/meson.build @@ -16,6 +16,10 @@ capi_headers = [] capi_headers += meson.current_source_dir() / 'include' / 'nntrainer.h' capi_headers += meson.current_source_dir() / '..' / 'nntrainer-api-common.h' +if get_option('enable-tizen') + capi_headers += meson.current_source_dir() / 'include' / 'nntrainer-tizen-internal.h' +endif + capi_deps = [ nntrainer_ccapi_dep, nnstreamer_capi_dep, diff --git a/api/ccapi/include/model.h b/api/ccapi/include/model.h index 3f91857..0bc05ee 100644 --- a/api/ccapi/include/model.h +++ b/api/ccapi/include/model.h @@ -19,6 +19,7 @@ #if __cplusplus >= MIN_CPP_VERSION #include +#include #include #include @@ -41,6 +42,21 @@ enum class ModelType { }; /** + * @brief Model saving options + * + */ +enum class ModelFormat { + MODEL_FORMAT_BIN = + ML_TRAIN_MODEL_FORMAT_BIN, /**< raw bin file saves parameters required +for inference and training without any configurations*/ + + /** Comment intended */ + // MODEL_FORMAT_INI = 1, /**< ini file with save_path defined */ + // MODEL_FORMAT_INI_WITH_BIN = 2, /**< ini file with save_path + // defined */ +}; + +/** * @class Model Class * @brief Model Class containing configuration, layers, optimizer and dataset */ @@ -95,15 +111,37 @@ public: /** * @brief save model and training parameters into file + * @todo deprecate this */ - virtual void saveModel() = 0; + [[deprecated("use saveModel(const std::string &path_prefix, " + "ModelFormat format)")]] virtual void + saveModel() = 0; + + /** + * @brief load model states and training parameters from a file + * @param file_path file_path to save the model, if full path is not + * given, it should be saved inside working directory + * @param format format to save parameters + */ + virtual void save(const std::string &file_path, + ModelFormat format = ModelFormat::MODEL_FORMAT_BIN){}; /** * @brief read model and training parameters from file + * @todo deprecate this */ virtual void readModel() = 0; /** + * @brief load model with regard to the format + * @param file_path file_path to save the model, if full path is not + * given, it should be saved inside working directory + * @param format format to save parameters + */ + virtual void load(const std::string &file_path, + ModelFormat format = ModelFormat::MODEL_FORMAT_BIN){}; + + /** * @brief Run Model training and validation * @param[in] values hyper parameters * @retval #ML_ERROR_NONE Successful. diff --git a/api/nntrainer-api-common.h b/api/nntrainer-api-common.h index 74f4590..19d4ee4 100644 --- a/api/nntrainer-api-common.h +++ b/api/nntrainer-api-common.h @@ -177,6 +177,16 @@ typedef enum { } ml_train_summary_type_e; /** + * @brief Enumeration for the neural network + * + */ +typedef enum { + ML_TRAIN_MODEL_FORMAT_BIN = + 0, /**< raw bin file saves parameters required for inference and training + without any configurations*/ +} ml_train_model_format_e; + +/** * @} */ #ifdef __cplusplus diff --git a/packaging/nntrainer.spec b/packaging/nntrainer.spec index e5057f0..ed4dec0 100644 --- a/packaging/nntrainer.spec +++ b/packaging/nntrainer.spec @@ -214,6 +214,13 @@ Requires: capi-machine-learning-training-devel = %{version}-%{release} %description -n capi-machine-learning-training-devel-static Static library of capi-machine-learning-training-devel package. +%package tizen-internal-devel +Summary: Tizen internal headers for Tizen Machine Learning Training API +Group: Machine Learning/ML Framework +Requires: capi-machine-learning-training-devel = %{version}-%{release} +%description tizen-internal-devel +Tizen internal headers for Tizen Machine Learning Training API. + %if 0%{?support_ccapi} %package -n ccapi-machine-learning-training Summary: Tizen Native API for NNTrainer @@ -472,6 +479,9 @@ cp -r result %{buildroot}%{_datadir}/nntrainer/unittest/ %files -n capi-machine-learning-training-devel-static %{_libdir}/libcapi-nntrainer.a +%files tizen-internal-devel +%{_includedir}/nntrainer/nntrainer-tizen-internal.h + %if 0%{?support_ccapi} %files -n ccapi-machine-learning-training %manifest capi-machine-learning-training.manifest -- 2.7.4