Embedding-bag pooling over int8 tables with power-law bags
/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
text
cpu_baseline INPUT_FILE
It reads one 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
text
NT NBATCH BS SEED
R_0 D_0 MODE_0 WEIGHTED_0 BASE_0 LMAX_0
... (NT table lines)
Output format
text
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 26 tables (dims from {16, 32, 64, 128}, sizes log-uniform before being rescaled to about 2.4 GB of int8 rows in total, some tables with millions of rows), 8 batches of about 750,000 samples, mean bag lengths of about 6 to 37 ids depending on the table, every fourth table mean-pooled and every third table weighted: about 3.5e9 row lookups.
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 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. - Grading runs on one H100 in a fresh container without network. Inputs are generated with a new random seed at grading time, so they cannot be known in advance.
- 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.
- Speed: One input with 26 tables holding about 2.4 GB of int8 rows (16-128 dims, up to millions of rows each) and 8 batches of about 750,000 samples, about 3.5e9 row lookups in total. 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.