tensorflow-serving-gpu 本地编译并使用

因为要部署,模型比较大,所以通常官网的pip install 和bazel 教程编译的都是cpu版本的代码,

所以为了感受下gpu就尝试自己编译了,首先,下载源码:

git clone --recurse-submodules https://github.com/tensorflow/serving

这里有个坑,下载玩serving代码后还要在在子模块,但是子模块主要包含两个:

tensorflow

tf_models

tensorflow是蛮快下载的,但是,tf_models却很麻烦,网速慢还文件大。后面我没下载下来

就直接取消了,不过这样就可以了。然后:

cd tensorflow
./configure

配置下就行,关于support,只留cuda相关配置就好了,有一个要注意的,对于cuda路径我经常写/usr/local/cuda,但是常失败,后面尝试/usr/local/cuda-8.0 就ok(我的是cuda8.0,/usr/local/cuda实际指向的是/usr/local/cuda-8.0)。

bazel build -c opt --config=cuda tensorflow_serving/...
然后,当然啦,报了一个通常的错误关于crosstool的。
tensorflow/third_party/gpus/crosstool
解决方法:https://github.com/tensorflow/serving/issues/186#issuecomment-251152755

1. the crosstool in tools/bazel.rc is invalid (AFAIK). change @org_tensorflow//third_party/gpus/crosstool to @local_config_cuda//crosstool:toolchain.

2. the cuda_configure repository rule will fail (haven't looked in to why exactly), but essentially an bazel clean --expunge && export TF_NEED_CUDA=1 will fix this.

简单地讲就是,打开tools/bazel.rc文件,修改@org_tensorflow//third_party/gpus/crosstool 替换为 @local_config_cuda//crosstool:toolchain (一般是第一行)

然后在serving目录下执行bazel clean --expunge && export TF_NEED_CUDA=1 就可以了。

不过,好戏才刚开始,你会发现报各种错,可能是bazel的,可能是cuda的。反正缺这缺那的,我编译个model_server,容易吗????对呀,我只编译model server呀。修改策略:

bazel build -c opt --config=cuda //tensorflow_serving/model_servers:tensorflow_model_server

经过若干时间的等待,终于ok了,之前报错是因为很多七七八八用不到的都在编译了。蛋疼!!!!

使用就很简单啦,看官网。

原文地址:https://www.cnblogs.com/YouXiangLiThon/p/8295499.html