[enco] Initial AvgPoolRewritePass implementation (#1374)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Thu, 6 Sep 2018 02:58:17 +0000 (11:58 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Thu, 6 Sep 2018 02:58:17 +0000 (11:58 +0900)
This commit introduces the initial implementation of AvgPoolRewritePass
which support only simple case (Static divisor with empty padding).

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/enco/core/src/Backend.cpp
contrib/enco/core/src/Transforms/Rewrite.cpp [new file with mode: 0644]
contrib/enco/core/src/Transforms/Rewrite.h [new file with mode: 0644]

index d0e3142..40e14b0 100644 (file)
@@ -4,6 +4,7 @@
 #include "CppCode.h"
 
 #include "Transforms/Duplicate.h"
+#include "Transforms/Rewrite.h"
 #include "Transforms/Normalize.h"
 #include "Transforms/Split.h"
 
@@ -53,6 +54,9 @@ void Backend::compile(coco::Module *m, coco::Data *d)
   // that share the same bag as their underlying bag
   assert(!has_inout_bag(code.module()));
 
+  AvgPoolRewritePass avgpool_rewrite;
+  avgpool_rewrite.runOnCode(&code);
+
   // Insert data ordering if necessary
   NormalizePass normalize;
   normalize.runOnCode(&code);
diff --git a/contrib/enco/core/src/Transforms/Rewrite.cpp b/contrib/enco/core/src/Transforms/Rewrite.cpp
new file mode 100644 (file)
index 0000000..ec01ad1
--- /dev/null
@@ -0,0 +1,45 @@
+#include "Rewrite.h"
+
+#include <cassert>
+
+namespace
+{
+
+bool empty(coco::Padding2D *pad)
+{
+  return (pad->top() == 0) && (pad->bottom() == 0) && (pad->left() == 0) && (pad->right() == 0);
+}
+
+} // namespace
+
+namespace enco
+{
+
+void AvgPoolRewritePass::runOnModule(coco::Module *m) const
+{
+  for (auto B = m->block()->head(); B; B = B->next())
+  {
+    for (auto I = B->instr()->head(); I; I = I->next())
+    {
+      if (auto unit = I->asUnitF())
+      {
+        assert(unit->op() != nullptr);
+        if (auto avgpool = unit->op()->asAvgPool2D())
+        {
+          if (avgpool->divisor() == coco::AvgPool2D::Divisor::Static)
+          {
+            if (empty(avgpool->pad()))
+            {
+              // NOTE If there is no padding, Static and PaddingExcluded schemes are equivalent
+              avgpool->divisor(coco::AvgPool2D::Divisor::PaddingExcluded);
+            }
+          }
+        }
+      }
+    }
+  }
+}
+
+void AvgPoolRewritePass::runOnCode(enco::Code *code) const { runOnModule(code->module()); }
+
+} // namespace enco
diff --git a/contrib/enco/core/src/Transforms/Rewrite.h b/contrib/enco/core/src/Transforms/Rewrite.h
new file mode 100644 (file)
index 0000000..3e0b37c
--- /dev/null
@@ -0,0 +1,23 @@
+#ifndef __REWRITE_H__
+#define __REWRITE_H__
+
+#include "Code.h"
+
+namespace enco
+{
+
+/**
+ * @brief Rewrite NN API-incompatible average pooling
+ */
+class AvgPoolRewritePass
+{
+private:
+  void runOnModule(coco::Module *m) const;
+
+public:
+  void runOnCode(enco::Code *) const;
+};
+
+} // namespace enco
+
+#endif // __REWRITE_H__