LiteRT कोर एमएल डेलिगेट

LiteRT Core ML डेलिगेट की मदद से, LiteRT मॉडल को इन पर इस्तेमाल किया जा सकता है मुख्य एमएल फ़्रेमवर्क, जिसमें से iOS डिवाइसों पर मॉडल का अनुमान जल्दी मिल जाता है.

इस्तेमाल किए जा सकने वाले iOS वर्शन और डिवाइस:

  • iOS 12 और उसके बाद के वर्शन. iOS के पुराने वर्शन में, Core ML डेलिगेट अपने-आप CPU पर फ़ॉलबैक हो जाता है.
  • डिफ़ॉल्ट रूप से, Core ML डेलिगेट की सुविधा सिर्फ़ A12 SoC वाले डिवाइसों पर चालू की जाएगी और बाद में (iPhone Xs और उसके बाद के वर्शन) तेज़ अनुमान के लिए Neural Engine का इस्तेमाल करें. अगर आपको पुराने डिवाइसों पर भी Core ML डेलिगेट का इस्तेमाल करना है, तो कृपया सबसे सही तरीके

इस्तेमाल किए जा सकने वाले मॉडल

फ़िलहाल, Core ML डेलिगेट का इस्तेमाल फ़्लोट (FP32 और FP16) मॉडल के साथ किया जा सकता है.

अपने मॉडल के हिसाब से, मुख्य एमएल डेलिगेट का इस्तेमाल करना

कोर एमएल डेलिगेट, LiteRT के हर रात वाले रिलीज़ में पहले से ही शामिल है कोकोपोड. मुख्य एमएल डेलिगेट का इस्तेमाल करने के लिए, अपने 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);
      

सबसे सही तरीके

जिन डिवाइसों में न्यूरल इंजन नहीं है उन पर कोर एमएल डेलिगेट का इस्तेमाल करना

डिफ़ॉल्ट रूप से, मुख्य एमएल डेलिगेट सिर्फ़ तब बनाया जाएगा, जब डिवाइस में न्यूरल हो अगर डेलिगेट नहीं किया गया है, तो इंजन और 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) डेलिगेट का इस्तेमाल करना.

अगर कोर एमएल डेलिगेट नहीं बनाया गया है, तो इसका इस्तेमाल किया जा सकता है. पाने के लिए मेटल डेलिगेट परफ़ॉर्मेंस के फ़ायदे. नीचे दिए गए उदाहरण में बताया गया है कि इसे कैसे किया जाए:

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) को पढ़कर यह और इसके न्यूरल इंजन की उपलब्धता पता चलती है. ज़्यादा जानकारी के लिए, कोड देखें. इसके अलावा, अपनी ब्लॉकलिस्ट का सेट भी लागू किया जा सकता है अन्य लाइब्रेरी का इस्तेमाल करने वाले डिवाइस, जैसे कि DeviceKit.

पुराने कोर एमएल वर्शन का इस्तेमाल करना

iOS 13, Core ML 3 के साथ काम करता है, लेकिन यह मॉडल तब बेहतर काम कर सकता है, जब यह कोर एमएल 2 मॉडल स्पेसिफ़िकेशन के साथ कन्वर्ट किया गया. टारगेट कन्वर्ज़न वर्शन यह है डिफ़ॉल्ट रूप से सबसे नए वर्शन पर सेट है, लेकिन आप coreMLVersion (स्विफ़्ट में, C API में coreml_version) को डेलिगेट करने के विकल्प में पुराना वर्शन मौजूद है.

इस्तेमाल किए जा सकने वाले Ops

नीचे दिए गए ऑपरेशन, Core ML डेलिगेट के साथ काम करते हैं.

  • जोड़ें
    • केवल कुछ आकार ब्रॉडकास्ट किए जा सकते हैं. कोर एमएल टेंसर लेआउट में, नीचे दिए गए टेंसर आकार ब्रॉडकास्ट किए जा सकते हैं. [B, C, H, W], [B, C, 1, 1], [B, 1, H, W], [B, 1, 1, 1].
  • औसत पूल 2डी
  • कॉन्कैट
    • जोड़ने का काम चैनल के ऐक्सिस के साथ किया जाना चाहिए.
  • कन्वर्ज़न 2D
    • भार और पक्षपात स्थिर होने चाहिए.
  • गहराई के हिसाब से Conv2D
    • भार और पक्षपात स्थिर होने चाहिए.
  • पूरी तरह से कनेक्ट (यानी Dense या InsideProduct)
    • भार और पूर्वाग्रह (अगर मौजूद है) स्थिर होने चाहिए.
    • सिर्फ़ सिंगल-बैच केस के साथ काम करता है. इनपुट डाइमेंशन 1 होना चाहिए. इसमें ये दोनों शामिल नहीं हैं आखिरी डाइमेंशन.
  • हार्डस्विश
  • लॉजिस्टिक (यानी सिगमॉइड)
  • ज़्यादा से ज़्यादा 2D
  • MirrorPad
    • REFLECT मोड के साथ सिर्फ़ 4D इनपुट काम करता है. पैडिंग स्थिर है और इसकी अनुमति सिर्फ़ H और W डाइमेंशन के लिए है.
  • मल
    • केवल कुछ आकार ब्रॉडकास्ट किए जा सकते हैं. कोर एमएल टेंसर लेआउट में, नीचे दिए गए टेंसर आकार ब्रॉडकास्ट किए जा सकते हैं. [B, C, H, W], [B, C, 1, 1], [B, 1, H, W], [B, 1, 1, 1].
  • पैड और padV2
    • सिर्फ़ 4D इनपुट काम करता है. पैडिंग स्थिर होनी चाहिए और केवल H और W डाइमेंशन के लिए अनुमति है.
  • Relu
  • ReluN1To1
  • Relu6
  • आकार बदलें
    • यह तब ही काम करता है, जब टारगेट Core ML वर्शन 2 हो. इसका इस्तेमाल तब नहीं किया जा सकता, जब कोर एमएल 3 को टारगेट करने वाला.
  • ResizeBilinear
  • SoftMax
  • तन
  • TransposeConv
    • वज़न एक जैसा होना चाहिए.

सुझाव/राय दें या शिकायत करें

समस्याओं के लिए, कृपया एक बनाएं GitHub समस्या बनी हुई है.

अक्सर पूछे जाने वाले सवाल

  • अगर किसी ग्राफ़ में असमर्थित है ओह?
    • हां
  • क्या CoreML डेलिगेट, iOS Simulator पर काम करता है?
    • हां. लाइब्रेरी में x86 और x86_64 के टारगेट शामिल हैं, इसलिए यह इन पर चल सकता है एक सिम्युलेटर का इस्तेमाल किया है, लेकिन आपको सीपीयू से परफ़ॉर्मेंस में बढ़ोतरी नहीं दिखेगी.
  • क्या LiteRT और CoreML डेलिगेट, MacOS के साथ काम करते हैं?
    • LiteRT की जांच सिर्फ़ iOS पर की जाती है, MacOS पर नहीं.
  • क्या कस्टम LiteRT Ops काम करते हैं?
    • नहीं, CoreML का प्रतिनिधि, कस्टम ऑपरेशन की सुविधा नहीं देता है. इसलिए, वह इस पर वापस आ जाएगा सीपीयू.

API