【搜索引擎】特征加工和预估
FGContext类包含了模型运行时的上下文信息feeds用于保存输入fetches为输出特征。step_id是tf运行的时候生成的唯一idstructFGContext{FGContext():session(nullptr),step_id(-1){}~FGContext(){Allocator::Get()-Release(step_id);}void*session;int64_tstep_id;std::unordered_mapstd::string,FGTensorfeeds;std::unordered_mapstd::string,FGTensorfetches;};FG 类负责加载和运行 TensorFlow 模型。Init 方法负责初始化会话和创建可调用对象boolFG::Init(intconcurrency){SessionOptions session_options;InitSessionOptions(session_options,concurrency);RunOptions run_options;autostatusLoadSavedModel(session_options,run_options,path_,{kSavedModelTagServe},bundle_);CallableOptions callable_options;staticconstchar*kDefaultSignatureserving_default;autoitbundle_.meta_graph_def.signature_def().find(kDefaultSignature);constautoinputsit-second.inputs();for(constautoinput:inputs){constautofeature_nameinput.first;constautotensorinput.second;inputs_.emplace_back(feature_name);callable_options.add_feed(tensor.name());}for(autooutput:it-second.outputs()){constautofeature_nameoutput.first;constautotensoroutput.second;outputs_.push_back(feature_name);callable_options.add_fetch(tensor.name());}statusbundle_.session-MakeCallable(callable_options,handle_);}Run执行模型的推理并将结果存储在上下文中。fg的run方法一般运行fg的模型输入数据输出fg的产出的特征。boolFG::Run(FGContext*context){vector::tensorflow::Tensorinputs;::tensorflow::Tensorctx(::tensorflow::DT_VARIANT,{});ctx.scalartensorflow::Variant()()VariantWrapperFGContext(context);inputs.emplace_back(std::move(ctx));vector::tensorflow::Tensoroutputs;autostatusbundle_.session-RunCallable(handle_,inputs,outputs,nullptr);for(size_t i0;ioutputs_.size();i){autofg_tensor*outputs[i].scalar::tensorflow::Variant()().getFGTensor();context-fetches.emplace(outputs_[i],fg_tensor);}}FGManager用于管理注册的fgclassFGManager:publicSingletonBaseFGManager{public:voidRegister(std::shared_ptrFGfg){std::lock_guardstd::mutexlock(mutex_);fgs_[fg-name()]fg;}std::shared_ptrFGGetFG(conststd::stringname)const{std::lock_guardstd::mutexlock(mutex_);staticstd::shared_ptrFGdummy;autoitfgs_.find(name);if(itfgs_.end()){returndummy;}returnit-second;}voidClear(){std::lock_guardstd::mutexlock(mutex_);fgs_.clear();}private:mutablestd::mutex mutex_;std::unordered_mapstd::string,std::shared_ptrFGfgs_;};FGPredictorPredict预处理数据调用fg运行fg生成特征tensor调用tfmodel运行算法预估模型生成预估分FGPredictor::Predict(GraphSession*graph_session){autofgfg::FGManager::Get()-GetFG(runtime.fg_name);autocontextPreProcess(graph_session,output,params,idxs);autofgfg::FGManager::Get()-GetFG(runtime.fg_name);fg::FGContext fg_ctx;fg_ctx.sessioncontext.get();if(!fg-Run(fg_ctx))auto*modelreinterpret_castTfModel*(runtime.model);autosmodel-Predict(fg_ctx,model_scores);}TfModelPredict调用tf Predict 函数输入feature构建Tensor调用模型预估StatusTfModel::Predict(constdfg::FGContextfg_context,std::vectorstd::vectorfloatscores){intbatch_sizereinterpret_castPredictContext*(fg_context.session)-GetTotalSize();std::vectortensorflow::Tensorfeeds;feeds.reserve(features_.size());for(constautofeature:features_){constautofeature_namefeature.first;autoitfg_context.fetches.find(feature_name);feeds.emplace_back(BuildTensor(it-second,feature.second,batch_size));}std::vectortensorflow::Tensorfetches;autostatusbundle_.session-RunCallable(handle_,feeds,fetches,nullptr);GetResultsFromTfOutputs(fetches,scores);returnStatus::OK();}