From 9d783aeddd86cc95508ef4d6a7e0982b90be31d3 Mon Sep 17 00:00:00 2001 From: Double_V Date: Sun, 27 Sep 2020 21:30:34 +0800 Subject: [PATCH] Error message opt, test=develop (#27467) * Error message opt, test=develop * solve comments, test=develop * fix typo, test=develop --- paddle/fluid/operators/center_loss_op.cu | 11 +-- paddle/fluid/operators/ctc_align_op.cu | 6 +- paddle/fluid/operators/ctc_align_op.h | 7 +- paddle/fluid/operators/pool_cudnn_op.cu.cc | 12 ++-- paddle/fluid/operators/pool_op.cc | 72 ++++++++++++-------- paddle/fluid/operators/pool_op.h | 18 +++-- paddle/fluid/operators/pool_with_index_op.cc | 26 ++++--- paddle/fluid/operators/pool_with_index_op.h | 10 ++- paddle/fluid/operators/psroi_pool_op.cu | 25 ++++--- paddle/fluid/operators/roi_pool_op.cu | 20 +++++- 10 files changed, 138 insertions(+), 69 deletions(-) diff --git a/paddle/fluid/operators/center_loss_op.cu b/paddle/fluid/operators/center_loss_op.cu index 10b65fa215a..f15d1fe5e02 100644 --- a/paddle/fluid/operators/center_loss_op.cu +++ b/paddle/fluid/operators/center_loss_op.cu @@ -30,8 +30,10 @@ __global__ void ComputeDifferent(T *centers_diff, const T *X, const T *centers, while (idy < K) { int64_t id = ids[idy]; - PADDLE_ENFORCE(id >= 0, "received id:", id); - PADDLE_ENFORCE(id < N, "received id:", id); + PADDLE_ENFORCE(id >= 0, "Id should larger than 0 but received id: %d.", id); + PADDLE_ENFORCE(id < N, "Id should smaller than %d but received id: %d.", N, + id); + T *out = centers_diff + idy * D; const T *x = X + idy * D; const T *cent = centers + id * D; @@ -52,8 +54,9 @@ __global__ void UpdateCenters(T *centers, T *centers_diff, const int64_t *ids, while (idy < K) { int count = 1; int64_t id = ids[idy]; - PADDLE_ENFORCE(id >= 0, "received id:", id); - PADDLE_ENFORCE(id < N, "received id:", id); + PADDLE_ENFORCE(id >= 0, "Id should larger than 0 but received id: %d.", id); + PADDLE_ENFORCE(id < N, "Id should smaller than %d but received id: %d.", N, + id); for (int i = 0; i < K; i++) { if (ids[i] == id) { diff --git a/paddle/fluid/operators/ctc_align_op.cu b/paddle/fluid/operators/ctc_align_op.cu index 44a7c16f96a..67bd71d4a1b 100644 --- a/paddle/fluid/operators/ctc_align_op.cu +++ b/paddle/fluid/operators/ctc_align_op.cu @@ -69,8 +69,10 @@ template class CTCAlignOpCUDAKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { - PADDLE_ENFORCE(platform::is_gpu_place(ctx.GetPlace()), - "It must use CUDAPlace."); + PADDLE_ENFORCE_EQ(platform::is_gpu_place(ctx.GetPlace()), true, + platform::errors::InvalidArgument( + "CTCAlign operator CUDA kernel must use CUDAPlace " + "rather than CPUPlace.")); auto* input = ctx.Input("Input"); auto* output = ctx.Output("Output"); const int blank = ctx.Attr("blank"); diff --git a/paddle/fluid/operators/ctc_align_op.h b/paddle/fluid/operators/ctc_align_op.h index ccf91471ab9..662f899c0a5 100644 --- a/paddle/fluid/operators/ctc_align_op.h +++ b/paddle/fluid/operators/ctc_align_op.h @@ -72,8 +72,11 @@ class CTCAlignKernel : public framework::OpKernel { // check input dims and lod PADDLE_ENFORCE_EQ( input_dims[0], static_cast(input_lod[level].back()), - "The first dimension of Input(Input) should be equal to " - "the sum of all sequences' lengths."); + platform::errors::InvalidArgument( + "The first dimension %d of CTCAlign operator Input(Input) should " + "be equal to " + "the sum of all sequences' lengths %d.", + input_dims[0], static_cast(input_lod[level].back()))); const size_t num_sequences = input_lod[level].size() - 1; diff --git a/paddle/fluid/operators/pool_cudnn_op.cu.cc b/paddle/fluid/operators/pool_cudnn_op.cu.cc index 9317a018333..3dc184facc7 100644 --- a/paddle/fluid/operators/pool_cudnn_op.cu.cc +++ b/paddle/fluid/operators/pool_cudnn_op.cu.cc @@ -45,8 +45,10 @@ template class PoolCUDNNOpKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext &ctx) const override { - PADDLE_ENFORCE_EQ(platform::is_gpu_place(ctx.GetPlace()), true, - "It must use CUDAPlace."); + PADDLE_ENFORCE_EQ( + platform::is_gpu_place(ctx.GetPlace()), true, + platform::errors::InvalidArgument("Pool operator CUDA kernel must use " + "CUDAPlace rather than CPUPlace.")); const Tensor *input = ctx.Input("X"); Tensor *output = ctx.Output("Out"); @@ -175,8 +177,10 @@ template class PoolCUDNNGradOpKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext &ctx) const override { - PADDLE_ENFORCE_EQ(platform::is_gpu_place(ctx.GetPlace()), true, - "It must use CUDAPlace."); + PADDLE_ENFORCE_EQ( + platform::is_gpu_place(ctx.GetPlace()), true, + platform::errors::InvalidArgument("Pool operator CUDA kernel must use " + "CUDAPlace rather than CPUPlace.")); const Tensor *input = ctx.Input("X"); const Tensor *output = ctx.Input("Out"); diff --git a/paddle/fluid/operators/pool_op.cc b/paddle/fluid/operators/pool_op.cc index 9900120e6c5..ba468b79605 100644 --- a/paddle/fluid/operators/pool_op.cc +++ b/paddle/fluid/operators/pool_op.cc @@ -38,18 +38,22 @@ int PoolOutputSize(int input_size, int filter_size, int padding_1, } PADDLE_ENFORCE_GT( output_size, 0, - "ShapeError: the output size must be greater than 0. But received: " - "output_size = %d due to the settings of input_size(%d), padding(%d,%d), " - "k_size(%d) and stride(%d). Please check again!", - output_size, input_size, padding_1, padding_2, filter_size, stride); + platform::errors::InvalidArgument( + "the output size must be greater than 0. But received: " + "output_size = %d due to the settings of input_size(%d), " + "padding(%d,%d), " + "k_size(%d) and stride(%d). Please check again!", + output_size, input_size, padding_1, padding_2, filter_size, stride)); return output_size; } void PoolOp::InferShape(framework::InferShapeContext* ctx) const { - PADDLE_ENFORCE_EQ(ctx->HasInput("X"), true, - "X(Input) of Pooling should not be null."); - PADDLE_ENFORCE_EQ(ctx->HasOutput("Out"), true, - "Out(Output) of Pooling should not be null."); + PADDLE_ENFORCE_EQ( + ctx->HasInput("X"), true, + platform::errors::NotFound("Input(X) of Pool operator is not found.")); + PADDLE_ENFORCE_EQ( + ctx->HasOutput("Out"), true, + platform::errors::NotFound("Output(Out) of Pool operator is not found.")); std::string pooling_type = ctx->Attrs().Get("pooling_type"); std::vector ksize = ctx->Attrs().Get>("ksize"); @@ -65,28 +69,32 @@ void PoolOp::InferShape(framework::InferShapeContext* ctx) const { auto in_x_dims = ctx->GetInputDim("X"); PADDLE_ENFORCE_EQ( in_x_dims.size() == 4 || in_x_dims.size() == 5, true, - "ShapeError: the input of Op(pool) should be 4-D or 5-D Tensor. But " - "received: %u-D Tensor and it's shape is [%s].", - in_x_dims.size(), in_x_dims); + platform::errors::InvalidArgument( + "the input of Op(pool) should be 4-D or 5-D Tensor. But " + "received: %u-D Tensor and it's shape is [%s].", + in_x_dims.size(), in_x_dims)); PADDLE_ENFORCE_EQ( in_x_dims.size() - ksize.size(), 2U, - "ShapeError: the dimension of input minus the size of " - "Attr(ksize) must be euqal to 2 in Op(pool). " - "But received: the dimension of input minus the size " - "of Attr(ksize) is %d, the " - "input's dimension is %d, the shape of input " - "is [%s], the Attr(ksize)'s size is %d, the Attr(ksize) is [%s].", - in_x_dims.size() - ksize.size(), in_x_dims.size(), in_x_dims, - ksize.size(), framework::make_ddim(ksize)); - - PADDLE_ENFORCE_EQ(ksize.size(), strides.size(), - "ShapeError: the size of Attr(ksize) and Attr(strides) in " - "Op(pool) must be equal. " - "But received: Attr(ksize)'s size is %d, Attr(strides)'s " - "size is %d, Attr(ksize) is [%s], Attr(strides)is [%s].", - ksize.size(), strides.size(), framework::make_ddim(ksize), - framework::make_ddim(strides)); + platform::errors::InvalidArgument( + "the dimension of input minus the size of " + "Attr(ksize) must be euqal to 2 in Op(pool). " + "But received: the dimension of input minus the size " + "of Attr(ksize) is %d, the " + "input's dimension is %d, the shape of input " + "is [%s], the Attr(ksize)'s size is %d, the Attr(ksize) is [%s].", + in_x_dims.size() - ksize.size(), in_x_dims.size(), in_x_dims, + ksize.size(), framework::make_ddim(ksize))); + + PADDLE_ENFORCE_EQ( + ksize.size(), strides.size(), + platform::errors::InvalidArgument( + "the size of Attr(ksize) and Attr(strides) in " + "Op(pool) must be equal. " + "But received: Attr(ksize)'s size is %d, Attr(strides)'s " + "size is %d, Attr(ksize) is [%s], Attr(strides)is [%s].", + ksize.size(), strides.size(), framework::make_ddim(ksize), + framework::make_ddim(strides))); // MKL-DNN Kernels are using NCHW order of dims description // so we ignore data_format consideration for MKL-DNN kernel @@ -182,9 +190,12 @@ framework::OpKernelType PoolOp::GetKernelTypeForVar( } void PoolOpGrad::InferShape(framework::InferShapeContext* ctx) const { - PADDLE_ENFORCE_EQ(ctx->HasInput("X"), true, "Input(X) must not be null."); + PADDLE_ENFORCE_EQ(ctx->HasInput("X"), true, + platform::errors::NotFound( + "Input(X) of Pool Gradoperator is not found.")); PADDLE_ENFORCE_EQ(ctx->HasOutput(framework::GradVarName("X")), true, - "Input(X@GRAD) should not be null."); + platform::errors::NotFound( + "Input(X@GRAD) of Pool Gradoperator is not found.")); ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X")); } @@ -210,7 +221,8 @@ framework::OpKernelType PoolOpGrad::GetExpectedKernelType( auto input_data_type = OperatorWithKernel::IndicateVarDataType(ctx, "X"); if (input_data_type == framework::proto::VarType::FP16) { PADDLE_ENFORCE_EQ(library_, framework::LibraryType::kCUDNN, - "float16 can only be used when CUDNN is used"); + platform::errors::InvalidArgument( + "Float16 can only be used when CUDNN is used")); } return framework::OpKernelType(input_data_type, ctx.GetPlace(), layout_, library_); diff --git a/paddle/fluid/operators/pool_op.h b/paddle/fluid/operators/pool_op.h index 48fb6793d2a..677c724069c 100644 --- a/paddle/fluid/operators/pool_op.h +++ b/paddle/fluid/operators/pool_op.h @@ -81,9 +81,11 @@ inline void UpdatePadding(std::vector* paddings, const bool global_pooling, paddings->insert(paddings->begin() + 2 * i + 1, copy_pad); } } else { - PADDLE_ENFORCE_EQ( - data_dims.size() * 2, paddings->size(), - "Paddings size should be the same or twice as the pooling size."); + PADDLE_ENFORCE_EQ(data_dims.size() * 2, paddings->size(), + platform::errors::InvalidArgument( + "Paddings size %d should be the same or twice as the " + "pooling size %d.", + paddings->size(), data_dims.size() * 2)); } // when padding_algorithm is "VALID" or "SAME" @@ -200,7 +202,10 @@ class PoolKernel : public framework::OpKernel { pool_process, exclusive, adaptive, out); } } break; - default: { PADDLE_THROW("Pool op only supports 2D and 3D input."); } + default: { + PADDLE_THROW(platform::errors::InvalidArgument( + "Pool op only supports 2D and 3D input.")); + } } } }; @@ -287,7 +292,10 @@ class PoolGradKernel : public framework::OpKernel { adaptive, in_x_grad); } } break; - default: { PADDLE_THROW("Pool op only supports 2D and 3D input."); } + default: { + PADDLE_THROW(platform::errors::InvalidArgument( + "Pool op only supports 2D and 3D input.")); + } } } } diff --git a/paddle/fluid/operators/pool_with_index_op.cc b/paddle/fluid/operators/pool_with_index_op.cc index 0371ea5b09b..3e44025e5b0 100644 --- a/paddle/fluid/operators/pool_with_index_op.cc +++ b/paddle/fluid/operators/pool_with_index_op.cc @@ -46,8 +46,11 @@ class MaxPoolWithIndexOp : public framework::OperatorWithKernel { std::vector paddings = ctx->Attrs().Get>("paddings"); bool adaptive = ctx->Attrs().Get("adaptive"); - PADDLE_ENFORCE(in_x_dims.size() == 4 || in_x_dims.size() == 5, - "Pooling intput should be 4-D or 5-D tensor."); + PADDLE_ENFORCE( + in_x_dims.size() == 4 || in_x_dims.size() == 5, + platform::errors::InvalidArgument("Pooling intput should be 4-D or 5-D " + "tensor but received %dD-Tensor", + in_x_dims.size())); if (ctx->Attrs().Get("global_pooling")) { ksize.resize(static_cast(in_x_dims.size()) - 2); @@ -57,16 +60,21 @@ class MaxPoolWithIndexOp : public framework::OperatorWithKernel { } } - PADDLE_ENFORCE_EQ(in_x_dims.size() - ksize.size(), 2U, - platform::errors::InvalidArgument( - "Input size and pooling size should be consistent.")); - PADDLE_ENFORCE_EQ(ksize.size(), strides.size(), - platform::errors::InvalidArgument( - "Strides size and pooling size should be the same.")); + PADDLE_ENFORCE_EQ( + in_x_dims.size() - ksize.size(), 2U, + platform::errors::InvalidArgument( + "The input size %d minus the kernel size %d should equal to 2.", + in_x_dims.size(), ksize.size())); + PADDLE_ENFORCE_EQ( + ksize.size(), strides.size(), + platform::errors::InvalidArgument( + "Strides size %d and pooling size %d should be the same.", + strides.size(), ksize.size())); PADDLE_ENFORCE_EQ( ksize.size(), paddings.size(), platform::errors::InvalidArgument( - "Paddings size and pooling size should be the same.")); + "Paddings size %d and pooling size %d should be the same.", + paddings.size(), ksize.size())); std::vector output_shape({in_x_dims[0], in_x_dims[1]}); if (adaptive) { diff --git a/paddle/fluid/operators/pool_with_index_op.h b/paddle/fluid/operators/pool_with_index_op.h index a6bec121d4f..065d90704cf 100644 --- a/paddle/fluid/operators/pool_with_index_op.h +++ b/paddle/fluid/operators/pool_with_index_op.h @@ -61,7 +61,10 @@ class MaxPoolWithIndexKernel : public framework::OpKernel { pool3d_forward(dev_ctx, *in_x, ksize, strides, paddings, adaptive, out, mask); } break; - default: { PADDLE_THROW("Pool op only supports 2D and 3D input."); } + default: { + PADDLE_THROW(platform::errors::InvalidArgument( + "Pool op only supports 2D and 3D input.")); + } } } }; @@ -106,7 +109,10 @@ class MaxPoolWithIndexGradKernel : public framework::OpKernel { pool3d_backward(device_ctx, *out_grad, *mask, ksize, strides, paddings, adaptive, in_x_grad); } break; - default: { PADDLE_THROW("Pool op only supports 2D and 3D input."); } + default: { + PADDLE_THROW(platform::errors::InvalidArgument( + "Pool op only supports 2D and 3D input.")); + } } } } diff --git a/paddle/fluid/operators/psroi_pool_op.cu b/paddle/fluid/operators/psroi_pool_op.cu index 22fec3244fa..748b6036008 100644 --- a/paddle/fluid/operators/psroi_pool_op.cu +++ b/paddle/fluid/operators/psroi_pool_op.cu @@ -176,22 +176,31 @@ class GPUPSROIPoolOpKernel : public framework::OpKernel { int height = in_dims[2]; int width = in_dims[3]; - PADDLE_ENFORCE_EQ(input_channels, - output_channels * pooled_height * pooled_width, - "the channels of input X should equal the product of " - "output_channels x pooled_height x pooled_width"); + PADDLE_ENFORCE_EQ( + input_channels, output_channels * pooled_height * pooled_width, + platform::errors::InvalidArgument( + "The channels %d of input X should equal the product of " + "output_channels %d x pooled_height %d x pooled_width %d.", + input_channels, output_channels, pooled_height, pooled_width)); int rois_num = rois->dims()[0]; if (rois_num == 0) return; auto rois_lod = rois->lod().back(); int rois_batch_size = rois_lod.size() - 1; - PADDLE_ENFORCE_EQ( - rois_batch_size, batch_size, - "The rois_batch_size and input(X) batch_size must be the same."); + PADDLE_ENFORCE_EQ(rois_batch_size, batch_size, + platform::errors::InvalidArgument( + "The batch size of input(ROIs) and input(X) must be " + "the same but received batch size of input(ROIs) and " + "input(X) is %d and %d respectively.", + rois_batch_size, batch_size)); int rois_num_with_lod = rois_lod[rois_batch_size]; PADDLE_ENFORCE_EQ(rois_num, rois_num_with_lod, - "The rois_num from input and lod must be the same."); + platform::errors::InvalidArgument( + "The number of rois from input(ROIs) and its LOD " + "must be the same. Received rois %d of input(ROIs) " + "but the number of rois %d from its LOD is %d", + rois_num, rois_num_with_lod)); // set rois batch id framework::Tensor rois_batch_id_list; diff --git a/paddle/fluid/operators/roi_pool_op.cu b/paddle/fluid/operators/roi_pool_op.cu index 98d9ef6b6e1..562ff8d576b 100644 --- a/paddle/fluid/operators/roi_pool_op.cu +++ b/paddle/fluid/operators/roi_pool_op.cu @@ -160,9 +160,14 @@ class GPUROIPoolOpKernel : public framework::OpKernel { if (ctx.HasInput("RoisNum")) { auto* rois_num_t = ctx.Input("RoisNum"); int rois_batch_size = rois_num_t->numel(); + PADDLE_ENFORCE_EQ( rois_batch_size, batch_size, - "The rois_batch_size and imgs batch_size must be the same."); + platform::errors::InvalidArgument( + "The batch size of input(ROIs) and input(X) must be the same but " + "received batch size of input(ROIs) and input(X) is %d and %d " + "respectively.", + rois_batch_size, batch_size)); std::vector rois_num_list(rois_batch_size); memory::Copy(cplace, rois_num_list.data(), gplace, rois_num_t->data(), sizeof(int) * rois_batch_size, 0); @@ -178,10 +183,19 @@ class GPUROIPoolOpKernel : public framework::OpKernel { int rois_batch_size = rois_lod.size() - 1; PADDLE_ENFORCE_EQ( rois_batch_size, batch_size, - "The rois_batch_size and imgs batch_size must be the same."); + platform::errors::InvalidArgument( + "The batch size of input(ROIs) and input(X) must be the same but " + "received batch size of input(ROIs) and input(X) is %d and %d " + "respectively.", + rois_batch_size, batch_size)); + int rois_num_with_lod = rois_lod[rois_batch_size]; PADDLE_ENFORCE_EQ(rois_num, rois_num_with_lod, - "The rois_num from input and lod must be the same."); + platform::errors::InvalidArgument( + "The number of rois from input(ROIs) and its LOD " + "must be the same. Received rois %d of input(ROIs) " + "but the number of rois %d from its LOD is %d", + rois_num, rois_num_with_lod)); for (int n = 0; n < rois_batch_size; ++n) { for (size_t i = rois_lod[n]; i < rois_lod[n + 1]; ++i) { roi_batch_id_data[i] = n; -- GitLab