diff --git a/paddle/fluid/inference/analysis/dot.h b/paddle/fluid/inference/analysis/dot.h index 4693729cb43d7a9df96b11c4bf3064a70d1db4c3..83212869d1f0130be45e8a7476e4095120372eed 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 5d8322833227e0e418ebb1de81bd865f26dfd212..5806bf5c67b34abea6ac629c0376f58801f7005d 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 04bcb388f627c97ea549f00f3464b01a9e21dae5..14a526fe1a4ec7d4014269bd4ea75514f1d64502 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 bedec869ba609a71649f59e907d28133ead4ef34..5d6e503949c4c77cced84eef8310b7ef41312fec 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 3b1d3a2763fc25b80a211ede2bade9ab73b65485..daa3d1734e8cf7681bf58fa40f0bc8e6c1e02b45 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 b02d34da3251b0462a79f2fd3c1803045943f58b..56a93cf2b1bef641fa74a0ecc69b83d0105910f6 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 50ec1708d24cbfaba6a532f9c5d2a0a8b63d66a9..3e40588b73ea8c152bf37b5e4127db4cba14fb83 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 363156b596f953e7e436a36aa1835bf6db255792..4d601df10d858bb40dd8aefe46d5d447e5a5b157 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 02e6947dd23125f5f539c8065c7387a11248aca3..94f329aeda9deb0574de7dc5c2033795261be330 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 91b39e1ac86c2698d02d119acbaae929a941efc0..d827a021108793abd1737d2c164c8d4092dba658 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 2919de96f98f11368b7ff90386a1780b43ae9cd7..67fa3c7ccbbebe4ed50c924fa78248bce348ff9e 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 4998a8005822f951257d3a32cde212583dbb264b..6d4b2ce246204e435c08d80c682a8cdfffbdeef5 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 0ce27e9a5673fe026dda42c1e1da5ea79477d3e4..a15bee1f1d7c85429382c3a5aa8c22a2f0c2706c 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 6e7755a49dd19cb97c3623f8f83c12cb4e094366..144198211e9ff3722c269d16ee94e486423af3ca 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 f14579bf6d724cf103bf698706acb47a039764ac..49b26a3f6fbdbe5bbc55152546ce7def24a2e361 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 5ddb5c8ab6c8ae1accd7e31005431cba085a1a57..94f336d0b3154a73b1ec754a18d0c9985838a0e0 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 ee11512fee4548af3bd56f0be13dbacf0f5fa98a..471d1b205c7cdb1304708be524cb52806e9749fe 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 050338b9846baacae4a629ce24d24212199869cd..b3e083e528d1921705c5762e5ea86853b5500f05 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 7e6936d613c953e22dd69a012ccb3fcb827ee546..69586f04ef57eaf852e3c59d801a737b0a794bb5 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 78710c1202d58560ab04a8028b4e311e966b60ce..1a6c4eb4c0fbe1398bf6e2356eb902a112c578b9 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 4975a0e0d43baba48c4e78290e2f7ca530a28829..52a6caed411029cd091fcc4e8742e4130f0331ec 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 a8a7eb069cd893b76becd81118e33fce0ff91479..eeef1611e69b59c8e9c83b18ec2a6f2c385259b0 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 ea87eb248eb49361e8a1e9391b87f90bb1c82b97..38f710c941405c02a44f13aff9d1991116133bc3 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 73f20d873dad17f4fe54c397a1d516939b887a22..3c459e49affbe3387179e55c8186caad9d8b98ce 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 ff1242163d6fffe1b1bd3c7415515dc82b120465..2ee0e078c38d5947ff151c717fa0ec631db534a6 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 0000000000000000000000000000000000000000..308e122f20898d5bec368afec910e73c8293bec8 --- /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 0000000000000000000000000000000000000000..466deae3de92ad5992695a505108e1e31b68a826 --- /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 f8386c08de5539056dd5323eeb6a3ff75d3dffee..97abce93747c98d24c7e5013fd8c6cea4c1e20aa 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 c4ac283eaea5b52e6c3797c518ed457314431f4a..4dba6f984292235d3f947477b09152bc37c2adb9 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 0000000000000000000000000000000000000000..df25fec2f6428668cef7b5e0e706601d72a1bf16 --- /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 0000000000000000000000000000000000000000..4aac0af00cb28098fc807190ae88d8b0ecedf9bb --- /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 fdd59502fccb4ccabba5026f68a84f166e4f4e91..4a9eacabfdfe7c8b8f566062c646fa7bbadb560c 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.