Skip to content

Commit 5fc4275

Browse files
Enhance checking in some operator. (#24935)
1 parent b8eb52f commit 5fc4275

File tree

10 files changed

+269
-113
lines changed

10 files changed

+269
-113
lines changed

paddle/fluid/operators/affine_grid_op.cc

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,29 +42,57 @@ class AffineGridOp : public framework::OperatorWithKernel {
4242
public:
4343
using framework::OperatorWithKernel::OperatorWithKernel;
4444
void InferShape(framework::InferShapeContext* ctx) const override {
45-
PADDLE_ENFORCE(ctx->HasInput("Theta"),
46-
"Input(Theta) of AffineGridOp should not be null.");
47-
PADDLE_ENFORCE(ctx->HasOutput("Output"),
48-
"Output(Output) of AffineGridOp should not be null.");
45+
PADDLE_ENFORCE_EQ(ctx->HasInput("Theta"), true,
46+
platform::errors::NotFound(
47+
"The input 'Theta' of AffineGridOp is not found."));
48+
PADDLE_ENFORCE_EQ(ctx->HasOutput("Output"), true,
49+
platform::errors::NotFound(
50+
"The output 'Output' of AffineGridOp is not found."));
4951
auto theta_dims = ctx->GetInputDim("Theta");
50-
PADDLE_ENFORCE(theta_dims.size() == 3,
51-
"AffineGrid's Input(Theta) should be 3-D tensor.");
52+
PADDLE_ENFORCE_EQ(
53+
theta_dims.size(), 3,
54+
platform::errors::InvalidArgument(
55+
"The input Theta's dimensions size should be 3. But received "
56+
"Theta's demensions size=[%d], Theta's dimensions=[%s].",
57+
theta_dims.size(), theta_dims));
5258

5359
auto output_shape = ctx->Attrs().Get<std::vector<int>>("output_shape");
5460
if (output_shape.size() == 0) {
55-
PADDLE_ENFORCE(ctx->HasInput("OutputShape"),
56-
"Input(OutputShape) of AffineGridOp should not be null if "
57-
"attr(output_shape) is not configured.");
61+
PADDLE_ENFORCE_EQ(
62+
ctx->HasInput("OutputShape"), true,
63+
platform::errors::NotFound(
64+
"The input 'OutputShape' of AffineGridOp should not be null if "
65+
"'output_shape' is not configured."));
5866
auto output_shape_dims = ctx->GetInputDim("OutputShape");
59-
PADDLE_ENFORCE(output_shape_dims.size() == 1,
60-
"AffineGrid's Input(OutputShape) should be 1-D tensor.");
67+
PADDLE_ENFORCE_EQ(
68+
output_shape_dims.size(), 1,
69+
platform::errors::InvalidArgument(
70+
"The dimesions size of input OutputShape in AffineGridOp should "
71+
"be 1. But received OutputShape's dimesions size=[%d], "
72+
"OutputShape's dimesions=[%s]",
73+
output_shape_dims.size(), output_shape_dims));
6174
} else {
62-
PADDLE_ENFORCE(output_shape.size() == 4,
63-
"The size of attr(output_shape) should be 4.");
75+
PADDLE_ENFORCE_EQ(
76+
output_shape.size(), 4,
77+
platform::errors::InvalidArgument(
78+
"The size of attribute 'output_shape' in AffineGridOp should be "
79+
"4. But received output_shape's size=[%d].",
80+
output_shape.size()));
6481
}
6582

66-
PADDLE_ENFORCE(theta_dims[1] == 2, "Input(theta) dims[1] should be 2.");
67-
PADDLE_ENFORCE(theta_dims[2] == 3, "Input(theta) dims[2] should be 3.");
83+
PADDLE_ENFORCE_EQ(
84+
theta_dims[1], 2,
85+
platform::errors::InvalidArgument(
86+
"The second dimesion of input 'theta' in AffineGridOp should be 2. "
87+
"But received second dimesion=[%d], dimesions=[%s]",
88+
theta_dims[1], theta_dims));
89+
PADDLE_ENFORCE_EQ(
90+
theta_dims[2], 3,
91+
platform::errors::InvalidArgument(
92+
"The third dimesion of input 'theta' in AffineGridOp should be 3. "
93+
"But received third dimesion=[%d], dimesions=[%s]",
94+
theta_dims[2], theta_dims));
95+
6896
// N * H * W * 2
6997
ctx->SetOutputDim("Output",
7098
framework::make_ddim({theta_dims[0], -1, -1, 2}));

paddle/fluid/operators/detection/generate_proposal_labels_op.cc

Lines changed: 72 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -38,42 +38,64 @@ class GenerateProposalLabelsOp : public framework::OperatorWithKernel {
3838
using framework::OperatorWithKernel::OperatorWithKernel;
3939

4040
void InferShape(framework::InferShapeContext* ctx) const override {
41-
PADDLE_ENFORCE(ctx->HasInput("RpnRois"),
42-
"Input(RpnRois) shouldn't be null.");
43-
PADDLE_ENFORCE(ctx->HasInput("GtClasses"),
44-
"Input(GtClasses) shouldn't be null.");
45-
PADDLE_ENFORCE(ctx->HasInput("IsCrowd"),
46-
"Input(IsCrowd) shouldn't be null.");
47-
PADDLE_ENFORCE(ctx->HasInput("GtBoxes"),
48-
"Input(GtBoxes) shouldn't be null.");
49-
PADDLE_ENFORCE(ctx->HasInput("ImInfo"), "Input(ImInfo) shouldn't be null.");
50-
51-
PADDLE_ENFORCE(
52-
ctx->HasOutput("Rois"),
53-
"Output(Rois) of GenerateProposalLabelsOp should not be null");
54-
PADDLE_ENFORCE(
55-
ctx->HasOutput("LabelsInt32"),
56-
"Output(LabelsInt32) of GenerateProposalLabelsOp should not be null");
57-
PADDLE_ENFORCE(
58-
ctx->HasOutput("BboxTargets"),
59-
"Output(BboxTargets) of GenerateProposalLabelsOp should not be null");
60-
PADDLE_ENFORCE(ctx->HasOutput("BboxInsideWeights"),
61-
"Output(BboxInsideWeights) of GenerateProposalLabelsOp "
62-
"should not be null");
63-
PADDLE_ENFORCE(ctx->HasOutput("BboxOutsideWeights"),
64-
"Output(BboxOutsideWeights) of GenerateProposalLabelsOp "
65-
"should not be null");
41+
PADDLE_ENFORCE_EQ(
42+
ctx->HasInput("RpnRois"), true,
43+
platform::errors::NotFound("Input(RpnRois) shouldn't be null."));
44+
PADDLE_ENFORCE_EQ(
45+
ctx->HasInput("GtClasses"), true,
46+
platform::errors::NotFound("Input(GtClasses) shouldn't be null."));
47+
PADDLE_ENFORCE_EQ(
48+
ctx->HasInput("IsCrowd"), true,
49+
platform::errors::NotFound("Input(IsCrowd) shouldn't be null."));
50+
PADDLE_ENFORCE_EQ(
51+
ctx->HasInput("GtBoxes"), true,
52+
platform::errors::NotFound("Input(GtBoxes) shouldn't be null."));
53+
PADDLE_ENFORCE_EQ(
54+
ctx->HasInput("ImInfo"), true,
55+
platform::errors::NotFound("Input(ImInfo) shouldn't be null."));
56+
57+
PADDLE_ENFORCE_EQ(
58+
ctx->HasOutput("Rois"), true,
59+
platform::errors::NotFound(
60+
"Output(Rois) of GenerateProposalLabelsOp should not be null"));
61+
PADDLE_ENFORCE_EQ(ctx->HasOutput("LabelsInt32"), true,
62+
platform::errors::NotFound("Output(LabelsInt32) of "
63+
"GenerateProposalLabelsOp "
64+
"should not be null"));
65+
PADDLE_ENFORCE_EQ(ctx->HasOutput("BboxTargets"), true,
66+
platform::errors::NotFound("Output(BboxTargets) of "
67+
"GenerateProposalLabelsOp "
68+
"should not be null"));
69+
PADDLE_ENFORCE_EQ(
70+
ctx->HasOutput("BboxInsideWeights"), true,
71+
platform::errors::NotFound(
72+
"Output(BboxInsideWeights) of GenerateProposalLabelsOp "
73+
"should not be null"));
74+
PADDLE_ENFORCE_EQ(
75+
ctx->HasOutput("BboxOutsideWeights"), true,
76+
platform::errors::NotFound(
77+
"Output(BboxOutsideWeights) of GenerateProposalLabelsOp "
78+
"should not be null"));
6679

6780
auto rpn_rois_dims = ctx->GetInputDim("RpnRois");
6881
auto gt_boxes_dims = ctx->GetInputDim("GtBoxes");
6982
auto im_info_dims = ctx->GetInputDim("ImInfo");
7083

7184
PADDLE_ENFORCE_EQ(rpn_rois_dims.size(), 2,
72-
"The rank of Input(RpnRois) must be 2.");
85+
platform::errors::InvalidArgument(
86+
"The dimensions size of Input(RpnRois) must be 2. "
87+
"But received dimensions size=[%d], dimensions=[%s].",
88+
rpn_rois_dims.size(), rpn_rois_dims));
7389
PADDLE_ENFORCE_EQ(gt_boxes_dims.size(), 2,
74-
"The rank of Input(GtBoxes) must be 2.");
90+
platform::errors::InvalidArgument(
91+
"The dimensions size of Input(GtBoxes) must be 2. "
92+
"But received dimensions size=[%d], dimensions=[%s].",
93+
gt_boxes_dims.size(), gt_boxes_dims));
7594
PADDLE_ENFORCE_EQ(im_info_dims.size(), 2,
76-
"The rank of Input(ImInfo) must be 2.");
95+
platform::errors::InvalidArgument(
96+
"The dimensions size of Input(ImInfo) must be 2. But "
97+
"received dimensions size=[%d], dimensions=[%s].",
98+
im_info_dims.size(), im_info_dims));
7799

78100
int class_nums = ctx->Attrs().Get<int>("class_nums");
79101

@@ -399,15 +421,30 @@ class GenerateProposalLabelsKernel : public framework::OpKernel<T> {
399421
bool use_random = context.Attr<bool>("use_random");
400422
bool is_cascade_rcnn = context.Attr<bool>("is_cascade_rcnn");
401423
bool is_cls_agnostic = context.Attr<bool>("is_cls_agnostic");
402-
PADDLE_ENFORCE_EQ(rpn_rois->lod().size(), 1UL,
403-
"GenerateProposalLabelsOp rpn_rois needs 1 level of LoD");
424+
PADDLE_ENFORCE_EQ(
425+
rpn_rois->lod().size(), 1UL,
426+
platform::errors::InvalidArgument(
427+
"GenerateProposalLabelsOp rpn_rois needs 1 level of LoD. But "
428+
"received level of LoD is [%d], LoD is [%s].",
429+
rpn_rois->lod().size(), rpn_rois->lod()));
404430
PADDLE_ENFORCE_EQ(
405431
gt_classes->lod().size(), 1UL,
406-
"GenerateProposalLabelsOp gt_classes needs 1 level of LoD");
407-
PADDLE_ENFORCE_EQ(is_crowd->lod().size(), 1UL,
408-
"GenerateProposalLabelsOp is_crowd needs 1 level of LoD");
409-
PADDLE_ENFORCE_EQ(gt_boxes->lod().size(), 1UL,
410-
"GenerateProposalLabelsOp gt_boxes needs 1 level of LoD");
432+
platform::errors::InvalidArgument(
433+
"GenerateProposalLabelsOp gt_classes needs 1 level of LoD. But "
434+
"received level of LoD is [%d], LoD is [%s].",
435+
gt_classes->lod().size(), gt_classes->lod()));
436+
PADDLE_ENFORCE_EQ(
437+
is_crowd->lod().size(), 1UL,
438+
platform::errors::InvalidArgument(
439+
"GenerateProposalLabelsOp is_crowd needs 1 level of LoD. But "
440+
"received level of LoD is [%d], LoD is [%s].",
441+
is_crowd->lod().size(), is_crowd->lod()));
442+
PADDLE_ENFORCE_EQ(
443+
gt_boxes->lod().size(), 1UL,
444+
platform::errors::InvalidArgument(
445+
"GenerateProposalLabelsOp gt_boxes needs 1 level of LoD. But "
446+
"received level of LoD is [%d], LoD is [%s].",
447+
gt_boxes->lod().size(), gt_boxes->lod()));
411448
int64_t n = static_cast<int64_t>(rpn_rois->lod().back().size() - 1);
412449

413450
rois->mutable_data<T>({n * batch_size_per_im, kBoxDim}, context.GetPlace());

paddle/fluid/operators/detection/generate_proposals_op.cc

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,21 @@ class GenerateProposalsOp : public framework::OperatorWithKernel {
4343
using framework::OperatorWithKernel::OperatorWithKernel;
4444

4545
void InferShape(framework::InferShapeContext *ctx) const override {
46-
PADDLE_ENFORCE(ctx->HasInput("Scores"), "Input(Scores) shouldn't be null.");
47-
PADDLE_ENFORCE(ctx->HasInput("BboxDeltas"),
48-
"Input(BboxDeltas) shouldn't be null.");
49-
PADDLE_ENFORCE(ctx->HasInput("ImInfo"), "Input(ImInfo) shouldn't be null.");
50-
PADDLE_ENFORCE(ctx->HasInput("Anchors"),
51-
"Input(Anchors) shouldn't be null.");
52-
PADDLE_ENFORCE(ctx->HasInput("Variances"),
53-
"Input(Variances) shouldn't be null.");
46+
PADDLE_ENFORCE_EQ(
47+
ctx->HasInput("Scores"), true,
48+
platform::errors::NotFound("Input(Scores) shouldn't be null."));
49+
PADDLE_ENFORCE_EQ(
50+
ctx->HasInput("BboxDeltas"), true,
51+
platform::errors::NotFound("Input(BboxDeltas) shouldn't be null."));
52+
PADDLE_ENFORCE_EQ(
53+
ctx->HasInput("ImInfo"), true,
54+
platform::errors::NotFound("Input(ImInfo) shouldn't be null."));
55+
PADDLE_ENFORCE_EQ(
56+
ctx->HasInput("Anchors"), true,
57+
platform::errors::NotFound("Input(Anchors) shouldn't be null."));
58+
PADDLE_ENFORCE_EQ(
59+
ctx->HasInput("Variances"), true,
60+
platform::errors::NotFound("Input(Variances) shouldn't be null."));
5461

5562
ctx->SetOutputDim("RpnRois", {-1, 4});
5663
ctx->SetOutputDim("RpnRoiProbs", {-1, 1});
@@ -247,7 +254,6 @@ static inline Tensor VectorToTensor(const std::vector<T> &selected_indices,
247254
template <class T>
248255
static inline Tensor NMS(const platform::DeviceContext &ctx, Tensor *bbox,
249256
Tensor *scores, T nms_threshold, float eta) {
250-
PADDLE_ENFORCE_NOT_NULL(bbox);
251257
int64_t num_boxes = bbox->dims()[0];
252258
// 4: [xmin ymin xmax ymax]
253259
int64_t box_size = bbox->dims()[1];

paddle/fluid/operators/detection/generate_proposals_op.cu

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,11 @@ class CUDAGenerateProposalsKernel : public framework::OpKernel<T> {
379379
float nms_thresh = context.Attr<float>("nms_thresh");
380380
float min_size = context.Attr<float>("min_size");
381381
float eta = context.Attr<float>("eta");
382-
PADDLE_ENFORCE_GE(eta, 1., "Not support adaptive NMS.");
382+
PADDLE_ENFORCE_GE(eta, 1.,
383+
platform::errors::InvalidArgument(
384+
"Not support adaptive NMS. The attribute 'eta' "
385+
"should not less than 1. But received eta=[%d]",
386+
eta));
383387

384388
auto &dev_ctx = context.template device_context<DeviceContext>();
385389

paddle/fluid/operators/detection/rpn_target_assign_op.cc

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,40 +31,44 @@ class RpnTargetAssignOp : public framework::OperatorWithKernel {
3131
using framework::OperatorWithKernel::OperatorWithKernel;
3232

3333
void InferShape(framework::InferShapeContext* ctx) const override {
34-
PADDLE_ENFORCE(ctx->HasInput("Anchor"),
35-
"Input(Anchor) of RpnTargetAssignOp should not be null");
36-
PADDLE_ENFORCE(ctx->HasInput("GtBoxes"),
37-
"Input(GtBoxes) of RpnTargetAssignOp should not be null");
38-
PADDLE_ENFORCE(ctx->HasInput("IsCrowd"),
39-
"Input(Anchor) of RpnTargetAssignOp should not be null");
40-
PADDLE_ENFORCE(ctx->HasInput("ImInfo"),
41-
"Input(ImInfo) of RpnTargetAssignOp should not be null");
42-
43-
PADDLE_ENFORCE(
44-
ctx->HasOutput("LocationIndex"),
45-
"Output(LocationIndex) of RpnTargetAssignOp should not be null");
46-
PADDLE_ENFORCE(
47-
ctx->HasOutput("ScoreIndex"),
48-
"Output(ScoreIndex) of RpnTargetAssignOp should not be null");
49-
PADDLE_ENFORCE(
50-
ctx->HasOutput("TargetLabel"),
51-
"Output(TargetLabel) of RpnTargetAssignOp should not be null");
52-
PADDLE_ENFORCE(
53-
ctx->HasOutput("TargetBBox"),
54-
"Output(TargetBBox) of RpnTargetAssignOp should not be null");
55-
PADDLE_ENFORCE(
56-
ctx->HasOutput("BBoxInsideWeight"),
57-
"Output(BBoxInsideWeight) of RpnTargetAssignOp should not be null");
34+
OP_INOUT_CHECK(ctx->HasInput("Anchor"), "Input", "Anchor",
35+
"rpn_target_assign");
36+
OP_INOUT_CHECK(ctx->HasInput("GtBoxes"), "Input", "GtBoxes",
37+
"rpn_target_assign");
38+
OP_INOUT_CHECK(ctx->HasInput("IsCrowd"), "Input", "IsCrowd",
39+
"rpn_target_assign");
40+
OP_INOUT_CHECK(ctx->HasInput("ImInfo"), "Input", "ImInfo",
41+
"rpn_target_assign");
42+
43+
OP_INOUT_CHECK(ctx->HasOutput("LocationIndex"), "Output", "LocationIndex",
44+
"rpn_target_assign");
45+
OP_INOUT_CHECK(ctx->HasOutput("ScoreIndex"), "Output", "ScoreIndex",
46+
"rpn_target_assign");
47+
OP_INOUT_CHECK(ctx->HasOutput("TargetLabel"), "Output", "TargetLabel",
48+
"rpn_target_assign");
49+
OP_INOUT_CHECK(ctx->HasOutput("TargetBBox"), "Output", "TargetBBox",
50+
"rpn_target_assign");
51+
OP_INOUT_CHECK(ctx->HasOutput("BBoxInsideWeight"), "Output",
52+
"BBoxInsideWeight", "rpn_target_assign");
5853

5954
auto anchor_dims = ctx->GetInputDim("Anchor");
6055
auto gt_boxes_dims = ctx->GetInputDim("GtBoxes");
6156
auto im_info_dims = ctx->GetInputDim("ImInfo");
6257
PADDLE_ENFORCE_EQ(anchor_dims.size(), 2,
63-
"The rank of Input(Anchor) must be 2.");
58+
platform::errors::InvalidArgument(
59+
"The dimensions size of Input(Anchor) must be 2. But "
60+
"received dimensions size=[%d], dimensions=[%s].",
61+
anchor_dims.size(), anchor_dims));
6462
PADDLE_ENFORCE_EQ(gt_boxes_dims.size(), 2,
65-
"The rank of Input(GtBoxes) must be 2.");
63+
platform::errors::InvalidArgument(
64+
"The dimensions size of Input(GtBoxes) must be 2. "
65+
"But received dimensions size=[%d], dimensions=[%s].",
66+
gt_boxes_dims.size(), gt_boxes_dims));
6667
PADDLE_ENFORCE_EQ(im_info_dims.size(), 2,
67-
"The rank of Input(ImInfo) must be 2.");
68+
platform::errors::InvalidArgument(
69+
"The dimensions size of Input(ImInfo) must be 2. But "
70+
"received dimensions size=[%d], dimensions=[%s].",
71+
im_info_dims.size(), im_info_dims));
6872

6973
ctx->SetOutputDim("LocationIndex", {-1});
7074
ctx->SetOutputDim("ScoreIndex", {-1});
@@ -357,9 +361,15 @@ class RpnTargetAssignKernel : public framework::OpKernel<T> {
357361
auto* bbox_inside_weight = context.Output<LoDTensor>("BBoxInsideWeight");
358362

359363
PADDLE_ENFORCE_EQ(gt_boxes->lod().size(), 1UL,
360-
"RpnTargetAssignOp gt_boxes needs 1 level of LoD");
364+
platform::errors::InvalidArgument(
365+
"RpnTargetAssignOp gt_boxes needs 1 level of LoD. "
366+
"But received level of LoD is [%d], LoD is [%s].",
367+
gt_boxes->lod().size(), gt_boxes->lod()));
361368
PADDLE_ENFORCE_EQ(is_crowd->lod().size(), 1UL,
362-
"RpnTargetAssignOp is_crowd needs 1 level of LoD");
369+
platform::errors::InvalidArgument(
370+
"RpnTargetAssignOp is_crowd needs 1 level of LoD. "
371+
"But received level of LoD is [%d], LoD is [%s].",
372+
is_crowd->lod().size(), is_crowd->lod()));
363373
int64_t anchor_num = static_cast<int64_t>(anchor->dims()[0]);
364374
int64_t batch_num = static_cast<int64_t>(gt_boxes->lod().back().size() - 1);
365375

@@ -479,8 +489,20 @@ class RpnTargetAssignKernel : public framework::OpKernel<T> {
479489
lod0_score.emplace_back(total_score_num);
480490
}
481491

482-
PADDLE_ENFORCE_LE(total_loc_num, max_num);
483-
PADDLE_ENFORCE_LE(total_score_num, max_num);
492+
PADDLE_ENFORCE_LE(
493+
total_loc_num, max_num,
494+
platform::errors::InvalidArgument(
495+
"The number of sampled bboxes should not be greater than the "
496+
"number of all anchor boxes(%d), but the number of sampled "
497+
"bboxes is :%d.",
498+
max_num, total_loc_num));
499+
PADDLE_ENFORCE_LE(
500+
total_score_num, max_num,
501+
platform::errors::InvalidArgument(
502+
"The number of sampled scores should not be greater than the "
503+
"number of all anchor boxes(%d), but the number of sampled "
504+
"scores is :%d.",
505+
max_num, total_score_num));
484506

485507
lod_loc.emplace_back(lod0_loc);
486508
loc_score.emplace_back(lod0_score);

0 commit comments

Comments
 (0)