Download code/scripts/07_fetch_embeddings.py from Jyo-K/PopTurk: direct link, hf CLI and curl.
- Browser
- Download file 4.25 kB
-
https://huggingface.co/datasets/Jyo-K/PopTurk/resolve/main/code/scripts/07_fetch_embeddings.py
- Command line
-
hf download hf://datasets/Jyo-K/PopTurk/code/scripts/07_fetch_embeddings.py
-
curl -L -o 07_fetch_embeddings.py https://huggingface.co/datasets/Jyo-K/PopTurk/resolve/main/code/scripts/07_fetch_embeddings.py
4.25 kB
| """Pull a channel's embedding matrices from its HF repo onto the box. | |
| Only e5-small was built locally. e5-large and bge-m3 exist on the Hub, and step C wants all | |
| three - the union of three top-100 lists is what moves the oracle from 0.9953 to 0.9976, and | |
| a "three-channel union" computed from one channel is just that channel. | |
| export HF_TOKEN=hf_... | |
| python scripts/07_fetch_embeddings.py --channel e5_large --split test | |
| SIZE. A 1024-d test pair is about 20 GB pool + 3.5 GB s1. Check free space first; the script | |
| refuses rather than filling /scratch and taking the box down for the other 25 users on it. | |
| The layout written here matches what paths.emb expects, so nothing else needs to change. The | |
| download is resumable - hf_hub keeps its own cache and a re-run skips what is already there. | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import os | |
| import shutil | |
| import sys | |
| sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
| from berx import io_hf # noqa: E402 | |
| from berx import paths as P # noqa: E402 | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--channel", required=True, choices=sorted(P.CHANNELS)) | |
| ap.add_argument("--split", default="test") | |
| ap.add_argument("--which", nargs="+", default=["s1", "pool"], choices=["s1", "pool"]) | |
| ap.add_argument("--min-free-gb", type=float, default=40.0) | |
| ap.add_argument("--list", action="store_true", help="only show what the repo contains") | |
| a = ap.parse_args() | |
| repo = P.CHANNELS[a.channel]["repo"] | |
| if a.list: | |
| from huggingface_hub import HfApi | |
| files = HfApi(token=io_hf.token()).list_repo_files(repo, repo_type="dataset") | |
| for f in sorted(files): | |
| print(" ", f) | |
| print(f"\n{len(files)} files in {repo}") | |
| return | |
| free = shutil.disk_usage(P.EMB).free / 2 ** 30 | |
| if free < a.min_free_gb: | |
| sys.exit(f"Only {free:.0f} GiB free under {P.EMB}; need about {a.min_free_gb:.0f}. " | |
| f"Refusing - filling /scratch takes the box down for everyone on it.") | |
| print(f"{free:.0f} GiB free under {P.EMB}") | |
| for which in a.which: | |
| dest = P.emb_dir(a.channel, a.split, which) | |
| if os.path.exists(os.path.join(dest, "matrix.npy")): | |
| print(f" {which}: already present at {dest}, skipping") | |
| continue | |
| os.makedirs(dest, exist_ok=True) | |
| # The repo layout is not guaranteed to match ours, so fetch by pattern and report | |
| # what arrived rather than assuming a path. | |
| pats = [f"**/{a.split}_{which}/matrix.npy", f"**/{a.split}_{which}/ids.txt", | |
| f"**/{a.split}_{which}/meta.json", | |
| f"{a.split}_{which}/*"] | |
| print(f" {which}: downloading from {repo} ...") | |
| got = io_hf.fetch_dir(repo, allow=pats, local_dir=dest) | |
| found = [] | |
| for root, _, files in os.walk(got): | |
| for f in files: | |
| if f in ("matrix.npy", "ids.txt", "meta.json"): | |
| src = os.path.join(root, f) | |
| dst = os.path.join(dest, f) | |
| if os.path.abspath(src) != os.path.abspath(dst): | |
| shutil.move(src, dst) | |
| found.append(f) | |
| if "matrix.npy" not in found: | |
| print(f" nothing matched. Run with --list to see the repo layout and adjust " | |
| f"the patterns; do NOT guess.") | |
| continue | |
| import numpy as np | |
| m = np.load(os.path.join(dest, "matrix.npy"), mmap_mode="r") | |
| want = P.CHANNELS[a.channel]["dim"] | |
| print(f" {m.shape} {m.dtype} expected dim {want}") | |
| assert m.shape[1] == want, ( | |
| f"{a.channel} should be {want}-d but this matrix is {m.shape[1]}-d. The two " | |
| f"1024-d channels are interchangeable by shape and not by content - check you " | |
| f"pulled the right repo.") | |
| n = np.linalg.norm(np.asarray(m[:256], dtype=np.float32), axis=1).mean() | |
| print(f" mean L2 norm {n:.5f} " + | |
| ("(normalised, good)" if abs(n - 1) < 0.02 else "(NOT normalised - fix before use)")) | |
| print(f"\nNow: python scripts/03_retrieve.py --split {a.split} --channel {a.channel} ...") | |
| if __name__ == "__main__": | |
| main() | |