Add shape helper (#325)
authorDmitry Mozolev/AI Tools Lab /SRR/Engineer/삼성전자 <d.mozolev@samsung.com>
Thu, 14 Jun 2018 13:57:36 +0000 (16:57 +0300)
committerSergey Vostokov/AI Tools Lab /SRR/Staff Engineer/삼성전자 <s.vostokov@samsung.com>
Thu, 14 Jun 2018 13:57:36 +0000 (16:57 +0300)
Add shape helper

This commits adds a common class that importers can use to
carry out various operations on shapes useful during model import.

Signed-off-by: Dmitry Mozolev <d.mozolev@samsung.com>
contrib/nnc/libs/frontend/CMakeLists.txt
contrib/nnc/libs/frontend/common/shape_helper.cpp [new file with mode: 0644]
contrib/nnc/libs/frontend/include/shape_helper.h [new file with mode: 0644]

index f793ded..9cb550c 100644 (file)
@@ -11,5 +11,8 @@ add_library(${nn_import_common} STATIC ${common_sources} ${common_headers})
 target_include_directories(${nn_import_common} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
 set_target_properties(${nn_import_common} PROPERTIES POSITION_INDEPENDENT_CODE ON)
 
+target_link_libraries(${nn_import_common} PRIVATE nncc_core)
+target_link_libraries(${nn_import_common} PRIVATE nnc_plugin_core)
+
 add_subdirectory(tflite)
 add_subdirectory(caffe)
diff --git a/contrib/nnc/libs/frontend/common/shape_helper.cpp b/contrib/nnc/libs/frontend/common/shape_helper.cpp
new file mode 100644 (file)
index 0000000..627d743
--- /dev/null
@@ -0,0 +1,37 @@
+#include <vector>
+
+#include "shape_helper.h"
+#include "PluginException.h"
+
+namespace nncc
+{
+namespace contrib
+{
+namespace frontend
+{
+namespace common
+{
+
+Shape &ShapeHelper::cutOffBatchDim(Shape &shape)
+{
+  if (shape.dim(0) != 1)
+  {
+    throw PluginException{"While attempting to cut off tensor batch dimension (first one),"
+                          "found that it is not 1. Check the model being imported, if the first"
+                          "dimension of the input is not 1, then it might be not batch, and the"
+                          "code needs some restructuring"};
+  }
+
+  for (unsigned int i = 0; i < shape.rank() - 1; ++i)
+  {
+    shape.dim(i) = shape.dim(i + 1);
+  }
+  shape.resize(shape.rank() - 1);
+
+  return shape;
+}
+
+} // namespace common
+} // namespace frontend
+} // namespace contrib
+} // namespace nncc
diff --git a/contrib/nnc/libs/frontend/include/shape_helper.h b/contrib/nnc/libs/frontend/include/shape_helper.h
new file mode 100644 (file)
index 0000000..edd5210
--- /dev/null
@@ -0,0 +1,46 @@
+#ifndef NNCC_SHAPE_HELPER_H
+#define NNCC_SHAPE_HELPER_H
+
+#include "nncc/core/ADT/tensor/Shape.h"
+
+namespace nncc
+{
+namespace contrib
+{
+namespace frontend
+{
+namespace common
+{
+
+using nncc::core::ADT::tensor::Shape;
+
+class ShapeHelper
+{
+public:
+  template<typename Iterable>
+  static Shape createShape(const Iterable &iter, std::size_t);
+
+  static Shape &cutOffBatchDim(Shape &shape);
+};
+
+template<typename Iterable>
+Shape ShapeHelper::createShape(const Iterable &iter, std::size_t size)
+{
+  Shape sh;
+  sh.resize(static_cast<uint32_t>(size));
+
+  unsigned int i = 0;
+  for (auto dim : iter)
+  {
+    sh.dim(i++) = dim;
+  }
+
+  return sh;
+}
+
+} // namespace common
+} // namespace frontend
+} // namespace contrib
+} // namespace nncc
+
+#endif // NNCC_SHAPE_HELPER_H