[morph] Introduce dims.h (#1432)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Tue, 11 Sep 2018 01:17:48 +0000 (10:17 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Tue, 11 Sep 2018 01:17:48 +0000 (10:17 +0900)
This commit introduces dims modulue which will include helpers
for dims object creation from various shapes.

The current implementation supports only a helper for tensor shape.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/morph/include/morph/dims.h [new file with mode: 0644]
contrib/morph/src/dims.cpp [new file with mode: 0644]
contrib/morph/src/dims.test.cpp [new file with mode: 0644]

diff --git a/contrib/morph/include/morph/dims.h b/contrib/morph/include/morph/dims.h
new file mode 100644 (file)
index 0000000..93190d6
--- /dev/null
@@ -0,0 +1,17 @@
+#ifndef __MORPH_DIMS_H__
+#define __MORPH_DIMS_H__
+
+#include <nncc/core/ADT/tensor/Shape.h>
+
+#include <vector>
+
+namespace morph
+{
+
+template <typename T> using Dims = std::vector<T>;
+
+Dims<uint32_t> as_dims(const nncc::core::ADT::tensor::Shape &);
+
+} // namespace morph
+
+#endif // __MORPH_DIMS_H__
diff --git a/contrib/morph/src/dims.cpp b/contrib/morph/src/dims.cpp
new file mode 100644 (file)
index 0000000..e7db50c
--- /dev/null
@@ -0,0 +1,20 @@
+#include "morph/dims.h"
+
+using namespace nncc::core::ADT;
+
+namespace morph
+{
+
+Dims<uint32_t> as_dims(const tensor::Shape &shape)
+{
+  Dims<uint32_t> res;
+
+  for (uint32_t n = 0; n < shape.rank(); ++n)
+  {
+    res.emplace_back(shape.dim(n));
+  }
+
+  return res;
+}
+
+} // namespace morph
diff --git a/contrib/morph/src/dims.test.cpp b/contrib/morph/src/dims.test.cpp
new file mode 100644 (file)
index 0000000..5223594
--- /dev/null
@@ -0,0 +1,16 @@
+#include "morph/dims.h"
+
+#include <gtest/gtest.h>
+
+using namespace nncc::core::ADT;
+
+TEST(DimsTest, as_dims_from_tensor)
+{
+  auto dims = morph::as_dims(tensor::Shape{1, 3, 4, 5});
+
+  ASSERT_EQ(dims.size(), 4);
+  ASSERT_EQ(dims.at(0), 1);
+  ASSERT_EQ(dims.at(1), 3);
+  ASSERT_EQ(dims.at(2), 4);
+  ASSERT_EQ(dims.at(3), 5);
+}