• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2021 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 #ifndef TENSORFLOW_LITE_EXPERIMENTAL_ACCELERATION_CONFIGURATION_GPU_PLUGIN_H_
16 #define TENSORFLOW_LITE_EXPERIMENTAL_ACCELERATION_CONFIGURATION_GPU_PLUGIN_H_
17 
18 // This file provides the GpuPlugin class, which implements the
19 // TFLite Delegate Plugin for the GPU Delegate.
20 
21 #if defined(__ANDROID__) || defined(CL_DELEGATE_NO_GL)
22 #define TFLITE_SUPPORTS_GPU_DELEGATE 1
23 #endif
24 
25 #include <string>
26 
27 #if TFLITE_SUPPORTS_GPU_DELEGATE
28 #include "tensorflow/lite/delegates/gpu/delegate.h"
29 #elif defined(__APPLE__)
30 #include "TargetConditionals.h"
31 #if (TARGET_OS_IPHONE && !TARGET_IPHONE_SIMULATOR) || \
32     (TARGET_OS_OSX && TARGET_CPU_ARM64)
33 // Only enable metal delegate when using a real iPhone device or Apple Silicon.
34 #define REAL_IPHONE_DEVICE
35 #include "tensorflow/lite/delegates/gpu/metal_delegate.h"
36 #endif
37 #endif
38 
39 #include "tensorflow/lite/experimental/acceleration/configuration/configuration_generated.h"
40 #include "tensorflow/lite/experimental/acceleration/configuration/delegate_registry.h"
41 
42 namespace tflite {
43 namespace delegates {
44 
45 // Note that if running on GPU is not supported for some reason (e.g., desktop
46 // machine with no OpenGL/CL), this library will still compile but calling
47 // Create() will return a nullptr.
48 class GpuPlugin : public DelegatePluginInterface {
49  public:
50   explicit GpuPlugin(const TFLiteSettings& tflite_settings);
51   static std::unique_ptr<DelegatePluginInterface> New(
52       const TFLiteSettings& acceleration);
53 
54   TfLiteDelegatePtr Create() override;
55   int GetDelegateErrno(TfLiteDelegate* from_delegate) override;
56 
57 #if TFLITE_SUPPORTS_GPU_DELEGATE
Options()58   const TfLiteGpuDelegateOptionsV2& Options() { return options_; }
59 #elif defined(REAL_IPHONE_DEVICE)
Options()60   const TFLGpuDelegateOptions& Options() { return options_; }
61 #endif
62 
63  private:
64 #if TFLITE_SUPPORTS_GPU_DELEGATE
65   TfLiteGpuDelegateOptionsV2 options_;
66 #elif defined(REAL_IPHONE_DEVICE)
67   TFLGpuDelegateOptions options_;
68 #endif
69   std::string cache_dir_;
70   std::string model_token_;
71 };
72 
73 }  // namespace delegates
74 }  // namespace tflite
75 
76 #endif  // TENSORFLOW_LITE_EXPERIMENTAL_ACCELERATION_CONFIGURATION_GPU_PLUGIN_H_
77