[exo-tflite] TFLFormattedGraph to print TFLNodes (#6836)
author윤현식/On-Device Lab(SR)/Principal Engineer/삼성전자 <hyunsik.yoon@samsung.com>
Thu, 22 Aug 2019 11:11:07 +0000 (20:11 +0900)
committer박종현/On-Device Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Thu, 22 Aug 2019 11:11:07 +0000 (20:11 +0900)
TFLFormattedGraph was added to print the info of TFLNodes.

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

index 61c84a3..5aabf69 100644 (file)
@@ -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 (file)
index 0000000..d2b0c17
--- /dev/null
@@ -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 <locoex/Service/COpFormattedGraph.h>
+
+#include <sstream>
+
+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<const CLASS *>(node))                  \
+  {                                                       \
+    return summary(dynamic_cast<const CLASS *>(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 (file)
index 0000000..5b890f5
--- /dev/null
@@ -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 <locop/FormattedGraph.h>
+
+#include <stdex/Memory.h>
+
+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<locop::NodeSummaryBuilder> create(const locop::SymbolTable *tlb) const final
+  {
+    return stdex::make_unique<NodeSummaryBuilder>(tlb);
+  }
+};
+
+} // namespace exo
+
+#endif // __TFL_FORMATTED_GRAPH_H__