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 #include "tensorflow/compiler/xla/service/compilation_cache.h" 17 18 #include <utility> 19 20 #include "tensorflow/compiler/xla/types.h" 21 #include "tensorflow/compiler/xla/util.h" 22 #include "tensorflow/compiler/xla/xla_data.pb.h" 23 #include "tensorflow/core/lib/strings/strcat.h" 24 #include "tensorflow/core/platform/logging.h" 25 26 namespace xla { 27 28 namespace { 29 GetUniqueId()30int64 GetUniqueId() { 31 static tensorflow::mutex mu(tensorflow::LINKER_INITIALIZED); 32 static int64 counter = 0; 33 tensorflow::mutex_lock loc(mu); 34 const int64 id = counter++; 35 return id; 36 } 37 38 } // namespace 39 Insert(std::unique_ptr<Executable> executable)40ExecutionHandle CompilationCache::Insert( 41 std::unique_ptr<Executable> executable) { 42 tensorflow::mutex_lock lock(mutex_); 43 44 CacheKey key = GetUniqueId(); 45 VLOG(2) << "inserting cache key: " << key; 46 CHECK_EQ(cache_.count(key), 0); 47 cache_.emplace(key, std::move(executable)); 48 49 ExecutionHandle handle; 50 handle.set_handle(key); 51 return handle; 52 } 53 LookUp(const ExecutionHandle & handle) const54StatusOr<std::shared_ptr<Executable>> CompilationCache::LookUp( 55 const ExecutionHandle& handle) const { 56 tensorflow::mutex_lock lock(mutex_); 57 58 CacheKey key = handle.handle(); 59 VLOG(2) << "looking up cache key: " << key; 60 if (cache_.count(key) == 0) { 61 VLOG(2) << "cache key not found: " << key; 62 return InvalidArgumentStrCat("can not find executable with handle ", key); 63 } else { 64 auto& result = cache_.at(key); 65 VLOG(2) << "hit executable: " << result->module().name(); 66 return result; 67 } 68 } 69 70 } // namespace xla 71