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_KEY_H_ 16 #define TENSORFLOW_CORE_TPU_KERNELS_TPU_COMPILATION_CACHE_KEY_H_ 17 18 #include <functional> 19 #include <string> 20 21 #include "absl/strings/str_cat.h" 22 #include "absl/types/optional.h" 23 24 namespace tensorflow { 25 namespace tpu { 26 27 struct TpuCompilationCacheKey { 28 // Prefix of the key. 29 std::string prefix; 30 31 // A boolean flag to specify if `guaranteed_const` is used. Guarantee const is 32 // normally used in TPU inference to avoid re-copying unchanged variables onto 33 // the TPU device. It promises the value is identical for every execution in 34 // the same session even if the actual value changes in later executions. 35 bool has_guaranteed_const = false; 36 37 // Unique session identifier. It is set when `has_guaranteed_const` is true. 38 std::string session_handle; 39 40 // Fingerprint of `guaranteed_const` value. It is set when the value of the 41 // `has_guaranteed_const` is true. Produce the value when necessary. 42 std::function<std::string()> guaranteed_const_fingerprint; 43 44 // A more verbose key for debugging purpose. 45 std::string debug_string; 46 47 // Constructs the TPU compilation cache key by concatenating the `prefix`, 48 // `session_handle` and `guaranteed_const_fingerprint`. ToStringTpuCompilationCacheKey49 std::string ToString() const { 50 if (!has_guaranteed_const) { 51 return prefix; 52 } 53 return absl::StrCat(prefix, "|", session_handle, "|", 54 guaranteed_const_fingerprint()); 55 } 56 TpuCompilationCacheKeyTpuCompilationCacheKey57 explicit TpuCompilationCacheKey() {} TpuCompilationCacheKeyTpuCompilationCacheKey58 explicit TpuCompilationCacheKey(const std::string& p) : prefix(p) {} 59 }; 60 61 } // namespace tpu 62 } // namespace tensorflow 63 64 #endif // TENSORFLOW_CORE_TPU_KERNELS_TPU_COMPILATION_CACHE_KEY_H_ 65