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/name_uniquer.h"
17
18 #include "absl/strings/ascii.h"
19 #include "absl/strings/numbers.h"
20 #include "absl/strings/str_cat.h"
21 #include "tensorflow/compiler/xla/primitive_util.h"
22 #include "tensorflow/compiler/xla/types.h"
23 #include "tensorflow/core/platform/logging.h"
24 #include "tensorflow/core/platform/types.h"
25
26 namespace xla {
27
28 namespace {
29
IsAllowed(char character)30 bool IsAllowed(char character) {
31 auto c = static_cast<unsigned char>(character);
32 return (absl::ascii_isalnum(c) != 0) || c == '_' || c == '.' || c == '-';
33 }
34
35 } // namespace
36
NameUniquer(const string & separator)37 NameUniquer::NameUniquer(const string& separator) {
38 CHECK(absl::c_all_of(separator, IsAllowed))
39 << "separator should comprises allowed characters only";
40 separator_ = separator;
41 }
42
GetSanitizedName(const string & name)43 /*static*/ string NameUniquer::GetSanitizedName(const string& name) {
44 if (name.empty()) {
45 return "";
46 }
47
48 string result = name;
49 char c = static_cast<unsigned char>(result[0]);
50 if (!absl::ascii_isalpha(c) && c != '_') {
51 result[0] = '_';
52 }
53 for (int i = 1; i < result.length(); i++) {
54 if (!IsAllowed(result[i])) {
55 result[i] = '_';
56 }
57 }
58
59 // HLO primitive type names (with the exception of 'tuple') are keywords in
60 // the HLO text representation and cannot be names, so append an underscore if
61 // the name is a primitive type.
62 if (primitive_util::IsPrimitiveTypeName(result) && result != "tuple") {
63 result += "_";
64 }
65 return result;
66 }
67
GetUniqueName(absl::string_view prefix)68 string NameUniquer::GetUniqueName(absl::string_view prefix) {
69 string root = GetSanitizedName(prefix.empty() ? "name" : string(prefix));
70
71 // Strip away numeric suffix (if any). Only recognize separator if it is in
72 // the middle of the name.
73 bool has_numeric_suffix = false;
74 int64 numeric_suffix = 0;
75 size_t separator_index = root.rfind(separator_);
76 if (separator_index != string::npos && (separator_index > 0) &&
77 (separator_index < root.size() - 1)) {
78 string after_suffix = root.substr(separator_index + 1);
79 if (absl::SimpleAtoi(after_suffix, &numeric_suffix)) {
80 has_numeric_suffix = true;
81 // Remove numeric suffix from root.
82 root = root.substr(0, separator_index);
83 } else {
84 // absl::SimpleAtoi may modify numeric_suffix even if it returns false.
85 numeric_suffix = 0;
86 }
87 }
88
89 SequentialIdGenerator& id_generator = generated_names_[root];
90 numeric_suffix = id_generator.RegisterId(numeric_suffix);
91 if (numeric_suffix == 0) {
92 return has_numeric_suffix ? absl::StrCat(root, separator_, 0) : root;
93 }
94 absl::StrAppend(&root, separator_, numeric_suffix);
95 return root;
96 }
97
98 } // namespace xla
99