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