diff --git a/paddle/fluid/operators/save_op.cc b/paddle/fluid/operators/save_op.cc index 338e2fbb5d868f146c9ff420b2d5d4cf6088316e..c660bbb8ed9a4caf564fd75d3c248827ea46d35a 100644 --- a/paddle/fluid/operators/save_op.cc +++ b/paddle/fluid/operators/save_op.cc @@ -31,8 +31,8 @@ class SaveOp : public framework::OperatorWithKernel { protected: framework::OpKernelType GetExpectedKernelType( const framework::ExecutionContext &ctx) const override { - return framework::OpKernelType(ctx.Input("X")->type(), - ctx.GetPlace()); + auto data_type = framework::GetDataTypeOfVar(ctx.InputVar("X")); + return framework::OpKernelType(data_type, ctx.device_context()); } }; diff --git a/paddle/fluid/operators/save_op.h b/paddle/fluid/operators/save_op.h index 642235aad58bef2ec7f741ee5fb5a65a2081f4ce..c5bc12caa23a484ba9e855f5ddeaec9998c320fa 100644 --- a/paddle/fluid/operators/save_op.h +++ b/paddle/fluid/operators/save_op.h @@ -103,12 +103,21 @@ class SaveOpKernel : public framework::OpKernel { const platform::Place &place, const framework::Variable *var) const { framework::Variable *out_put_var = ctx.OutputVar(LOOKUP_TABLE_PATH); - PADDLE_ENFORCE( - out_put_var != nullptr, - "Can not find variable kLookupTablePath for SaveSelectedRows"); - auto *lt_var = out_put_var->GetMutable(); + auto file_path = ctx.Attr("file_path"); + auto overwrite = ctx.Attr("overwrite"); + + std::string filename = file_path; + + if (out_put_var != nullptr) { + auto *lt_var = out_put_var->GetMutable(); + filename = *lt_var; + } + + if (FileExists(filename) && !overwrite) { + PADDLE_THROW("%s is existed, cannot save to it when overwrite=false", + filename, overwrite); + } - std::string filename = lt_var->data(); VLOG(4) << "SaveSelectedRows get File name: " << filename; MkDirRecursively(DirName(filename).c_str());