From 8414575b780268cc0a25a5101bc7419771e78e60 Mon Sep 17 00:00:00 2001 From: Chen Weihang Date: Wed, 13 Nov 2019 18:49:38 +0800 Subject: [PATCH] Add examples for error message writing specification - PreconditionNotMet, Unimplemented, Unavailable (#21137) * add examples for error spec, test=develop * change ENFORCE to ENFORCE_**, test=develop --- paddle/fluid/framework/data_feed.cc | 15 ++++++++++----- paddle/fluid/framework/data_feed_test.cc | 6 ++++-- paddle/fluid/framework/details/build_strategy.cc | 3 ++- .../details/fast_threaded_ssa_graph_executor.cc | 10 ++++++---- paddle/fluid/framework/executor_thread_worker.cc | 4 +++- .../fluid/framework/ir/fusion_group/operation.h | 3 ++- .../fuse_all_reduce_op_pass.cc | 10 ++++++---- paddle/fluid/inference/utils/benchmark.cc | 4 +++- paddle/fluid/operators/mul_op.cc | 2 +- 9 files changed, 37 insertions(+), 20 deletions(-) diff --git a/paddle/fluid/framework/data_feed.cc b/paddle/fluid/framework/data_feed.cc index e2869e5dedc..c4a33e0fb83 100644 --- a/paddle/fluid/framework/data_feed.cc +++ b/paddle/fluid/framework/data_feed.cc @@ -111,10 +111,13 @@ void DataFeed::SetBatchSize(int batch_size) { } bool DataFeed::PickOneFile(std::string* filename) { - PADDLE_ENFORCE(mutex_for_pick_file_ != nullptr, - "should call SetFileListMutex before PickOneFile"); - PADDLE_ENFORCE(file_idx_ != nullptr, - "should call SetFileListIndex before PickOneFile"); + PADDLE_ENFORCE_NOT_NULL( + mutex_for_pick_file_, + platform::errors::PreconditionNotMet( + "You should call SetFileListMutex before PickOneFile")); + PADDLE_ENFORCE_NOT_NULL( + file_idx_, platform::errors::PreconditionNotMet( + "You should call SetFileListIndex before PickOneFile")); std::unique_lock lock(*mutex_for_pick_file_); if (*file_idx_ == filelist_.size()) { VLOG(3) << "DataFeed::PickOneFile no more file to pick"; @@ -134,7 +137,9 @@ void DataFeed::CheckSetFileList() { } void DataFeed::CheckStart() { - PADDLE_ENFORCE(finish_start_, "Datafeed has not started running yet."); + PADDLE_ENFORCE_EQ(finish_start_, true, + platform::errors::PreconditionNotMet( + "Datafeed has not started running yet.")); } void DataFeed::AssignFeedVar(const Scope& scope) { diff --git a/paddle/fluid/framework/data_feed_test.cc b/paddle/fluid/framework/data_feed_test.cc index e1d62468621..7298ab4c9b7 100644 --- a/paddle/fluid/framework/data_feed_test.cc +++ b/paddle/fluid/framework/data_feed_test.cc @@ -34,7 +34,8 @@ paddle::framework::DataFeedDesc load_datafeed_param_from_file( const char* filename) { paddle::framework::DataFeedDesc data_feed_desc; int file_descriptor = open(filename, O_RDONLY); - PADDLE_ENFORCE(file_descriptor != -1, "Can not open %s.", filename); + PADDLE_ENFORCE_NE(file_descriptor, -1, platform::errors::Unavaliable( + "Cannot open file %s.", filename)); google::protobuf::io::FileInputStream fileInput(file_descriptor); google::protobuf::TextFormat::Parse(&fileInput, &data_feed_desc); close(file_descriptor); @@ -44,7 +45,8 @@ paddle::framework::DataFeedDesc load_datafeed_param_from_file( const std::vector load_filelist_from_file(const char* filename) { std::vector filelist; std::ifstream fin(filename); - PADDLE_ENFORCE(fin.good(), "Can not open %s.", filename); + PADDLE_ENFORCE_EQ(fin.good(), true, platform::errors::Unavaliable( + "Cannot open file %s.", filename)); std::string line; while (getline(fin, line)) { filelist.push_back(line); diff --git a/paddle/fluid/framework/details/build_strategy.cc b/paddle/fluid/framework/details/build_strategy.cc index 1f5fd015b16..c621905507e 100644 --- a/paddle/fluid/framework/details/build_strategy.cc +++ b/paddle/fluid/framework/details/build_strategy.cc @@ -272,7 +272,8 @@ class ParallelExecutorPassBuilder : public ir::PassBuilder { } #else PADDLE_ENFORCE_NE(FLAGS_use_ngraph, true, - "Please compile with NGRAPH first to use NGRAPH"); + platform::errors::PreconditionNotMet( + "Please compile with NGRAPH first to use NGRAPH")); #endif } diff --git a/paddle/fluid/framework/details/fast_threaded_ssa_graph_executor.cc b/paddle/fluid/framework/details/fast_threaded_ssa_graph_executor.cc index e9635d8003a..cab1966ecc1 100644 --- a/paddle/fluid/framework/details/fast_threaded_ssa_graph_executor.cc +++ b/paddle/fluid/framework/details/fast_threaded_ssa_graph_executor.cc @@ -139,10 +139,12 @@ void FastThreadedSSAGraphExecutor::InsertFetchOps( for (size_t i = 0; i < fetch_tensors.size(); ++i) { auto &var_name = fetch_tensors.at(i); auto fetched_var_it = fetched_vars->find(var_name); - PADDLE_ENFORCE(fetched_var_it != fetched_vars->end(), - "Cannot find fetched variable(%s).(Perhaps the main_program " - "is not set to ParallelExecutor)", - var_name); + PADDLE_ENFORCE_NE( + fetched_var_it, fetched_vars->end(), + platform::errors::PreconditionNotMet( + "Cannot find fetched variable(%s). Perhaps the main_program " + "is not set to ParallelExecutor.", + var_name)); auto &vars = fetched_var_it->second; diff --git a/paddle/fluid/framework/executor_thread_worker.cc b/paddle/fluid/framework/executor_thread_worker.cc index 005d98c6e8f..e8f3b57777b 100644 --- a/paddle/fluid/framework/executor_thread_worker.cc +++ b/paddle/fluid/framework/executor_thread_worker.cc @@ -143,7 +143,9 @@ void ExecutorThreadWorker::CreateThreadScope(const ProgramDesc& program) { auto& block = program.Block(0); PADDLE_ENFORCE_NOT_NULL( - root_scope_, "root_scope should be set before creating thread scope"); + root_scope_, + platform::errors::PreconditionNotMet( + "root_scope should be set before creating thread scope.")); thread_scope_ = &root_scope_->NewScope(); for (auto& var : block.AllVars()) { diff --git a/paddle/fluid/framework/ir/fusion_group/operation.h b/paddle/fluid/framework/ir/fusion_group/operation.h index c2f9a9dc759..cd738aa777f 100644 --- a/paddle/fluid/framework/ir/fusion_group/operation.h +++ b/paddle/fluid/framework/ir/fusion_group/operation.h @@ -76,7 +76,8 @@ class OperationMap { Operation& Get(std::string op_type) { auto iter = operations_.find(op_type); PADDLE_ENFORCE_NE(iter, operations_.end(), - "Operation %s is not supported yet.", op_type); + platform::errors::Unimplemented( + "Operation %s is not supported yet.", op_type)); return iter->second; } diff --git a/paddle/fluid/framework/ir/multi_devices_graph_pass/fuse_all_reduce_op_pass.cc b/paddle/fluid/framework/ir/multi_devices_graph_pass/fuse_all_reduce_op_pass.cc index 3f7ceb57958..9b7bd8cc82d 100644 --- a/paddle/fluid/framework/ir/multi_devices_graph_pass/fuse_all_reduce_op_pass.cc +++ b/paddle/fluid/framework/ir/multi_devices_graph_pass/fuse_all_reduce_op_pass.cc @@ -61,10 +61,12 @@ class FuseAllReduceOpPass : public ir::Pass { return; } - PADDLE_ENFORCE_EQ(all_reduce_ops.size(), grads.size(), - "The number of all_reduce OpHandle is not equal to the " - "number of grads. Maybe some gradients are sparse type, " - "it is not supported currently."); + PADDLE_ENFORCE_EQ( + all_reduce_ops.size(), grads.size(), + platform::errors::Unimplemented( + "The number of all_reduce OpHandle is not equal to the " + "number of grads. Maybe some gradients are sparse type, " + "it is not supported currently.")); auto &group_params_grads = graph->Get( details::kGroupParamsAndDenseGrads); diff --git a/paddle/fluid/inference/utils/benchmark.cc b/paddle/fluid/inference/utils/benchmark.cc index 0bd526bcac2..074a397e323 100644 --- a/paddle/fluid/inference/utils/benchmark.cc +++ b/paddle/fluid/inference/utils/benchmark.cc @@ -39,7 +39,9 @@ std::string Benchmark::SerializeToString() const { } void Benchmark::PersistToFile(const std::string &path) const { std::ofstream file(path, std::ios::app); - PADDLE_ENFORCE(file.is_open(), "Can not open %s to add benchmark", path); + PADDLE_ENFORCE_EQ( + file.is_open(), true, + platform::errors::Unavailable("Can not open %s to add benchmark.", path)); file << SerializeToString(); file.flush(); file.close(); diff --git a/paddle/fluid/operators/mul_op.cc b/paddle/fluid/operators/mul_op.cc index 45e85dd5b62..8d0898d7fe4 100644 --- a/paddle/fluid/operators/mul_op.cc +++ b/paddle/fluid/operators/mul_op.cc @@ -51,7 +51,7 @@ class MulOp : public framework::OperatorWithKernel { PADDLE_ENFORCE_NE(framework::product(y_dims), 0, platform::errors::PreconditionNotMet( - "Maybe the Input variable Y(%s) has not " + "The Input variable Y(%s) has not " "been initialized. You may need to confirm " "if you put exe.run(startup_program) " "after optimizer.minimize function.", -- GitLab