diff --git a/paddle/fluid/framework/type_defs.h b/paddle/fluid/framework/type_defs.h index 61859264441abed87b7e56a099f1e61d5e41a5cc..0937d96ad4c204f23c3aa63b8c7a47f23ef590fa 100644 --- a/paddle/fluid/framework/type_defs.h +++ b/paddle/fluid/framework/type_defs.h @@ -21,8 +21,8 @@ limitations under the License. */ #include #include #include +#include "paddle/fluid/imperative/type_defs.h" #include "paddle/fluid/platform/variant.h" -#include "paddle/phi/core/compat/type_defs.h" #include "paddle/utils/small_vector.h" namespace paddle { @@ -39,6 +39,12 @@ class InferNoNeedBufferVarsFN; using VariableNameMap = std::map>; using VariableValueMap = std::map>; +using Attribute = boost::variant< + boost::blank, int, float, std::string, std::vector, std::vector, + std::vector, bool, std::vector, BlockDesc*, int64_t, + std::vector, std::vector, std::vector>; +using AttributeMap = std::unordered_map; + #ifdef PADDLE_WITH_ASCEND_CL using NPUAttribute = boost::variant, diff --git a/paddle/fluid/imperative/layout_autotune.h b/paddle/fluid/imperative/layout_autotune.h index 679612fdf1ae3d547bef0f6a0ec6d3e220afa9ca..df3772b826da1ec09c84057c48cb87d301a697ca 100644 --- a/paddle/fluid/imperative/layout_autotune.h +++ b/paddle/fluid/imperative/layout_autotune.h @@ -16,8 +16,8 @@ #include #include #include +#include "paddle/fluid/framework/type_defs.h" #include "paddle/phi/common/layout.h" -#include "paddle/phi/core/compat/type_defs.h" namespace paddle { namespace imperative { diff --git a/paddle/fluid/imperative/tests/test_eager.cc b/paddle/fluid/imperative/tests/test_eager.cc index 4a0b99518a63f87e67c33d96f06a39e1e904cee1..3def103ae9aa5c68932b717190b7972fd3660d36 100644 --- a/paddle/fluid/imperative/tests/test_eager.cc +++ b/paddle/fluid/imperative/tests/test_eager.cc @@ -23,10 +23,10 @@ #include "paddle/fluid/imperative/execution_context.h" #include "paddle/fluid/imperative/layer.h" #include "paddle/fluid/imperative/tracer.h" +#include "paddle/fluid/imperative/type_defs.h" #include "paddle/fluid/imperative/var_helper.h" #include "paddle/fluid/memory/memcpy.h" #include "paddle/fluid/platform/device_context.h" -#include "paddle/phi/core/compat/type_defs.h" namespace paddle { namespace imperative { diff --git a/paddle/fluid/imperative/type_defs.h b/paddle/fluid/imperative/type_defs.h index 79b6e057d21fc110089eb4ad1468512fda11058f..08f3c8d4a0fc2b87f5eb78a70da355d78729f060 100644 --- a/paddle/fluid/imperative/type_defs.h +++ b/paddle/fluid/imperative/type_defs.h @@ -13,4 +13,58 @@ See the License for the specific language governing permissions and limitations under the License. */ #pragma once -#include "paddle/phi/core/compat/type_defs.h" + +#include +#include +#include +#include + +namespace egr { +class EagerVariable; +} +namespace paddle { +namespace imperative { + +class VariableWrapper; +class SavedVariableWrapperList; +class VarBase; +class OpBase; +class GradOpNode; +class Tracer; + +using WeakNameVarBaseMap = + std::map>>; + +namespace details { +template +struct NameVarMapTrait {}; + +template <> +struct NameVarMapTrait { + using Type = std::map>>; +}; + +template <> +struct NameVarMapTrait { + using Type = std::map; +}; + +template <> +struct NameVarMapTrait { + using Type = + std::map>>; +}; + +} // namespace details + +template +using NameVarMap = typename details::NameVarMapTrait::Type; + +using NameVarBaseMap = NameVarMap; +using NameVariableWrapperMap = NameVarMap; +using NameTensorMap = NameVarMap; + +using VariableWrapperList = std::vector>; + +} // namespace imperative +} // namespace paddle diff --git a/paddle/fluid/memory/allocation/allocator.h b/paddle/fluid/memory/allocation/allocator.h index 003d6988671b5a0a1d282ce2c44e44a70a2d39c8..d51ec52a49e690566da414bf0c9d149acf4380cf 100644 --- a/paddle/fluid/memory/allocation/allocator.h +++ b/paddle/fluid/memory/allocation/allocator.h @@ -33,8 +33,8 @@ namespace allocation { // Exception when `Alloc`/`AllocShared` failed struct BadAlloc : public std::exception { inline explicit BadAlloc(std::string err_msg, const char* file, int line) - : err_str_(platform::GetTraceBackString(std::move(err_msg), file, line)) { - } + : err_str_(platform::GetCompleteTraceBackString(std::move(err_msg), file, + line)) {} const char* what() const noexcept override { return err_str_.c_str(); } diff --git a/paddle/fluid/pybind/imperative.cc b/paddle/fluid/pybind/imperative.cc index 1da0831fc6323111835a9bacc8eecd69fe655b3c..83acedc69bb6018d16a47351f272331e9232f5c1 100644 --- a/paddle/fluid/pybind/imperative.cc +++ b/paddle/fluid/pybind/imperative.cc @@ -59,7 +59,6 @@ limitations under the License. */ #include "paddle/fluid/pybind/tensor_py.h" #include "paddle/fluid/pybind/uva_utils.h" #include "paddle/phi/core/compat/arg_map_context.h" -#include "paddle/phi/core/compat/type_defs.h" #include "paddle/phi/core/type_defs.h" namespace paddle { diff --git a/paddle/infrt/tensor/phi/tensor_map.h b/paddle/infrt/tensor/phi/tensor_map.h index 1b9fbdd9defc735131c0ead15b38c98dc619650a..5a754f42fb63cac8b49a46a3ce258dd461269161 100644 --- a/paddle/infrt/tensor/phi/tensor_map.h +++ b/paddle/infrt/tensor/phi/tensor_map.h @@ -14,6 +14,8 @@ #pragma once +#include + #include "paddle/phi/core/dense_tensor.h" namespace infrt { diff --git a/paddle/phi/backends/device_base.cc b/paddle/phi/backends/device_base.cc index 14fe90192e5bcaedf50ba96a6a047d08f622a76d..b72c6efd51f2c23d7bdb3f2d72eafc868e26c43f 100644 --- a/paddle/phi/backends/device_base.cc +++ b/paddle/phi/backends/device_base.cc @@ -14,6 +14,7 @@ #include "paddle/phi/backends/device_base.h" #include "gflags/gflags.h" +#include "glog/logging.h" #include "paddle/phi/core/enforce.h" DECLARE_double(fraction_of_gpu_memory_to_use); diff --git a/paddle/phi/backends/device_manager.h b/paddle/phi/backends/device_manager.h index 39eef27b4a607bd3af75a6b5dde07f715e5537e5..18d51687ef121c29e3a51021e9b9a58923ddb40d 100644 --- a/paddle/phi/backends/device_manager.h +++ b/paddle/phi/backends/device_manager.h @@ -15,6 +15,8 @@ #pragma once #ifdef PADDLE_WITH_CUSTOM_DEVICE +#include + #include "paddle/phi/backends/device_base.h" #include "paddle/phi/backends/device_ext.h" #include "paddle/phi/backends/event.h" diff --git a/paddle/phi/backends/gpu/gpu_context.cc b/paddle/phi/backends/gpu/gpu_context.cc index e5d34376834dd7eec866b65f52e9c0864decbe32..e8c264b884fe3105dcab540846639f433b647fda 100644 --- a/paddle/phi/backends/gpu/gpu_context.cc +++ b/paddle/phi/backends/gpu/gpu_context.cc @@ -14,6 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ #include "paddle/phi/backends/gpu/gpu_context.h" + #include #include #include @@ -21,8 +22,9 @@ limitations under the License. */ #include #include -#include "paddle/phi/api/ext/exception.h" +#include "glog/logging.h" +#include "paddle/phi/api/ext/exception.h" #include "paddle/phi/backends/gpu/gpu_decls.h" #include "paddle/phi/backends/gpu/gpu_info.h" #include "paddle/phi/backends/gpu/gpu_resources.h" diff --git a/paddle/phi/core/CMakeLists.txt b/paddle/phi/core/CMakeLists.txt index dcf1826012c1319bf78a001fff46d77768a49e48..41f654bfc8f304f514838e8fa2b2d10142b5d7ab 100644 --- a/paddle/phi/core/CMakeLists.txt +++ b/paddle/phi/core/CMakeLists.txt @@ -6,7 +6,7 @@ set(phi_enforce_deps errors flags) if(WITH_GPU) set(phi_enforce_deps ${phi_enforce_deps} external_error_proto) endif() -cc_library(phi_enforce INTERFACE SRCS enforce.cc DEPS ${phi_enforce_deps}) +cc_library(phi_enforce SRCS enforce.cc DEPS ${phi_enforce_deps}) cc_library(kernel_factory SRCS kernel_factory.cc DEPS phi_enforce fluid_convert_utils) cc_library(kernel_context SRCS kernel_context.cc DEPS phi_enforce phi_context) diff --git a/paddle/phi/core/compat/op_utils.h b/paddle/phi/core/compat/op_utils.h index bd19d403c94061b120ac2cd10dac0e6002d495ea..8eb6524e79c0f7b3e3551f898544aef2348a28be 100644 --- a/paddle/phi/core/compat/op_utils.h +++ b/paddle/phi/core/compat/op_utils.h @@ -17,6 +17,8 @@ limitations under the License. */ #include #include +#include "glog/logging.h" + #include "paddle/phi/core/compat/arg_map_context.h" #include "paddle/phi/core/enforce.h" #include "paddle/phi/core/infermeta_utils.h" diff --git a/paddle/phi/core/compat/type_defs.h b/paddle/phi/core/compat/type_defs.h deleted file mode 100644 index c9d7d5bb54b620ceeac55de21a28e2440a15186b..0000000000000000000000000000000000000000 --- a/paddle/phi/core/compat/type_defs.h +++ /dev/null @@ -1,96 +0,0 @@ -/* Copyright (c) 2016 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 -#include -#include -#include - -#include - -namespace egr { -class EagerVariable; -} -namespace paddle { -namespace framework { -// The order should be as same as framework.proto -// NOTE(xiongkun): we extract from framework/typedef.h to ensure we can transfer -// enforce.h -class BlockDesc; -using Attribute = boost::variant, - std::vector, - std::vector, - bool, - std::vector, - BlockDesc*, - int64_t, - std::vector, - std::vector, - std::vector>; -using AttributeMap = std::unordered_map; -} // namespace framework - -namespace imperative { - -class VariableWrapper; -class SavedVariableWrapperList; -class VarBase; -class OpBase; -class GradOpNode; -class Tracer; - -using WeakNameVarBaseMap = - std::map>>; - -namespace details { -template -struct NameVarMapTrait {}; - -template <> -struct NameVarMapTrait { - using Type = std::map>>; -}; - -template <> -struct NameVarMapTrait { - using Type = std::map; -}; - -template <> -struct NameVarMapTrait { - using Type = - std::map>>; -}; - -} // namespace details - -template -using NameVarMap = typename details::NameVarMapTrait::Type; - -using NameVarBaseMap = NameVarMap; -using NameVariableWrapperMap = NameVarMap; -using NameTensorMap = NameVarMap; - -using VariableWrapperList = std::vector>; - -} // namespace imperative -} // namespace paddle diff --git a/paddle/phi/core/custom_kernel.cc b/paddle/phi/core/custom_kernel.cc index 48778bb38e5487506f4b402176fff26cbe485de7..356dd1482c9d8330e97a0395e3ead25edf5f75a8 100644 --- a/paddle/phi/core/custom_kernel.cc +++ b/paddle/phi/core/custom_kernel.cc @@ -14,6 +14,8 @@ #include "paddle/phi/core/custom_kernel.h" +#include "glog/logging.h" + namespace phi { void CustomKernelMap::RegisterCustomKernel(const std::string& name, diff --git a/paddle/phi/core/enforce.cc b/paddle/phi/core/enforce.cc index c5ac3b7815bf9c4427c2e118321bd5034325cbb0..ae6b0135b3222effa7207c3fc28d4af6cbf1a7be 100644 --- a/paddle/phi/core/enforce.cc +++ b/paddle/phi/core/enforce.cc @@ -13,3 +13,162 @@ See the License for the specific language governing permissions and limitations under the License. */ #include "paddle/phi/core/enforce.h" + +#include +#include +#include +#include + +#include + +// is not suitable to be placed in the header file, +// it will introduce a large number of unnecessary includes, and these type +// declarations that depend on boost are also not suitable for the phi header +// file. Do some repeated forward declarations here to avoid +// spreading to a large number of phi kernel files +namespace egr { +class EagerVariable; +} +namespace paddle { +namespace framework { +class BlockDesc; +using Attribute = boost::variant, + std::vector, + std::vector, + bool, + std::vector, + BlockDesc*, + int64_t, + std::vector, + std::vector, + std::vector>; +using AttributeMap = std::unordered_map; +} // namespace framework +namespace imperative { +class VariableWrapper; +class SavedVariableWrapperList; +class VarBase; + +namespace details { +template +struct NameVarMapTrait {}; + +template <> +struct NameVarMapTrait { + using Type = std::map>>; +}; + +template <> +struct NameVarMapTrait { + using Type = std::map; +}; + +template <> +struct NameVarMapTrait { + using Type = + std::map>>; +}; + +} // namespace details + +template +using NameVarMap = typename details::NameVarMapTrait::Type; + +using NameVarBaseMap = NameVarMap; +using NameVariableWrapperMap = NameVarMap; +using NameTensorMap = NameVarMap; + +} // namespace imperative +} // namespace paddle + +namespace phi { +namespace enforce { + +template +static std::string ReplaceComplexTypeStr(std::string str, + const std::string& type_name) { + auto demangle_type_str = demangle(typeid(T).name()); + size_t start_pos = 0; + while ((start_pos = str.find(demangle_type_str, start_pos)) != + std::string::npos) { + str.replace(start_pos, demangle_type_str.length(), type_name); + start_pos += type_name.length(); + } + return str; +} + +#define __REPLACE_COMPLEX_TYPE_STR__(__TYPENAME, __STR) \ + do { \ + __STR = \ + phi::enforce::ReplaceComplexTypeStr<__TYPENAME>(__STR, #__TYPENAME); \ + } while (0) + +static std::string SimplifyDemangleStr(std::string str) { + // the older is important, you have to put complex types in front + __REPLACE_COMPLEX_TYPE_STR__(paddle::framework::AttributeMap, str); + __REPLACE_COMPLEX_TYPE_STR__(paddle::framework::Attribute, str); + __REPLACE_COMPLEX_TYPE_STR__(paddle::imperative::NameVariableWrapperMap, str); + __REPLACE_COMPLEX_TYPE_STR__(paddle::imperative::NameVarBaseMap, str); + __REPLACE_COMPLEX_TYPE_STR__(paddle::imperative::NameTensorMap, str); + __REPLACE_COMPLEX_TYPE_STR__(std::string, str); + return str; +} + +std::string GetCurrentTraceBackString(bool for_signal) { + std::ostringstream sout; + + if (!for_signal) { + sout << "\n\n--------------------------------------\n"; + sout << "C++ Traceback (most recent call last):"; + sout << "\n--------------------------------------\n"; + } +#if !defined(_WIN32) && !defined(PADDLE_WITH_MUSL) + static constexpr int TRACE_STACK_LIMIT = 100; + + void* call_stack[TRACE_STACK_LIMIT]; + auto size = backtrace(call_stack, TRACE_STACK_LIMIT); + auto symbols = backtrace_symbols(call_stack, size); + Dl_info info; + int idx = 0; + // `for_signal` used to remove the stack trace introduced by + // obtaining the error stack trace when the signal error occurred, + // that is not related to the signal error self, remove it to + // avoid misleading users and developers + int end_idx = for_signal ? 2 : 0; + for (int i = size - 1; i >= end_idx; --i) { + if (dladdr(call_stack[i], &info) && info.dli_sname) { + auto demangled = demangle(info.dli_sname); + std::string path(info.dli_fname); + // C++ traceback info are from core.so + if (path.substr(path.length() - 3).compare(".so") == 0) { + sout << paddle::string::Sprintf( + "%-3d %s\n", idx++, SimplifyDemangleStr(demangled)); + } + } + } + free(symbols); +#else + sout << "Not support stack backtrace yet.\n"; +#endif + return sout.str(); +} + +std::string SimplifyErrorTypeFormat(const std::string& str) { + std::ostringstream sout; + size_t type_end_pos = str.find(":", 0); + if (type_end_pos == std::string::npos) { + sout << str; + } else { + // Remove "Error:", add "()"" + sout << "(" << str.substr(0, type_end_pos - 5) << ")" + << str.substr(type_end_pos + 1); + } + return sout.str(); +} + +} // namespace enforce +} // namespace phi diff --git a/paddle/phi/core/enforce.h b/paddle/phi/core/enforce.h index 2c41c6235329616016534408c2738a821ff638f0..8da2623bb2c2d4f64623dabb46fc115e3687cdb0 100644 --- a/paddle/phi/core/enforce.h +++ b/paddle/phi/core/enforce.h @@ -28,38 +28,32 @@ limitations under the License. */ #include // GetModuleFileName, Sleep #endif -#include -#include -#include #include #include #include #include -#include #if !defined(_WIN32) && !defined(PADDLE_WITH_MUSL) #include #endif -#define GLOG_NO_ABBREVIATED_SEVERITIES // msvc conflict logging with windows.h #include "gflags/gflags.h" -#include "glog/logging.h" + +#define GLOG_NO_ABBREVIATED_SEVERITIES // msvc conflict logging with windows.h #include "paddle/phi/core/errors.h" #include "paddle/utils/string/printf.h" #include "paddle/utils/string/to_string.h" -// Note: these headers for simplify demangle type string -#include "paddle/phi/core/compat/type_defs.h" +DECLARE_int32(call_stack_level); namespace phi { class ErrorSummary; } // namespace phi -DECLARE_int32(call_stack_level); namespace phi { namespace enforce { -/** HELPER MACROS AND FUNCTIONS **/ +/** HELPER MACROS AND FUNCTIONS **/ #ifndef PADDLE_MAY_THROW #define PADDLE_MAY_THROW noexcept(false) #endif @@ -180,76 +174,11 @@ struct BinaryCompareMessageConverter { }; } // namespace details -template -inline std::string ReplaceComplexTypeStr(std::string str, - const std::string& type_name) { - auto demangle_type_str = demangle(typeid(T).name()); - size_t start_pos = 0; - while ((start_pos = str.find(demangle_type_str, start_pos)) != - std::string::npos) { - str.replace(start_pos, demangle_type_str.length(), type_name); - start_pos += type_name.length(); - } - return str; -} - -#define __REPLACE_COMPLEX_TYPE_STR__(__TYPENAME, __STR) \ - do { \ - __STR = \ - phi::enforce::ReplaceComplexTypeStr<__TYPENAME>(__STR, #__TYPENAME); \ - } while (0) - -inline std::string SimplifyDemangleStr(std::string str) { - // the older is important, you have to put complex types in front - __REPLACE_COMPLEX_TYPE_STR__(paddle::framework::AttributeMap, str); - __REPLACE_COMPLEX_TYPE_STR__(paddle::framework::Attribute, str); - __REPLACE_COMPLEX_TYPE_STR__(paddle::imperative::NameVariableWrapperMap, str); - __REPLACE_COMPLEX_TYPE_STR__(paddle::imperative::NameVarBaseMap, str); - __REPLACE_COMPLEX_TYPE_STR__(std::string, str); - return str; -} - -inline std::string GetCurrentTraceBackString(bool for_signal = false) { - std::ostringstream sout; - - if (!for_signal) { - sout << "\n\n--------------------------------------\n"; - sout << "C++ Traceback (most recent call last):"; - sout << "\n--------------------------------------\n"; - } -#if !defined(_WIN32) && !defined(PADDLE_WITH_MUSL) - static constexpr int TRACE_STACK_LIMIT = 100; - - void* call_stack[TRACE_STACK_LIMIT]; - auto size = backtrace(call_stack, TRACE_STACK_LIMIT); - auto symbols = backtrace_symbols(call_stack, size); - Dl_info info; - int idx = 0; - // `for_signal` used to remove the stack trace introduced by - // obtaining the error stack trace when the signal error occurred, - // that is not related to the signal error self, remove it to - // avoid misleading users and developers - int end_idx = for_signal ? 2 : 0; - for (int i = size - 1; i >= end_idx; --i) { - if (dladdr(call_stack[i], &info) && info.dli_sname) { - auto demangled = demangle(info.dli_sname); - std::string path(info.dli_fname); - // C++ traceback info are from core.so - if (path.substr(path.length() - 3).compare(".so") == 0) { - sout << paddle::string::Sprintf( - "%-3d %s\n", idx++, SimplifyDemangleStr(demangled)); - } - } - } - free(symbols); -#else - sout << "Not support stack backtrace yet.\n"; -#endif - return sout.str(); -} +std::string GetCurrentTraceBackString(bool for_signal = false); +std::string SimplifyErrorTypeFormat(const std::string& str); template -inline std::string GetErrorSumaryString(StrType&& what, +static std::string GetErrorSumaryString(StrType&& what, const char* file, int line) { std::ostringstream sout; @@ -264,7 +193,20 @@ inline std::string GetErrorSumaryString(StrType&& what, } template -inline std::string GetTraceBackString(StrType&& what, +std::string GetCompleteTraceBackString(StrType&& what, + const char* file, + int line) { + std::ostringstream sout; + sout << "\n----------------------\nError Message " + "Summary:\n----------------------\n"; + sout << paddle::string::Sprintf( + "%s (at %s:%d)", std::forward(what), file, line) + << std::endl; + return GetCurrentTraceBackString() + sout.str(); +} + +template +static std::string GetTraceBackString(StrType&& what, const char* file, int line) { if (FLAGS_call_stack_level > 1) { @@ -275,19 +217,6 @@ inline std::string GetTraceBackString(StrType&& what, } } -inline std::string SimplifyErrorTypeFormat(const std::string& str) { - std::ostringstream sout; - size_t type_end_pos = str.find(":", 0); - if (type_end_pos == std::string::npos) { - sout << str; - } else { - // Remove "Error:", add "()"" - sout << "(" << str.substr(0, type_end_pos - 5) << ")" - << str.substr(type_end_pos + 1); - } - return sout.str(); -} - inline bool is_error(bool stat) { return !stat; } // Note: This Macro can only be used within enforce.h @@ -348,6 +277,8 @@ struct EnforceNotMet : public std::exception { } } + ~EnforceNotMet() override = default; + private: // Used to determine the final type of exception thrown phi::ErrorCode code_ = phi::ErrorCode::LEGACY; diff --git a/paddle/phi/core/kernel_factory.cc b/paddle/phi/core/kernel_factory.cc index 08329d0c8636a93797181241a5e931622dcaff8b..d479147f06ba186a04d985fc3e974837fbc55fbd 100644 --- a/paddle/phi/core/kernel_factory.cc +++ b/paddle/phi/core/kernel_factory.cc @@ -14,7 +14,8 @@ #include "paddle/phi/core/kernel_factory.h" -// See Note [ Why still include the fluid headers? ] +#include "glog/logging.h" + #include "paddle/phi/core/enforce.h" namespace phi { diff --git a/paddle/phi/core/utils/type_registry.h b/paddle/phi/core/utils/type_registry.h index 5b64dbd01643e60c8eca9077dacd1631f55034c5..c233e1f743b213c50a8f49ee6b5f4f48d1826feb 100644 --- a/paddle/phi/core/utils/type_registry.h +++ b/paddle/phi/core/utils/type_registry.h @@ -15,6 +15,7 @@ limitations under the License. */ #pragma once #include +#include #include #include