[moco/tf] Introduce Domain Conversion Simplification (#4037)
author박종현/On-Device Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Tue, 2 Jul 2019 04:05:52 +0000 (13:05 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Tue, 2 Jul 2019 04:05:52 +0000 (13:05 +0900)
This commit introduces Domain Conversion Simplification optimizations
which convertes FeatureDecode followed by FeatureEncode as Forward.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/moco-tf/src/Knob.lst
contrib/moco-tf/src/Optimizer.cpp
contrib/moco-tf/src/Transforms.h
contrib/moco-tf/src/Transforms/SimplifyDomainConversionTransform.cpp [new file with mode: 0644]
contrib/moco-tf/src/Transforms/SimplifyDomainConversionTransform.h [new file with mode: 0644]

index f22001a..fe4f283 100644 (file)
@@ -5,3 +5,4 @@
 // KNOB_BOOL(NAME, DEFAULT_VALUE, DESCRIPTION)
 KNOB_BOOL(RemoveDeadNode, false, Enable RemoveDeadNode optimization)
 KNOB_BOOL(RemoveForwardNode, false, Enable RemoveForwardNode optimization)
+KNOB_BOOL(SimplifyDomainConversion, false, Enable SimplifyDomainConversion optimization)
index b547c85..ae8f8b1 100644 (file)
@@ -37,6 +37,11 @@ void Optimizer::optimize(loco::Graph *g) const
     phase.emplace_back(stdex::make_unique<RemoveDeadNodeTransform>());
   }
 
+  if (moco::tf::get<moco::tf::Knob::SimplifyDomainConversion>())
+  {
+    phase.emplace_back(stdex::make_unique<SimplifyDomainConversionTransform>());
+  }
+
   if (moco::tf::get<moco::tf::Knob::RemoveForwardNode>())
   {
     phase.emplace_back(stdex::make_unique<RemoveForwardNodeTransform>());
index ba6ba6e..bbb2515 100644 (file)
@@ -22,5 +22,6 @@
 #include "Transforms/FixShapeTransform.h"
 #include "Transforms/RemoveDeadNodeTransform.h"
 #include "Transforms/RemoveForwardNodeTransform.h"
+#include "Transforms/SimplifyDomainConversionTransform.h"
 
 #endif // __MOCO_TF_TRANSFORMS_H__
diff --git a/contrib/moco-tf/src/Transforms/SimplifyDomainConversionTransform.cpp b/contrib/moco-tf/src/Transforms/SimplifyDomainConversionTransform.cpp
new file mode 100644 (file)
index 0000000..7de1897
--- /dev/null
@@ -0,0 +1,112 @@
+/*
+ * 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 "SimplifyDomainConversionTransform.h"
+
+#include <loco/IR/Algorithm.h>
+#include <loco/IR/CanonicalDialect.h>
+#include <loco/IR/CanonicalNode.h>
+
+#include <set>
+#include <cassert>
+
+namespace moco
+{
+namespace tf
+{
+
+bool SimplifyDomainConversionTransform::run(loco::Graph *g)
+{
+  // Let's find FeatureDecode followed by FeatureEncode
+  //
+  // TODO Introduce and Use "Pattern Match"
+  struct Collector final : public loco::CanonicalNodeMutableVisitor<void>
+  {
+    void visit(loco::FeatureEncode *encode_node) final
+    {
+      using namespace loco;
+
+      auto encoder = encode_node->encoder();
+      assert(encoder != nullptr);
+
+      auto decode_node = dynamic_cast<loco::FeatureDecode *>(encode_node->input());
+      if (decode_node == nullptr)
+      {
+        return;
+      }
+      assert(decode_node->input() != nullptr);
+
+      auto decoder = decode_node->decoder();
+      assert(decoder != nullptr);
+
+      // NOTE Work only for permuting codec
+      auto perm_decoder = dynamic_cast<const PermutingDecoder<Domain::Feature> *>(decoder);
+      auto perm_encoder = dynamic_cast<const PermutingEncoder<Domain::Feature> *>(encoder);
+
+      if (perm_encoder == nullptr || perm_decoder == nullptr)
+      {
+        return;
+      }
+
+      // TODO Move this helper into loco
+      auto equal = [](const Permutation<Domain::Feature> *lhs,
+                      const Permutation<Domain::Feature> *rhs) {
+        for (const auto &axis :
+             {FeatureAxis::Count, FeatureAxis::Depth, FeatureAxis::Height, FeatureAxis::Width})
+        {
+          if (lhs->axis(axis) != rhs->axis(axis))
+          {
+            return false;
+          }
+        }
+        return true;
+      };
+
+      if (equal(perm_encoder->perm(), perm_decoder->perm()))
+      {
+        candidates.insert({encode_node, decode_node->input()});
+      }
+    }
+
+    void visit(loco::Node *) final { return; }
+
+    std::set<std::pair<loco::FeatureEncode *, loco::Node *>> candidates;
+  };
+
+  Collector collector;
+
+  for (auto node : loco::all_nodes(g))
+  {
+    if (node->dialect() == loco::CanonicalDialect::get())
+    {
+      auto canonical_node = dynamic_cast<loco::CanonicalNode *>(node);
+      canonical_node->accept(&collector);
+    }
+  }
+
+  for (auto p : collector.candidates)
+  {
+    auto forward_node = g->nodes()->create<loco::Forward>();
+    forward_node->input(p.second);
+    replace(p.first).with(forward_node);
+    p.first->input(nullptr);
+  }
+
+  return collector.candidates.size() > 0;
+}
+
+} // namespace tf
+} // namespace moco
diff --git a/contrib/moco-tf/src/Transforms/SimplifyDomainConversionTransform.h b/contrib/moco-tf/src/Transforms/SimplifyDomainConversionTransform.h
new file mode 100644 (file)
index 0000000..ecca28c
--- /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_SIMPLIFY_DOMAIN_CONVERSION_H__
+#define __MOCO_TF_SIMPLIFY_DOMAIN_CONVERSION_H__
+
+#include "Transform.h"
+
+namespace moco
+{
+namespace tf
+{
+
+/**
+ * @brief Simplify redundant domain conversion
+ *
+ * SimplifyDomainConversionTransform recognizes the following patterns:
+ * - FeatureDecode followed by FeatureEncode
+ * - (TO BE ADDED)
+ */
+struct SimplifyDomainConversionTransform final : public Transform
+{
+  const char *name(void) const final { return "SimplifyDomainConversion"; }
+
+  bool run(loco::Graph *g) final;
+};
+
+} // namespace tf
+} // namespace moco
+
+#endif // __MOCO_TF_SIMPLIFY_DOMAIN_CONVERSION_H__