容器化运行TensorFlow Serving

现如今,ModelServer已经开始大行其道。 以前训练了一个模型,还必须得用FlaskFalcon手写一个Server的时代,快要一去不复返了。

TensorFlow作为一个流行的AI框架,早已自己解决ModelServer的问题。 并且发展迅速,目前已经成为了可用于生产环境的高性能解决方案。

TensorFlow Serving is a flexible, high-performance serving system for machine learning models, designed for production environments. TensorFlow Serving makes it easy to deploy new algorithms and experiments, while keeping the same server architecture and APIs. TensorFlow Serving provides out of the box integration with TensorFlow models, but can be easily extended to serve other types of models.

TensorFlow Serving

Dockerized TensorFlow Serving

利用Docker运行一个TensorFlow的ModelServe,是非常容易的。

docker pull tensorflow/serving:latest
docker pull tensorflow/serving:latest-gpu

按需选择CPU或GPU版本(GPU版本的镜像大愈百倍)。 如果需要指定版本,可以看其tags

以下列出几个版本(并非全部)及其镜像大小(2018年11月):

TensorFlow Version Tag (Compressed) Size Comment
1.12.0 latest, 1.12.0 67 MB 目前最新CPU版本
1.12.0 latest-gpu, 1.12.0-gpu 1 GB 目前最新GPU版本
1.10.1 1.10.1 62 MB
1.10.1 1.10.1-gpu 919 MB
1.8.0 1.8.0 98 MB
1.8.0 1.8.0-devel-gpu 2 GB
1.6.1 1.6.1 98 MB 官方Docker最低CPU版本
1.6.1 1.6.1-devel-gpu 2 GB 官方Docker最低GPU版本

准备model

训练好的模型,需要利用SavedModelBuilder,导出为一个SavedModel。

SavedModelBuilder导出的官方示例:

export_path_base = sys.argv[-1]
export_path = os.path.join(
      compat.as_bytes(export_path_base),
      compat.as_bytes(str(FLAGS.model_version)))
print 'Exporting trained model to', export_path
builder = tf.saved_model.builder.SavedModelBuilder(export_path)
builder.add_meta_graph_and_variables(
      sess, [tag_constants.SERVING],
      signature_def_map={
           'predict_images':
               prediction_signature,
           signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY:
               classification_signature,
      },
      legacy_init_op=legacy_init_op)
builder.save()

导出后的SavedModel,应该具有以下目录结构:

assets/
assets.extra/
variables/
    variables.data-?????-of-?????
    variables.index
saved_model.pb

运行服务

首先,确保dockerdocker-compose已安装。 docker的安装略,docker-compose的安装可以通过pip来完成。

sudo -H pip install docker-compose

新建一个目录,里面除了放置Model(这里叫model0)以外,再新增一个docker-compolse.yaml文件,内容如下:

version: "3"

services:
  serving:
    image: tensorflow/serving:latest
    restart: unless-stopped
    ports:
      - 8500:8500
      - 8501:8501
    volumes:
      - ./model0:/models/MODEL0
    environment:
      - MODEL_NAME=MODEL0
      - NVIDIA_VISIBLE_DEVICES=0

其中,MODLE0为自定义名称,可按需修改。 但是,MODEL_NAME=*volumes里的/models/*,应该保持一致。 NVIDIA_VISIBLE_DEVICES=0是限定只使用第一块显卡。

docker-compose.yaml准备完毕后,在同路径下,执行一行命令即可运行服务。

docker-compose up -d

GPU预留方案

除了NVIDIA_VISIBLE_DEVICES=0,Docker Swarm还支持另一种GPU限定方案。

    deploy:
      replicas: 2
      resources:
        reservations:
          generic_resources:
          - discrete_resource_spec:
              kind: 'gpu'
              value: 1

这里表示预留1个单位的GPU。 需要docker-compose的版本在3.5以上。

遗憾的是,还没有limit的限定方案,设不了上限。

serve成功的log示例

docker-compose up-d参数,会让服务在后台运行。 如果想查看log,可以用docker-compose logs

$ docker-compose logs -f
serving_1  | 2018-11-12 11:45:29.679213: I tensorflow_serving/model_servers/main.cc:157] Building single TensorFlow model file config:  model_name: MODEL0 model_base_path: /models/MODEL0
serving_1  | 2018-11-12 11:45:29.679673: I tensorflow_serving/model_servers/server_core.cc:462] Adding/updating models.
serving_1  | 2018-11-12 11:45:29.679700: I tensorflow_serving/model_servers/server_core.cc:517]  (Re-)adding model: MODEL0
serving_1  | 2018-11-12 11:45:29.780031: I tensorflow_serving/core/basic_manager.cc:739] Successfully reserved resources to load servable {name: MODEL0 version: 1}
serving_1  | 2018-11-12 11:45:29.780088: I tensorflow_serving/core/loader_harness.cc:66] Approving load for servable version {name: MODEL0 version: 1}
serving_1  | 2018-11-12 11:45:29.780101: I tensorflow_serving/core/loader_harness.cc:74] Loading servable version {name: MODEL0 version: 1}
serving_1  | 2018-11-12 11:45:29.780127: I external/org_tensorflow/tensorflow/contrib/session_bundle/bundle_shim.cc:360] Attempting to load native SavedModelBundle in bundle-shim from: /models/MODEL0/1
serving_1  | 2018-11-12 11:45:29.780140: I external/org_tensorflow/tensorflow/cc/saved_model/reader.cc:31] Reading SavedModel from: /models/MODEL0/1
serving_1  | 2018-11-12 11:45:29.847226: I external/org_tensorflow/tensorflow/cc/saved_model/reader.cc:54] Reading meta graph with tags { serve }
serving_1  | 2018-11-12 11:45:29.862407: I external/org_tensorflow/tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
serving_1  | 2018-11-12 11:45:29.920827: I external/org_tensorflow/tensorflow/cc/saved_model/loader.cc:113] Restoring SavedModel bundle.
serving_1  | 2018-11-12 11:45:29.920907: I external/org_tensorflow/tensorflow/cc/saved_model/loader.cc:123] The specified SavedModel has no variables; no checkpoints were restored.
serving_1  | 2018-11-12 11:45:29.920917: I external/org_tensorflow/tensorflow/cc/saved_model/loader.cc:148] Running LegacyInitOp on SavedModel bundle.
serving_1  | 2018-11-12 11:45:29.920932: I external/org_tensorflow/tensorflow/cc/saved_model/loader.cc:233] SavedModel load for tags { serve }; Status: success. Took 140784 microseconds.
serving_1  | 2018-11-12 11:45:29.920969: I tensorflow_serving/servables/tensorflow/saved_model_warmup.cc:83] No warmup data file found at /models/MODEL0/1/assets.extra/tf_serving_warmup_requests
serving_1  | 2018-11-12 11:45:29.921085: I tensorflow_serving/core/loader_harness.cc:86] Successfully loaded servable version {name: MODEL0 version: 1}
serving_1  | 2018-11-12 11:45:29.922849: I tensorflow_serving/model_servers/main.cc:327] Running ModelServer at 0.0.0.0:8500 ...
serving_1  | [warn] getaddrinfo: address family for nodename not supported
serving_1  | 2018-11-12 11:45:29.924139: I tensorflow_serving/model_servers/main.cc:337] Exporting HTTP/REST API at:localhost:8501 ...
serving_1  | [evhttp_server.cc : 235] RAW: Entering the event loop ...

如果运行失败,将提示找不到model之类的信息。

以上配置和运行,都是CPU的版本。 如果要使用GPU版本,除驱动和镜像外,还需准备nvidia-docker或(更推荐使用)nvidia-docker2

使用

1.8.0以上的TensorFlow,除了传统的gRPC以外,还支持RESTful API。 gRPC在8500端口,RESTful API8501端口。

gRPC通信的形式,可以参考prediction_service.proto

service PredictionService {
  // Classify.
  rpc Classify(ClassificationRequest) returns (ClassificationResponse);

  // Regress.
  rpc Regress(RegressionRequest) returns (RegressionResponse);

  // Predict -- provides access to loaded TensorFlow model.
  rpc Predict(PredictRequest) returns (PredictResponse);

  // MultiInference API for multi-headed models.
  rpc MultiInference(MultiInferenceRequest) returns (MultiInferenceResponse);

  // GetModelMetadata - provides access to metadata for loaded models.
  rpc GetModelMetadata(GetModelMetadataRequest)
      returns (GetModelMetadataResponse);
}

RESTful API方式的通信,HTTP报文大致如下:

POST http://host:port/<URI>:<VERB>

URI: /v1/models/${MODEL_NAME}[/versions/${MODEL_VERSION}]
VERB: classify|regress|predict

内容为JSON,classifyregress的示例如下(predict略):

{
  // Optional: serving signature to use.
  // If unspecifed default serving signature is used.
  "signature_name": <string>,

  // Optional: Common context shared by all examples.
  // Features that appear here MUST NOT appear in examples (below).
  "context": {
    "<feature_name3>": <value>|<list>
    "<feature_name4>": <value>|<list>
  },

  // List of Example objects
  "examples": [
    {
      // Example 1
      "<feature_name1>": <value>|<list>,
      "<feature_name2>": <value>|<list>,
      ...
    },
    {
      // Example 2
      "<feature_name1>": <value>|<list>,
      "<feature_name2>": <value>|<list>,
      ...
    }
    ...
  ]
}

Request发送成功后,Response大致如下。

classify

{
  "result": [
    // List of class label/score pairs for first Example (in request)
    [ [<label1>, <score1>], [<label2>, <score2>], ... ],

    // List of class label/score pairs for next Example (in request)
    [ [<label1>, <score1>], [<label2>, <score2>], ... ],
    ...
  ]
}

regress

{
  // One regression value for each example in the request in the same order.
  "result": [ <value1>, <value2>, <value3>, ...]
}

参考

以上TensorFlow网址,使用的是境内tensorflow.google.cn的网址。 最新内容可访问tensorflow.org,自备工具。


相关笔记