[exo-tflite] Introducing Logging (#6896)
author윤현식/On-Device Lab(SR)/Principal Engineer/삼성전자 <hyunsik.yoon@samsung.com>
Mon, 26 Aug 2019 04:44:27 +0000 (13:44 +0900)
committer박세희/On-Device Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com>
Mon, 26 Aug 2019 04:44:27 +0000 (13:44 +0900)
* [exo-tflite] Introducing Logging

This adds classes related to Logging. These are borrowed from moco except 1) removed logging of annoted padding, 2) change namespace

Signed-off-by: Hyun Sik Yoon <hyunsik.yoon@samsung.com>
* int to uint32_t

compiler/exo-tflite/CMakeLists.txt
compiler/exo-tflite/src/Log.cpp [new file with mode: 0644]
compiler/exo-tflite/src/Log.h [new file with mode: 0644]
compiler/exo-tflite/src/LogHelper.cpp [new file with mode: 0644]
compiler/exo-tflite/src/LogHelper.h [new file with mode: 0644]

index 5aabf69..c4be59b 100644 (file)
@@ -35,6 +35,7 @@ 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)
+target_link_libraries(exo_tflite PRIVATE hermes_std)
 
 # Let's apply nncc common compile options
 #
diff --git a/compiler/exo-tflite/src/Log.cpp b/compiler/exo-tflite/src/Log.cpp
new file mode 100644 (file)
index 0000000..de69b65
--- /dev/null
@@ -0,0 +1,102 @@
+/*
+ * 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 "Log.h"
+
+#include <hermes/ConsoleReporter.h>
+#include <stdex/Memory.h>
+
+#include <cassert>
+#include <cstdlib>
+#include <iostream>
+
+// TODO Extract these lexical conversion routines as a library
+namespace
+{
+
+/**
+ * @brief Convert C-string as a value of type T
+ *
+ * safecast(s, v) returns v if s is nullptr.
+ */
+template <typename T> T safecast(const char *, const T &);
+
+template <> bool safecast<bool>(const char *s, const bool &value)
+{
+  return (s == nullptr) ? value : (std::stoi(s) != 0);
+}
+
+} // namespace
+
+namespace exo
+{
+
+//
+// Logger
+//
+Logger::Logger(hermes::Context *ctx) { activate(ctx->sources(), ctx->bus()); }
+Logger::~Logger() { deactivate(); }
+
+//
+// LoggerConfig
+//
+LoggerConfig::LoggerConfig()
+{
+  // Turn on logging if EXOTFLITE_LOG is set as non-zero value
+  _enabled = safecast<bool>(std::getenv("EXOTFLITE_LOG"), false);
+}
+
+void LoggerConfig::configure(const hermes::Source *source, hermes::Source::Setting &setting) const
+{
+  // Let's ignore hermes::Sources if that is not a exotflite logger
+  if (auto logger = dynamic_cast<const Logger *>(source))
+  {
+    configure(logger, setting);
+  }
+}
+
+void LoggerConfig::configure(const Logger *, hermes::Source::Setting &setting) const
+{
+  if (_enabled)
+  {
+    // Enable all catagories
+    setting.accept_all();
+  }
+  else
+  {
+    // Disable all catagories
+    setting.reject_all();
+  }
+}
+
+//
+// LoggingContext
+//
+hermes::Context *LoggingContext::get(void)
+{
+  static hermes::Context *ctx = nullptr;
+
+  if (ctx == nullptr)
+  {
+    ctx = new hermes::Context;
+    ctx->sinks()->append(stdex::make_unique<hermes::ConsoleReporter>());
+    ctx->config(stdex::make_unique<LoggerConfig>());
+  }
+
+  return ctx;
+}
+
+} // namespace exo
diff --git a/compiler/exo-tflite/src/Log.h b/compiler/exo-tflite/src/Log.h
new file mode 100644 (file)
index 0000000..8b25921
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ * 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 __LOG_H__
+#define __LOG_H__
+
+#include <hermes.h>
+
+namespace exo
+{
+
+/**
+ * @brief Logger Implementation
+ */
+class Logger final : public hermes::Source
+{
+public:
+  Logger(hermes::Context *ctx);
+  ~Logger();
+};
+
+/**
+ * @brief Logger Configuration
+ *
+ * Users are able to turn logging on/off via EXOTFLITE_LOG environment variable.
+ */
+class LoggerConfig final : public hermes::Config
+{
+public:
+  LoggerConfig();
+
+public:
+  void configure(const hermes::Source *, hermes::Source::Setting &) const final;
+  void configure(const Logger *, hermes::Source::Setting &) const;
+
+private:
+  bool _enabled;
+};
+
+/**
+ * @brief Global logging context
+ */
+struct LoggingContext
+{
+  static hermes::Context *get(void);
+};
+
+} // namespace exo
+
+/**
+ * HOW TO USE:
+ *
+ *   LOGGER(l);
+ *
+ *   INFO(l) << "Hello, World" << std::endl;
+ *
+ */
+#define LOGGER(name) ::exo::Logger name{::exo::LoggingContext::get()};
+
+// TODO Support FATAL, ERROR, WARN, and VERBOSE
+#define INFO(name) HERMES_INFO(name)
+
+// WARNING!
+//
+//   THE CURRENT IMPLEMENTATION IS NOT THREAD SAFE.
+//
+
+#endif // __LOG_H__
diff --git a/compiler/exo-tflite/src/LogHelper.cpp b/compiler/exo-tflite/src/LogHelper.cpp
new file mode 100644 (file)
index 0000000..50377b8
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * 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 "LogHelper.h"
+
+namespace loco
+{
+
+std::ostream &operator<<(std::ostream &os, const loco::FeatureShape &feature_shape)
+{
+  os << "[" << feature_shape.count().value() << "," << feature_shape.height().value() << ","
+     << feature_shape.width().value() << "," << feature_shape.depth().value() << "]";
+  return os;
+}
+
+std::ostream &operator<<(std::ostream &os, const loco::FilterShape &filter_shape)
+{
+  os << "[" << filter_shape.height().value() << "," << filter_shape.width().value() << ","
+     << filter_shape.depth().value() << "," << filter_shape.count().value() << "]";
+  return os;
+}
+
+std::ostream &operator<<(std::ostream &os, const loco::TensorShape &tensor_shape)
+{
+  os << "[";
+  for (uint32_t r = 0; r < tensor_shape.rank(); ++r)
+  {
+    if (r)
+      os << ",";
+    os << tensor_shape.dim(r).value();
+  }
+  os << "]";
+  return os;
+}
+
+std::ostream &operator<<(std::ostream &os, const loco::Pad<2> &pad)
+{
+  os << "[TLBR " << pad.top() << "," << pad.left() << "," << pad.bottom() << "," << pad.right()
+     << "]";
+
+  return os;
+}
+
+} // namespace loco
+
+std::ostream &operator<<(std::ostream &os, const std::vector<int64_t> &vi64)
+{
+  for (auto vi : vi64)
+  {
+    os << vi << " ";
+  }
+  return os;
+}
+
+#include "TFLFormattedGraph.h"
+
+namespace exo
+{
+
+FormattedGraph fmt(loco::Graph *g)
+{
+  auto node_summary_builder = stdex::make_unique<NodeSummaryBuilderFactory>();
+  return std::move(locop::fmt<locop::LinearV1>(g).with(std::move(node_summary_builder)));
+}
+
+} // namespace exo
diff --git a/compiler/exo-tflite/src/LogHelper.h b/compiler/exo-tflite/src/LogHelper.h
new file mode 100644 (file)
index 0000000..e7ab6a1
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * 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 __LOG_HELPER_H__
+#define __LOG_HELPER_H__
+
+#include <locop/FormattedGraph.h>
+
+#include <loco/IR/FeatureShape.h>
+#include <loco/IR/FilterShape.h>
+#include <loco/IR/TensorShape.h>
+
+#include <sstream>
+#include <vector>
+
+namespace loco
+{
+
+/**
+ * @brief dump FeatureShape values to stream
+ */
+std::ostream &operator<<(std::ostream &os, const loco::FeatureShape &feature_shape);
+
+/**
+ * @brief dump FilterShape values to stream
+ */
+std::ostream &operator<<(std::ostream &os, const loco::FilterShape &filter_shape);
+
+/**
+ * @brief dump TensorShape values to stream
+ */
+std::ostream &operator<<(std::ostream &os, const loco::TensorShape &tensor_shape);
+
+/**
+ * @brief dump Pad<2> values to stream
+ */
+std::ostream &operator<<(std::ostream &os, const loco::Pad<2> &pad);
+
+} // namespace loco
+
+/**
+ * @brief dump std::vector<int64_t> values to stream
+ */
+std::ostream &operator<<(std::ostream &os, const std::vector<int64_t> &vi64);
+
+namespace exo
+{
+
+using FormattedGraph = locop::FormattedGraphImpl<locop::Formatter::LinearV1>;
+
+FormattedGraph fmt(loco::Graph *g);
+
+static inline FormattedGraph fmt(const std::unique_ptr<loco::Graph> &g) { return fmt(g.get()); }
+
+} // namespace exo
+
+#endif // __LOG_HELPER_H__