LiteRT Core ML デリゲート

LiteRT Core ML デリゲートを使用すると、Google Cloud 上で LiteRT モデルを実行できます。 Core ML フレームワーク: iOS デバイスでのモデルの推論が高速になります。

サポートされている iOS のバージョンとデバイス:

  • iOS 12 以降。古い iOS バージョンでは、Core ML デリゲートは 自動的にフォールバックされます。
  • デフォルトでは、Core ML デリゲートは A12 SoC を搭載したデバイスでのみ有効になります。 Neural Engine を使用して推論を高速化できます。 古いデバイスでも Core ML デリゲートを使用する場合は、 ベスト プラクティス

サポートされているモデル

現在、Core ML デリゲートは浮動小数点数(FP32 と FP16)モデルをサポートしています。

独自のモデルで Core ML デリゲートを試す

Core ML デリゲートは LiteRT のナイトリー リリースにすでに含まれている 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 デリゲートを使用する

デフォルトでは、Core ML デリゲートは、デバイスにニューラル ネットワーク 委任が作成されていない場合は null が返されます。目標 他の環境(シミュレータなど)で Core ML デリゲートを実行する場合は、.all を渡します。 を使用します。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
      

フォールバックとしてメタル(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)を読み取り、 Neural Engine の可用性を判断します。詳しくは、 コード をご覧ください。または、独自の拒否リストのセットを実装することもできます。 など、他のライブラリを使用するデバイス DeviceKit.

古い Core ML バージョンの使用

iOS 13 は Core ML 3 をサポートしていますが、Core ML 3 が適しているのは Core ML 2 モデル仕様で変換されたモデルです。ターゲット コンバージョンのバージョン: デフォルトで最新バージョンに設定されますが、この設定は coreMLVersion(Swift では、C API では coreml_version) 古いバージョンです。

サポートされているオペレーション

Core ML デリゲートでは、次の演算がサポートされています。

  • 以下を追加
    • ブロードキャストできるのは特定のシェイプのみです。Core ML のテンソル レイアウトでは、 次のテンソル形状はブロードキャスト可能です。[B, C, H, W][B, C, 1, 1][B, 1, H, W][B, 1, 1, 1]
  • 平均プール 2 日
  • 連結 <ph type="x-smartling-placeholder">
      </ph>
    • 連結はチャネルの軸に沿って行う必要があります。
  • コンバージョン(2 日間) <ph type="x-smartling-placeholder">
      </ph>
    • 重みとバイアスは一定である必要があります。
  • DepthwiseConv2D <ph type="x-smartling-placeholder">
      </ph>
    • 重みとバイアスは一定である必要があります。
  • FullyConnected(別名 Dense または InnerProduct) <ph type="x-smartling-placeholder">
      </ph>
    • 重みとバイアス(存在する場合)は一定である必要があります。
    • 単一バッチのケースのみをサポートします。入力ディメンションは 1 にしてください。例外 作成します。
  • ハードスウィッシュ
  • ロジスティック(別名 Sigmoid)
  • 最大プール 2D
  • MirrorPad <ph type="x-smartling-placeholder">
      </ph>
    • REFLECT モードの 4D 入力のみがサポートされています。パディングは 高さと幅の寸法にのみ使用できます。
  • ムル <ph type="x-smartling-placeholder">
      </ph>
    • ブロードキャストできるのは特定のシェイプのみです。Core ML のテンソル レイアウトでは、 次のテンソル形状はブロードキャスト可能です。[B, C, H, W][B, C, 1, 1][B, 1, H, W][B, 1, 1, 1]
  • Pad と PadV2 <ph type="x-smartling-placeholder">
      </ph>
    • 4D 入力のみがサポートされています。パディングは一定で、 横幅と縦幅は指定できません
  • Relu
  • ReluN1To1
  • Relu6
  • 形を変更 <ph type="x-smartling-placeholder">
      </ph>
    • 対象の Core ML バージョンが 2 の場合にのみサポートされ、次の場合はサポートされない Core ML 3 がターゲットになります。
  • ResizeBilinear
  • Softmax
  • タン
  • TransposeConv <ph type="x-smartling-placeholder">
      </ph>
    • 重みは一定である必要があります。

フィードバック

問題がある場合は、 GitHub 報告します。

よくある質問

  • サポートされていない内容がグラフに含まれている場合、CoreML デリゲートは CPU へのフォールバックをサポートしますか? どうでしょうか。
  • iOS Simulator で CoreML デリゲートは機能しますか?
    • はい。ライブラリには x86 および x86_64 ターゲットが含まれているため、 ただし、CPU によるパフォーマンスの向上は見られません。
  • LiteRT と CoreML デリゲートは MacOS をサポートしていますか?
    • LiteRT は iOS でのみテストされ、macOS ではテストされません。
  • カスタム LiteRT オペレーションはサポートされていますか?
    • いいえ。CoreML デリゲートはカスタム演算をサポートしておらず、 できます。

API