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 // Unique session identifier for TPU compilation; it should be a 64 bit 41 // positive integer, which can uniquely distinguish a live session. 42 // TPU compiler may use this information to choose dynamically provided 43 // compilation options without hurting reproducibility for debugging. 44 uint64_t session_id; 45 46 // Fingerprint of `guaranteed_const` value. It is set when the value of the 47 // `has_guaranteed_const` is true. Produce the value when necessary. 48 std::function<std::string()> guaranteed_const_fingerprint; 49 50 // A more verbose key for debugging purpose. 51 std::string debug_string; 52 53 // Constructs the TPU compilation cache key by concatenating the `prefix`, 54 // `session_handle` and `guaranteed_const_fingerprint`. ToStringTpuCompilationCacheKey55 std::string ToString() const { 56 if (!has_guaranteed_const) { 57 return prefix; 58 } 59 return absl::StrCat(prefix, "|", session_handle, "|", 60 guaranteed_const_fingerprint()); 61 } 62 TpuCompilationCacheKeyTpuCompilationCacheKey63 explicit TpuCompilationCacheKey() {} TpuCompilationCacheKeyTpuCompilationCacheKey64 explicit TpuCompilationCacheKey(const std::string& p) : prefix(p) {} 65 }; 66 67 } // namespace tpu 68 } // namespace tensorflow 69 70 #endif // TENSORFLOW_CORE_TPU_KERNELS_TPU_COMPILATION_CACHE_KEY_H_ 71