Add resume-from-checkpoint: accumulate across preemptions
Browse files- scripts/cloud/embed_modal.py +31 -5
scripts/cloud/embed_modal.py
CHANGED
|
@@ -173,13 +173,33 @@ def embed_year(year: str = "2022", model_name: str = "ViT-L-14",
|
|
| 173 |
def fetch_image(fig: dict) -> Image.Image | None:
|
| 174 |
return None # unused — we use scan_zip_and_embed instead
|
| 175 |
|
|
|
|
|
|
|
|
|
|
| 176 |
all_ids: list[str] = []
|
| 177 |
all_vecs: list[np.ndarray] = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
batch_imgs: list[Image.Image] = []
|
| 179 |
batch_ids: list[str] = []
|
| 180 |
n_processed = 0
|
| 181 |
-
last_checkpoint_n =
|
| 182 |
-
CHECKPOINT_EVERY = 25000
|
| 183 |
|
| 184 |
def flush_batch():
|
| 185 |
if not batch_imgs:
|
|
@@ -205,14 +225,20 @@ def embed_year(year: str = "2022", model_name: str = "ViT-L-14",
|
|
| 205 |
continue
|
| 206 |
|
| 207 |
pid_norm = str(pid_raw).lstrip("D").zfill(7)
|
| 208 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
batch_imgs.append(img)
|
| 210 |
if len(batch_imgs) >= batch_size:
|
| 211 |
flush_batch()
|
| 212 |
|
| 213 |
-
# Checkpoint every 10k
|
| 214 |
current_boundary = len(all_ids) // 10000
|
| 215 |
-
if current_boundary > last_checkpoint_n:
|
| 216 |
_vecs_cp = np.vstack(all_vecs).astype(np.float32)
|
| 217 |
_norms_cp = np.linalg.norm(_vecs_cp, axis=1, keepdims=True)
|
| 218 |
_vecs_cp /= np.maximum(_norms_cp, 1e-8)
|
|
|
|
| 173 |
def fetch_image(fig: dict) -> Image.Image | None:
|
| 174 |
return None # unused — we use scan_zip_and_embed instead
|
| 175 |
|
| 176 |
+
# ── Resume from existing HF checkpoint ───────────────────────────────────
|
| 177 |
+
# Download any existing embeddings from HF so we accumulate across preemptions
|
| 178 |
+
# rather than restarting from zero each time.
|
| 179 |
all_ids: list[str] = []
|
| 180 |
all_vecs: list[np.ndarray] = []
|
| 181 |
+
already_embedded: set[str] = set()
|
| 182 |
+
|
| 183 |
+
existing_path = f"/tmp/{year}_existing.parquet"
|
| 184 |
+
try:
|
| 185 |
+
existing_hf = hf_hub_download(
|
| 186 |
+
repo_id=OUT_REPO, filename=f"embeddings/{year}_vitl14.parquet",
|
| 187 |
+
repo_type="dataset", token=token, local_dir="/tmp/impact",
|
| 188 |
+
)
|
| 189 |
+
existing_df = pd.read_parquet(existing_hf)
|
| 190 |
+
already_embedded = set(existing_df["figure_id"].tolist())
|
| 191 |
+
# Seed all_ids / all_vecs with existing embeddings
|
| 192 |
+
all_ids = existing_df["figure_id"].tolist()
|
| 193 |
+
all_vecs = [existing_df["embedding"].to_numpy()]
|
| 194 |
+
print(f"Resuming from {len(already_embedded):,} existing embeddings on HF")
|
| 195 |
+
except Exception:
|
| 196 |
+
print("No existing checkpoint — starting fresh")
|
| 197 |
+
|
| 198 |
batch_imgs: list[Image.Image] = []
|
| 199 |
batch_ids: list[str] = []
|
| 200 |
n_processed = 0
|
| 201 |
+
last_checkpoint_n = len(all_ids) // 10000 # start above existing checkpoints
|
| 202 |
+
CHECKPOINT_EVERY = 25000 # unused field — kept for reference
|
| 203 |
|
| 204 |
def flush_batch():
|
| 205 |
if not batch_imgs:
|
|
|
|
| 225 |
continue
|
| 226 |
|
| 227 |
pid_norm = str(pid_raw).lstrip("D").zfill(7)
|
| 228 |
+
fig_id = f"D{pid_norm}_{fig_num}"
|
| 229 |
+
|
| 230 |
+
# Skip already-embedded figures
|
| 231 |
+
if fig_id in already_embedded:
|
| 232 |
+
continue
|
| 233 |
+
|
| 234 |
+
batch_ids.append(fig_id)
|
| 235 |
batch_imgs.append(img)
|
| 236 |
if len(batch_imgs) >= batch_size:
|
| 237 |
flush_batch()
|
| 238 |
|
| 239 |
+
# Checkpoint every 10k NEW embeddings to HF
|
| 240 |
current_boundary = len(all_ids) // 10000
|
| 241 |
+
if current_boundary > last_checkpoint_n and len(all_ids) > len(already_embedded):
|
| 242 |
_vecs_cp = np.vstack(all_vecs).astype(np.float32)
|
| 243 |
_norms_cp = np.linalg.norm(_vecs_cp, axis=1, keepdims=True)
|
| 244 |
_vecs_cp /= np.maximum(_norms_cp, 1e-8)
|