ตัวแทน LiteRT Core ML

ผู้มอบสิทธิ์ LiteRT Core ML จะเปิดใช้โมเดล LiteRT ใน เฟรมเวิร์ก Core ML ส่งผลให้การอนุมานโมเดลในอุปกรณ์ iOS ได้เร็วขึ้น

เวอร์ชันและอุปกรณ์ iOS ที่รองรับมีดังนี้

  • iOS 12 ขึ้นไป ใน iOS เวอร์ชันเก่า ผู้รับมอบสิทธิ์ Core ML จะ สำรอง CPU โดยอัตโนมัติ
  • โดยค่าเริ่มต้น ระบบจะเปิดใช้ผู้รับมอบสิทธิ์ Core ML ในอุปกรณ์ที่มี A12 SoC เท่านั้น ขึ้นไป (iPhone X ขึ้นไป) เพื่อใช้ Neural Engine สำหรับการอนุมานที่เร็วขึ้น หากต้องการใช้ผู้รับมอบสิทธิ์ Core ML ในอุปกรณ์รุ่นเก่าด้วย โปรดดูที่ แนวทางปฏิบัติแนะนำ

รุ่นที่รองรับ

ปัจจุบันผู้รับมอบสิทธิ์ Core ML รองรับโมเดลจำนวนลอยตัว (FP32 และ FP16)

การทดลองใช้ผู้รับมอบสิทธิ์ Core ML ในโมเดลของคุณเอง

ผู้รับมอบสิทธิ์ Core ML รวมอยู่ในการเผยแพร่ LiteRT ทุกคืนแล้ว CocoaPods หากต้องการใช้การมอบสิทธิ์ Core ML ให้เปลี่ยนพ็อด LiteRT เพื่อรวม ข้อกำหนดย่อย CoreML ใน Podfile

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);
      

แนวทางปฏิบัติแนะนำ

การใช้ผู้รับมอบสิทธิ์ Core ML ในอุปกรณ์ที่ไม่มี Neural Engine

โดยค่าเริ่มต้น ระบบจะสร้างการมอบสิทธิ์ Core ML ก็ต่อเมื่ออุปกรณ์มี Neural เท่านั้น Engine และจะส่งกลับ 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
      

ตรรกะการสร้างการมอบสิทธิ์จะอ่านรหัสเครื่องของอุปกรณ์ (เช่น iPhone11,1) ไปยัง ระบุความพร้อมให้บริการของ Neural Engine โปรดดู รหัส เพื่อดูรายละเอียดเพิ่มเติม หรือคุณจะใช้ชุดรายการที่ปฏิเสธของคุณเองก็ได้ โดยใช้ไลบรารีอื่น เช่น DeviceKit.

การใช้ Core ML เวอร์ชันเก่า

แม้ว่า iOS 13 จะรองรับ Core ML 3 แต่โมเดลนี้อาจทำงานได้ดีขึ้นเมื่อ ตามข้อมูลจำเพาะของโมเดล Core ML 2 เวอร์ชัน Conversion เป้าหมายคือ ให้เป็นเวอร์ชันล่าสุดโดยค่าเริ่มต้น แต่คุณสามารถเปลี่ยนค่านี้ได้โดยการตั้งค่า coreMLVersion (ใน Swift, coreml_version ใน C API) ในตัวเลือกการมอบสิทธิ์สำหรับ เวอร์ชันเก่า

การดำเนินการที่รองรับ

ผู้รับมอบสิทธิ์ Core ML สนับสนุนการดำเนินการต่อไปนี้

  • เพิ่ม
    • รูปร่างบางอย่างเท่านั้นที่สามารถเผยแพร่ได้ ในเลย์เอาต์ Tensor ML หลัก รูปร่าง Tensor ต่อไปนี้สามารถออกอากาศได้ [B, C, H, W], [B, C, 1, 1], [B, 1, H, W], [B, 1, 1, 1]
  • ค่าเฉลี่ยพูล 2 วัน
  • คอนแคต
    • ควรต่อสายตามแกนช่อง
  • Conv2D
    • การให้น้ำหนักและความเอนเอียงควรคงที่
  • DepthwiseConv2D
    • การให้น้ำหนักและความเอนเอียงควรคงที่
  • เชื่อมต่อกันโดยสมบูรณ์ (หรือที่เรียกว่า Dense หรือ InnerProduct)
    • น้ำหนักและความเอนเอียง (หากมี) ควรคงที่
    • รองรับเฉพาะเคสแบบกลุ่มเดียว ขนาดอินพุตควรเป็น 1 ยกเว้น มิติข้อมูลสุดท้าย
  • ฮาร์ดสวิช
  • โลจิสติกส์ (หรือ Sigmoid)
  • MaxPool2 มิติ
  • MirrorPad
    • รองรับการป้อนข้อมูลแบบ 4 มิติกับโหมด REFLECT เท่านั้น Padding ควรเป็น คงที่ และใช้ได้เฉพาะกับขนาด H และ W เท่านั้น
  • ทรงมัล
    • รูปร่างบางอย่างเท่านั้นที่สามารถเผยแพร่ได้ ในเลย์เอาต์ Tensor ML หลัก รูปร่าง Tensor ต่อไปนี้สามารถออกอากาศได้ [B, C, H, W], [B, C, 1, 1], [B, 1, H, W], [B, 1, 1, 1]
  • Pad และ PadV2
    • รองรับเฉพาะอินพุต 4 มิติเท่านั้น Padding ควรเป็นค่าคงที่และ อนุญาตสำหรับขนาด H และ W
  • Relu
  • ReluN1To1
  • Relu6
  • ปรับรูปร่าง
    • รองรับเมื่อเวอร์ชัน Core ML เป้าหมายเป็น 2 เท่านั้น ไม่รองรับเมื่อ ซึ่งกำหนดเป้าหมายเป็น Core ML 3
  • ResizeBilinear
  • SoftMax
  • แทนห์
  • TransposeConv
    • น้ำหนักควรคงที่

ความคิดเห็น

หากมีปัญหา โปรดสร้าง GitHub ปัญหาที่มีรายละเอียดที่จำเป็นทั้งหมดในการทำซ้ำ

คำถามที่พบบ่อย

  • การมอบสิทธิ์ CoreML รองรับการใช้ CPU ระบบสำรองหากกราฟมีข้อมูลที่ไม่รองรับหรือไม่ อ๊ะ
    • ใช่
  • ผู้รับมอบสิทธิ์ CoreML ทำงานใน iOS Simulator ไหม
    • ได้ ไลบรารีมีเป้าหมาย x86 และ x86_64 เพื่อให้เรียกใช้ได้ แต่จะไม่เห็นการเพิ่มประสิทธิภาพบน CPU
  • ผู้รับมอบสิทธิ์ LiteRT และ CoreML รองรับ MacOS ไหม
    • LiteRT ได้รับการทดสอบเฉพาะใน iOS เท่านั้น ไม่ใช่ MacOS
  • รองรับการทำงานของ LiteRT ที่กำหนดเองไหม
    • ไม่ ผู้รับมอบสิทธิ์ CoreML ไม่สนับสนุนการดำเนินการที่กำหนดเอง และจะสำรอง CPU

API