Integer beam-search decoding over a large vocabulary
/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
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.
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 a vocabulary of 180,000-220,000 tokens, state dimension 256, beam width 16 and 28 chained sentences of 128 steps (3584 sequential steps).
/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.