yang29's picture
Record CF training and evaluation implementation
f947839 verified
Raw History Blame Contribute Delete
2.84 kB
"""Require an actual completed LoRA optimizer step before the long run."""
import argparse
import json
from pathlib import Path
import torch
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("run_dir", type=Path)
args = parser.parse_args()
checkpoints = list(args.run_dir.glob("checkpoints_*/checkpoints/checkpoint-1"))
if len(checkpoints) != 1:
raise RuntimeError(f"Expected one completed checkpoint-1, found {checkpoints}")
checkpoint = checkpoints[0]
weights = torch.load(checkpoint / "lora/adapter_model.bin", map_location="cpu", weights_only=True)
if not weights or not all(torch.isfinite(value).all().item() for value in weights.values()):
raise RuntimeError("LoRA checkpoint has missing or non-finite tensors")
b_weights = [value for key, value in weights.items() if "lora_B" in key]
if not b_weights or not any(torch.count_nonzero(value).item() for value in b_weights):
raise RuntimeError("No LoRA B weights changed from their zero initialization")
optimizer = torch.load(checkpoint / "optimizer.pt", map_location="cpu", weights_only=True)
steps = [int(state["step"].item() if torch.is_tensor(state["step"]) else state["step"])
for state in optimizer["state"].values() if "step" in state]
if not steps or min(steps) != 1 or max(steps) != 1:
raise RuntimeError(f"Expected one actual optimizer update, got {set(steps)}")
manifest = json.loads((args.run_dir / "manifest.json").read_text())
if manifest["generator_family"] != "causal_forcing" or manifest["baseline"]["world_size"] != 2:
raise RuntimeError("Smoke checkpoint belongs to an unexpected model or GPU count")
record = {"passed": True, "checkpoint": str(checkpoint), "optimizer_steps": 1,
"generator_family": "causal_forcing", "world_size": 2,
"lora_tensor_count": len(weights), "all_lora_tensors_finite": True,
"lora_B_changed": True, "training_rows": manifest["train_prompt_count"]}
budget = json.loads((args.run_dir / "budget.json").read_text())
if budget.get("effective_global_batch") == 24:
paths = list((args.run_dir / "logs").rglob("metrics.jsonl"))
if len(paths) != 1:
raise RuntimeError("Expected one persisted optimizer metrics file")
metrics = [json.loads(line) for line in paths[0].read_text().splitlines()]
if len(metrics) != 1 or metrics[0]["step"] != 1 or metrics[0]["effective_global_batch"] != 24:
raise RuntimeError("Smoke did not perform a global-batch-24 optimizer update")
record["metrics"] = metrics[0]
(args.run_dir / "smoke_verified.json").write_text(json.dumps(record, indent=2) + "\n")
print(json.dumps(record, indent=2), flush=True)
if __name__ == "__main__":
main()