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 TFLMetalDelegateThreadWaitType
23  * This enum specifies wait type for Metal delegate.
24  */
25 typedef NS_ENUM(NSUInteger, TFLMetalDelegateThreadWaitType) {
26 
27   /**
28    * The thread does not wait for the work to complete. Useful when the output of the work is used
29    * with the GPU pipeline.
30    */
31   TFLMetalDelegateThreadWaitTypeDoNotWait,
32   /** The thread waits until the work is complete. */
33   TFLMetalDelegateThreadWaitTypePassive,
34   /**
35    * The thread waits for the work to complete with minimal latency, which may require additional
36    * CPU resources.
37    */
38   TFLMetalDelegateThreadWaitTypeActive,
39   /** The thread waits for the work while trying to prevent the GPU from going into sleep mode. */
40   TFLMetalDelegateThreadWaitTypeAggressive,
41 };
42 
43 /** Custom configuration options for a Metal delegate. */
44 @interface TFLMetalDelegateOptions : NSObject
45 
46 /**
47  * Indicates whether the GPU delegate allows precision loss, such as allowing `Float16` precision
48  * for a `Float32` computation. The default is `false`.
49  */
50 @property(nonatomic, getter=isPrecisionLossAllowed) BOOL precisionLossAllowed;
51 
52 /**
53  * Indicates how the current thread should wait for work on the GPU to complete. The default
54  * is `TFLMetalDelegateThreadWaitTypePassive`.
55  */
56 @property(nonatomic) TFLMetalDelegateThreadWaitType waitType;
57 
58 /**
59  * Indicates whether the GPU delegate allows execution of an 8-bit quantized model. The default is
60  * `true`.
61  */
62 @property(nonatomic, getter=isQuantizationEnabled) BOOL quantizationEnabled;
63 
64 @end
65 
66 /**
67  * A delegate that uses the `Metal` framework for performing TensorFlow Lite graph operations with
68  * GPU acceleration.
69  */
70 @interface TFLMetalDelegate : TFLDelegate
71 
72 /**
73  * Initializes a new GPU delegate with default options.
74  *
75  * @return A new GPU delegate with default options. `nil` when the GPU delegate creation fails.
76  */
77 - (nullable instancetype)init;
78 
79 /**
80  * Initializes a new GPU delegate with the given options.
81  *
82  * @param options GPU delegate options.
83  *
84  * @return A new GPU delegate with default options. `nil` when the GPU delegate creation fails.
85  */
86 - (nullable instancetype)initWithOptions:(TFLMetalDelegateOptions *)options
87     NS_DESIGNATED_INITIALIZER;
88 
89 @end
90 
91 NS_ASSUME_NONNULL_END
92