[moco-tf] Support TFMean Canonicalization (#7594)
author남궁석/On-Device Lab(SR)/Engineer/삼성전자 <sk.namkoong@samsung.com>
Thu, 19 Sep 2019 04:05:44 +0000 (13:05 +0900)
committer박종현/On-Device Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Thu, 19 Sep 2019 04:05:44 +0000 (13:05 +0900)
* [moco-tf] Support TFMean Canonicalization

This commit will enable for supporting `TFMean` canonicalization

Signed-off-by: Seok NamKoong <sk.namkoong@samsung.com>
* apply comments

compiler/moco-tf/src/Canonicalization/MeanCanonicalizer.cpp [new file with mode: 0644]
compiler/moco-tf/src/Canonicalization/MeanCanonicalizer.h [new file with mode: 0644]
compiler/moco-tf/src/Canonicalizer.cpp
compiler/moco-tf/src/TFReduceCanonicalzeHelper.h [new file with mode: 0644]

diff --git a/compiler/moco-tf/src/Canonicalization/MeanCanonicalizer.cpp b/compiler/moco-tf/src/Canonicalization/MeanCanonicalizer.cpp
new file mode 100644 (file)
index 0000000..fef77a1
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * 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 "MeanCanonicalizer.h"
+#include "TFReduceCanonicalzeHelper.h"
+
+namespace moco
+{
+namespace tf
+{
+
+bool MeanCanonicalizer::run(loco::Graph *graph)
+{
+  auto active_nodes = loco::active_nodes(loco::output_nodes(graph));
+  bool changed = false;
+
+  for (auto node : active_nodes)
+  {
+    if (node->dialect() == TFDialect::get())
+    {
+      auto tf_mean = dynamic_cast<moco::tf::TFMean *>(node);
+      if (tf_mean != nullptr)
+      {
+        if (canonicalize_reduce_node(tf_mean))
+          changed = true;
+      }
+    }
+  }
+
+  return changed;
+}
+
+} // namespace tf
+} // namespace moco
diff --git a/compiler/moco-tf/src/Canonicalization/MeanCanonicalizer.h b/compiler/moco-tf/src/Canonicalization/MeanCanonicalizer.h
new file mode 100644 (file)
index 0000000..857c8b8
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * 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_MEAN_CANONICALIZER_H__
+#define __MOCO_TF_MEAN_CANONICALIZER_H__
+
+#include "Transform.h"
+
+#include <loco.h>
+
+namespace moco
+{
+namespace tf
+{
+
+/**
+ * @brief Canonicalize TF-dialect TFMean into canonical TensorReduce(Mean) node
+ */
+class MeanCanonicalizer : public Transform
+{
+public:
+  const char *name(void) const final { return "MeanCanonicalizer"; }
+
+public:
+  bool run(loco::Graph *graph) override;
+};
+
+} // namespace tf
+} // namespace moco
+
+#endif // __MOCO_TF_MEAN_CANONICALIZER_H__
index c705d68..6f39ca3 100644 (file)
@@ -31,6 +31,7 @@
 #include "Canonicalization/DepthwiseConv2dNativeCanonicalizer.h"
 #include "Canonicalization/IdentityCanonicalizer.h"
 #include "Canonicalization/MaxPoolCanonicalizer.h"
+#include "Canonicalization/MeanCanonicalizer.h"
 #include "Canonicalization/MulCanonicalizer.h"
 #include "Canonicalization/RealDivCanonicalizer.h"
 #include "Canonicalization/ReluCanonicalizer.h"
@@ -101,6 +102,7 @@ void Canonicalizer::canonicalize(loco::Graph *g) const
   phase.emplace_back(stdex::make_unique<DepthwiseConv2dNativeCanonicalizer>());
   phase.emplace_back(stdex::make_unique<IdentityCanonicalizer>());
   phase.emplace_back(stdex::make_unique<MaxPoolCanonicalizer>());
+  phase.emplace_back(stdex::make_unique<MeanCanonicalizer>());
   phase.emplace_back(stdex::make_unique<MulCanonicalizer>());
   phase.emplace_back(stdex::make_unique<RealDivCanonicalizer>());
   phase.emplace_back(stdex::make_unique<ReluCanonicalizer>());
diff --git a/compiler/moco-tf/src/TFReduceCanonicalzeHelper.h b/compiler/moco-tf/src/TFReduceCanonicalzeHelper.h
new file mode 100644 (file)
index 0000000..88d285c
--- /dev/null
@@ -0,0 +1,118 @@
+/*
+ * 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 __TF_REDUCE_CANONICALIZE_HELPER_H__
+#define __TF_REDUCE_CANONICALIZE_HELPER_H__
+
+#include "Dialect/TFDialect.h"
+#include "Dialect/TFNodes.h"
+
+#include <loco/Service/ShapeInference.h>
+
+#include <moco/Log.h>
+
+namespace
+{
+
+template <typename TFNodeT> loco::ReduceFunc reduceFunc(void);
+
+template <> loco::ReduceFunc reduceFunc<moco::tf::TFMean>(void) { return loco::ReduceFunc::Mean; }
+
+template <typename TFNode> bool canonicalize_reduce_node(TFNode *node)
+{
+  LOGGER(l);
+
+  INFO(l) << "TFNodeCanonicalize ReduceNode begin";
+
+  auto graph = node->graph();
+
+  /**
+   * This will replace T/F Reduce node with a corresponding Canonical Reduce node
+   *
+   * BEFORE
+   *   reduction_indices -------- T/F Node -- C
+   *               input -------/
+   *
+   * AFTER
+   *                      +------ T/F Node --
+   *                      |     /
+   *   reduction_indices -------
+   *                      |     \
+   *               input -+------ Canonical Node -- C
+   *
+   * NOTE
+   *   - T/F Node is disconnected from C after transformation
+   */
+
+  // TFSqueeze had to be inserted if keep_dims() was false
+  assert(node->keep_dims());
+
+  auto axes_node = node->reduction_indices();
+  assert(axes_node != nullptr);
+
+  auto node_tensor_shape = loco::shape_get(node).template as<loco::TensorShape>();
+
+  // Canonicalization into TensorReduce is valid when reduction indices is constant
+  // TODO Support general TensorReduce case
+  std::vector<int32_t> axes_values;
+  if (auto const_axes = dynamic_cast<moco::tf::TFConst *>(axes_node))
+  {
+    // TODO Support S64 type
+    assert(const_axes->dtype() == loco::DataType::S32);
+
+    for (uint32_t i = 0; i < const_axes->size<loco::DataType::S32>(); ++i)
+    {
+      int32_t axis = const_axes->at<loco::DataType::S32>(i);
+      if (axis < 0)
+        axis += node_tensor_shape.rank();
+      axes_values.push_back(axis);
+    }
+  }
+  else if (auto const_axes = dynamic_cast<loco::ConstGen *>(axes_node))
+  {
+    // TODO Support S64 type
+    assert(const_axes->dtype() == loco::DataType::S32);
+
+    for (uint32_t i = 0; i < const_axes->size<loco::DataType::S32>(); ++i)
+    {
+      int32_t axis = const_axes->at<loco::DataType::S32>(i);
+      if (axis < 0)
+        axis += node_tensor_shape.rank();
+      axes_values.push_back(axis);
+    }
+  }
+  else
+    return false;
+
+  // Create loco node to replace
+  auto reduce = graph->nodes()->template create<loco::TensorReduce>();
+
+  // replace
+  reduce->func(reduceFunc<TFNode>());
+  reduce->input(node->input());
+  for (uint32_t i = 0; i < axes_values.size(); ++i)
+    reduce->axes()->insert(axes_values.at(i));
+
+  replace(node).with(reduce);
+
+  INFO(l) << "TFNodeCanonicalize ReduceNode done";
+
+  return true;
+}
+
+} // namespace
+
+#endif // __TF_REDUCE_CANONICALIZE_HELPER_H__