PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度
fix: materialize checkpoint-backed cpu buffers (#2412)
Heng Guo6c4d95cb67c6
diff --git a/auto_round/utils/disk_stream_util.py b/auto_round/utils/disk_stream_util.pyindex 793f59466..0a96312db 100644--- a/auto_round/utils/disk_stream_util.py+++ b/auto_round/utils/disk_stream_util.py@@ -964,11 +964,16 @@ def materialize_module(module: nn.Module, module_name: str, index: SafetensorsIntargets = [] # (param_name, full_checkpoint_name, declared_meta_dtype)fused_targets = [] # (param_name, sliced_value)+ buffer_names = {name for name, _ in module.named_buffers()}for name, tensor in list(module.named_parameters()) + list(module.named_buffers()):- if str(tensor.device) != "meta":- continue # already materialized (e.g. shared/tied weights)+ is_meta = str(tensor.device) == "meta"+ is_checkpoint_backed_buffer = name in buffer_names+ if not is_meta and not is_checkpoint_backed_buffer:+ continue # already materialized parameter (e.g. shared/tied weights)full_name = f"{module_name}.{name}".replace(".orig_layer.", ".")resolved_name = _resolve_checkpoint_name(index, full_name)+ if not is_meta and resolved_name is None:+ continue # runtime buffer without a checkpoint counterpartif resolved_name is None:sliced = _fused_lookup(full_name, tensor.shape)if sliced is None:diff --git a/test/unit/common/utils/test_disk_stream_util.py b/test/unit/common/utils/test_disk_stream_util.pyindex dd7dafcaf..14d8ce403 100644--- a/test/unit/common/utils/test_disk_stream_util.py+++ b/test/unit/common/utils/test_disk_stream_util.py@@ -154,6 +154,31 @@ class TestMaterializeModuleRoundTrip:materialize_module(block, "model.decoder.layers.0", index, device="cpu")assert torch.equal(block.self_attn.k_proj.weight.data, weight_before)+ def test_checkpoint_backed_cpu_buffer_is_overwritten(self):+ """Checkpoint data must replace a CPU buffer's constructor default."""++ class _CpuBufferModule(nn.Module):+ def __init__(self):+ super().__init__()+ self.layer_scalar = nn.Buffer(torch.ones(1))++ with init_empty_weights():+ module = _CpuBufferModule()+ assert module.layer_scalar.device.type == "cpu"+ assert torch.equal(module.layer_scalar, torch.ones(1))++ class _FakeIndex:+ def has_tensor(self, name):+ return name == "block.layer_scalar"++ def read_tensors(self, names, device="cpu"):+ assert names == ["block.layer_scalar"]+ return {"block.layer_scalar": torch.tensor([0.25])}++ materialize_module(module, "block", _FakeIndex(), device="cpu")++ assert torch.equal(module.layer_scalar, torch.tensor([0.25]))+class TestMaterializeModuleDtype:"""Regression coverage for the dtype-promotion bug: materialize_module used