[exo-tflite] Convert EltwiseMul (#7459)
author박세희/On-Device Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com>
Mon, 16 Sep 2019 08:23:13 +0000 (17:23 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Mon, 16 Sep 2019 08:23:13 +0000 (17:23 +0900)
This will introduce EltwiseMulConverter that converts EltwiseMul to TFLMul

Signed-off-by: SaeHie Park <saehie.park@samsung.com>
compiler/exo-tflite/src/Conversion/CanonicalNodeConverter.cpp
compiler/exo-tflite/src/Conversion/EltwiseMulConverter.cpp [new file with mode: 0644]
compiler/exo-tflite/src/Conversion/EltwiseMulConverter.h [new file with mode: 0644]

index 0586d61..86dd4a0 100644 (file)
@@ -52,7 +52,7 @@ template bool CanonicalNodeConverter<loco::AvgPool2D>::run(loco::Graph *graph);
 // TODO loco::DepthwiseFilterEncode
 template bool CanonicalNodeConverter<loco::EltwiseAdd>::run(loco::Graph *graph);
 // TODO loco::EltwiseDiv
-// TODO loco::EltwiseMul
+template bool CanonicalNodeConverter<loco::EltwiseMul>::run(loco::Graph *graph);
 // TODO loco::EltwiseSqrt
 // TODO loco::EltwiseSub
 // TODO loco::FeatureBiasAdd
diff --git a/compiler/exo-tflite/src/Conversion/EltwiseMulConverter.cpp b/compiler/exo-tflite/src/Conversion/EltwiseMulConverter.cpp
new file mode 100644 (file)
index 0000000..5b48838
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * 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 "EltwiseMulConverter.h"
+
+#include "GraphBlock.h"
+#include "Check.h"
+
+#include "Dialect/IR/TFLNodes.h"
+
+#include <loco/Service/ShapeInference.h>
+
+namespace exo
+{
+
+bool EltwiseMulConverter::convert(loco::EltwiseMul *origin)
+{
+  if (!loco::shape_known(origin))
+  {
+    return false;
+  }
+
+  if (loco::shape_get(origin).domain() == loco::Domain::Tensor)
+  {
+    auto tfl_mul = origin->graph()->nodes()->create<locoex::TFLMul>();
+    tfl_mul->x(origin->lhs());
+    tfl_mul->y(origin->rhs());
+
+    loco::replace(origin).with(tfl_mul);
+    origin->lhs(nullptr);
+    origin->rhs(nullptr);
+
+    return true;
+  }
+  else if (loco::shape_get(origin).domain() == loco::Domain::Feature)
+  {
+    /*
+    if EltwiseMul's domain is Feature, EltwiseMul is replaced with
+    FeatureDecoder-TFLMul-FeatureEncoder.
+
+    Before :
+       A (output: feature) -- loco::EltwiseMul --- B (input:feature)
+
+    After  :
+       A -- loco::FeatureDecode -- locoex::TFLMul -- loco::FeatureEncode --- B
+
+                      loco::EltwiseMul (dead node)
+    */
+    auto graph = origin->graph();
+    auto dec_l = make_feature_decode<DefaultLayout::NHWC>(origin->lhs());
+    auto dec_r = make_feature_decode<DefaultLayout::NHWC>(origin->rhs());
+    auto tfl_mul = graph->nodes()->create<locoex::TFLMul>();
+    {
+      tfl_mul->x(dec_l);
+      tfl_mul->y(dec_r);
+    }
+    auto enc = make_feature_encode<DefaultLayout::NHWC>(tfl_mul);
+
+    loco::replace(origin).with(enc);
+    origin->lhs(nullptr);
+    origin->rhs(nullptr);
+
+    return true;
+  }
+  else
+    EXO_THROW("Not yet supported loco::Domain");
+}
+
+} // namespace exo
diff --git a/compiler/exo-tflite/src/Conversion/EltwiseMulConverter.h b/compiler/exo-tflite/src/Conversion/EltwiseMulConverter.h
new file mode 100644 (file)
index 0000000..4f73484
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * 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 __CONVERSION_ELTWISEMUL_CONVERTER_H__
+#define __CONVERSION_ELTWISEMUL_CONVERTER_H__
+
+#include "CanonicalNodeConverter.h"
+
+#include <loco.h>
+
+namespace exo
+{
+
+/**
+ * @brief  Convert loco::EltwiseMul to TFLMul
+ */
+class EltwiseMulConverter : public CanonicalNodeConverter<loco::EltwiseMul>
+{
+public:
+  const char *name(void) const final { return "exo::EltwiseMulConverter"; }
+
+public:
+  bool convert(loco::EltwiseMul *origin) final;
+};
+
+} // namespace exo
+
+#endif // __CONVERSION_ELTWISEMUL_CONVERTER_H__