xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/service/compilation_cache.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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 
28 namespace xla {
29 
30 // A cache which stores Executables indexed by computation handle and version.
31 //
32 // TODO(b/119042872): Provide mechanism for removing computations from the
33 // compilation cache.
34 class CompilationCache {
35  public:
CompilationCache()36   CompilationCache() {}
37 
38   ExecutionHandle Insert(std::unique_ptr<Executable> executable);
39 
40   // Lookup the Executable for the specified handle in the cache. Return a
41   // shared_ptr to the Executable if it exists in the cache.
42   StatusOr<std::shared_ptr<Executable>> LookUp(
43       const ExecutionHandle& handle) const;
44 
45  protected:
46   mutable absl::Mutex mutex_;
47 
48   using CacheKey = int64_t;
49 
50   absl::flat_hash_map<CacheKey, std::shared_ptr<Executable>> cache_
51       ABSL_GUARDED_BY(mutex_);
52 
53  private:
54   CompilationCache(const CompilationCache&) = delete;
55   CompilationCache& operator=(const CompilationCache&) = delete;
56 };
57 
58 }  // namespace xla
59 
60 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_COMPILATION_CACHE_H_
61