xuan-luo's picture
download
raw
1.12 kB
"""Megatron checkpoint retention helpers."""
from __future__ import annotations
import re
import shutil
from pathlib import Path
_ITER_DIR_RE = re.compile(r"^iter_(\d+)$")
def list_iteration_dirs(checkpoint_root: Path) -> list[tuple[int, Path]]:
if not checkpoint_root.is_dir():
return []
entries: list[tuple[int, Path]] = []
for child in checkpoint_root.iterdir():
match = _ITER_DIR_RE.match(child.name)
if match and child.is_dir():
entries.append((int(match.group(1)), child))
entries.sort(key=lambda item: item[0])
return entries
def prune_checkpoints(checkpoint_root: Path, keep_last: int = 2) -> list[int]:
"""Delete older Megatron iter_* directories, keeping the newest *keep_last*."""
if keep_last < 1:
raise ValueError("keep_last must be >= 1")
iterations = list_iteration_dirs(checkpoint_root)
if len(iterations) <= keep_last:
return []
removed: list[int] = []
for iteration, path in iterations[: len(iterations) - keep_last]:
shutil.rmtree(path)
removed.append(iteration)
return removed

Xet Storage Details

Size:
1.12 kB
·
Xet hash:
96d9d14042f99a3aad86de1489f013eb0326e57abc9ce316f8de42272bec1123

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.