[exo-tflite] functions to build node (#7080)
author윤현식/On-Device Lab(SR)/Principal Engineer/삼성전자 <hyunsik.yoon@samsung.com>
Mon, 2 Sep 2019 09:06:39 +0000 (18:06 +0900)
committer박종현/On-Device Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Mon, 2 Sep 2019 09:06:39 +0000 (18:06 +0900)
This adds functions to build featureEncode and featureDecode, which are frequently used.

Signed-off-by: Hyun Sik Yoon <hyunsik.yoon@samsung.com>
compiler/exo-tflite/src/Conversion/GraphBlock.cpp [new file with mode: 0644]
compiler/exo-tflite/src/Conversion/GraphBlock.h [new file with mode: 0644]

diff --git a/compiler/exo-tflite/src/Conversion/GraphBlock.cpp b/compiler/exo-tflite/src/Conversion/GraphBlock.cpp
new file mode 100644 (file)
index 0000000..e38b1d1
--- /dev/null
@@ -0,0 +1,86 @@
+/*
+ * 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 "GraphBlock.h"
+
+#include "Check.h"
+
+#include <loco.h>
+#include <stdex/Memory.h>
+
+namespace
+{
+
+template <exo::DefaultLayout T> loco::Permutation<loco::Domain::Feature> perm();
+
+template <> loco::Permutation<loco::Domain::Feature> perm<exo::DefaultLayout::NHWC>()
+{
+  // Make NHWC permutation for encoder and decoder
+  loco::Permutation<loco::Domain::Feature> NHWC;
+
+  NHWC.axis(loco::FeatureAxis::Count) = 0;
+  NHWC.axis(loco::FeatureAxis::Height) = 1;
+  NHWC.axis(loco::FeatureAxis::Width) = 2;
+  NHWC.axis(loco::FeatureAxis::Depth) = 3;
+
+  return NHWC;
+}
+
+} // namespace
+
+namespace exo
+{
+
+template <DefaultLayout T> loco::FeatureEncode *make_feature_encode(loco::Node *input_for_encode)
+{
+  EXO_ASSERT(input_for_encode != nullptr, "input should not be nullptr");
+  loco::Graph *g = input_for_encode->graph();
+
+  auto encoder = stdex::make_unique<loco::PermutingEncoder<loco::Domain::Feature>>();
+
+  encoder->perm(perm<T>());
+
+  auto enc = g->nodes()->create<loco::FeatureEncode>();
+  enc->input(input_for_encode);
+  enc->encoder(std::move(encoder));
+
+  return enc;
+}
+
+template <DefaultLayout T> loco::FeatureDecode *make_feature_decode(loco::Node *input_for_decode)
+{
+  EXO_ASSERT(input_for_decode != nullptr, "input should not be nullptr");
+  loco::Graph *g = input_for_decode->graph();
+
+  auto decoder = stdex::make_unique<loco::PermutingDecoder<loco::Domain::Feature>>();
+
+  decoder->perm(perm<T>());
+
+  auto dec = g->nodes()->create<loco::FeatureDecode>();
+  dec->input(input_for_decode);
+  dec->decoder(std::move(decoder));
+
+  return dec;
+}
+
+// template instantiation
+template loco::FeatureEncode *
+make_feature_encode<DefaultLayout::NHWC>(loco::Node *input_for_encode);
+
+template loco::FeatureDecode *
+make_feature_decode<DefaultLayout::NHWC>(loco::Node *input_for_encode);
+
+} // namespace exo
diff --git a/compiler/exo-tflite/src/Conversion/GraphBlock.h b/compiler/exo-tflite/src/Conversion/GraphBlock.h
new file mode 100644 (file)
index 0000000..d6dd575
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * 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_GRAPH_BLOCK_H__
+#define __CONVERSION_GRAPH_BLOCK_H__
+
+#include <loco.h>
+
+namespace exo
+{
+
+/// @brief default layout of TFLITE file
+enum class DefaultLayout
+{
+  NHWC,
+};
+
+/// @brief Creates a loco::FeatureEncode of default layout (NHWC for tflite) and add it to graph.
+template <DefaultLayout T> loco::FeatureEncode *make_feature_encode(loco::Node *input_for_encode);
+
+/// @brief Create a loco::FeatureDecode of default layout (NHWC for tflite) and add it to graph.
+template <DefaultLayout T> loco::FeatureDecode *make_feature_decode(loco::Node *input_for_decode);
+
+} // namespace exo
+
+#endif //__CONVERSION_GRAPH_BLOCK_H__