Skip to content

PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度

fix: keep escapes and quantified dots in to_standard_regex (#2418)

Shubham Padkondefd28e78b40b1
diff --git a/auto_round/utils/common.py b/auto_round/utils/common.py
index 390c2cdae..6cc0fde63 100644
--- a/auto_round/utils/common.py
+++ b/auto_round/utils/common.py
@@ -896,23 +896,26 @@ def to_standard_regex(pattern: str) -> str:
'.*mlp.*gate.*'
"""
# Heuristic: if pattern contains regex meta characters, assume partial regex
- meta_chars = {".*", "^", "$", "|", "(", ")", "[", "]", "?", "+"}
+ meta_chars = {"*", "{", "}", "^", "$", "|", "(", ")", "[", "]", "?", "+", "\\"}
has_regex = any(tok in pattern for tok in meta_chars)
if not has_regex:
# Escape literal dots, etc., and wrap with .* for substring matching
pattern = re.escape(pattern)
regex = f".*{pattern}.*"
else:
- # Only escape bare dots that are not already part of regex constructs
- # Avoid double escaping .* sequences
+ # Only escape bare dots that are not already part of regex constructs:
+ # keep escape sequences (e.g. "\\.", "\\d") and quantified dots
+ # (".*", ".+", ".?", ".{n}") unchanged.
tmp = []
i = 0
while i < len(pattern):
+ if pattern[i] == "\\" and i + 1 < len(pattern):
+ tmp.append(pattern[i : i + 2]) # keep escape sequence
+ i += 2
+ continue
if pattern[i] == ".":
- if i + 1 < len(pattern) and pattern[i + 1] == "*":
- tmp.append(".*") # keep regex token
- i += 2
- continue
+ if i + 1 < len(pattern) and pattern[i + 1] in "*+?{":
+ tmp.append(".") # keep regex token
else:
tmp.append("\\.") # escape bare dot
else:
diff --git a/test/unit/common/utils/test_common_pure_helpers.py b/test/unit/common/utils/test_common_pure_helpers.py
index 1744c3cd2..1e474683d 100644
--- a/test/unit/common/utils/test_common_pure_helpers.py
+++ b/test/unit/common/utils/test_common_pure_helpers.py
@@ -228,6 +228,35 @@ class TestToStandardRegex:
# Must not raise when compiled
_re.compile(to_standard_regex("plain_text"))
+ @pytest.mark.parametrize(
+ "pattern, matching, nonmatching",
+ [
+ ("layers.{3}mlp", "layers.3.mlp", "layers.33.mlp"),
+ ("layers.{2,3}mlp", "layers.3.mlp", "layers.333.mlp"),
+ ("layers.{3,}mlp", "layers.33.mlp", "layers3mlp"),
+ ("layer*s", "layerrs", "layexs"),
+ ],
+ )
+ def test_unanchored_quantifiers(self, pattern, matching, nonmatching):
+ import re as _re
+
+ from auto_round.utils.common import to_standard_regex
+
+ regex = to_standard_regex(pattern)
+ assert _re.search(regex, matching)
+ assert not _re.search(regex, nonmatching)
+
+ def test_regex_tokens_and_escapes_kept(self):
+ import re as _re
+
+ from auto_round.utils.common import to_standard_regex
+
+ name = "model.layers.3.mlp.gate"
+ for pattern in ("layers.+mlp", r"layers\.[0-9]+\.mlp", r"model\.layers\.3"):
+ assert _re.search(to_standard_regex(pattern), name), pattern
+ # Bare dots are still literal.
+ assert not _re.search(to_standard_regex("mlp.gate"), "mlpXgate")
+
# ---------------------------------------------------------------------------
# matches_any_regex