Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/node/script_printer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <tvm/node/script_printer.h>
#include <tvm/runtime/registry.h>

#include <regex>
#include <algorithm>

namespace tvm {

Expand All @@ -38,8 +38,17 @@ std::string TVMScriptPrinter::Script(const ObjectRef& node, const Optional<Print
}

bool IsIdentifier(const std::string& name) {
static const std::regex kValidIdentifier("^[a-zA-Z_][a-zA-Z0-9_]*$");
return std::regex_match(name, kValidIdentifier);
// Python identifiers follow the regex: "^[a-zA-Z_][a-zA-Z0-9_]*$"
// `std::regex` would cause a symbol conflict with PyTorch, we avoids to use it in the codebase.
//
// We convert the regex into following conditions:
// 1. The name is not empty.
// 2. The first character is either an alphabet or an underscore.
// 3. The rest of the characters are either an alphabet, a digit or an underscore.
return name.size() > 0 && //
(std::isalpha(name[0]) || name[0] == '_') && //
std::all_of(name.begin() + 1, name.end(),
[](char c) { return std::isalnum(c) || c == '_'; });
}

PrinterConfig::PrinterConfig(Map<String, ObjectRef> config_dict) {
Expand Down
7 changes: 4 additions & 3 deletions src/relax/ir/dataflow_matcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include <cstddef>
#include <limits>
#include <optional>
#include <regex>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
Expand Down Expand Up @@ -557,8 +557,9 @@ bool DFPatternMatcher::VisitDFPattern_(const DataflowVarPatternNode* op, const E
bool DFPatternMatcher::VisitDFPattern_(const GlobalVarPatternNode* op, const Expr& expr) {
// GlobalVarPattern is not inherited from Var, so we need to handle it separately.
if (const auto* var_node = expr.as<GlobalVarNode>()) {
std::regex pat{std::string(op->name_hint())};
return "" == op->name_hint() || std::regex_search(std::string(var_node->name_hint), pat);
std::string pat = std::string(op->name_hint());
std::string var_name = std::string(var_node->name_hint);
return pat.empty() || var_name.find(pat) != std::string::npos;
}
return false;
}
Expand Down
1 change: 0 additions & 1 deletion src/runtime/contrib/cublas/cublas_json_runtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include <tvm/runtime/registry.h>

#include <cstddef>
#include <regex>
#include <string>
#include <vector>

Expand Down
3 changes: 1 addition & 2 deletions src/runtime/contrib/cudnn/cudnn_json_runtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include <tvm/runtime/registry.h>

#include <cstddef>
#include <regex>
#include <string>
#include <vector>

Expand Down Expand Up @@ -54,7 +53,7 @@ class cuDNNJSONRuntime : public JSONRuntimeBase {
stream = static_cast<cudaStream_t>((*func)().operator void*());

auto attr_in_name = [](const std::string& op_name, const std::string& attr_name) {
return std::regex_search(op_name, std::regex(attr_name));
return op_name.find(attr_name) != std::string::npos;
};

auto vstr2vint = [](const JSONGraphNode& node, const std::string& attrStr) {
Expand Down
4 changes: 3 additions & 1 deletion tests/python/relax/test_dataflow_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ def test_dataflow_var_pattern():

def test_global_var_pattern():
assert is_gv("x").match(rx.GlobalVar("x"))
assert is_gv("x.*").match(rx.GlobalVar("x_2"))
# TODO: disabled as regex is not supported due to
# symbol conflict with PyTorch
# assert is_gv("x.*").match(rx.GlobalVar("x_2"))
assert is_gv().match(rx.GlobalVar("x"))
assert not is_gv("x").match(rx.GlobalVar("y"))
assert not is_gv("x").match(rx.Var("x"))
Expand Down