This is an automated email from the ASF dual-hosted git repository.
junrushao pushed a commit to branch unity
in repository https://gitbox.apache.org/repos/asf/tvm.git
The following commit(s) were added to refs/heads/unity by this push:
new f794db4373 [Unity] Avoid to use `std::regex` (#16249)
f794db4373 is described below
commit f794db4373d48ee8a5c0b6d47e9a70019a1613bd
Author: Siyuan Feng <[email protected]>
AuthorDate: Sat Dec 16 08:15:38 2023 +0800
[Unity] Avoid to use `std::regex` (#16249)
`std::regex` in TVM codebase may cause a symbol conflict with PyTorch,
we temporarily disable it before we find a better solution, meanwhile
the current usage of `std::regex` is not necessary.
---
src/node/script_printer.cc | 15 ++++++++++++---
src/relax/ir/dataflow_matcher.cc | 7 ++++---
src/runtime/contrib/cublas/cublas_json_runtime.cc | 1 -
src/runtime/contrib/cudnn/cudnn_json_runtime.cc | 3 +--
tests/python/relax/test_dataflow_pattern.py | 4 +++-
5 files changed, 20 insertions(+), 10 deletions(-)
diff --git a/src/node/script_printer.cc b/src/node/script_printer.cc
index f2d985279f..38334de357 100644
--- a/src/node/script_printer.cc
+++ b/src/node/script_printer.cc
@@ -21,7 +21,7 @@
#include <tvm/node/script_printer.h>
#include <tvm/runtime/registry.h>
-#include <regex>
+#include <algorithm>
namespace tvm {
@@ -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) {
diff --git a/src/relax/ir/dataflow_matcher.cc b/src/relax/ir/dataflow_matcher.cc
index 9524c90b57..7fb67d9376 100644
--- a/src/relax/ir/dataflow_matcher.cc
+++ b/src/relax/ir/dataflow_matcher.cc
@@ -36,7 +36,7 @@
#include <cstddef>
#include <limits>
#include <optional>
-#include <regex>
+#include <string>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
@@ -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;
}
diff --git a/src/runtime/contrib/cublas/cublas_json_runtime.cc
b/src/runtime/contrib/cublas/cublas_json_runtime.cc
index c6916d4f86..23e35d2f71 100644
--- a/src/runtime/contrib/cublas/cublas_json_runtime.cc
+++ b/src/runtime/contrib/cublas/cublas_json_runtime.cc
@@ -26,7 +26,6 @@
#include <tvm/runtime/registry.h>
#include <cstddef>
-#include <regex>
#include <string>
#include <vector>
diff --git a/src/runtime/contrib/cudnn/cudnn_json_runtime.cc
b/src/runtime/contrib/cudnn/cudnn_json_runtime.cc
index 58e4e59afc..7d701396d0 100644
--- a/src/runtime/contrib/cudnn/cudnn_json_runtime.cc
+++ b/src/runtime/contrib/cudnn/cudnn_json_runtime.cc
@@ -26,7 +26,6 @@
#include <tvm/runtime/registry.h>
#include <cstddef>
-#include <regex>
#include <string>
#include <vector>
@@ -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)
{
diff --git a/tests/python/relax/test_dataflow_pattern.py
b/tests/python/relax/test_dataflow_pattern.py
index 685a382ad7..edd3bd1610 100644
--- a/tests/python/relax/test_dataflow_pattern.py
+++ b/tests/python/relax/test_dataflow_pattern.py
@@ -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"))