xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/delegates/gpu/cl/gpu_api_delegate.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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_CL_GPU_API_DELEGATE_H_
17 #define TENSORFLOW_LITE_DELEGATES_GPU_CL_GPU_API_DELEGATE_H_
18 
19 #define GL_NO_PROTOTYPES
20 #define EGL_NO_PROTOTYPES
21 #include <EGL/egl.h>
22 #include <GLES3/gl31.h>
23 #undef GL_NO_PROTOTYPES
24 #undef EGL_NO_PROTOTYPES
25 
26 #include <stdint.h>
27 
28 #include "tensorflow/lite/c/common.h"
29 #include "tensorflow/lite/delegates/gpu/delegate.h"
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif  // __cplusplus
34 
35 // Shader compilation options.
36 typedef struct {
37   // When set to zero, computations are carried out in 32-bit floating point.
38   // Otherwise, the GPU may quantify tensors, downcast values, process in FP16
39   // (recommended).
40   int32_t precision_loss_allowed;
41 
42   // Priority is defined in TfLiteGpuInferencePriority.
43   int32_t inference_priority;
44 } TfLiteGpuCompileOptions_New;
45 
46 typedef struct {
47   TfLiteGpuCompileOptions_New compile_options;
48 
49   // [Optional]
50   // Whenever EGL display and EGL context are set, corresponding OpenCL context
51   // will be created.
52   // These variables are required when using GL objects as inputs or outputs.
53   EGLDisplay egl_display;
54   EGLContext egl_context;
55 
56   // [Optional]
57   // Contains data returned from TfLiteGpuDelegateGetSerializedBinaryCache call.
58   // Invalid or incompatible data will be discarded. Compiled binary may become
59   // incompatible when GPU driver is updated.
60   const uint8_t* serialized_binary_cache_data;
61   size_t serialized_binary_cache_size;
62 } TfLiteGpuDelegateOptions_New;
63 
64 // Creates a new delegate instance that need to be destroyed with
65 // TfLiteGpuDelegateDelete_New when delegate is no longer used by TFLite.
66 // When `options` is set to `nullptr`, the following default values are used:
67 // .compile_options = {
68 //   .precision_loss_allowed = false,
69 // }
70 // .egl_display = EGL_NO_DISPLAY;
71 // .egl_context = EGL_NO_CONTEXT;
72 TFL_CAPI_EXPORT TfLiteDelegate* TfLiteGpuDelegateCreate_New(
73     const TfLiteGpuDelegateOptions_New* options);
74 
75 // Destroys a delegate created with `TfLiteGpuDelegateCreate_New` call.
76 TFL_CAPI_EXPORT void TfLiteGpuDelegateDelete_New(TfLiteDelegate* delegate);
77 
78 typedef enum {
79   TFLITE_GPU_DATA_LAYOUT_BHWC = 0,
80   TFLITE_GPU_DATA_LAYOUT_DHWC4 = 1,
81 } TfLiteGpuDataLayout;
82 
83 // Binds GL shader storage object to an input or an output tensor in the
84 // initialized delegate. Bound buffer should have sufficient storage to
85 // accommodate all elements of a tensor.
86 //
87 // Supports data of kTfliteFloat16 or kTfliteFloat32 types in BHWC or DHWC4 data
88 // layouts.
89 //
90 // *** Must be called *before* `Interpreter::ModifyGraphWithDelegate`. ***
91 TFL_CAPI_EXPORT TfLiteStatus TfLiteGpuDelegateBindGlBufferToTensor(
92     TfLiteDelegate* delegate, GLuint buffer_id, int tensor_index,
93     TfLiteType data_type, TfLiteGpuDataLayout data_layout);
94 
95 // Returns opaque binary blob that contains a collection of cached OpenCL
96 // binaries. Returned data could be re-used later to speed up initialization
97 // time when new delegate is created for the same model.
98 // Returned data is valid only if used on the same device, otherwise it will
99 // not be compatible and will be discarded.
100 TFL_CAPI_EXPORT bool TfLiteGpuDelegateGetSerializedBinaryCache(
101     TfLiteDelegate* delegate, size_t* size, const uint8_t** data);
102 
103 #ifdef __cplusplus
104 }
105 #endif  // __cplusplus
106 
107 #endif  // TENSORFLOW_LITE_DELEGATES_GPU_CL_GPU_API_DELEGATE_H_
108