Skip to content

Commit ca41e55

Browse files
author
lilong12
authored
[Cherry-pick 1.8] Improving error reporting messages for ops #24438 (#24534)
* improve error reporting messages
1 parent 56eead2 commit ca41e55

File tree

12 files changed

+324
-144
lines changed

12 files changed

+324
-144
lines changed

paddle/fluid/operators/crop_op.cc

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,18 @@ class CropOp : public framework::OperatorWithKernel {
2727
using framework::OperatorWithKernel::OperatorWithKernel;
2828

2929
void InferShape(framework::InferShapeContext* ctx) const override {
30-
PADDLE_ENFORCE(ctx->HasInput("X"),
31-
"Input(X) of CropOp should not be null.");
32-
PADDLE_ENFORCE(ctx->HasOutput("Out"),
33-
"Output(Out) of CropOp should not be null.");
30+
OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "Crop");
31+
OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "Crop");
3432
auto x_dim = ctx->GetInputDim("X");
3533
if (!ctx->HasInput("Y")) {
3634
auto shape = ctx->Attrs().Get<std::vector<int>>("shape");
3735
PADDLE_ENFORCE_EQ(
3836
int64_t(shape.size()), x_dim.size(),
39-
"Shape size should be equal to dimension size of input tensor.");
37+
platform::errors::InvalidArgument(
38+
"The number of elements (%d) of CropOp's "
39+
"'shape' attribute should be equal to the number of dimensions "
40+
"(%d) of the Input(X).",
41+
shape.size(), x_dim.size()));
4042
std::vector<int64_t> tensor_shape(shape.size());
4143
for (size_t i = 0; i < shape.size(); ++i) {
4244
tensor_shape[i] = static_cast<int64_t>(shape[i]);
@@ -45,8 +47,10 @@ class CropOp : public framework::OperatorWithKernel {
4547
} else {
4648
auto y_dim = ctx->GetInputDim("Y");
4749
PADDLE_ENFORCE_EQ(framework::arity(x_dim), framework::arity(y_dim),
48-
"Tensor rank of both CropOp's "
49-
"inputs must be same.");
50+
platform::errors::InvalidArgument(
51+
"The number of dimensions (%d) of CropOp's input(X)"
52+
" must be equal to that (%d) of input(Y).",
53+
framework::arity(x_dim), framework::arity(y_dim)));
5054
ctx->SetOutputDim("Out", y_dim);
5155
}
5256
}
@@ -163,9 +167,9 @@ class CropOpGrad : public framework::OperatorWithKernel {
163167
using framework::OperatorWithKernel::OperatorWithKernel;
164168

165169
void InferShape(framework::InferShapeContext* ctx) const override {
166-
PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should not be null");
167-
PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")),
168-
"Input(Out@GRAD) should not be null");
170+
OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "CropGrad");
171+
OP_INOUT_CHECK(ctx->HasInput(framework::GradVarName("Out")), "Input",
172+
framework::GradVarName("Out"), "CropGrad");
169173
auto x_dims = ctx->GetInputDim("X");
170174
auto x_grad_name = framework::GradVarName("X");
171175
if (ctx->HasOutput(x_grad_name)) {

paddle/fluid/operators/crop_op.h

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,23 @@ static std::vector<int> GetOffsets(const framework::ExecutionContext& ctx) {
3131
std::vector<int> res;
3232
int rank = ctx.Input<Tensor>("X")->dims().size();
3333
if (ctx.HasInput("Offsets")) {
34-
PADDLE_ENFORCE(ctx.Attr<std::vector<int>>("offsets").empty(),
35-
"Input 'Offsets' and attribute 'offsets' should not be used "
36-
"at the same time.");
34+
PADDLE_ENFORCE_EQ(ctx.Attr<std::vector<int>>("offsets").empty(), true,
35+
platform::errors::InvalidArgument(
36+
"Input 'Offsets' and attribute 'offsets' "
37+
"should not be used at the same time for CropOp."));
3738
const auto* offsets_tensor = ctx.Input<Tensor>("Offsets");
38-
PADDLE_ENFORCE_EQ(offsets_tensor->dims().size(), 1);
39+
PADDLE_ENFORCE_EQ(offsets_tensor->dims().size(), 1,
40+
platform::errors::InvalidArgument(
41+
"The number of dimensions of input 'Offsets' for "
42+
"CropOp must be 1, but the value received is %d.",
43+
offsets_tensor->dims().size()));
3944
PADDLE_ENFORCE_EQ(
4045
rank, offsets_tensor->dims()[0],
41-
"Offsets size should be equal to dimension size of input tensor.");
46+
platform::errors::InvalidArgument("The number of elements (%d) for "
47+
"input 'Offsets' must be equal to "
48+
"the number of dimensions (%d) "
49+
"of the input tensor.",
50+
offsets_tensor->dims()[0], rank));
4251
const int* offsets_data;
4352
framework::Tensor cpu_tmp_tensor;
4453
if (platform::is_cpu_place(offsets_tensor->place())) {
@@ -53,7 +62,11 @@ static std::vector<int> GetOffsets(const framework::ExecutionContext& ctx) {
5362
res = ctx.Attr<std::vector<int>>("offsets");
5463
PADDLE_ENFORCE_EQ(
5564
rank, static_cast<int>(res.size()),
56-
"Offsets size should be equal to dimension size of input tensor.");
65+
platform::errors::InvalidArgument("The number of elements (%d) for "
66+
"input 'Offsets' must be equal to "
67+
"the number of dimensions (%d) "
68+
"of the input tensor.",
69+
res.size(), rank));
5770
}
5871
return res;
5972
}
@@ -92,6 +105,18 @@ class CropKernel : public framework::OpKernel<T> {
92105
public:
93106
void Compute(const framework::ExecutionContext& context) const override {
94107
int rank = context.Input<Tensor>("X")->dims().size();
108+
PADDLE_ENFORCE_GE(
109+
rank, 1,
110+
platform::errors::InvalidArgument(
111+
"The number of dimensions of the Input(X) for CropOp must be "
112+
"greater than or equal to 1, but the value received is %d.",
113+
rank));
114+
PADDLE_ENFORCE_LE(
115+
rank, 6,
116+
platform::errors::InvalidArgument(
117+
"The number of dimensions of the Input(X) for CropOp must be "
118+
"less than or equal to 6, but the value received is %d.",
119+
rank));
95120
switch (rank) {
96121
case 1:
97122
CropFunction<DeviceContext, T, 1>(context);
@@ -111,9 +136,6 @@ class CropKernel : public framework::OpKernel<T> {
111136
case 6:
112137
CropFunction<DeviceContext, T, 6>(context);
113138
break;
114-
default:
115-
PADDLE_THROW(
116-
"CropOp only support tensors with no more than 6 dimensions.");
117139
}
118140
}
119141
};
@@ -145,6 +167,18 @@ class CropGradKernel : public framework::OpKernel<T> {
145167
void Compute(const framework::ExecutionContext& context) const override {
146168
size_t rank =
147169
context.Input<Tensor>(framework::GradVarName("Out"))->dims().size();
170+
PADDLE_ENFORCE_GE(
171+
rank, 1, platform::errors::InvalidArgument(
172+
"The number of dimensions of the input 'Out@GRAD' for "
173+
"CropGrad must be greater than or equal "
174+
"to 1, but the value received is %d.",
175+
rank));
176+
PADDLE_ENFORCE_LE(
177+
rank, 6, platform::errors::InvalidArgument(
178+
"The number of dimensions of the input 'Out@GRAD' for "
179+
"CropGrad must be less than or equal "
180+
"to 6, but the value received is %d.",
181+
rank));
148182
switch (rank) {
149183
case 1:
150184
CropGradFunction<DeviceContext, T, 1>(context);
@@ -164,9 +198,6 @@ class CropGradKernel : public framework::OpKernel<T> {
164198
case 6:
165199
CropGradFunction<DeviceContext, T, 6>(context);
166200
break;
167-
default:
168-
PADDLE_THROW(
169-
"CropOp only support tensors with no more than 6 dimensions.");
170201
}
171202
}
172203
};

paddle/fluid/operators/crop_tensor_op.cc

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@ class CropTensorOp : public framework::OperatorWithKernel {
2727
using framework::OperatorWithKernel::OperatorWithKernel;
2828

2929
void InferShape(framework::InferShapeContext *ctx) const override {
30-
PADDLE_ENFORCE_EQ(ctx->HasInput("X"), true,
31-
"Input(X) of Op(crop_tensor) should not be null.");
32-
PADDLE_ENFORCE_EQ(ctx->HasOutput("Out"), true,
33-
"Output(Out) of Op(crop_tensor) should not be null.");
30+
OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "CropTensor");
31+
OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "CropTensor");
3432
auto x_dim = ctx->GetInputDim("X");
3533
auto shape = ctx->Attrs().Get<std::vector<int>>("shape");
3634
auto offsets = ctx->Attrs().Get<std::vector<int>>("offsets");
@@ -39,9 +37,11 @@ class CropTensorOp : public framework::OperatorWithKernel {
3937
auto inputs_name = ctx->Inputs("ShapeTensor");
4038
PADDLE_ENFORCE_GT(
4139
inputs_name.size(), 0,
42-
"Input(ShapeTensor)'size of Op(crop_tensor) can't be zero. "
43-
"Please check the Attr(shape)'s size of "
44-
"Op(fluid.layers.crop_tensor).");
40+
platform::errors::InvalidArgument(
41+
"The number of elements of the input 'ShapeTensor' for "
42+
"CropTensor must be greater than zero, "
43+
"but the value received is %d.",
44+
inputs_name.size()));
4545
auto out_dims = std::vector<int>(inputs_name.size(), -1);
4646
for (size_t i = 0; i < shape.size(); ++i) {
4747
if (shape[i] > 0) {
@@ -59,16 +59,18 @@ class CropTensorOp : public framework::OperatorWithKernel {
5959

6060
if (ctx->HasInput("Shape")) {
6161
auto shape_dim = ctx->GetInputDim("Shape");
62-
PADDLE_ENFORCE_EQ(
63-
shape_dim.size(), 1,
64-
"Input(Shape)'s dimension size of Op(crop_tensor) must be 1. "
65-
"Please check the Attr(shape)'s dimension size of "
66-
"Op(fluid.layers.crop_tensor).");
62+
PADDLE_ENFORCE_EQ(shape_dim.size(), 1,
63+
platform::errors::InvalidArgument(
64+
"The number of dimensions of the input "
65+
"'Shape' for CropTensor must be 1, "
66+
"but the value received is %d.",
67+
shape_dim.size()));
6768
PADDLE_ENFORCE_EQ(shape_dim[0], x_dim.size(),
68-
"Input(Shape)'s size of Op(crop_tensor) must be equal "
69-
"to dimension size of input tensor. "
70-
"Please check the Attr(shape)'s size of "
71-
"Op(fluid.layers.crop_tensor).");
69+
platform::errors::InvalidArgument(
70+
"The number of elements (%d) of the input 'Shape' "
71+
"for CropTensor must be equal to the number of"
72+
" dimensions (%d) of the input.",
73+
shape_dim[0], x_dim.size()));
7274
if (ctx->IsRuntime()) {
7375
// If true, set the shape of Output(Out) according to Input(Shape) in
7476
// CropTensorKernel with ExecutionContext. Also check LoD in
@@ -80,9 +82,13 @@ class CropTensorOp : public framework::OperatorWithKernel {
8082
}
8183
return;
8284
}
83-
PADDLE_ENFORCE_EQ(int64_t(shape.size()), x_dim.size(),
84-
"Attr(shape)'size of Op(crop_tensor) should be equal to "
85-
"dimension size of input tensor.");
85+
PADDLE_ENFORCE_EQ(
86+
int64_t(shape.size()), x_dim.size(),
87+
platform::errors::InvalidArgument(
88+
"The number of elements (%d) of attribute 'shape' for "
89+
"CropTensor must be equal to the number of "
90+
"dimensions (%d) of the input.",
91+
shape.size(), x_dim.size()));
8692
std::vector<int64_t> out_shape(shape.size(), -1);
8793
for (size_t i = 0; i < shape.size(); ++i) {
8894
if (shape[i] > 0) {
@@ -242,10 +248,9 @@ class CropTensorOpGrad : public framework::OperatorWithKernel {
242248
using framework::OperatorWithKernel::OperatorWithKernel;
243249

244250
void InferShape(framework::InferShapeContext *ctx) const override {
245-
PADDLE_ENFORCE_EQ(ctx->HasInput("X"), true,
246-
"Input(X) of Op(crop_tensor) should not be null.");
247-
PADDLE_ENFORCE_EQ(ctx->HasInput(framework::GradVarName("Out")), true,
248-
"Input(Out@GRAD) of Op(crop_tensor) should not be null.");
251+
OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "CropTensorGrad");
252+
OP_INOUT_CHECK(ctx->HasInput(framework::GradVarName("Out")), "Input",
253+
framework::GradVarName("Out"), "CropTensorGrad");
249254
auto x_dims = ctx->GetInputDim("X");
250255
auto x_grad_name = framework::GradVarName("X");
251256
if (ctx->HasOutput(x_grad_name)) {

0 commit comments

Comments
 (0)