Skip to content

Commit 1e961b1

Browse files
authored
Merge pull request #12591 from chenwhql/enforce_msg_polish
polish high frequency enforce error message
2 parents d7873e1 + da39d84 commit 1e961b1

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

paddle/fluid/platform/enforce.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,8 @@ inline void throw_on_error(T e) {
263263
* PADDLE_ENFORCE_EQ(a, b);
264264
*
265265
* will raise an expression described as follows:
266-
* "enforce a == b failed, 1 != 2" with detailed stack information.
266+
* "Enforce failed. Expected input a == b, but received a(1) != b(2)."
267+
* with detailed stack information.
267268
*
268269
* extra messages is also supported, for example:
269270
* PADDLE_ENFORCE(a, b, "some simple enforce failed between %d numbers", 2)
@@ -292,9 +293,10 @@ inline void throw_on_error(T e) {
292293
#define __PADDLE_BINARY_COMPARE(__VAL0, __VAL1, __CMP, __INV_CMP, ...) \
293294
do { \
294295
if (UNLIKELY(!((__VAL0)__CMP(__VAL1)))) { \
295-
PADDLE_THROW("enforce %s " #__CMP " %s failed, %s " #__INV_CMP \
296-
" %s\n%s", \
297-
#__VAL0, #__VAL1, paddle::string::to_string(__VAL0), \
296+
PADDLE_THROW("Enforce failed. Expected %s " #__CMP \
297+
" %s, but received %s:%s " #__INV_CMP " %s:%s.\n%s", \
298+
#__VAL0, #__VAL1, #__VAL0, \
299+
paddle::string::to_string(__VAL0), #__VAL1, \
298300
paddle::string::to_string(__VAL1), \
299301
paddle::string::Sprintf("" __VA_ARGS__)); \
300302
} \

paddle/fluid/platform/enforce_test.cc

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ TEST(ENFORCE_EQ, NO_EXTRA_MSG_FAIL) {
5454
PADDLE_ENFORCE_EQ(a, 1 + 3);
5555
} catch (paddle::platform::EnforceNotMet error) {
5656
caught_exception = true;
57-
HasPrefix(StringPiece(error.what()), "enforce a == 1 + 3 failed, 2 != 4");
57+
HasPrefix(
58+
StringPiece(error.what()),
59+
"Enforce failed. Expected a == 1 + 3, but received a:2 != 1 + 3:4.");
5860
}
5961
EXPECT_TRUE(caught_exception);
6062
}
@@ -67,7 +69,8 @@ TEST(ENFORCE_EQ, EXTRA_MSG_FAIL) {
6769
} catch (paddle::platform::EnforceNotMet error) {
6870
caught_exception = true;
6971
HasPrefix(StringPiece(error.what()),
70-
"enforce a == 1 + 3 failed, 2 != 4\ntheir size not match");
72+
"Enforce failed. Expected a == 1 + 3, but received a:2 != 1 + "
73+
"3:4.\ntheir size not match");
7174
}
7275
EXPECT_TRUE(caught_exception);
7376
}
@@ -84,8 +87,9 @@ TEST(ENFORCE_NE, FAIL) {
8487
PADDLE_ENFORCE_NE(1.0, 1UL);
8588
} catch (paddle::platform::EnforceNotMet error) {
8689
caught_exception = true;
87-
EXPECT_TRUE(HasPrefix(StringPiece(error.what()),
88-
"enforce 1.0 != 1UL failed, 1 == 1"))
90+
EXPECT_TRUE(HasPrefix(
91+
StringPiece(error.what()),
92+
"Enforce failed. Expected 1.0 != 1UL, but received 1.0:1 == 1UL:1."))
8993
<< error.what() << " does not have expected prefix";
9094
}
9195
EXPECT_TRUE(caught_exception);
@@ -98,8 +102,9 @@ TEST(ENFORCE_GT, FAIL) {
98102
PADDLE_ENFORCE_GT(1, 2UL);
99103
} catch (paddle::platform::EnforceNotMet error) {
100104
caught_exception = true;
101-
EXPECT_TRUE(
102-
HasPrefix(StringPiece(error.what()), "enforce 1 > 2UL failed, 1 <= 2"));
105+
EXPECT_TRUE(HasPrefix(
106+
StringPiece(error.what()),
107+
"Enforce failed. Expected 1 > 2UL, but received 1:1 <= 2UL:2."));
103108
}
104109
EXPECT_TRUE(caught_exception);
105110
}
@@ -116,8 +121,9 @@ TEST(ENFORCE_GE, FAIL) {
116121
PADDLE_ENFORCE_GE(1, 2UL);
117122
} catch (paddle::platform::EnforceNotMet error) {
118123
caught_exception = true;
119-
EXPECT_TRUE(
120-
HasPrefix(StringPiece(error.what()), "enforce 1 >= 2UL failed, 1 < 2"));
124+
EXPECT_TRUE(HasPrefix(
125+
StringPiece(error.what()),
126+
"Enforce failed. Expected 1 >= 2UL, but received 1:1 < 2UL:2."));
121127
}
122128
EXPECT_TRUE(caught_exception);
123129
}
@@ -135,8 +141,9 @@ TEST(ENFORCE_LE, FAIL) {
135141
PADDLE_ENFORCE_GT(1, 2UL);
136142
} catch (paddle::platform::EnforceNotMet error) {
137143
caught_exception = true;
138-
EXPECT_TRUE(
139-
HasPrefix(StringPiece(error.what()), "enforce 1 > 2UL failed, 1 <= 2"));
144+
EXPECT_TRUE(HasPrefix(
145+
StringPiece(error.what()),
146+
"Enforce failed. Expected 1 > 2UL, but received 1:1 <= 2UL:2."));
140147
}
141148
EXPECT_TRUE(caught_exception);
142149
}
@@ -153,7 +160,8 @@ TEST(ENFORCE_LT, FAIL) {
153160
} catch (paddle::platform::EnforceNotMet error) {
154161
caught_exception = true;
155162
EXPECT_TRUE(HasPrefix(StringPiece(error.what()),
156-
"enforce 1UL < 0.12 failed, 1 >= 0.12"));
163+
"Enforce failed. Expected 1UL < 0.12, but "
164+
"received 1UL:1 >= 0.12:0.12."));
157165
}
158166
EXPECT_TRUE(caught_exception);
159167
}

paddle/fluid/platform/gpu_info.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ size_t GpuMaxChunkSize() {
116116
size_t allocating = static_cast<size_t>(FLAGS_fraction_of_gpu_memory_to_use *
117117
(total - reserving));
118118

119-
PADDLE_ENFORCE_LE(allocating, available);
119+
PADDLE_ENFORCE_LE(allocating, available,
120+
"Insufficient GPU memory to allocation.");
120121

121122
return allocating;
122123
}

0 commit comments

Comments
 (0)