1 /* Copyright 2019 The TensorFlow Authors. 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 16 #ifndef TENSORFLOW_LITE_DELEGATES_GPU_METAL_DELEGATE_H_ 17 #define TENSORFLOW_LITE_DELEGATES_GPU_METAL_DELEGATE_H_ 18 19 #import <Metal/Metal.h> 20 21 #include "tensorflow/lite/c/common.h" 22 23 #ifdef __cplusplus 24 extern "C" { 25 #else 26 // For "C" 'bool' is not built-in type. 27 #include <stdbool.h> 28 #endif // __cplusplus 29 30 typedef struct TfLiteDelegate TfLiteDelegate; 31 32 typedef enum { 33 // waitUntilCompleted 34 TFLGpuDelegateWaitTypePassive, 35 // Minimize latency. It uses active spinning instead of mutex and consumes 36 // additional CPU resources. 37 TFLGpuDelegateWaitTypeActive, 38 // Useful when the output is used with GPU pipeline then or if external 39 // command encoder is set. 40 TFLGpuDelegateWaitTypeDoNotWait, 41 // Tries to avoid GPU sleep mode. 42 TFLGpuDelegateWaitTypeAggressive, 43 } TFLGpuDelegateWaitType; 44 45 // Creates a new delegate instance that need to be destroyed with 46 // DeleteFlowDelegate when delegate is no longer used by tflite. 47 typedef struct { 48 // Allows to quantify tensors, downcast values, process in float16 etc. 49 bool allow_precision_loss; 50 TFLGpuDelegateWaitType wait_type; 51 // Allows execution of integer quantized models 52 bool enable_quantization; 53 } TFLGpuDelegateOptions; 54 55 // Populates TFLGpuDelegateOptions as follows: 56 // allow_precision_loss = false; 57 // wait_type = TFLGpuDelegateWaitType::TFLGpuDelegateWaitTypePassive; 58 // enable_quantization = true; 59 TFL_CAPI_EXPORT extern TFLGpuDelegateOptions TFLGpuDelegateOptionsDefault(void); 60 61 // Creates a new delegate instance that need to be destroyed with 62 // `TFLDeleteTfLiteGpuDelegate` when delegate is no longer used by TFLite. 63 // When `options` is set to `nullptr`, the following default values are used: 64 // .precision_loss_allowed = false, 65 // .wait_type = kPassive, 66 TFL_CAPI_EXPORT extern TfLiteDelegate* TFLGpuDelegateCreate( 67 const TFLGpuDelegateOptions* options); 68 69 // Destroys a delegate created with `TFLGpuDelegateCreate` call. 70 TFL_CAPI_EXPORT extern void TFLGpuDelegateDelete(TfLiteDelegate* delegate); 71 72 // Binds Metal buffer to an input or an output tensor in the initialized 73 // delegate. Bound buffer should have sufficient storage to accommodate all 74 // elements of a tensor. For quantized model, the buffer is bound to internal 75 // dequantized float32 tensor. 76 // Returns non-zero on success, or zero otherwise. 77 // 78 // *** Must be called *after* `Interpreter::ModifyGraphWithDelegate`. *** 79 // WARNING: This is an experimental API and subject to change. 80 TFL_CAPI_EXPORT extern bool TFLGpuDelegateBindMetalBufferToTensor( 81 TfLiteDelegate* delegate, int tensor_index, id<MTLBuffer> metal_buffer); 82 83 #ifdef __cplusplus 84 } // extern "C" 85 #endif // __cplusplus 86 87 #endif // TENSORFLOW_LITE_DELEGATES_GPU_METAL_DELEGATE_H_ 88