---
title: 编译TensorFlow Serving
description: "TensorFlow Serving 2.15作为推理服务的服务端需要在测试前正确安装。"
url: https://www.hikunpeng.com/document/detail/zh/SRA/perfEval/benchmarksra/kunpengmodelzoo_06_0011.html
sourcePath: /source/zh/SRA/perfEval/benchmarksra/kunpengmodelzoo_06_0011.html
indexId: 4dabd14b560e0c3d02d22a2cc8a6659e705966f682f53115b8a9113160e2eee665
---
# 编译TensorFlow Serving

TensorFlow Serving 2.15作为推理服务的服务端需要在测试前正确安装。

1. 请参见《TensorFlow Serving推理部署框架 移植指南》中的“配置编译环境(https://www.hikunpeng.com/document/detail/zh/SRA/ecosystemEnable/TensorFlowServing/kunpengtfserving_02_0005.html)”章节，准备TensorFlow Serving编译环境。
2. 下载优化补丁。
  1 2 git clone -b v2.15.0-2512 https://gitcode.com/boostkit/tensorflow.git sra-tensorflow git clone -b v2.15.1-2512 https://gitcode.com/boostkit/tensorflow-serving.git sra-serving

3. 下载开源TensorFlow 2.15.0版本、TensorFlow Serving 2.15.1版本。
  1 2 git clone -b v2.15.0 https://github.com/tensorflow/tensorflow.git git clone -b 2.15.1 https://github.com/tensorflow/serving.git

4. 将优化补丁合入开源TensorFlow、TensorFlow Serving目录中。
  1 2 3 4 cp /path/to/sra-tensorflow/0001-boostsra-tensorflow.patch /path/to/tensorflow/ cp /path/to/sra-serving/0001-boostsra-tensorflow-serving.patch /path/to/serving/ cd /path/to/tensorflow && patch -p1 < 0001-boostsra-tensorflow.patch cd /path/to/serving && patch -p1 < 0001-boostsra-tensorflow-serving.patch

5. 进入“serving”目录。
  1 cd /path/to/serving/

6. 设置Bazel路径为配置编译环境章节编译的Bazel二进制所在的目录，创建编译依赖存储目录。
  1 2 3 export BAZEL_PATH=/path/to/bazel/bazel-6.5.0/output export DISTDIR=$(pwd)/download mkdir -p $DISTDIR

7. 执行构建脚本编译TensorFlow Serving。
  1 sh compile_serving.sh --tensorflow_dir /path/to/tensorflow --features gcc12

  “/path/to/tensorflow”指定TensorFlow路径；“gcc12”表示使用GCC 12.3.1版本编译。

  构建的结果为TensorFlow Serving二进制文件“tensorflow_model_server”，文件路径为“/path/to/serving/bazel-bin/tensorflow_serving/model_servers/tensorflow_model_server”。

  构建脚本compile_serving.sh中执行的编译命令如下，部分参数说明如表1
  所示。

```
bazel --output_user_root=$BAZEL_COMPILE_CACHE build -c opt --distdir=$DISTDIR --override_repository=org_tensorflow=$TENSORFLOW_DIR \
--copt=-march=armv8.3-a+crc --copt=-O3 --copt=-fprefetch-loop-arrays --copt=-Wno-error=maybe-uninitialized  \
--copt=-Werror=stringop-overflow=0 \
--define tflite_with_xnnpack=false tensorflow_serving/model_servers:tensorflow_model_server
```


**表1 构建脚本compile_serving.sh中执行的编译命令部分参数说明**

| 参数名称 | 参数说明 |  |  |
| --- | --- | --- | --- |
| \-\-output\_user\_root | Bazel编译缓存目录，默认为“/path/to/serving/output”。可通过环境变量BAZEL\_COMPILE\_CACHE设置自定义路径，命令如下。 1 export BAZEL\_COMPILE\_CACHE=/path/to/your/cache\_dir | 1 | export BAZEL\_COMPILE\_CACHE=/path/to/your/cache\_dir |
| 1 | export BAZEL\_COMPILE\_CACHE=/path/to/your/cache\_dir |  |  |
| \-\-distdir | TensorFlow Serving编译依赖存放目录，用来解决第三方依赖包下载失败问题。 |  |  |
| \-\-override\_repository | 指定使用本地TensorFlow构建，使用tensorflow\_dir指定目录作为本地TensorFlow。 |  |  |


  构建常见报错请参见《TensorFlow 移植指南(https://www.hikunpeng.com/document/detail/zh/SRA/ecosystemEnable/TensorFlow/kunpengtensorflow_02_0012.html)》、《TensorFlow Serving推理部署框架 移植指南(https://www.hikunpeng.com/document/detail/zh/SRA/ecosystemEnable/TensorFlowServing/kunpengtfserving_02_0014.html)》和故障排除进行解决。
