1 /* Copyright 2017 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_COMPILER_XLA_SERVICE_COMPILATION_CACHE_H_ 17 #define TENSORFLOW_COMPILER_XLA_SERVICE_COMPILATION_CACHE_H_ 18 19 #include <map> 20 #include <memory> 21 #include <string> 22 23 #include "absl/container/flat_hash_map.h" 24 #include "tensorflow/compiler/xla/service/executable.h" 25 #include "tensorflow/compiler/xla/service/hlo_module_config.h" 26 #include "tensorflow/compiler/xla/types.h" 27 #include "tensorflow/core/platform/macros.h" 28 #include "tensorflow/core/platform/mutex.h" 29 #include "tensorflow/core/platform/thread_annotations.h" 30 31 namespace xla { 32 33 // A cache which stores Executables indexed by computation handle and version. 34 // 35 // TODO(b/119042872): Provide mechanism for removing computations from the 36 // compilation cache. 37 class CompilationCache { 38 public: CompilationCache()39 CompilationCache() {} 40 41 ExecutionHandle Insert(std::unique_ptr<Executable> executable); 42 43 // Lookup the Executable for the specified handle in the cache. Return a 44 // shared_ptr to the Executable if it exists in the cache. 45 StatusOr<std::shared_ptr<Executable>> LookUp( 46 const ExecutionHandle& handle) const; 47 48 protected: 49 mutable tensorflow::mutex mutex_; 50 51 using CacheKey = int64; 52 53 absl::flat_hash_map<CacheKey, std::shared_ptr<Executable>> cache_ 54 GUARDED_BY(mutex_); 55 56 private: 57 TF_DISALLOW_COPY_AND_ASSIGN(CompilationCache); 58 }; 59 60 } // namespace xla 61 62 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_COMPILATION_CACHE_H_ 63