1 | /* Copyright 2018 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/framework/function_handle_cache.h" |
16 | |
17 | #include "tensorflow/core/lib/gtl/map_util.h" |
18 | #include "tensorflow/core/lib/random/random.h" |
19 | #include "tensorflow/core/lib/strings/stringprintf.h" |
20 | |
21 | namespace tensorflow { |
22 | |
23 | FunctionHandleCache::FunctionHandleCache(FunctionLibraryRuntime* lib) |
24 | : lib_(lib), |
25 | state_handle_( |
26 | strings::Printf("%lld" , static_cast<long long>(random::New64()))) {} |
27 | |
28 | FunctionHandleCache::~FunctionHandleCache() { |
29 | Status s = Clear(); |
30 | if (!s.ok()) { |
31 | LOG(ERROR) << "Failed to clear function handle cache: " << s.ToString(); |
32 | } |
33 | } |
34 | |
35 | Status FunctionHandleCache::Instantiate( |
36 | const string& function_name, AttrSlice attrs, |
37 | FunctionLibraryRuntime::InstantiateOptions options, |
38 | FunctionLibraryRuntime::Handle* handle) { |
39 | string key = Canonicalize(function_name, attrs, options); |
40 | FunctionLibraryRuntime::Handle h; |
41 | { |
42 | tf_shared_lock l(mu_); |
43 | h = gtl::FindWithDefault(handles_, key, kInvalidHandle); |
44 | } |
45 | if (h == kInvalidHandle) { |
46 | options.state_handle = state_handle_; |
47 | TF_RETURN_IF_ERROR( |
48 | lib_->Instantiate(function_name, attrs, options, handle)); |
49 | mutex_lock l(mu_); |
50 | handles_[key] = *handle; |
51 | } else { |
52 | *handle = h; |
53 | } |
54 | return OkStatus(); |
55 | } |
56 | |
57 | Status FunctionHandleCache::Clear() { |
58 | mutex_lock l(mu_); |
59 | for (const auto& entry : handles_) { |
60 | TF_RETURN_IF_ERROR(lib_->ReleaseHandle(entry.second)); |
61 | } |
62 | handles_.clear(); |
63 | return OkStatus(); |
64 | } |
65 | |
66 | } // namespace tensorflow |
67 | |