diff --git a/paddle/fluid/framework/type_defs.h b/paddle/fluid/framework/type_defs.h index 61859264441ab..0937d96ad4c20 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 679612fdf1ae3..df3772b826da1 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 4a0b99518a63f..3def103ae9aa5 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 79b6e057d21fc..08f3c8d4a0fc2 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 003d6988671b5..d51ec52a49e69 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 1da0831fc6323..83acedc69bb60 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 1b9fbdd9defc7..5a754f42fb63c 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 14fe90192e5bc..b72c6efd51f2c 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 39eef27b4a607..18d51687ef121 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 e5d34376834dd..e8c264b884fe3 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 dcf1826012c13..41f654bfc8f30 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 bd19d403c9406..8eb6524e79c0f 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 c9d7d5bb54b62..0000000000000 --- 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 48778bb38e548..356dd1482c9d8 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 c5ac3b7815bf9..ae6b0135b3222 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 2c41c62353296..8da2623bb2c2d 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 08329d0c8636a..d479147f06ba1 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 5b64dbd01643e..c233e1f743b21 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