|
FZGPUModules 2.0
GPU-accelerated modular compression pipelines
|
Header: modules/coders/bitpack/bitpack_stage.h
Class: fz::BitpackStage<T>
Category: Coder (lossless)
Packs each element using only its low nbits bits into a dense byte stream.
Most useful after a Lorenzo predictor stage where small delta values only need a few bits of representation.
| Parameter | Constraint |
|---|---|
| T | Unsigned integer type (see available instantiations below) |
Only these types are compiled and linked:
Using any other type will result in a linker error. Most common: BitpackStage<uint32_t> (to match typical quantizer code width).
| Setting | Purpose | Notes |
|---|---|---|
| setNBits(nbits) | Bits per element | Power of two, 1..8 * sizeof(T); ignored when auto-detect is on |
| setAutoDetect(bool) | GPU scan to pick nbits automatically | Adds a host sync while active |
| setBase(base) | Frame-of-reference offset subtracted before packing | Removes dead high bits; always lossless |
| setShift(shift) | Right shift applied after the base subtraction | Removes dead low bits; lossy unless those bits are zero |
| setAutoBase(bool) | GPU min-reduce to pick base automatically | Adds a host sync while active |
| setAutoShift(bool) | GPU OR-reduce to pick the largest lossless shift | Adds a host sync while active |
| setAdaptive(bool) | Convenience: auto base + auto shift + auto nbits | Fully adaptive lossless mode |
nbits must be a power of two in [1, 8 × sizeof(T)]. Default is 8 * sizeof(T) (identity, no compression).
| T | Allowed nbits |
|---|---|
| uint8_t | 1, 2, 4, 8 |
| uint16_t | 1, 2, 4, 8, 16 |
| uint32_t | 1, 2, 4, 8, 16, 32 |
Violations throw std::invalid_argument at setNBits() time.
When enabled, forward execute scans the input for its maximum value using cub::DeviceReduce::Max and selects the smallest valid power-of-two nbits that covers it. The chosen nbits is written into the compressed header so the inverse pass unpacks correctly without any out-of-band configuration.
After compress(), getNBits() reflects the detected value.
Scratch buffers for the scan are allocated through the pipeline's memory pool (with a transparent cudaMalloc fallback in vGPU / pool-fallback mode), so all device memory remains tracked by the pipeline.
Host synchronization: auto-detect requires a device-to-host transfer and a stream synchronization to read the max value. If you know the bit-width ahead of time, use setNBits() instead to avoid it.
Output size estimate: estimateOutputSizes() returns the worst-case (full input size) when auto-detect is enabled, so PREALLOCATE mode reserves sufficient space regardless of the detected nbits.
nbits alone only helps when the useful information already sits in the low bits of each word. The shift transform moves it there first:
The two knobs attack opposite ends of the word and compose:
| Knob | Removes | Lossless? |
|---|---|---|
| base | dead high bits — values clustered far from zero | always |
| shift | dead low bits — values that are all multiples of 1 << shift | only if those bits are zero |
Both default to 0, which is the identity: behaviour is unchanged unless you opt in.
setShift() is lossy in general. The inverse restores (packed << shift) + base, so any low bits you drop are gone — values come back rounded down to a multiple of 1 << shift. Use setAutoShift(true) if you want the largest shift that is provably lossless for your data.
Forward execute then runs three GPU scans, in this order, and writes all three results into the compressed header:
After compress(), getBase(), getShift(), and getNBits() reflect the detected values.
Host synchronization: each auto mode needs a device-to-host readback while any of setAutoDetect, setAutoBase, or setAutoShift is on. Hand-set base/shift/nbits avoid it.
File format: the serialized header is 15 bytes — the previous 10 plus shift (1 byte) and base (4 bytes, zero-extended). Archives written before this option decode with shift = base = 0.
Fixed-width bit packing is a standard lossless representation. This stage, its adaptive parameter selection, and its archive layout are original FZGPUModules code; no third-party implementation was copied. It is distributed under the repository's BSD-3-Clause license.