11#include < fstream>
2+ #include " serving/processor/serving/message_coding.h"
23#include " serving/processor/serving/model_instance.h"
34#include " serving/processor/serving/model_partition.h"
45#include " serving/processor/serving/model_session.h"
@@ -24,6 +25,7 @@ namespace processor {
2425namespace {
2526constexpr int _60_Seconds = 60 ;
2627constexpr int MAX_TRY_COUNT = 10 ;
28+ constexpr int WARMUP_COUNT = 5 ;
2729
2830Tensor CreateTensor (const TensorInfo& tensor_info) {
2931 auto real_ts = tensor_info.tensor_shape ();
@@ -71,44 +73,35 @@ Tensor CreateTensor(const TensorInfo& tensor_info) {
7173 return tensor;
7274}
7375
74- Call CreateWarmupParams (SignatureDef& sig_def) {
75- Call call;
76+ Status CreateWarmupParams (SignatureDef& sig_def, Call* call) {
7677 for (auto it : sig_def.inputs ()) {
7778 const auto & tensor = CreateTensor (it.second );
78- call. request .inputs .emplace_back (it.second .name (), tensor);
79+ call-> request .inputs .emplace_back (it.second .name (), tensor);
7980 }
8081
8182 for (auto it : sig_def.outputs ()) {
82- call. request .output_tensor_names .emplace_back (it.second .name ());
83+ call-> request .output_tensor_names .emplace_back (it.second .name ());
8384 }
8485
85- return call;
86+ return Status::OK ();
8687}
8788
88- Call CreateWarmupParams (SignatureDef& sig_def,
89- const std::string& warmup_file_name) {
89+ Status CreateWarmupParams (SignatureDef& sig_def,
90+ const std::string& warmup_file_name,
91+ Call* call, IParser* parser,
92+ const SignatureInfo& signature_info) {
9093 // Parse warmup file
9194 eas::PredictRequest request;
9295 std::fstream input (warmup_file_name, std::ios::in | std::ios::binary);
93- request.ParseFromIstream (&input);
94- input.close ();
95-
96- Call call;
97- for (auto & input : request.inputs ()) {
98- call.request .inputs .emplace_back (input.first ,
99- util::Proto2Tensor (input.second ));
100- }
101-
102- call.request .output_tensor_names =
103- std::vector<std::string>(request.output_filter ().begin (),
104- request.output_filter ().end ());
105-
106- // User need to set fetches
107- if (call.request .output_tensor_names .size () == 0 ) {
108- LOG (FATAL) << " warmup file must be contain fetches." ;
96+ bool success = request.ParseFromIstream (&input);
97+ if (!success) {
98+ LOG (ERROR) << " Read warmp file failed: " << warmup_file_name;
99+ return Status (error::Code::INTERNAL,
100+ " Read warmp file failed, please check warmp file path" );
109101 }
102+ input.close ();
110103
111- return call;
104+ return parser-> ParseRequest (request, &signature_info, * call);
112105}
113106
114107bool ShouldWarmup (SignatureDef& sig_def) {
@@ -264,6 +257,7 @@ Status LocalSessionInstance::Init(ModelConfig* config,
264257 {kSavedModelTagServe }, &meta_graph_def_));
265258
266259 warmup_file_name_ = config->warmup_file_name ;
260+ parser_ = ParserFactory::GetInstance (config->serialize_protocol , 4 );
267261
268262 GraphOptimizerOption option;
269263 option.native_tf_mode = true ;
@@ -352,21 +346,38 @@ Status LocalSessionInstance::Warmup(
352346 return Status::OK ();
353347 }
354348
349+ LOG (INFO) << " Try to warmup model: " << warmup_file_name_;
350+ Status s;
355351 Call call;
356352 if (warmup_file_name_.empty ()) {
357- call = CreateWarmupParams (model_signature_.second );
353+ s = CreateWarmupParams (model_signature_.second , &call );
358354 } else {
359- call = CreateWarmupParams (model_signature_.second ,
360- warmup_file_name_);
355+ s = CreateWarmupParams (model_signature_.second ,
356+ warmup_file_name_, &call,
357+ parser_, signature_info_);
358+ }
359+ if (!s.ok ()) {
360+ LOG (ERROR) << " Create warmup params failed, warmup will be canceled." ;
361+ return s;
361362 }
362363
363- if (warmup_session) {
364- return warmup_session->LocalPredict (
365- call.request , call.response );
364+ int left_try_count = WARMUP_COUNT;
365+ while (left_try_count > 0 ) {
366+ if (warmup_session) {
367+ s = warmup_session->LocalPredict (
368+ call.request , call.response );
369+ } else {
370+ s = session_mgr_->LocalPredict (
371+ call.request , call.response );
372+ }
373+ if (!s.ok ()) return s;
374+
375+ --left_try_count;
376+ call.response .outputs .clear ();
366377 }
378+ LOG (INFO) << " Warmup model successful: " << warmup_file_name_;
367379
368- return session_mgr_->LocalPredict (
369- call.request , call.response );
380+ return Status::OK ();
370381}
371382
372383std::string LocalSessionInstance::DebugString () {
@@ -474,6 +485,7 @@ Status RemoteSessionInstance::Init(ModelConfig* model_config,
474485 backup_storage_ = new FeatureStoreMgr (&backup_model_config);
475486
476487 warmup_file_name_ = model_config->warmup_file_name ;
488+ parser_ = ParserFactory::GetInstance (model_config->serialize_protocol , 4 );
477489
478490 // set active flag
479491 serving_storage_->SetStorageActiveStatus (active);
@@ -534,21 +546,36 @@ Status RemoteSessionInstance::Warmup(
534546 return Status::OK ();
535547 }
536548
549+ Status s;
537550 Call call;
538551 if (warmup_file_name_.empty ()) {
539- call = CreateWarmupParams (model_signature_.second );
552+ s = CreateWarmupParams (model_signature_.second , &call );
540553 } else {
541- call = CreateWarmupParams (model_signature_.second ,
542- warmup_file_name_);
554+ s = CreateWarmupParams (model_signature_.second ,
555+ warmup_file_name_, &call,
556+ parser_, signature_info_);
557+ }
558+ if (!s.ok ()) {
559+ LOG (ERROR) << " Create warmup params failed, warmup will be canceled." ;
560+ return s;
543561 }
544562
545- if (warmup_session) {
546- return warmup_session->Predict (
547- call.request , call.response );
563+ int left_try_count = WARMUP_COUNT;
564+ while (left_try_count > 0 ) {
565+ if (warmup_session) {
566+ s = warmup_session->LocalPredict (
567+ call.request , call.response );
568+ } else {
569+ s = session_mgr_->LocalPredict (
570+ call.request , call.response );
571+ }
572+ if (!s.ok ()) return s;
573+
574+ --left_try_count;
575+ call.response .outputs .clear ();
548576 }
549577
550- return session_mgr_->Predict (
551- call.request , call.response );
578+ return Status::OK ();
552579}
553580
554581Status RemoteSessionInstance::FullModelUpdate (
0 commit comments