GeoZL

From compressed bytes to a GPU tensor

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.

The path of a batch. On the host, CPU threads capture each frame's payload, write one task per block, or one per strip of a band for planar>zigzag>pfor, and stage both in memory the GPU can read. Across the bus, GPU waves unpack the PFOR or PivCo blocks, undo planar, in registers for planar>zigzag>pfor and with scans for planar>zigzag>lanes, and place the samples into the caller's tensor.
The CPU prepares, the bus carries compressed bytes, the waves decode.

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.

Four 16-bit tiles, 2 MiB compressed and 8 MiB decoded. Decoded on the CPU, all eight cores expand the frames and 8 MiB of samples cross the bus to the tensor. Decoded on the GPU, the cores only find the blocks, 2 MiB of frames cross the bus, and the GPU waves expand them into the 8 MiB tensor.
Decoding on the GPU sends only the compressed frames over the bus and leaves the CPU cores free.

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.

Before, each header sat beside its body, so block 3 waited for blocks 0 to 2, and each pixel needed every residual above it and to its left, back to the corner of the plane. After, all headers come first and a prefix sum gives every body its start, prediction restarts in strips of 16 rows by 1024 columns so a pixel needs only the residuals of its own strip, and one wave decodes each strip and stores each sample once.
1 / 5
Headers first let the GPU find every block with a prefix sum. Strips let each pixel be rebuilt from its own strip, so all strips decode at once.

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.

A PFOR block stores 3 bits for each of its 256 values and keeps the extra bits of three larger values apart. The 32 threads of one wave each read 3 bits for their 8 values, share the three exception positions with one wave operation, find the missing bits of their own exceptions, and write the finished values.
1 / 5
One wave decodes one block. The threads share the exception positions through wave operations, never through memory.

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.

The CPU reads the header and checksum of an id>pfor frame, copies its 4096 compressed blocks into staging without decoding them, and writes one task per block saying where its bytes start and where its values go. One copy moves blocks and tasks to the GPU, where each wave takes one task.
1 / 4
The CPU reads the frame and writes the tasks, the GPU decodes the blocks.

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.

Before, a host thread copied frames 0, 3 and 6 before planning frame 0, so frame 0 had left the core cache and was read from memory a second time. After, the thread plans frame 0 right after copying it, while it is still in the cache.
Planning a frame right after copying it finds its bytes still in the cache.

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.

Before, each of three calls asked the operating system for GPU-readable memory, wrote its inputs and freed it. After, the first call asks once and the next two write into the same memory, so the three calls end sooner.
Without an arena every call asks for memory and frees it. With one, only the first call asks.

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.

With one arena on CUDA, the CPU waits for each decode before it can prepare the next call, so the CPU and GPU take turns. With two arenas, the CPU prepares call N+1 in arena B while the GPU decodes call N from arena A, and call N+2 waits only until decode N frees arena A, so the three calls finish sooner.
1 / 3
Two arenas let the CPU prepare the next call while the GPU decodes this one.

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.

Four waves of 32 threads take one PivCo block and share its tree through shared memory. They decode the levels from the deepest to the root, with a barrier between each level and the next.
Four waves share one block through shared memory and wait for each other after every level.

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.

Three rows of planar residuals across sixteen columns, columns 0 to 7 owned by thread t0 and 8 to 15 by t1. Each row is summed along by the two threads, a wave prefix sum adds t0 total of 37 to t1, and the row sum is added to the decoded row above, so the decoded rows fill in one at a time until the plane is the original.
1 / 6
Each row is summed across the wave, then added to the decoded row above, which every thread keeps in registers.

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.