LiteRT Core ML 위임

LiteRT Core ML 위임을 사용하면 Core ML 프레임워크에 관해 알아봅니다. iOS 기기에서 모델 추론 속도가 더 빠릅니다.

지원되는 iOS 버전 및 기기:

  • iOS 12 이상. 이전 iOS 버전에서는 Core ML 위임이 자동으로 CPU로 대체됩니다.
  • 기본적으로 Core ML 위임은 A12 SoC가 있는 기기에서만 사용 설정됩니다. 이상 (iPhone Xs 이상)에서는 더 빠른 추론을 위해 Neural Engine을 사용합니다. 이전 기기에서도 Core ML 위임을 사용하려면 다음을 참고하세요. 권장사항

지원되는 모델

Core ML 위임은 현재 부동 (FP32 및 FP16) 모델을 지원합니다.

자체 모델에서 Core ML 위임 시도

Core ML 위임은 이미 LiteRT 야간 출시에 포함되어 있습니다. CocoaPods Core ML 위임을 사용하려면 다음을 포함하도록 LiteRT 포드를 변경하세요. 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 위임은 기기에 Neural이 있는 경우에만 생성됩니다. 엔진에 전달되고 위임이 생성되지 않은 경우 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
      

금속(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 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>
    • 가중치와 편향은 일정해야 합니다.
  • DepthwayConv2D <ph type="x-smartling-placeholder">
      </ph>
    • 가중치와 편향은 일정해야 합니다.
  • FullyConnected (Dense 또는 InnerProduct라고도 함) <ph type="x-smartling-placeholder">
      </ph>
    • 가중치와 편향 (있는 경우)은 일정해야 합니다.
    • 단일 배치 대소문자만 지원합니다. 다음 경우를 제외하고 입력 차원은 1이어야 합니다. 지정할 수 있습니다.
  • 하드위시
  • 로지스틱 (일명 시그모이드)
  • 최대 풀2D
  • MirrorPad <ph type="x-smartling-placeholder">
      </ph>
    • REFLECT 모드에서는 4D 입력만 지원됩니다. 패딩은 상수이며 H 및 W 치수에만 허용됩니다.
  • 물 <ph type="x-smartling-placeholder">
      </ph>
    • 특정 도형만 브로드캐스트할 수 있습니다. Core ML 텐서 레이아웃에서 다음 텐서 형태가 브로드캐스트 가능합니다. [B, C, H, W], [B, C, 1, 1], [B, 1, H, W], [B, 1, 1, 1]:
  • 패드 및 PadV2 <ph type="x-smartling-placeholder">
      </ph>
    • 4D 입력만 지원됩니다. 패딩은 일정해야 하며 세로 및 가로 크기에서 허용됩니다.
  • Relu
  • ReluN1To1
  • Relu6
  • 재구성 <ph type="x-smartling-placeholder">
      </ph>
    • 대상 Core ML 버전이 2인 경우에만 지원되며 다음 경우에는 지원되지 않습니다. ML 3을 타겟팅해야 합니다
  • ResizeBilinear
  • SoftMax
  • TransposeConv <ph type="x-smartling-placeholder">
      </ph>
    • 가중치는 일정해야 합니다.

의견

문제가 있는 경우 GitHub 문제를 재현할 수 있습니다.

FAQ

  • 그래프에 지원되지 않는 항목이 포함된 경우 CoreML 위임이 CPU로의 대체를 지원하나요? 어떻게 해야 할까요?
  • CoreML 위임은 iOS 시뮬레이터에서 작동하나요?
    • 예. 이 라이브러리는 x86 및 x86_64 타겟을 포함하여 CPU에 비해 성능이 크게 향상되지는 않습니다.
  • LiteRT 및 CoreML 위임은 MacOS를 지원하나요?
    • LiteRT는 iOS에서만 테스트되고 MacOS에서는 테스트되지 않습니다.
  • 커스텀 LiteRT 작업이 지원되나요?
    • 아니요, CoreML 위임은 커스텀 작업을 지원하지 않으며 CPU 사용량입니다.

API