Re-enable 10k checkpoints — accumulates across T4 preemptions
Browse files- scripts/cloud/embed_modal.py +17 -2
scripts/cloud/embed_modal.py
CHANGED
|
@@ -210,8 +210,23 @@ def embed_year(year: str = "2022", model_name: str = "ViT-L-14",
|
|
| 210 |
if len(batch_imgs) >= batch_size:
|
| 211 |
flush_batch()
|
| 212 |
|
| 213 |
-
#
|
| 214 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
|
| 216 |
flush_batch()
|
| 217 |
print(f"Scan complete: {n_processed:,} files scanned, {len(all_ids):,} embedded")
|
|
|
|
| 210 |
if len(batch_imgs) >= batch_size:
|
| 211 |
flush_batch()
|
| 212 |
|
| 213 |
+
# Checkpoint every 10k to HF — accumulates progress across preemptions
|
| 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)
|
| 219 |
+
_df_cp = pd.DataFrame({"figure_id": list(all_ids), "embedding": list(_vecs_cp)})
|
| 220 |
+
_out_cp = f"/tmp/{year}_cp.parquet"
|
| 221 |
+
_df_cp.to_parquet(_out_cp, index=False)
|
| 222 |
+
HfApi(token=token).upload_file(
|
| 223 |
+
path_or_fileobj=_out_cp,
|
| 224 |
+
path_in_repo=f"embeddings/{year}_vitl14.parquet",
|
| 225 |
+
repo_id=OUT_REPO, repo_type="dataset",
|
| 226 |
+
commit_message=f"Checkpoint {len(all_ids):,} for {year}",
|
| 227 |
+
)
|
| 228 |
+
print(f" Checkpoint: {len(all_ids):,} embeddings on HF")
|
| 229 |
+
last_checkpoint_n = current_boundary
|
| 230 |
|
| 231 |
flush_batch()
|
| 232 |
print(f"Scan complete: {n_processed:,} files scanned, {len(all_ids):,} embedded")
|