[loco] Introduce FeatureShape class (#3448)
author박종현/On-Device Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Tue, 14 May 2019 03:50:38 +0000 (12:50 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Tue, 14 May 2019 03:50:38 +0000 (12:50 +0900)
This commit introduces FeautreShape class which describes the shape of
Feature Map.

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

diff --git a/contrib/loco/include/loco/IR/FeatureShape.h b/contrib/loco/include/loco/IR/FeatureShape.h
new file mode 100644 (file)
index 0000000..d09a2b2
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __LOCO_IR_FEATURE_SHAPE_H__
+#define __LOCO_IR_FEATURE_SHAPE_H__
+
+#include "loco/IR/Dimension.h"
+
+namespace loco
+{
+
+/**
+ * \brief Feature Map Shape
+ *
+ * This class describes the shape of feature maps, which serves as the input/output of 2D
+ * convolutional operations (e.g. Convolution).
+ *
+ * Each feature map is a collection of 3D features conceptually.
+ * Each feature has depth, height, width.
+ *
+ * count() refers to the number of features in a feature map
+ * depth() refers to the depth of features in a given feature map
+ * height() refers to the height of features in a given feature map
+ * width() refers to the width of features in a given feature map
+ */
+class FeatureShape final
+{
+public:
+  FeatureShape() = default;
+
+public:
+  const Dimension &count(void) const { return _count; }
+  Dimension &count(void) { return _count; }
+
+  const Dimension &depth(void) const { return _depth; }
+  Dimension &depth(void) { return _depth; }
+
+  const Dimension &height(void) const { return _height; }
+  Dimension &height(void) { return _height; }
+
+  const Dimension &width(void) const { return _width; }
+  Dimension &width(void) { return _width; }
+
+private:
+  Dimension _count;
+  Dimension _depth;
+  Dimension _height;
+  Dimension _width;
+};
+
+} // namespace loco
+
+#endif // __LOCO_IR_FEATURE_SHAPE_H__
diff --git a/contrib/loco/src/IR/FeatureShape.test.cpp b/contrib/loco/src/IR/FeatureShape.test.cpp
new file mode 100644 (file)
index 0000000..69d793b
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "loco/IR/FeatureShape.h"
+
+#include <gtest/gtest.h>
+
+TEST(FeatureShapeTest, default_constructor)
+{
+  loco::FeatureShape shape;
+
+  ASSERT_FALSE(shape.count().known());
+  ASSERT_FALSE(shape.depth().known());
+  ASSERT_FALSE(shape.height().known());
+  ASSERT_FALSE(shape.width().known());
+}
+
+TEST(FeatureShapeTest, settet_and_getter)
+{
+  loco::FeatureShape shape;
+
+  // Set count
+  shape.count() = loco::make_dimension(2);
+
+  ASSERT_TRUE(shape.count().known());
+  ASSERT_FALSE(shape.depth().known());
+  ASSERT_FALSE(shape.height().known());
+  ASSERT_FALSE(shape.width().known());
+
+  ASSERT_EQ(shape.count().value(), 2);
+
+  // Set depth
+  shape.depth() = loco::make_dimension(3);
+
+  ASSERT_TRUE(shape.count().known());
+  ASSERT_TRUE(shape.depth().known());
+  ASSERT_FALSE(shape.height().known());
+  ASSERT_FALSE(shape.width().known());
+
+  ASSERT_EQ(shape.count().value(), 2);
+  ASSERT_EQ(shape.depth().value(), 3);
+
+  // Set height
+  shape.height() = loco::make_dimension(4);
+
+  ASSERT_TRUE(shape.count().known());
+  ASSERT_TRUE(shape.depth().known());
+  ASSERT_TRUE(shape.height().known());
+  ASSERT_FALSE(shape.width().known());
+
+  ASSERT_EQ(shape.count().value(), 2);
+  ASSERT_EQ(shape.depth().value(), 3);
+  ASSERT_EQ(shape.height().value(), 4);
+
+  // Set width
+  shape.width() = loco::make_dimension(5);
+
+  ASSERT_TRUE(shape.count().known());
+  ASSERT_TRUE(shape.depth().known());
+  ASSERT_TRUE(shape.height().known());
+  ASSERT_TRUE(shape.width().known());
+
+  ASSERT_EQ(shape.count().value(), 2);
+  ASSERT_EQ(shape.depth().value(), 3);
+  ASSERT_EQ(shape.height().value(), 4);
+  ASSERT_EQ(shape.width().value(), 5);
+}