[moco-tf] Introduce TFMaxPool IR (#5975)
author박세희/On-Device Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com>
Mon, 29 Jul 2019 06:21:05 +0000 (15:21 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Mon, 29 Jul 2019 06:21:05 +0000 (15:21 +0900)
This will introduce TFMaxPool IR in TensorFlow Dialect for MaxPool node

Signed-off-by: SaeHie Park <saehie.park@samsung.com>
compiler/moco-tf/src/Dialect/TFNodes.h
compiler/moco-tf/src/Dialect/TFNodes.lst
compiler/moco-tf/src/IR/TFMaxPool.h [new file with mode: 0644]
compiler/moco-tf/src/IR/TFMaxPool.test.cpp [new file with mode: 0644]
compiler/moco-tf/src/Transforms/FixPaddingTransform.cpp
compiler/moco-tf/src/Transforms/FixShapeTransform.cpp

index c927279..39b8743 100644 (file)
@@ -25,6 +25,7 @@
 #include "IR/TFDepthwiseConv2dNative.h"
 #include "IR/TFFusedBatchNorm.h"
 #include "IR/TFIdentity.h"
+#include "IR/TFMaxPool.h"
 #include "IR/TFMul.h"
 #include "IR/TFReshape.h"
 #include "IR/TFRsqrt.h"
index a07834c..96d0346 100644 (file)
@@ -15,6 +15,7 @@ TENSORFLOW_NODE(Conv2D, TFConv2D)
 TENSORFLOW_NODE(DepthwiseConv2dNative, TFDepthwiseConv2dNative)
 TENSORFLOW_NODE(FusedBatchNorm, TFFusedBatchNorm)
 TENSORFLOW_NODE(Identity, TFIdentity)
+TENSORFLOW_NODE(MaxPool, TFMaxPool)
 TENSORFLOW_NODE(Mul, TFMul)
 TENSORFLOW_NODE(Reshape, TFReshape)
 TENSORFLOW_NODE(Rsqrt, TFRsqrt)
diff --git a/compiler/moco-tf/src/IR/TFMaxPool.h b/compiler/moco-tf/src/IR/TFMaxPool.h
new file mode 100644 (file)
index 0000000..847e85d
--- /dev/null
@@ -0,0 +1,104 @@
+/*
+ * 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_TFMAXPOOL_H__
+#define __MOCO_TF_IR_TFMAXPOOL_H__
+
+#include "Dialect/TFNodeDecl.h"
+
+#include <vector>
+
+namespace moco
+{
+namespace tf
+{
+
+/// @note TFMaxPool corresponds to the following GraphDef
+/*
+node {
+  name: "maxpool2d"
+  op: "MaxPool"
+  input: "placeholder"
+  attr {
+    key: "T"
+    value {
+      type: DT_FLOAT
+    }
+  }
+  attr {
+    key: "data_format"
+    value {
+      s: "NHWC"
+    }
+  }
+  attr {
+    key: "ksize"
+    value {
+      list {
+        i: 1 i: 2 i: 2 i: 1
+      }
+    }
+  }
+  attr {
+    key: "padding"
+    value {
+      s: "VALID"
+    }
+  }
+  attr {
+    key: "strides"
+    value {
+      list {
+        i: 1 i: 1 i: 1 i: 1
+      }
+    }
+  }
+}
+*/
+
+class TFMaxPool final : public loco::FixedArityNode<1, TFNodeImpl<TFOpcode::MaxPool>>
+{
+public:
+  TFMaxPool() = default;
+
+public:
+  Node *value(void) const { return at(0)->node(); }
+  void value(Node *node) { return at(0)->node(node); }
+
+public:
+  const TFDataLayout &data_layout(void) const { return _data_layout; }
+  void data_layout(const TFDataLayout &data_layout) { _data_layout = data_layout; }
+
+  const TFPadding &padding(void) const { return _padding; }
+  void padding(const TFPadding &padding) { _padding = padding; }
+
+  const std::vector<int64_t> &ksize(void) const { return _ksize; }
+  void ksize(const std::vector<int64_t> &ksize) { _ksize = ksize; }
+
+  const std::vector<int64_t> &strides(void) const { return _strides; }
+  void strides(const std::vector<int64_t> &strides) { _strides = strides; }
+
+private:
+  TFDataLayout _data_layout;
+  TFPadding _padding;
+  std::vector<int64_t> _ksize;
+  std::vector<int64_t> _strides;
+};
+
+} // namespace tf
+} // namespace moco
+
+#endif // __MOCO_TF_IR_TFMAXPOOL_H__
diff --git a/compiler/moco-tf/src/IR/TFMaxPool.test.cpp b/compiler/moco-tf/src/IR/TFMaxPool.test.cpp
new file mode 100644 (file)
index 0000000..b86e21e
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * 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/TFMaxPool.h"
+
+#include "Dialect/TFDialect.h"
+
+#include <gtest/gtest.h>
+
+TEST(TFMaxPoolTest, constructor)
+{
+  moco::tf::TFMaxPool maxpool;
+
+  ASSERT_EQ(maxpool.dialect(), moco::tf::TFDialect::get());
+  ASSERT_EQ(maxpool.opcode(), moco::tf::TFOpcode::MaxPool);
+
+  ASSERT_EQ(maxpool.value(), nullptr);
+  ASSERT_EQ(maxpool.data_layout(), "");
+  ASSERT_EQ(maxpool.padding(), "");
+  ASSERT_EQ(maxpool.ksize(), std::vector<int64_t>({}));
+  ASSERT_EQ(maxpool.strides(), std::vector<int64_t>({}));
+}
index 617d7a5..3d89717 100644 (file)
@@ -556,6 +556,12 @@ bool fix_padding(moco::tf::TFIdentity *node)
   return false;
 }
 
+bool fix_padding(moco::tf::TFMaxPool *node)
+{
+  // TODO implement
+  throw std::runtime_error("NYI fix_padding TFMaxPool");
+}
+
 bool fix_padding(moco::tf::TFMul *node)
 {
   // Nothing to do with padding
index ec3e5d2..c75f347 100644 (file)
@@ -827,6 +827,12 @@ bool fix_shape(moco::tf::TFIdentity *node)
   return copy_shapedata(input, node);
 }
 
+bool fix_shape(moco::tf::TFMaxPool *node)
+{
+  // TODO implement
+  throw std::runtime_error("NYI fix_shape TFMaxPool");
+}
+
 bool fix_shape(moco::tf::TFMul *node)
 {
   auto x = node->x();