الإصدار 12 من نظام التشغيل iOS والإصدارات الأحدث. في إصدارات iOS الأقدم، سيتولى تفويض Core ML
إلى وحدة المعالجة المركزية تلقائيًا.
سيتم تلقائيًا تفعيل التفويض الأساسي لتعلُّم الآلة على الأجهزة المزوّدة بالمنظومة على الرقاقة (A12 SoC)
والإصدارات الأحدث (هاتف iPhone X والإصدارات الأحدث) لاستخدام المحرّك العصبي للاستنتاج بشكل أسرع.
إذا كنت تريد استخدام التفويض الأساسي لتعلُّم الآلة أيضًا على الأجهزة الأقدم، يُرجى مراجعة
أفضل الممارسات
الطُرز المتوافقة
يتيح تفويض Core ML حاليًا النماذج العائمة (FP32 وFP16).
تجربة المفوَّض الأساسي لتعلُّم الآلة وفقًا لنموذجك الخاص
سبق أن تم إدراج تفويض Core ML في الإصدار الليلي من LiteRT
CocoaPods لاستخدام التفويض الأساسي لتعلُّم الآلة، عليك تغيير مجموعة 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']
استخدام المفوَّض الأساسي لتعلُّم الآلة على الأجهزة التي لا تتضمّن "المحرّك العصبي"
تلقائيًا، لن يتم إنشاء تفويض تعلُّم الآلة الأساسي إلا إذا كان الجهاز يتضمّن تقنية Neual
محرّك البحث، وسيعرض null إذا لم يتم إنشاء التفويض. إذا كنت ترغب في
تشغيل تفويض Core ML على البيئات الأخرى (على سبيل المثال، المحاكي)، اجتياز .all
كخيار أثناء إنشاء التفويض في Swift. في لغة C++ (والهدف-C)، يمكنك
الاجتياز TfLiteCoreMlDelegateAllDevices يوضّح المثال التالي كيفية إجراء ذلك:
عندما لا يتم إنشاء تفويض Core ML، يمكنك بدلاً من ذلك استخدام
مفوَّض في فريق موسيقى "الميتال" للحصول على
ومزايا الأداء. يوضّح المثال التالي كيفية إجراء ذلك:
يقرأ منطق الإنشاء المفوَّض رقم تعريف الجهاز للجهاز (مثل iPhone11,1) بهدف
تحديد مدى توفر المحرك العصبوني. يمكنك الاطّلاع على
الرموز البرمجية
لمزيد من التفاصيل. يمكنك بدلاً من ذلك تنفيذ مجموعتك الخاصة من قائمة الحظر.
الأجهزة التي تستخدم مكتبات أخرى مثل
DeviceKit.
استخدام إصدار أقدم من Core ML
على الرغم من أنّ iOS 13 يتوافق مع Core ML 3، قد يعمل النموذج بشكل أفضل
ويتم تحويلها باستخدام مواصفات نموذج Core ML 2. إصدار الإحالة الناجحة المستهدَف هو
على أحدث إصدار افتراضيًا، ولكن يمكنك تغيير ذلك من خلال
coreMLVersion (في Swift، coreml_version في C API) في خيار التفويض
الإصدار الأقدم.
العمليات المتاحة
يوفِّر المفوَّض الأساسي لتعلُّم الآلة العمليات التالية.
إضافة
هناك أشكال معينة فقط قابلة للبث. في تخطيط Core ML tensor،
يمكن بث أشكال متوترة التالية. [B, C, H, W]، [B, C, 1,
1]، [B, 1, H, W]، [B, 1, 1, 1].
متوسط مستوى المسبح 2D
كونكات
يجب أن تتم عملية الدمج على طول محور القناة.
الإحالة الناجحة 2D
يجب أن تكون الترجيحات والتحيز ثابتين.
DepthwiseConvert2D
يجب أن تكون الترجيحات والتحيز ثابتين.
متّصل بالكامل (المعروف أيضًا باسم Dense أو InnerProduct)
يجب أن تكون الترجيحات والانحياز (إن وُجد) ثابتين.
تتوافق فقط مع حافظة "دفعة واحدة". يجب أن تكون أبعاد الإدخال 1، باستثناء
البعد الأخير.
غموض
الخدمات اللوجستية (المعروفة أيضًا باسم Sigmoid)
الحد الأقصى لـ "حمام السباحة 2D"
MirrorPad
لا يتوفّر إلا الإدخال رباعي الأبعاد مع وضع REFLECT. يجب أن تكون المساحة المتروكة
ثابت، ولا يُسمح به إلا لأبعاد H وW.
مول
هناك أشكال معينة فقط قابلة للبث. في تخطيط Core ML tensor،
يمكن بث أشكال متوترة التالية. [B, C, H, W]، [B, C, 1,
1]، [B, 1, H, W]، [B, 1, 1, 1].
PadV2
ولا يتوفّر سوى الإدخال رباعي الأبعاد. ينبغي أن تكون المساحة المتروكة ثابتة،
مسموح به لأبعاد H وW.
Relu
سلسلة ReluN1To1
Relu6
إعادة تشكيل
لا تتوفّر هذه الميزة إلا عندما يكون الإصدار 2 المستهدف لتعلُّم الآلة هو، ولا تتوفّر في الحالات التالية:
التي تستهدف Core ML 3
ResizeBilinear
SoftMax
تانه
TransposeConv
يجب أن تكون الأوزان ثابتة.
ملاحظات
بالنسبة إلى المشاكل، يُرجى إنشاء
GitHub
المشكلة في جميع التفاصيل اللازمة لإعادة إنتاج الخطأ.
الأسئلة الشائعة
هل يتيح التفويض في CoreML إمكانية الرجوع إلى وحدة المعالجة المركزية (CPU) إذا كان الرسم البياني يحتوي على ملفات غير متوافقة؟
العمليات؟
نعم
هل يعمل تفويض CoreML على iOS Simulator؟
نعم. تتضمن المكتبة أهداف x86 وx86_64 بحيث يمكن تشغيلها على
محاكي، لكنك لن تتحسن الأداء عبر وحدة المعالجة المركزية.
هل يدعم تفويض LiteRT وCoreML نظام التشغيل MacOS؟
يتم اختبار LiteRT على نظام iOS فقط، وليس على نظام التشغيل MacOS.
هل عمليات LiteRT المخصّصة متوافقة؟
لا، لا يدعم تفويض CoreML العمليات المخصصة وسينتقل إلى
وحدة المعالجة المركزية (CPU).
تاريخ التعديل الأخير: 2025-07-24 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","easyToUnderstand","thumb-up"],["ساعَدني المحتوى في حلّ مشكلتي.","solvedMyProblem","thumb-up"],["غير ذلك","otherUp","thumb-up"]],[["لا يحتوي على المعلومات التي أحتاج إليها.","missingTheInformationINeed","thumb-down"],["الخطوات معقدة للغاية / كثيرة جدًا.","tooComplicatedTooManySteps","thumb-down"],["المحتوى قديم.","outOfDate","thumb-down"],["ثمة مشكلة في الترجمة.","translationIssue","thumb-down"],["مشكلة في العيّنات / التعليمات البرمجية","samplesCodeIssue","thumb-down"],["غير ذلك","otherDown","thumb-down"]],["تاريخ التعديل الأخير: 2025-07-24 (حسب التوقيت العالمي المتفَّق عليه)"],[],[],null,["# LiteRT Core ML delegate\n\nThe LiteRT Core ML delegate enables running LiteRT models on\n[Core ML framework](https://developer.apple.com/documentation/coreml), which\nresults in faster model inference on iOS devices.\n| **Note:** This delegate is in experimental (beta) phase. It is available from LiteRT 2.4.0 and latest nightly releases.\n| **Note:** Core ML delegate supports Core ML version 2 and later.\n\n**Supported iOS versions and devices:**\n\n- iOS 12 and later. In the older iOS versions, Core ML delegate will automatically fallback to CPU.\n- By default, Core ML delegate will only be enabled on devices with A12 SoC and later (iPhone Xs and later) to use Neural Engine for faster inference. If you want to use Core ML delegate also on the older devices, please see [best practices](#best_practices)\n\n**Supported models**\n\nThe Core ML delegate currently supports float (FP32 and FP16) models.\n\nTrying the Core ML delegate on your own model\n---------------------------------------------\n\nThe Core ML delegate is already included in nightly release of LiteRT\nCocoaPods. To use Core ML delegate, change your LiteRT pod to include\nsubspec `CoreML` in your `Podfile`.\n**Note:** If you want to use C API instead of Objective-C API, you can include `TensorFlowLiteC/CoreML` pod to do so. \n\n target 'YourProjectName'\n pod 'TensorFlowLiteSwift/CoreML', '~\u003e 2.4.0' # Or TensorFlowLiteObjC/CoreML\n\nOR \n\n # Particularily useful when you also want to include 'Metal' subspec.\n target 'YourProjectName'\n pod 'TensorFlowLiteSwift', '~\u003e 2.4.0', :subspecs =\u003e ['CoreML']\n\n**Note:** Core ML delegate can also use C API for Objective-C code. Prior to LiteRT 2.4.0 release, this was the only option. \n\n### Swift\n\n\u003cbr /\u003e\n\n```swift\n let coreMLDelegate = CoreMLDelegate()\n var interpreter: Interpreter\n\n // Core ML delegate will only be created for devices with Neural Engine\n if coreMLDelegate != nil {\n interpreter = try Interpreter(modelPath: modelPath,\n delegates: [coreMLDelegate!])\n } else {\n interpreter = try Interpreter(modelPath: modelPath)\n }\n \n```\n\n\u003cbr /\u003e\n\n### Objective-C\n\n\u003cbr /\u003e\n\n```objective-c\n // Import module when using CocoaPods with module support\n @import TFLTensorFlowLite;\n\n // Or import following headers manually\n # import \"tensorflow/lite/objc/apis/TFLCoreMLDelegate.h\"\n # import \"tensorflow/lite/objc/apis/TFLTensorFlowLite.h\"\n\n // Initialize Core ML delegate\n TFLCoreMLDelegate* coreMLDelegate = [[TFLCoreMLDelegate alloc] init];\n\n // Initialize interpreter with model path and Core ML delegate\n TFLInterpreterOptions* options = [[TFLInterpreterOptions alloc] init];\n NSError* error = nil;\n TFLInterpreter* interpreter = [[TFLInterpreter alloc]\n initWithModelPath:modelPath\n options:options\n delegates:@[ coreMLDelegate ]\n error:&error];\n if (error != nil) { /* Error handling... */ }\n\n if (![interpreter allocateTensorsWithError:&error]) { /* Error handling... */ }\n if (error != nil) { /* Error handling... */ }\n\n // Run inference ...\n \n```\n\n\u003cbr /\u003e\n\n### C (Until 2.3.0)\n\n\u003cbr /\u003e\n\n```c\n #include \"tensorflow/lite/delegates/coreml/coreml_delegate.h\"\n\n // Initialize interpreter with model\n TfLiteModel* model = TfLiteModelCreateFromFile(model_path);\n\n // Initialize interpreter with Core ML delegate\n TfLiteInterpreterOptions* options = TfLiteInterpreterOptionsCreate();\n TfLiteDelegate* delegate = TfLiteCoreMlDelegateCreate(NULL); // default config\n TfLiteInterpreterOptionsAddDelegate(options, delegate);\n TfLiteInterpreterOptionsDelete(options);\n\n TfLiteInterpreter* interpreter = TfLiteInterpreterCreate(model, options);\n\n TfLiteInterpreterAllocateTensors(interpreter);\n\n // Run inference ...\n\n /* ... */\n\n // Dispose resources when it is no longer used.\n // Add following code to the section where you dispose of the delegate\n // (e.g. `dealloc` of class).\n\n TfLiteInterpreterDelete(interpreter);\n TfLiteCoreMlDelegateDelete(delegate);\n TfLiteModelDelete(model);\n \n```\n\n\u003cbr /\u003e\n\nBest practices\n--------------\n\n### Using Core ML delegate on devices without Neural Engine\n\nBy default, Core ML delegate will only be created if the device has Neural\nEngine, and will return `null` if the delegate is not created. If you want to\nrun Core ML delegate on other environments (for example, simulator), pass `.all`\nas an option while creating delegate in Swift. On C++ (and Objective-C), you can\npass `TfLiteCoreMlDelegateAllDevices`. Following example shows how to do this: \n\n### Swift\n\n\u003cbr /\u003e\n\n```swift\n var options = CoreMLDelegate.Options()\n options.enabledDevices = .all\n let coreMLDelegate = CoreMLDelegate(options: options)!\n let interpreter = try Interpreter(modelPath: modelPath,\n delegates: [coreMLDelegate])\n \n```\n\n\u003cbr /\u003e\n\n### Objective-C\n\n\u003cbr /\u003e\n\n```objective-c\n TFLCoreMLDelegateOptions* coreMLOptions = [[TFLCoreMLDelegateOptions alloc] init];\n coreMLOptions.enabledDevices = TFLCoreMLDelegateEnabledDevicesAll;\n TFLCoreMLDelegate* coreMLDelegate = [[TFLCoreMLDelegate alloc]\n initWithOptions:coreMLOptions];\n\n // Initialize interpreter with delegate\n \n```\n\n\u003cbr /\u003e\n\n### C\n\n\u003cbr /\u003e\n\n```c\n TfLiteCoreMlDelegateOptions options;\n options.enabled_devices = TfLiteCoreMlDelegateAllDevices;\n TfLiteDelegate* delegate = TfLiteCoreMlDelegateCreate(&options);\n // Initialize interpreter with delegate\n \n```\n\n\u003cbr /\u003e\n\n### Using Metal(GPU) delegate as a fallback.\n\nWhen the Core ML delegate is not created, alternatively you can still use\n[Metal delegate](../performance/gpu#ios) to get\nperformance benefits. Following example shows how to do this: \n\n### Swift\n\n\u003cbr /\u003e\n\n```swift\n var delegate = CoreMLDelegate()\n if delegate == nil {\n delegate = MetalDelegate() // Add Metal delegate options if necessary.\n }\n\n let interpreter = try Interpreter(modelPath: modelPath,\n delegates: [delegate!])\n \n```\n\n\u003cbr /\u003e\n\n### Objective-C\n\n\u003cbr /\u003e\n\n```objective-c\n TFLDelegate* delegate = [[TFLCoreMLDelegate alloc] init];\n if (!delegate) {\n // Add Metal delegate options if necessary\n delegate = [[TFLMetalDelegate alloc] init];\n }\n // Initialize interpreter with delegate\n \n```\n\n\u003cbr /\u003e\n\n### C\n\n\u003cbr /\u003e\n\n```c\n TfLiteCoreMlDelegateOptions options = {};\n delegate = TfLiteCoreMlDelegateCreate(&options);\n if (delegate == NULL) {\n // Add Metal delegate options if necessary\n delegate = TFLGpuDelegateCreate(NULL);\n }\n // Initialize interpreter with delegate\n \n```\n\n\u003cbr /\u003e\n\nThe delegate creation logic reads device's machine id (e.g. iPhone11,1) to\ndetermine its Neural Engine availability. See the\n[code](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/delegates/coreml/coreml_delegate.mm)\nfor more detail. Alternatively, you can implement your own set of denylist\ndevices using other libraries such as\n[DeviceKit](https://github.com/devicekit/DeviceKit).\n\n### Using older Core ML version\n\nAlthough iOS 13 supports Core ML 3, the model might work better when it is\nconverted with Core ML 2 model specification. The target conversion version is\nset to the latest version by default, but you can change this by setting\n`coreMLVersion` (in Swift, `coreml_version` in C API) in the delegate option to\nolder version.\n\nSupported ops\n-------------\n\nFollowing ops are supported by the Core ML delegate.\n\n- Add\n - Only certain shapes are broadcastable. In Core ML tensor layout, following tensor shapes are broadcastable. `[B, C, H, W]`, `[B, C, 1,\n 1]`, `[B, 1, H, W]`, `[B, 1, 1, 1]`.\n- AveragePool2D\n- Concat\n - Concatenation should be done along the channel axis.\n- Conv2D\n - Weights and bias should be constant.\n- DepthwiseConv2D\n - Weights and bias should be constant.\n- FullyConnected (aka Dense or InnerProduct)\n - Weights and bias (if present) should be constant.\n - Only supports single-batch case. Input dimensions should be 1, except the last dimension.\n- Hardswish\n- Logistic (aka Sigmoid)\n- MaxPool2D\n- MirrorPad\n - Only 4D input with `REFLECT` mode is supported. Padding should be constant, and is only allowed for H and W dimensions.\n- Mul\n - Only certain shapes are broadcastable. In Core ML tensor layout, following tensor shapes are broadcastable. `[B, C, H, W]`, `[B, C, 1,\n 1]`, `[B, 1, H, W]`, `[B, 1, 1, 1]`.\n- Pad and PadV2\n - Only 4D input is supported. Padding should be constant, and is only allowed for H and W dimensions.\n- Relu\n- ReluN1To1\n- Relu6\n- Reshape\n - Only supported when target Core ML version is 2, not supported when targeting Core ML 3.\n- ResizeBilinear\n- SoftMax\n- Tanh\n- TransposeConv\n - Weights should be constant.\n\nFeedback\n--------\n\nFor issues, please create a\n[GitHub](https://github.com/tensorflow/tensorflow/issues/new?template=50-other-issues.md)\nissue with all the necessary details to reproduce.\n\nFAQ\n---\n\n- Does CoreML delegate support fallback to CPU if a graph contains unsupported ops?\n - Yes\n- Does CoreML delegate work on iOS Simulator?\n - Yes. The library includes x86 and x86_64 targets so it can run on a simulator, but you will not see performance boost over CPU.\n- Does LiteRT and CoreML delegate support MacOS?\n - LiteRT is only tested on iOS but not MacOS.\n- Is custom LiteRT ops supported?\n - No, CoreML delegate does not support custom ops and they will fallback to CPU.\n\nAPIs\n----\n\n- [Core ML delegate Swift API](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/swift/Sources/CoreMLDelegate.swift)\n- [Core ML delegate C API](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/delegates/coreml/coreml_delegate.h)"]]