Byte-level BPE tokenisation with ranked merges
/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
Byte-level BPE tokenisation of NDOC generated documents of DOCLEN bytes each, with a ranked
merge table derived from the seed. The program reports piece and token counts, an order-defined
token-stream hash, token frequency statistics and the most frequent tokens.
Vocabulary (mix64 is the SplitMix64 finaliser; exact formulas in the CPU source): 6000
morphemes of 2-7 lowercase letters drawn with English-like letter frequencies, and 65536 word types
of 1-4 morphemes (morpheme popularity falling off like 1/sqrt(rank)).
Merge table. Token ids 0-255 are the bytes; merge number r (its rank) creates token
256 + r from a pair of existing tokens, and the token's byte string is the concatenation of the
pair's strings. The table is built from an ordered list of target strings: for each target, while
the table has fewer than NMERGE merges and the BPE encoding of the target (see below, with the
merges built so far) has more than one token, the pair formed by its first two tokens is added as
the next merge. Targets, in order: for each morpheme i: " " + morpheme, morpheme, and for
i < 500 also " " + Morpheme (first letter upper case); then " " + word for every word type of
at least 2 morphemes and at most 23 bytes. The table ends with fewer than NMERGE merges if every
target is a single token.
Documents. Document d is a stream of items from a counter-based generator, cut at DOCLEN
bytes: vocabulary words (74%; word popularity falling off like 1/sqrt(rank); 1/64 of them in
upper case, 5/64 capitalised), random lowercase strings of 3-14 letters with occasional 2-byte
sequences C3 xx (6%), digit strings of 1-9 digits (6%), punctuation (, . ! ? ; : ' " ( ) - ... -- ?!, 10%), newlines (2.5%) and tabs (1.5%). Words, random strings and digit strings are
preceded by a space except at the start of the document or after a newline or tab. Then every byte
is, with probability TYPO / 10000, replaced by a character from a fixed set of 40 (letters,
spaces, digits, . and ,), decided by a counter keyed by the global byte index
d * DOCLEN + i.
Pre-tokenisation. Bytes are letters (a-z, A-Z and every byte >= 0x80), digits, spaces,
other whitespace (\n \t \r) or punctuation (everything else). Each document is split from left to
right into pieces:
- a single space followed by a letter or punctuation starts the piece of what follows;
- a letter run continues over letters, but a lowercase letter followed by an uppercase letter ends it;
- a punctuation run continues over punctuation;
- a digit run is cut into groups of at most 3 digits;
- a whitespace run continues over spaces and other whitespace; if it has at least 2 bytes, ends with a space and is followed by a letter or punctuation, that last space is left to the next piece.
Pieces longer than 24 bytes are cut into chunks of 24 bytes (the last one shorter).
BPE. Every piece is encoded on its own: start from its bytes and repeatedly merge the adjacent pair of tokens with the lowest merge rank (the leftmost such pair on ties) until no adjacent pair is in the merge table.
Input format
One line:
NDOC DOCLEN NMERGE TYPO SEED
Output format
docs NDOC bytes NB merges M
pieces NP tokens NT used_tokens U
tokenhash TH
freqhash FH
tokens_per_piece h0 .. h6
top ID COUNT HEX (up to 20 lines)
NB = NDOC * DOCLEN;M: number of merges built (at mostNMERGE).TH: sum over all tokens ofmix64((d << 40) | (idx << 20) | id), whereidxis the token's 0-based position in the token stream of documentd(modulo 2^64).U: number of distinct token ids that occur;FH: sum over those ids ofmix64(id * 0x9E3779B97F4A7C15 + count)(modulo 2^64).tokens_per_piece: pieces by their token count 1, 2, 3, 4, 5-8, 9-16, 17-24.top: the 20 most frequent token ids (ties: smaller id first), only ids that occur, with their count and their byte string in lowercase hex.
Hidden inputs
0 <= NDOC <= 2^24 - 1,1 <= DOCLEN <= 2^20 - 1,0 <= NMERGE <= 60000,0 <= TYPO <= 10000.- Correctness inputs include no documents, one-byte documents, no merges (bytes only), 60000 merges, every byte replaced by a typo, 2^20 - 1 byte documents, 20000 documents of 37 bytes, no typos, and random mixes.
- The timing input has
DOCLENin [15000, 17500],NDOCsuch that the text is about 3.8 GB (+-5%),NMERGEin [24000, 32000] andTYPOin [60, 100].
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. - This sandbox has no GPU.
nvccworks here for compiling. To run on the GPU, use/app/bin/gpu_run [--repeat N] [--quiet] SOURCE.cu INPUT_FILE...: it compiles the file with the flags above on a remote H100 worker, runs it on each input, and prints stdout, exit code and wall-clock time. Grading also runs your code on an H100 worker, one job per GPU at a time. 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: The timing input has about 234K documents of 15000-17500 bytes (about 3.8 GB of text), 24000-32000 merges and a typo rate of 0.6-1.0% per byte. 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.