From compressed bytes to a GPU tensor
- Kernels written once in Slang and compiled for each backend. Sources in
gpu/kernelsandcore/src/gpu.
A model trains on tensors in GPU memory. To get a batch of GeoZL frames there, the CPU finds and checks the work, the bus carries the compressed bytes, and GPU waves decode them into the tensor.
Rumi loads its training batches this way, with one call to the GPU API.
Move the compressed bytes, not the raster
Picture four 16-bit tiles of 2 MiB each, compressed with planar>zigzag>pfor to about 512 KiB each, a ratio of 4, sitting in host memory.
Decoded on the CPU. The cores expand every frame into its tile, and then all 8 MiB of samples cross the host-to-device bus.
Decoded on the GPU. Only the compressed frames and a short task list cross, and the waves expand them in device memory. The bus carries the compressed size instead of the decoded size, here 2 MiB instead of 8 MiB, so a higher ratio saves more. No CPU core spends its time decoding.
A frame goes to the device when it is a single chunk, its last codec is pfor, lanes or one of their fused planar forms, and its samples are one or two bytes wide. So the device recipes are id>pfor, planar>zigzag>pfor, id>lanes and planar>zigzag>lanes.
Everything else, zstd, the entropy recipes, NoData, quantized and multi-chunk frames, decodes with OpenZL on the CPU and is uploaded as samples. Both kinds can share one call, so a batch with a few odd frames still lands in one tensor, and a data loader does not have to sort its frames by codec first.
Independent strips and headers first
A PFOR block has no fixed size, because its exceptions take extra bytes. If each header sat next to its body, the start of block 3 would be known only after reading blocks 0, 1 and 2, and the CPU would have to walk the whole payload before the GPU could start. Planar has the same kind of chain. Undoing it makes each pixel the sum of every residual above it and to its left, back to the corner of the plane.
planar>zigzag>pfor breaks both chains. It writes the three-byte headers of all blocks first and the bodies after them. On the GPU, size_chunks reads every header in parallel to get each body's size, and plan_blocks runs a prefix sum over those sizes to get each body's start. It also cuts each plane into strips of 16 rows by 1024 columns and restarts prediction in every strip, so a pixel needs only the residuals of its own strip. The CPU only lists frames, bands and strips, never single blocks.
Then decode_strips gives each strip to one wave. The wave walks the strip's 64 blocks left to right. For each one it unpacks PFOR, undoes Zigzag and rebuilds the planar values in registers, adds the last value of each row of the block before, and writes each finished pixel to the tensor once. Restarting prediction at each strip costs a little compression, and in exchange every strip decodes at the same time.
One PFOR block per wave
A wave can broadcast, OR and prefix-sum across its 32 threads without touching memory. A 256-value PFOR block fits one wave, eight values per thread, so it never needs what work across waves needs, shared memory and a barrier where every thread waits for the slowest.
The lane layout from part one puts a thread's eight values in the same slot of the eight lanes of a 16-byte group, so thread t reads slot t of every lane with one load. The exception positions here come as a list. When a list would take more room than a 32-byte bitmap, the block stores the bitmap instead, threads 0 to 7 each load one 32-bit word of it and broadcast it, and every thread ends with the same mask. Either form takes eight wave operations, one per 32-bit word of the mask, and each rank a few popcounts. The whole block is one function of pfor.slang.
An id>pfor frame runs this function in one pass, one wave per block, and stores the values as they come out. A planar>zigzag>pfor frame decodes the same blocks inside decode_strips, described in The PFOR trick.
The CPU prepares the work
The kernels never read a GeoZL frame. Before they run, the CPU does three things with each frame, without decoding a single sample. It captures the compressed blocks, plans where each one is read and where its values go, and stages both in memory the GPU can read. The figure follows an id>pfor frame, where the plan holds one task per block.
Capture happens inside OpenZL's decoder. OpenZL reads the frame and walks its graph as usual, but at the last node a capture hook takes the place of GeoZL's decoder and copies the PFOR payload instead of decoding it. If an earlier node already expanded the data, there is nothing compressed left to send, and that frame is decoded on the CPU.
The plan tells each kernel where to read and where to write, so one wrong length could make it write outside the tensor. The planner checks every length, and one malformed frame rejects the whole call. A fuzzer feeds it damaged PFOR and PivCo frames and compares the plans with CPU decoding. For planar>zigzag>pfor the CPU only lists strips, so the size_chunks and plan_blocks kernels check the blocks on the GPU and record the first bad frame in a small status buffer. Metal reports it when the call returns, CUDA on a later call or in geozl_gpu_synchronize.
Each host thread plans a frame right after capturing it, while its bytes are still in that core's cache. Planning used to be a separate pass afterwards that read every payload from memory a second time. The threads merge their task lists in frame order, so the result does not depend on which thread took which frame.
Staging is memory the GPU can read directly, page-locked or shared with the device depending on the backend. Capture and planning write straight into a staging arena. Before, each call built its inputs in ordinary memory, asked for page-locked memory and copied them across.
What an arena is. Asking the operating system for memory the GPU can read is slow, so GeoZL asks once and keeps the block from one call to the next. A call takes the pieces it needs from the block one after another, and the next call starts again from the beginning, so nothing is freed or asked for again. If a batch does not fit, the arena grows, and after a few calls it stops growing.
On CUDA a call returns before the GPU has finished, so the decoder keeps two arenas. While the GPU decodes call N from one, the CPU prepares call N+1 in the other. With one arena the CPU would wait for the GPU before every call, and the two would take turns.
PivCo, one block per group of four waves
How PivCo decodes from the leaves up is in What a GPU wants. On the device, two things are added. A PivCo block is one byte lane of up to 32 768 symbols, far more than one wave can hold, so a group of four waves takes each block and shares its tree through shared memory. That makes the level loop the only place in the decoder with barriers, one between each level and the next.
Each block decodes one byte lane. A join pass then builds each sample from its byte lanes, low byte first.
Undoing planar for lanes
Planar stores x − (W + N − NW). For planar>zigzag>lanes the GPU undoes it with a running sum along the rows and then down the columns, a two-dimensional prefix sum. In part one the row sum ran inside one register and carried one number to the next. On a GPU the 32 threads of a wave share a row, and a wave prefix sum hands each thread the total of the threads before it.
The scans run once the byte lanes are joined into whole planes. For a plane up to 256 columns wide, one wave takes the whole plane as in the stepper, and loads the next row while it sums the current one. Wider planes do not fit in one wave's registers, so they take two passes, one wave per row walking it in 256-wide segments and carrying the sum between them, then one thread per column adding down the rows. A lanes frame with several planes shorter than 256 samples, or with planes past the device's grid limits, decodes on the CPU instead.
planar>zigzag>pfor does not use these scans. It undoes planar inside each strip, as in The PFOR trick, so it has none of their short-plane or grid limits.