Quantized BM25 block-max index and exact top-10 retrieval
/app/cpu/main.cpp is the source of a multicore CPU program (C++17, OpenMP).
The program takes one input-file path (PROGRAM INPUT_FILE) and writes its result to stdout.
What the program computes
The program builds a quantized-BM25 inverted index with block-max metadata over a generated document collection, answers a batch of known-item queries with exact top-10 retrieval, and reports retrieval quality (MRR@10) plus checksums. The CPU source is the exact specification; the stages are:
- Corpus. Term ids
0..V-1follow a Zipf-like law: weightfloor(2^40/(r+16))for termr, sampled byupper_boundon the cumulative weights ofh % total. Docd(hashhd) hasDLMIN + hd % (DLSPAN+1)tokens; tokenjis a topic term (one of 64 terms of the doc's topic(hd>>40) % 1024) with probability 3/8, else a Zipf term (seedoc_tok). - Term frequencies and inversion. Each doc's tokens are sorted; each distinct term gives a
posting
(term, doc, tf)packed asterm<<37 | doc<<12 | tf; all postings are sorted (term-major, then docid). - Integer BM25.
idf = ilog2_q16(2^20 + floor(((2N-2df+1)<<20)/(2df+1))) - 20*2^16(ilog2_q16: fixed-point log2 by 16 squaring steps),K = floor(((3T + 9*dl*N)<<16)/(10T))(k1 = 1.2, b = 0.75,T= total tokens),tfn = floor((tf*22)<<32 / (10*((tf<<16)+K))),raw = idf*tfn. Impacts are quantized uniformly against the global maximum:impact = 1 + floor(raw*254/maxraw)in1..255(1ifmaxraw = 0). - Block-max. Each posting list is cut into blocks of 64 postings; per block the maximum impact and the last docid; per term the maximum impact.
- Queries. Query
qpicks a target doc and 2..5 of its token positions (plus, for a quarter of the queries, one extra Zipf noise term); terms are deduplicated and terms without postings dropped. Score of a doc = sum of its impacts over the query terms. The result is the exact top-10 ordered by score descending, then docid ascending, computed by Block-Max WAND (bmw). Any method that returns exactly the same 10 (doc, score) pairs in the same order is correct; the skipping strategy is not part of the output. - Evaluation. MRR@10 against the target doc, hits@1, hits@10, and checksums.
Input format
N V SEED
DLMIN DLSPAN NQ
1 <= N <= 4*10^6 docs, 16 <= V <= 2^21, 64-bit SEED, DLMIN >= 1,
DLMIN + DLSPAN <= 4000, NQ >= 0 queries.
Output format
index docs N tokens T postings P terms U blocks B maxraw M | imp c0 .. c7 | chk HP HB
queries NQ terms QT results R | mrr_num S mrr@10 X.XXXXXX | hit1 A hit10 B | full F theta_sum TS | chk HQ
q i target t terms m | d:s d:s ... (for the first min(NQ, 6) queries)
U = terms with at least one posting, c_k = number of postings with impact>>5 == k,
HP = sum over posting positions i (global term-major order) of
mix64(((doc<<8)|impact) ^ (i*0xC2B2AE3D27D4EB4F)), HB = sum over global block index b of
mix64(((last<<8)|blockmax) ^ (b*0x165667B19E3779F9)). QT = total query terms after dedup,
R = total results, S = sum of 2520/rank over queries whose target is in the top-10
(mrr@10 = floor(S*10^6/(NQ*2520)) with 6 decimals, - if NQ = 0), F = queries with 10
results, TS = sum of their 10th score, HQ = sum over all results of
mix64((q<<36) ^ (rank0<<32) ^ doc ^ (score*0x9FB21C651E98DF25)). Hex values have 16 digits.
Hidden inputs
Correctness inputs (10 files): a single one-token doc, 7 docs (fewer than 10 results),
V = 2^21, V = 50 with 3000 docs (very long lists, many score ties), docs of ~4000 tokens,
NQ = 0, SEED = 0 and 2^64-1, and random cases with N in [500, 12000], V in
[1000, 500000], DLMIN in [1, 60], DLSPAN in [0, 400], NQ in [100, 1500].
The timing input has N in [750000, 850000], V = 2^20, DLMIN = 40, DLSPAN = 400
and NQ = 200000.
Your task
Port this program to CUDA in /app/gpu/main.cu: same command line (PROGRAM INPUT_FILE),
same output on stdout. Then make it as fast as you can on the GPU while keeping the output correct.
The main computation must execute on the GPU. Renaming CPU code to .cu, running a
CPU fallback, or launching a dummy kernel while computing the answer on the CPU does not satisfy this task.
The CPU source is the specification: when this text and the code disagree, the code wins.
This sandbox is for writing the source only: it has g++ and the CPU program, but no CUDA
compiler and no GPU. Compilation, execution and verification happen later on an H100 in a
separate verifier, and a port that does not compile is rejected, so check your logic against
the CPU program before you finish (for example by emulating your kernels on the host).
How your port is verified
- Only
/app/gpu/main.cuis collected. It is compiled withnvcc -O3 -arch=sm_90 -std=c++17 -o port /app/gpu/main.cu; no extra libraries are linked. The CUDA runtime and the header-only libraries that ship with the CUDA toolkit (CUB, Thrust, libcu++) are allowed. - Your source must not contain any of:
system(,popen(,fork(,exec*,dlopen(,getenv(,#include <filesystem>,/tests/,/proc/. The program may read only the input file it is given. - Verification runs on one H100 in a fresh container without network. Hidden inputs are generated with a fresh random seed at verification time.
- Correctness: every hidden input (small edge cases plus a large timing input) must pass. Your output must be identical to the CPU program's output (trailing whitespace ignored). Any mismatch, crash or timeout is rejected.
- GPU execution is checked: a port that computes its answer on the CPU is rejected even when its output is correct.
- Speed: The timing input has about 800k documents (about 1.9e8 tokens and 1.5e8 postings over a 2^20-term vocabulary) and 200000 queries of 2 to 6 terms.
/app/inputs/timing_example.inhas the shape and size of the hidden timing input. Your port is timed against a reference GPU port on it; the faster your port, the higher its score. /app/inputs/holds public input examples for understanding the input format.