Upload code/train_sft.py with huggingface_hub
Browse files485 unmodified lines486487488489490491964 unmodified lines1456145714581459146014611462146314641465146664 unmodified lines1531153215331534153515361537153818 unmodified lines15571558155915601561156215631564485 unmodified linesprint(f"โ ๏ธ Error downloading base checkpoint: {e}")# ------------------------------------------------------------------------------# 7. Live Generation Helper for Training Telemetry (Multi-Domain Previews)# ------------------------------------------------------------------------------964 unmodified lines"timestamp": time.strftime("%Y-%m-%d %H:%M:%S")}if IS_MAIN:torch.save(save_payload, save_path)print(f" ๐พ Saved checkpoint -> {save_path}")if is_best:torch.save(save_payload, best_save_path)print(f" ๐ Saved BEST checkpoint -> {best_save_path}")if IS_DDP:dist.barrier()64 unmodified lines"model_args": vars(model_args),"timestamp": time.strftime("%Y-%m-%d %H:%M:%S")}torch.save(final_payload, save_path)print(f"๐พ Final master checkpoint saved to: {save_path}")if os.path.exists(best_save_path):print(f"๐ Best checkpoint (lowest val loss) kept at: {best_save_path}")18 unmodified linessoup_payload = dict(final_payload)soup_payload["model_state_dict"] = soup_sdsoup_payload["model_soup"] = Truetorch.save(soup_payload, soup_save_path)print(f"โ Saved Model Soup checkpoint -> {soup_save_path}")# Load soup into model for final evaluation and benchmarkraw_model.load_state_dict(soup_sd)485 unmodified lines486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530964 unmodified lines14951496149714981499150015011502150364 unmodified lines156815691570157115721573157418 unmodified lines1593159415951596159715981599485 unmodified linesprint(f"โ ๏ธ Error downloading base checkpoint: {e}")def safe_save_checkpoint(payload: dict, target_path: str, save_optimizer: bool = True):"""Safely saves checkpoint with atomic temp file, memory optimization, and Low-Disk Shield."""os.makedirs(os.path.dirname(target_path), exist_ok=True)out_dir = os.path.dirname(os.path.abspath(target_path))try:free_bytes = shutil.disk_usage(out_dir).freefree_gb = free_bytes / (1024 ** 3)except Exception:free_gb = 999.0p = dict(payload)# Best checkpoints and low-disk conditions never include the ~4.2GB optimizer stateif (not save_optimizer) or (free_gb < 6.5):if "optimizer_state_dict" in p:if save_optimizer and free_gb < 6.5 and IS_MAIN:print(f" โ ๏ธ Low-Disk Shield: Only {free_gb:.1f} GB disk space left. "f"Saving lightweight model checkpoint (~1.0 GB) without optimizer states to prevent crash!")p.pop("optimizer_state_dict", None)tmp_path = f"{target_path}.tmp"try:torch.save(p, tmp_path)if os.path.exists(target_path):os.remove(target_path)os.replace(tmp_path, target_path)size_mb = os.path.getsize(target_path) / (1024 * 1024)if IS_MAIN:print(f" ๐พ Saved checkpoint -> {target_path} ({size_mb:.1f} MB)")return Trueexcept Exception as e:if os.path.exists(tmp_path):try: os.remove(tmp_path)except Exception: passif IS_MAIN:print(f" โ ๏ธ Error saving checkpoint to {target_path}: {e}")return False# ------------------------------------------------------------------------------# 7. Live Generation Helper for Training Telemetry (Multi-Domain Previews)# ------------------------------------------------------------------------------964 unmodified lines"timestamp": time.strftime("%Y-%m-%d %H:%M:%S")}if IS_MAIN:safe_save_checkpoint(save_payload, save_path, save_optimizer=True)if is_best:safe_save_checkpoint(save_payload, best_save_path, save_optimizer=False)if IS_DDP:dist.barrier()64 unmodified lines"model_args": vars(model_args),"timestamp": time.strftime("%Y-%m-%d %H:%M:%S")}safe_save_checkpoint(final_payload, save_path, save_optimizer=True)if os.path.exists(best_save_path):print(f"๐ Best checkpoint (lowest val loss) kept at: {best_save_path}")18 unmodified linessoup_payload = dict(final_payload)soup_payload["model_state_dict"] = soup_sdsoup_payload["model_soup"] = Truesafe_save_checkpoint(soup_payload, soup_save_path, save_optimizer=False)# Load soup into model for final evaluation and benchmarkraw_model.load_state_dict(soup_sd)
code/train_sft.py+43-8