未验证 提交 8da0cd53 编写于 作者: C Chen Weihang 提交者: GitHub

Add examples for error message writing specification - NotFound, OutOfRange,...

Add examples for error message writing specification - NotFound, OutOfRange, AlreadyExists, PermissionDenied (#21134)

* add examples for error msg spec, test=develop

* change ENFORCE to ENFORCE_**, test=develop

* add more already exists examples, test=develop
上级 b93870e6
...@@ -55,7 +55,8 @@ class Array { ...@@ -55,7 +55,8 @@ class Array {
HOSTDEVICE inline T &at(size_t i) { HOSTDEVICE inline T &at(size_t i) {
#ifndef __CUDA_ARCH__ #ifndef __CUDA_ARCH__
PADDLE_ENFORCE_LT(i, N, "Array index out of bounds"); PADDLE_ENFORCE_LT(
i, N, platform::errors::OutOfRange("Array index out of bounds."));
#endif #endif
return (*this)[i]; return (*this)[i];
} }
......
...@@ -76,7 +76,8 @@ void AsyncExecutor::RunFromFile(const ProgramDesc& main_program, ...@@ -76,7 +76,8 @@ void AsyncExecutor::RunFromFile(const ProgramDesc& main_program,
auto& block = main_program.Block(0); auto& block = main_program.Block(0);
for (auto var_name : fetch_var_names) { for (auto var_name : fetch_var_names) {
auto var_desc = block.FindVar(var_name); auto var_desc = block.FindVar(var_name);
PADDLE_ENFORCE_NOT_NULL(var_desc, "%s is not found.", var_name); PADDLE_ENFORCE_NOT_NULL(
var_desc, platform::errors::NotFound("%s is not found.", var_name));
auto shapes = var_desc->GetShape(); auto shapes = var_desc->GetShape();
PADDLE_ENFORCE(shapes[shapes.size() - 1] == 1, PADDLE_ENFORCE(shapes[shapes.size() - 1] == 1,
"var %s: Fetched var has wrong shape, " "var %s: Fetched var has wrong shape, "
...@@ -93,7 +94,8 @@ void AsyncExecutor::RunFromFile(const ProgramDesc& main_program, ...@@ -93,7 +94,8 @@ void AsyncExecutor::RunFromFile(const ProgramDesc& main_program,
actual_thread_num_ = thread_num; actual_thread_num_ = thread_num;
int file_cnt = filelist.size(); int file_cnt = filelist.size();
PADDLE_ENFORCE(file_cnt > 0, "File list cannot be empty"); PADDLE_ENFORCE_GT(file_cnt, 0,
platform::errors::NotFound("Input file list is empty"));
if (actual_thread_num_ > file_cnt) { if (actual_thread_num_ > file_cnt) {
VLOG(1) << "Thread num = " << thread_num << ", file num = " << file_cnt VLOG(1) << "Thread num = " << thread_num << ", file num = " << file_cnt
......
...@@ -192,7 +192,8 @@ class GreaterThanChecker { ...@@ -192,7 +192,8 @@ class GreaterThanChecker {
public: public:
explicit GreaterThanChecker(T lower_bound) : lower_bound_(lower_bound) {} explicit GreaterThanChecker(T lower_bound) : lower_bound_(lower_bound) {}
void operator()(const T& value) const { void operator()(const T& value) const {
PADDLE_ENFORCE(value > lower_bound_, "larger_than check fails."); PADDLE_ENFORCE_GT(value, lower_bound_,
platform::errors::OutOfRange("larger_than check fails."));
} }
private: private:
......
...@@ -94,8 +94,9 @@ void FuseOptimizerOpPass::ApplyImpl(ir::Graph *graph) const { ...@@ -94,8 +94,9 @@ void FuseOptimizerOpPass::ApplyImpl(ir::Graph *graph) const {
auto fused_var_name = prefix + "_" + fuse_op_type + "_" + var_name + "_" + auto fused_var_name = prefix + "_" + fuse_op_type + "_" + var_name + "_" +
aux_var_map[var_name][0]; aux_var_map[var_name][0];
VLOG(6) << var_name << ": " << fused_var_name; VLOG(6) << var_name << ": " << fused_var_name;
PADDLE_ENFORCE_EQ(fused_var_set.count(fused_var_name), 0, PADDLE_ENFORCE_EQ(
"The fused variable already existed."); fused_var_set.count(fused_var_name), 0,
platform::errors::AlreadyExists("The fused variable already exists."));
fused_var_set.insert(fused_var_name); fused_var_set.insert(fused_var_name);
fused_vars_name.emplace(var_name, fused_var_name); fused_vars_name.emplace(var_name, fused_var_name);
} }
......
...@@ -109,8 +109,10 @@ class Graph { ...@@ -109,8 +109,10 @@ class Graph {
template <typename AttrType> template <typename AttrType>
void Set(const std::string &attr_name, AttrType *attr) { void Set(const std::string &attr_name, AttrType *attr) {
PADDLE_ENFORCE_EQ(attrs_.count(attr_name), 0, "%s already set in the graph", PADDLE_ENFORCE_EQ(
attr_name); attrs_.count(attr_name), 0,
platform::errors::AlreadyExists(
"The attribute %s has been set in the graph.", attr_name));
attrs_[attr_name] = attr; attrs_[attr_name] = attr;
attr_dels_[attr_name] = [attr, attr_name]() { attr_dels_[attr_name] = [attr, attr_name]() {
VLOG(3) << "deleting " << attr_name; VLOG(3) << "deleting " << attr_name;
...@@ -120,15 +122,19 @@ class Graph { ...@@ -120,15 +122,19 @@ class Graph {
template <typename AttrType> template <typename AttrType>
void SetNotOwned(const std::string &attr_name, AttrType *attr) { void SetNotOwned(const std::string &attr_name, AttrType *attr) {
PADDLE_ENFORCE_EQ(attrs_.count(attr_name), 0, "%s already set in the graph", PADDLE_ENFORCE_EQ(
attr_name); attrs_.count(attr_name), 0,
platform::errors::AlreadyExists(
"The attribute %s has been set in the graph.", attr_name));
attrs_[attr_name] = attr; attrs_[attr_name] = attr;
attr_dels_[attr_name] = []() {}; attr_dels_[attr_name] = []() {};
} }
void Erase(const std::string &attr_name) { void Erase(const std::string &attr_name) {
PADDLE_ENFORCE_NE(attrs_.count(attr_name), 0, "%s not set in the graph", PADDLE_ENFORCE_NE(
attr_name); attrs_.count(attr_name), 0,
platform::errors::NotFound(
"The attribute %s has not been set in the graph.", attr_name));
attr_dels_[attr_name](); attr_dels_[attr_name]();
attrs_.erase(attr_name); attrs_.erase(attr_name);
attr_dels_.erase(attr_name); attr_dels_.erase(attr_name);
......
...@@ -77,7 +77,8 @@ PDNode *PDPattern::RetrieveNode(const std::string &id) const { ...@@ -77,7 +77,8 @@ PDNode *PDPattern::RetrieveNode(const std::string &id) const {
void PDPattern::AddEdge(PDNode *a, PDNode *b) { void PDPattern::AddEdge(PDNode *a, PDNode *b) {
PADDLE_ENFORCE(a); PADDLE_ENFORCE(a);
PADDLE_ENFORCE(b); PADDLE_ENFORCE(b);
PADDLE_ENFORCE(a != b, "can't connect to the same nodes."); PADDLE_ENFORCE_NE(a, b, platform::errors::PermissionDenied(
"Cannot connect the same node in the graph."));
edges_.emplace_back(a, b); edges_.emplace_back(a, b);
} }
......
...@@ -159,7 +159,9 @@ class PassRegistry { ...@@ -159,7 +159,9 @@ class PassRegistry {
} }
void Insert(const std::string &pass_type, const PassCreator &pass_creator) { void Insert(const std::string &pass_type, const PassCreator &pass_creator) {
PADDLE_ENFORCE(!Has(pass_type), "Pass %s has been registered", pass_type); PADDLE_ENFORCE_NE(Has(pass_type), true,
platform::errors::AlreadyExists(
"Pass %s has been registered.", pass_type));
map_.insert({pass_type, pass_creator}); map_.insert({pass_type, pass_creator});
} }
......
...@@ -281,7 +281,8 @@ static int BuildFusion(Graph* graph, const std::string& name_scope, ...@@ -281,7 +281,8 @@ static int BuildFusion(Graph* graph, const std::string& name_scope,
PADDLE_ENFORCE(subgraph.count(pat.RetrieveNode(name)), PADDLE_ENFORCE(subgraph.count(pat.RetrieveNode(name)),
"pattern has no Node called %s", name.c_str()); "pattern has no Node called %s", name.c_str());
Node* p = subgraph.at(pat.RetrieveNode(name)); Node* p = subgraph.at(pat.RetrieveNode(name));
PADDLE_ENFORCE_NOT_NULL(p, "subgraph has no node %s", name.c_str()); PADDLE_ENFORCE_NOT_NULL(
p, platform::errors::NotFound("subgraph has no node %s", name.c_str()));
return p; return p;
}; };
......
...@@ -142,7 +142,8 @@ static int BuildFusion(Graph* graph, const std::string& name_scope, ...@@ -142,7 +142,8 @@ static int BuildFusion(Graph* graph, const std::string& name_scope,
PADDLE_ENFORCE(subgraph.count(pat.RetrieveNode(name)), PADDLE_ENFORCE(subgraph.count(pat.RetrieveNode(name)),
"pattern has no Node called %s", name.c_str()); "pattern has no Node called %s", name.c_str());
Node* p = subgraph.at(pat.RetrieveNode(name)); Node* p = subgraph.at(pat.RetrieveNode(name));
PADDLE_ENFORCE_NOT_NULL(p, "subgraph has no node %s", name.c_str()); PADDLE_ENFORCE_NOT_NULL(
p, platform::errors::NotFound("subgraph has no node %s", name.c_str()));
return p; return p;
}; };
......
...@@ -12,7 +12,9 @@ ...@@ -12,7 +12,9 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include <memory>
#include <string> #include <string>
#include <utility>
#include <vector> #include <vector>
#include "paddle/fluid/framework/feed_fetch_method.h" #include "paddle/fluid/framework/feed_fetch_method.h"
......
...@@ -53,7 +53,9 @@ struct OpInfo { ...@@ -53,7 +53,9 @@ struct OpInfo {
} }
const proto::OpProto& Proto() const { const proto::OpProto& Proto() const {
PADDLE_ENFORCE_NOT_NULL(proto_, "Operator's Proto has not been registered"); PADDLE_ENFORCE_NOT_NULL(
proto_,
platform::errors::NotFound("Operator's Proto has not been registered"));
PADDLE_ENFORCE_EQ(proto_->IsInitialized(), true, PADDLE_ENFORCE_EQ(proto_->IsInitialized(), true,
platform::errors::InvalidArgument( platform::errors::InvalidArgument(
"Operator's Proto in op info is not initialized.")); "Operator's Proto in op info is not initialized."));
......
...@@ -191,9 +191,11 @@ bool OperatorBase::HasInputs(const std::string& name) const { ...@@ -191,9 +191,11 @@ bool OperatorBase::HasInputs(const std::string& name) const {
std::string OperatorBase::Input(const std::string& name) const { std::string OperatorBase::Input(const std::string& name) const {
auto& ins = Inputs(name); auto& ins = Inputs(name);
PADDLE_ENFORCE_LE(ins.size(), 1UL, PADDLE_ENFORCE_LE(
"Operator %s's input %s should contain only one variable.", ins.size(), 1UL,
type_, name); platform::errors::AlreadyExists(
"Operator %s's input %s should contain only one variable.", type_,
name));
return ins.empty() ? kEmptyVarName : ins[0]; return ins.empty() ? kEmptyVarName : ins[0];
} }
...@@ -429,9 +431,11 @@ const Variable* ExecutionContext::InputVar(const std::string& name) const { ...@@ -429,9 +431,11 @@ const Variable* ExecutionContext::InputVar(const std::string& name) const {
auto it = ctx_.inputs.find(name); auto it = ctx_.inputs.find(name);
if (it == ctx_.inputs.end()) return nullptr; if (it == ctx_.inputs.end()) return nullptr;
PADDLE_ENFORCE_LE(it->second.size(), 1UL, PADDLE_ENFORCE_LE(
it->second.size(), 1UL,
platform::errors::AlreadyExists(
"Operator %s's input %s should contain only one variable.", "Operator %s's input %s should contain only one variable.",
op_.Type(), name); op_.Type(), name));
return it->second.empty() ? nullptr : it->second[0]; return it->second.empty() ? nullptr : it->second[0];
} }
......
...@@ -32,12 +32,15 @@ class MulOp : public framework::OperatorWithKernel { ...@@ -32,12 +32,15 @@ class MulOp : public framework::OperatorWithKernel {
using framework::OperatorWithKernel::OperatorWithKernel; using framework::OperatorWithKernel::OperatorWithKernel;
void InferShape(framework::InferShapeContext* ctx) const override { void InferShape(framework::InferShapeContext* ctx) const override {
PADDLE_ENFORCE_EQ(ctx->HasInput("X"), true, PADDLE_ENFORCE_EQ(
"Input(X) of MulOp should not be null."); ctx->HasInput("X"), true,
PADDLE_ENFORCE_EQ(ctx->HasInput("Y"), true, platform::errors::NotFound("Input(X) of MulOp should not be null."));
"Input(Y) of MulOp should not be null."); PADDLE_ENFORCE_EQ(
PADDLE_ENFORCE_EQ(ctx->HasOutput("Out"), true, ctx->HasInput("Y"), true,
"Output(Out) of MulOp should not be null."); platform::errors::NotFound("Input(Y) of MulOp should not be null."));
PADDLE_ENFORCE_EQ(
ctx->HasOutput("Out"), true,
platform::errors::NotFound("Output(Out) of MulOp should not be null."));
auto x_dims = ctx->GetInputDim("X"); auto x_dims = ctx->GetInputDim("X");
auto y_dims = ctx->GetInputDim("Y"); auto y_dims = ctx->GetInputDim("Y");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册