[CAPI] Add ml_train_model_run_with_single_param() internally for C# DllImport
authorHyunil <hyunil46.park@samsung.com>
Tue, 24 May 2022 05:39:02 +0000 (14:39 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Tue, 31 May 2022 04:39:45 +0000 (13:39 +0900)
ml_train_model_run() has a va_list and this is a problem with DllImport in C#.
va_list must be passed dynamically for each architect, this is not guaranteed to
work for some architects. This api receive model compile param as a single string
from C# DllImport.

**Self evaluation:**
1. Build test:   [X]Passed [ ]Failed [ ]Skipped
2. Run test:     [X]Passed [ ]Failed [ ]Skipped

Signed-off-by: Hyunil <hyunil46.park@samsung.com>
api/capi/include/nntrainer_internal.h
api/capi/src/nntrainer.cpp

index 841d4c3424573767be2294b683014cecff00dafc..7424dedc5b7f64506980ee0e496e1ee01eb0f14e 100644 (file)
@@ -334,6 +334,27 @@ int ml_train_model_insert_layer(ml_train_model_h model, ml_train_layer_h layer,
  * 9")
  */
 int ml_train_model_compile_with_single_param(ml_train_model_h model,
+                                             const char *single_param);
+
+/**
+ * @brief Trains the neural network model with params.
+ * @details Use this function to train the compiled neural network model with
+ * the passed training hyperparameters. This function will return once the
+ * training, along with requested validation and testing, is completed.
+ * @since_tizen 7.0
+ * @param[in] model The NNTrainer model handle.
+ * @param[in] single_param Hyperparameters for train model.
+ * @return @c 0 on success. Otherwise a negative error value.
+ * @retval #ML_ERROR_NONE Successful.
+ * @retval #ML_ERROR_NOT_SUPPORTED Not supported.
+ * @retval #ML_ERROR_INVALID_PARAMETER Invalid parameter.
+ * API to solve va_list issue of Dllimport of C# interop.
+ * The input format of single_param must be 'key = value' format, and it
+ * received as shown in the example below. delimiter is '|'. e.g)
+ * ml_train_model_run_with_single_param(model, "epochs=2 | batch_size=16")
+ */
+int ml_train_model_run_with_single_param(ml_train_model_h model,
+                                         const char *single_param);
 
 #if defined(__TIZEN__)
 /**
index 4a3a053ee60b15d6919374efe18e66f55f9c6451..ec74dc96c0ac5a26eb4da179216495c21d7466a1 100644 (file)
@@ -290,10 +290,10 @@ int ml_train_model_construct_with_conf(const char *model_conf,
   return status;
 }
 
-static std::vector<std::string> split_param(std::string singgle_param,
+static std::vector<std::string> split_param(std::string single_param,
                                             char delimiter) {
   std::vector<std::string> param_list;
-  std::stringstream sstream(singgle_param);
+  std::stringstream sstream(single_param);
   std::string param;
 
   while (std::getline(sstream, param, delimiter))
@@ -389,6 +389,43 @@ static int nntrainer_model_run(ml_train_model_h model,
   return status;
 }
 
+int ml_train_model_run_with_single_param(ml_train_model_h model,
+                                         const char *single_param) {
+
+  std::vector<std::string> param_list;
+
+  check_feature_state();
+  ML_TRAIN_VERIFY_VALID_HANDLE(model);
+
+  if (single_param)
+    param_list = split_param(single_param, '|');
+
+  return nntrainer_model_run(model, param_list);
+}
+
+int ml_train_model_run(ml_train_model_h model, ...) {
+  int status = ML_ERROR_NONE;
+  ml_train_model *nnmodel;
+  const char *data;
+  std::shared_ptr<ml::train::Model> m;
+
+  check_feature_state();
+
+  ML_TRAIN_VERIFY_VALID_HANDLE(model);
+
+  std::vector<std::string> arg_list;
+  va_list arguments;
+  va_start(arguments, model);
+
+  while ((data = va_arg(arguments, const char *))) {
+    arg_list.push_back(data);
+  }
+
+  va_end(arguments);
+
+  return nntrainer_model_run(model, arg_list);
+}
+
 int ml_train_model_destroy(ml_train_model_h model) {
   int status = ML_ERROR_NONE;
   ml_train_model *nnmodel;