PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度
[Bug fix] Release offload temp-file mmap to fix cleanup issue (#2487)
Liang Lve1ab33b8926e
diff --git a/auto_round/utils/offload.py b/auto_round/utils/offload.pyindex 8aecc7d11..7a3713e3d 100644--- a/auto_round/utils/offload.py+++ b/auto_round/utils/offload.py@@ -911,6 +911,12 @@ class OffloadManager:from safetensors.torch import load_file as safe_load_filestate_dict = safe_load_file(save_path, device="cpu")+ # Replace every tensor with a private copy so the mapping is+ # released as soon as this method returns.+ for key in list(state_dict.keys()):+ tensor = state_dict[key]+ state_dict[key] = tensor.clone()+ del tensor_load_state_dict_into_module(state_dict, module)except Exception as e:logger.warning(f"OffloadManager: failed to load {name}: {e}")diff --git a/test/unit/common/schemes/test_auto_scheme_low_cpu_mem.py b/test/unit/common/schemes/test_auto_scheme_low_cpu_mem.pyindex da37892a3..716a1f978 100644--- a/test/unit/common/schemes/test_auto_scheme_low_cpu_mem.py+++ b/test/unit/common/schemes/test_auto_scheme_low_cpu_mem.py@@ -21,6 +21,7 @@ by offloading block weights to disk during gradient computation.import jsonimport shutil+import sysimport pytestimport torch@@ -120,6 +121,27 @@ class TestOffloadManager:assert len(mgr._saved) == 0assert mgr._tempdir is None # auto-cleaned+ @pytest.mark.skipif(not sys.platform.startswith("linux"), reason="/proc/self/maps is Linux-only")+ def test_reload_does_not_keep_temp_file_mapped(self, tmp_path, monkeypatch):+ """Regression test for https://github.com/intel/auto-round/issues/2467."""+ monkeypatch.setenv("AR_WORK_SPACE", str(tmp_path))+ model = torch.nn.Sequential(torch.nn.Linear(32, 32))+ mgr = OffloadManager(enabled=True, mode="offload")+ mgr.offload(model, "0")+ tempdir = mgr._tempdir+ assert tempdir is not None++ mgr.reload(model, "0")++ try:+ with open("/proc/self/maps") as f:+ maps = f.read()+ except OSError as e:+ # procfs may be absent or unreadable in containers/sandboxes+ # (e.g. hidepid, restricted seccomp), so skip instead of failing.+ pytest.skip(f"/proc/self/maps is not readable: {e}")+ assert tempdir not in maps, f"reload left the deleted offload file mmap'd: {tempdir}"+def test_reload_all_auto_cleans(self):"""Test that reload() with no names auto-cleans all temp files."""model = torch.nn.Sequential(