Skip to content

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

fix google/gemma-3-4b-it (#1547)

Xin Heb5fc17242038
diff --git a/auto_round/utils/missing_tensors.py b/auto_round/utils/missing_tensors.py
index 68c8f8fa3..1466acd49 100644
--- a/auto_round/utils/missing_tensors.py
+++ b/auto_round/utils/missing_tensors.py
@@ -160,6 +160,10 @@ def copy_missing_tensors_from_source(
saved_parent_layers: set = {name.rsplit(".", 1)[0] for name in saved_tensor_names}
+ # To match model.language_model.layers.0.fc1 with language_model.layers.0.fc1
+ shortcut_parent_layers: set = {name.split(".", 1)[1] for name in saved_parent_layers if "." in name}
+ saved_parent_layers.update(shortcut_parent_layers)
+
def _first_numeric_prefix(tensor_name: str) -> str | None:
"""Return the path up to and including the first numeric segment, or None.
@@ -180,9 +184,17 @@ def copy_missing_tensors_from_source(
bp = _first_numeric_prefix(tensor_name)
if bp is not None:
saved_block_prefix.add(bp)
+ # To match model.language_model.layers.0 with language_model.layers.0
+ shortcut_block_prefix: set = {name.split(".", 1)[1] for name in saved_block_prefix if "." in name}
+ saved_block_prefix.update(shortcut_block_prefix)
def _is_truly_missing(name: str) -> bool:
- # For Qwen/Qwen3-0.6B-FP8, lm_head is tied but still in source_dir → not missing
+ # Special case: google/gemma-3-4b-it
+ # saved tensors have "model.language_model" prefix
+ if name.startswith("language_model.model."):
+ name = name.replace("language_model.model.", "model.language_model.")
+ # Special case: Qwen/Qwen3-0.6B-FP8
+ # lm_head is tied but still in source_dir → not missing
if name == "lm_head.weight":
return False
if name in saved_tensor_names:
@@ -190,11 +202,9 @@ def copy_missing_tensors_from_source(
parent = name.rsplit(".", 1)[0]
if parent in saved_parent_layers:
return False
+ # For split experts, name is changed but block name is the same.
src_block = _first_numeric_prefix(name)
if src_block is not None:
- # Source tensor belongs to a numbered block; if that exact block
- # appears in the saved output it was processed (possibly renamed
- # or split) rather than omitted.
return src_block not in saved_block_prefix
return True
diff --git a/test/test_cpu/utils/test_missing_tensors.py b/test/test_cpu/utils/test_missing_tensors.py
index 1461533db..83293af9a 100644
--- a/test/test_cpu/utils/test_missing_tensors.py
+++ b/test/test_cpu/utils/test_missing_tensors.py
@@ -599,6 +599,37 @@ class TestCopyMissingTensorsFromSource(unittest.TestCase):
self.assertEqual(weight_map["mtp.0.norm.weight"], "model_extra_tensors.safetensors")
self.assertEqual(weight_map["model.embed_tokens.weight"], "model.safetensors")
+ def test_tensor_with_known_block_prefix_not_copied_gemma(self):
+ """Source tensor whose numeric block-prefix exists in the saved output is not missing.
+
+ Scenario: source has an extra gate weight inside 'model.layers.0'; the target
+ already contains other tensors from 'model.layers.0' — the block was processed.
+ """
+ with tempfile.TemporaryDirectory() as source_dir, tempfile.TemporaryDirectory() as target_dir:
+ _save_safetensors(
+ {
+ "language_model.model.layers.0.mlp.gate_proj.weight": torch.randn(32, 64),
+ "language_model.model.norm.weight": torch.randn(64),
+ "language_model.layers.0.mlp.gate_proj.weight": torch.randn(32, 64),
+ "language_model.norm.weight": torch.randn(64),
+ },
+ os.path.join(source_dir, "model.safetensors"),
+ )
+ # Target has a different sub-layer inside the same block
+ _save_safetensors(
+ {
+ "model.language_model.layers.0.mlp.gate_proj.weight": torch.randn(32, 64),
+ "model.language_model.norm.weight": torch.randn(64),
+ },
+ os.path.join(target_dir, "model.safetensors"),
+ )
+ _write_config(target_dir)
+
+ copy_missing_tensors_from_source(source_dir, target_dir)
+
+ extra_shard = os.path.join(target_dir, "model_extra_tensors.safetensors")
+ self.assertFalse(os.path.exists(extra_shard), "No extra shard expected: block prefix is known")
+
if __name__ == "__main__":
unittest.main()