-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add variant of new load and save ops for storing model params in a single file #7909
Changes from 8 commits
7781287
6adc36e
99d1d07
a24fca3
11e3181
53cc3e3
b1cc306
6a3710f
98d2aba
c7c2125
57bcdbb
3d0f724
383acc7
511f472
d9de9ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. */ | ||
#include <fstream> | ||
|
||
#include "paddle/framework/op_registry.h" | ||
#include "paddle/platform/device_context.h" | ||
|
||
namespace paddle { | ||
namespace operators { | ||
|
||
class LoadCombineOp : public framework::OperatorBase { | ||
public: | ||
LoadCombineOp(const std::string &type, | ||
const framework::VariableNameMap &inputs, | ||
const framework::VariableNameMap &outputs, | ||
const framework::AttributeMap &attrs) | ||
: OperatorBase(type, inputs, outputs, attrs) {} | ||
void Run(const framework::Scope &scope, | ||
const platform::Place &place) const override { | ||
auto filename = Attr<std::string>("file_path"); | ||
|
||
std::ifstream fin(filename); | ||
PADDLE_ENFORCE(static_cast<bool>(fin), | ||
"Cannot open file %s for load_combine op", filename); | ||
|
||
auto out_var_names = Outputs("Out"); | ||
PADDLE_ENFORCE(out_var_names.size(), "Output variables cannot be found"); | ||
|
||
platform::DeviceContextPool &pool = platform::DeviceContextPool::Instance(); | ||
auto &dev_ctx = *pool.Get(place); | ||
|
||
for (size_t i = 0; i < out_var_names.size(); i++) { | ||
auto *out_var = scope.FindVar(out_var_names[i]); | ||
|
||
PADDLE_ENFORCE(out_var != nullptr, "Output variable %s cannot be found", | ||
out_var_names[i]); | ||
|
||
auto *tensor = out_var->GetMutable<framework::LoDTensor>(); | ||
|
||
uint64_t data_length; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure is there still a need to save the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, thanks for pointing this out. I have simplified the logic. |
||
char *buffer = NULL; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there is no need a buffer, we can directly use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, thanks for the suggestion. |
||
|
||
// Error checking | ||
PADDLE_ENFORCE(static_cast<bool>(fin), "Cannot read more from file %s", | ||
filename); | ||
|
||
// Read a fixed-width int, to get the number of bytes | ||
// for the serialized data. | ||
fin.read(reinterpret_cast<char *>(&data_length), sizeof(data_length)); | ||
|
||
// Error checking | ||
PADDLE_ENFORCE(static_cast<bool>(fin), "Cannot read more from file %s", | ||
filename); | ||
|
||
buffer = new char[data_length]; | ||
|
||
// Read the serialized data into the buffer | ||
fin.read(buffer, data_length); | ||
|
||
std::string current_serialized_data; | ||
current_serialized_data.assign(buffer, data_length); | ||
|
||
// Create an input string stream | ||
std::istringstream ist(current_serialized_data); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need another stream, we can directly use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
DeserializeFromStream(ist, tensor, dev_ctx); | ||
|
||
delete[] buffer; // delete the allocated memory | ||
|
||
if (platform::is_gpu_place(place)) { | ||
// copy CPU to GPU | ||
framework::LoDTensor cpu_tensor; | ||
cpu_tensor.ShareDataWith(*tensor); | ||
cpu_tensor.set_lod(tensor->lod()); | ||
|
||
// reset tensor | ||
out_var->Clear(); | ||
tensor = out_var->GetMutable<framework::LoDTensor>(); | ||
tensor->set_lod(cpu_tensor.lod()); | ||
Copy(cpu_tensor, place, dev_ctx, tensor); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
class LoadCombineOpProtoMaker : public framework::OpProtoAndCheckerMaker { | ||
public: | ||
LoadCombineOpProtoMaker(OpProto *proto, OpAttrChecker *op_checker) | ||
: OpProtoAndCheckerMaker(proto, op_checker) { | ||
AddOutput("Out", "(LoDTensor) The tensor need to be load_combined") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (LoDTensor) The tensor need to be load_combined -> (vector) The tensors need to be loaded There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, thanks. |
||
.AsDuplicable(); | ||
AddAttr<std::string>("file_path", | ||
"(string) " | ||
"Variable will be load_combined from \"file_path\".") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Variable will be load_combined from "file_path". -> Variables will be loaded from "file_path". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
.AddCustomChecker( | ||
[](const std::string &path) { return !path.empty(); }); | ||
AddComment(R"DOC( | ||
LoadCombine Operator. | ||
|
||
LoadCombine operator combines together various tensor variable into a file. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should it be something like "load various tensor variables from a file"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, will change the comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
|
||
)DOC"); | ||
} | ||
}; | ||
} // namespace operators | ||
} // namespace paddle | ||
namespace ops = paddle::operators; | ||
|
||
REGISTER_OPERATOR(load_combine, ops::LoadCombineOp, | ||
ops::LoadCombineOpProtoMaker); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. */ | ||
|
||
#include <stdint.h> | ||
#include <sys/stat.h> | ||
#include <fstream> | ||
#include <numeric> | ||
#include <sstream> | ||
#include "paddle/framework/data_type.h" | ||
#include "paddle/framework/framework.pb.h" | ||
#include "paddle/framework/lod_tensor.h" | ||
#include "paddle/framework/op_registry.h" | ||
#include "paddle/platform/device_context.h" | ||
|
||
namespace paddle { | ||
namespace operators { | ||
|
||
// TODO(sidgoyal78): These function are needed by other files (save_op), move | ||
// them to paddle::filesystem namespace. (as noted by yuyang18 in save_op). | ||
constexpr char kSEP = '/'; | ||
static bool FileExists(const std::string &filepath) { | ||
struct stat buffer; | ||
return (stat(filepath.c_str(), &buffer) == 0); | ||
} | ||
|
||
static std::string DirName(const std::string &filepath) { | ||
auto pos = filepath.rfind(kSEP); | ||
if (pos == std::string::npos) { | ||
return ""; | ||
} | ||
return filepath.substr(0, pos); | ||
} | ||
|
||
static void MkDir(const char *path) { | ||
if (mkdir(path, 0755)) { | ||
PADDLE_ENFORCE_EQ(errno, EEXIST, "%s mkdir failed!", path); | ||
} | ||
} | ||
|
||
static void MkDirRecursively(const char *fullpath) { | ||
if (*fullpath == '\0') return; // empty string | ||
if (FileExists(fullpath)) return; | ||
|
||
MkDirRecursively(DirName(fullpath).c_str()); | ||
MkDir(fullpath); | ||
} | ||
|
||
class SaveCombineOp : public framework::OperatorBase { | ||
public: | ||
SaveCombineOp(const std::string &type, | ||
const framework::VariableNameMap &inputs, | ||
const framework::VariableNameMap &outputs, | ||
const framework::AttributeMap &attrs) | ||
: OperatorBase(type, inputs, outputs, attrs) {} | ||
void Run(const framework::Scope &scope, | ||
const platform::Place &place) const override { | ||
auto filename = Attr<std::string>("file_path"); | ||
auto overwrite = Attr<bool>("overwrite"); | ||
|
||
bool is_present = FileExists(filename); | ||
if (is_present && !overwrite) { | ||
PADDLE_THROW("%s exists!, cannot save_combine to it when overwrite=false", | ||
filename, overwrite); | ||
} | ||
|
||
MkDirRecursively(DirName(filename).c_str()); | ||
|
||
std::ofstream fout(filename); | ||
PADDLE_ENFORCE(static_cast<bool>(fout), "Cannot open %s to write", | ||
filename); | ||
|
||
auto inames = Inputs("X"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. inames -> in_names There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Modified, to make it consistent with variable name in load_combine: I modified it to |
||
|
||
// get device context from pool | ||
platform::DeviceContextPool &pool = platform::DeviceContextPool::Instance(); | ||
auto &dev_ctx = *pool.Get(place); | ||
|
||
for (size_t i = 0; i < inames.size(); i++) { | ||
auto *var = scope.FindVar(inames[i]); | ||
|
||
PADDLE_ENFORCE(var != nullptr, | ||
"Cannot find variable %s for save_combine_op", inames[i]); | ||
PADDLE_ENFORCE(var->IsType<framework::LoDTensor>(), | ||
"SaveCombineOp only support LoDTensor, %s has wrong type", | ||
inames[i]); | ||
|
||
auto &tensor = var->Get<framework::LoDTensor>(); | ||
|
||
// Create "output string stream" to get the serialized LodTensor | ||
std::ostringstream str_stream; | ||
framework::SerializeToStream(str_stream, tensor, dev_ctx); | ||
std::string current_serialized_data = str_stream.str(); | ||
|
||
// Save 'current_size' information as a fixed width integer, and | ||
// further save the serialized data using 'current_size' bytes | ||
uint64_t current_size = current_serialized_data.size(); | ||
fout.write(reinterpret_cast<const char *>(¤t_size), | ||
sizeof(current_size)); | ||
fout.write( | ||
reinterpret_cast<const char *>(current_serialized_data.c_str()), | ||
static_cast<std::streamsize>(current_size)); | ||
} | ||
fout.close(); | ||
} | ||
}; | ||
|
||
class SaveCombineOpProtoMaker : public framework::OpProtoAndCheckerMaker { | ||
public: | ||
SaveCombineOpProtoMaker(OpProto *proto, OpAttrChecker *op_checker) | ||
: OpProtoAndCheckerMaker(proto, op_checker) { | ||
AddInput("X", "(Tensor) Input tensors to be save_combined").AsDuplicable(); | ||
AddComment(R"DOC( | ||
Save_combine operator | ||
|
||
This operator will serialize and write a list of input tensor variables | ||
to a file on disk. | ||
)DOC"); | ||
AddAttr<bool>("overwrite", | ||
"(boolean, default true)" | ||
"Overwrite the output file if exist") | ||
.SetDefault(true); | ||
AddAttr<std::string>( | ||
"file_path", | ||
"(string)" | ||
"The \"file_path\" where the variable will be save_combined.") | ||
.AddCustomChecker( | ||
[](const std::string &path) { return !path.empty(); }); | ||
} | ||
}; | ||
|
||
} // namespace operators | ||
} // namespace paddle | ||
|
||
namespace ops = paddle::operators; | ||
|
||
REGISTER_OPERATOR(save_combine, ops::SaveCombineOp, | ||
ops::SaveCombineOpProtoMaker); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change to
PADDLE_ENFORCE_LT(out_var_names.size(), 0, "The number of output variables should be > 0.");
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. (I think you mean
PADDLE_ENFORCE_GT
:) )