xref: /aosp_15_r20/external/tensorflow/tensorflow/core/tfrt/eager/op_cache.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2021 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 #include "tensorflow/core/tfrt/eager/op_cache.h"
16 
17 #include "tensorflow/core/tfrt/eager/c_api_tfrt.h"
18 #include "tfrt/core_runtime/core_runtime.h"  // from @tf_runtime
19 #include "tfrt/core_runtime/core_runtime_op.h"  // from @tf_runtime
20 #include "tfrt/support/error_util.h"  // from @tf_runtime
21 
22 namespace tfrt {
23 namespace tf {
24 
GetOrAddOp(string_view op_name,OpHandler * op_handler,string_view device_name,llvm::SmallVector<string_view,4> dtypes,OperationInterface * const op_interface)25 Expected<CoreRuntimeOp*> OpCache::GetOrAddOp(
26     string_view op_name, OpHandler* op_handler, string_view device_name,
27     llvm::SmallVector<string_view, 4> dtypes,
28     OperationInterface* const op_interface) {
29   CacheKey cache_key{op_name, op_handler,
30                      (op_handler == nullptr ? device_name : ""), dtypes};
31   {
32     mutex_lock l(cache_mu_);
33     auto iter = cache_.find(cache_key);
34     if (iter != cache_.end()) return &iter->second;
35   }
36 
37   ContextInterface* context = op_interface->context_;
38 
39   auto tfrt_op_name = StrCat("tf.", op_name);
40   op_interface->MaybeInferInputAttrs();
41   if (op_handler == nullptr) {
42     tensorflow::Status s = context->SelectOpHandlerFromNodeDef(
43         *op_interface, &op_interface->fallback_attrs_.BuildNodeDef(),
44         &op_handler);
45     if (!s.ok()) return MakeStringError(s.error_message());
46   }
47   Expected<CoreRuntimeOp> expected_op =
48       context->GetCoreRuntime()->MakeOp(tfrt_op_name, op_handler);
49   if (!expected_op) return MakeStringError(expected_op.takeError());
50 
51   mutex_lock l(cache_mu_);
52   // Insert the new op to cache. If an entry with the same key is already
53   // present in the cache at this moment due to race condition, overwrites it.
54   cache_key.MakeConcrete();
55   cache_[cache_key] = std::move(expected_op.get());
56   return &cache_[cache_key];
57 }
58 
GetOrAddXlaOp(string_view op_name,ContextInterface * context)59 Expected<CoreRuntimeOp*> OpCache::GetOrAddXlaOp(string_view op_name,
60                                                 ContextInterface* context) {
61   // Device name and dtype are not meaningful to a XLA op.
62   CacheKey cache_key{op_name, nullptr, "", {}};
63   {
64     mutex_lock l(cache_mu_);
65     auto iter = cache_.find(cache_key);
66     if (iter != cache_.end()) return &iter->second;
67   }
68 
69   auto tfrt_op_name = StrCat("tf.", op_name);
70   Expected<CoreRuntimeOp> expected_op = context->GetCoreRuntime()->MakeOp(
71       tfrt_op_name, context->GetFallbackOpHandler());
72   if (!expected_op) return MakeStringError(expected_op.takeError());
73 
74   mutex_lock l(cache_mu_);
75   // Insert the new op to cache. If an entry with the same key is already
76   // present in the cache at this moment due to race condition, overwrites it.
77   cache_key.MakeConcrete();
78   cache_[cache_key] = std::move(expected_op.get());
79   return &cache_[cache_key];
80 }
81 
82 }  // namespace tf
83 }  // namespace tfrt
84