Skip to content

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

Fix invalid group reference in revert_checkpoint_conversion_mapping (#2035)

A Sivasubramanian Manoj6d1c6db6d006
diff --git a/auto_round/utils/common.py b/auto_round/utils/common.py
index 236d56c05..4af0291a2 100644
--- a/auto_round/utils/common.py
+++ b/auto_round/utils/common.py
@@ -1191,7 +1191,10 @@ def revert_checkpoint_conversion_mapping(name: str, key_mapping: dict[str, str])
target_patterns = [target_patterns]
for target_pattern in target_patterns:
source_pattern = source_pattern.lstrip("^") # strip off un-needed chars and patterns
- source_pattern = re.sub(r"\(.*\)", "", source_pattern)
+ # Skip stripping the capture group if the target backreferences it,
+ # otherwise re.subn raises "invalid group reference".
+ if not re.search(r"\\g?<?\d+>?", target_pattern):
+ source_pattern = re.sub(r"\(.*\)", "", source_pattern)
# Weight-conversion reverse mappings may expose bare tensor names
# such as "weight" -> ".weight_packed". Treat those as terminal
diff --git a/test/test_cpu/utils/test_utils.py b/test/test_cpu/utils/test_utils.py
index 587fe57d7..463f8cbb4 100644
--- a/test/test_cpu/utils/test_utils.py
+++ b/test/test_cpu/utils/test_utils.py
@@ -56,6 +56,14 @@ def test_revert_checkpoint_conversion_mapping_does_not_rewrite_quantized_tensor_
)
+def test_revert_checkpoint_conversion_mapping_handles_backreference_patterns():
+ mapping = {r"^model\.language_model\.(.+)$": r"model.\1"}
+
+ converted = revert_checkpoint_conversion_mapping("model.language_model.layers.0.self_attn.q_proj.weight", mapping)
+
+ assert converted == "model.layers.0.self_attn.q_proj.weight"
+
+
def test_preserve_original_visual_block_name():
# Single visual block name
assert preserve_original_visual_block_name("model.visual.blocks", "visual.blocks") == "model.visual.blocks"