add stage-artifact push/fetch resume logic (protects long GPU run against external cancellation)
Browse files- experiment.py +66 -7
experiment.py
CHANGED
|
@@ -686,21 +686,80 @@ def push_results():
|
|
| 686 |
repo_id=OUT_REPO, repo_type="dataset")
|
| 687 |
log("pushed results to %s" % OUT_REPO)
|
| 688 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 689 |
# ---------------- main ----------------
|
| 690 |
|
| 691 |
def main():
|
| 692 |
log("starting mode=%s prepare_only=%s" % (MODE, PREPARE_ONLY))
|
| 693 |
-
if
|
| 694 |
-
prepare()
|
| 695 |
else:
|
| 696 |
-
|
|
|
|
| 697 |
if PREPARE_ONLY:
|
| 698 |
log("prepare-only mode; done")
|
| 699 |
return
|
| 700 |
-
|
| 701 |
-
|
| 702 |
-
|
| 703 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 704 |
metrics()
|
| 705 |
if MODE == "full":
|
| 706 |
push_results()
|
|
|
|
| 686 |
repo_id=OUT_REPO, repo_type="dataset")
|
| 687 |
log("pushed results to %s" % OUT_REPO)
|
| 688 |
|
| 689 |
+
ART_DIR = "results/full/artifacts"
|
| 690 |
+
|
| 691 |
+
def push_artifact(name):
|
| 692 |
+
if MODE != "full":
|
| 693 |
+
return
|
| 694 |
+
token = os.environ.get("HF_TOKEN")
|
| 695 |
+
if not token:
|
| 696 |
+
log("no HF_TOKEN; skipping artifact push (%s)" % name)
|
| 697 |
+
return
|
| 698 |
+
try:
|
| 699 |
+
from huggingface_hub import HfApi
|
| 700 |
+
api = HfApi(token=token)
|
| 701 |
+
api.upload_file(path_or_fileobj=os.path.join(DATA_DIR, name),
|
| 702 |
+
path_in_repo="%s/%s" % (ART_DIR, name),
|
| 703 |
+
repo_id=OUT_REPO, repo_type="dataset")
|
| 704 |
+
log("artifact pushed: %s" % name)
|
| 705 |
+
except Exception as e:
|
| 706 |
+
log("artifact push FAILED (%s): %s" % (name, str(e)[:300]))
|
| 707 |
+
|
| 708 |
+
def fetch_artifact(name):
|
| 709 |
+
p = os.path.join(DATA_DIR, name)
|
| 710 |
+
if os.path.exists(p):
|
| 711 |
+
log("artifact exists locally: %s" % name)
|
| 712 |
+
return True
|
| 713 |
+
if MODE != "full":
|
| 714 |
+
return False
|
| 715 |
+
if not os.environ.get("HF_TOKEN"):
|
| 716 |
+
return False
|
| 717 |
+
try:
|
| 718 |
+
import shutil
|
| 719 |
+
from huggingface_hub import hf_hub_download
|
| 720 |
+
dl = hf_hub_download(repo_id=OUT_REPO,
|
| 721 |
+
filename="%s/%s" % (ART_DIR, name),
|
| 722 |
+
repo_type="dataset")
|
| 723 |
+
os.makedirs(DATA_DIR, exist_ok=True)
|
| 724 |
+
shutil.move(dl, p)
|
| 725 |
+
log("artifact fetched from hub: %s" % name)
|
| 726 |
+
return True
|
| 727 |
+
except Exception as e:
|
| 728 |
+
log("artifact not on hub (%s): %s" % (name, str(e)[:200]))
|
| 729 |
+
return False
|
| 730 |
+
|
| 731 |
# ---------------- main ----------------
|
| 732 |
|
| 733 |
def main():
|
| 734 |
log("starting mode=%s prepare_only=%s" % (MODE, PREPARE_ONLY))
|
| 735 |
+
if fetch_artifact("state.json"):
|
| 736 |
+
log("skipping prepare (state.json available)")
|
| 737 |
else:
|
| 738 |
+
prepare()
|
| 739 |
+
push_artifact("state.json")
|
| 740 |
if PREPARE_ONLY:
|
| 741 |
log("prepare-only mode; done")
|
| 742 |
return
|
| 743 |
+
if fetch_artifact("triples.json"):
|
| 744 |
+
log("skipping extract (triples.json available)")
|
| 745 |
+
else:
|
| 746 |
+
run_stage("extract")
|
| 747 |
+
push_artifact("triples.json")
|
| 748 |
+
if fetch_artifact("emb.npy"):
|
| 749 |
+
log("skipping embed (emb.npy available)")
|
| 750 |
+
else:
|
| 751 |
+
run_stage("embed")
|
| 752 |
+
push_artifact("emb.npy")
|
| 753 |
+
if fetch_artifact("retrieval.json"):
|
| 754 |
+
log("skipping retrieve (retrieval.json available)")
|
| 755 |
+
else:
|
| 756 |
+
retrieve_all()
|
| 757 |
+
push_artifact("retrieval.json")
|
| 758 |
+
if fetch_artifact("answers.json"):
|
| 759 |
+
log("skipping read (answers.json available)")
|
| 760 |
+
else:
|
| 761 |
+
run_stage("read")
|
| 762 |
+
push_artifact("answers.json")
|
| 763 |
metrics()
|
| 764 |
if MODE == "full":
|
| 765 |
push_results()
|