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_GL_DELEGATE_H_ 17 #define TENSORFLOW_LITE_DELEGATES_GPU_GL_DELEGATE_H_ 18 19 #include <stdint.h> 20 21 #include <GLES3/gl31.h> 22 #include "absl/base/macros.h" 23 #include "tensorflow/lite/c/common.h" 24 25 #ifdef SWIG 26 #define TFL_CAPI_EXPORT 27 #else 28 #if defined(_WIN32) 29 #ifdef TFL_COMPILE_LIBRARY 30 #define TFL_CAPI_EXPORT __declspec(dllexport) 31 #else 32 #define TFL_CAPI_EXPORT __declspec(dllimport) 33 #endif // TFL_COMPILE_LIBRARY 34 #else 35 #define TFL_CAPI_EXPORT __attribute__((visibility("default"))) 36 #endif // _WIN32 37 #endif // SWIG 38 39 #ifdef __cplusplus 40 extern "C" { 41 #endif // __cplusplus 42 43 // WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING 44 // 45 // GPU delegate declared in this file is OBSOLETE and replaced with the delegate 46 // declared in delegate.h. New delegate combines all GL, CL and soon 47 // Vulkan-based implementations in one. 48 // Please migrate before end of 2019. 49 // 50 // WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING 51 52 // LINT.IfChange 53 enum TfLiteGlObjectType { 54 TFLITE_GL_OBJECT_TYPE_FASTEST = 0, 55 TFLITE_GL_OBJECT_TYPE_TEXTURE = 1, 56 TFLITE_GL_OBJECT_TYPE_BUFFER = 2, 57 }; 58 59 // Shader compilation options. 60 // Always use TfLiteGlCompileOptionsDefault() method to create new instance 61 // of TfLiteGlCompileOptions, otherwise every new added option may break 62 // inference. 63 // TODO(impjdi): Unify with opengl::CompilationOptions. 64 typedef struct { 65 // When set to zero, computations are carried out in 32-bit floating point. 66 // Otherwise, the GPU may quantify tensors, downcast values, process in FP16 67 // (recommended). 68 int32_t precision_loss_allowed; 69 70 // User's preferred GL object to represent tensors. When set to: 71 // * `TFLITE_GL_OBJECT_TYPE_FASTEST`, the delegate chooses a GL object type 72 // automatically that will perform fastest (recommended). 73 // * `TFLITE_GL_OBJECT_TYPE_TEXTURE`: GL textures are used to represent 74 // tensors which often work faster on Adreno-based devices, but may use more 75 // memory. 76 // * `TFLITE_GL_OBJECT_TYPE_BUFFER`: GL shader storage buffer objects are used 77 // to represent tensors. 78 int32_t preferred_gl_object_type; 79 80 // When set to zero, dynamic batching is disabled and input/output tensors 81 // must have a batch size of 1 (probably what you unless you use LSTMs). 82 // Otherwise, enables dynamic batching and input/output tensor can have a 83 // batch size greater than 1. 84 int32_t dynamic_batch_enabled; 85 86 // Parameters will be inlined into a shader. This in turn will generated more 87 // unique shaders where each will need to be compiled. 88 int32_t inline_parameters; 89 } TfLiteGlCompileOptions; 90 91 // Populates TfLiteGlCompileOptions as follows: 92 // precision_loss_allowed = 0; 93 // preferred_gl_object_type = TFLITE_GL_OBJECT_TYPE_FASTEST; 94 // dynamic_batch_enabled = 0; 95 // inline_parameters = 0; 96 TFL_CAPI_EXPORT TfLiteGlCompileOptions TfLiteGlCompileOptionsDefault(); 97 98 // Always use TfLiteGpuDelegateOptionsDefault() method to create new instance 99 // of TfLiteGpuDelegateOptions, otherwise every new added option may break 100 // inference. 101 typedef struct { 102 const uint8_t* metadata; // Internal. 103 TfLiteGlCompileOptions compile_options; 104 } TfLiteGpuDelegateOptions; 105 106 // Populates TfLiteGlCompileOptions as follows: 107 // metadata = nullptr; 108 // compile_options = TfLiteGlCompileOptionsDefault(); 109 TFL_CAPI_EXPORT TfLiteGpuDelegateOptions TfLiteGpuDelegateOptionsDefault(); 110 111 // LINT.ThenChange(//tensorflow/lite/delegates/gpu/java/src/main/java/org/tensorflow/lite/gpu/GpuDelegate.java) 112 113 // Creates a new delegate instance that need to be destroyed with 114 // TfLiteGpuDelegateDelete when delegate is no longer used by TFLite. 115 // When `options` is set to `nullptr`, the following default values are used: 116 // .metadata = nullptr, 117 // .compile_options = { 118 // .precision_loss_allowed = false, 119 // .preferred_gl_object_type = TFLITE_GL_OBJECT_TYPE_FASTEST, 120 // .dynamic_batch_enabled = false, 121 // }, 122 ABSL_DEPRECATED("Use TfLiteGpuDelegateV2Create defined in delegate.h instead.") 123 TFL_CAPI_EXPORT TfLiteDelegate* TfLiteGpuDelegateCreate( 124 const TfLiteGpuDelegateOptions* options); 125 126 // Destroys a delegate created with `TfLiteGpuDelegateCreate` call. 127 TFL_CAPI_EXPORT void TfLiteGpuDelegateDelete(TfLiteDelegate* delegate); 128 129 // Binds GL shader storage object to an input or an output tensor in the 130 // initialized delegate. Bound buffer should have sufficient storage to 131 // accommodate all elements of a tensor. 132 // 133 // *** Must be called *before* `Interpreter::ModifyGraphWithDelegate`. *** 134 TFL_CAPI_EXPORT TfLiteStatus TfLiteGpuDelegateBindBufferToTensor( 135 TfLiteDelegate* delegate, GLuint buffer, int tensor_index); 136 137 #ifndef TFLITE_GPU_BINARY_RELEASE 138 // Returns the metadata of `tflite_model` if it has one, or `nullptr` otherwise. 139 // Designed to be used with `TfLiteGpuDelegateOptions.metadata`. 140 TFL_CAPI_EXPORT const uint8_t* TfLiteGpuDelegateGetModelMetadata( 141 const void* tflite_model); 142 #endif // TFLITE_GPU_BINARY_RELEASE 143 144 #ifdef __cplusplus 145 } 146 #endif // __cplusplus 147 148 #endif // TENSORFLOW_LITE_DELEGATES_GPU_GL_DELEGATE_H_ 149