查看原文
其他

实现 iPhone 和 iPad 上的更快推理:TensorFlow Lite Core ML Delegate

Google TensorFlow 2021-08-05

文 / Tei Jeong 和 Karim Nosseir,软件工程师 

TensorFlow Lite 面向 GPU、DSP 和/或 NPU 等加速器提供了部分或整套模型推理的 Delegate 选项,旨在实现高效的移动推理。在 Android 上,有几种 Delegate 可供选择:NNAPIGPU 及最近添加的 Hexagon delegate。此前,Apple 的移动设备 iPhone 和 iPad 上仅可使用 GPU delegate。


Apple 发布机器学习框架 Core ML 和 Neural Engine(Apple Bionic SoC 中的一种神经处理单元 (NPU))后,TensorFlow Lite 也可利用 Apple 的硬件。


神经处理单元 (Neural processing units, NPU) 与 Google 的 Edge TPU 和 Apple 的 Neural Engine 类似,是专为加速机器学习应用而设计的硬件加速器。这类芯片用于加速移动或边缘设备上的模型推理,与在 CPU 或 GPU 上运行推理相比,功耗更低。


今天,我们很高兴发布一款全新的 TensorFlow Lite Delegate,使用 Neural Engine 及 Apple 的 Core ML API 让浮点模型可以更快速地在 iPhone 和 iPad 上运行。借助它 ,MobileNet 和 Inception V3 等模型能够实现高达 14 倍的性能提升(参见以下详细信息)。

图 1:Delegate 在运行时工作方式的简要概览:计算图中受支持的部分在加速器上运行,其他操作则通过 TensorFlow Lite 内核在 CPU 上运行



支持哪些设备?

此 Delegate 可在 iOS 版本为 12 或更高的 iOS 设备上运行(包括 iPadOS)。不过,要获得切实的性能收益,建议在搭载 Apple A12 SoC 或更高版本的设备上运行,例如 iPhone XS。对于旧版 iPhone,建议使用 TensorFlow Lite GPU Delegate 来获得更高的性能。



支持哪些模型?

初始发布版中支持 32 位浮点模型。支持模型的示例包括但不限于图像分类、对象检测、对象分割以及姿势预测模型。此 delegate 支持许多运算量大的算子(如卷积),对于某些算子会存在特定限制。在进入运行时之前 Delegate 会检查这些限制,将不受支持的算子自动退回到 CPU。关于算子及对应限制(如有)的完整列表,请参阅 delegate 文档



对性能的影响

我们使用两个常见的浮点模型 MobileNet V2 和 Inception V3 进行测试。基准测试在 iPhone 8+(A11 SoC)、iPhone XS(A12 SoC)和 iPhone 11 Pro(A13 SoC)上进行,测试了以下三个 delegate 选项:仅使用 CPU(无 delegate)、GPU 和 Core ML delegate。如前所述,搭载 A12 SoC及更高版本的模型能够实现性能加速,但在 iPhone 8+ 上,由于第三方无法使用 Neural Engine,在小型模型上使用 Core ML delegate,未观察到性能增益。而应用于较大模型时,性能与使用 GPU delegate 类似。


除了模型推理延迟时间之外,我们还测试了启动延迟时间。请注意,速度提升会以启动延迟时间为代价。Core ML delegate 的启动延迟时间会随模型大小的增加而延长。例如,在如 MobileNet 的较小模型上,我们观察到启动延迟时间介于 200-400 毫秒之间。而对于如 Inception V3 等较大模型,启动延迟时间则可能长达 2-4 秒。为此,我们正在努力缩短启动延迟时间。Delegate 对二进制文件的大小也有影响。使用 Core ML delegate 后,二进制文件大小最多可能增加 1 MB。


模型

  • MobileNet V2 (1.0, 224, float) [下载]:图像分类

    • 小模型。整个计算图均在 Core ML 上运行。

  • Inception V3 [下载]:图像分类

    • 大模型。整个计算图均在 Core ML 上运行。


设备

  • iPhone 8+(Apple A11、iOS 13.2.3)

  • iPhone XS(Apple A12、iOS 13.2.3)

  • iPhone 11 Pro(Apple A13、iOS 13.2.3)


在 MobileNet V2 模型上观察到的延迟时间和加速

图 2:在 MobileNet V2 模型上观察到的延迟时间和加速:均使用浮点模型。CPU 基线为双线程 TensorFlow Lite 内核

* GPU:Core ML 使用 CPU 和 GPU 进行推理。NPU:Core ML 使用 CPU 和 GPU 以及 NPU (Neural Engine) 进行推理


在 Inception V3 模型上观察到的延迟时间和加速

图 3:在 Inception V3 模型上观察到的延迟时间和加速。所有版本均使用浮点模型。CPU 基线为双线程 TensorFlow Lite 内核

* GPU:Core ML 使用 CPU 和 GPU 进行推理。NPU:Core ML 使用 CPU 和 GPU 以及 NPU (Neural Engine) 进行推理



如何使用 Delegate?

您只需通过 TensorFlow Lite Interpreter 调用一个新 delegate 实例即可。有关详细说明,请阅读完整文档。您可以使用 Swift API(示例如下)或 C++ API(文档中所示)在推理期间调用 TensorFlow Lite delegate。


Swift 示例

这是一个典型 Swift 应用调用 delegate 的具体操作。您只需创建一个新的 Core ML delegate,然后将其传递给原始 interpreter 初始化代码即可。

let coreMLDelegate = CoreMLDelegate()
let interpreter = try Interpreter(modelPath: modelPath,
delegates: [coreMLDelegate])



研究展望

未来几个月,我们将继续改进 delegate,支持更多算子,并实现进一步优化。在产品路线图中,我们还将支持通过训练后的半精度浮点量化 (float16 quantization) 训练的模型。这将在模型大小大约减半且精度损失较小的情况下,实现模型加速。


产品路线图中同时涵盖了对训练后权重量化(也称为动态量化)的支持。



反馈

此功能源于我们从开发者处收到的常见功能请求。我们很高兴发布此功能,并期待聆听您的想法。您也可以直接分享您的用例。如要提 bug 或 issue,请在 GitHub 上联系我们。



致谢

感谢 Tei Jeong、Karim Nosseir、Sachin Joglekar、Jared Duke、Wei Wei 和 Khanh LeViet。

注:Core ML、Neural Engine 和 Bionic SoC(A12、A13)是 Apple Inc. 的产品。



如果您想详细了解 本文提及 的相关内容,请参阅以下文档。这些文档深入探讨了这篇文章中提及的许多主题:

  • NNAPI
    https://tensorflow.google.cn/lite/performance/nnapi

  • GPU / TensorFlow Lite GPU Delegate
    https://tensorflow.google.cn/lite/performance/gpu

  • Hexagon
    https://tensorflow.google.cn/lite/performance/hexagon_delegate

  • Core ML
    https://developer.apple.com/documentation/coreml

  • Edge TPU 
    https://coral.ai/docs/accelerator/get-started/

  • TensorFlow Lite Delegate
    https://tensorflow.google.cn/lite/performance/delegates

  • delegate 文档
    https://tensorflow.google.cn/lite/performance/coreml_delegate

  • MobileNet V2 下载
    https://storage.googleapis.com/download.tensorflow.org/models/tflite_11_05_08/mobilenet_v2_1.0_224.tgz

  • Inception V3 下载
    https://storage.googleapis.com/download.tensorflow.org/models/tflite/model_zoo/upload_20180427/inception_v3_2018_04_27.tgz

  • 完整文档
    https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/g3doc/performance/coreml_delegate.md

  • 训练后的半精度浮点量化
    https://tensorflow.google.cn/lite/performance/post_training_float16_quant

  • 训练后权重量化
    https://tensorflow.google.cn/lite/performance/post_training_quantization#weight_quantization

  • 分享您的用例
    http://services.google.cn/fb/forms/TFCS/?channel=wechat

  • GitHub 
    https://github.com/tensorflow/tensorflow/issues



—推荐阅读—



了解更多请点击 “阅读原文” 访问官网。

    您可能也对以下帖子感兴趣

    文章有问题?点此查看未经处理的缓存