Skip to content

Commit 05c9642

Browse files
authored
Update paddle enforce message (#24498)
* test=develop error message update
1 parent 9f83f0f commit 05c9642

19 files changed

+534
-281
lines changed

paddle/fluid/operators/bilinear_tensor_product_op.cc

Lines changed: 62 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,39 +28,61 @@ class BilinearTensorProductOp : public framework::OperatorWithKernel {
2828

2929
protected:
3030
void InferShape(framework::InferShapeContext* ctx) const override {
31-
PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should not be null.");
32-
PADDLE_ENFORCE(ctx->HasInput("Y"), "Input(Y) should not be null.");
33-
PADDLE_ENFORCE(ctx->HasInput("Weight"),
34-
"Input(Weight) should not be null.");
35-
PADDLE_ENFORCE(ctx->HasOutput("Out"), "Output(Out) should not be null.");
31+
PADDLE_ENFORCE_EQ(
32+
ctx->HasInput("X"), true,
33+
platform::errors::InvalidArgument("Input(X) should not be null."));
34+
PADDLE_ENFORCE_EQ(
35+
ctx->HasInput("Y"), true,
36+
platform::errors::InvalidArgument("Input(Y) should not be null."));
37+
PADDLE_ENFORCE_EQ(
38+
ctx->HasInput("Weight"), true,
39+
platform::errors::InvalidArgument("Input(Weight) should not be null."));
40+
PADDLE_ENFORCE_EQ(
41+
ctx->HasOutput("Out"), true,
42+
platform::errors::InvalidArgument("Output(Out) should not be null."));
3643
auto x_dims = ctx->GetInputDim("X");
3744
auto y_dims = ctx->GetInputDim("Y");
3845
auto weight_dims = ctx->GetInputDim("Weight");
3946

40-
PADDLE_ENFORCE_EQ(x_dims.size(), 2UL, "The input(X) must be a 2D Tensor.");
41-
PADDLE_ENFORCE_EQ(y_dims.size(), 2UL, "The input(Y) must be a 2D Tensor.");
47+
PADDLE_ENFORCE_EQ(
48+
x_dims.size(), 2UL,
49+
platform::errors::InvalidArgument("The input(X) must be a 2D Tensor."));
50+
PADDLE_ENFORCE_EQ(
51+
y_dims.size(), 2UL,
52+
platform::errors::InvalidArgument("The input(Y) must be a 2D Tensor."));
4253
PADDLE_ENFORCE_EQ(weight_dims.size(), 3UL,
43-
"The input(Weight) must be a 3D tensor.");
54+
platform::errors::InvalidArgument(
55+
"The input(Weight) must be a 3D tensor."));
4456
if (ctx->IsRuntime() || (x_dims[0] > 0 && y_dims[0] > 0)) {
45-
PADDLE_ENFORCE_EQ(x_dims[0], y_dims[0],
46-
"The first dimension(batch_size) of input(X) must be "
47-
"equal to the first dimension of the input(Y).");
57+
PADDLE_ENFORCE_EQ(
58+
x_dims[0], y_dims[0],
59+
platform::errors::InvalidArgument(
60+
"The first dimension(batch_size) of input(X) must be "
61+
"equal to the first dimension of the input(Y)."));
4862
}
4963
PADDLE_ENFORCE_EQ(x_dims[1], weight_dims[1],
50-
"The second dimension of input(X) must be equal to "
51-
"the second dimension of the input(Weight).");
64+
platform::errors::InvalidArgument(
65+
"The second dimension of input(X) must be equal to "
66+
"the second dimension of the input(Weight)."));
5267
PADDLE_ENFORCE_EQ(y_dims[1], weight_dims[2],
53-
"The second dimension of input(Y) must be equal to "
54-
"the third dimension of the input(Weight).");
68+
platform::errors::InvalidArgument(
69+
"The second dimension of input(Y) must be equal to "
70+
"the third dimension of the input(Weight)."));
5571

5672
if (ctx->HasInput("Bias")) {
5773
auto bias_dims = ctx->GetInputDim("Bias");
58-
PADDLE_ENFORCE(bias_dims.size() == 2UL && bias_dims[0] == 1UL,
59-
"The Input(Bias) must be a 2-D tensor with "
60-
"the 2nd dimension fixed to 1 (a row vector).");
74+
PADDLE_ENFORCE_EQ(bias_dims.size(), 2UL,
75+
platform::errors::InvalidArgument(
76+
"The Input(Bias) must be a 2-D tensor with "
77+
"the 2nd dimension fixed to 1 (a row vector)."));
78+
PADDLE_ENFORCE_EQ(bias_dims[0], 1UL,
79+
platform::errors::InvalidArgument(
80+
"The Input(Bias) must be a 2-D tensor with "
81+
"the 2nd dimension fixed to 1 (a row vector)."));
6182
PADDLE_ENFORCE_EQ(bias_dims[1], weight_dims[0],
62-
"The second dimension of input(Bias) must be equal "
63-
"to the first dimension of the input(Weight).");
83+
platform::errors::InvalidArgument(
84+
"The second dimension of input(Bias) must be equal "
85+
"to the first dimension of the input(Weight)."));
6486
}
6587

6688
ctx->SetOutputDim("Out", {x_dims[0], weight_dims[0]});
@@ -104,27 +126,36 @@ class BilinearTensorProductOpGrad : public framework::OperatorWithKernel {
104126

105127
protected:
106128
void InferShape(framework::InferShapeContext* ctx) const override {
107-
PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should not be null.");
108-
PADDLE_ENFORCE(ctx->HasInput("Y"), "Input(Y) should not be null.");
109-
PADDLE_ENFORCE(ctx->HasInput("Weight"),
110-
"Input(Weight) should not be null.");
111-
PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")),
112-
"Input(Out@GRAD) should not be null.");
129+
PADDLE_ENFORCE_EQ(
130+
ctx->HasInput("X"), true,
131+
platform::errors::InvalidArgument("Input(X) should not be null."));
132+
PADDLE_ENFORCE_EQ(
133+
ctx->HasInput("Y"), true,
134+
platform::errors::InvalidArgument("Input(Y) should not be null."));
135+
PADDLE_ENFORCE_EQ(
136+
ctx->HasInput("Weight"), true,
137+
platform::errors::InvalidArgument("Input(Weight) should not be null."));
138+
PADDLE_ENFORCE_EQ(ctx->HasInput(framework::GradVarName("Out")), true,
139+
platform::errors::InvalidArgument(
140+
"Input(Out@GRAD) should not be null."));
113141
auto x_dims = ctx->GetInputDim("X");
114142
auto y_dims = ctx->GetInputDim("Y");
115143
auto weight_dims = ctx->GetInputDim("Weight");
116144
auto out_dims = ctx->GetInputDim(framework::GradVarName("Out"));
117145

118146
PADDLE_ENFORCE_EQ(out_dims.size(), 2UL,
119-
"The input(Out@GRAD) must be a 2D Tensor.");
147+
platform::errors::InvalidArgument(
148+
"The input(Out@GRAD) must be a 2D Tensor."));
120149
PADDLE_ENFORCE_EQ(
121150
x_dims[0], out_dims[0],
122-
"The first dimension(batch_size) of input(Out@GRAD) must be "
123-
"equal to the first dimension of the Input(X).");
151+
platform::errors::InvalidArgument(
152+
"The first dimension(batch_size) of input(Out@GRAD) must be "
153+
"equal to the first dimension of the Input(X)."));
124154
PADDLE_ENFORCE_EQ(
125155
weight_dims[0], out_dims[1],
126-
"The second dimension of input(Out@GRAD) must be equal to "
127-
"the third dimension of the Input(Weight).");
156+
platform::errors::InvalidArgument(
157+
"The second dimension of input(Out@GRAD) must be equal to "
158+
"the third dimension of the Input(Weight)."));
128159

129160
auto bias_grad_name = framework::GradVarName("Bias");
130161
if (ctx->HasOutput(bias_grad_name)) {

paddle/fluid/operators/detection/anchor_generator_op.cc

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,23 @@ class AnchorGeneratorOp : public framework::OperatorWithKernel {
2222
using framework::OperatorWithKernel::OperatorWithKernel;
2323

2424
void InferShape(framework::InferShapeContext* ctx) const override {
25-
PADDLE_ENFORCE(ctx->HasInput("Input"),
26-
"Input(Input) of AnchorGeneratorOp should not be null.");
27-
PADDLE_ENFORCE(ctx->HasOutput("Anchors"),
28-
"Output(Anchors) of AnchorGeneratorOp should not be null.");
29-
PADDLE_ENFORCE(
30-
ctx->HasOutput("Variances"),
31-
"Output(Variances) of AnchorGeneratorOp should not be null.");
25+
PADDLE_ENFORCE_EQ(
26+
ctx->HasInput("Input"), true,
27+
platform::errors::InvalidArgument(
28+
"Input(Input) of AnchorGeneratorOp should not be null."));
29+
PADDLE_ENFORCE_EQ(
30+
ctx->HasOutput("Anchors"), true,
31+
platform::errors::InvalidArgument(
32+
"Output(Anchors) of AnchorGeneratorOp should not be null."));
33+
PADDLE_ENFORCE_EQ(
34+
ctx->HasOutput("Variances"), true,
35+
platform::errors::InvalidArgument(
36+
"Output(Variances) of AnchorGeneratorOp should not be null."));
3237

3338
auto input_dims = ctx->GetInputDim("Input");
34-
PADDLE_ENFORCE(input_dims.size() == 4, "The layout of input is NCHW.");
39+
PADDLE_ENFORCE_EQ(
40+
input_dims.size(), 4,
41+
platform::errors::InvalidArgument("The layout of input is NCHW."));
3542

3643
auto anchor_sizes = ctx->Attrs().Get<std::vector<float>>("anchor_sizes");
3744
auto aspect_ratios = ctx->Attrs().Get<std::vector<float>>("aspect_ratios");
@@ -87,10 +94,12 @@ class AnchorGeneratorOpMaker : public framework::OpProtoAndCheckerMaker {
8794
"equals to 64**2.")
8895
.AddCustomChecker([](const std::vector<float>& anchor_sizes) {
8996
PADDLE_ENFORCE_GT(anchor_sizes.size(), 0UL,
90-
"Size of anchor_sizes must be at least 1.");
97+
platform::errors::InvalidArgument(
98+
"Size of anchor_sizes must be at least 1."));
9199
for (size_t i = 0; i < anchor_sizes.size(); ++i) {
92100
PADDLE_ENFORCE_GT(anchor_sizes[i], 0.0,
93-
"anchor_sizes[%d] must be positive.", i);
101+
platform::errors::InvalidArgument(
102+
"anchor_sizes[%d] must be positive.", i));
94103
}
95104
});
96105
AddAttr<std::vector<float>>(
@@ -105,10 +114,12 @@ class AnchorGeneratorOpMaker : public framework::OpProtoAndCheckerMaker {
105114
"in box regression deltas")
106115
.AddCustomChecker([](const std::vector<float>& variances) {
107116
PADDLE_ENFORCE_EQ(variances.size(), 4UL,
108-
"Must and only provide 4 variance.");
117+
platform::errors::InvalidArgument(
118+
"Must provide 4 variance only."));
109119
for (size_t i = 0; i < variances.size(); ++i) {
110120
PADDLE_ENFORCE_GT(variances[i], 0.0,
111-
"variance[%d] must be greater than 0.", i);
121+
platform::errors::InvalidArgument(
122+
"variance[%d] must be greater than 0.", i));
112123
}
113124
});
114125

@@ -119,10 +130,12 @@ class AnchorGeneratorOpMaker : public framework::OpProtoAndCheckerMaker {
119130
.AddCustomChecker([](const std::vector<float>& stride) {
120131
PADDLE_ENFORCE_EQ(
121132
stride.size(), 2UL,
122-
"Must and only provide 2 stride for width and height.");
133+
platform::errors::InvalidArgument(
134+
"Must provide 2 stride for width and height only."));
123135
for (size_t i = 0; i < stride.size(); ++i) {
124136
PADDLE_ENFORCE_GT(stride[i], 0.0,
125-
"stride[%d] should be larger than 0.", i);
137+
platform::errors::InvalidArgument(
138+
"stride[%d] should be larger than 0.", i));
126139
}
127140
});
128141

paddle/fluid/operators/detection/bipartite_match_op.cc

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,23 @@ class BipartiteMatchOp : public framework::OperatorWithKernel {
2626
using framework::OperatorWithKernel::OperatorWithKernel;
2727

2828
void InferShape(framework::InferShapeContext* ctx) const override {
29-
PADDLE_ENFORCE(ctx->HasInput("DistMat"),
30-
"Input(DistMat) of BipartiteMatch should not be null.");
31-
PADDLE_ENFORCE(
32-
ctx->HasOutput("ColToRowMatchIndices"),
33-
"Output(ColToRowMatchIndices) of BipartiteMatch should not be null.");
34-
PADDLE_ENFORCE(
35-
ctx->HasOutput("ColToRowMatchDist"),
36-
"Output(ColToRowMatchDist) of BipartiteMatch should not be null.");
29+
PADDLE_ENFORCE_EQ(
30+
ctx->HasInput("DistMat"), true,
31+
platform::errors::InvalidArgument(
32+
"Input(DistMat) of BipartiteMatch should not be null."));
33+
PADDLE_ENFORCE_EQ(ctx->HasOutput("ColToRowMatchIndices"), true,
34+
platform::errors::InvalidArgument(
35+
"Output(ColToRowMatchIndices) of BipartiteMatch "
36+
"should not be null."));
37+
PADDLE_ENFORCE_EQ(
38+
ctx->HasOutput("ColToRowMatchDist"), true,
39+
platform::errors::InvalidArgument(
40+
"Output(ColToRowMatchDist) of BipartiteMatch should not be null."));
3741

3842
auto dims = ctx->GetInputDim("DistMat");
39-
PADDLE_ENFORCE_EQ(dims.size(), 2, "The rank of Input(DistMat) must be 2.");
43+
PADDLE_ENFORCE_EQ(dims.size(), 2,
44+
platform::errors::InvalidArgument(
45+
"The rank of Input(DistMat) must be 2."));
4046

4147
ctx->SetOutputDim("ColToRowMatchIndices", dims);
4248
ctx->SetOutputDim("ColToRowMatchDist", dims);
@@ -64,7 +70,9 @@ class BipartiteMatchKernel : public framework::OpKernel<T> {
6470
// The match_dist must be initialized to 0 at first.
6571
void BipartiteMatch(const Tensor& dist, int* match_indices,
6672
T* match_dist) const {
67-
PADDLE_ENFORCE_EQ(dist.dims().size(), 2, "The rank of dist must be 2.");
73+
PADDLE_ENFORCE_EQ(
74+
dist.dims().size(), 2,
75+
platform::errors::InvalidArgument("The rank of dist must be 2."));
6876
int64_t row = dist.dims()[0];
6977
int64_t col = dist.dims()[1];
7078
auto* dist_data = dist.data<T>();
@@ -127,7 +135,11 @@ class BipartiteMatchKernel : public framework::OpKernel<T> {
127135
// Cannot find good match.
128136
break;
129137
} else {
130-
PADDLE_ENFORCE_EQ(match_indices[max_idx], -1);
138+
PADDLE_ENFORCE_EQ(
139+
match_indices[max_idx], -1,
140+
platform::errors::InvalidArgument(
141+
"The match_indices must be initialized to -1 at [%d].",
142+
max_idx));
131143
match_indices[max_idx] = max_row_idx;
132144
match_dist[max_idx] = max_dist;
133145
// Erase the row index.
@@ -163,7 +175,10 @@ class BipartiteMatchKernel : public framework::OpKernel<T> {
163175
}
164176
}
165177
if (max_row_idx != -1) {
166-
PADDLE_ENFORCE_EQ(match_indices[j], -1);
178+
PADDLE_ENFORCE_EQ(
179+
match_indices[j], -1,
180+
platform::errors::InvalidArgument(
181+
"The match_indices must be initialized to -1 at [%d].", j));
167182
match_indices[j] = max_row_idx;
168183
match_dist[j] = max_dist;
169184
}
@@ -183,8 +198,9 @@ class BipartiteMatchKernel : public framework::OpKernel<T> {
183198
? 1
184199
: static_cast<int64_t>(dist_mat->lod().back().size() - 1);
185200
if (dist_mat->lod().size()) {
186-
PADDLE_ENFORCE_EQ(dist_mat->lod().size(), 1UL,
187-
"Only support 1 level of LoD.");
201+
PADDLE_ENFORCE_EQ(
202+
dist_mat->lod().size(), 1UL,
203+
platform::errors::InvalidArgument("Only support 1 level of LoD."));
188204
}
189205
match_indices->mutable_data<int>({n, col}, context.GetPlace());
190206
match_dist->mutable_data<T>({n, col}, context.GetPlace());

0 commit comments

Comments
 (0)