Skip to content

Commit a9520db

Browse files
author
Yibing Liu
authored
Format error message for ops (#24482)
* Format error message for ops, test=develop * Fix check in sequence_expand, test=develop
1 parent 2644cb8 commit a9520db

File tree

13 files changed

+361
-185
lines changed

13 files changed

+361
-185
lines changed

paddle/fluid/operators/crf_decoding_op.cc

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -89,41 +89,57 @@ class CRFDecodingOp : public framework::OperatorWithKernel {
8989
using framework::OperatorWithKernel::OperatorWithKernel;
9090

9191
void InferShape(framework::InferShapeContext* ctx) const override {
92-
PADDLE_ENFORCE_EQ(ctx->HasInput("Emission"), true,
93-
"Input(Emission) should be not null.");
94-
PADDLE_ENFORCE_EQ(ctx->HasInput("Transition"), true,
95-
"Input(Transition) should be not null.");
96-
97-
PADDLE_ENFORCE_EQ(ctx->HasOutput("ViterbiPath"), true,
98-
"Output(ViterbiPath) should be not null.");
92+
OP_INOUT_CHECK(ctx->HasInput("Emission"), "Input", "Emission",
93+
"CRFDecoding");
94+
OP_INOUT_CHECK(ctx->HasInput("Transition"), "Input", "Transition",
95+
"CRFDecoding");
96+
OP_INOUT_CHECK(ctx->HasOutput("ViterbiPath"), "Output", "ViterbiPath",
97+
"CRFDecoding");
9998

10099
auto emission_dims = ctx->GetInputDim("Emission");
101100
bool has_length = ctx->HasInput("Length");
102101

103102
if (has_length) {
104103
PADDLE_ENFORCE_EQ(emission_dims.size(), 3,
105-
"The Input(Emission) should be a 3-D tensor.");
104+
platform::errors::InvalidArgument(
105+
"The Input(Emission) should be a 3-D tensor. But "
106+
"received: input rank %u, input shape [%s]. ",
107+
emission_dims.size(), emission_dims));
106108
} else {
107109
PADDLE_ENFORCE_EQ(emission_dims.size(), 2,
108-
"The Input(Emission) should be a 2-D tensor.");
110+
platform::errors::InvalidArgument(
111+
"The Input(Emission) should be a 2-D tensor. But "
112+
"received: input rank %u, input shape [%s].",
113+
emission_dims.size(), emission_dims));
109114
}
110-
PADDLE_ENFORCE_NE(emission_dims[0], 0,
111-
"An empty mini-batch is not allowed.");
112115

113116
auto transition_dims = ctx->GetInputDim("Transition");
114117
PADDLE_ENFORCE_EQ(transition_dims.size(), 2UL,
115-
"The Input(Transition) should be a 2-D tensor.");
118+
platform::errors::InvalidArgument(
119+
"The Input(Transition) should be a 2-D tensor. But "
120+
"received: input rank %u, input shape [%s].",
121+
transition_dims.size(), transition_dims));
116122
PADDLE_ENFORCE_EQ(
117123
transition_dims[0] - 2, transition_dims[1],
118-
"An invalid dimension for the Input(Transition), which should "
119-
"be a 2-D tensor with shape [(D + 2) x D].");
124+
platform::errors::InvalidArgument(
125+
"An invalid dimension for the Input(Transition), which should "
126+
"be a 2-D tensor with shape [(D + 2) x D]. But received: input "
127+
"rank %u, "
128+
"input shape [%s].",
129+
transition_dims.size(), transition_dims));
120130
if (ctx->IsRuntime() || (emission_dims[emission_dims.size() - 1] > 0 &&
121131
transition_dims[transition_dims.size() - 1] > 0)) {
122-
PADDLE_ENFORCE_EQ(
123-
emission_dims[emission_dims.size() - 1],
124-
transition_dims[transition_dims.size() - 1],
125-
"The last dimension of the Input(Emission) and the Input(Transition) "
126-
"should be equal to the tag number.");
132+
PADDLE_ENFORCE_EQ(emission_dims[emission_dims.size() - 1],
133+
transition_dims[transition_dims.size() - 1],
134+
platform::errors::InvalidArgument(
135+
"The last dimension of the Input(Emission) and the "
136+
"Input(Transition) "
137+
"should be equal to the tag number. But received "
138+
"Input(Emission): rank "
139+
"%u, shape [%s]; received Input(Transition): rank "
140+
"%u, shape [%s].",
141+
emission_dims.size(), emission_dims,
142+
transition_dims.size(), transition_dims));
127143
}
128144
if (ctx->HasInput("Label")) {
129145
auto label_dims = ctx->GetInputDim("Label");
@@ -132,20 +148,31 @@ class CRFDecodingOp : public framework::OperatorWithKernel {
132148
(label_dims.size() == 3UL && label_dims[2] == 1) ||
133149
label_dims.size() == 2UL,
134150
true,
135-
"The Input(Label) should be a 3-D tensor with last dimension "
136-
"fixed to 1 or a 2-D tensor in padding mode.");
151+
platform::errors::InvalidArgument(
152+
"The Input(Label) should be a 3-D tensor with last dimension "
153+
"fixed to 1 or a 2-D tensor in padding mode. But received: "
154+
"input "
155+
"rank %u, input shape [%s].",
156+
label_dims.size(), label_dims));
137157
} else {
138-
PADDLE_ENFORCE_EQ((label_dims.size() == 2UL && label_dims[1] == 1) ||
139-
label_dims.size() == 1UL,
140-
true,
141-
"The Input(Label) should be a 2-D tensor with last "
142-
"dimension fixed to 1 or a 1-D tensor.");
158+
PADDLE_ENFORCE_EQ(
159+
(label_dims.size() == 2UL && label_dims[1] == 1) ||
160+
label_dims.size() == 1UL,
161+
true, platform::errors::InvalidArgument(
162+
"The Input(Label) should be a 2-D tensor with last "
163+
"dimension fixed to 1 or a 1-D tensor. But received: "
164+
"input rank %u, input shape [%s].",
165+
label_dims.size(), label_dims));
143166
}
144167
if (ctx->IsRuntime() || (emission_dims[0] > 0 && label_dims[0] > 0)) {
145168
PADDLE_ENFORCE_EQ(
146169
emission_dims[0], label_dims[0],
147-
"The first dimension of Input(Emission) and Input(Label) "
148-
"should be the same.");
170+
platform::errors::InvalidArgument(
171+
"The first dimension of Input(Emission) and Input(Label) "
172+
"should be the same. But received Input(Emission): rank %u, "
173+
"shape [%s]; received Input(Label): rank %u, shape [%s].",
174+
emission_dims.size(), emission_dims, label_dims.size(),
175+
label_dims));
149176
}
150177
}
151178

paddle/fluid/operators/crf_decoding_op.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,16 @@ class CRFDecodingOpKernel : public framework::OpKernel<T> {
7676
}
7777
} else {
7878
PADDLE_ENFORCE_EQ(emission_weights->NumLevels(), 1UL,
79-
"The Input(Emission) should be a sequence.");
79+
platform::errors::InvalidArgument(
80+
"The Input(Emission) should be a sequence with lod "
81+
"level 1. But received: lod level %u.",
82+
emission_weights->NumLevels()));
8083
auto lod = emission_weights->lod();
81-
PADDLE_ENFORCE_GT(lod.size(), 0, "Input(Emission) must be a sequence.");
84+
PADDLE_ENFORCE_GT(
85+
lod.size(), 0,
86+
platform::errors::InvalidArgument(
87+
"Input(Emission) must be a sequence. But received: lod level %u.",
88+
lod.size()));
8289
const size_t level = 0;
8390
const size_t seq_num = lod[level].size() - 1;
8491

@@ -92,7 +99,10 @@ class CRFDecodingOpKernel : public framework::OpKernel<T> {
9299
}
93100
if (label) {
94101
PADDLE_ENFORCE_EQ(label->NumLevels(), 1UL,
95-
"The Input(Label) should be a sequence.");
102+
platform::errors::InvalidArgument(
103+
"The Input(label) should be a sequence with lod "
104+
"level 1. But received: lod level %u.",
105+
label->NumLevels()));
96106
const int64_t* label_value = label->data<int64_t>();
97107
size_t numel = label->numel();
98108
for (size_t i = 0; i < numel; ++i) {

paddle/fluid/operators/edit_distance_op.cc

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,53 @@ class EditDistanceOp : public framework::OperatorWithKernel {
2222
using framework::OperatorWithKernel::OperatorWithKernel;
2323

2424
void InferShape(framework::InferShapeContext *ctx) const override {
25-
PADDLE_ENFORCE(ctx->HasInput("Hyps"), "Input(Hyps) shouldn't be null.");
26-
PADDLE_ENFORCE(ctx->HasInput("Refs"), "Input(Refs) shouldn't be null.");
27-
PADDLE_ENFORCE(ctx->HasOutput("Out"), "Output(Out) shouldn't be null.");
28-
PADDLE_ENFORCE(ctx->HasOutput("SequenceNum"),
29-
"Output(SequenceNum) shouldn't be null.");
25+
OP_INOUT_CHECK(ctx->HasInput("Hyps"), "Input", "Hyps", "EditDistance");
26+
OP_INOUT_CHECK(ctx->HasInput("Refs"), "Input", "Refs", "EditDistance");
27+
OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "EditDistance");
28+
OP_INOUT_CHECK(ctx->HasOutput("SequenceNum"), "Output", "SequenceNum",
29+
"EditDistance");
3030
auto hyp_dims = ctx->GetInputDim("Hyps");
3131
auto ref_dims = ctx->GetInputDim("Refs");
3232

3333
if (ctx->HasInput("HypsLength") && ctx->HasInput("RefsLength")) {
3434
auto hyp_length_dims = ctx->GetInputDim("HypsLength");
3535
auto ref_length_dims = ctx->GetInputDim("RefsLength");
3636

37-
PADDLE_ENFORCE(hyp_dims.size() == 2 && ref_dims.size() == 2 &&
38-
hyp_dims[0] == ref_dims[0],
39-
"Input(Hyps) and Input(Refs) must be 2-D Tensors with "
40-
"identical first dimension");
41-
PADDLE_ENFORCE(hyp_length_dims[0] == ref_length_dims[0] &&
42-
hyp_length_dims[0] == hyp_dims[0],
43-
"Input(HypsLength), Input(RefsLength) and Input(Hyps) "
44-
"should have identical first dimension");
37+
PADDLE_ENFORCE_EQ(
38+
hyp_dims.size() == 2 && ref_dims.size() == 2 &&
39+
hyp_dims[0] == ref_dims[0],
40+
true, platform::errors::InvalidArgument(
41+
"Input(Hyps) and Input(Refs) must be 2-D Tensors with "
42+
"identical first dimension. But received Input(Hyps): "
43+
"input rank %u, input shape [%s]; received Input(Refs): "
44+
"input rank %u, input shape [%s]",
45+
hyp_dims.size(), hyp_dims, ref_dims.size(), ref_dims));
46+
PADDLE_ENFORCE_EQ(
47+
hyp_length_dims[0] == ref_length_dims[0] &&
48+
hyp_length_dims[0] == hyp_dims[0],
49+
true,
50+
platform::errors::InvalidArgument(
51+
"Input(HypsLength), Input(RefsLength) and Input(Hyps) "
52+
"should have identical first dimension. But received "
53+
"Input(HypsLength): input rank %u, input shape [%s]; "
54+
"received Input(RefsLength): input rank %u, input shape "
55+
"[%s]; received Input(Hyps): input rank %u, input shape "
56+
"[%s].",
57+
hyp_length_dims.size(), hyp_length_dims, ref_length_dims.size(),
58+
ref_length_dims, hyp_dims.size(), hyp_dims));
4559
} else {
46-
PADDLE_ENFORCE(
47-
hyp_dims.size() == 2 && hyp_dims[1] == 1,
48-
"Input(Hyps) must be a 2-D LoDTensor with the 2nd dimension "
49-
"equal to 1.");
50-
PADDLE_ENFORCE(
51-
ref_dims.size() == 2 && ref_dims[1] == 1,
52-
"Input(Refs) must be a 2-D LoDTensor with the 2nd dimension "
53-
"equal to 1.");
60+
PADDLE_ENFORCE_EQ(
61+
hyp_dims.size() == 2 && hyp_dims[1] == 1, true,
62+
platform::errors::InvalidArgument(
63+
"Input(Hyps) must be a 2-D LoDTensor with the 2nd dimension "
64+
"equal to 1. But received: input rank %u, input shape [%s].",
65+
hyp_dims.size(), hyp_dims));
66+
PADDLE_ENFORCE_EQ(
67+
ref_dims.size() == 2 && ref_dims[1] == 1, true,
68+
platform::errors::InvalidArgument(
69+
"Input(Refs) must be a 2-D LoDTensor with the 2nd dimension "
70+
"equal to 1. But received: input rank %u, input shape [%s].",
71+
ref_dims.size(), ref_dims));
5472
}
5573

5674
ctx->SetOutputDim("Out", ctx->GetInputDim("Refs"));

paddle/fluid/operators/expand_as_op.cc

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,27 @@ class ExpandAsOp : public framework::OperatorWithKernel {
2424

2525
protected:
2626
void InferShape(framework::InferShapeContext* ctx) const override {
27-
PADDLE_ENFORCE_EQ(ctx->HasInput("X"), true);
28-
PADDLE_ENFORCE_EQ(ctx->HasInput("target_tensor"), true);
29-
PADDLE_ENFORCE_EQ(ctx->HasOutput("Out"), true);
27+
OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "ExpandAs");
28+
OP_INOUT_CHECK(ctx->HasInput("target_tensor"), "Input", "target_tensor",
29+
"ExpandAs");
30+
OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "ExpandAs");
3031
auto x_dims = ctx->GetInputDim("X");
3132
auto target_tensor_dims = ctx->GetInputDim("target_tensor");
32-
PADDLE_ENFORCE_EQ(static_cast<size_t>(x_dims.size()),
33-
target_tensor_dims.size(),
34-
"The rank of input(target_tensor) must be equal "
35-
"to the rank of Input(X).");
36-
PADDLE_ENFORCE_LE(x_dims.size(), 6,
37-
"The rank of Input(X) must not be greater than 6.");
33+
PADDLE_ENFORCE_EQ(
34+
static_cast<size_t>(x_dims.size()), target_tensor_dims.size(),
35+
platform::errors::InvalidArgument(
36+
"The rank of Input(target_tensor) must be equal "
37+
"to the rank of Input(X). But received Input(X): input "
38+
"rank %u, input shape [%s]; received Input(target_tensor): "
39+
"input rank %u, input shape [%s].",
40+
x_dims.size(), x_dims, target_tensor_dims.size(),
41+
target_tensor_dims));
42+
PADDLE_ENFORCE_LE(
43+
x_dims.size(), 6,
44+
platform::errors::InvalidArgument(
45+
"The rank of Input(X) must not be greater than 6. But "
46+
"received: input rank %u, input shape [%s].",
47+
x_dims.size(), x_dims));
3848
std::vector<int64_t> out_shape(x_dims.size());
3949
ctx->SetOutputDim("Out", framework::make_ddim(out_shape));
4050
}

0 commit comments

Comments
 (0)