File size: 18,762 Bytes
ff80158 | 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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 | #!/usr/bin/env python3
"""Sync paladin model checkpoints to HuggingFace.
Idempotent script for exporting, uploading, and registering paladin models.
Works for any project name (paladin-redo, or future variants).
Phases:
gen-configs -- Generate Hydra pkl configs for SLURM export jobs
upload -- Upload exported pkl files to HF repo (idempotent)
update-map -- Update paladin_model_map.csv locally and push to HF
Usage:
python scripts/sync_paladin_models.py --project paladin-redo --phase gen-configs
python scripts/sync_paladin_models.py --project paladin-redo --phase upload
python scripts/sync_paladin_models.py --project paladin-redo --phase update-map
# Or run all phases sequentially (gen-configs + upload + update-map),
# except gen-configs requires SLURM to complete first:
python scripts/sync_paladin_models.py --project paladin-redo
"""
import argparse
import glob
import os
import re
import sys
from pathlib import Path
import pandas as pd
# ---------------------------------------------------------------------------
# Paths
# ---------------------------------------------------------------------------
MOSAIC_ROOT = Path(__file__).resolve().parent.parent
DEFAULT_AUROC_CSV = MOSAIC_ROOT / "aggregated_auroc.freeze.corrected.csv"
MODEL_MAP_CSV = MOSAIC_ROOT / "data" / "paladin_model_map.csv"
HF_CRED_ENV = Path.home() / ".hf_cred.env"
# Paladin worktree (dev branch) — used for SLURM jobs and Hydra configs
# The main repo (/repos/paladin) is on feature/webdataset-integration which has
# an incompatible cancer_type API change. The dev worktree uses the stable API.
PALADIN_REPO = Path("/gpfs/mskmind_ess/limr/repos/paladin-dev")
PALADIN_CONF_DIR = PALADIN_REPO / "conf" / "train"
PALADIN_VENV = PALADIN_REPO / "venv"
# Boehmk's paladin storage (read-only for us — checkpoints live here)
PALADIN_STORAGE_BASE = Path("/gpfs/mskmind_ess/boehmk/paladin/storage")
# Our writable output dir for exported pkl files (boehmk's storage is read-only)
PKL_OUTPUT_BASE = Path("/gpfs/mskmind_ess/limr/paladin-redo-pkl")
HF_REPO_ID = "PDM-Group/paladin-aeon-models"
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def load_hf_token() -> str:
"""Load HF_TOKEN from ~/.hf_cred.env or environment."""
if "HF_TOKEN" in os.environ:
return os.environ["HF_TOKEN"]
if HF_CRED_ENV.exists():
for line in HF_CRED_ENV.read_text().splitlines():
line = line.strip()
if line.startswith("export HF_TOKEN="):
return line.split("=", 1)[1].strip()
raise RuntimeError(
"HF_TOKEN not found. Set it in the environment or in ~/.hf_cred.env"
)
def get_best_run_per_combo(project: str, csv_path: Path) -> dict[tuple[str, str], str]:
"""Return {(histology, target_name): run_id} for best-auroc run per combo."""
df = pd.read_csv(csv_path)
proj = df[df["project"] == project]
if proj.empty:
raise ValueError(f"No rows found for project={project!r} in {csv_path}")
best = proj.sort_values("auroc", ascending=False).drop_duplicates(
subset=["histology", "target_name"]
)
return dict(zip(zip(best["histology"], best["target_name"]), best["run_id"]))
def find_ckpt_zip(storage_dir: Path, run_id: str) -> Path:
"""Find the (single) .ckpt.zip file for a run_id."""
pattern = str(storage_dir / run_id / "checkpoints" / "*.ckpt.zip")
matches = glob.glob(pattern)
if not matches:
raise FileNotFoundError(f"No .ckpt.zip found for {run_id} in {storage_dir}")
if len(matches) > 1:
raise RuntimeError(f"Multiple .ckpt.zip files found for {run_id}: {matches}")
return Path(matches[0])
def parse_epoch_step(filename: str) -> str:
"""Parse 'epoch=E-step=S' from a checkpoint filename."""
m = re.search(r"(epoch=\d+-step=\d+)", filename)
if not m:
raise ValueError(f"Cannot parse epoch/step from filename: {filename}")
return m.group(1)
def find_pkl(storage_dir: Path, run_id: str) -> Path | None:
"""Return the pkl file path if it exists, else None.
Checks both the dedicated PKL_OUTPUT_BASE (our writable area) and
the original boehmk storage dir (in case pkls were already generated there).
"""
# Primary: our output area
out_dir = PKL_OUTPUT_BASE / run_id / "checkpoints"
pattern = str(out_dir / "*.model.pkl")
matches = glob.glob(pattern)
if matches:
return Path(matches[0])
# Fallback: boehmk's storage (read-only, but may have pre-existing pkls)
pattern = str(storage_dir / run_id / "checkpoints" / "*.model.pkl")
matches = glob.glob(pattern)
return Path(matches[0]) if matches else None
# ---------------------------------------------------------------------------
# Phase 1: gen-configs
# ---------------------------------------------------------------------------
PKL_YAML_TEMPLATE = """\
callbacks:
- _target_: lightning.pytorch.callbacks.EarlyStopping
mode: ${{train.monitor.mode}}
monitor: ${{train.monitor.metric}}
patience: 5
verbose: false
- _target_: lightning.pytorch.callbacks.ModelCheckpoint
mode: ${{train.monitor.mode}}
monitor: ${{train.monitor.metric}}
save_top_k: 5
verbose: false
- _target_: lightning.pytorch.callbacks.LearningRateMonitor
log_momentum: false
logging_interval: step
- _target_: lightning.pytorch.callbacks.progress.tqdm_progress.TQDMProgressBar
refresh_rate: 5
- _target_: paladin.pl_modules.callbacks.ExportCallback
- _target_: paladin.pl_modules.callbacks.ConvertToONNX
export_path: {export_path}
command: test
deterministic: false
logging:
logger:
_target_: lightning.pytorch.loggers.WandbLogger
entity: null
log_model: ${{..upload.run_files}}
mode: disabled
name: pkl_export
project: paladin
tags: ${{core.tags}}
upload:
run_files: false
source: false
wandb_watch:
log: all
log_freq: 5
monitor:
metric: loss/val_epoch
mode: min
restore:
ckpt_or_run_path: {ckpt_path}
mode: continue
seed_index: 0
trainer:
accelerator: gpu
deterministic: ${{train.deterministic}}
devices: 1
fast_dev_run: false
max_epochs: 500
max_steps: 1000000
num_sanity_val_steps: 0
precision: 16
strategy: ddp
val_check_interval: 1.0
"""
SBATCH_TEMPLATE = """\
#!/bin/bash
# Submit SLURM pickle-export jobs for {project} models.
# Generated by scripts/sync_paladin_models.py --phase gen-configs
# Run this from {paladin_repo}
PALADIN_REPO="{paladin_repo}"
PALADIN_CONDA_ENV="{paladin_venv}"
cd "$PALADIN_REPO"
RUN_IDS=(
{run_id_list}
)
echo "Submitting ${{#RUN_IDS[@]}} pickle-export jobs..."
for run_id in "${{RUN_IDS[@]}}"; do
config_name="pkl_${{run_id}}"
config_file="{conf_dir}/pkl_${{run_id}}.yaml"
if [ ! -f "$config_file" ]; then
echo "WARNING: config not found, skipping: $config_file"
continue
fi
sbatch \\
--nodes=1 \\
--ntasks=1 \\
--cpus-per-task=40 \\
--mem=100G \\
--time=1:00:00 \\
--output="{paladin_repo}/logs/pickle_%A_%a.out" \\
--error="{paladin_repo}/logs/pickle_%A_%a.err" \\
--qos=premium \\
--job-name="pkl_${{run_id}}" \\
--gres=gpu:1 \\
--wrap="source ~/.bashrc && conda activate {paladin_venv} && cd {paladin_repo} && WANDB_MODE=disabled python src/paladin/run.py train=${{config_name}} nn=joint nn/module=jointbb"
sleep 0.5
done
echo "Done submitting!"
"""
def phase_gen_configs(project: str, csv_path: Path) -> None:
"""Generate Hydra pkl configs and sbatch submit script."""
storage_dir = PALADIN_STORAGE_BASE / project
if not storage_dir.exists():
raise FileNotFoundError(f"Storage directory not found: {storage_dir}")
combos = get_best_run_per_combo(project, csv_path)
print(
f"Found {len(combos)} (histology, target_name) combos for project={project!r}"
)
PALADIN_CONF_DIR.mkdir(parents=True, exist_ok=True)
logs_dir = PALADIN_REPO / "logs"
logs_dir.mkdir(exist_ok=True)
skipped, written, errors = [], [], []
for (histology, target), run_id in combos.items():
pkl_path = find_pkl(storage_dir, run_id)
if pkl_path:
skipped.append(run_id)
continue
try:
ckpt_zip = find_ckpt_zip(storage_dir, run_id)
except FileNotFoundError as e:
errors.append((run_id, str(e)))
continue
epoch_step = parse_epoch_step(ckpt_zip.name)
# Write pkl to our own writable area (boehmk's storage is read-only)
out_ckpt_dir = PKL_OUTPUT_BASE / run_id / "checkpoints"
out_ckpt_dir.mkdir(parents=True, exist_ok=True)
export_path = str(out_ckpt_dir / f"{run_id}.{epoch_step}.model.onnx")
yaml_content = PKL_YAML_TEMPLATE.format(
export_path=export_path,
ckpt_path=str(ckpt_zip),
)
config_file = PALADIN_CONF_DIR / f"pkl_{run_id}.yaml"
config_file.write_text(yaml_content)
written.append(run_id)
print(f" Written: {len(written)} configs")
print(f" Skipped (pkl exists): {len(skipped)}")
if errors:
print(f" ERRORS ({len(errors)}):")
for run_id, msg in errors:
print(f" {run_id}: {msg}")
# Write the sbatch submit script
needs_export = [
run_id for run_id in combos.values() if not find_pkl(storage_dir, run_id)
]
run_id_list = "\n".join(f" {r}" for r in needs_export)
sbatch_content = SBATCH_TEMPLATE.format(
project=project,
paladin_repo=str(PALADIN_REPO),
paladin_venv=str(PALADIN_VENV),
conf_dir=str(PALADIN_CONF_DIR),
run_id_list=run_id_list,
)
submit_script = (
MOSAIC_ROOT / "scripts" / f"submit_{project.replace('-', '_')}_pickles.sh"
)
submit_script.write_text(sbatch_content)
submit_script.chmod(0o755)
print(f"\nSubmit script written to: {submit_script}")
print(f"Run it from: {PALADIN_REPO}")
print(" cd /gpfs/mskmind_ess/limr/repos/paladin")
print(f" bash {submit_script}")
# ---------------------------------------------------------------------------
# Phase 2: upload
# ---------------------------------------------------------------------------
def phase_upload(project: str, csv_path: Path) -> None:
"""Upload pkl files from storage to HuggingFace (idempotent).
Batches all files into a single commit for efficiency.
"""
from huggingface_hub import HfApi, CommitOperationAdd
token = load_hf_token()
api = HfApi(token=token)
storage_dir = PALADIN_STORAGE_BASE / project
hf_folder = project.replace("-", "_") # paladin-redo -> paladin_redo
# Get existing files in HF repo for this project folder
print(f"Listing existing files in HF repo ({hf_folder}/)...")
try:
repo_files = set(api.list_repo_files(HF_REPO_ID, repo_type="model"))
except Exception as e:
raise RuntimeError(f"Failed to list HF repo files: {e}") from e
existing_hf = {f for f in repo_files if f.startswith(f"{hf_folder}/")}
print(f" Already in HF ({hf_folder}/): {len(existing_hf)}")
# Find all pkl files — check our writable output area first, then boehmk's storage
pkl_files = glob.glob(str(PKL_OUTPUT_BASE / "*" / "checkpoints" / "*.model.pkl"))
pkl_files += glob.glob(str(storage_dir / "*" / "checkpoints" / "*.model.pkl"))
print(f" Found {len(pkl_files)} pkl files (output area + boehmk storage)")
combos = get_best_run_per_combo(project, csv_path)
expected_run_ids = set(combos.values())
# Deduplicate by hf_path (output area takes priority over boehmk storage)
seen_hf_paths: set[str] = set()
to_upload: list[tuple[str, Path]] = [] # (hf_path, local_path)
for pkl_path_str in pkl_files:
pkl_path = Path(pkl_path_str)
run_id = (
pkl_path.parent.parent.name
) # .../paladin-redo/{run_id}/checkpoints/...
if run_id not in expected_run_ids:
continue # not a best-run pkl, skip
hf_path = f"{hf_folder}/{pkl_path.name}"
if hf_path in seen_hf_paths:
continue # dedup
seen_hf_paths.add(hf_path)
if hf_path in existing_hf:
continue # already uploaded
to_upload.append((hf_path, pkl_path))
skipped = len(seen_hf_paths) - len(to_upload)
print(f" To upload: {len(to_upload)} | Already in HF (skipped): {skipped}")
# Check for missing pkls (SLURM not done yet)
found_run_ids = {
Path(p).parent.parent.name
for p in pkl_files
if Path(p).parent.parent.name in expected_run_ids
}
missing = expected_run_ids - found_run_ids
if missing:
print(f" WARNING: Missing pkl files (SLURM not done?): {len(missing)}")
for r in sorted(missing)[:10]:
print(f" {r}")
if len(missing) > 10:
print(f" ... and {len(missing) - 10} more")
sys.exit(1)
if not to_upload:
print("Nothing to upload.")
return
# Batch into commits of up to BATCH_SIZE files to avoid oversized commits
BATCH_SIZE = 50
total_uploaded = 0
for batch_start in range(0, len(to_upload), BATCH_SIZE):
batch = to_upload[batch_start : batch_start + BATCH_SIZE]
batch_end = batch_start + len(batch)
print(
f" Committing files {batch_start + 1}–{batch_end} of {len(to_upload)} ...",
flush=True,
)
operations = [
CommitOperationAdd(path_in_repo=hf_path, path_or_fileobj=str(local_path))
for hf_path, local_path in batch
]
api.create_commit(
repo_id=HF_REPO_ID,
repo_type="model",
operations=operations,
commit_message=f"Add {project} pkl files ({batch_start + 1}–{batch_end})",
)
total_uploaded += len(batch)
print(f" ok ({total_uploaded} uploaded so far)")
print(f"\nUpload summary:")
print(f" Uploaded: {total_uploaded}")
print(f" Already in HF (skipped): {skipped}")
# ---------------------------------------------------------------------------
# Phase 3: update-map
# ---------------------------------------------------------------------------
def phase_update_map(project: str, csv_path: Path) -> None:
"""Update paladin_model_map.csv and push to HuggingFace."""
from huggingface_hub import HfApi
token = load_hf_token()
api = HfApi(token=token)
storage_dir = PALADIN_STORAGE_BASE / project
hf_folder = project.replace("-", "_") # paladin-redo -> paladin_redo
combos = get_best_run_per_combo(project, csv_path)
print(f"Processing {len(combos)} combos for project={project!r}")
# Build (histology, target_name) -> model_path mapping
new_entries: list[dict] = []
missing: list[str] = []
for (histology, target_name), run_id in combos.items():
pkl = find_pkl(storage_dir, run_id)
if pkl is None:
missing.append(run_id)
continue
model_path = f"data/{hf_folder}/{pkl.name}"
new_entries.append(
{
"cancer_subtype": histology,
"target_name": target_name,
"model_path": model_path,
}
)
if missing:
print(
f"ERROR: {len(missing)} run_ids have no pkl file yet (run upload phase first?):"
)
for r in sorted(missing)[:10]:
print(f" {r}")
if len(missing) > 10:
print(f" ... and {len(missing) - 10} more")
sys.exit(1)
# Load existing model map
model_map = pd.read_csv(MODEL_MAP_CSV)
existing_keys = set(zip(model_map["cancer_subtype"], model_map["target_name"]))
replaced, added = 0, 0
for entry in new_entries:
key = (entry["cancer_subtype"], entry["target_name"])
if key in existing_keys:
idx = model_map[
(model_map["cancer_subtype"] == entry["cancer_subtype"])
& (model_map["target_name"] == entry["target_name"])
].index
model_map.loc[idx, "model_path"] = entry["model_path"]
replaced += 1
else:
new_row = pd.DataFrame([entry])
model_map = pd.concat([model_map, new_row], ignore_index=True)
added += 1
model_map.to_csv(MODEL_MAP_CSV, index=False)
print(f"\npaladin_model_map.csv updated:")
print(f" Replaced: {replaced}")
print(f" Added: {added}")
print(f" Total entries: {len(model_map)}")
print(f" Saved to: {MODEL_MAP_CSV}")
# Push updated CSV to HF
print(f"\nUploading paladin_model_map.csv to HF...")
api.upload_file(
path_or_fileobj=str(MODEL_MAP_CSV),
path_in_repo="paladin_model_map.csv",
repo_id=HF_REPO_ID,
repo_type="model",
commit_message=f"Update paladin_model_map.csv for {project}",
)
print("Done!")
print(f"\nNext step: commit and push the updated model map:")
print(f" git add {MODEL_MAP_CSV.relative_to(MOSAIC_ROOT)}")
print(f" git commit -m 'feat: add {project} models to paladin_model_map.csv'")
print(f" git push")
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
def main() -> None:
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument(
"--project",
required=True,
help="Project name in aggregated_auroc CSV (e.g. paladin-redo)",
)
parser.add_argument(
"--phase",
choices=["gen-configs", "upload", "update-map"],
default=None,
help="Phase to run. If omitted, runs upload + update-map (gen-configs requires SLURM).",
)
parser.add_argument(
"--csv",
default=str(DEFAULT_AUROC_CSV),
help=f"Path to aggregated AUROC CSV (default: {DEFAULT_AUROC_CSV})",
)
args = parser.parse_args()
csv_path = Path(args.csv)
if args.phase == "gen-configs":
phase_gen_configs(args.project, csv_path)
elif args.phase == "upload":
phase_upload(args.project, csv_path)
elif args.phase == "update-map":
phase_update_map(args.project, csv_path)
else:
# Run upload + update-map (gen-configs requires separate SLURM step)
print("Running upload + update-map phases (skipping gen-configs)...")
phase_upload(args.project, csv_path)
phase_update_map(args.project, csv_path)
if __name__ == "__main__":
main()
|