MinHash and LSH near-duplicate detection with exact multiset Jaccard
/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
Near-duplicate detection over N generated token documents: MinHash signatures, LSH banding,
candidate pairs, and exact verification by multiset Jaccard similarity.
Documents. Tokens are ids in [0, 2^17) whose frequency falls off like 1/sqrt(id). Document
d is generated from the seed (mix64 is the SplitMix64 finaliser; exact formulas in the CPU
source). With probability DUPPM / 10^6 (and only if T > 0) it is an edited copy of one of T
templates (small template indices are much more popular, so cluster sizes are skewed): it gets an
edit rate e in [0, EDMAX] per mille, and each template token is, with probability e/1000,
substituted, deleted, or preceded by an inserted token. Otherwise the document is unique. The base
length is uniform in [LMIN, LMAX] (copies share their template's base length), and documents are
cut to 2048 tokens.
Shingles. The m = n - 3 overlapping token 4-grams of a document of n tokens, each hashed to
64 bits. A document with fewer than 4 tokens is empty and takes no further part.
Signatures and bands. K = 256 multiply-shift hash functions
h_k(x) = ((A_k * x + B_k) mod 2^64) >> 32 (A_k odd, derived from the seed); sig[k] is the
minimum of h_k over the document's shingles. The signature is cut into 64 bands of 4 consecutive
values, and each band is hashed with its band index to a 64-bit band key.
Candidates. A pair of non-empty documents i < j is a candidate if their band keys are equal in
at least one band (64-bit key equality); it is counted once, in its first such band. shared is
the number of bands with equal keys.
Verification. For every candidate, the multiset intersection I of the two shingle multisets
(sum of the smaller multiplicities) and the union U = m_i + m_j - I. The pair is verified if
5 * I >= 3 * U (Jaccard >= 3/5, exact integer test).
Input format
One line:
N T LMIN LMAX DUPPM EDMAX SEED
Output format
docs N empty E shingles S
sighash SH
candidates C
shared_bands s0 .. s6
verified VP
pairhash PH
jaccard_deciles j0 .. j10
degree_hist g0 .. g9
maxdeg D G
E: empty documents;S: total shingles.SH: sum over non-empty documents ofmix64(F_d + d)withF_d = sum over k of mix64(sig[k] ^ (k << 32))(all sums modulo 2^64).shared_bands: candidates bysharedin 1, 2, 3-4, 5-8, 9-16, 17-32, 33-64.PH: sum over verified pairs ofmix64((i << 32) | j).jaccard_deciles: all candidates byfloor(10 * I / U)(11 buckets).degree_hist: documents with at least one verified pair, by their number of verified pairs: 1, 2, 3-4, 5-8, ..., 129-256, 257 or more (10 buckets).maxdeg D G: the document with the most verified pairs (smallest index on ties) and that number;-1 0if there is none.
Hidden inputs
0 <= N <= 4*10^6,0 <= T <= N,1 <= LMIN <= LMAX <= 10^5,0 <= DUPPM <= 10^6,0 <= EDMAX <= 1000.- Correctness inputs include no documents, one document, mostly empty documents, 4-7 token
documents, one giant cluster, exact duplicates (
EDMAX = 0), documents cut at 2048 tokens, no planted duplicates, and random mixes. - The timing input has the shape and size of
/app/inputs/timing_example.in.
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:
/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.