[coco] Support Sqrt operator in coco (#2790)
author장지섭/On-Device Lab(SR)/Engineer/삼성전자 <jiseob.jang@samsung.com>
Wed, 9 Jan 2019 04:25:53 +0000 (13:25 +0900)
committer박종현/On-Device Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Wed, 9 Jan 2019 04:25:53 +0000 (13:25 +0900)
This commit adds Sqrt op in coco.

Signed-off-by: jiseob.jang <jiseob.jang@samsung.com>
contrib/coco/core/include/coco/IR/Op.lst
contrib/coco/core/include/coco/IR/Ops.h
contrib/coco/core/src/IR/OpManager.test.cpp
contrib/coco/core/src/IR/Sqrt.test.cpp [new file with mode: 0644]

index 2bdb7c8..a3028bd 100644 (file)
@@ -12,6 +12,7 @@ OP(PadF)
 OP(ReLU)
 OP(ReLU6)
 OP(Add)
+OP(Sqrt)
 OP(Sub)
 OP(Mul)
 OP(Div)
index 40cc489..4f2169d 100644 (file)
@@ -390,6 +390,23 @@ private:
   Axis _axis = Axis::Unknown;
 };
 
+/**
+ * @brief Apply Sqrt over elements
+ */
+class Sqrt final : public UnaryOp
+{
+public:
+  explicit Sqrt() = default;
+
+public:
+  Sqrt(const Sqrt &) = delete;
+  Sqrt(Sqrt &&) = delete;
+
+public:
+  Sqrt *asSqrt(void) override { return this; }
+  const Sqrt *asSqrt(void) const override { return this; }
+};
+
 } // namesapce coco
 
 #endif // __COCO_IR_OPS_H__
index 0af6ac7..9d463b3 100644 (file)
@@ -61,6 +61,13 @@ TEST_F(OpManagerTest, ReLU6)
   ASSERT_NE(obj, nullptr);
 }
 
+TEST_F(OpManagerTest, Sqrt)
+{
+  auto obj = mgr.create<coco::Sqrt>();
+
+  ASSERT_NE(obj, nullptr);
+}
+
 TEST_F(OpManagerTest, Sub)
 {
   auto obj = mgr.create<coco::Sub>();
diff --git a/contrib/coco/core/src/IR/Sqrt.test.cpp b/contrib/coco/core/src/IR/Sqrt.test.cpp
new file mode 100644 (file)
index 0000000..cf9b232
--- /dev/null
@@ -0,0 +1,85 @@
+/*
+ * Copyright (c) 2018 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 "coco/IR/Ops.h"
+
+#include <memory>
+#include <vector>
+
+#include <gtest/gtest.h>
+
+namespace
+{
+struct IsSqrt : public coco::Op::Visitor<bool>
+{
+  bool visit(const coco::Sqrt *) override { return true; }
+};
+
+class SqrtTest : public ::testing::Test
+{
+public:
+  SqrtTest()
+  {
+    // DO NOTHING
+  }
+
+protected:
+  coco::Sqrt *allocate(void)
+  {
+    auto op = new coco::Sqrt;
+    _allocated.emplace_back(op);
+    return op;
+  }
+
+private:
+  std::vector<std::unique_ptr<coco::Sqrt>> _allocated;
+};
+} // namespace
+
+TEST_F(SqrtTest, initialization)
+{
+  auto op = allocate();
+
+  // uses() should be empty on construction
+  ASSERT_EQ(op->uses().size(), 0);
+  // parent() should be nullptr on construction
+  ASSERT_EQ(op->parent(), nullptr);
+
+  ASSERT_EQ(op->arg(), nullptr);
+}
+
+TEST_F(SqrtTest, asSqrt)
+{
+  auto op = allocate();
+
+  coco::Op *mutable_base = op;
+  const coco::Op *immutable_base = op;
+
+  ASSERT_EQ(mutable_base->asSqrt(), op);
+  ASSERT_EQ(mutable_base->asSqrt(), immutable_base->asSqrt());
+}
+
+TEST_F(SqrtTest, accept)
+{
+  // Test 'Sqrt' class
+  auto op = allocate();
+
+  coco::Sqrt *mutable_ptr = op;
+  const coco::Sqrt *immutable_ptr = op;
+
+  ASSERT_TRUE(mutable_ptr->accept(IsSqrt{}));
+  ASSERT_TRUE(immutable_ptr->accept(IsSqrt{}));
+}