Quantised stacked GRU over a long sequence
/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
Inference of a quantised stacked GRU (L layers, hidden size H) over one long sequence of
T steps for a small batch of B independent streams, followed by an int8 output projection that
is hashed at every step. The CPU source is the exact specification; in short:
- Parameters are drawn from counter hashes of
SEED: int8 weights in[-31, 31](W_l: 3 gates xHxK_lwithK_0 = IandK_l = Habove,U_l: 3 xHxH, outputWO:OxH), int32 biases in[-5000, 5000], and the input bytex[t][b][k]is a byte of a hash of(SEED, t, b, k / 8). All hidden states start at zero. - Gates. For layer
l, unitjand streamb, withxthe input of the layer (the new hidden state of the layer below at the same step) andhthe layer's previous state:a_z = W_z x + bx_z + U_z h + bh_z,a_rlikewise,a_xn = W_n x + bx_n,a_hn = U_n h + bh_n(int32). Pre-activations are requantised to 12 bits:q(a) = clamp((a * M_l + 2^15) >> 16, -2048, 2047)withM_l = max(1, (384 << 16) / (1314 * isqrt(K_l + H))).z = SIG[q(a_z)],r = SIG[q(a_r)](in[0, 256]),n = TANH[q(a_xn + ((r * a_hn) >> 8))](in[-127, 127]) and the new state ish' = ((256 - z) * n + z * h + 128) >> 8(int8). - Lookup tables over
pin[-2048, 2047]use the rationalR(y) = clamp(y (27*65536 + y^2) / (27*65536 + 9 y^2), -256, 256)(C division):SIG[p] = 128 + R(p / 2) / 2,TANH[p] = R(p) * 127 / 256. - Output. After the top layer,
y[b][o] = WO_o . h_top[b](int32). The step hash issum_b mix64(P_b ^ (((t << 8) | b) * 0x9E3779B97F4A7C15))withP_b = sum_o (uint32)y[b][o] * 0x100000001B3^o, everything modulo 2^64 (mix64= SplitMix64 finaliser). Steps are strictly sequential: steptof layerlneeds steptof layerl - 1and stept - 1of layerl.
Input format
One line: L H I B T O SEED.
Output format
gru L=<L> H=<H> I=<I> B=<B> T=<T> O=<O>
step <t> hash=<step hash> (t = floor(T*k/16) for k = 0..15 and t = T-1, distinct, in order)
layer <l> h_sum=<sum of final h> h_hash=<sum of mix64((l << 40) ^ (e << 8) ^ (uint8)h[e])> h0=<first min(H, 8) units of stream 0>
y_last b0=<first min(O, 8) outputs of stream 0 at the last step; zeros when T = 0>
total_hash <sum over t of mix64(step_hash[t] + t)>
Hidden inputs
1 <= L <= 4,1 <= H <= 2048,1 <= I <= 256,1 <= B <= 16,0 <= T <= 200000,1 <= O <= 256, any 64-bitSEED.- Correctness inputs include
T = 0, all sizes 1, four layers with odd sizes and the largest batch,H = 2048andO = 256for a few steps, a short run of the timing shape, a 1000-step sequence, batch 1, and random small configurations. - The timing input has 3 layers,
H = 512,I = 256,B = 8,O = 64and about 50,000 time steps (about 35 million int8 multiply-adds per step, all on the sequential critical path).
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: One input with 3 layers, hidden size 512, input size 256, batch 8, 64 outputs and about 50,000 time 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.