与TensorFlow模型推断相比,CPU上的TensorFlow模型服务器的性能问题



i使用TensorFlow模型服务器观察CPU的性能问题。与RAW TensorFlow模型推断相比,它的推断时间翻了一番。两者都使用MKL构建仅用于CPU。

复制的代码:https://github.com/bogdanruzh/tf_model_service_benchmark

TensorFlow MKL构建: bazel build --config=mkl -c opt --copt=-msse4.1 --copt=-msse4.2 --copt=-mavx --copt=-mavx2 --copt=-mfma --copt=-O3 //tensorflow/tools/pip_package:build_pip_package

TensorFlow服务器MKL构建: bazel build --config=mkl --config=opt --copt=-msse4.1 --copt=-msse4.2 --copt=-mavx --copt=-mavx2 --copt=-mfma --copt=-O3 tensorflow_serving/model_servers:tensorflow_model_server

目标模型是用于分割的简单CNN。

RAW TensorFlow模型在0.17s中的图像处理图像。Tensorflow模型服务器处理0.32s中的同一图像。

如何提高这种表现?这对于我的应用程序至关重要。

我想解释会帮助您。据说,使用Intel优化的不良配置TensorFlow的性能可能较差,然后清除构建https://github.com/tensorflow/serving/serving/issues/1272#issuecomment-477878180

您可以尝试配置批处理的参数(使用配置文件和-enable_batching参数)https://github.com/tensorflow/serving/serving/tree/master/master/tensorflow_serving/batching/batching

和set(inter/intra)_op_parallelism_threads。

附加,MKL有自己的标志来提高性能https://www.tensorflow.org/guide/guide/performance/overview#tuning_mkl_for_the_bor_the_best_performance

如果您需要更好的性能,我建议尝试OpenVino。它通过例如,优化了推理时间将修剪并融合一些操作。OpenVino已针对英特尔硬件进行了优化,但应与任何CPU一起使用。但是,我认为当您与MKL支持编译TensorFlow时,您有Intel CPU。

这是各种型号和CPU的一些性能基准。

除非您有精美的自定义层,否则将TensorFlow模型转换为OpenVino是很简单的。有关如何做的完整教程可以在这里找到。下面的一些摘要。

安装OpenVino

最简单的方法是使用PIP。另外,您可以使用此工具在情况下找到最佳方法。

pip install openvino-dev[tensorflow2]

使用模型优化器转换SavedModel模型

模型优化器是来自OpenVino开发软件包的命令行工具。它将TensorFlow模型转换为IR,这是OpenVino的默认格式。您还可以尝试FP16的精度,这应该为您提供更好的性能而不会出现明显的准确性下降(只需更改data_type)即可。在命令行中运行:

mo --saved_model_dir "model" --input_shape "[1, 3, 224, 224]" --data_type FP32 --output_dir "model_ir"

运行推理

可以通过运行时加载转换的模型,并为特定设备进行编译,例如CPU或GPU(像Intel HD图形一样集成到您的CPU中)。如果您不知道什么是最佳选择,请使用自动。

# Load the network
ie = Core()
model_ir = ie.read_model(model="model_ir/model.xml")
compiled_model_ir = ie.compile_model(model=model_ir, device_name="CPU")
# Get output layer
output_layer_ir = compiled_model_ir.output(0)
# Run inference on the input image
result = compiled_model_ir([input_image])[output_layer_ir]

甚至还有OpenVino Model Server,它与TensorFlow Serving非常相似。

免责声明:我在OpenVino上工作。

最新更新