-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Add variant of new load and save ops for storing model params in a single file #7909
Conversation
… serialize_alternate
paddle/operators/load_combine_op.cc
Outdated
"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"); |
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
:) )
paddle/operators/load_combine_op.cc
Outdated
auto *tensor = out_var->GetMutable<framework::LoDTensor>(); | ||
|
||
uint64_t data_length; | ||
char *buffer = NULL; |
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.
I think there is no need a buffer, we can directly use fin
to read the file sequentially and deserialize the LoDTensor one by one.
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, thanks for the suggestion.
paddle/operators/load_combine_op.cc
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
No need another stream, we can directly use fin
.
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.
paddle/operators/load_combine_op.cc
Outdated
|
||
auto *tensor = out_var->GetMutable<framework::LoDTensor>(); | ||
|
||
uint64_t data_length; |
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.
I am not sure is there still a need to save the data_length
...
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.
Yes, thanks for pointing this out. I have simplified the logic.
#include "gtest/gtest.h" | ||
#include "paddle/framework/op_registry.h" | ||
|
||
USE_NO_KERNEL_OP(save); |
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.
save -> save_combine?
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.
Fixed, thanks.
#include "paddle/framework/op_registry.h" | ||
|
||
USE_NO_KERNEL_OP(save); | ||
USE_NO_KERNEL_OP(load); |
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.
load -> load_combine?
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.
Fixed, thanks. Based on the naming issues, i fixed some other things as well. Thanks.
paddle/operators/load_combine_op.cc
Outdated
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 comment
The 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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Done
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.
Great work~ It is almost LGTM except some minor aspects.
We can merge this PR first and fix the comments in next PR if it blocks other work.
paddle/operators/load_combine_op.cc
Outdated
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Done, thanks.
paddle/operators/load_combine_op.cc
Outdated
.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 comment
The 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".
or The file_path to load Variables.
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
paddle/operators/load_combine_op.cc
Outdated
AddComment(R"DOC( | ||
LoadCombine Operator. | ||
|
||
LoadCombine operator loads tensor variables from a file. |
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.
More comments here.
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.
Added.
paddle/operators/save_combine_op.cc
Outdated
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 comment
The 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 comment
The 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 inp_var_names
paddle/operators/save_combine_op.cc
Outdated
auto inames = Inputs("X"); | ||
PADDLE_ENFORCE_GT( | ||
static_cast<int>(inames.size()), 0, | ||
"The number of output variables should be greater than 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.
output -> input
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.
USE_NO_KERNEL_OP(save_combine); | ||
USE_NO_KERNEL_OP(load_combine); | ||
|
||
TEST(SaveLoadNewOp, CPU) { |
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.
Move this unittest to save_load_combine_op_test.cc
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
auto load_var = scope.Var(out_var_name); | ||
auto target = load_var->GetMutable<paddle::framework::LoDTensor>(); | ||
return target; | ||
} |
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.
Add a blank line here.
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
USE_NO_KERNEL_OP(save_combine); | ||
USE_NO_KERNEL_OP(load_combine); | ||
|
||
int* create_for_save_combine_op(int x, int y, const std::vector<int>& lod_info, |
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.
create_for_save_combine_op -> CreateSaveCombineOp
the same for following functions.
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
return actual; | ||
} | ||
|
||
void check_values(int* expect, int* actual, paddle::framework::LoD expect_lod, |
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.
check_values -> CheckValues
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
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.
LGTM!
This PR basically implements a different approach for issue #7722 , and is based on the suggestions in PR #7780.
Things added:
TODO: To remove the original load_op and save_op and rename these.