[moco/tf] Import TFDepthwiseConv2d Op (#6001)
author채성우/On-Device Lab(SR)/Engineer/삼성전자 <sw4670.chae@samsung.com>
Tue, 30 Jul 2019 10:06:22 +0000 (10:06 +0000)
committer박세희/On-Device Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com>
Tue, 30 Jul 2019 10:06:22 +0000 (19:06 +0900)
* [moco/tf] Import TFDepthwiseConv2d Op

This commit is for importing TFDepthwiseConv2d Op.

Signed-off-by: seongwoo sw4670.chae@samsung.com
* This commit makes tensor_content of pbtxt human-readable.

compiler/moco-tf/src/Op/DepthwiseConv2dNative.cpp [new file with mode: 0644]
compiler/moco-tf/src/Op/DepthwiseConv2dNative.test.cpp [new file with mode: 0644]

diff --git a/compiler/moco-tf/src/Op/DepthwiseConv2dNative.cpp b/compiler/moco-tf/src/Op/DepthwiseConv2dNative.cpp
new file mode 100644 (file)
index 0000000..9f30b6a
--- /dev/null
@@ -0,0 +1,153 @@
+/*
+ * 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 <plier/tf/Convert.h>
+#include "GraphBuilder.h"
+
+#include "IR/TFDepthwiseConv2dNative.h"
+
+#include "Annotations/PaddingData.h"
+#include "Annotations/PadData.h"
+
+#include <moco/tf/Names.h>
+
+#include <loco/IR/PermutingCodec.h>
+#include <stdex/Memory.h>
+
+#include <tensorflow/core/framework/graph.pb.h>
+
+#include <cassert>
+
+namespace
+{
+using namespace moco::tf;
+
+class TFDepthwiseConv2dNativeGraphUpdate final : public GraphUpdate
+{
+public:
+  TFDepthwiseConv2dNativeGraphUpdate(TFDepthwiseConv2dNative *node, std::vector<TensorName> names)
+      : _node(node), _names(names)
+  {
+  }
+
+  void input(const SymbolTable *) const override;
+
+private:
+  TFDepthwiseConv2dNative *_node;
+  std::vector<TensorName> _names;
+};
+
+void TFDepthwiseConv2dNativeGraphUpdate::input(const SymbolTable *node_table) const
+{
+  assert(_names.size() == 2);
+
+  auto ifm_node = node_table->node(_names[0]);
+  auto ker_node = node_table->node(_names[1]);
+  assert(ifm_node != nullptr);
+  assert(ker_node != nullptr);
+
+  _node->ifm(ifm_node);
+  _node->ker(ker_node);
+}
+
+} // namespace
+
+namespace moco
+{
+namespace tf
+{
+
+/**
+ * @brief GraphBuilder for DepthwiseConv2dNative node
+ */
+class DepthwiseConv2dNativeGraphBuilder final : public GraphBuilder
+{
+public:
+  bool validate(const tensorflow::NodeDef &) const override;
+  void build(const tensorflow::NodeDef &, GraphBuilderContext *) const override;
+};
+
+bool DepthwiseConv2dNativeGraphBuilder::validate(const tensorflow::NodeDef &node) const
+{
+  assert(node.input_size() == 2);
+
+  auto data_layout = get_string_attr(node, "data_format");
+  if (!(data_layout == "NHWC" || data_layout == "NCHW"))
+  {
+    throw std::runtime_error("Not yet supported");
+  }
+
+  auto padding = moco::str_toupper(get_string_attr(node, "padding"));
+  assert(padding == "VALID" || padding == "SAME");
+
+  auto tf_strides = get_list_attr(node, "strides");
+  auto strides = as_int64_list(tf_strides);
+  assert(strides.size() == 4);
+  auto stride_n = strides.at(0);
+  auto stride_h = strides.at(1);
+  auto stride_w = strides.at(2);
+  auto stride_c = strides.at(3);
+  assert(stride_n == 1 && stride_c == 1);
+  assert(stride_h == stride_w);
+
+  // note: even though "data_format" and "dilations" are not entered when a model is written,
+  //       TF seems to generate those field into a pb file.
+  return has_attrs(node, {"T", "data_format", "dilations", "padding", "strides"});
+}
+
+void DepthwiseConv2dNativeGraphBuilder::build(const tensorflow::NodeDef &node,
+                                              GraphBuilderContext *context) const
+{
+  assert(context != nullptr);
+
+  loco::Graph *graph = context->graph();
+  SymbolTable *tensor_names = context->tensor_names();
+  UpdateQueue *updates = context->updates();
+
+  auto depthwiseconv2d_native_node = graph->nodes()->create<TFDepthwiseConv2dNative>();
+
+  // read attributes
+  auto data_layout = get_string_attr(node, "data_format");
+  depthwiseconv2d_native_node->data_layout(data_layout);
+
+  auto tf_strides = get_list_attr(node, "strides");
+  auto strides = as_int64_list(tf_strides);
+  depthwiseconv2d_native_node->strides(strides);
+
+  auto padding = moco::str_toupper(get_string_attr(node, "padding"));
+  depthwiseconv2d_native_node->padding(padding);
+
+  // save the name for graph link updates
+  TensorName output_name(node.name(), 0);
+  tensor_names->enroll(output_name, depthwiseconv2d_native_node);
+
+  std::vector<TensorName> input_names;
+  input_names.push_back(TensorName(node.input(0))); // input
+  input_names.push_back(TensorName(node.input(1))); // kernel
+
+  // Record ifm inputs to featureEncode_node
+  auto tfdepthwiseconv2dnative_update = stdex::make_unique<TFDepthwiseConv2dNativeGraphUpdate>(
+      depthwiseconv2d_native_node, input_names);
+
+  updates->enroll(std::move(tfdepthwiseconv2dnative_update));
+}
+
+} // namespace tf
+} // namespace moco
+
+#include "GraphBuilderRegistry.h"
+
+REGISTER_OP_BUILDER(DepthwiseConv2dNative, DepthwiseConv2dNativeGraphBuilder)
diff --git a/compiler/moco-tf/src/Op/DepthwiseConv2dNative.test.cpp b/compiler/moco-tf/src/Op/DepthwiseConv2dNative.test.cpp
new file mode 100644 (file)
index 0000000..7515ba7
--- /dev/null
@@ -0,0 +1,219 @@
+/*
+ * 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 "TestHelper.h"
+
+#include "Importer.h"
+#include "IR/TFDepthwiseConv2dNative.h"
+
+#include <loco/IR/TensorShape.h>
+#include <loco/IR/FeatureShape.h>
+#include <plier/tf/TestHelper.h>
+
+#include <gtest/gtest.h>
+
+#include <memory>
+
+using namespace moco::tf::test;
+
+namespace
+{
+// clang-format off
+const char *depthwise_conv2d_native_01_pbtxtdata = STRING_CONTENT(
+node {
+  name: "input"
+  op: "Placeholder"
+  attr {
+    key: "dtype"
+    value {
+      type: DT_FLOAT
+    }
+  }
+  attr {
+    key: "shape"
+    value {
+      shape {
+        dim {
+          size: 1
+        }
+        dim {
+          size: 4
+        }
+        dim {
+          size: 4
+        }
+        dim {
+          size: 3
+        }
+      }
+    }
+  }
+}
+node {
+  name: "filter"
+  op: "Placeholder"
+  attr {
+    key: "dtype"
+    value {
+      type: DT_FLOAT
+    }
+  }
+  attr {
+    key: "shape"
+    value {
+      shape {
+        dim {
+          size: 2
+        }
+        dim {
+          size: 2
+        }
+        dim {
+          size: 3
+        }
+        dim {
+          size: 2
+        }
+      }
+    }
+  }
+}
+node {
+  name: "depthwise/Shape"
+  op: "Const"
+  attr {
+    key: "dtype"
+    value {
+      type: DT_INT32
+    }
+  }
+  attr {
+    key: "value"
+    value {
+      tensor {
+        dtype: DT_INT32
+        tensor_shape {
+          dim {
+            size: 4
+          }
+        }
+        int_val: 2
+        int_val: 2
+        int_val: 3
+        int_val: 2
+      }
+    }
+  }
+}
+node {
+  name: "depthwise/dilation_rate"
+  op: "Const"
+  attr {
+    key: "dtype"
+    value {
+      type: DT_INT32
+    }
+  }
+  attr {
+    key: "value"
+    value {
+      tensor {
+        dtype: DT_INT32
+        tensor_shape {
+          dim {
+            size: 2
+          }
+        }
+        int_val: 1
+        int_val: 1
+      }
+    }
+  }
+}
+node {
+  name: "depthwise"
+  op: "DepthwiseConv2dNative"
+  input: "input"
+  input: "filter"
+  attr {
+    key: "T"
+    value {
+      type: DT_FLOAT
+    }
+  }
+  attr {
+    key: "data_format"
+    value {
+      s: "NHWC"
+    }
+  }
+  attr {
+    key: "dilations"
+    value {
+      list {
+        i: 1
+        i: 1
+        i: 1
+        i: 1
+      }
+    }
+  }
+  attr {
+    key: "padding"
+    value {
+      s: "VALID"
+    }
+  }
+  attr {
+    key: "strides"
+    value {
+      list {
+        i: 1
+        i: 1
+        i: 1
+        i: 1
+      }
+    }
+  }
+}
+);
+// clang-format on
+} // namespace
+
+TEST(TensorFlowImport, Depthwise_conv2d_native)
+{
+  moco::tf::Importer importer;
+  moco::tf::ModelSignature signature;
+
+  signature.add_input(moco::tf::TensorName("input", 0));
+  signature.add_output(moco::tf::TensorName("depthwise", 0));
+
+  tensorflow::GraphDef graph_def;
+
+  EXPECT_TRUE(plier::tf::parse_graphdef(depthwise_conv2d_native_01_pbtxtdata, graph_def));
+
+  std::unique_ptr<loco::Graph> graph = importer.import(signature, graph_def);
+
+  moco::tf::TFDepthwiseConv2dNative *tfdepthwiseconv2dnative =
+      moco::tf::test::find_first_node_bytype<moco::tf::TFDepthwiseConv2dNative>(graph.get());
+  ASSERT_NE(tfdepthwiseconv2dnative, nullptr);
+  ASSERT_NE(tfdepthwiseconv2dnative->ifm(), nullptr);
+  ASSERT_NE(tfdepthwiseconv2dnative->ker(), nullptr);
+
+  ASSERT_EQ(tfdepthwiseconv2dnative->padding(), "VALID");
+  ASSERT_EQ(tfdepthwiseconv2dnative->data_layout(), "NHWC");
+  ASSERT_EQ(tfdepthwiseconv2dnative->strides().size(), 4);
+}