From 95a47cc1000a80daffc6ff6356cea39c703661fc Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/On-Device=20Lab=28SR=29/Staff?= =?utf8?q?=20Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Thu, 13 Jun 2019 16:05:53 +0900 Subject: [PATCH] [locop] Initial commit (#3756) This commit creates "locop" which provides various pretty-printing routines for loco IR. Signed-off-by: Jonghyun Park --- contrib/locop/.FORMATCHECKED | 0 contrib/locop/CMakeLists.txt | 26 +++++ contrib/locop/README.md | 3 + contrib/locop/include/locop/FormattedGraph.h | 52 +++++++++ contrib/locop/src/FormattedGraph.cpp | 151 +++++++++++++++++++++++++++ contrib/locop/src/FormattedGraph.test.cpp | 33 ++++++ 6 files changed, 265 insertions(+) create mode 100644 contrib/locop/.FORMATCHECKED create mode 100644 contrib/locop/CMakeLists.txt create mode 100644 contrib/locop/README.md create mode 100644 contrib/locop/include/locop/FormattedGraph.h create mode 100644 contrib/locop/src/FormattedGraph.cpp create mode 100644 contrib/locop/src/FormattedGraph.test.cpp diff --git a/contrib/locop/.FORMATCHECKED b/contrib/locop/.FORMATCHECKED new file mode 100644 index 0000000..e69de29 diff --git a/contrib/locop/CMakeLists.txt b/contrib/locop/CMakeLists.txt new file mode 100644 index 0000000..ff60c88 --- /dev/null +++ b/contrib/locop/CMakeLists.txt @@ -0,0 +1,26 @@ +file(GLOB_RECURSE SOURCES "src/*.cpp") +file(GLOB_RECURSE TESTS "src/*.test.cpp") +list(REMOVE_ITEM SOURCES ${TESTS}) + +add_library(locop STATIC ${SOURCES}) +set_target_properties(locop PROPERTIES POSITION_INDEPENDENT_CODE ON) +target_include_directories(locop PUBLIC include) +target_link_libraries(locop PUBLIC loco) +# Let's apply nncc common compile options +# +# NOTE This will enable strict compilation (warnings as error). +# Please refer to the top-level CMakeLists.txt for details +target_link_libraries(locop PRIVATE nncc_common) +target_link_libraries(locop PUBLIC nncc_coverage) +target_link_libraries(locop PRIVATE pp) + +if(NOT ENABLE_TEST) + return() +endif(NOT ENABLE_TEST) + +# Google Test is mandatory for internal testing +nncc_find_package(GTest REQUIRED) + +GTest_AddTest(locop_test ${TESTS}) +target_link_libraries(locop_test stdex) +target_link_libraries(locop_test locop) diff --git a/contrib/locop/README.md b/contrib/locop/README.md new file mode 100644 index 0000000..81ef41c --- /dev/null +++ b/contrib/locop/README.md @@ -0,0 +1,3 @@ +# locop + +_locop_ is a collection of _loco_ pretty printers. diff --git a/contrib/locop/include/locop/FormattedGraph.h b/contrib/locop/include/locop/FormattedGraph.h new file mode 100644 index 0000000..2a6f7cd --- /dev/null +++ b/contrib/locop/include/locop/FormattedGraph.h @@ -0,0 +1,52 @@ +#ifndef __LOCOP_FORMATTED_GRAPH_H__ +#define __LOCOP_FORMATTED_GRAPH_H__ + +#include + +#include + +namespace locop +{ + +struct FormattedGraph +{ + virtual ~FormattedGraph() = default; + + virtual void dump(std::ostream &os) const = 0; +}; + +std::ostream &operator<<(std::ostream &, const FormattedGraph &); + +enum Formatter +{ + LinearV1, + // TO BE ADDED +}; + +template class FormattedGraphImpl; + +template <> class FormattedGraphImpl final : public FormattedGraph +{ +public: + FormattedGraphImpl(loco::Graph *graph) : _graph{graph} {} + +public: + void dump(std::ostream &os) const final; + +private: + loco::Graph *_graph; +}; + +template FormattedGraphImpl fmt(loco::Graph *g) +{ + return FormattedGraphImpl{g}; +} + +template FormattedGraphImpl fmt(const std::unique_ptr &g) +{ + return fmt(g.get()); +} + +} // namespace locop + +#endif // __LOCOP_FORMATTED_GRAPH_H__ diff --git a/contrib/locop/src/FormattedGraph.cpp b/contrib/locop/src/FormattedGraph.cpp new file mode 100644 index 0000000..84c8478 --- /dev/null +++ b/contrib/locop/src/FormattedGraph.cpp @@ -0,0 +1,151 @@ +#include "locop/FormattedGraph.h" + +#include + +#include +#include + +namespace +{ + +std::string opname(loco::Node *node) +{ + // TODO Use Visitor later + if (dynamic_cast(node)) + { + return "Push"; + } + + if (dynamic_cast(node)) + { + return "Pull"; + } + + if (dynamic_cast(node)) + { + return "MaxPool2D"; + } + + if (dynamic_cast(node)) + { + return "FeatureEncode"; + } + + return "Unknown"; +} + +} // namespace + +namespace locop +{ + +std::ostream &operator<<(std::ostream &os, const FormattedGraph &fmt) +{ + fmt.dump(os); + return os; +} + +} // namespace locop + +namespace locop +{ + +void FormattedGraphImpl::dump(std::ostream &os) const +{ + // TODO Introduce SymolTable + std::map symbols; + + auto symbol = [&symbols](loco::Node *node) -> std::string { + if (node == nullptr) + { + return "(null)"; + } + + return symbols.at(node); + }; + + for (uint32_t n = 0; n < _graph->nodes()->size(); ++n) + { + symbols[_graph->nodes()->at(n)] = pp::fmt("%", n); + } + + // Find the disjoint node clusters + // + // TODO Move this implementation into loco Algorithms.h + std::map parents; + + for (auto node : loco::all_nodes(_graph)) + { + parents[node] = nullptr; + } + + for (auto node : loco::all_nodes(_graph)) + { + for (uint32_t n = 0; n < node->arity(); ++n) + { + if (auto arg = node->arg(n)) + { + parents[arg] = node; + } + } + } + + auto find = [&parents](loco::Node *node) { + loco::Node *cur = node; + + while (parents.at(cur) != nullptr) + { + cur = parents.at(cur); + } + + return cur; + }; + + std::set roots; + + for (auto node : loco::all_nodes(_graph)) + { + roots.insert(find(node)); + } + + std::map> clusters; + + // Create clusters + for (auto root : roots) + { + clusters[root] = std::set{}; + } + + for (auto node : loco::all_nodes(_graph)) + { + clusters.at(find(node)).insert(node); + } + + for (auto it = clusters.begin(); it != clusters.end(); ++it) + { + std::vector cluster_outputs; + + for (auto node : it->second) + { + // NOTE This is inefficient but anyway working :) + if (loco::succs(node).empty()) + { + cluster_outputs.emplace_back(node); + } + } + + // TODO How to print arguments + for (auto node : loco::postorder_traversal(cluster_outputs)) + { + os << symbol(node) << " = " << opname(node) << "("; + for (uint32_t n = 0; n < node->arity(); ++n) + { + os << " " << symbol(node->arg(n)); + } + os << " )" << std::endl; + } + os << std::endl; + } +} + +} // namespace locop diff --git a/contrib/locop/src/FormattedGraph.test.cpp b/contrib/locop/src/FormattedGraph.test.cpp new file mode 100644 index 0000000..8db7f36 --- /dev/null +++ b/contrib/locop/src/FormattedGraph.test.cpp @@ -0,0 +1,33 @@ +/* + * 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 "locop/FormattedGraph.h" + +#include + +TEST(LinearV1FormatterTest, simple) +{ + auto g = loco::make_graph(); + { + auto pull = g->nodes()->create(); + auto push = g->nodes()->create(); + + push->from(pull); + } + + // TODO Validate the output (when the implementation becomes stable) + std::cout << locop::fmt(g) << std::endl; +} -- 2.7.4