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 16 #include "tensorflow/core/tpu/tpu_embedding_configuration_utils.h" 17 18 #include "absl/strings/str_format.h" 19 #include "tensorflow/core/protobuf/tpu/optimization_parameters.pb.h" 20 21 namespace tensorflow { 22 namespace tpu { 23 ComputeTotalTagCountForDynamicLearningRates(const tensorflow::tpu::TPUEmbeddingConfiguration & tpu_embedding_config)24absl::StatusOr<int32_t> ComputeTotalTagCountForDynamicLearningRates( 25 const tensorflow::tpu::TPUEmbeddingConfiguration& tpu_embedding_config) { 26 // Ordering of tag elements helps make the subsequent error checking simpler. 27 std::set<int32_t> tag_set; 28 29 for (const auto& table_descriptor : tpu_embedding_config.table_descriptor()) { 30 const auto& lr_spec = 31 table_descriptor.optimization_parameters().learning_rate(); 32 if (lr_spec.has_dynamic()) { 33 tag_set.insert(lr_spec.dynamic().tag()); 34 } 35 } 36 37 // Traverse the tag set to determine that tags are contiguous. 38 int32_t next_tag = 0; 39 for (const int32_t tag : tag_set) { 40 if (tag != next_tag) { 41 return absl::InvalidArgumentError(absl::StrFormat( 42 "Dynamic learning rate tag: %d not found in the TPU embedding " 43 "configuration, instead found: %d. tag set size: %d", 44 next_tag, tag, tag_set.size())); 45 } 46 ++next_tag; 47 } 48 49 return tag_set.size(); 50 } 51 52 } // namespace tpu 53 } // namespace tensorflow 54