Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >TensorFlow Serving在Kubernetes中的实践

TensorFlow Serving在Kubernetes中的实践

作者头像
Walton
发布于 2018-04-16 03:07:15
发布于 2018-04-16 03:07:15
3.2K00
代码可运行
举报
文章被收录于专栏:KubernetesKubernetes
运行总次数:0
代码可运行

xidianwangtao@gmail.com

关于TensorFlow Serving

下面是TensorFlow Serving的架构图:

关于TensorFlow Serving的更多基础概念等知识,请看官方文档,翻译的再好也不如原文写的好。

这里,我总结了下面一些知识点,我认为是比较重要的:

  • TensorFlow Serving通过Model Version Policy来配置多个模型的多个版本同时serving;
  • 默认只加载model的latest version;
  • 支持基于文件系统的模型自动发现和加载;
  • 请求处理延迟低;
  • 无状态,支持横向扩展;
  • 可以使用A/B测试不同Version Model;
  • 支持从本地文件系统扫描和加载TensorFlow模型;
  • 支持从HDFS扫描和加载TensorFlow模型;
  • 提供了用于client调用的gRPC接口;

TensorFlow Serving配置

当我翻遍整个TensorFlow Serving的官方文档,我还是没找到一个完整的model config是怎么配置的,很沮丧。没办法,发展太快了,文档跟不上太正常,只能撸代码了。

在model_servers的main方法中,我们看到tensorflow_model_server的完整配置项及说明如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
tensorflow_serving/model_servers/main.cc#L314

int main(int argc, char** argv) {
...
    std::vector<tensorflow::Flag> flag_list = {
        tensorflow::Flag("port", &port, "port to listen on"),
        tensorflow::Flag("enable_batching", &enable_batching, "enable batching"),
        tensorflow::Flag("batching_parameters_file", &batching_parameters_file,
                       "If non-empty, read an ascii BatchingParameters "
                       "protobuf from the supplied file name and use the "
                       "contained values instead of the defaults."),
        tensorflow::Flag("model_config_file", &model_config_file,
                       "If non-empty, read an ascii ModelServerConfig "
                       "protobuf from the supplied file name, and serve the "
                       "models in that file. This config file can be used to "
                       "specify multiple models to serve and other advanced "
                       "parameters including non-default version policy. (If "
                       "used, --model_name, --model_base_path are ignored.)"),
        tensorflow::Flag("model_name", &model_name,
                       "name of model (ignored "
                       "if --model_config_file flag is set"),
        tensorflow::Flag("model_base_path", &model_base_path,
                       "path to export (ignored if --model_config_file flag "
                       "is set, otherwise required)"),
        tensorflow::Flag("file_system_poll_wait_seconds",
                       &file_system_poll_wait_seconds,
                       "interval in seconds between each poll of the file "
                       "system for new model version"),
        tensorflow::Flag("tensorflow_session_parallelism",
                       &tensorflow_session_parallelism,
                       "Number of threads to use for running a "
                       "Tensorflow session. Auto-configured by default."
                       "Note that this option is ignored if "
                       "--platform_config_file is non-empty."),
        tensorflow::Flag("platform_config_file", &platform_config_file,
                       "If non-empty, read an ascii PlatformConfigMap protobuf "
                       "from the supplied file name, and use that platform "
                       "config instead of the Tensorflow platform. (If used, "
                       "--enable_batching is ignored.)")};
...
}

因此,我们看到关于model version config的配置,全部在--model_config_file中进行配置,下面是model config的完整结构:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
tensorflow_serving/config/model_server_config.proto#L55

// Common configuration for loading a model being served.
message ModelConfig {
  // Name of the model.
  string name = 1;

  // Base path to the model, excluding the version directory.
  // E.g> for a model at /foo/bar/my_model/123, where 123 is the version, the
  // base path is /foo/bar/my_model.
  //
  // (This can be changed once a model is in serving, *if* the underlying data
  // remains the same. Otherwise there are no guarantees about whether the old
  // or new data will be used for model versions currently loaded.)
  string base_path = 2;

  // Type of model.
  // TODO(b/31336131): DEPRECATED. Please use 'model_platform' instead.
  ModelType model_type = 3 [deprecated = true];

  // Type of model (e.g. "tensorflow").
  //
  // (This cannot be changed once a model is in serving.)
  string model_platform = 4;

  reserved 5;

  // Version policy for the model indicating how many versions of the model to
  // be served at the same time.
  // The default option is to serve only the latest version of the model.
  //
  // (This can be changed once a model is in serving.)
  FileSystemStoragePathSourceConfig.ServableVersionPolicy model_version_policy =
      7;

  // Configures logging requests and responses, to the model.
  //
  // (This can be changed once a model is in serving.)
  LoggingConfig logging_config = 6;
}

我们看到了model_version_policy,那便是我们要找的配置,它的定义如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
tensorflow_serving/sources/storage_path/file_system_storage_path_source.proto

message ServableVersionPolicy {
    // Serve the latest versions (i.e. the ones with the highest version
    // numbers), among those found on disk.
    //
    // This is the default policy, with the default number of versions as 1.
    message Latest {
      // Number of latest versions to serve. (The default is 1.)
      uint32 num_versions = 1;
    }

    // Serve all versions found on disk.
    message All {
    }

    // Serve a specific version (or set of versions).
    //
    // This policy is useful for rolling back to a specific version, or for
    // canarying a specific version while still serving a separate stable
    // version.
    message Specific {
      // The version numbers to serve.
      repeated int64 versions = 1;
    }
}

因此model_version_policy目前支持三种选项:

  • all: {} 表示加载所有发现的model;
  • latest: { num_versions: n } 表示只加载最新的那n个model,也是默认选项;
  • specific: { versions: m } 表示只加载指定versions的model,通常用来测试;

因此,通过tensorflow_model_server —port=9000 —model_config_file=<file>启动时,一个完整的model_config_file格式可参考如下:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
model_config_list: {
	config: {
		name: "mnist",
		base_path: "/tmp/monitored/_model",mnist
		model_platform: "tensorflow",
		model_version_policy: {
		   all: {}
		}
	},
	config: {
		name: "inception",
		base_path: "/tmp/monitored/inception_model",
		model_platform: "tensorflow",
		model_version_policy: {
		   latest: {
		   	num_versions: 2
		   }
		}
	},
	config: {
		name: "mxnet",
		base_path: "/tmp/monitored/mxnet_model",
		model_platform: "tensorflow",
		model_version_policy: {
		   specific: {
		   	versions: 1
		   }
		}
	}
}

TensorFlow Serving编译

其实TensorFlow Serving的编译安装,在github setup文档中已经写的比较清楚了,在这里我只想强调一点,而且是非常重要的一点,就是文档中提到的:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Optimized build

It's possible to compile using some platform specific instruction sets (e.g. AVX) that can significantly improve performance. Wherever you see 'bazel build' in the documentation, you can add the flags -c opt --copt=-msse4.1 --copt=-msse4.2 --copt=-mavx --copt=-mavx2 --copt=-mfma --copt=-O3 (or some subset of these flags). For example:

bazel build -c opt --copt=-msse4.1 --copt=-msse4.2 --copt=-mavx --copt=-mavx2 --copt=-mfma --copt=-O3 tensorflow_serving/...
Note: These instruction sets are not available on all machines, especially with older processors, so it may not work with all flags. You can try some subset of them, or revert to just the basic '-c opt' which is guaranteed to work on all machines.

这很重要,开始的时候我们并没有加上对应的copt选项进行编译,测试发现这样编译出来的tensorflow_model_server的性能是很差的(至少不能满足我们的要求),client并发请求tensorflow serving的延迟很高(基本上所有请求延迟都大于100ms)。加上这些copt选项时,对同样的model进行同样并发测试,结果99.987%的延迟都在50ms以内,对比悬殊。

关于使用--copt=O2还是O3及其含义,请看gcc optimizers的说明,这里不作讨论。(因为我也不懂...)

那么,是不是都是按照官方给出的一模一样的copt选项进行编译呢?答案是否定的!这取决于你运行TensorFlow Serving的服务器的cpu配置,通过查看/proc/cpuinfo可知道你该用的编译copt配置项:

使用注意事项

  • 由于TensorFlow支持同时serve多个model的多个版本,因此建议client在gRPC调用时尽量指明想调用的model和version,因为不同的version对应的model不同,得到的预测值也可能大不相同。
  • 将训练好的模型复制导入到model base path时,尽量先压缩成tar包,复制到base path后再解压。因为模型很大,复制过程需要耗费一些时间,这可能会导致导出的模型文件已复制,但相应的meta文件还没复制,此时如果TensorFlow Serving开始加载这个模型,并且无法检测到meta文件,那么服务器将无法成功加载该模型,并且会停止尝试再次加载该版本。
  • 如果你使用的protobuf version <= 3.2.0,那么请注意TensorFlow Serving只能加载不超过64MB大小的model。可以通过命令 pip list | grep proto查看到probtobuf version。我的环境是使用3.5.0 post1,不存在这个问题,请你留意。更多请查看issue 582
  • 官方宣称支持通过gRPC接口动态更改model_config_list,但实际上你需要开发custom resource才行,意味着不是开箱即用的。可持续关注issue 380

TensorFlow Serving on Kubernetes

将TensorFlow Serving以Deployment方式部署到Kubernetes中,下面是对应的Deployment yaml:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: tensorflow-serving
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: "tensorflow-serving"
    spec:
      restartPolicy: Always
      imagePullSecrets:
      - name: harborsecret
      containers:
      - name: tensorflow-serving
        image: registry.vivo.xyz:4443/bigdata_release/tensorflow_serving1.3.0:v0.5
        command: ["/bin/sh", "-c","export CLASSPATH=.:/usr/lib/jvm/java-1.8.0/lib/tools.jar:$(/usr/lib/hadoop-2.6.1/bin/hadoop classpath --glob); /root/tensorflow_model_server --port=8900 --model_name=test_model --model_base_path=hdfs://xx.xx.xx.xx:zz/data/serving_model"]
        ports:
        - containerPort: 8900

总结

TensorFlow Serving真的是太棒了,让你轻松的serve你的模型,还提供了基于文件系统的模型自动发现,多种模型加载策略,支持A/B测试等等特性。把它部署在Kubernetes中是那么容易,更是让人欢喜。目前我们已经在TaaS平台中提供TensorFlow Serving服务的自助申请,用户可以很方便的创建一个配置自定义的TensorFlow Serving实例供client调用了,后续将完善TensorFlow Serving的负载均衡弹性伸缩、实例自动创建等。欢迎有兴趣的同学一起交流。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
TensorFlow-Serving的使用实战案例笔记(tf=1.4)
最近在测试一些通用模型+项目,包括:CLUE(tf+pytorch),bert4keras(keras), Kashgari(keras+tf)等。其中如果要部署的话,就有tensorflow-serving和flask的选择了。 这里刚好有一个非常好的实战例子,基于tensorflow 1.x的,比较全面。
悟乙己
2020/03/27
3.2K0
TensorFlow Serving
TensorFlow Serving[1] 可以快速部署 Tensorflow 模型,上线 gRPC 或 REST API。
GoCoding
2021/05/06
5990
TensorFlow Serving
Kubernetes 环境的 Tensorflow Serving on S3
在 Tensorflow 给的官方例子中 Use TensorFlow Serving with Kubernetes,是将模型拷贝到镜像里的,这里是会有点不太灵活,因为更新模型就要重新构建镜像,并且再去更新对应的 Pod。
runzhliu
2020/08/06
1.1K0
tf43:tensorflow Serving gRPC 部署实例
版权声明:本文为博主原创文章,未经博主允许不得转载。有问题可以加微信:lp9628(注明CSDN)。 https://blog.csdn.net/u014365862/article/details/81009551
MachineLP
2019/05/26
2.6K1
Tensorflow Serving模型指向s3地址,Could not find base path?
之前有同学遇到一个问题,通过 Workload 配置一个 Serving 服务的时候,通过 model_config_file 这个选项来指定多个模型文件,配置文件大概长这个样子。
runzhliu
2020/08/06
9100
Jetson TX1上安装Tensorflow Serving遇到的问题总结
本文的目的是分享在TX1上安装Tensorflow Serving时遇到的主要问题,避免重复踩坑。
ericzli
2018/06/29
2.8K0
kubeflow系列(三):模型即服务,关于tensorflow serving的使用
kubeflow 中采用了 tensorflow serving 作为官方的tensorflow模型接口, TensorFlow Serving是GOOGLE开源的一个服务系统,适用于部署机器学习模型,灵活、性能高、可用于生产环境。 TensorFlow Serving可以轻松部署新算法和实验,同时保持相同的服务器架构和API。
机械视角
2020/02/11
1.8K0
如何用TF Serving部署TensorFlow模型
如何将机器学习(ML)模型部署上线至生产环境已成为经常性的热门话题。为此许多公司和框架提出了各种不同的解决方案。
AI研习社
2019/05/08
3.1K0
如何用TF Serving部署TensorFlow模型
基础服务系列-安装TensorFlow Serving
git clone https://github.com/tensorflow/serving
用户2146693
2020/02/11
8180
基础服务系列-安装TensorFlow Serving
学习笔记TF067:TensorFlow Serving、Flod、计算加速,机器学习评测体系,公开数据集
本文介绍了TensorFlow的发展历史、生态系统、基本概念、原理、实战案例、性能测试、与其他框架的对比以及未来的发展方向。作者希望通过对TensorFlow的深入剖析,使读者能够快速掌握TensorFlow的核心思想和功能。
利炳根
2017/11/15
2.1K0
[译]TensorFlow Serving RESTful API
今年六月TensorFlow Serving在以往的gRPC API之外,开始支持RESTful API了,使得访问更加符合常用的JSON习惯,本文翻译自官方文档,提供RESTful API的使用指南,如与官网有出入,以官网为准,以下为正文。
Cloudox
2021/11/23
9440
Tensorflow笔记:通过tf.Serving+Docker部署
很多时候仅仅是线下跑一个模型,对特定一批数据进行预测并不够,需要随时来一个或几个样本都能输出结果。这时候就需要起一个服务,然后随时一个包含数据的请求过来,就返回相应的结果。架起这个服务的过程就称作“部署”。本文主要介绍通过tf.Serving+Docker来部署tensorflow模型的过程。
共产主义搬砖人
2021/09/24
2.4K0
Tensorflow c++ 实践及各种坑
曾子骄
2017/10/13
7.1K12
Tensorflow c++ 实践及各种坑
使用tensorflow-serving部署模型
TensorFlow训练好的模型以tensorflow原生方式保存成protobuf文件后可以用许多方式部署运行。
lyhue1991
2020/07/20
1.4K0
使用tensorflow-serving部署模型
TensorFlow 2.0入门
谷歌于2019年3月6日和7日在其年度TensorFlow开发者峰会上发布了最新版本的TensorFlow机器学习框架。这一新版本使用TensorFlow的方式进行了重大改进。TensorFlow拥有最大的开发者社区之一,从机器学习库到完善的机器学习生态系统已经走过了漫长的道路。
代码医生工作室
2019/06/21
2K0
TensorFlow 2.0入门
构建并用 TensorFlow Serving 部署 Wide &amp; Deep 模型
> 正文共6912个字,4张图,预计阅读时间18分钟。 Wide & Deep 模型是谷歌在 2016 年发表的论文中所提到的模型。在论文中,谷歌将 LR 模型与 深度神经网络 结合在一起作为 Google Play 的推荐获得了一定的效果。在这篇论文后,Youtube,美团等公司也进行了相应的尝试并公开了他们的工作(相关链接请看本文底部) 官方提供的 Wide & Deep 模型的(简称,WD 模型)教程 都是使用 TensorFlow (简称,TF )自带的函数来做的特征工程,并且模型也进行了封装,
用户1332428
2018/06/11
1.5K0
十分钟搞定 Tensorflow 服务
Tensorflow 服务是谷歌推荐用来部署 Tensorflow 模型的方法。如果你不具备一定的计算机工程知识背景,即使你对 Tensorflow 本身感觉很顺手,但是我觉得想要搞定 Tensorflow 服务也不是辣么容易的。以下三点是我总结的难点: (谷歌官方)教程含有 C++ 代码(我不会 C++) 教程里含有 kubernetes,gRPG,Bezel(其中一些我也是第一次见) 需要被编译出来。那个过程时间太长了,恐怕要用一个世纪吧! 这里介绍一种可能是最简单的方式——用 tensorflow 服
AI研习社
2018/03/19
1.5K0
十分钟搞定 Tensorflow 服务
掌声送给TensorFlow 2.0!用Keras搭建一个CNN | 入门教程
2019 年 3 月 6 日,谷歌在 TensorFlow 开发者年度峰会上发布了最新版的 TensorFlow 框架 TensorFlow2.0 。新版本对 TensorFlow 的使用方式进行了重大改进,使其更加灵活和更具人性化。具体的改变和新增内容可以从 TensorFlow 的官网找到,本文将介绍如何使用 TensorFlow2.0 构建和部署端到端的图像分类器,以及新版本中的新增内容,包括:
AI科技大本营
2019/05/22
1.6K0
【tensorflow2.0】使用tensorflow-serving部署模型
TensorFlow训练好的模型以tensorflow原生方式保存成protobuf文件后可以用许多方式部署运行。
西西嘛呦
2020/08/26
1.8K0
Model deployment for Triton
NVIDIA Triton Inference Server提供了针对NVIDIA GPU优化的云推理解决方案。服务器通过HTTP或GRPC端点提供推理服务,从而允许远程客户端为服务器管理的任何模型请求推理。对于边缘部署,Triton Server也可以作为带有API的共享库使用,该API允许将服务器的全部功能直接包含在应用程序中。
iResearch666
2023/09/13
1.2K0
Model deployment for Triton
推荐阅读
相关推荐
TensorFlow-Serving的使用实战案例笔记(tf=1.4)
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验