File size: 4,617 Bytes
6bca5f0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | """Strip `train_state/` from all but the newest checkpoint, so a long run fits on disk.
uv run python scripts/prune_checkpoints.py CKPT_ROOT # one shot
uv run python scripts/prune_checkpoints.py CKPT_ROOT --watch # every 5 min, until Ctrl+C
uv run python scripts/prune_checkpoints.py CKPT_ROOT --dry-run
Each openpi checkpoint holds:
params/ 12.5 GiB the weights -- this is what you ship and serve
train_state/ 37.5 GiB params + Adam mu + nu -- only needed to RESUME
assets/ tiny norm_stats.json, needed at serve time
With `save_interval=2500` and `keep_period=2500` every save lands on a keep_period multiple, so
orbax retains all 12 permanently: 12 x 50 GiB = 600 GiB. Keeping `train_state/` only for the
newest checkpoint brings that to 12 x 12.5 + 37.5 = 188 GiB, and costs nothing except that you
can only resume from the most recent checkpoint.
Safety: never touches the newest step, skips in-progress writes (`*.orbax-checkpoint-tmp-*` and
any step lacking `_CHECKPOINT_METADATA`), and only ever removes directories named `train_state`.
"""
import argparse
import pathlib
import shutil
import sys
import time
def log(*a):
print(*a, flush=True)
def du(p: pathlib.Path) -> int:
return sum(f.stat().st_size for f in p.rglob("*") if f.is_file())
def sweep(root: pathlib.Path, dry: bool, min_step: int = 0) -> tuple[int, int]:
steps = []
for d in root.iterdir():
if not d.is_dir() or not d.name.isdigit():
continue
if not (d / "_CHECKPOINT_METADATA").exists():
log(f" skip {d.name}: still being written (no _CHECKPOINT_METADATA)")
continue
steps.append(int(d.name))
if not steps:
return 0, 0
newest = max(steps)
freed, n = 0, 0
# Whole-checkpoint deletion below min_step. openpi's keep_period is uniform, so the only
# way not to spend 12.5 GiB on a checkpoint too early to be useful is to remove it after
# the fact. Never touches the newest, so resume always works.
for s in sorted(steps):
if min_step and s < min_step and s != newest:
d = root / str(s)
size = du(d)
log(f" {'would delete' if dry else 'deleting'} {s}/ entirely {size/2**30:.1f} GiB"
f" (below --min-step {min_step})")
if not dry:
shutil.rmtree(d)
freed += size
n += 1
steps = [s for s in steps if not (min_step and s < min_step and s != newest)]
for s in sorted(steps):
ts = root / str(s) / "train_state"
if s == newest:
if ts.exists():
log(f" keep {s}: newest, train_state retained for resume")
continue
if not ts.exists():
continue
size = du(ts)
log(f" {'would remove' if dry else 'removing'} {s}/train_state {size/2**30:.1f} GiB")
if not dry:
shutil.rmtree(ts)
freed += size
n += 1
return freed, n
def main() -> int:
ap = argparse.ArgumentParser()
ap.add_argument("root", help="e.g. ~/openpi/checkpoints/pi05_g1_pickplace/pickplace_full_v1")
ap.add_argument("--watch", action="store_true", help="keep sweeping every --interval seconds")
ap.add_argument("--interval", type=int, default=300)
ap.add_argument("--min-step", type=int, default=0,
help="delete checkpoints below this step entirely (params included). Use to "
"avoid spending 12.5 GiB each on checkpoints too early to be useful.")
ap.add_argument("--dry-run", action="store_true")
a = ap.parse_args()
root = pathlib.Path(a.root).expanduser()
if not root.is_dir():
if not a.watch:
log(f"not a directory: {root}")
return 1
# In --watch mode the training run may not have created it yet. Wait rather than exit,
# so this can be launched immediately after training without a race.
log(f"waiting for {root} to appear...")
while not root.is_dir():
time.sleep(5)
log(f"{root} exists, starting to watch")
while True:
total, freed_now = du(root), 0
log(f"[{time.strftime('%H:%M:%S')}] {root.name}: {total/2**30:.1f} GiB on disk")
freed, n = sweep(root, a.dry_run, a.min_step)
if n:
log(f" {'would free' if a.dry_run else 'freed'} {freed/2**30:.1f} GiB "
f"from {n} checkpoint(s)")
if not a.watch:
return 0
time.sleep(a.interval)
if __name__ == "__main__":
sys.exit(main())
|