xref: /aosp_15_r20/external/tensorflow/tensorflow/core/tpu/kernels/tpu_compilation_cache_factory.cc (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 
16 #include "tensorflow/core/tpu/kernels/tpu_compilation_cache_factory.h"
17 
18 #include "tensorflow/core/framework/resource_mgr.h"
19 #include "tensorflow/core/platform/types.h"
20 #include "tensorflow/core/tpu/kernels/tpu_compilation_cache_external.h"
21 #include "tensorflow/core/tpu/kernels/tpu_compilation_cache_interface.h"
22 #include "tensorflow/core/tpu/kernels/tpu_op_consts.h"
23 
24 namespace tensorflow {
25 namespace tpu {
26 namespace {
27 
CreateCompilationCacheExternal()28 TpuCompilationCacheInterface* CreateCompilationCacheExternal() {
29   // NOTE: Change the 1 << 33 value to change the compilation cache size.
30   // TODO(frankchn): Make this configurable.
31   return new TpuCompilationCacheExternal(int64_t{1} << 33);  // 8 GB
32 }
33 
34 // Using a pointer here to fulfill the trivially destructible requirement for
35 // static variables.
36 static std::function<TpuCompilationCacheInterface*()>*
37     compilation_cache_creation_fn =
38         new std::function<TpuCompilationCacheInterface*()>(
39             CreateCompilationCacheExternal);
40 
41 }  // namespace
42 
GetCompilationCacheCreateFn()43 std::function<TpuCompilationCacheInterface*()> GetCompilationCacheCreateFn() {
44   return *compilation_cache_creation_fn;
45 }
46 
SetCompilationCacheCreateFn(std::function<TpuCompilationCacheInterface * ()> fn)47 void SetCompilationCacheCreateFn(
48     std::function<TpuCompilationCacheInterface*()> fn) {
49   delete compilation_cache_creation_fn;
50   compilation_cache_creation_fn =
51       new std::function<TpuCompilationCacheInterface*()>(fn);
52 }
53 
54 }  // namespace tpu
55 }  // namespace tensorflow
56