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 16 #ifndef TENSORFLOW_CORE_TPU_KERNELS_TPU_FINGERPRINT_LOOKUP_H_ 17 #define TENSORFLOW_CORE_TPU_KERNELS_TPU_FINGERPRINT_LOOKUP_H_ 18 19 #include <cstddef> 20 #include <deque> 21 22 #include "absl/container/flat_hash_map.h" 23 #include "absl/container/node_hash_map.h" 24 #include "absl/strings/string_view.h" 25 #include "absl/synchronization/mutex.h" 26 #include "tensorflow/core/framework/resource_mgr.h" 27 #include "tensorflow/core/platform/stringpiece.h" 28 29 namespace tensorflow { 30 namespace tpu { 31 32 // A class that holds the key-value pair of fingerprints. By calling the 33 // Register method, this class can map the key to the value. Note that this 34 // class holds invariant key-value pairs. That is, it does not allow updating 35 // key-value pairs, nor N-key-to-1-value and 1-key-to-M-value pairs. If such 36 // cases occur, the class keeps the earliest registered pairs and discards any 37 // violating pairs. 38 // 39 // Example: 40 // TpuFingerprintLookup fingerprint_lookup; 41 // 42 // // Register key-intermediate pair. 43 // fingerprint_lookup.RegisterKeyValuePair("key1", "intermediate1"); 44 // // Register intermediate-value pair. 45 // fingerprint_lookup.RegisterKeyValuePair("intermediate1", "value1"); 46 // 47 // // Lookup fingerprint with key. 48 // std::string fingerprint = fingerprint_lookup.Lookup("key1"); 49 // 50 // TODO(chiachenc): use templates and add Unregister methods. 51 class TpuFingerprintLookup : public ResourceBase { 52 public: 53 // Creates an instance of TpuFingerprintLookup. 54 static TpuFingerprintLookup* Create(); 55 56 // Register key-intermediate pair 57 void RegisterKeyAndIntermediatePair(uint64 key, uint64 intermediate); 58 59 // Register intermediate-value pair. A successful registration requires a 60 // preceding RegisterKeyAndIntermediatePair. Return true if successfully 61 // registering a key-value pair; otherwise, return false. 62 bool RegisterIntermediateAndValuePair(uint64 intermediate, std::string value); 63 64 // Look up fingerprint with key. 65 // Return absl::optional<::tensorflow::StringPiece>{} if 66 // not found. 67 absl::optional<::tensorflow::StringPiece> Lookup(uint64 key); 68 num_valid()69 size_t num_valid() { 70 absl::MutexLock lock(&mu_); 71 return key_to_value_.size(); 72 } 73 DebugString()74 std::string DebugString() const override { return "TpuFingerprintLookup"; } 75 76 private: TpuFingerprintLookup()77 explicit TpuFingerprintLookup() {} 78 79 absl::Mutex mu_; 80 // Main storage for lookup 81 absl::node_hash_map<uint64, std::string> key_to_value_ ABSL_GUARDED_BY(mu_); 82 83 // An auxiliary storage to ensure 1-to-1 and invariant key-value pair 84 absl::node_hash_map<std::string, uint64> value_to_key_ ABSL_GUARDED_BY(mu_); 85 86 // An auxiliary storage to keep intermediate-key pairs. 87 absl::flat_hash_map<uint64, uint64> intermediate_to_key_ ABSL_GUARDED_BY(mu_); 88 89 TpuFingerprintLookup(const TpuFingerprintLookup&) = delete; 90 TpuFingerprintLookup& operator=(const TpuFingerprintLookup&) = delete; 91 }; 92 } // namespace tpu 93 } // namespace tensorflow 94 #endif // TENSORFLOW_CORE_TPU_KERNELS_TPU_FINGERPRINT_LOOKUP_H_ 95