@@ -661,9 +661,14 @@ void LoadParamNaive(const std::string &path,
661661
662662void LoadCombinedParamsNaive (const std::string &path,
663663 lite::Scope *scope,
664- const cpp::ProgramDesc &cpp_prog) {
664+ const cpp::ProgramDesc &cpp_prog,
665+ bool params_from_memory) {
665666 naive_buffer::BinaryTable table;
666- table.LoadFromFile (path);
667+ if (params_from_memory) {
668+ table.LoadFromMemory (path.c_str (), path.length ());
669+ } else {
670+ table.LoadFromFile (path);
671+ }
667672 naive_buffer::proto::CombinedParamsDesc pt_desc (&table);
668673 pt_desc.Load ();
669674 naive_buffer::CombinedParamsDesc desc (&pt_desc);
@@ -710,7 +715,7 @@ void LoadModelNaive(const std::string &model_dir,
710715 // NOTE: Only main block be used now.
711716 if (combined) {
712717 const std::string combined_params_path = model_dir + " /param.nb" ;
713- LoadCombinedParamsNaive (combined_params_path, scope, *cpp_prog);
718+ LoadCombinedParamsNaive (combined_params_path, scope, *cpp_prog, false );
714719 } else {
715720 auto &prog = *cpp_prog;
716721 auto &main_block_desc = *prog.GetBlock <cpp::BlockDesc>(0 );
@@ -750,5 +755,40 @@ void LoadModelNaive(const std::string &model_dir,
750755 VLOG (4 ) << " Load naive buffer model in '" << model_dir << " ' successfully" ;
751756}
752757
758+ void LoadModelNaiveFromMemory (const std::string &model_buffer,
759+ const std::string ¶m_buffer,
760+ Scope *scope,
761+ cpp::ProgramDesc *cpp_prog) {
762+ CHECK (cpp_prog);
763+ CHECK (scope);
764+ cpp_prog->ClearBlocks ();
765+
766+ // Load model
767+
768+ std::string prog_path = model_buffer;
769+
770+ naive_buffer::BinaryTable table;
771+ table.LoadFromMemory (prog_path.c_str (), prog_path.length ());
772+
773+ naive_buffer::proto::ProgramDesc nb_proto_prog (&table);
774+ nb_proto_prog.Load ();
775+ naive_buffer::ProgramDesc nb_prog (&nb_proto_prog);
776+
777+ // Transform to cpp::ProgramDesc
778+ TransformProgramDescAnyToCpp (nb_prog, cpp_prog);
779+
780+ // Load Params
781+ // NOTE: Only main block be used now.
782+ // only combined Params are supported in Loading Model from memory
783+ std::string combined_params_path = param_buffer;
784+ LoadCombinedParamsNaive (combined_params_path, scope, *cpp_prog, true );
785+
786+ #ifdef LITE_WITH_NPU
787+ LOG (FATAL) << " load from memory is not supported by NPU" ;
788+ #endif
789+
790+ VLOG (4 ) << " Load model from naive buffer memory successfully" ;
791+ }
792+
753793} // namespace lite
754794} // namespace paddle
0 commit comments