Introduce a way to support all TFLite versions in one library (#172)
authorDmitry Mozolev/AI Tools Lab/Engineer/삼성전자 <d.mozolev@samsung.com>
Wed, 30 May 2018 13:54:11 +0000 (16:54 +0300)
committerSergey Vostokov/AI Tools Lab/Staff Engineer/삼성전자 <s.vostokov@samsung.com>
Wed, 30 May 2018 13:54:11 +0000 (16:54 +0300)
* Introduce a way to support all TFLite versions in one library

It is not obvious how to support all TFLite model versions in one
library, and this commit changes the structure of the code to show
how other tflite versions can be accommodated.

Signed-off-by: Dmitry Mozolev <d.mozolev@samsung.com>
contrib/nnc/libs/frontend/tflite/CMakeLists.txt
contrib/nnc/libs/frontend/tflite/examples/sanity_check.cpp
contrib/nnc/libs/frontend/tflite/include/schema_v3.h [new file with mode: 0644]
contrib/nnc/libs/frontend/tflite/include/tflite_dump_visitor.h
contrib/nnc/libs/frontend/tflite/include/tflite_importer.h [deleted file]
contrib/nnc/libs/frontend/tflite/include/tflite_importer.inline.h [new file with mode: 0644]
contrib/nnc/libs/frontend/tflite/include/tflite_v3_importer.h [new file with mode: 0644]
contrib/nnc/libs/frontend/tflite/include/tflite_visitor.h
contrib/nnc/libs/frontend/tflite/include/tflite_walker.h
contrib/nnc/libs/frontend/tflite/src/tflite_importer.inline.cpp [moved from contrib/nnc/libs/frontend/tflite/src/tflite_importer.cpp with 78% similarity]
contrib/nnc/libs/frontend/tflite/src/tflite_v3_importer.cpp [new file with mode: 0644]

index 4f2a39f..f7c2ce3 100644 (file)
@@ -12,7 +12,9 @@ FlatBuffers_Generate(FB_GEN
 # TFLITE importer #
 ###################
 
-file(GLOB tflite_importer_sources src/*)
+set(tflite_importer_sources ${CMAKE_CURRENT_SOURCE_DIR}/src/tflite_walker.cpp
+                            ${CMAKE_CURRENT_SOURCE_DIR}/src/tflite_dump_visitor.cpp
+                            ${CMAKE_CURRENT_SOURCE_DIR}/src/tflite_v3_importer.cpp)
 file(GLOB tflite_importer_headers include/*.h)
 list(APPEND tflite_importer_headers ${FB_GEN_SOURCES})
 
index a92e794..b01fcae 100644 (file)
@@ -1,6 +1,6 @@
 #include <iostream>
 
-#include "tflite_importer.h"
+#include "tflite_v3_importer.h"
 
 int main(int argc, char **argv)
 {
@@ -14,7 +14,7 @@ int main(int argc, char **argv)
     modelName = "mobilenet_v1.0.tflite";
   }
 
-  nncc::contrib::frontend::tflite::TfliteImporter importer{modelName};
+  nncc::contrib::frontend::tflite::v3::TfliteImporter importer{modelName};
 
   bool success = importer.import();
 
diff --git a/contrib/nnc/libs/frontend/tflite/include/schema_v3.h b/contrib/nnc/libs/frontend/tflite/include/schema_v3.h
new file mode 100644 (file)
index 0000000..30cd688
--- /dev/null
@@ -0,0 +1,8 @@
+#ifndef NNCC_SCHEMA_V3_H
+#define NNCC_SCHEMA_V3_H
+
+#define tflite v3_tflite
+#include "schema_v3_generated.h"
+#undef tflite
+
+#endif // NNCC_SCHEMA_V3_H
index 8b489f7..048a313 100644 (file)
@@ -3,8 +3,10 @@
 
 #include <vector>
 
-#include "schema_v3_generated.h"
 #include "tflite_visitor.h"
+#include "schema_v3.h"
+
+using namespace v3_tflite;
 
 namespace nncc
 {
diff --git a/contrib/nnc/libs/frontend/tflite/include/tflite_importer.h b/contrib/nnc/libs/frontend/tflite/include/tflite_importer.h
deleted file mode 100644 (file)
index 89e2f8b..0000000
+++ /dev/null
@@ -1,45 +0,0 @@
-#ifndef NNCC_TFLITE_IMPORTER_H
-#define NNCC_TFLITE_IMPORTER_H
-
-#include <memory>
-
-#include "schema_v3_generated.h"
-
-#include "nn_importer.h"
-#include "model_allocation.h"
-
-namespace nncc
-{
-namespace contrib
-{
-namespace frontend
-{
-namespace tflite
-{
-
-using namespace nncc::contrib::frontend::common;
-using namespace ::tflite;
-
-class TfliteImporter : NNImporter
-{
-public:
-  explicit TfliteImporter(std::string filename);
-
-  bool import() override;
-  void *createIR() override;
-  void dump() override;
-
-  bool importUnpacked();
-
-protected:
-  std::unique_ptr<ModelAllocation> modelRaw;
-  std::unique_ptr<ModelT> model;
-  const Model *modelPacked;
-};
-
-} // namespace tflite
-} // namespace frontend
-} // namespace contrib
-} // namespace nncc
-
-#endif // NNCC_TFLITE_IMPORTER_H
diff --git a/contrib/nnc/libs/frontend/tflite/include/tflite_importer.inline.h b/contrib/nnc/libs/frontend/tflite/include/tflite_importer.inline.h
new file mode 100644 (file)
index 0000000..1f553fc
--- /dev/null
@@ -0,0 +1,19 @@
+using namespace nncc::contrib::frontend::common;
+
+class TfliteImporter : NNImporter
+{
+public:
+  explicit TfliteImporter(std::string filename);
+
+  bool import() override;
+  void *createIR() override;
+  void dump() override;
+
+  bool importUnpacked();
+
+protected:
+  std::unique_ptr<ModelAllocation> modelRaw;
+  std::unique_ptr<ModelT> model;
+  const Model *modelPacked;
+};
+
diff --git a/contrib/nnc/libs/frontend/tflite/include/tflite_v3_importer.h b/contrib/nnc/libs/frontend/tflite/include/tflite_v3_importer.h
new file mode 100644 (file)
index 0000000..683f7e3
--- /dev/null
@@ -0,0 +1,31 @@
+#ifndef NNCC_TFLITE_V3_IMPORTER_H
+#define NNCC_TFLITE_V3_IMPORTER_H
+
+#include <memory>
+#include <string>
+
+#include "schema_v3.h"
+#include "nn_importer.h"
+#include "model_allocation.h"
+
+namespace nncc
+{
+namespace contrib
+{
+namespace frontend
+{
+namespace tflite
+{
+namespace v3
+{
+
+using namespace ::v3_tflite;
+#include "tflite_importer.inline.h"
+
+} // namespace v3
+} // namespace contrib
+} // namespace tflite
+} // namespace frontend
+} // namespace nncc
+
+#endif // NNCC_TFLITE_V3_IMPORTER_H
index 9ef9e8e..a189b0f 100644 (file)
@@ -1,9 +1,9 @@
 #ifndef NNCC_TFLITE_VISITOR_H
 #define NNCC_TFLITE_VISITOR_H
 
-#include "schema_v3_generated.h"
+#include "schema_v3.h"
 
-using namespace tflite;
+using namespace v3_tflite;
 
 namespace nncc
 {
index e1425e0..de002e0 100644 (file)
@@ -6,10 +6,10 @@
 #include <vector>
 #include <memory>
 
-#include "schema_v3_generated.h"
+#include "schema_v3.h"
 #include "tflite_visitor.h"
 
-using namespace tflite;
+using namespace v3_tflite;
 
 namespace nncc
 {
@@ -1,16 +1,3 @@
-#include "tflite_importer.h"
-#include "tflite_dump_visitor.h"
-#include "tflite_walker.h"
-
-namespace nncc
-{
-namespace contrib
-{
-namespace frontend
-{
-namespace tflite
-{
-
 TfliteImporter::TfliteImporter(std::string filename)
 {
   modelRaw.reset(new ModelAllocation{std::move(filename)});
@@ -63,7 +50,3 @@ void TfliteImporter::dump()
   walker.walk(modelPacked);
 }
 
-} // namespace tflite
-} // namespace frontend
-} // namespace contrib
-} // namespace nncc
diff --git a/contrib/nnc/libs/frontend/tflite/src/tflite_v3_importer.cpp b/contrib/nnc/libs/frontend/tflite/src/tflite_v3_importer.cpp
new file mode 100644 (file)
index 0000000..420722e
--- /dev/null
@@ -0,0 +1,26 @@
+#include <iostream>
+
+#include "tflite_v3_importer.h"
+#include "tflite_dump_visitor.h"
+#include "tflite_walker.h"
+
+namespace nncc
+{
+namespace contrib
+{
+namespace frontend
+{
+namespace tflite
+{
+namespace v3
+{
+
+using namespace ::v3_tflite;
+
+#include "tflite_importer.inline.cpp"
+
+} // namespace v3
+} // namespace tflite
+} // namespace frontend
+} // namespace contrib
+} // namespace nncc