1 /* Copyright 2020 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_COMPATIBILITY_GPU_COMPATIBILITY_H_ 16 #define TENSORFLOW_LITE_EXPERIMENTAL_ACCELERATION_COMPATIBILITY_GPU_COMPATIBILITY_H_ 17 18 #include <map> 19 #include <memory> 20 #include <string> 21 22 #include "tensorflow/lite/delegates/gpu/common/gpu_info.h" 23 #include "tensorflow/lite/delegates/gpu/delegate_options.h" 24 #include "tensorflow/lite/experimental/acceleration/compatibility/android_info.h" 25 #include "tensorflow/lite/experimental/acceleration/compatibility/database_generated.h" 26 27 namespace tflite { 28 namespace acceleration { 29 30 // This class provides information on GPU delegate support. 31 // 32 // The GPU delegate is supported on a subset of Android devices, depending on 33 // Android version, OpenGL ES version, GPU chipset etc. The support is based on 34 // measure stability, correctness and peformance. For more detail see README.md. 35 // 36 // Example usage: 37 // tflite::Interpreter* interpreter = ... ; 38 // tflite::acceleration::AndroidInfo android_info; 39 // tflite::gpu::GpuInfo gpu_info; 40 // EXPECT_OK(tflite::acceleration::RequestAndroidInfo(&android_info)); 41 // EXPECT_OK(tflite::gpu::gl::EglEnvironment::NewEglEnvironment(&env)); 42 // EXPECT_OK(tflite::gpu::gl::RequestGpuInfo(&tflite_gpu_info)); 43 // tflite::acceleration::GPUCompatibilityList list; 44 // TfLiteDelegate* gpu_delegate = nullptr; 45 // TfLiteGpuDelegateOptions gpu_options; 46 // if (list.Includes(android_info, gpu_info)) { 47 // gpu_options = list.BestOptionsFor(android_info, gpu_info); 48 // gpu_delegate = TfLiteGpuDelegateCreate(&gpu_options); 49 // EXPECT_EQ(interpreter->ModifyGraphWithDelegate(gpu_delegate), TfLiteOk); 50 // } else { 51 // // Fallback path. 52 // } 53 class GPUCompatibilityList { 54 public: 55 // Construct list from bundled data. Returns a unique_ptr to a nullptr if 56 // creation fails. 57 static std::unique_ptr<GPUCompatibilityList> Create(); 58 // Constructs list from the given flatbuffer data. Returns a unique_ptr to a 59 // nullptr is the given flatbuffer is empty or invalid. 60 static std::unique_ptr<GPUCompatibilityList> Create( 61 const unsigned char* compatibility_list_flatbuffer, int length); 62 // Returns true if the provided device specs are supported by the database. 63 bool Includes(const AndroidInfo& android_info, 64 const ::tflite::gpu::GpuInfo& gpu_info) const; 65 66 // Returns the best TfLiteGpuDelegateOptionsV2 for the provided device specs 67 // based on the database. The output can be modified as desired before passing 68 // to delegate creation. 69 TfLiteGpuDelegateOptionsV2 GetBestOptionsFor( 70 const AndroidInfo& android_info, 71 const ::tflite::gpu::GpuInfo& gpu_info) const; 72 73 // Convert android_info and gpu_info into a set of variables used for querying 74 // the list, and update variables from list data. See variables.h 75 // and devicedb.h for more information. 76 std::map<std::string, std::string> CalculateVariables( 77 const AndroidInfo& android_info, 78 const ::tflite::gpu::GpuInfo& gpu_info) const; 79 80 GPUCompatibilityList(const GPUCompatibilityList&) = delete; 81 GPUCompatibilityList& operator=(const GPUCompatibilityList&) = delete; 82 83 // Checks if the provided byte array represents a valid compatibility list 84 static bool IsValidFlatbuffer(const unsigned char* data, int len); 85 86 protected: 87 const DeviceDatabase* database_; 88 89 private: 90 explicit GPUCompatibilityList( 91 const unsigned char* compatibility_list_flatbuffer); 92 }; 93 94 } // namespace acceleration 95 } // namespace tflite 96 97 #endif // TENSORFLOW_LITE_EXPERIMENTAL_ACCELERATION_COMPATIBILITY_GPU_COMPATIBILITY_H_ 98