[exo-tflite] adding FeatureBiasAdd converter (#7467)
author윤현식/On-Device Lab(SR)/Principal Engineer/삼성전자 <hyunsik.yoon@samsung.com>
Tue, 17 Sep 2019 01:37:44 +0000 (10:37 +0900)
committer박종현/On-Device Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Tue, 17 Sep 2019 01:37:44 +0000 (10:37 +0900)
* [exo-tflite] adding FeatureBiasAdd converter

This adds FeatureBiasAdd converter, which converts loco::FeatureBiasEEdd to locoex::TFLAdd.

Signed-off-by: Hyun Sik Yoon <hyunsik.yoon@samsung.com>
* typo

compiler/exo-tflite/src/Conversion/CanonicalNodeConverter.cpp
compiler/exo-tflite/src/Conversion/FeatureBiasAddConverter.cpp [new file with mode: 0644]
compiler/exo-tflite/src/Conversion/FeatureBiasAddConverter.h [new file with mode: 0644]

index 86dd4a0..ac98c6a 100644 (file)
@@ -55,7 +55,7 @@ template bool CanonicalNodeConverter<loco::EltwiseAdd>::run(loco::Graph *graph);
 template bool CanonicalNodeConverter<loco::EltwiseMul>::run(loco::Graph *graph);
 // TODO loco::EltwiseSqrt
 // TODO loco::EltwiseSub
-// TODO loco::FeatureBiasAdd
+template bool CanonicalNodeConverter<loco::FeatureBiasAdd>::run(loco::Graph *graph);
 // TODO loco::FixedReshape
 template bool CanonicalNodeConverter<loco::MaxPool2D>::run(loco::Graph *graph);
 template bool CanonicalNodeConverter<loco::ReLU>::run(loco::Graph *graph);
diff --git a/compiler/exo-tflite/src/Conversion/FeatureBiasAddConverter.cpp b/compiler/exo-tflite/src/Conversion/FeatureBiasAddConverter.cpp
new file mode 100644 (file)
index 0000000..1163f0e
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * 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 "FeatureBiasAddConverter.h"
+
+#include "Dialect/IR/TFLNodes.h"
+
+#include "GraphBlock.h"
+
+#include <loco.h>
+#include <loco/Service/ShapeInference.h>
+
+#include <cassert>
+
+namespace exo
+{
+
+/**
+ * @brief Converts loco::FeatureBiasAdd to locoex::TFLAdd
+ *
+ * Before:
+ *                  Foo ---+
+ *                         |
+ *                        loco::FeatureBiasAdd - FeatureDecode - ...
+ *                         |
+ *      Bar - BiasEncode --+
+ *
+ * After:
+ *
+ *              Foo - loco::FeatureDecode --+           loco::FeatureBiasAdd
+ *                                          |(x)
+ *                                          TFLAdd -- loco::FeatureEncode - FeatureDecode - ...
+ *                                          |(y)
+ *    Bar - BiasEncode - loco::BiasDecode --+
+ */
+bool FeatureBiasAddConverter::convert(loco::FeatureBiasAdd *origin)
+{
+  auto *graph = origin->graph();
+
+  auto tfl_add = graph->nodes()->create<locoex::TFLAdd>();
+
+  // handling input x
+  assert(loco::shape_get(origin->value()).domain() == loco::Domain::Feature);
+
+  auto fea_dec = make_feature_decode<DefaultLayout::NHWC>(origin->value());
+  tfl_add->x(fea_dec);
+
+  // handling input y
+  auto bias_dec = graph->nodes()->create<loco::BiasDecode>();
+  assert(bias_dec != nullptr);
+
+  bias_dec->input(origin->bias());
+
+  tfl_add->y(bias_dec);
+
+  // handling output
+  auto fea_enc = make_feature_encode<DefaultLayout::NHWC>(tfl_add);
+
+  loco::replace(origin).with(fea_enc);
+  origin->value(nullptr);
+
+  return true;
+}
+
+} // namespace exo
diff --git a/compiler/exo-tflite/src/Conversion/FeatureBiasAddConverter.h b/compiler/exo-tflite/src/Conversion/FeatureBiasAddConverter.h
new file mode 100644 (file)
index 0000000..5c4f102
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * 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_FEATUREBIASADD_CONVERTER__
+#define __CONVERSION_FEATUREBIASADD_CONVERTER__
+
+#include "CanonicalNodeConverter.h"
+
+#include <loco.h>
+
+namespace exo
+{
+
+class FeatureBiasAddConverter : public CanonicalNodeConverter<loco::FeatureBiasAdd>
+{
+public:
+  const char *name(void) const final { return "exo::TFLAddConverter"; }
+
+public:
+  bool convert(loco::FeatureBiasAdd *origin) final;
+};
+
+} // namespace exo
+
+#endif // __CONVERSION_FEATUREBIASADD_CONVERTER__