File size: 15,214 Bytes
d309c96
 
25c9427
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d309c96
 
25c9427
 
 
 
 
 
 
d309c96
 
 
 
 
 
 
 
25c9427
d309c96
 
 
 
 
25c9427
 
 
 
0a78817
25c9427
 
 
 
5ccbf36
 
 
 
 
d309c96
 
25c9427
d309c96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25c9427
 
 
d309c96
25c9427
 
 
d309c96
 
 
25c9427
d309c96
 
 
25c9427
d309c96
 
 
 
 
25c9427
d309c96
 
 
 
 
 
 
 
 
 
 
 
 
25c9427
 
d309c96
 
25c9427
 
 
 
 
 
 
 
d309c96
 
25c9427
 
 
d309c96
25c9427
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d309c96
25c9427
 
d309c96
 
25c9427
 
 
 
 
 
 
 
 
 
 
 
 
 
d309c96
 
 
 
 
 
 
 
25c9427
d309c96
 
25c9427
 
 
 
d309c96
25c9427
d309c96
 
 
 
 
 
 
 
 
 
25c9427
d309c96
 
 
 
 
 
5ccbf36
 
 
 
d309c96
 
 
 
 
 
 
 
25c9427
d309c96
 
 
 
25c9427
 
d309c96
25c9427
d309c96
 
25c9427
d309c96
 
 
 
 
 
 
25c9427
5ccbf36
d309c96
 
 
 
 
 
 
 
 
 
25c9427
 
 
 
 
 
 
 
d309c96
25c9427
d309c96
 
 
 
 
 
 
25c9427
d309c96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25c9427
d309c96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25c9427
d309c96
 
25c9427
 
 
d309c96
25c9427
 
 
 
0a78817
25c9427
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d309c96
 
 
 
 
25c9427
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d309c96
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
"""Compute exact global k-NN ground truth for an embedding collection.

Two modes share the same per-stripe partition + per-shard output pipeline:

  --mode dense  (default)
    Cosine top-k over a `(N, dim)` FP16 corpus. Used for the
    article-level dense models (qwen3-embedding-0.6b,
    snowflake-arctic-embed-l-v2.0, nomic-embed-text-v1.5, ...).

  --mode maxsim
    ColBERT-style late-interaction MaxSim top-k over a
    `(T_total, dim)` token bank + `(N+1,)` section offsets. Used for
    the section-level multi-vector models (gte-moderncolbert-v1).

Both modes write per-shard `{wiki}/{stem}.{suffix}.ground_truth.{ibin,fbin}`
files in canonical shard-walk order. The neighbor IDs in the `.ibin` are
global row IDs (article-id space for dense, section-id space for MaxSim).

Usage:
  python ground_truth.py --mode dense \
      --output /path/to/embeddings --model-subdir qwen3-embedding-0.6b \
      --num-gpus 8

  python ground_truth.py --mode maxsim \
      --output /path/to/embeddings --model-subdir gte-moderncolbert-v1 \
      --num-gpus 8
"""

from __future__ import annotations

import argparse
import multiprocessing as mp
import os
import struct
import sys
import time
from pathlib import Path

import numpy as np

REPO_ROOT = Path(__file__).resolve().parent
sys.path.insert(0, str(REPO_ROOT))

from retrievers import (  # noqa: E402
    load_maxsim_corpus,
    gt_stripe_dense,
    gt_stripe_maxsim,
)
from usearchwiki import (  # noqa: E402
    CollectionShard,
    discover_collection,
    resolve_lfs_pointer,
    write_bin,
)


def load_dense_collection(
    model_root: Path,
    suffix: str,
    dimensions: int,
    shards: list[CollectionShard],
) -> np.ndarray:
    total_vectors = sum(shard.row_count for shard in shards)
    embeddings = np.empty((total_vectors, dimensions), dtype=np.float16)
    started = time.monotonic()
    for shard in shards:
        path = model_root / shard.wikiname / f"{shard.stem}.{suffix}.f16bin"
        blob_path = resolve_lfs_pointer(path)
        with open(blob_path, "rb") as file:
            header = file.read(8)
            rows, columns = struct.unpack("<II", header)
            if columns != dimensions:
                raise ValueError(f"{path}: dim {columns} != expected {dimensions}")
            if rows != shard.row_count:
                raise ValueError(f"{path}: rows {rows} != cached {shard.row_count}")
            destination = embeddings[shard.row_offset : shard.row_offset + rows]
            file.readinto(memoryview(destination))  # type: ignore[arg-type]
            del destination
    elapsed = time.monotonic() - started
    gigabytes = embeddings.nbytes / 1e9
    print(
        f"loaded {total_vectors:,} x {dimensions} fp16 ({gigabytes:.1f} GB) "
        f"from {len(shards)} shards in {elapsed:.1f}s",
        flush=True,
    )

    # Sanitize: a handful of rows in some collections contain stray NaN/Inf
    # (the embedder emitted noise for empty/degenerate articles). One NaN row
    # poisons every query's top-k via NaN-tainted similarities.
    started = time.monotonic()
    bad_rows = 0
    for chunk_start in range(0, total_vectors, 1_000_000):
        chunk_end = min(chunk_start + 1_000_000, total_vectors)
        chunk = embeddings[chunk_start:chunk_end]
        bad_mask = ~np.isfinite(chunk).all(axis=1)
        if bad_mask.any():
            bad_rows += int(bad_mask.sum())
            chunk[bad_mask] = 0
    elapsed = time.monotonic() - started
    print(
        f"sanitized {bad_rows} non-finite rows -> zero vectors in {elapsed:.1f}s",
        flush=True,
    )
    return embeddings


def dense_worker(
    gpu_index: int,
    num_gpus: int,
    embeddings: np.ndarray,
    num_neighbors: int,
    query_tile_rows: int,
    candidate_tile_rows: int,
    scratch_dir: Path,
) -> None:
    os.environ["CUDA_VISIBLE_DEVICES"] = str(gpu_index)
    total_vectors = embeddings.shape[0]
    stripe_start = (total_vectors * gpu_index) // num_gpus
    stripe_end = (total_vectors * (gpu_index + 1)) // num_gpus
    print(
        f"[gpu{gpu_index} dense] queries [{stripe_start:,}, {stripe_end:,}) "
        f"vs corpus {total_vectors:,}",
        flush=True,
    )
    indices, scores = gt_stripe_dense(
        embeddings_host=embeddings,
        stripe_start=stripe_start,
        stripe_end=stripe_end,
        num_neighbors=num_neighbors,
        query_tile_rows=query_tile_rows,
        candidate_tile_rows=candidate_tile_rows,
        log_prefix=f"[gpu{gpu_index} dense] ",
    )
    scratch_dir.mkdir(parents=True, exist_ok=True)
    write_bin(scratch_dir / f"stripe_{gpu_index:02d}.ibin", indices, dtype="i32")
    write_bin(scratch_dir / f"stripe_{gpu_index:02d}.fbin", scores, dtype="f32")
    print(f"[gpu{gpu_index} dense] DONE -> stripe_{gpu_index:02d}.{{ibin,fbin}}", flush=True)


def maxsim_worker(
    gpu_index: int,
    num_gpus: int,
    token_bank: np.ndarray,
    section_offsets: np.ndarray,
    num_neighbors: int,
    query_tile_sections: int,
    candidate_tile_sections: int,
    scratch_dir: Path,
) -> None:
    os.environ["CUDA_VISIBLE_DEVICES"] = str(gpu_index)
    total_sections = section_offsets.shape[0] - 1
    stripe_start = (total_sections * gpu_index) // num_gpus
    stripe_end = (total_sections * (gpu_index + 1)) // num_gpus
    print(
        f"[gpu{gpu_index} maxsim] queries [{stripe_start:,}, {stripe_end:,}) "
        f"vs corpus {total_sections:,} sections, {token_bank.shape[0]:,} tokens",
        flush=True,
    )
    indices, scores = gt_stripe_maxsim(
        token_bank_host=token_bank,
        section_offsets_host=section_offsets,
        stripe_start_section=stripe_start,
        stripe_end_section=stripe_end,
        num_neighbors=num_neighbors,
        query_tile_sections=query_tile_sections,
        candidate_tile_sections=candidate_tile_sections,
        log_prefix=f"[gpu{gpu_index} maxsim] ",
    )
    scratch_dir.mkdir(parents=True, exist_ok=True)
    write_bin(scratch_dir / f"stripe_{gpu_index:02d}.ibin", indices, dtype="i32")
    write_bin(scratch_dir / f"stripe_{gpu_index:02d}.fbin", scores, dtype="f32")
    print(f"[gpu{gpu_index} maxsim] DONE -> stripe_{gpu_index:02d}.{{ibin,fbin}}", flush=True)


def gather_outputs(
    scratch_dir: Path,
    model_root: Path,
    suffix: str,
    shards: list[CollectionShard],
    num_gpus: int,
    total_rows: int,
    num_neighbors: int,
) -> None:
    """Slice per-stripe scratch files into per-shard
    `.{suffix}.ground_truth.{ibin,fbin}` files. Each `CollectionShard.row_count`
    is in whatever unit the per-stripe rows were written (articles for dense,
    sections for maxsim) — `gather_outputs` is unit-agnostic.
    """
    bytes_per_row = num_neighbors * 4
    indices_files = [
        open(scratch_dir / f"stripe_{gpu_index:02d}.ibin", "rb")
        for gpu_index in range(num_gpus)
    ]
    scores_files = [
        open(scratch_dir / f"stripe_{gpu_index:02d}.fbin", "rb")
        for gpu_index in range(num_gpus)
    ]
    try:
        stripe_starts = [
            (total_rows * gpu_index) // num_gpus for gpu_index in range(num_gpus + 1)
        ]
        for shard in shards:
            wiki_dir = model_root / shard.wikiname
            wiki_dir.mkdir(parents=True, exist_ok=True)
            indices_path = wiki_dir / f"{shard.stem}.{suffix}.ground_truth.ibin"
            scores_path = wiki_dir / f"{shard.stem}.{suffix}.ground_truth.fbin"
            with (
                open(indices_path, "wb") as out_indices,
                open(scores_path, "wb") as out_scores,
            ):
                out_indices.write(struct.pack("<II", shard.row_count, num_neighbors))
                out_scores.write(struct.pack("<II", shard.row_count, num_neighbors))
                cursor = shard.row_offset
                shard_end = shard.row_offset + shard.row_count
                while cursor < shard_end:
                    stripe_index = next(
                        gpu_index
                        for gpu_index in range(num_gpus)
                        if stripe_starts[gpu_index] <= cursor < stripe_starts[gpu_index + 1]
                    )
                    chunk_end = min(shard_end, stripe_starts[stripe_index + 1])
                    chunk_rows = chunk_end - cursor
                    offset_in_stripe = cursor - stripe_starts[stripe_index]
                    indices_files[stripe_index].seek(8 + offset_in_stripe * bytes_per_row)
                    scores_files[stripe_index].seek(8 + offset_in_stripe * bytes_per_row)
                    out_indices.write(
                        indices_files[stripe_index].read(chunk_rows * bytes_per_row)
                    )
                    out_scores.write(
                        scores_files[stripe_index].read(chunk_rows * bytes_per_row)
                    )
                    cursor = chunk_end
    finally:
        for handle in indices_files + scores_files:
            handle.close()


def run_dense(args, model_root: Path) -> None:
    shards = discover_collection(model_root, args.output_suffix)
    if not shards:
        raise SystemExit(f"no .{args.output_suffix}.f16bin files under {model_root}")
    total_vectors = sum(shard.row_count for shard in shards)
    print(
        f"discovered {len(shards)} shards across "
        f"{len({shard.wikiname for shard in shards})} wikis, "
        f"{total_vectors:,} total rows",
        flush=True,
    )

    # Read dimensions from the first shard's header.
    first_blob = resolve_lfs_pointer(shards[0].path)
    with open(first_blob, "rb") as file:
        _, dimensions = struct.unpack("<II", file.read(8))
    if args.dimensions and args.dimensions != dimensions:
        raise SystemExit(
            f"--dimensions {args.dimensions} != on-disk {dimensions} for {model_root}"
        )

    embeddings = load_dense_collection(model_root, args.output_suffix, dimensions, shards)
    scratch_dir = model_root / f"_ground_truth_scratch_{args.output_suffix}"
    scratch_dir.mkdir(parents=True, exist_ok=True)

    mp_context = mp.get_context("fork")
    workers: list[mp.Process] = []
    for gpu_index in range(args.num_gpus):
        process = mp_context.Process(
            target=dense_worker,
            args=(
                gpu_index,
                args.num_gpus,
                embeddings,
                args.num_neighbors,
                args.query_tile_rows,
                args.candidate_tile_rows,
                scratch_dir,
            ),
        )
        process.start()
        workers.append(process)

    failed = False
    for process in workers:
        process.join()
        if process.exitcode != 0:
            failed = True
            print(
                f"worker pid {process.pid} exited code {process.exitcode}", flush=True
            )
    if failed:
        raise SystemExit("one or more GPU workers failed")

    gather_outputs(
        scratch_dir,
        model_root,
        args.output_suffix,
        shards,
        args.num_gpus,
        total_vectors,
        args.num_neighbors,
    )
    print(
        f"wrote {len(shards)} per-shard "
        f"`.{args.output_suffix}.ground_truth.{{ibin,fbin}}` files under {model_root}",
        flush=True,
    )
    for path in scratch_dir.iterdir():
        path.unlink()
    scratch_dir.rmdir()


def run_maxsim(args, model_root: Path) -> None:
    started = time.monotonic()
    print(f"loading multi-vector corpus under {model_root} ...", flush=True)
    token_bank, section_offsets, shards, dimensions = load_maxsim_corpus(
        model_root, args.output_suffix
    )
    elapsed = time.monotonic() - started
    total_sections = section_offsets.shape[0] - 1
    total_tokens = token_bank.shape[0]
    print(
        f"loaded {total_sections:,} sections, {total_tokens:,} tokens "
        f"({token_bank.nbytes/1e9:.1f} GB) across {len(shards)} shards "
        f"in {elapsed:.1f}s; dim={dimensions}",
        flush=True,
    )

    scratch_dir = model_root / f"_ground_truth_scratch_{args.output_suffix}"
    scratch_dir.mkdir(parents=True, exist_ok=True)

    mp_context = mp.get_context("fork")
    workers: list[mp.Process] = []
    for gpu_index in range(args.num_gpus):
        process = mp_context.Process(
            target=maxsim_worker,
            args=(
                gpu_index,
                args.num_gpus,
                token_bank,
                section_offsets,
                args.num_neighbors,
                args.query_tile_sections,
                args.candidate_tile_sections,
                scratch_dir,
            ),
        )
        process.start()
        workers.append(process)

    failed = False
    for process in workers:
        process.join()
        if process.exitcode != 0:
            failed = True
            print(
                f"worker pid {process.pid} exited code {process.exitcode}", flush=True
            )
    if failed:
        raise SystemExit("one or more GPU workers failed")

    gather_outputs(
        scratch_dir,
        model_root,
        args.output_suffix,
        shards,
        args.num_gpus,
        total_sections,
        args.num_neighbors,
    )
    print(
        f"wrote {len(shards)} per-shard "
        f"`.{args.output_suffix}.ground_truth.{{ibin,fbin}}` files under {model_root}",
        flush=True,
    )
    for path in scratch_dir.iterdir():
        path.unlink()
    scratch_dir.rmdir()


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--mode",
        default="dense",
        choices=["dense", "maxsim"],
        help="dense = single vector per row (cosine); maxsim = multi-vector per "
        "section (ColBERT late interaction)",
    )
    parser.add_argument("--output", default="/home/ubuntu/USearchWiki")
    parser.add_argument("--model-subdir", required=True)
    parser.add_argument(
        "--dimensions",
        type=int,
        default=0,
        help="optional sanity check; if 0, read from first shard's header (dense only)",
    )
    parser.add_argument("--output-suffix", default="body", choices=["body", "title"])
    parser.add_argument("--num-neighbors", type=int, default=100)
    parser.add_argument("--num-gpus", type=int, default=8)
    parser.add_argument(
        "--query-tile-rows",
        type=int,
        default=16384,
        help="dense: rows per query chunk inside the resident stripe",
    )
    parser.add_argument(
        "--candidate-tile-rows",
        type=int,
        default=131072,
        help="dense: rows per candidate tile streamed past the query stripe",
    )
    parser.add_argument(
        "--query-tile-sections",
        type=int,
        default=256,
        help="maxsim: sections per query micro-batch",
    )
    parser.add_argument(
        "--candidate-tile-sections",
        type=int,
        default=65536,
        help="maxsim: sections per streamed candidate tile",
    )
    args = parser.parse_args()

    model_root = Path(args.output) / args.model_subdir
    if not model_root.is_dir():
        raise SystemExit(f"no collection at {model_root}")

    if args.mode == "dense":
        run_dense(args, model_root)
    else:
        run_maxsim(args, model_root)


if __name__ == "__main__":
    main()