Skip to content

Commit 5493920

Browse files
committed
[PIR] remove log simply na me mechnism from phi to common.
1 parent c0d6d7d commit 5493920

File tree

12 files changed

+197
-209
lines changed

12 files changed

+197
-209
lines changed

paddle/common/enforce.cc

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/* Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#include "paddle/common/enforce.h"
16+
17+
#include <map>
18+
#include <string>
19+
#include <vector>
20+
21+
REGISTER_LOG_SIMPLY_STR(std::string, std::string);
22+
23+
namespace {
24+
class StrSizeCmp {
25+
public:
26+
bool operator()(const std::string& lhs, const std::string& rhs) const {
27+
return lhs.size() > rhs.size();
28+
}
29+
};
30+
31+
using LogSimplyStrMap = std::map<std::string, std::string, StrSizeCmp>;
32+
33+
LogSimplyStrMap& GetLogStrSimplyMap() {
34+
static LogSimplyStrMap str_simply_map;
35+
return str_simply_map;
36+
}
37+
38+
std::string SimplifyDemangleStr(std::string str) {
39+
auto& str_map = GetLogStrSimplyMap();
40+
for (auto& value : str_map) {
41+
size_t start_pos = 0;
42+
while ((start_pos = str.find(value.first, start_pos)) !=
43+
std::string::npos) {
44+
str.replace(start_pos, value.first.length(), value.second);
45+
start_pos += value.second.length();
46+
}
47+
}
48+
return str;
49+
}
50+
} // namespace
51+
52+
namespace common {
53+
namespace enforce {
54+
55+
bool RegisterLogSimplyStr(const std::string& type_name,
56+
const std::string& simply_name) {
57+
return GetLogStrSimplyMap()
58+
.emplace(std::make_pair(type_name, simply_name))
59+
.second;
60+
}
61+
62+
std::string GetCurrentTraceBackString(bool for_signal) {
63+
std::ostringstream sout;
64+
65+
if (!for_signal) {
66+
sout << "\n\n--------------------------------------\n";
67+
sout << "C++ Traceback (most recent call last):";
68+
sout << "\n--------------------------------------\n";
69+
}
70+
#if !defined(_WIN32) && !defined(PADDLE_WITH_MUSL)
71+
static constexpr int TRACE_STACK_LIMIT = 100;
72+
73+
std::array<void*, TRACE_STACK_LIMIT> call_stack;
74+
auto size = backtrace(call_stack.data(), TRACE_STACK_LIMIT);
75+
auto symbols = backtrace_symbols(call_stack.data(), size);
76+
Dl_info info;
77+
int idx = 0;
78+
// `for_signal` used to remove the stack trace introduced by
79+
// obtaining the error stack trace when the signal error occurred,
80+
// that is not related to the signal error self, remove it to
81+
// avoid misleading users and developers
82+
int end_idx = for_signal ? 2 : 0;
83+
for (int i = size - 1; i >= end_idx; --i) {
84+
if (dladdr(call_stack[i], &info) && info.dli_sname) {
85+
auto demangled = common::demangle(info.dli_sname);
86+
std::string path(info.dli_fname);
87+
// C++ traceback info are from core.so
88+
if (path.substr(path.length() - 3).compare(".so") == 0) {
89+
sout << paddle::string::Sprintf(
90+
"%-3d %s\n", idx++, SimplifyDemangleStr(demangled));
91+
}
92+
}
93+
}
94+
free(symbols); // NOLINT
95+
#else
96+
sout << "Not support stack backtrace yet.\n";
97+
#endif
98+
return sout.str();
99+
}
100+
101+
} // namespace enforce
102+
} // namespace common

paddle/common/enforce.h

Lines changed: 27 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include "paddle/common/errors.h"
3333
#include "paddle/common/macros.h"
34+
#include "paddle/utils/test_macros.h"
3435

3536
#if !defined(_WIN32) && !defined(PADDLE_WITH_MUSL)
3637
#include <execinfo.h>
@@ -40,10 +41,20 @@
4041
#define GLOG_NO_ABBREVIATED_SEVERITIES
4142
#include "paddle/utils/string/printf.h"
4243
#include "paddle/utils/string/to_string.h"
43-
#include "paddle/utils/test_macros.h"
4444
#include "paddle/utils/variant.h"
4545

4646
namespace common {
47+
#ifdef __GNUC__
48+
inline std::string demangle(std::string name) {
49+
int status = -4; // some arbitrary value to eliminate the compiler warning
50+
std::unique_ptr<char, void (*)(void*)> res{
51+
abi::__cxa_demangle(name.c_str(), NULL, NULL, &status), std::free};
52+
return (status == 0) ? res.get() : name;
53+
}
54+
#else
55+
inline std::string demangle(std::string name) { return name; }
56+
#endif
57+
4758
class CommonNotMetException : public std::exception {
4859
public:
4960
explicit CommonNotMetException(const std::string& str) : err_str_(str) {}
@@ -53,9 +64,7 @@ class CommonNotMetException : public std::exception {
5364
private:
5465
std::string err_str_;
5566
};
56-
} // namespace common
5767

58-
namespace common {
5968
namespace enforce {
6069

6170
/** HELPER MACROS AND FUNCTIONS **/
@@ -161,6 +170,20 @@ using CommonType2 = typename std::add_lvalue_reference<
161170
#define COMMON_ENFORCE_LE(__VAL0, __VAL1, ...) \
162171
__COMMON_BINARY_COMPARE(__VAL0, __VAL1, <=, >, __VA_ARGS__)
163172

173+
TEST_API bool RegisterLogSimplyStr(const std::string& type,
174+
const std::string& simply);
175+
TEST_API std::string GetCurrentTraceBackString(bool for_signal = false);
176+
template <typename T>
177+
class LogSimplyStrRegistrar {
178+
public:
179+
static bool success;
180+
};
181+
182+
#define REGISTER_LOG_SIMPLY_STR(Type, simply_name) \
183+
template <> \
184+
bool ::common::enforce::LogSimplyStrRegistrar<Type>::success = \
185+
::common::enforce::RegisterLogSimplyStr( \
186+
::common::demangle(typeid(Type).name()), #simply_name);
164187
} // namespace enforce
165188
} // namespace common
166189

@@ -172,53 +195,10 @@ inline bool is_error(const T& stat) {
172195
}
173196

174197
namespace pir {
175-
176-
#ifdef __GNUC__
177-
inline std::string demangle(std::string name) {
178-
int status = -4; // some arbitrary value to eliminate the compiler warning
179-
std::unique_ptr<char, void (*)(void*)> res{
180-
abi::__cxa_demangle(name.c_str(), NULL, NULL, &status), std::free};
181-
return (status == 0) ? res.get() : name;
182-
}
183-
#else
184-
inline std::string demangle(std::string name) { return name; }
185-
#endif
186-
187-
static std::string GetCurrentTraceBackString() {
188-
std::ostringstream sout;
189-
sout << "\n\n--------------------------------------\n";
190-
sout << "C++ Traceback (most recent call last):";
191-
sout << "\n--------------------------------------\n";
192-
#if !defined(_WIN32) && !defined(PADDLE_WITH_MUSL)
193-
static constexpr int TRACE_STACK_LIMIT = 100;
194-
195-
void* call_stack[TRACE_STACK_LIMIT];
196-
auto size = backtrace(call_stack, TRACE_STACK_LIMIT);
197-
auto symbols = backtrace_symbols(call_stack, size);
198-
Dl_info info;
199-
int idx = 0;
200-
int end_idx = 0;
201-
for (int i = size - 1; i >= end_idx; --i) {
202-
if (dladdr(call_stack[i], &info) && info.dli_sname) {
203-
auto demangled = demangle(info.dli_sname);
204-
std::string path(info.dli_fname);
205-
// C++ traceback info are from core.so
206-
if (path.substr(path.length() - 3).compare(".so") == 0) {
207-
sout << idx++ << " " << demangled << "\n";
208-
}
209-
}
210-
}
211-
free(symbols);
212-
#else
213-
sout << "Not support stack backtrace yet.\n";
214-
#endif
215-
return sout.str();
216-
}
217-
218198
class IrNotMetException : public std::exception {
219199
public:
220200
explicit IrNotMetException(const std::string& str)
221-
: err_str_(str + GetCurrentTraceBackString()) {}
201+
: err_str_(str + ::common::enforce::GetCurrentTraceBackString()) {}
222202

223203
const char* what() const noexcept override { return err_str_.c_str(); }
224204

paddle/fluid/framework/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ cc_library(
947947
imperative_flag
948948
layer)
949949

950-
cc_library(type_info SRCS type_info.cc)
950+
cc_library(type_info SRCS type_info.cc type_defs.cc)
951951
target_link_libraries(type_info pir op_dialect)
952952
add_dependencies(type_info framework_proto auto_parallel_proto xxhash)
953953
if(WITH_MKLDNN)

paddle/fluid/framework/type_defs.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#include "paddle/fluid/framework/type_defs.h"
16+
17+
#include "paddle/common/enforce.h"
18+
19+
REGISTER_LOG_SIMPLY_STR(paddle::framework::AttributeMap,
20+
paddle::framework::AttributeMap);
21+
REGISTER_LOG_SIMPLY_STR(paddle::framework::Attribute,
22+
paddle::framework::Attribute);

paddle/fluid/imperative/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ cc_library(
44
DEPS phi common)
55
cc_library(
66
var_helper
7-
SRCS var_helper.cc
7+
SRCS var_helper.cc type_defs.cc
88
DEPS tensor phi common)
99
if(WITH_XPU)
1010
cc_library(

paddle/fluid/imperative/type_defs.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License. */
14+
15+
#include "paddle/fluid/imperative/type_defs.h"
16+
17+
#include "paddle/common/enforce.h"
18+
19+
REGISTER_LOG_SIMPLY_STR(paddle::imperative::NameTensorMap,
20+
paddle::imperative::NameTensorMap);
21+
REGISTER_LOG_SIMPLY_STR(paddle::imperative::NameVarBaseMap,
22+
paddle::imperative::NameVarBaseMap);
23+
REGISTER_LOG_SIMPLY_STR(paddle::imperative::NameVariableWrapperMap,
24+
paddle::imperative::NameVariableWrapperMap);

paddle/fluid/memory/stats.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ class Stat : public StatBase {
7777
thread_local_stat->current += increment;
7878

7979
VLOG(8) << string::split_string(
80-
phi::enforce::demangle(typeid(*thread_local_stat).name()),
81-
"::")
80+
common::demangle(typeid(*thread_local_stat).name()), "::")
8281
.back()
8382
<< ": Update current_value with " << increment
8483
<< ", after update, current value = " << GetCurrentValue();
@@ -91,8 +90,7 @@ class Stat : public StatBase {
9190
!peak_value_.compare_exchange_weak(prev_value, current_value)) {
9291
}
9392
VLOG(8) << string::split_string(
94-
phi::enforce::demangle(typeid(*thread_local_stat).name()),
95-
"::")
93+
common::demangle(typeid(*thread_local_stat).name()), "::")
9694
.back()
9795
<< ": Update current_value with " << increment
9896
<< ", after update, peak_value = " << peak_value_.load()

paddle/fluid/platform/enforce.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ PHI_DECLARE_int32(call_stack_level);
108108
namespace paddle {
109109
namespace platform {
110110
using namespace ::phi::enforce; // NOLINT
111+
using ::common::demangle;
111112

112113
/** HELPER MACROS AND FUNCTIONS **/
113114

paddle/fluid/platform/init.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ limitations under the License. */
5151
#include "paddle/fluid/platform/device/ipu/ipu_info.h"
5252
#endif
5353

54+
#include "paddle/common/enforce.h"
5455
#include "paddle/fluid/memory/allocation/allocator_facade.h"
5556
#include "paddle/fluid/memory/memory.h"
5657
#include "paddle/fluid/platform/flags.h"
@@ -310,7 +311,8 @@ void SignalHandle(const char *data, int size) {
310311
sout << "\n\n--------------------------------------\n";
311312
sout << "C++ Traceback (most recent call last):";
312313
sout << "\n--------------------------------------\n";
313-
auto traceback = platform::GetCurrentTraceBackString(/*for_signal=*/true);
314+
auto traceback =
315+
::common::enforce::GetCurrentTraceBackString(/*for_signal=*/true);
314316
if (traceback.empty()) {
315317
sout
316318
<< "No stack trace in paddle, may be caused by external reasons.\n";

0 commit comments

Comments
 (0)