"""One place that knows where things live, so the same script runs on three machines. Override with environment variables; the defaults are the college box layout. BERX_DATA raw TSV / parquet root, containing train/ and test/ BERX_WORK writable outputs BERX_EMB embedding root, one subdirectory per channel """ from __future__ import annotations import os DATA = os.environ.get("BERX_DATA", "/scratch/user4/dai/dataset_unstop/dataset") WORK = os.environ.get("BERX_WORK", "/scratch/user4/dai/v2/work") EMB = os.environ.get("BERX_EMB", "/scratch/user4/dai/model-3/work/emb") # The three embedding channels, and the HF dataset repo each one was uploaded to. # NOTE the e5_large / bge_m3 repo names: they are NOT the pairing a reader would guess from # the repo titles. This mapping is the one the user confirmed, and guessing it wrong silently # produces a 1024-d matrix of the wrong model with no shape error to catch it. CHANNELS = { "e5_small": {"repo": "Jyo-K/PopTards_Smol_Boy", "dim": 384, "dir": "e5-small", "model": "intfloat/multilingual-e5-small"}, "e5_large": {"repo": "Jyo-K/PopTard_Qwen_Machine", "dim": 1024, "dir": "e5-large", "model": "intfloat/multilingual-e5-large"}, "bge_m3": {"repo": "Jyo-K/PopTards_Big_Boy", "dim": 1024, "dir": "bge-m3", "model": "BAAI/bge-m3"}, } PAYLOAD_REPO = os.environ.get("BERX_PAYLOAD", "Jyo-K/PopTurk") def raw(split: str, i: int) -> str: """Raw source file, preferring parquet when the prepare step has produced it.""" base = os.path.join(DATA, split, f"{split}_source{i}") return base + ".parquet" if os.path.exists(base + ".parquet") else base + ".tsv" def ground_truth(split: str = "train") -> str: base = os.path.join(DATA, split, f"{split}_ground_truth") return base + ".parquet" if os.path.exists(base + ".parquet") else base + ".tsv" def work(*parts: str) -> str: p = os.path.join(WORK, *parts) os.makedirs(os.path.dirname(p) or ".", exist_ok=True) return p def channel_cfg(channel: str) -> dict: """Config for a channel, inventing one for the ad-hoc views 08_embed.py writes. The rescue channels (nameonly, addronly) are built on demand rather than declared, so they have no entry in CHANNELS. They are always e5-small 384-d and their directory name is the channel name itself. """ if channel in CHANNELS: return CHANNELS[channel] return {"repo": None, "dim": 384, "dir": channel, "model": "intfloat/multilingual-e5-small"} def emb_dir(channel: str, split: str, which: str) -> str: """Directory holding one matrix. `which` is 's1' or 'pool'. The on-disk layout is //_/ containing matrix.npy (the full fp16 matrix), ids.txt (one entity id per row) and meta.json. The per-shard files beside them are the build's intermediates; matrix.npy is the concatenation and is what to read. Note the directory names use hyphens ("e5-small") while the channel keys use underscores ("e5_small"). That is not worth unifying now - the embeddings took 28 minutes to build and renaming them to please a key is how row order gets disturbed. """ return os.path.join(EMB, channel_cfg(channel)["dir"], f"{split}_{which}") def emb(channel: str, split: str, which: str) -> str: """The matrix itself. Pool matrices are S2 rows then S3 rows, in source file order.""" return os.path.join(emb_dir(channel, split, which), "matrix.npy") def emb_rows(channel: str, split: str, which: str) -> str: """Map from matrix row -> row in the full prepared source. Present only when the matrix covers a SUBSET (F4's pool is address-less records only). Without applying it, candidate index i means row i of a 265k subset while everything downstream reads it as row i of the 10M pool - wrong, and silently so. """ return os.path.join(emb_dir(channel, split, which), "rows.npy") def emb_ids(channel: str, split: str, which: str) -> str: """One entity id per matrix row. This is what makes row order CHECKABLE rather than assumed - see 03_retrieve.py, which verifies it before spending GPU time.""" return os.path.join(emb_dir(channel, split, which), "ids.txt") def available_channels(split: str) -> list[str]: """Channels whose matrices are actually present locally. Only e5-small was built on the box; the two 1024-d channels live on the Hub. A run that silently skipped them would report a three-channel union it never computed, so callers check this and say which ones are missing. """ return [c for c in CHANNELS if os.path.exists(emb(c, split, "s1")) and os.path.exists(emb(c, split, "pool"))]