forbiddenmichael commited on
Commit
afb8f35
·
verified ·
1 Parent(s): 0d370fa

Prefetch models with the parallel Hugging Face downloader

Browse files
Files changed (1) hide show
  1. krea2-start.py +38 -0
krea2-start.py CHANGED
@@ -79,6 +79,44 @@ if not has_krea:
79
  rc = subprocess.run(cmd + f" >>{LOG} 2>&1", shell=True).returncode
80
  log(f"comfyui upgrade exit {rc}")
81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  log("starting bootstrap")
83
  upload()
84
  with open(LOG, "a") as fh:
 
79
  rc = subprocess.run(cmd + f" >>{LOG} 2>&1", shell=True).returncode
80
  log(f"comfyui upgrade exit {rc}")
81
 
82
+ # The bootstrap's own downloader uses one connection, and from RunPod machines
83
+ # Hugging Face transfers stall on it (seen at 120 MB, 2026-09-25). Prefetch the
84
+ # three files with Hugging Face's parallel downloader into the exact paths the
85
+ # bootstrap checks; it then finds them verified and skips its own download.
86
+ PREFETCH = r'''
87
+ import os, shutil, sys, time, urllib.parse
88
+ from huggingface_hub import hf_hub_download
89
+ url, dest = sys.argv[1], sys.argv[2]
90
+ parts = urllib.parse.urlparse(url).path.strip("/").split("/")
91
+ repo, rev, path = "/".join(parts[:2]), parts[3], urllib.parse.unquote("/".join(parts[4:]))
92
+ t = time.time()
93
+ local = hf_hub_download(repo_id=repo, filename=path, revision=rev, local_dir="/tmp/hfdl",
94
+ token=os.environ.get("LOG_TOKEN") or None)
95
+ os.makedirs(os.path.dirname(dest), exist_ok=True)
96
+ shutil.move(local, dest)
97
+ size = os.path.getsize(dest)
98
+ print(f"prefetched {os.path.basename(dest)} {size/1e9:.2f} GB in {time.time()-t:.0f}s ({size/1e6/max(time.time()-t,1):.0f} MB/s)")
99
+ '''
100
+ rc = subprocess.run("python3 -m pip install -q --target /opt/hfdl 'huggingface_hub>=0.32,<1.0' hf_xet "
101
+ f">>{LOG} 2>&1 || uv pip install -q --target /opt/hfdl 'huggingface_hub>=0.32,<1.0' hf_xet >>{LOG} 2>&1",
102
+ shell=True).returncode
103
+ log(f"hf downloader install exit {rc}")
104
+ open("/opt/prefetch.py", "w").write(PREFETCH)
105
+ env = dict(os.environ, PYTHONPATH="/opt/hfdl", HF_HUB_DISABLE_PROGRESS_BARS="1")
106
+ root = "/comfyui/models"
107
+ targets = [
108
+ (os.environ["KREA2_MODEL_URL"], f"{root}/unet/{os.environ['KREA2_MODEL_FILENAME']}"),
109
+ ("https://huggingface.co/Comfy-Org/Krea-2/resolve/8038ce89b91b042141541ad0fa51b985ca262c5f/text_encoders/qwen3vl_4b_fp8_scaled.safetensors",
110
+ f"{root}/clip/qwen3vl_4b_fp8_scaled.safetensors"),
111
+ ("https://huggingface.co/Comfy-Org/Krea-2/resolve/8038ce89b91b042141541ad0fa51b985ca262c5f/vae/qwen_image_vae.safetensors",
112
+ f"{root}/vae/qwen_image_vae.safetensors"),
113
+ ]
114
+ for url, dest in targets:
115
+ with open(LOG, "a") as fh:
116
+ rc = subprocess.run(["python3", "/opt/prefetch.py", url, dest], stdout=fh, stderr=subprocess.STDOUT, env=env).returncode
117
+ log(f"prefetch exit {rc} for {os.path.basename(dest)}")
118
+ upload()
119
+
120
  log("starting bootstrap")
121
  upload()
122
  with open(LOG, "a") as fh: