Synchronous collapsed Gibbs sampling for LDA with exact integer weights
/app/cpu/main.cpp is a multicore CPU program (C++17, OpenMP). It is built as
/app/bin/cpu_baseline with g++ -O3 -march=native -fopenmp -std=c++17 and run as
cpu_baseline INPUT_FILE
It reads one input file and writes its result to stdout.
What the program computes
Training of an LDA topic model by synchronous (Jacobi) collapsed Gibbs sampling with exact integer arithmetic. All constants, hash functions and generators are defined in the CPU source.
Corpus. D documents; document d has a length in [LMIN, LMAX] from a hash, so the token
offsets are a prefix sum. Each document mixes 1 to 3 of P planted topics; planted topic p draws
words from a Zipf(1) law (rank weights floor(2^32/(rank+3)), inverse-CDF by binary search) mapped
through the bijection rank -> (rank*mul_p + add_p) mod V. Every token gets an initial topic in
[0, K): three quarters of the tokens start in slot p mod K of their planted topic, the rest at
random (a warm start).
Counts. n_dk (document x topic), n_wk (word x topic) and n_k (topic totals) always match
the current assignment.
Sweep s (SWEEPS times). With VB = V*B16, every topic gets the factor
R_k = max(1, floor(VB * 2^15 / (16*n_k + VB))). Then every token is resampled from the counts
frozen at the start of the sweep, with its own current assignment z removed from them:
x_k = (16*(n_dk - [k==z]) + A16) * R_k (fits in 32 bits)
y_k = 16*(n_wk - [k==z]) + B16 (fits in 32 bits)
w_k = x_k * y_k (exact 64-bit)
total = sum_k w_k, u = top 32 bits of a counter hash of (sweep, token index)
r = floor(total * u / 2^32) (128-bit product)
new topic = the smallest k with w_0 + ... + w_k > r
A16 and B16 are the Dirichlet parameters a and b in units of 1/16. After all tokens have
been sampled, n_dk, n_wk and n_k are rebuilt from the new assignment. Per sweep the program
also sums, over all tokens, ilog(total) - ilog(w_new) (an integer log-likelihood proxy, where
ilog(x) = 256*floor(log2 x) + the next 8 bits of x), and counts the tokens whose topic changed.
Any evaluation order is fine as long as the sampled topic is exactly the one defined above (the
smallest k in topic order, not a topic chosen by some other partition of the total).
Input format
One line:
D V K P LMIN LMAX SWEEPS A16 B16 SEED
0 <= D, 1 <= V <= 2^20, 1 <= K <= 1024, 1 <= P <= 4096, 0 <= LMIN <= LMAX <= 4096,
0 <= SWEEPS <= 64, 1 <= A16, B16 <= 64, V*K <= 2^27, SEED an unsigned 64-bit integer, and the
total token count is at most 2*10^8.
Output format
tokens T docs D vocab V topics K
sweep s ll LL changed C maxk M empty E hash H (one line per sweep)
top s_1 ... s_8 (largest topic sizes after the last sweep, at most K values)
sizehist h_0 ... h_31 (h_b = number of topics with bit length of n_k equal to b)
nwk_hash HW ndk_hash HD
M and E are the largest topic size and the number of empty topics after the sweep's rebuild,
H is the sum over tokens i of mix64((i << 16) ^ z_i) modulo 2^64; HW and HD hash the final
count tables (see the source). All values are integers and must match exactly.
Hidden inputs
Correctness inputs (about 14) include D = 0, empty documents, V = 1, K = 1, K = 2,
K = 1024, K not a multiple of 32, one document of length 4096, A16 = B16 = 64, zero sweeps,
and random small corpora with up to a few hundred thousand tokens. The timing input has about
130,000 documents with lengths in [50, 350] (about 2.6e7 tokens), V in [40000, 80000],
K in {384, 448, 512}, P in [96, 192], 10 sweeps, A16 in [4, 16] and B16 in [1, 4].
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.
How you are scored
- 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. - GPU verification uses the backend selected for this run: an H100 attached to the separate verifier, or a remote H100 worker. Hidden inputs use a fresh random seed.
- For public GPU experiments, use
/app/bin/gpu_run [--backend auto|local|remote] [--repeat N] [--quiet] SOURCE.cu INPUT_FILE.... The default usesGPU_RUNNER_URLwhen configured, otherwise a GPU attached to this sandbox. Sandbox GPU access is configured independently from verification; a CPU-only sandbox needs a runner URL. - Correctness: every hidden input (small edge cases plus the timing input) must pass. Your output must be identical to the CPU program's output (trailing whitespace ignored). Any mismatch, crash or timeout gives reward 0.
- GPU execution: a separate, untimed CUPTI check must observe completed GPU kernel activity on the hidden timing input. A CPU-only submission receives reward 0 even if its output is correct.
- Speed: The timing input has about 130,000 documents (about 2.6e7 tokens), a vocabulary of 40,000-80,000 words, K in {384, 448, 512} topics and 10 sweeps. Your port and our GPU reference are each run on it
once to warm up and then 5 times, interleaved; the time is the wall clock of the whole process
(including CUDA initialisation, input parsing and output). When correct,
reward = min(2, t_reference / t_yours)using the medians. /app/inputs/holds a few public inputs./app/bin/run_public.shcompiles your port with the flags above and compares it against the CPU baseline on each of them.