From a71eaad6bb4e80a75819e07ded157fadb5425812 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: Mon, 26 Aug 2019 13:44:27 +0900 Subject: [PATCH] [exo-tflite] Introducing Logging (#6896) * [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 * int to uint32_t --- compiler/exo-tflite/CMakeLists.txt | 1 + compiler/exo-tflite/src/Log.cpp | 102 ++++++++++++++++++++++++++++++++++ compiler/exo-tflite/src/Log.h | 81 +++++++++++++++++++++++++++ compiler/exo-tflite/src/LogHelper.cpp | 79 ++++++++++++++++++++++++++ compiler/exo-tflite/src/LogHelper.h | 70 +++++++++++++++++++++++ 5 files changed, 333 insertions(+) create mode 100644 compiler/exo-tflite/src/Log.cpp create mode 100644 compiler/exo-tflite/src/Log.h create mode 100644 compiler/exo-tflite/src/LogHelper.cpp create mode 100644 compiler/exo-tflite/src/LogHelper.h diff --git a/compiler/exo-tflite/CMakeLists.txt b/compiler/exo-tflite/CMakeLists.txt index 5aabf69..c4be59b 100644 --- a/compiler/exo-tflite/CMakeLists.txt +++ b/compiler/exo-tflite/CMakeLists.txt @@ -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 index 0000000..de69b65 --- /dev/null +++ b/compiler/exo-tflite/src/Log.cpp @@ -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 +#include + +#include +#include +#include + +// 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 T safecast(const char *, const T &); + +template <> bool safecast(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(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(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()); + ctx->config(stdex::make_unique()); + } + + return ctx; +} + +} // namespace exo diff --git a/compiler/exo-tflite/src/Log.h b/compiler/exo-tflite/src/Log.h new file mode 100644 index 0000000..8b25921 --- /dev/null +++ b/compiler/exo-tflite/src/Log.h @@ -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 + +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 index 0000000..50377b8 --- /dev/null +++ b/compiler/exo-tflite/src/LogHelper.cpp @@ -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 &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(); + return std::move(locop::fmt(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 index 0000000..e7ab6a1 --- /dev/null +++ b/compiler/exo-tflite/src/LogHelper.h @@ -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 + +#include +#include +#include + +#include +#include + +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 values to stream + */ +std::ostream &operator<<(std::ostream &os, const std::vector &vi64); + +namespace exo +{ + +using FormattedGraph = locop::FormattedGraphImpl; + +FormattedGraph fmt(loco::Graph *g); + +static inline FormattedGraph fmt(const std::unique_ptr &g) { return fmt(g.get()); } + +} // namespace exo + +#endif // __LOG_HELPER_H__ -- 2.7.4