xref: /aosp_15_r20/external/tensorflow/tensorflow/core/tpu/kernels/tpu_compilation_cache_external.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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_CORE_TPU_KERNELS_TPU_COMPILATION_CACHE_EXTERNAL_H_
16 #define TENSORFLOW_CORE_TPU_KERNELS_TPU_COMPILATION_CACHE_EXTERNAL_H_
17 
18 #include <functional>
19 #include <memory>
20 #include <unordered_map>
21 #include <vector>
22 
23 #include "absl/container/node_hash_map.h"
24 #include "absl/strings/string_view.h"
25 #include "absl/synchronization/mutex.h"
26 #include "absl/types/span.h"
27 #include "tensorflow/compiler/xla/service/hlo.pb.h"
28 #include "tensorflow/core/framework/resource_mgr.h"
29 #include "tensorflow/core/framework/tensor.h"
30 #include "tensorflow/core/platform/refcount.h"
31 #include "tensorflow/core/protobuf/tpu/compile_metadata.pb.h"
32 #include "tensorflow/core/tpu/kernels/compiled_subgraph.h"
33 #include "tensorflow/core/tpu/kernels/tpu_compilation_cache_common.pb.h"
34 #include "tensorflow/core/tpu/kernels/tpu_compilation_cache_entry.h"
35 #include "tensorflow/core/tpu/kernels/tpu_compilation_cache_interface.h"
36 #include "tensorflow/core/tpu/kernels/tpu_compilation_cache_key.h"
37 #include "tensorflow/core/tpu/kernels/tpu_compile_op_support.h"
38 #include "tensorflow/core/tpu/kernels/tpu_mesh_state_interface.h"
39 #include "tensorflow/core/tpu/kernels/tpu_op_consts.h"
40 #include "tensorflow/core/tpu/kernels/tpu_program_group.h"
41 #include "tensorflow/core/tpu/tpu_ops_c_api.h"
42 
43 namespace tensorflow {
44 namespace tpu {
45 
46 class TpuCompilationCacheExternal : public TpuCompilationCacheInterface {
47  public:
TpuCompilationCacheExternal(int64_t max_cache_size)48   explicit TpuCompilationCacheExternal(int64_t max_cache_size)
49       : TpuCompilationCacheInterface(max_cache_size) {}
50 
DebugString()51   string DebugString() const override { return "TpuCompilationCacheExternal"; }
52 
53  private:
54   // Creates a new entry by running initialize_programs and places it in the
55   // cache to be looked up by key. The new entry is in the 'marked for eviction'
56   // state (not present in entries_by_last_use_) and the caller is expected to
57   // call LookupEntryMarkedForEviction after InitializeEntry.
58   //
59   // **InitializeEntry releases mu_ during the call to initialize_programs.**
60   CompiledSubgraph* InitializeEntry(
61       const string& key,
62       const std::function<Status(TpuProgramGroupInterface*)>&
63           initialize_program,
64       const TpuCompilationCacheKey& subgraph_key)
65       ABSL_EXCLUSIVE_LOCKS_REQUIRED(TpuCompilationCacheInterface::mu_) override;
66 };
67 
68 }  // namespace tpu
69 }  // namespace tensorflow
70 
71 #endif  // TENSORFLOW_CORE_TPU_KERNELS_TPU_COMPILATION_CACHE_EXTERNAL_H_
72