1 // Copyright 2020 Google Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at:
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #import <Foundation/Foundation.h>
16 
17 #import "TFLDelegate.h"
18 
19 NS_ASSUME_NONNULL_BEGIN
20 
21 /**
22  * @enum TFLCoreMLDelegateEnabledDevices
23  * This enum specifies for which devices the Core ML delegate will be enabled.
24  */
25 typedef NS_ENUM(NSUInteger, TFLCoreMLDelegateEnabledDevices) {
26   /** Enables the delegate for devices with Neural Engine only. */
27   TFLCoreMLDelegateEnabledDevicesNeuralEngine,
28   /** Enables the delegate for all devices. */
29   TFLCoreMLDelegateEnabledDevicesAll,
30 };
31 
32 /** Custom configuration options for a Core ML delegate. */
33 @interface TFLCoreMLDelegateOptions : NSObject
34 
35 /**
36  * Indicates which devices the Core ML delegate should be enabled for. The default value is
37  * `TFLCoreMLDelegateEnabledDevicesNeuralEngine`, indicating that the delegate is enabled for
38  * Neural Engine devices only.
39  */
40 @property(nonatomic) TFLCoreMLDelegateEnabledDevices enabledDevices;
41 
42 /**
43  * Target Core ML version for the model conversion. When it's not set, Core ML version will be set
44  * to highest available version for the platform.
45  */
46 @property(nonatomic) NSUInteger coreMLVersion;
47 
48 /**
49  * The maximum number of Core ML delegate partitions created. Each graph corresponds to one
50  * delegated node subset in the TFLite model. The default value is `0` indicating that all possible
51  * partitions are delegated.
52  */
53 @property(nonatomic) NSUInteger maxDelegatedPartitions;
54 
55 /**
56  * The minimum number of nodes per partition to be delegated by the Core ML delegate. The default
57  * value is `2`.
58  */
59 @property(nonatomic) NSUInteger minNodesPerPartition;
60 
61 @end
62 
63 /** A delegate that uses the Core ML framework for performing TensorFlow Lite graph operations. */
64 @interface TFLCoreMLDelegate : TFLDelegate
65 
66 /**
67  * Initializes a new Core ML delegate with default options.
68  *
69  * @return A Core ML delegate initialized with default options. `nil` when the delegate creation
70  * fails. For example, trying to initialize a Core ML delegate on an unsupported device.
71  */
72 - (nullable instancetype)init;
73 
74 /**
75  * Initializes a new Core ML delegate with the given options.
76  *
77  * @param options Core ML delegate options.
78  *
79  * @return A Core ML delegate initialized with default options. `nil` when the delegate creation
80  * fails. For example, trying to initialize Core ML delegate on an unsupported device.
81  */
82 - (nullable instancetype)initWithOptions:(TFLCoreMLDelegateOptions *)options
83     NS_DESIGNATED_INITIALIZER;
84 
85 @end
86 
87 NS_ASSUME_NONNULL_END
88