Integer beam-search decoding over a large vocabulary
/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
Beam-search decoding of a fixed-point toy language model over a large vocabulary, with exact
integer scores. NS sentences are decoded one after the other, T steps each, with beam width
B. All constants and hash functions are defined in the CPU source.
Model (generated from SEED by counter hashes): an int8 output matrix W (V x D, values in
[-64, 63]), an int32 bias per token ([-32, 31]), token embeddings emb(v, d) in [-512, 511]
computed on the fly from a hash, and an int16 start state h0 of dimension D. Token 0 is the
end-of-sentence token EOS.
One step t = 0 .. T-1 of a sentence. Every live beam b has an int16 state h_b, a
cumulative score cum_b (int32, in units of 1/16 bit) and its token history.
logit_b[v] = floor((sum_d W[v][d] * h_b[d]) / 2^LSH) + bias[v] + (v == 0 ? EOSB * (t - floor(T/2)) : 0)
mx_b = max_v logit_b[v]
Z_b = sum_v e2(logit_b[v] - mx_b) e2(-k) = floor(T16[k mod 16] / 2^floor(k/16)), 0 for k >= 336
lse_b = mx_b + ilog16(Z_b) ilog16 = 16*log2(Z / 2^20) rounded down to 1/16 (table TQ)
score(b,v) = cum_b + logit_b[v] - lse_b
e2 and ilog16 are an exact integer log-sum-exp (64-bit sum, tables T16 and TQ in the
source). No repeated trigram: token v is banned for beam b if the beam's history y has an
index i with i + 2 < t, y[i] = y[t-2], y[i+1] = y[t-1] and y[i+2] = v.
All non-banned candidates (b, v) of all live beams are ranked by score descending, then token
ascending, then beam ascending, and the best 2B are walked in rank order:
- an EOS candidate at rank
k < Bfinishes a hypothesis: the finished counter is increased and its final score isfs = -floor((-score) * 64 / (len + 5))withlen = t + 1; iffsis strictly larger than the best final score so far, it becomes the best (history plus EOS, and the stateadvance(h_b, 0)). EOS candidates at rankk >= Bare skipped; - any other candidate becomes a new live beam (state
advance(h_b, v),cum = score, history plusv) untilBlive beams exist.
advance(h, v)[d] = sat16( floor(h[d] * (24576 + floor(h[(d+1) mod D] / 8)) / 2^15) + 4 * emb(v, d) )
(all divisions by powers of two are arithmetic shifts, i.e. floors). The first step starts from one
beam (h0, cum = 0). After step T-1 the live beams, in beam order, also compete for the best
with fs = -floor((-cum) * 64 / (T + 5)) (strictly larger replaces). The next sentence starts from
h0[d] = sat16(floor(hbest[d] / 2) + 8 * emb((s*7919 + 13) mod V, d)), where hbest is the state
of the best hypothesis of sentence s.
Any evaluation order is fine as long as the ranking, the tie-breaks and the integer arithmetic are exactly the ones above.
Input format
One line:
V D B T NS LSH EOSB SEED
1 <= B <= 16, 16 <= D <= 512 with D a multiple of 16, 1 <= T <= 256, 2B + T <= V <= 10^6,
1 <= NS <= 100, 8 <= LSH <= 24, 0 <= EOSB <= 64, SEED an unsigned 64-bit integer.
(V >= 2B + T guarantees that there are always B live beams after the first step.)
Output format
vocab V dim D beams B steps T sentences NS
sent s best_len L best_score S best_hash H first y_0 y_1 y_2 y_3 finished F live_hash LH (one line per sentence)
L, S are the length (EOS included) and final score of the best hypothesis, H is mix64 of a
fold of its tokens, first lists its first (up to) 4 tokens, F counts the finished hypotheses of
the sentence and LH is a sum of hashes of the live beams after the last step (see the source). All
values are integers and must match exactly.
Hidden inputs
Correctness inputs (about 12) include V = 2B + T (frequent trigram bans), a single step per
sentence, greedy search (B = 1), B = 16, D = 16 and D = 512, an EOS bias strong enough to
end most hypotheses early, EOSB = 0, and random small shapes. The timing input has V in
[180000, 220000], D = 256, B = 16, T = 128, 28 chained sentences (3584 sequential steps),
LSH = 16 and EOSB in [8, 12].
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 a vocabulary of 180,000-220,000 tokens, state dimension 256, beam width 16 and 28 chained sentences of 128 steps (3584 sequential steps). 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.