[Flatbuffer] Create flatbuffer_interpreter.h
authorDongHak Park <donghak.park@samsung.com>
Thu, 9 Feb 2023 07:58:34 +0000 (16:58 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Mon, 27 Feb 2023 01:34:25 +0000 (10:34 +0900)
Create flatbuffer_interpreter.h
add FLATBUFFER Type into apis

for support flatbuffer write interpreter from GraphInterpreter, tflite_interpreter

Signed-off-by: DongHak Park <donghak.park@samsung.com>
api/ccapi/include/common.h
api/ccapi/include/model.h
api/nntrainer-api-common.h
nntrainer/compiler/flatbuffer_interpreter.cpp [new file with mode: 0644]
nntrainer/compiler/flatbuffer_interpreter.h [new file with mode: 0644]
nntrainer/models/neuralnet.cpp

index 486e3e3..813cc15 100644 (file)
@@ -29,6 +29,7 @@ namespace train {
 enum class ExportMethods {
   METHOD_STRINGVECTOR = 0, /**< export to a string vector */
   METHOD_TFLITE = 1,       /**< export to tflite */
+  METHOD_FLATBUFFER = 2,   /**< export to flatbuffer */
   METHOD_UNDEFINED = 999,  /**< undefined */
 };
 
index 42478dd..c772956 100644 (file)
@@ -65,6 +65,8 @@ for inference and training without any configurations*/
   MODEL_FORMAT_INI_WITH_BIN =
     ML_TRAIN_MODEL_FORMAT_INI_WITH_BIN, /**< ini file with save_path defined
                                            where the binary will be saved */
+  MODEL_FORMAT_FLATBUFFER =
+    ML_TRAIN_MODEL_FORMAT_FLATBUFFER, /**< flatbuffer file */
 };
 
 /**
@@ -174,7 +176,7 @@ public:
   virtual int addLayer(std::shared_ptr<Layer> layer) = 0;
 
   /**
-   * @brief add refering to reference layers.
+   * @brief add referring to reference layers.
    * @note This method does add the provided layers itself but adds a deep copy
    * of the passed layers to the model. The layers passed to this function can
    * be reused later.
index 0d0fe00..b11a884 100644 (file)
@@ -235,8 +235,10 @@ typedef enum {
   ML_TRAIN_MODEL_FORMAT_INI =
     1, /**< Ini format file saves model configurations. */
   ML_TRAIN_MODEL_FORMAT_INI_WITH_BIN =
-    2 /**< Ini with bin format file saves configurations with parameters
+    2, /**< Ini with bin format file saves configurations with parameters
          required for inference and training. */
+  ML_TRAIN_MODEL_FORMAT_FLATBUFFER =
+    3 /**< Flatbuffer format file saves model configurations and weights. */
 } ml_train_model_format_e;
 
 /**
diff --git a/nntrainer/compiler/flatbuffer_interpreter.cpp b/nntrainer/compiler/flatbuffer_interpreter.cpp
new file mode 100644 (file)
index 0000000..bdaf457
--- /dev/null
@@ -0,0 +1,11 @@
+// SPDX-License-Identifier: Apache-2.0
+/**
+ * Copyright (C) 2023 DongHak Park <donghak.park@samsung.com>
+ *
+ * @file flatbuffer_interpreter.cpp
+ * @date 09 February 2023
+ * @brief NNTrainer *.flatbuffer Interpreter
+ * @see        https://github.com/nnstreamer/nntrainer
+ * @author Donghak Park <donghak.park@samsung.com>
+ * @bug No known bugs except for NYI items
+ */
diff --git a/nntrainer/compiler/flatbuffer_interpreter.h b/nntrainer/compiler/flatbuffer_interpreter.h
new file mode 100644 (file)
index 0000000..3f79136
--- /dev/null
@@ -0,0 +1,58 @@
+// SPDX-License-Identifier: Apache-2.0
+/**
+ * Copyright (C) 2023 DongHak Park <donghak.park@samsung.com>
+ *
+ * @file flatbuffer_interpreter.h
+ * @date 09 February 2023
+ * @brief NNTrainer flatbuffer Interpreter
+ * @see        https://github.com/nnstreamer/nntrainer
+ * @author Donghak Park <donghak.park@samsung.com>
+ * @bug No known bugs except for NYI items
+ */
+
+#ifndef __FLATBUFFER_INTERPRETER_H__
+#define __FLATBUFFER_INTERPRETER_H__
+
+#include <app_context.h>
+#include <interpreter.h>
+
+namespace nntrainer {
+
+/**
+ * @brief flatbuffer graph interpreter class
+ *
+ */
+class FlatBufferInterpreter : public GraphInterpreter {
+public:
+  /**
+   * @brief Construct a new flatbuffer Graph Interpreter object
+   *
+   * @param app_context_ app context to create layers
+   */
+  FlatBufferInterpreter(AppContext &app_context_ = AppContext::Global()) :
+    app_context(app_context_) {}
+
+  /**
+   * @brief Destroy the flatbuffer Interpreter object
+   *
+   */
+  virtual ~FlatBufferInterpreter() = default;
+
+  /**
+   * @copydoc GraphInterpreter::serialize(const std::string &out)
+   */
+  void serialize(const GraphRepresentation &representation,
+                 const std::string &out) override;
+
+  /**
+   * @copydoc GraphInterpreter::deserialize(const std::string &in)
+   */
+  GraphRepresentation deserialize(const std::string &in) override;
+
+private:
+  AppContext &app_context;
+};
+
+} // namespace nntrainer
+
+#endif // __FLATBUFFER_INTERPRETER_H__
index 285133c..03a217d 100644 (file)
@@ -488,6 +488,9 @@ void NeuralNetwork::load(const std::string &file_path,
     throw_status(ret);
     break;
   }
+  case ml::train::ModelFormat::MODEL_FORMAT_FLATBUFFER: {
+    break;
+  }
   default:
     throw nntrainer::exception::not_supported(
       "loading with given format is not supported yet");
@@ -1282,6 +1285,12 @@ void NeuralNetwork::exports(const ml::train::ExportMethods &method,
 #endif
     break;
   }
+  case ml::train::ExportMethods::METHOD_FLATBUFFER: {
+
+    model_graph.deallocateTensors();
+    model_graph.allocateTensors(ExecutionMode::TRAIN);
+    break;
+  }
   default:
     throw std::runtime_error{"Unsupported export method"};
   }