Embedding-bag pooling over int8 tables with power-law bags
/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
Embedding-bag pooling (the sparse-feature front end of a recommendation model) over many int8 tables with per-row scales. The CPU source is the exact specification; in short:
- Tables. Table
thasR_trows ofD_tint8 values and a uint16 scale per row. Rows are built in chunks of 1024 (rows c*1024 .. c*1024+1023): the first row of a chunk starts from an all-zero previous row, and rowrisnl8(3*prev[d] + prev[(d+1) % D] + byte_{d%8}(h_r) + d)for eachd, whereh_ris a counter hash of(SEED, t, r)andnl8a fixed nonlinear byte bijection. Rowrtherefore depends on all earlier rows of its chunk; computing one row on its own costs up to 1023 row steps, so tables have to be materialised.scale_r = 16 + (h_r >> 54). - Bags. For batch
b, sampleiand tablet, a hashhbof(SEED, b, i, t)gives the bag lengthL = min(LMAX_t, floor(BASE_t * 65536 / u) - BASE_t)withu = (hb & 0xFFFF) + 1(a power law with a long tail). Lookupjusesh = mix64(hb + (j+1)*0x9E3779B97F4A7C15): a Zipf-like rankk(log-uniform over[0, 2^bits(R_t)), so the first ranks get several percent of all lookups) is hashed into a row with a per-(table, batch pair) salt, so the hot rows move every two batches. Weighted tables multiply a lookup byw = 1 + ((h >> 8) & 7), others by 1. - Pooling.
v[d] = sum_j w_j * scale[row_j] * q[row_j][d](int32). Tables withMODE = 1use the floor meanfloor(v[d] / L)whenL > 0. An empty bag gives a zero vector. - Hashes. For every (batch, sample, table) the pooled vector is folded into
poly = sum_d (uint32)v[d] * 0x100000001B3^dand the batch hash addsmix64(poly + key * 0x9E3779B97F4A7C15)withkey = (b << 32) | (i << 5) | t(all modulo 2^64;mix64= SplitMix64 finaliser).
Input format
NT NBATCH BS SEED
R_0 D_0 MODE_0 WEIGHTED_0 BASE_0 LMAX_0
... (NT table lines)
Output format
batch <b> lookups=<ids looked up in batch b> hash=<batch hash> (b = 0..NBATCH-1)
table <t> R=<R> D=<D> mode=<sum|mean> weighted=<0|1> lookups=<ids> sum=<sum of all pooled values of table t>
probe <p> len=<L> v=<v[0]>,...,<v[7]> (table 0 bag of sample 0 in batch 0, then samples BS/2 and BS-1
of the last batch; missing dims print 0; a probe is printed only
if it exists and is not already an earlier probe)
embedbag tables=<NT> batches=<NBATCH> batch_size=<BS> rows=<sum R> bytes=<sum R*D> lookups=<total> hash=<sum of batch hashes>
Hidden inputs
1 <= NT <= 32,0 <= NBATCH <= 16,0 <= BS <= 2^20; per table1 <= R <= 2^24,1 <= D <= 128(any value),MODEandWEIGHTEDin{0, 1},0 <= BASE <= 64,0 <= LMAX <= 255; any 64-bitSEED. Pooled values always fit in int32.- Correctness inputs include no batches, empty batches, a single sample with an empty bag, one-row
tables with very long bags, row counts at chunk edges (1023, 1024, 1025), odd dimensions, a
2^24-row table withD = 1, 32 tables over 16 batches, and random small configurations. - 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.