[moco-tf] Introduce RemoveTFIdentityNodeTransform (#6772)
author박세희/On-Device Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com>
Wed, 21 Aug 2019 05:36:52 +0000 (14:36 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Wed, 21 Aug 2019 05:36:52 +0000 (14:36 +0900)
This will introduce RemoveTFIdentityNodeTransform that disconnects TFIdentity for optimization

Signed-off-by: SaeHie Park <saehie.park@samsung.com>
compiler/moco-tf/src/Transforms/RemoveTFIdentityNodeTransform.cpp [new file with mode: 0644]
compiler/moco-tf/src/Transforms/RemoveTFIdentityNodeTransform.h [new file with mode: 0644]

diff --git a/compiler/moco-tf/src/Transforms/RemoveTFIdentityNodeTransform.cpp b/compiler/moco-tf/src/Transforms/RemoveTFIdentityNodeTransform.cpp
new file mode 100644 (file)
index 0000000..50f7ab9
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * 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 "RemoveTFIdentityNodeTransform.h"
+
+#include "Dialect/TFDialect.h"
+#include "Dialect/TFNode.h"
+
+#include <set>
+
+namespace moco
+{
+namespace tf
+{
+
+bool RemoveTFIdentityNodeTransform::run(loco::Graph *g)
+{
+  struct Collector final : public moco::tf::TFNodeMutableVisitor<void>
+  {
+    void visit(moco::tf::TFIdentity *node) final
+    {
+      if (node->input() != nullptr)
+      {
+        candidates.insert(node);
+      }
+    }
+
+    void visit(moco::tf::TFNode *) final { return; }
+
+    std::set<moco::tf::TFIdentity *> candidates;
+  };
+
+  Collector collector;
+
+  for (auto node : loco::all_nodes(g))
+  {
+    if (node->dialect() == moco::tf::TFDialect::get())
+    {
+      auto tf_node = dynamic_cast<moco::tf::TFNode *>(node);
+      tf_node->accept(&collector);
+    }
+  }
+
+  for (auto node : collector.candidates)
+  {
+    replace(node).with(node->input());
+    node->input(nullptr);
+  }
+
+  return collector.candidates.size() > 0;
+}
+
+} // namespace tf
+} // namespace moco
diff --git a/compiler/moco-tf/src/Transforms/RemoveTFIdentityNodeTransform.h b/compiler/moco-tf/src/Transforms/RemoveTFIdentityNodeTransform.h
new file mode 100644 (file)
index 0000000..534061e
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * 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_REMOVE_TFIDENTITY_NODE_TRANSFORM_H__
+#define __MOCO_TF_REMOVE_TFIDENTITY_NODE_TRANSFORM_H__
+
+#include "Transform.h"
+
+namespace moco
+{
+namespace tf
+{
+
+/**
+ * @brief Use the input of "TFIdentity" node instead
+ *
+ * BEFORE:
+ *   [X] -> [TFIdentity] -> [Y]
+ *
+ * AFTER:
+ *   [X]         ->         [Y]
+ *          [TFIdentity]
+ *
+ * NOTE This transform does not remove "TFIdentity" node
+ *      This transform is identical to RemoveForwardNodeTransform
+ */
+struct RemoveTFIdentityNodeTransform final : public Transform
+{
+  const char *name(void) const final { return "RemoveTFIdentityNodeTransform"; }
+
+  bool run(loco::Graph *g) final;
+};
+
+} // namespace tf
+} // namespace moco
+
+#endif // __MOCO_TF_REMOVE_TFIDENTITY_NODE_TRANSFORM_H__