From 7e5f74b8252de73513ffefa6feb37f205610fa38 Mon Sep 17 00:00:00 2001 From: Chen Weihang Date: Wed, 13 Nov 2019 17:28:13 +0800 Subject: [PATCH] Add examples for error message writing specification - InvalidArgument (#21132) * add examples for error msg spec, test=develop * change ENFORCE to ENFORCE_**, test=develop * fix error, test=develop --- paddle/fluid/framework/async_executor.cc | 6 +++-- paddle/fluid/framework/data_feed.cc | 18 +++++++------- .../fluid/framework/data_layout_transform.cc | 8 +++++-- .../details/eager_deletion_op_handle.cc | 3 ++- paddle/fluid/framework/op_info.h | 5 ++-- paddle/fluid/framework/operator.cc | 24 +++++++++++-------- paddle/fluid/framework/tensor_impl.h | 9 ++++--- paddle/fluid/operators/reshape_op.cc | 19 ++++++++------- .../operators/sequence_ops/sequence_pool_op.h | 6 ++--- 9 files changed, 58 insertions(+), 40 deletions(-) diff --git a/paddle/fluid/framework/async_executor.cc b/paddle/fluid/framework/async_executor.cc index 7eb80a4617..3c5943daa6 100644 --- a/paddle/fluid/framework/async_executor.cc +++ b/paddle/fluid/framework/async_executor.cc @@ -86,8 +86,10 @@ void AsyncExecutor::RunFromFile(const ProgramDesc& main_program, DataFeedDesc data_feed_desc; bool success = data_feed_desc.ParseFromString(data_feed_desc_str); - PADDLE_ENFORCE(success, "Fail to parse DataFeedDesc from string:\n%s", - data_feed_desc_str.c_str()); + PADDLE_ENFORCE_EQ(success, true, + platform::errors::InvalidArgument( + "Fail to parse DataFeedDesc from string: %s.", + data_feed_desc_str.c_str())); actual_thread_num_ = thread_num; int file_cnt = filelist.size(); diff --git a/paddle/fluid/framework/data_feed.cc b/paddle/fluid/framework/data_feed.cc index bfeb29778e..e2869e5ded 100644 --- a/paddle/fluid/framework/data_feed.cc +++ b/paddle/fluid/framework/data_feed.cc @@ -105,7 +105,8 @@ bool DataFeed::SetFileList(const std::vector& files) { } void DataFeed::SetBatchSize(int batch_size) { - PADDLE_ENFORCE(batch_size > 0, "Illegal batch size: %d.", batch_size); + PADDLE_ENFORCE_GT(batch_size, 0, platform::errors::InvalidArgument( + "Batch size is illegal.", batch_size)); default_batch_size_ = batch_size; } @@ -556,13 +557,14 @@ bool MultiSlotDataFeed::ParseOneInstanceFromPipe( for (size_t i = 0; i < use_slots_index_.size(); ++i) { int idx = use_slots_index_[i]; int num = strtol(&str[pos], &endptr, 10); - PADDLE_ENFORCE( - num, - "The number of ids can not be zero, you need padding " - "it in data generator; or if there is something wrong with " - "the data, please check if the data contains unresolvable " - "characters.\nplease check this error line: %s", - str); + PADDLE_ENFORCE_NE( + num, 0, + platform::errors::InvalidArgument( + "The number of ids can not be zero, you need padding " + "it in data generator; or if there is something wrong with " + "the data, please check if the data contains unresolvable " + "characters.\nplease check this error line: %s", + str)); if (idx != -1) { (*instance)[idx].Init(all_slots_type_[i]); if ((*instance)[idx].GetType()[0] == 'f') { // float diff --git a/paddle/fluid/framework/data_layout_transform.cc b/paddle/fluid/framework/data_layout_transform.cc index fe53c6f99d..7fe8e9eb73 100644 --- a/paddle/fluid/framework/data_layout_transform.cc +++ b/paddle/fluid/framework/data_layout_transform.cc @@ -135,9 +135,13 @@ void innerTransDataLayoutFromMKLDNN(DataLayout in_layout, DataLayout out_layout, platform::Place place) { #ifdef PADDLE_WITH_MKLDNN PADDLE_ENFORCE_NE(in.format(), MKLDNNMemoryFormat::format_undef, - "Input tensor should have specified memory format"); + platform::errors::InvalidArgument( + "Input tensor format is invalid. Input tensor should " + "have specified memory format.")); PADDLE_ENFORCE_NE(in.format(), MKLDNNMemoryFormat::any, - "Input tensor should have specified memory format"); + platform::errors::InvalidArgument( + "Input tensor format is invalid. Input tensor should " + "have specified memory format.")); // Set default as NCHW in case not specified out_layout = diff --git a/paddle/fluid/framework/details/eager_deletion_op_handle.cc b/paddle/fluid/framework/details/eager_deletion_op_handle.cc index 2e64f9d4fb..780224d06b 100644 --- a/paddle/fluid/framework/details/eager_deletion_op_handle.cc +++ b/paddle/fluid/framework/details/eager_deletion_op_handle.cc @@ -50,7 +50,8 @@ EagerDeletionOpHandle::EagerDeletionOpHandle( } } #endif - PADDLE_ENFORCE(!vars.empty(), "Var names cannot be empty"); + PADDLE_ENFORCE_NE(vars.empty(), true, platform::errors::InvalidArgument( + "Variable names are empty.")); for (auto *var : var_infos_) { PADDLE_ENFORCE_NOT_NULL(var); } diff --git a/paddle/fluid/framework/op_info.h b/paddle/fluid/framework/op_info.h index 79a6a06abb..778739419c 100644 --- a/paddle/fluid/framework/op_info.h +++ b/paddle/fluid/framework/op_info.h @@ -54,8 +54,9 @@ struct OpInfo { const proto::OpProto& Proto() const { PADDLE_ENFORCE_NOT_NULL(proto_, "Operator's Proto has not been registered"); - PADDLE_ENFORCE(proto_->IsInitialized(), - "Operator's Proto must be initialized in op info"); + PADDLE_ENFORCE_EQ(proto_->IsInitialized(), true, + platform::errors::InvalidArgument( + "Operator's Proto in op info is not initialized.")); return *proto_; } diff --git a/paddle/fluid/framework/operator.cc b/paddle/fluid/framework/operator.cc index 424a8fb7d5..2bb27ee7e3 100644 --- a/paddle/fluid/framework/operator.cc +++ b/paddle/fluid/framework/operator.cc @@ -1147,17 +1147,21 @@ void OperatorWithKernel::ParseInputDataType( t = &(var->Get().value()); } if (t != nullptr) { - PADDLE_ENFORCE_EQ(t->IsInitialized(), true, - "The Tensor in the %s Op's Input Variable %s(%s) is " - "not initialized.", - Type(), name, ctx.Inputs(name).at(i)); + PADDLE_ENFORCE_EQ( + t->IsInitialized(), true, + platform::errors::InvalidArgument( + "The Tensor in the %s Op's Input Variable %s(%s) is " + "not initialized.", + Type(), name, ctx.Inputs(name).at(i))); proto::VarType::Type tmp = t->type(); - PADDLE_ENFORCE(tmp == *data_type || *data_type == dafault_data_type, - "The DataType of %s Op's duplicable Variable %s must be " - "consistent. The current variable type is (%s), but the " - "previous variable type is (%s).", - Type(), name, DataTypeToString(tmp), - DataTypeToString(*data_type)); + PADDLE_ENFORCE( + tmp == *data_type || *data_type == dafault_data_type, + platform::errors::InvalidArgument( + "The DataType of %s Op's duplicable Variable %s must be " + "consistent. The current variable type is (%s), but the " + "previous variable type is (%s).", + Type(), name, DataTypeToString(tmp), + DataTypeToString(*data_type))); *data_type = tmp; } } diff --git a/paddle/fluid/framework/tensor_impl.h b/paddle/fluid/framework/tensor_impl.h index a5c39b7e92..34dd3c59ff 100644 --- a/paddle/fluid/framework/tensor_impl.h +++ b/paddle/fluid/framework/tensor_impl.h @@ -25,9 +25,12 @@ inline const T* Tensor::data() const { check_memory_size(); bool valid = std::is_same::value || type_ == DataTypeTrait::DataType(); - PADDLE_ENFORCE( - valid, "Tensor holds the wrong type, it holds %s, but desires to be %s", - DataTypeToString(type_), DataTypeToString(DataTypeTrait::DataType())); + PADDLE_ENFORCE_EQ( + valid, true, + platform::errors::InvalidArgument( + "Tensor holds the wrong type, it holds %s, but desires to be %s.", + DataTypeToString(type_), + DataTypeToString(DataTypeTrait::DataType()))); return reinterpret_cast( reinterpret_cast(holder_->ptr()) + offset_); diff --git a/paddle/fluid/operators/reshape_op.cc b/paddle/fluid/operators/reshape_op.cc index df4fbb279f..9c4cb63537 100644 --- a/paddle/fluid/operators/reshape_op.cc +++ b/paddle/fluid/operators/reshape_op.cc @@ -173,15 +173,16 @@ class ReshapeOp : public framework::OperatorWithKernel { // capacity = -24, in_size = -8, output_shape[0] = 0 // the following check will fail. output_shape[unk_dim_idx] = -in_size / capacity; - PADDLE_ENFORCE_EQ(output_shape[unk_dim_idx] * capacity, -in_size, - "ShapeError: The 'shape' in ReshapeOp is invalid. " - "The input tensor X'size must be divisible by known " - "capacity of 'shape'. " - "But received X's shape = [%s], X's size = %d, " - "'shape' is [%s], known " - "capacity of 'shape' is %d.", - in_dims, in_size, framework::make_ddim(shape), - capacity); + PADDLE_ENFORCE_EQ( + output_shape[unk_dim_idx] * capacity, -in_size, + platform::errors::InvalidArgument( + "The 'shape' attribute in ReshapeOp is invalid. " + "The input tensor X'size must be divisible by known " + "capacity of 'shape'. " + "But received X's shape = [%s], X's size = %d, " + "'shape' is [%s], known " + "capacity of 'shape' is %d.", + in_dims, in_size, framework::make_ddim(shape), capacity)); } else { output_shape[unk_dim_idx] = -1; } diff --git a/paddle/fluid/operators/sequence_ops/sequence_pool_op.h b/paddle/fluid/operators/sequence_ops/sequence_pool_op.h index 2d924585b4..8fe68deca6 100644 --- a/paddle/fluid/operators/sequence_ops/sequence_pool_op.h +++ b/paddle/fluid/operators/sequence_ops/sequence_pool_op.h @@ -38,9 +38,9 @@ class SequencePoolKernel : public framework::OpKernel { auto lod = in->lod(); auto lod_level = lod.size(); // InferShape by lod - PADDLE_ENFORCE_GT( - lod_level, 0, - "Input(X) Tensor of SequencePoolOp does not contain LoD information."); + PADDLE_ENFORCE_GT(lod_level, 0, platform::errors::InvalidArgument( + "Input(X) Tensor of SequencePoolOp " + "does not contain LoD information.")); PADDLE_ENFORCE_LE(lod_level, 2UL, "The lod level of input shall be no more than 2."); PADDLE_ENFORCE_GE( -- GitLab