From 37fcf03af7aa54a745e656d1b95889496684a067 Mon Sep 17 00:00:00 2001 From: zhongpu <2013000149@qq.com> Date: Fri, 10 Apr 2020 11:36:00 +0800 Subject: [PATCH] Op (Save/Load) error message enhancement, test=develop (#23650) --- paddle/fluid/operators/load_op.h | 23 ++++++++++-------- paddle/fluid/operators/save_op.h | 41 ++++++++++++++++++-------------- 2 files changed, 36 insertions(+), 28 deletions(-) diff --git a/paddle/fluid/operators/load_op.h b/paddle/fluid/operators/load_op.h index 5a409502fd..3e58a6462d 100644 --- a/paddle/fluid/operators/load_op.h +++ b/paddle/fluid/operators/load_op.h @@ -34,26 +34,29 @@ class LoadOpKernel : public framework::OpKernel { // it to save an output stream. auto filename = ctx.Attr("file_path"); std::ifstream fin(filename, std::ios::binary); - PADDLE_ENFORCE(static_cast(fin), "Cannot open file %s for load op", - filename); + PADDLE_ENFORCE_EQ(static_cast(fin), true, + platform::errors::Unavailable( + "Load operator fail to open file %s, please check " + "whether the model file is complete or damaged.", + filename)); auto out_var_name = ctx.OutputNames("Out").data(); auto *out_var = ctx.OutputVar("Out"); - PADDLE_ENFORCE(out_var != nullptr, "Output variable %s cannot be found ", - out_var_name); - - PADDLE_ENFORCE(out_var != nullptr, "Output variable cannot be found "); + PADDLE_ENFORCE_NOT_NULL( + out_var, + platform::errors::InvalidArgument( + "The variable %s to be loaded cannot be found.", out_var_name)); if (out_var->IsType()) { LoadLodTensor(fin, place, out_var, ctx); } else if (out_var->IsType()) { LoadSelectedRows(fin, place, out_var); } else { - PADDLE_ENFORCE( - false, - "Load only support LoDTensor and SelectedRows, %s has wrong type", - out_var_name); + PADDLE_THROW(platform::errors::InvalidArgument( + "Load operator only supports loading LoDTensor and SelectedRows " + "variable, %s has wrong type", + out_var_name)); } } diff --git a/paddle/fluid/operators/save_op.h b/paddle/fluid/operators/save_op.h index ebb2099f0c..62ccf0c17d 100644 --- a/paddle/fluid/operators/save_op.h +++ b/paddle/fluid/operators/save_op.h @@ -41,18 +41,19 @@ class SaveOpKernel : public framework::OpKernel { auto *input_var = ctx.InputVar("X"); auto iname = ctx.InputNames("X").data(); - PADDLE_ENFORCE(input_var != nullptr, "Cannot find variable %s for save_op", - iname); + PADDLE_ENFORCE_NOT_NULL( + input_var, platform::errors::InvalidArgument( + "The variable %s to be saved cannot be found.", iname)); if (input_var->IsType()) { SaveLodTensor(ctx, place, input_var); } else if (input_var->IsType()) { SaveSelectedRows(ctx, place, input_var); } else { - PADDLE_ENFORCE( - false, - "SaveOp only support LoDTensor and SelectedRows, %s has wrong type", - iname); + PADDLE_THROW(platform::errors::InvalidArgument( + "Save operator only supports saving LoDTensor and SelectedRows " + "variable, %s has wrong type", + iname)); } } @@ -62,10 +63,11 @@ class SaveOpKernel : public framework::OpKernel { auto filename = ctx.Attr("file_path"); auto overwrite = ctx.Attr("overwrite"); - if (FileExists(filename) && !overwrite) { - PADDLE_THROW("%s is existed, cannot save to it when overwrite=false", - filename, overwrite); - } + PADDLE_ENFORCE_EQ( + FileExists(filename) && !overwrite, false, + platform::errors::PreconditionNotMet( + "%s exists!, cannot save to it when overwrite is set to false.", + filename, overwrite)); MkDirRecursively(DirName(filename).c_str()); @@ -78,8 +80,9 @@ class SaveOpKernel : public framework::OpKernel { // FIXME(yuyang18): We save variable to local file now, but we should change // it to save an output stream. std::ofstream fout(filename, std::ios::binary); - PADDLE_ENFORCE(static_cast(fout), "Cannot open %s to write", - filename); + PADDLE_ENFORCE_EQ(static_cast(fout), true, + platform::errors::Unavailable( + "Cannot open %s to save variables.", filename)); auto save_as_fp16 = ctx.Attr("save_as_fp16"); auto in_dtype = tensor.type(); @@ -117,10 +120,11 @@ class SaveOpKernel : public framework::OpKernel { } } - if (FileExists(filename) && !overwrite) { - PADDLE_THROW("%s is existed, cannot save to it when overwrite=false", - filename, overwrite); - } + PADDLE_ENFORCE_EQ( + FileExists(filename) && !overwrite, false, + platform::errors::PreconditionNotMet( + "%s exists!, cannot save to it when overwrite is set to false.", + filename, overwrite)); VLOG(4) << "SaveSelectedRows get File name: " << filename; @@ -135,8 +139,9 @@ class SaveOpKernel : public framework::OpKernel { // FIXME(yuyang18): We save variable to local file now, but we should change // it to save an output stream. std::ofstream fout(filename, std::ios::binary); - PADDLE_ENFORCE(static_cast(fout), "Cannot open %s to write", - filename); + PADDLE_ENFORCE_EQ(static_cast(fout), true, + platform::errors::Unavailable( + "Cannot open %s to save variables.", filename)); framework::SerializeToStream(fout, selectedRows, dev_ctx); fout.close(); } -- GitLab