一些重要的概念
Servables
Servables 是客户端请求执行计算的基础对象,大小和粒度是灵活的。
Servables 不会管理自己的运行周期。
典型的Servables包括:
a TensorFlow SavedModelBundle (tensorflow::Session)
a lookup table for embedding or vocabulary lookups
Servable Versions
Tensorflow servables 可以管理多个版本的servable,可以通过版本管理来更新算法配置和模型参数;
可以支持同时加载多个servable
Servable Streams
servable的版本序列。
Models
TensorFlow Serving 将 model 表示为一个或者多个Servables,一个Servable可能对应着模型的一部分,例如,a large lookup table 可以被许多 TensorFlow Serving 共享。
Loaders
Loaders 管理着对应的一个servable的生命周期
Source
Sources 是可以寻找和提供 servables 的模块,每个 Source 提供了0个或者多个servable streams,对于每个servable stream,Source 都会提供一个Loader实例;
Sources 可以保存在多个服务或者版本之间共享的state;
Aspired Versions
Aspired Versions 是可以被加载服务版本集合;
Managers
管理 Servable 的整个的生命周期,包括:
loading Servables
serving Servables
unloading Servables
如果服务要求的资源不足,Manager 可能会拒绝加载新的模型;
流程
流程图
简单来说:
Source 为 Servable 创建 Loader 对象;
Loader 被作为 Aspired Versions 发送给 Manager,之后 Manager 加载指定的服务去处理客户端的请求;
具体来说:
Source 为指定的服务创建Loader,Loader里包含了服务所需要的元数据(模型);
之后 Source 使用 回掉函数通知 Manager 的 Aspired Version(servable version的集合);
Manager 根据配置的Version Policy决定下一步的操作(是否 unload 之前的servable,或者 load 新的servable);
如果 Manager 判定是操作安全的,就会给 Loader 要求的resource并让 Loader 加载新的版本;
客户端向 Manager 请求服务,可以指定服务版本或者只是请求最新的版本。Manager 返回服务端的处理结果;
Source 可以看作不断更新weights的graph,weights 存储在磁盘中
例如:
Source 选择了一个新版本的模型,创建了一个Loader,Loader包含指向模型文件路径的指针;
Source 使用回掉函数通知 Manager 的 Aspired Version;
根据 Version Policy ,Manager 决定加载新的模型;
Manager 通知 Loader 有足够的内存用于模型加载,Loader 使用新的weights将graph实例化;
客户端发送请求,Manager 返回处理结果;
生成可用于tensorflow serving的模型
环境:Tensorflow 1.0.1
模型:基于LSTM的文本情感分类的模型:
import os,time
import tensorflow as tf
import online_lstm_model
from utils import data_utils,data_process
from lib import config
#载入词向量,生成字典
embedding_matrix, word_list = data_utils.load_pretained_vector(config.WORD_VECTOR_PATH)
data_utils.create_vocabulary(config.VOCABULARY_PATH, w