-
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
Better version of PR #7985 (Modify load() for inference) #8024
Changes from 1 commit
e556d23
503f73a
749e33e
f8570e6
076e0e5
0692742
b17af18
e2b88c8
55f40e5
19343d4
4177474
a6f0b0f
e0f8e74
8fb97bc
e813d13
5430ddd
83b4616
8a002ce
4d61569
805b415
33570f7
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 |
---|---|---|
|
@@ -73,24 +73,74 @@ void LoadPersistables(framework::Executor& executor, | |
delete load_program; | ||
} | ||
|
||
std::unique_ptr<framework::ProgramDesc> Load(framework::Executor& executor, | ||
framework::Scope& scope, | ||
const std::string& dirname) { | ||
// This is called when we have a filen named "param_filename" | ||
// which has all the persistable variables stored in a | ||
// combined manner. | ||
void LoadPersistables(framework::Executor& executor, | ||
framework::Scope& scope, | ||
const std::string& dirname, | ||
const framework::ProgramDesc& main_program, | ||
const std::string& param_filename) { | ||
const framework::BlockDesc& global_block = main_program.Block(0); | ||
|
||
framework::ProgramDesc* load_program = new framework::ProgramDesc(); | ||
framework::BlockDesc* load_block = load_program->MutableBlock(0); | ||
std::vector<std::string> paramlist; | ||
|
||
for (auto* var : global_block.AllVars()) { | ||
if (IsParameter(var, main_program)) { | ||
VLOG(3) << "parameter's name: " << var->Name(); | ||
|
||
framework::VarDesc* new_var = load_block->Var(var->Name()); | ||
new_var->SetShape(var->Shape()); | ||
new_var->SetDataType(var->GetDataType()); | ||
new_var->SetType(var->GetType()); | ||
new_var->SetLoDLevel(var->GetLoDLevel()); | ||
new_var->SetPersistable(true); | ||
|
||
paramlist.push_back(new_var->Name()); | ||
} | ||
} | ||
|
||
// sort paramlist to have consistent ordering | ||
std::sort(paramlist.begin(), paramlist.end()); | ||
|
||
// append_op | ||
framework::OpDesc* op = load_block->AppendOp(); | ||
op->SetType("load_combine"); | ||
op->SetOutput("Out", paramlist); | ||
op->SetAttr("file_path", {dirname + "/" + param_filename}); | ||
op->CheckAttrs(); | ||
executor.Run(*load_program, &scope, 0, true, true); | ||
|
||
VLOG(3) << "Ran loading successfully"; | ||
delete load_program; | ||
} | ||
|
||
std::unique_ptr<framework::ProgramDesc> Load( | ||
framework::Executor& executor, | ||
framework::Scope& scope, | ||
const std::string& dirname, | ||
const std::string& param_filename) { | ||
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'd like to keep two std::unique_ptr<framework::ProgramDesc> Load(
framework::Executor& executor,
framework::Scope& scope,
const std::string& dirname);
std::unique_ptr<framework::ProgramDesc> Load(
framework::Executor& executor,
framework::Scope& scope,
const std::string& prog_filename,
const std::string& params_filename); Users are free to rename the file which saving the program. The argument list of 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. Okay. 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 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. Sorry for not describing my suggest clearly. I mean to define the interface to: std::unique_ptr<framework::ProgramDesc> Load(
framework::Executor& executor,
framework::Scope& scope,
const std::string& prog_filepath,
const std::string& param_filepath); No need of 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. Oh, I think you described your suggestion clearly: #8024 (comment) But, I chose to keep another argument of Hence, I put that extra argument. But it seems that you would prefer an implementation without it, so I will modify it. Thanks. |
||
std::string model_filename = dirname + "/__model__"; | ||
LOG(INFO) << "loading model from " << model_filename; | ||
VLOG(3) << "loading model from " << model_filename; | ||
std::ifstream inputfs(model_filename, std::ios::in | std::ios::binary); | ||
std::string program_desc_str; | ||
inputfs.seekg(0, std::ios::end); | ||
program_desc_str.resize(inputfs.tellg()); | ||
inputfs.seekg(0, std::ios::beg); | ||
LOG(INFO) << "program_desc_str's size: " << program_desc_str.size(); | ||
VLOG(3) << "program_desc_str's size: " << program_desc_str.size(); | ||
inputfs.read(&program_desc_str[0], program_desc_str.size()); | ||
inputfs.close(); | ||
|
||
std::unique_ptr<framework::ProgramDesc> main_program( | ||
new framework::ProgramDesc(program_desc_str)); | ||
|
||
LoadPersistables(executor, scope, dirname, *main_program); | ||
if (!param_filename.empty()) { | ||
LoadPersistables(executor, scope, dirname, *main_program, param_filename); | ||
} else { | ||
LoadPersistables(executor, scope, dirname, *main_program); | ||
} | ||
return main_program; | ||
} | ||
|
||
|
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.
There are two
LoadPersistables
. Can merge them into 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