Upload code/train.py with huggingface_hub
Browse files223 unmodified lines2242252262272282292301 unmodified line232233234235236237238239240241242243244245246247248249250251252253254255256257258223 unmodified linesdef save_checkpoint(model, optimizer, step, val_loss, filename, blocking=False):if not IS_MAIN:returnpath = f"{WORK}/{filename}"state_dict = extract_clean_state_dict(model)data = {"model": state_dict, "step": step, "val_loss": val_loss}1 unmodified lineif filename == "ckpt_latest.pt" and optimizer is not None:data["optimizer"] = optimizer.state_dict()torch.save(data, path)log(f" [{filename} saved @ step {step}, val_loss={val_loss:.4f}] locally")if blocking:log(f" ☁️ Uploading {filename} to HF (blocking, please wait)...")with_retry(api.upload_file, path_or_fileobj=path, path_in_repo=f"{CKPT_SUBFOLDER}/{filename}",repo_id=CKPT_REPO, repo_type="model", token=HF_TOKEN)log(f" ✅ Upload complete for {filename} @ step {step}")else:def _bg_upload():try:with_retry(api.upload_file, path_or_fileobj=path,path_in_repo=f"{CKPT_SUBFOLDER}/{filename}",repo_id=CKPT_REPO, repo_type="model", token=HF_TOKEN)log(f" ☁️ [Background upload complete for {filename} @ step {step}]")except Exception as e:log(f" ❌ [Background upload failed for {filename}: {e}]")t = threading.Thread(target=_bg_upload, daemon=False)t.start()_upload_threads.append(t)log(f" ☁️ Uploading {filename} in background...")def load_checkpoint(model, optimizer):ckpt_path = f"{WORK}/{CKPT_SUBFOLDER}/ckpt_latest.pt"223 unmodified lines2242252262272282291 unmodified line231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272223 unmodified linesdef save_checkpoint(model, optimizer, step, val_loss, filename, blocking=False):if not IS_MAIN:returnstate_dict = extract_clean_state_dict(model)data = {"model": state_dict, "step": step, "val_loss": val_loss}1 unmodified lineif filename == "ckpt_latest.pt" and optimizer is not None:data["optimizer"] = optimizer.state_dict()step_path = f"{WORK}/ckpt_upload_{step}.pt"latest_path = f"{WORK}/{filename}"torch.save(data, step_path)log(f" 💾 [{filename} saved locally @ step {step}, val_loss={val_loss:.4f}]")if blocking:log(f" ☁️ Uploading {filename} to HF (blocking, please wait)...")with_retry(api.upload_file, path_or_fileobj=step_path, path_in_repo=f"{CKPT_SUBFOLDER}/{filename}",repo_id=CKPT_REPO, repo_type="model", token=HF_TOKEN)if os.path.exists(latest_path):try: os.remove(latest_path)except OSError: passtry: os.rename(step_path, latest_path)except OSError: passlog(f" ✅ Upload complete for {filename} @ step {step}")else:def _bg_upload(upload_file, s):try:with_retry(api.upload_file, path_or_fileobj=upload_file,path_in_repo=f"{CKPT_SUBFOLDER}/{filename}",repo_id=CKPT_REPO, repo_type="model", token=HF_TOKEN)log(f" ☁️ [Background upload complete for {filename} @ step {s}]")if os.path.exists(latest_path):try: os.remove(latest_path)except OSError: passtry: os.rename(upload_file, latest_path)except OSError: passexcept Exception as e:log(f" ❌ [Background upload failed for {filename} @ step {s}: {e}]")try: os.remove(upload_file)except OSError: passt = threading.Thread(target=_bg_upload, args=(step_path, step), daemon=True)t.start()_upload_threads.append(t)log(f" ☁️ Training continues UNINTERRUPTED! Uploading {filename} to HF in background...")def load_checkpoint(model, optimizer):ckpt_path = f"{WORK}/{CKPT_SUBFOLDER}/ckpt_latest.pt"
code/train.py+24-10