Synchronous collapsed Gibbs sampling for LDA with exact integer weights
/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
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 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.