forked from apache/tvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathname_transforms.cc
More file actions
104 lines (85 loc) · 3.39 KB
/
name_transforms.cc
File metadata and controls
104 lines (85 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include "name_transforms.h"
#include <tvm/runtime/registry.h>
#include <cctype>
#include <string>
namespace tvm {
namespace relay {
namespace backend {
std::string ToCFunctionStyle(const std::string& original_name) {
ICHECK(!original_name.empty()) << "Function name is empty";
ICHECK_EQ(original_name.find("TVM"), 0) << "Function not TVM prefixed";
int tvm_prefix_length = 3;
std::string function_name("TVM");
bool new_block = true;
for (const char& symbol : original_name.substr(tvm_prefix_length)) {
if (std::isalpha(symbol)) {
if (new_block) {
function_name.push_back(std::toupper(symbol));
new_block = false;
} else {
function_name.push_back(std::tolower(symbol));
}
} else if (symbol == '_') {
new_block = true;
}
}
return function_name;
}
std::string ToCVariableStyle(const std::string& original_name) {
ICHECK(!original_name.empty()) << "Variable name is empty";
ICHECK_EQ(original_name.find("TVM"), 0) << "Variable not TVM prefixed";
std::string variable_name;
variable_name.resize(original_name.size());
std::transform(original_name.begin(), original_name.end(), variable_name.begin(), ::tolower);
return variable_name;
}
std::string CombineNames(const Array<String>& names) {
std::stringstream combine_stream;
ICHECK(!names.empty()) << "Name segments empty";
for (const String& name : names) {
ICHECK(!name.empty()) << "Name segment is empty";
combine_stream << name << "_";
}
std::string combined_name = combine_stream.str();
combined_name.pop_back();
return combined_name;
}
std::string SanitizeName(const std::string& name) {
ICHECK(!name.empty()) << "Name is empty";
auto multipleSeparators = [](char before, char after) {
return before == '_' && before == after;
};
auto isNotAlnum = [](char c) { return !std::isalnum(c); };
std::string sanitized_input = name;
std::replace_if(sanitized_input.begin(), sanitized_input.end(), isNotAlnum, '_');
sanitized_input.erase(
std::unique(sanitized_input.begin(), sanitized_input.end(), multipleSeparators),
sanitized_input.end());
return sanitized_input;
}
TVM_REGISTER_GLOBAL("relay.backend.ToCFunctionStyle").set_body_typed(ToCFunctionStyle);
TVM_REGISTER_GLOBAL("relay.backend.ToCVariableStyle").set_body_typed(ToCVariableStyle);
TVM_REGISTER_GLOBAL("relay.backend.PrefixName").set_body_typed(PrefixName);
TVM_REGISTER_GLOBAL("relay.backend.PrefixGeneratedName").set_body_typed(PrefixGeneratedName);
TVM_REGISTER_GLOBAL("relay.backend.SanitizeName").set_body_typed(SanitizeName);
} // namespace backend
} // namespace relay
} // namespace tvm