GeezerVison is showing github.com — fix: let a reused checkpoint directory replace earlier steps · cslht11/diffct@4e0c0a9
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: diffct/_solver_state.py +7-2Lines changed: 7 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -288,10 +288,15 @@ def save_checkpoint(operator, directory, states, metadata, iteration, history, m | 288 | 288 | step = Path(directory).resolve() / f"step-{iteration:06d}" | 289 | 289 | step.mkdir(parents=True, exist_ok=True) | 290 | 290 | rank = step / f"rank-{getattr(operator, 'rank', 0):05d}" | 291 | | - rank.mkdir() | | 291 | + rank.mkdir(exist_ok=True) | 292 | 292 | for name, source in states.items(): | 293 | 293 | source = TensorStore(source.detach()) if isinstance(source, torch.Tensor) else source | 294 | | - target = NpyStore.create(rank / f"{name}.npy", source.shape, dtype=source.dtype) | | 294 | + path = rank / f"{name}.npy" | | 295 | + if path.exists(): | | 296 | + # Re-saving replaces only this rank's previous file; other | | 297 | + # ranks may be writing the same step concurrently. | | 298 | + path.unlink() | | 299 | + target = NpyStore.create(path, source.shape, dtype=source.dtype) | 295 | 300 | copy_blocks(source, target, elements) | 296 | 301 | target.flush() | 297 | 302 | manifest[name] = dict(shape=list(source.shape), dtype=str(source.dtype), fingerprint=fingerprint(target, elements)) | |
Collapse file Copy file name to clipboardExpand all lines: tests/test_solver_checkpoint_edges.py +14Lines changed: 14 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -285,3 +285,17 @@ def test_four_rank_atomic_metadata_failure_rejects_collectively_before_more_oper | 285 | 285 | assert len(record["calls"]) == record["calls_at_publication"], "rank executed another operator after publication failure" | 286 | 286 | assert record["complete_state_observations"] >= 2, "missing all-rank write/flush evidence before step2 publication" | 287 | 287 | assert record["elapsed"] < 15, "publication failure waited for the Gloo timeout" | | 288 | + | | 289 | + | | 290 | +def test_reusing_a_checkpoint_directory_replaces_steps(tmp_path): | | 291 | + from diffct import Projector, cgls | | 292 | + from diffct.geometry import circular_trajectory_2d_parallel | | 293 | + | | 294 | + trajectory = circular_trajectory_2d_parallel(8, device="cpu", dtype=torch.float32) | | 295 | + projector = Projector(trajectory, volume_shape=(16, 16), detector_shape=(24,), | | 296 | + beam="parallel", backend="torch") | | 297 | + sinogram = projector(torch.rand(16, 16)) | | 298 | + first = cgls(projector, sinogram, iterations=2, checkpoint_directory=str(tmp_path)) | | 299 | + second = cgls(projector, sinogram, iterations=2, checkpoint_directory=str(tmp_path)) | | 300 | + assert [p.name for p in sorted(tmp_path.iterdir())] == ["step-000001", "step-000002"] | | 301 | + torch.testing.assert_close(first.volume.float(), second.volume.float()) |
|