Skip to content

Commit 9cbf601

Browse files
[Cherry-Pick] [2.0-beta] c++ error enhancement (#24189)
1 parent 53cb220 commit 9cbf601

File tree

3 files changed

+54
-59
lines changed

3 files changed

+54
-59
lines changed

paddle/fluid/operators/average_accumulates_op.cc

Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -48,48 +48,33 @@ class AverageAccumulatesOp : public framework::OperatorWithKernel {
4848
using framework::OperatorWithKernel::OperatorWithKernel;
4949

5050
void InferShape(framework::InferShapeContext* ctx) const override {
51-
PADDLE_ENFORCE(
52-
ctx->HasInput("param"),
53-
"Input (param) of average_accumulates op should not be null.");
54-
PADDLE_ENFORCE(
55-
ctx->HasInput("in_sum_1"),
56-
"Input (sum_1) of average_accumulates op should not be null.");
57-
PADDLE_ENFORCE(
58-
ctx->HasInput("in_sum_2"),
59-
"Input (sum_2) of average_accumulates op should not be null.");
60-
PADDLE_ENFORCE(
61-
ctx->HasInput("in_sum_3"),
62-
"Input (sum_3) of average_accumulates op should not be null.");
63-
PADDLE_ENFORCE(
64-
ctx->HasInput("in_num_accumulates"),
65-
"Input (in_num_accumulates) of average_accumulates op should "
66-
"not be null.");
67-
PADDLE_ENFORCE(ctx->HasInput("in_old_num_accumulates"),
68-
"Input (old_num_accumulates) of average_accumulates op "
69-
"should not be null.");
70-
PADDLE_ENFORCE(
71-
ctx->HasInput("in_num_updates"),
72-
"Input (num_updates) of average_accumulates op should not be null.");
73-
74-
PADDLE_ENFORCE(
75-
ctx->HasOutput("out_sum_1"),
76-
"Output (sum_1) of average_accumulates op should not be null.");
77-
PADDLE_ENFORCE(
78-
ctx->HasOutput("out_sum_2"),
79-
"Output (sum_2) of average_accumulates op should not be null.");
80-
PADDLE_ENFORCE(
81-
ctx->HasOutput("out_sum_3"),
82-
"Output (sum_3) of average_accumulates op should not be null.");
83-
PADDLE_ENFORCE(ctx->HasOutput("out_num_accumulates"),
84-
"Output (num_accumulates) of average_accumulates op should "
85-
"not be null.");
86-
PADDLE_ENFORCE(ctx->HasOutput("out_old_num_accumulates"),
87-
"Output (old_num_accumulates) of average_accumulates op "
88-
"should not be null.");
89-
PADDLE_ENFORCE(
90-
ctx->HasOutput("out_num_updates"),
91-
"Output (num_updates) of average_accumulates op should not be null.");
92-
51+
OP_INOUT_CHECK(ctx->HasInput("param"), "Input", "param",
52+
"AverageAccumulates");
53+
OP_INOUT_CHECK(ctx->HasInput("in_sum_1"), "Input", "in_sum_1",
54+
"AverageAccumulates");
55+
OP_INOUT_CHECK(ctx->HasInput("in_sum_2"), "Input", "in_sum_2",
56+
"AverageAccumulates");
57+
OP_INOUT_CHECK(ctx->HasInput("in_sum_3"), "Input", "in_sum_3",
58+
"AverageAccumulates");
59+
OP_INOUT_CHECK(ctx->HasInput("in_num_accumulates"), "Input",
60+
"in_num_accumulates", "AverageAccumulates");
61+
OP_INOUT_CHECK(ctx->HasInput("in_old_num_accumulates"), "Input",
62+
"in_old_num_accumulates", "AverageAccumulates");
63+
OP_INOUT_CHECK(ctx->HasInput("in_num_updates"), "Input", "in_num_updates",
64+
"AverageAccumulates");
65+
66+
OP_INOUT_CHECK(ctx->HasOutput("out_sum_1"), "Output", "out_sum_1",
67+
"AverageAccumulates");
68+
OP_INOUT_CHECK(ctx->HasOutput("out_sum_2"), "Output", "out_sum_2",
69+
"AverageAccumulates");
70+
OP_INOUT_CHECK(ctx->HasOutput("out_sum_3"), "Output", "out_sum_3",
71+
"AverageAccumulates");
72+
OP_INOUT_CHECK(ctx->HasOutput("out_num_accumulates"), "Output",
73+
"out_num_accumulates", "AverageAccumulates");
74+
OP_INOUT_CHECK(ctx->HasOutput("out_old_num_accumulates"), "Output",
75+
"out_old_num_accumulates", "AverageAccumulates");
76+
OP_INOUT_CHECK(ctx->HasOutput("out_num_updates"), "Output",
77+
"out_num_updates", "AverageAccumulates");
9378
auto in_dim = ctx->GetInputDim("param");
9479

9580
ctx->SetOutputDim("out_sum_1", in_dim);

paddle/fluid/operators/get_tensor_from_selected_rows_op.cc

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,25 @@ class GetTensorFromSelectedRowsOp : public framework::OperatorWithKernel {
2323
using framework::OperatorWithKernel::OperatorWithKernel;
2424

2525
void InferShape(framework::InferShapeContext *ctx) const override {
26-
PADDLE_ENFORCE(ctx->HasInput("X"),
27-
"GetTensorFromSelectedRowsOp must have input X.");
28-
PADDLE_ENFORCE(ctx->HasOutput("Out"),
29-
"GetTensorFromSelectedRowsOp must have output Out.");
30-
PADDLE_ENFORCE(
31-
ctx->GetInputsVarType("X").front() ==
32-
framework::proto::VarType::SELECTED_ROWS,
33-
"The input X's type should be SelectedRows, but the received is %s",
34-
ctx->Inputs("X").front(), ctx->GetInputsVarType("X").front());
35-
PADDLE_ENFORCE(
36-
ctx->GetOutputsVarType("Out").front() ==
37-
framework::proto::VarType::LOD_TENSOR,
38-
"The output Out's type should be LoDTensor, but the received is %s",
39-
ctx->Outputs("Out").front(), ctx->GetOutputsVarType("Out").front());
40-
26+
OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X",
27+
"GetTensorFromSelectedRows");
28+
OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out",
29+
"GetTensorFromSelectedRows");
30+
31+
PADDLE_ENFORCE_EQ(
32+
ctx->GetInputsVarType("X").front(),
33+
framework::proto::VarType::SELECTED_ROWS,
34+
platform::errors::InvalidArgument(
35+
"The input X(%s)'s type should be SelectedRows, "
36+
"but the received is %s",
37+
ctx->Inputs("X").front(), ctx->GetInputsVarType("X").front()));
38+
PADDLE_ENFORCE_EQ(ctx->GetOutputsVarType("Out").front(),
39+
framework::proto::VarType::LOD_TENSOR,
40+
platform::errors::InvalidArgument(
41+
"The output Out(%s)'s type should be LoDTensor, "
42+
"but the received is %s",
43+
ctx->Outputs("Out").front(),
44+
ctx->GetOutputsVarType("Out").front()));
4145
ctx->SetOutputDim("Out", ctx->GetInputDim("X"));
4246
}
4347

paddle/fluid/operators/tensor_array_to_tensor_op.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ class LoDTensorArray2TensorOp : public framework::OperatorBase {
9696
*scope.FindVar(Output("OutIndex"))->GetMutable<framework::LoDTensor>();
9797

9898
const size_t n = inx.size();
99-
PADDLE_ENFORCE_GT(n, 0, "Input tensorarray size should > 0.");
99+
PADDLE_ENFORCE_GT(n, 0, platform::errors::InvalidArgument(
100+
"Input tensorarray size should > 0,"
101+
"but the received is %d",
102+
n));
100103

101104
std::string base_name = Inputs("X")[0];
102105
std::vector<std::string> names;
@@ -232,7 +235,10 @@ class LoDTensorArray2TensorGradOp : public framework::OperatorBase {
232235

233236
auto &inx = scope.FindVar(Input("X"))->Get<framework::LoDTensorArray>();
234237
const size_t n = inx.size();
235-
PADDLE_ENFORCE_GT(n, 0, "Input tensorarray size should > 0.");
238+
PADDLE_ENFORCE_GT(n, 0, platform::errors::InvalidArgument(
239+
"Input tensorarray size should > 0, "
240+
"but the received is: %d. ",
241+
n));
236242

237243
std::string base_name = Inputs("X")[0];
238244
std::vector<std::string> names;

0 commit comments

Comments
 (0)