From 66c8b388e45ed1981b464383dd7ced70456c0ec0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EC=9C=A4=ED=98=84=EC=8B=9D/On-Device=20Lab=28SR=29/Princip?= =?utf8?q?al=20Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Wed, 28 Aug 2019 12:37:08 +0900 Subject: [PATCH] [exo-tflite] Function to convert canonical nodes to TFL nodes (#6939) * [exo-tflite] Function to convert canonical nodes to TFL nodes This adds a skeleton level implementation of a function that will convert canonical nodes to TFL nodes Signed-off-by: Hyun Sik Yoon * copyright. fix log for knob checking --- compiler/exo-tflite/CMakeLists.txt | 1 + compiler/exo-tflite/requires.cmake | 1 + compiler/exo-tflite/src/Conversion/Convert.cpp | 53 ++++++++++++++++++++++++++ compiler/exo-tflite/src/Conversion/Convert.h | 28 ++++++++++++++ compiler/exo-tflite/src/TFLExporterImpl.cpp | 14 +++++++ 5 files changed, 97 insertions(+) create mode 100644 compiler/exo-tflite/src/Conversion/Convert.cpp create mode 100644 compiler/exo-tflite/src/Conversion/Convert.h diff --git a/compiler/exo-tflite/CMakeLists.txt b/compiler/exo-tflite/CMakeLists.txt index c4be59b..40acd21 100644 --- a/compiler/exo-tflite/CMakeLists.txt +++ b/compiler/exo-tflite/CMakeLists.txt @@ -36,6 +36,7 @@ target_link_libraries(exo_tflite PRIVATE pepper_strcast) target_link_libraries(exo_tflite PRIVATE locoex_customop) target_link_libraries(exo_tflite PRIVATE locop) target_link_libraries(exo_tflite PRIVATE hermes_std) +target_link_libraries(exo_tflite PRIVATE logo) # Let's apply nncc common compile options # diff --git a/compiler/exo-tflite/requires.cmake b/compiler/exo-tflite/requires.cmake index 9815422..554bbe3 100644 --- a/compiler/exo-tflite/requires.cmake +++ b/compiler/exo-tflite/requires.cmake @@ -1,3 +1,4 @@ require("stdex") require("loco") require("locoex-customop") +require("logo") diff --git a/compiler/exo-tflite/src/Conversion/Convert.cpp b/compiler/exo-tflite/src/Conversion/Convert.cpp new file mode 100644 index 0000000..319462c --- /dev/null +++ b/compiler/exo-tflite/src/Conversion/Convert.cpp @@ -0,0 +1,53 @@ +/* + * 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 "Convert.h" + +#include +#include +#include +#include + +#include +#include + +namespace exo +{ + +void convert_to_TFLNodes(loco::Graph *graph) +{ + // run Shape and Type inference must be run before conversion + loco::CanonicalShapeInferenceRule shape_rule; + loco::apply(&shape_rule).to(graph); + + loco::CanonicalTypeInferenceRule type_rule; + loco::apply(&type_rule).to(graph); + + logo::Phase phase; + { + // TODO add each converter that converts canonical to TFLDialect + // For example, something like the following: + // phase.emplace_back(stdex::make_unique()); + // phase.emplace_back(stdex::make_unique()); + } + + logo::PhaseRunner phase_runner{graph}; + phase_runner.run(phase); + + // TODO Assert if all canonical nodes are converted to TFL node +} + +} // namespace exo diff --git a/compiler/exo-tflite/src/Conversion/Convert.h b/compiler/exo-tflite/src/Conversion/Convert.h new file mode 100644 index 0000000..c274d61 --- /dev/null +++ b/compiler/exo-tflite/src/Conversion/Convert.h @@ -0,0 +1,28 @@ +/* + * 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_CONVERT_H__ +#define __CONVERSION_CONVERT_H__ + +#include + +namespace exo +{ + +void convert_to_TFLNodes(loco::Graph *graph); +} + +#endif //__CONVERSION_CONVERT_H__ diff --git a/compiler/exo-tflite/src/TFLExporterImpl.cpp b/compiler/exo-tflite/src/TFLExporterImpl.cpp index f15f897..cf5ec81 100644 --- a/compiler/exo-tflite/src/TFLExporterImpl.cpp +++ b/compiler/exo-tflite/src/TFLExporterImpl.cpp @@ -16,12 +16,17 @@ #include "TFLExporterImpl.h" +#include "Conversion/Convert.h" + #include "TypeInference.h" #include "ShapeInference.h" #include "TensorExporter.h" #include "OperationExporter.h" #include "ExporterUtils.h" +#include "Log.h" +#include "Knob.h" + #include #include #include @@ -99,6 +104,15 @@ flatbuffers::Offset TFLExporter::Impl::exportSubgraph(Serializ void TFLExporter::Impl::exportGraph(loco::Graph *graph) { + LOGGER(l); + + INFO(l) << "Knob::EnableTFLDialect is " << (get_knob() ? "set" : "unset"); + + if (get_knob()) // guard for backward compatibility + { + convert_to_TFLNodes(graph); + } + // Infer the type of each node TypeInference::run(graph); // TypeInference::get(node) now works -- 2.7.4