From cb44cff27739995af9c1974d2dbb3ff69046e225 Mon Sep 17 00:00:00 2001 From: superjomn Date: Thu, 9 May 2019 19:47:02 +0800 Subject: [PATCH] remove glog and gtest dependency for light framework --- paddle/fluid/inference/analysis/dot.h | 2 +- paddle/fluid/lite/api/CMakeLists.txt | 2 + paddle/fluid/lite/api/cxx_api.cc | 4 + paddle/fluid/lite/api/cxx_api.h | 3 + paddle/fluid/lite/api/cxx_api_test.cc | 2 + paddle/fluid/lite/core/CMakeLists.txt | 1 + paddle/fluid/lite/core/context.h | 44 +++------- paddle/fluid/lite/core/memory.h | 1 - paddle/fluid/lite/core/mir/CMakeLists.txt | 2 +- paddle/fluid/lite/core/mir/node.h | 2 +- .../core/mir/runtime_context_assign_pass.cc | 2 +- .../core/mir/variable_place_inference_pass.cc | 1 - paddle/fluid/lite/core/op_lite.cc | 5 +- paddle/fluid/lite/core/op_lite.h | 11 ++- paddle/fluid/lite/core/target_wrapper.h | 2 +- paddle/fluid/lite/core/type_system.h | 1 - paddle/fluid/lite/cuda/blas.h | 2 - paddle/fluid/lite/cuda/cuda_utils.h | 2 +- paddle/fluid/lite/cuda/target_wrapper.cc | 1 - paddle/fluid/lite/kernels/host/fc_compute.h | 1 - paddle/fluid/lite/model_parser/pb/var_desc.h | 2 +- paddle/fluid/lite/model_parser/runtime.cc | 3 - paddle/fluid/lite/model_parser/runtime.h | 1 - paddle/fluid/lite/utils/CMakeLists.txt | 13 ++- paddle/fluid/lite/utils/all.h | 1 + paddle/fluid/lite/utils/any.cc | 23 +++++ paddle/fluid/lite/utils/any.h | 66 ++++++++++++++ paddle/fluid/lite/utils/factory.h | 2 +- paddle/fluid/lite/utils/io.h | 4 +- paddle/fluid/lite/utils/logging.h | 88 +++++++++++++++++++ paddle/fluid/lite/utils/logging_test.cc | 31 +++++++ paddle/fluid/lite/utils/varient.h | 2 +- 32 files changed, 270 insertions(+), 57 deletions(-) create mode 100644 paddle/fluid/lite/utils/any.cc create mode 100644 paddle/fluid/lite/utils/any.h create mode 100644 paddle/fluid/lite/utils/logging.h create mode 100644 paddle/fluid/lite/utils/logging_test.cc diff --git a/paddle/fluid/inference/analysis/dot.h b/paddle/fluid/inference/analysis/dot.h index 4693729cb43..83212869d1f 100644 --- a/paddle/fluid/inference/analysis/dot.h +++ b/paddle/fluid/inference/analysis/dot.h @@ -19,11 +19,11 @@ */ #pragma once -#include #include #include #include #include +#include "paddle/fluid/lite/utils/logging.h" namespace paddle { namespace inference { diff --git a/paddle/fluid/lite/api/CMakeLists.txt b/paddle/fluid/lite/api/CMakeLists.txt index 5d832283322..5806bf5c67b 100644 --- a/paddle/fluid/lite/api/CMakeLists.txt +++ b/paddle/fluid/lite/api/CMakeLists.txt @@ -21,3 +21,5 @@ message(STATUS "get kernels ${host_kernels}") lite_cc_test(test_cxx_api_lite SRCS cxx_api_test.cc DEPS cxx_api_lite model_parser_lite target_wrapper_host ${ops_lite} ${host_kernels}) lite_cc_test(test_light_api SRCS light_api_test.cc DEPS light_api_lite) + +cc_binary(cxx_api_lite_bin SRCS cxx_api_bin.cc DEPS cxx_api_lite model_parser_lite target_wrapper_host ${ops_lite} ${host_kernels}) diff --git a/paddle/fluid/lite/api/cxx_api.cc b/paddle/fluid/lite/api/cxx_api.cc index 04bcb388f62..14a526fe1a4 100644 --- a/paddle/fluid/lite/api/cxx_api.cc +++ b/paddle/fluid/lite/api/cxx_api.cc @@ -13,15 +13,19 @@ // limitations under the License. #include "paddle/fluid/lite/api/cxx_api.h" +#ifndef LITE_WITH_LIGHT_WEIGHT_FRAMEWORK #include "paddle/fluid/platform/port.h" +#endif namespace paddle { namespace lite { +#ifndef LITE_WITH_LIGHT_WEIGHT_FRAMEWORK void LightPredictor::SaveModel(const std::string &dir) { MkDirRecursively(dir.c_str()); program_->PersistModel(dir, program_desc_); } +#endif } // namespace lite } // namespace paddle diff --git a/paddle/fluid/lite/api/cxx_api.h b/paddle/fluid/lite/api/cxx_api.h index bedec869ba6..5d6e503949c 100644 --- a/paddle/fluid/lite/api/cxx_api.h +++ b/paddle/fluid/lite/api/cxx_api.h @@ -41,7 +41,10 @@ class LightPredictor { program_ = optimizer_.GenRuntimeProgram(); } +// This method is disabled in mobile, or unnecessary dependencies required. +#ifndef LITE_WITH_LIGHT_WEIGHT_FRAMEWORK void SaveModel(const std::string& dir); +#endif // Get offset-th col of feed. lite::Tensor* GetInput(size_t offset) { diff --git a/paddle/fluid/lite/api/cxx_api_test.cc b/paddle/fluid/lite/api/cxx_api_test.cc index 3b1d3a2763f..daa3d1734e8 100644 --- a/paddle/fluid/lite/api/cxx_api_test.cc +++ b/paddle/fluid/lite/api/cxx_api_test.cc @@ -60,6 +60,7 @@ TEST(CXXApi, test) { LOG(INFO) << "out " << *out; } +#ifndef LITE_WITH_LIGHT_WEIGHT_FRAMEWORK TEST(CXXApi, save_model) { lite::LightPredictor predictor; std::vector valid_places({Place{TARGET(kHost), PRECISION(kFloat)}}); @@ -68,6 +69,7 @@ TEST(CXXApi, save_model) { predictor.SaveModel("./optimized_model"); } +#endif } // namespace lite } // namespace paddle diff --git a/paddle/fluid/lite/core/CMakeLists.txt b/paddle/fluid/lite/core/CMakeLists.txt index b02d34da325..56a93cf2b1b 100644 --- a/paddle/fluid/lite/core/CMakeLists.txt +++ b/paddle/fluid/lite/core/CMakeLists.txt @@ -19,6 +19,7 @@ cc_library(kernel_lite SRCS kernel.cc DEPS type_system target_wrapper_lite) cc_library(variable_lite SRCS variable.cc) cc_library(op_registry_lite SRCS op_registry.cc) cc_library(scope_lite SRCS scope.cc) +cc_library(context_lite SRCS context.cc DEPS any_lite) cc_library(op_lite SRCS op_lite.cc DEPS scope_lite op_registry_lite compatible_pb_lite) cc_library(types_lite SRCS types.cc) cc_library(type_system SRCS type_system.cc DEPS ${tensor_lite}) diff --git a/paddle/fluid/lite/core/context.h b/paddle/fluid/lite/core/context.h index 50ec1708d24..3e40588b73e 100644 --- a/paddle/fluid/lite/core/context.h +++ b/paddle/fluid/lite/core/context.h @@ -14,6 +14,7 @@ #pragma once +#include "paddle/fluid/lite/utils/any.h" #ifdef LITE_WITH_CUDA #include #include "paddle/fluid/lite/cuda/cuda_utils.h" @@ -25,6 +26,12 @@ namespace paddle { namespace lite { +struct HostContext {}; + +#ifdef LITE_WITH_ARM +struct ARMContext {}; +#endif + #ifdef LITE_WITH_CUDA // Only works with CUDA kernels. struct CUDAContext { @@ -44,6 +51,7 @@ struct CUDAContext { #ifdef LITE_WITH_X86 struct X86Context { // overall information + // kernel information }; #endif @@ -52,40 +60,16 @@ struct X86Context { // Holds the necessary resource and information. class KernelContext { public: -#ifdef LITE_WITH_CUDA - CUDAContext& AsCudaContext() { - if (target_ != TARGET(kUnk)) { - CHECK(target_ == TARGET(kCUDA)); - } else { - target_ = TARGET(kCUDA); - cuda_ctx_.reset(new CUDAContext); + template + ContextT& As() { + if (!ctx_.valid()) { + ctx_.set(); } - return *cuda_ctx_; + return *ctx_.get_mutable(); } -#endif // LITE_WITH_CUDA - -#ifdef LITE_WITH_X86 - X86Context& AsX86Context() { - if (target_ != TARGET(kUnk)) { - CHECK(target_ == TARGET(kX86)); - } else { - target_ = TARGET(kX86); - x86_ctx_.reset(new X86Context); - } - return *x86_ctx_; - } -#endif // lite_with_x86 private: -#ifdef LITE_WITH_CUDA - std::unique_ptr cuda_ctx_; -#endif - -#ifdef LITE_WITH_X86 - std::unique_ptr x86_ctx_; -#endif - - TargetType target_{TARGET(kUnk)}; + Any ctx_; }; } // namespace lite diff --git a/paddle/fluid/lite/core/memory.h b/paddle/fluid/lite/core/memory.h index 363156b596f..4d601df10d8 100644 --- a/paddle/fluid/lite/core/memory.h +++ b/paddle/fluid/lite/core/memory.h @@ -13,7 +13,6 @@ // limitations under the License. #pragma once -#include #include "paddle/fluid/lite/core/target_wrapper.h" namespace paddle { diff --git a/paddle/fluid/lite/core/mir/CMakeLists.txt b/paddle/fluid/lite/core/mir/CMakeLists.txt index 02e6947dd23..94f329aeda9 100644 --- a/paddle/fluid/lite/core/mir/CMakeLists.txt +++ b/paddle/fluid/lite/core/mir/CMakeLists.txt @@ -13,7 +13,7 @@ cc_library(mir_passes argument_type_display_pass.cc demo_pass.cc runtime_context_assign_pass.cc - DEPS mir_pass types_lite) + DEPS mir_pass types_lite context_lite) # for mobile, unnecessary to compile the following testings. if (LITE_WITH_LIGHT_WEIGHT_FRAMEWORK) diff --git a/paddle/fluid/lite/core/mir/node.h b/paddle/fluid/lite/core/mir/node.h index 91b39e1ac86..d827a021108 100644 --- a/paddle/fluid/lite/core/mir/node.h +++ b/paddle/fluid/lite/core/mir/node.h @@ -58,7 +58,7 @@ class Node { } KernelBase& picked_kernel() { - CHECK(!valid_kernels.empty()); + CHECK(!valid_kernels.empty()) << "no kernel for " << op_type; return *valid_kernels.front(); } diff --git a/paddle/fluid/lite/core/mir/runtime_context_assign_pass.cc b/paddle/fluid/lite/core/mir/runtime_context_assign_pass.cc index 2919de96f98..67fa3c7ccbb 100644 --- a/paddle/fluid/lite/core/mir/runtime_context_assign_pass.cc +++ b/paddle/fluid/lite/core/mir/runtime_context_assign_pass.cc @@ -52,7 +52,7 @@ class RuntimeContextAssignPass : public StmtPass { std::unique_ptr NewHostContext() { std::unique_ptr ctx(new KernelContext); - ctx->AsX86Context(); + ctx->As(); // Some initialization here. return ctx; } diff --git a/paddle/fluid/lite/core/mir/variable_place_inference_pass.cc b/paddle/fluid/lite/core/mir/variable_place_inference_pass.cc index 4998a800582..6d4b2ce2462 100644 --- a/paddle/fluid/lite/core/mir/variable_place_inference_pass.cc +++ b/paddle/fluid/lite/core/mir/variable_place_inference_pass.cc @@ -13,7 +13,6 @@ // limitations under the License. #include "paddle/fluid/lite/core/mir/variable_place_inference_pass.h" -#include #include "paddle/fluid/lite/core/mir/pass_registry.h" namespace paddle { diff --git a/paddle/fluid/lite/core/op_lite.cc b/paddle/fluid/lite/core/op_lite.cc index 0ce27e9a567..a15bee1f1d7 100644 --- a/paddle/fluid/lite/core/op_lite.cc +++ b/paddle/fluid/lite/core/op_lite.cc @@ -66,7 +66,10 @@ bool OpLite::Run() { } bool OpLite::Attach(const OpDesc &opdesc, lite::Scope *scope) { - CHECK(scope); + // valid_places_.clear(); + LOG(INFO) << "valid_places " << valid_places_.size(); + CHECK(scope != nullptr); + CHECK(!op_info_.get()); scope_ = scope; op_info_.reset(new OpInfo); // Force clean the out-of-date infomation. op_info_->Build(opdesc.ReadonlyProto()); diff --git a/paddle/fluid/lite/core/op_lite.h b/paddle/fluid/lite/core/op_lite.h index 6e7755a49dd..144198211e9 100644 --- a/paddle/fluid/lite/core/op_lite.h +++ b/paddle/fluid/lite/core/op_lite.h @@ -14,7 +14,6 @@ #pragma once -#include #include #include #include @@ -50,11 +49,15 @@ class OpInfo; class OpLite : public Registry { public: OpLite() = default; - OpLite(const std::string &type) : op_type_(type) {} - OpLite(const std::vector &valid_places) - : valid_places_(valid_places) {} + OpLite(const std::string &type) : op_type_(type) { + LOG(INFO) << "valid places " << valid_places_.size(); + } + OpLite(const std::vector &valid_places) : valid_places_(valid_places) { + LOG(INFO) << "valid places " << valid_places.size(); + } void SetValidPlaces(const std::vector &places) { + LOG(INFO) << "valid places " << valid_places_.size(); valid_places_ = places; } const std::vector &valid_places() const { return valid_places_; } diff --git a/paddle/fluid/lite/core/target_wrapper.h b/paddle/fluid/lite/core/target_wrapper.h index f14579bf6d7..49b26a3f6fb 100644 --- a/paddle/fluid/lite/core/target_wrapper.h +++ b/paddle/fluid/lite/core/target_wrapper.h @@ -13,9 +13,9 @@ // limitations under the License. #pragma once -#include #include #include +#include "paddle/fluid/lite/utils/cp_logging.h" #ifdef LITE_WITH_CUDA #include #include diff --git a/paddle/fluid/lite/core/type_system.h b/paddle/fluid/lite/core/type_system.h index 5ddb5c8ab6c..94f336d0b31 100644 --- a/paddle/fluid/lite/core/type_system.h +++ b/paddle/fluid/lite/core/type_system.h @@ -20,7 +20,6 @@ // the dubious typed Variables in the Operators' inputs and outputs are disaster // for analysis and runtime. -#include #include #include #include diff --git a/paddle/fluid/lite/cuda/blas.h b/paddle/fluid/lite/cuda/blas.h index ee11512fee4..471d1b205c7 100644 --- a/paddle/fluid/lite/cuda/blas.h +++ b/paddle/fluid/lite/cuda/blas.h @@ -16,8 +16,6 @@ #include #include #include -#include -#include #include #include "paddle/fluid/lite/cuda/cuda_utils.h" #include "paddle/fluid/lite/utils/all.h" diff --git a/paddle/fluid/lite/cuda/cuda_utils.h b/paddle/fluid/lite/cuda/cuda_utils.h index 050338b9846..b3e083e528d 100644 --- a/paddle/fluid/lite/cuda/cuda_utils.h +++ b/paddle/fluid/lite/cuda/cuda_utils.h @@ -17,7 +17,7 @@ #include #include #include -#include +#include "paddle/fluid/lite/utils/cp_logging.h" /* * This file contains some CUDA specific utils. diff --git a/paddle/fluid/lite/cuda/target_wrapper.cc b/paddle/fluid/lite/cuda/target_wrapper.cc index 7e6936d613c..69586f04ef5 100644 --- a/paddle/fluid/lite/cuda/target_wrapper.cc +++ b/paddle/fluid/lite/cuda/target_wrapper.cc @@ -13,7 +13,6 @@ // limitations under the License. #include "paddle/fluid/lite/cuda/target_wrapper.h" -#include namespace paddle { namespace lite { diff --git a/paddle/fluid/lite/kernels/host/fc_compute.h b/paddle/fluid/lite/kernels/host/fc_compute.h index 78710c1202d..1a6c4eb4c0f 100644 --- a/paddle/fluid/lite/kernels/host/fc_compute.h +++ b/paddle/fluid/lite/kernels/host/fc_compute.h @@ -13,7 +13,6 @@ // limitations under the License. #pragma once -#include #include #include "paddle/fluid/lite/core/kernel.h" #include "paddle/fluid/lite/operators/fc_op.h" diff --git a/paddle/fluid/lite/model_parser/pb/var_desc.h b/paddle/fluid/lite/model_parser/pb/var_desc.h index 4975a0e0d43..52a6caed411 100644 --- a/paddle/fluid/lite/model_parser/pb/var_desc.h +++ b/paddle/fluid/lite/model_parser/pb/var_desc.h @@ -14,11 +14,11 @@ #pragma once -#include #include #include #include #include "paddle/fluid/framework/framework.pb.h" +#include "paddle/fluid/lite/utils/cp_logging.h" namespace paddle { namespace lite { diff --git a/paddle/fluid/lite/model_parser/runtime.cc b/paddle/fluid/lite/model_parser/runtime.cc index a8a7eb069cd..eeef1611e69 100644 --- a/paddle/fluid/lite/model_parser/runtime.cc +++ b/paddle/fluid/lite/model_parser/runtime.cc @@ -13,9 +13,6 @@ // limitations under the License. #include "paddle/fluid/lite/model_parser/runtime.h" -#include "runtime.h" - -#include namespace paddle { namespace lite { diff --git a/paddle/fluid/lite/model_parser/runtime.h b/paddle/fluid/lite/model_parser/runtime.h index ea87eb248eb..38f710c9414 100644 --- a/paddle/fluid/lite/model_parser/runtime.h +++ b/paddle/fluid/lite/model_parser/runtime.h @@ -13,7 +13,6 @@ // limitations under the License. #pragma once -#include #include "paddle/fluid/framework/framework.pb.h" #include "paddle/fluid/lite/utils/all.h" diff --git a/paddle/fluid/lite/utils/CMakeLists.txt b/paddle/fluid/lite/utils/CMakeLists.txt index 73f20d873da..3c459e49aff 100644 --- a/paddle/fluid/lite/utils/CMakeLists.txt +++ b/paddle/fluid/lite/utils/CMakeLists.txt @@ -1 +1,12 @@ -lite_cc_test(test_varient SRCS varient_test.cc) +if(LITE_WITH_LIGHT_WEIGHT_FRAMEWORK) + set(utils_DEPS) +else() + set(utils_DEPS glog) +endif() + +lite_cc_test(test_logging_lite SRCS logging_test.cc) + +cc_library(utils_lite SRCS cp_logging.cc DEPS ${utils_DEPS}) +lite_cc_test(test_varient SRCS varient_test.cc DEPS utils_lite) + +cc_library(any_lite SRCS any.cc) diff --git a/paddle/fluid/lite/utils/all.h b/paddle/fluid/lite/utils/all.h index ff1242163d6..2ee0e078c38 100644 --- a/paddle/fluid/lite/utils/all.h +++ b/paddle/fluid/lite/utils/all.h @@ -15,6 +15,7 @@ #pragma once #include "paddle/fluid/lite/utils/check.h" +#include "paddle/fluid/lite/utils/cp_logging.h" #include "paddle/fluid/lite/utils/factory.h" #include "paddle/fluid/lite/utils/hash.h" #include "paddle/fluid/lite/utils/io.h" diff --git a/paddle/fluid/lite/utils/any.cc b/paddle/fluid/lite/utils/any.cc new file mode 100644 index 00000000000..308e122f208 --- /dev/null +++ b/paddle/fluid/lite/utils/any.cc @@ -0,0 +1,23 @@ +// Copyright (c) 2019 PaddlePaddle Authors. 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 "paddle/fluid/lite/utils/any.h" + +namespace paddle { +namespace lite { + +size_t Any::kInvalidType{typeid(void).hash_code()}; + +} // namespace lite +} // namespace paddle diff --git a/paddle/fluid/lite/utils/any.h b/paddle/fluid/lite/utils/any.h new file mode 100644 index 00000000000..466deae3de9 --- /dev/null +++ b/paddle/fluid/lite/utils/any.h @@ -0,0 +1,66 @@ +// Copyright (c) 2019 PaddlePaddle Authors. 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. + +#pragma once +#include +#include +#include "paddle/fluid/lite/utils/cp_logging.h" + +namespace paddle { +namespace lite { + +class Any { + public: + template + void set(const T& v) { + set(); + *get_mutable() = v; + } + + template + void set() { + if (type_ != kInvalidType) { + CHECK(type_ == typeid(T).hash_code()); + } else { + type_ = typeid(T).hash_code(); + data_ = new T; + deleter_ = [&] { delete static_cast(data_); }; + } + data_ = new T; + } + + template + const T& get() const { + CHECK(data_); + CHECK(type_ == typeid(T).hash_code()); + return *static_cast(data_); + } + template + T* get_mutable() { + CHECK(data_); + CHECK(type_ == typeid(T).hash_code()); + return static_cast(data_); + } + + bool valid() const { return data_; } + + private: + static size_t kInvalidType; + size_t type_{kInvalidType}; + void* data_{}; + std::function deleter_; +}; + +} // namespace lite +} // namespace paddle diff --git a/paddle/fluid/lite/utils/factory.h b/paddle/fluid/lite/utils/factory.h index f8386c08de5..97abce93747 100644 --- a/paddle/fluid/lite/utils/factory.h +++ b/paddle/fluid/lite/utils/factory.h @@ -13,12 +13,12 @@ // limitations under the License. #pragma once -#include #include #include #include #include #include +#include "paddle/fluid/lite/utils/cp_logging.h" namespace paddle { namespace lite { diff --git a/paddle/fluid/lite/utils/io.h b/paddle/fluid/lite/utils/io.h index c4ac283eaea..4dba6f98429 100644 --- a/paddle/fluid/lite/utils/io.h +++ b/paddle/fluid/lite/utils/io.h @@ -14,13 +14,15 @@ #pragma once +#include #include #include +#include "paddle/fluid/lite/utils/cp_logging.h" namespace paddle { namespace lite { -static bool IsFileExists(const std::string& path) { +static bool IsFileExists(const std::string &path) { std::ifstream file(path); bool res = file.is_open(); if (res) { diff --git a/paddle/fluid/lite/utils/logging.h b/paddle/fluid/lite/utils/logging.h new file mode 100644 index 00000000000..df25fec2f64 --- /dev/null +++ b/paddle/fluid/lite/utils/logging.h @@ -0,0 +1,88 @@ +// Copyright (c) 2019 PaddlePaddle Authors. 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. + +/* + * This file implements an lightweight alternative for glog, which is more + * friendly for mobile. + */ +#pragma once +#include +#include +#include +#include + +// LOG() +#define LOG(status) LOG_##status.stream() +#define LOG_ERROR LOG_INFO +#define LOG_INFO paddle::lite::LogMessage(__FILE__, __LINE__) +#define LOG_WARNING paddle::lite::LogMessage(__FILE__, __LINE__) +#define LOG_FATAL paddle::lite::LogMessageFatal(__FILE__, __LINE__) +// Not supported yet. +#define VLOG(level) LOG_INFO.stream() + +// CHECK() +#define CHECK(x) \ + if (!(x)) \ + paddle::lite::LogMessageFatal(__FILE__, __LINE__).stream() \ + << "Check failed: " #x << ": " +#define CHECK_EQ(x, y) _CHECK_BINARY(x, ==, y) +#define CHECK_LT(x, y) _CHECK_BINARY(x, <, y) +#define CHECK_LE(x, y) _CHECK_BINARY(x, <=, y) +#define CHECK_GT(x, y) _CHECK_BINARY(x, >, y) +#define CHECK_GE(x, y) _CHECK_BINARY(x, >=, y) +#define _CHECK_BINARY(x, cmp, y) CHECK(x cmp y) << x << "!" #cmp << y << " " + +namespace paddle { +namespace lite { + +class LogMessage { + public: + LogMessage(const char* file, int lineno) { + const int kMaxLen = 20; + const int len = strlen(file); + if (len > kMaxLen) { + log_stream_ << '[' << "..." << file + len - kMaxLen << ":" << lineno + << "] "; + } else { + log_stream_ << '[' << file << ":" << lineno << "] "; + } + } + + ~LogMessage() { + log_stream_ << '\n'; + std::cerr << log_stream_.str(); + } + + std::ostream& stream() { return log_stream_; } + + protected: + std::stringstream log_stream_; + + LogMessage(const LogMessage&) = delete; + void operator=(const LogMessage&) = delete; +}; + +class LogMessageFatal : public LogMessage { + public: + LogMessageFatal(const char* file, int lineno) : LogMessage(file, lineno) {} + + ~LogMessageFatal() { + log_stream_ << '\n'; + std::cerr << log_stream_.str(); + abort(); + } +}; + +} // namespace lite +} // namespace paddle diff --git a/paddle/fluid/lite/utils/logging_test.cc b/paddle/fluid/lite/utils/logging_test.cc new file mode 100644 index 00000000000..4aac0af00cb --- /dev/null +++ b/paddle/fluid/lite/utils/logging_test.cc @@ -0,0 +1,31 @@ +// Copyright (c) 2019 PaddlePaddle Authors. 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 "paddle/fluid/lite/utils/logging.h" +#include + +namespace paddle { +namespace lite { + +TEST(logging, basic) { + CHECK_EQ(1, 1) << "error"; + CHECK_EQ(0 + 1, 1) << "error"; + CHECK_GT(12, 1); + + int a; + CHECK(&a); +} + +} // namespace lite +} // namespace paddle diff --git a/paddle/fluid/lite/utils/varient.h b/paddle/fluid/lite/utils/varient.h index fdd59502fcc..4a9eacabfdf 100644 --- a/paddle/fluid/lite/utils/varient.h +++ b/paddle/fluid/lite/utils/varient.h @@ -13,11 +13,11 @@ // limitations under the License. #pragma once -#include #include #include #include #include +#include "paddle/fluid/lite/utils/cp_logging.h" // This is an equivalent implementation of boost::any. We implement this to // avoid including the whole boost library and keep the inference library small. -- GitLab