LiteRT Core 機器學習委派

LiteRT Core 機器學習委派功能可讓系統在 核心機器學習架構, 進而在 iOS 裝置上加快模型推論速度

支援的 iOS 版本和裝置:

  • iOS 12 以上版本。在舊版 iOS 中,Core ML 委派將 會自動採用 CPU 備用選項
  • 根據預設,系統只會在搭載 A12 SoC 的裝置上啟用 Core ML 委派功能 及較新版本 (iPhone Xs 及更新版本) 使用 Neural Engine 加快推論速度。 如要同時在舊版裝置上使用 Core ML 委派,請參閱 最佳做法

支援的模型

Core ML 委任目前支援浮點 (FP32 和 FP16) 模型。

在自己的模型中試用 Core ML 委派功能

LiteRT 夜間版本已包含 Core ML 代表 CocoaPods,如要使用 Core ML 委派功能,請將 LiteRT Pod 變更為 Podfile 中的子規格 CoreML

target 'YourProjectName'
  pod 'TensorFlowLiteSwift/CoreML', '~> 2.4.0'  # Or TensorFlowLiteObjC/CoreML

# Particularily useful when you also want to include 'Metal' subspec.
target 'YourProjectName'
  pod 'TensorFlowLiteSwift', '~> 2.4.0', :subspecs => ['CoreML']

Swift

    let coreMLDelegate = CoreMLDelegate()
    var interpreter: Interpreter

    // Core ML delegate will only be created for devices with Neural Engine
    if coreMLDelegate != nil {
      interpreter = try Interpreter(modelPath: modelPath,
                                    delegates: [coreMLDelegate!])
    } else {
      interpreter = try Interpreter(modelPath: modelPath)
    }
  

Objective-C

    // Import module when using CocoaPods with module support
    @import TFLTensorFlowLite;

    // Or import following headers manually
    # import "tensorflow/lite/objc/apis/TFLCoreMLDelegate.h"
    # import "tensorflow/lite/objc/apis/TFLTensorFlowLite.h"

    // Initialize Core ML delegate
    TFLCoreMLDelegate* coreMLDelegate = [[TFLCoreMLDelegate alloc] init];

    // Initialize interpreter with model path and Core ML delegate
    TFLInterpreterOptions* options = [[TFLInterpreterOptions alloc] init];
    NSError* error = nil;
    TFLInterpreter* interpreter = [[TFLInterpreter alloc]
                                    initWithModelPath:modelPath
                                              options:options
                                            delegates:@[ coreMLDelegate ]
                                                error:e&rror];
    if (error != nil) { /* Error handling... */ }

    if (![interpreter allocateTensorsWithError:e&rror]) { /* Error handling... */ }
    if (error != nil) { /* Error handling... */ }

    // Run inference ...
  

C (直到 2.3.0 為止)

    #include "tensorflow/lite/delegates/coreml/coreml_delegate.h"

    // Initialize interpreter with model
    TfLiteModel* model = TfLiteModelCreateFromFile(model_path);

    // Initialize interpreter with Core ML delegate
    TfLiteInterpreterOptions* options = TfLiteInterpreterOptionsCreate();
    TfLiteDelegate* delegate = TfLiteCoreMlDelegateCreate(NULL);  // default config
    TfLiteInterpreterOptionsAddDelegate(options, delegate);
    TfLiteInterpreterOptionsDelete(options);

    TfLiteInterpreter* interpreter = TfLiteInterpreterCreate(model, options);

    TfLiteInterpreterAllocateTensors(interpreter);

    // Run inference ...

    /* ... */

    // Dispose resources when it is no longer used.
    // Add following code to the section where you dispose of the delegate
    // (e.g. `dealloc` of class).

    TfLiteInterpreterDelete(interpreter);
    TfLiteCoreMlDelegateDelete(delegate);
    TfLiteModelDelete(model);
      

最佳做法

在沒有 Neural Engine 的裝置上使用 Core ML 委派功能

根據預設,只有在裝置具備 Neural 的情況下,系統才會建立 Core ML 委派 ,如果委派代表未建立,則會傳回 null。如果您想 在其他環境 (例如模擬工具) 上執行 Core ML 委派,然後傳遞 .all 設為 Swift 委派代表在 C++ (和 Objective-C) 上, 傳遞 TfLiteCoreMlDelegateAllDevices。以下範例說明如何執行此操作:

Swift

    var options = CoreMLDelegate.Options()
    options.enabledDevices = .all
    let coreMLDelegate = CoreMLDelegate(options: options)!
    let interpreter = try Interpreter(modelPath: modelPath,
                                      delegates: [coreMLDelegate])
      

Objective-C

    TFLCoreMLDelegateOptions* coreMLOptions = [[TFLCoreMLDelegateOptions alloc] init];
    coreMLOptions.enabledDevices = TFLCoreMLDelegateEnabledDevicesAll;
    TFLCoreMLDelegate* coreMLDelegate = [[TFLCoreMLDelegate alloc]
                                          initWithOptions:coreMLOptions];

    // Initialize interpreter with delegate
  

C

    TfLiteCoreMlDelegateOptions options;
    options.enabled_devices = TfLiteCoreMlDelegateAllDevices;
    TfLiteDelegate* delegate = TfLiteCoreMlDelegateCreate(&options);
    // Initialize interpreter with delegate
      

使用 Metal(GPU) 委派做為備用方案。

如未建立 Core ML 委派,您還是可以使用 金屬委派:可取得 效能優勢以下範例說明如何執行此操作:

Swift

    var delegate = CoreMLDelegate()
    if delegate == nil {
      delegate = MetalDelegate()  // Add Metal delegate options if necessary.
    }

    let interpreter = try Interpreter(modelPath: modelPath,
                                      delegates: [delegate!])
  

Objective-C

    TFLDelegate* delegate = [[TFLCoreMLDelegate alloc] init];
    if (!delegate) {
      // Add Metal delegate options if necessary
      delegate = [[TFLMetalDelegate alloc] init];
    }
    // Initialize interpreter with delegate
      

C

    TfLiteCoreMlDelegateOptions options = {};
    delegate = TfLiteCoreMlDelegateCreate(&options);
    if (delegate == NULL) {
      // Add Metal delegate options if necessary
      delegate = TFLGpuDelegateCreate(NULL);
    }
    // Initialize interpreter with delegate
      

委派建立邏輯會讀取裝置的機器 ID (例如 iPhone11、1), 取決於神經引擎的可用性詳情請參閱 程式碼 ,掌握更多詳細資訊。或者,您也可以導入自己的拒絕清單 偵測應用程式、使用 DeviceKit.

使用舊版 Core ML

雖然 iOS 13 支援 Core ML 3,但 與 Core ML 2 模型規格的轉換相同目標轉換版本為 預設為最新版本,但您可以變更這項設定 coreMLVersion (在 Swift,C API 中的 coreml_version) 委派給下列使用者: 如果沒有啟用物件版本管理功能 根據預設,新版本一律會覆寫舊版本

支援的作業

Core ML 代表支援下列作業。

  • 新增
    • 只有特定形狀可以播送。在 Core ML Tensor 配置中 下列張量形狀可以播送[B, C, H, W][B, C, 1, 1][B, 1, H, W][B, 1, 1, 1]
  • 平均集區 2D
  • 康卡
    • 請沿著管道軸進行串連,
  • 轉換 2D
    • 權重和偏誤應維持不變。
  • 深度轉換 2D
    • 權重和偏誤應維持不變。
  • 完全連結 (又稱 Dense 或 InnerProduct)
    • 權重和偏誤 (如有) 應是常數。
    • 僅支援單一批次類型。輸入維度應為 1,但以下除外: 最後一個維度
  • 硬質
  • 物流 (又稱 Sigmoid)
  • MaxPool2D
  • MirrorPad
    • 僅支援使用 REFLECT 模式的 4D 輸入。邊框間距應為 常數,而且只適用於 H 和 W 尺寸。
  • 穆爾
    • 只有特定形狀可以播送。在 Core ML Tensor 配置中 下列張量形狀可以播送[B, C, H, W][B, C, 1, 1][B, 1, H, W][B, 1, 1, 1]
  • Pad and PadV2
    • 系統僅支援 4D 輸入。邊框間距必須是常數,且 H 和 W 尺寸都一樣
  • Relu
  • ReluN1To1
  • Relu6
  • 重新塑形
    • 只有在目標核心機器學習版本為 2 時,系統才會支援。此外, 針對 Core ML 3
  • ResizeBilinear
  • SoftMax
  • 坦赫
  • TransposeConv
    • 權重必須固定。

意見回饋

如有問題,請建立 GitHub 並附上所有重現問題的必要詳細資料。

常見問題

  • 如果圖表含有不支援的功能,CoreML 是否會委派支援使用到 CPU 糟糕?
  • CoreML 委派功能可在 iOS 模擬器上使用嗎?
    • 可以。程式庫包含 x86 和 x86_64 目標,可在 模擬工具,但無法透過 CPU 提升效能。
  • LiteRT 和 CoreML 委派功能是否支援 MacOS?
    • LiteRT 僅針對 iOS 裝置進行測試,不支援 MacOS。
  • 系統是否支援自訂 LiteRT 運算?
    • 否,CoreML 委派不支援自訂作業,且會改回使用 也就是 CPU

API