[moco-tf] Introduce TFDialect for Tanh (#6835)
author남궁석/On-Device Lab(SR)/Engineer/삼성전자 <sk.namkoong@samsung.com>
Thu, 22 Aug 2019 09:54:13 +0000 (18:54 +0900)
committer박세희/On-Device Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com>
Thu, 22 Aug 2019 09:54:13 +0000 (18:54 +0900)
* [moco-tf] Introduce TFDialect for Tanh

This commit will introduce TFDialect for `Tanh`

Signed-off-by: Seok NamKoong <sk.namkoong@samsung.com>
* Add missing code

compiler/moco-tf/src/Dialect/TFNodes.h
compiler/moco-tf/src/Dialect/TFNodes.lst
compiler/moco-tf/src/Dialect/TFShapeInferenceRule.cpp
compiler/moco-tf/src/Dialect/TFTypeInferenceRule.cpp
compiler/moco-tf/src/IR/TFTanh.h [new file with mode: 0644]
compiler/moco-tf/src/IR/TFTanh.test.cpp [new file with mode: 0644]
compiler/moco-tf/src/Transforms/FixShapeTransform.cpp

index 3edd043..c5d5d4e 100644 (file)
@@ -40,5 +40,6 @@
 #include "IR/TFSqueeze.h"
 #include "IR/TFStopGradient.h"
 #include "IR/TFSub.h"
+#include "IR/TFTanh.h"
 
 #endif // __MOCO_TF_DIALECT_TFNODES_H__
index d946cf0..87d811e 100644 (file)
@@ -30,3 +30,4 @@ TENSORFLOW_NODE(SquaredDifference, TFSquaredDifference)
 TENSORFLOW_NODE(Squeeze, TFSqueeze)
 TENSORFLOW_NODE(StopGradient, TFStopGradient)
 TENSORFLOW_NODE(Sub, TFSub)
+TENSORFLOW_NODE(Tanh, TFTanh)
index 55108ba..be0767e 100644 (file)
@@ -63,6 +63,7 @@ public:
   // TODO TFSqueeze
   // TODO TFStopGradient
   // TODO TFSub
+  // TODO TFTanh
 
   // temporary default fallback to use legacy ShapeInferenceData
   // TODO remove using ShapeInferenceData
@@ -113,6 +114,8 @@ public:
 
 // TODO TFSub
 
+// TODO TFTanh
+
 // temporary default fallback to use ShapeInferenceData
 // TODO remove using ShapeInferenceData
 loco::NodeShape ForwardShapeInferenceAlgorithm::visit(const TFNode *node)
index d905135..7d05503 100644 (file)
@@ -58,6 +58,7 @@ struct TypeForwardAlgorithm final : public moco::tf::TFNodeVisitor<loco::DataTyp
   loco::DataType visit(const TFSqueeze *node) { return dtype_get(node->input()); }
   loco::DataType visit(const TFStopGradient *node) { return dtype_get(node->input()); }
   loco::DataType visit(const TFSub *node) { return dtype_get(node->x()); }
+  loco::DataType visit(const TFTanh *node) { return dtype_get(node->x()); }
 };
 
 } // namespace
diff --git a/compiler/moco-tf/src/IR/TFTanh.h b/compiler/moco-tf/src/IR/TFTanh.h
new file mode 100644 (file)
index 0000000..eebee4b
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * 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 __MOCO_TF_IR_TFTANH_H__
+#define __MOCO_TF_IR_TFTANH_H__
+
+#include "Dialect/TFNodeDecl.h"
+
+namespace moco
+{
+namespace tf
+{
+
+class TFTanh final : public loco::FixedArityNode<1, TFNodeImpl<TFOpcode::Tanh>>
+{
+public:
+  TFTanh() = default;
+
+public:
+  Node *x(void) const { return at(0)->node(); }
+  void x(Node *node) { at(0)->node(node); }
+};
+
+} // namespace tf
+} // namespace moco
+
+#endif // __MOCO_TF_IR_TFTANH_H__
diff --git a/compiler/moco-tf/src/IR/TFTanh.test.cpp b/compiler/moco-tf/src/IR/TFTanh.test.cpp
new file mode 100644 (file)
index 0000000..0ff1af6
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * 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 "IR/TFTanh.h"
+
+#include "Dialect/TFDialect.h"
+
+#include <gtest/gtest.h>
+
+TEST(TFTanhTest, constructor)
+{
+  moco::tf::TFTanh tanh_node;
+
+  ASSERT_EQ(tanh_node.dialect(), moco::tf::TFDialect::get());
+  ASSERT_EQ(tanh_node.opcode(), moco::tf::TFOpcode::Tanh);
+
+  ASSERT_EQ(tanh_node.x(), nullptr);
+}
index acea640..428114b 100644 (file)
@@ -1629,6 +1629,13 @@ bool fix_shape(moco::tf::TFSub *node)
   return copy_shapedata(x, node);
 }
 
+bool fix_shape(moco::tf::TFTanh *node)
+{
+  // Output shape is same as the input
+  auto x = node->x();
+  return copy_shapedata(x, node);
+}
+
 bool fix_shape(locoex::COpCall *node)
 {
   // if node already has ShapeInferenceData, skip