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_PROGRAM_CACHE_H_ 17 #define TENSORFLOW_LITE_DELEGATES_GPU_CL_PROGRAM_CACHE_H_ 18 19 #include <cstdint> 20 #include <string> 21 #include <vector> 22 23 #include "absl/container/flat_hash_map.h" 24 #include "absl/types/span.h" 25 #include "tensorflow/lite/delegates/gpu/cl/cl_context.h" 26 #include "tensorflow/lite/delegates/gpu/cl/cl_device.h" 27 #include "tensorflow/lite/delegates/gpu/cl/cl_kernel.h" 28 #include "tensorflow/lite/delegates/gpu/cl/cl_program.h" 29 #include "tensorflow/lite/delegates/gpu/common/status.h" 30 31 namespace tflite { 32 namespace gpu { 33 namespace cl { 34 35 class ProgramCache { 36 public: 37 ProgramCache() = default; 38 39 ProgramCache(ProgramCache&& program_cache); 40 ProgramCache& operator=(ProgramCache&& program_cache); 41 ProgramCache(const ProgramCache&) = delete; 42 ProgramCache& operator=(const ProgramCache&) = delete; 43 44 absl::Status GetOrCreateCLKernel( 45 const std::string& code, const std::string& function_name, 46 const std::vector<CompilerOptions>& compiler_options, 47 const CLContext& context, const CLDevice& device, CLKernel* result); 48 49 absl::Status GetOrCreateCLKernel(const std::string& code, 50 const std::string& function_name, 51 const CLContext& context, 52 const CLDevice& device, CLKernel* result); 53 54 absl::Status AddSerializedCache(const CLContext& context, 55 const CLDevice& device, 56 absl::Span<const uint8_t> serialized_cache); 57 absl::Status GetSerializedCache(const CLDevice& device, 58 std::vector<uint8_t>* serialized_cache) const; 59 60 private: 61 struct ProgramDescriptor { 62 ProgramDescriptor() = default; 63 ProgramDescriptor(const std::string& code_text, const std::string& options, 64 bool use_fingerprint); 65 explicit ProgramDescriptor(uint64_t fingerprint); 66 67 std::string code; 68 std::string compiler_options; 69 uint64_t fingerprint; 70 bool use_fingerprint; 71 }; 72 struct ProgramDescriptorHasher { operatorProgramDescriptorHasher73 std::size_t operator()(const ProgramDescriptor& k) const { 74 if (k.use_fingerprint) { 75 return std::hash<uint64_t>()(k.fingerprint); 76 } else { 77 return std::hash<std::string>()(k.code) + 78 std::hash<std::string>()(k.compiler_options); 79 } 80 } 81 }; 82 struct ProgramDescriptorEqual { operatorProgramDescriptorEqual83 bool operator()(const ProgramDescriptor& a, 84 const ProgramDescriptor& b) const { 85 if (a.use_fingerprint && b.use_fingerprint) { 86 return a.fingerprint == b.fingerprint; 87 } else { 88 return a.compiler_options == b.compiler_options && a.code == b.code; 89 } 90 } 91 }; 92 93 // There is a low probability of a hash collision when cache is deserialized 94 // because only fingerprints are serialized instead of full source code. 95 bool use_fingerprints_ = false; 96 absl::flat_hash_map<ProgramDescriptor, CLProgram, ProgramDescriptorHasher, 97 ProgramDescriptorEqual> 98 programs_; 99 }; 100 101 } // namespace cl 102 } // namespace gpu 103 } // namespace tflite 104 105 #endif // TENSORFLOW_LITE_DELEGATES_GPU_CL_PROGRAM_CACHE_H_ 106