From c3bdd7807a298797f7dc1ed3e97a3fb2009f614c 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: Thu, 22 Aug 2019 20:11:07 +0900 Subject: [PATCH] [exo-tflite] TFLFormattedGraph to print TFLNodes (#6836) TFLFormattedGraph was added to print the info of TFLNodes. Signed-off-by: Hyun Sik Yoon --- compiler/exo-tflite/CMakeLists.txt | 1 + compiler/exo-tflite/src/TFLFormattedGraph.cpp | 118 ++++++++++++++++++++++++++ compiler/exo-tflite/src/TFLFormattedGraph.h | 56 ++++++++++++ 3 files changed, 175 insertions(+) create mode 100644 compiler/exo-tflite/src/TFLFormattedGraph.cpp create mode 100644 compiler/exo-tflite/src/TFLFormattedGraph.h diff --git a/compiler/exo-tflite/CMakeLists.txt b/compiler/exo-tflite/CMakeLists.txt index 61c84a3..5aabf69 100644 --- a/compiler/exo-tflite/CMakeLists.txt +++ b/compiler/exo-tflite/CMakeLists.txt @@ -34,6 +34,7 @@ target_link_libraries(exo_tflite PUBLIC loco) target_link_libraries(exo_tflite PRIVATE stdex) target_link_libraries(exo_tflite PRIVATE pepper_strcast) target_link_libraries(exo_tflite PRIVATE locoex_customop) +target_link_libraries(exo_tflite PRIVATE locop) # Let's apply nncc common compile options # diff --git a/compiler/exo-tflite/src/TFLFormattedGraph.cpp b/compiler/exo-tflite/src/TFLFormattedGraph.cpp new file mode 100644 index 0000000..d2b0c17 --- /dev/null +++ b/compiler/exo-tflite/src/TFLFormattedGraph.cpp @@ -0,0 +1,118 @@ +/* + * 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 "TFLFormattedGraph.h" + +#include "Dialect/IR/TFLDialect.h" +#include "Dialect/IR/TFLNodes.h" + +#include + +#include + +namespace +{ + +// TFLNodeSummaryBuilder with default implementation +class TFLNodeSummaryBuilderBase : public locop::NodeSummaryBuilder +{ +public: + TFLNodeSummaryBuilderBase(const locop::SymbolTable *tbl) : _tbl{tbl} + { + // DO NOTHING + } + +public: + bool build(const loco::Node *, locop::NodeSummary &s) const final; + +protected: +#define TFL_NODE(OPCODE, CLASS) \ + virtual bool summary(const CLASS *node, locop::NodeSummary &s) const \ + { \ + s.opname("NYI " #CLASS); \ + s.state(locop::NodeSummary::State::PartiallyKnown); \ + return true; \ + } +#include "Dialect/IR/TFLNodes.lst" +#undef TFL_NODE + +protected: + const locop::SymbolTable *tbl(void) const { return _tbl; } + + // Please do not use _tbl directly and use tbl(). + // This will be changed to private in near future. +protected: + const locop::SymbolTable *_tbl; +}; + +class TFLNodeSummaryBuilder final : public TFLNodeSummaryBuilderBase +{ +public: + TFLNodeSummaryBuilder(const locop::SymbolTable *tbl) : TFLNodeSummaryBuilderBase(tbl) + { + // DO NOTHING + } + +private: +#define TFL_NODE(OPCODE, CLASS) bool summary(const CLASS *, locop::NodeSummary &) const final; +#include "Dialect/IR/TFLNodes.lst" +#undef TFL_NODE +}; + +bool TFLNodeSummaryBuilderBase::build(const loco::Node *node, locop::NodeSummary &s) const +{ + (void)s; // TODO remove this when TFL_NODE(..) is defined in TFLNodes.lst + + if (node->dialect() != locoex::TFLDialect::get()) + return false; + +#define TFL_NODE(OPCODE, CLASS) \ + if (dynamic_cast(node)) \ + { \ + return summary(dynamic_cast(node), s); \ + } +#include "Dialect/IR/TFLNodes.lst" +#undef TFL_NODE + + return false; +} + +} // namespace + +namespace exo +{ + +bool NodeSummaryBuilder::build(const loco::Node *node, locop::NodeSummary &s) const +{ + if (locop::CanonicalNodeSummaryBuilder(_tbl).build(node, s)) + { + return true; + } + + if (TFLNodeSummaryBuilder(_tbl).build(node, s)) + { + return true; + } + + if (locoex::COpNodeSummaryBuilder(_tbl).build(node, s)) + { + return true; + } + + return false; +} + +} // namespace exo diff --git a/compiler/exo-tflite/src/TFLFormattedGraph.h b/compiler/exo-tflite/src/TFLFormattedGraph.h new file mode 100644 index 0000000..5b890f5 --- /dev/null +++ b/compiler/exo-tflite/src/TFLFormattedGraph.h @@ -0,0 +1,56 @@ +/* + * 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 __TFL_FORMATTED_GRAPH_H__ +#define __TFL_FORMATTED_GRAPH_H__ + +#include + +#include + +namespace exo +{ + +class NodeSummaryBuilder final : public locop::NodeSummaryBuilder +{ +public: + NodeSummaryBuilder(const locop::SymbolTable *tbl) : _tbl{tbl} + { + // DO NOTHING + } + +public: + bool build(const loco::Node *node, locop::NodeSummary &s) const final; + +private: + const locop::SymbolTable *_tbl; +}; + +class NodeSummaryBuilderFactory final : public locop::NodeSummaryBuilderFactory +{ +public: + NodeSummaryBuilderFactory() = default; + +public: + std::unique_ptr create(const locop::SymbolTable *tlb) const final + { + return stdex::make_unique(tlb); + } +}; + +} // namespace exo + +#endif // __TFL_FORMATTED_GRAPH_H__ -- 2.7.4