Metadata-Version: 2.1
Name: slabitz
Version: 0.0.1
Summary: GPU bitshuffle-ZSTD and bitshuffle-LZ4 decoder and encoder for HDF5 filter 32008
Home-page: https://github.com/jonwright/slabitz
Author: Jon Wright
Author-email: wright@esrf.fr
License: MIT
Platform: manylinux_2_17_ppc64le
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 2.7
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: Free Threading :: 3 - Stable
Classifier: Topic :: Scientific/Engineering :: Physics
Requires-Python: >=2.7
Requires-Dist: numpy
Description-Content-Type: text/markdown

# slabitz

GPU **decoder and encoder** for bitshuffle-zstd and bitshuffle-LZ4 HDF5 chunks
(filter 32008) from X-ray detectors, written in Slang/CUDA and callable from
Python and C.

- **Zstd decode:** Microsoft's DirectStorage GPU zstd shaders (HLSL), transformed
  and compiled with Slang to CUDA.
- **LZ4 decode:** Slang port of the nvCOMP decoder used by `bslz4_to_sparse`,
  with a fused decode + bit-unshuffle kernel.
- **Encode:** GPU bitshuffle transpose and plane-aware LZ4/zstd encoders
  (producing filter-32008 chunks compatible with `hdf5plugin` and `pac-zstd`).
- **Host & Python:** Small C host library (`src/`, `build/libslabitz.so`) over
  the CUDA driver API (`dlopen("libcuda.so.1")`, no CUDA runtime dependency),
  wrapped by the `slabitz` Python package (`ctypes`, `__cuda_array_interface__`
  v3, and `__dlpack__`).

## Status and Limits

- **Supported GPUs:** NVIDIA Kepler (`sm_35`) through Ada/Hopper (`sm_89`/`sm_90`),
  verified on L40S (`sm_89`), GTX 1050 Ti (`sm_61`), and TITAN Black (`sm_35`,
  driver 340).
- **Packaging:** Build a standalone, Python-version-independent wheel
  (`py2.py3-none-manylinux_2_17_x86_64` / `ppc64le`, bundling `libslabitz.so`
  and all 30 portable fatbins) with `pixi run wheel` (or
  `python3 tools/build_wheel.py`) and install with `pip install dist/slabitz-*.whl`.
- **Security limit:** Valid frames are memcheck-clean, and host frame walks are
  bounds-checked, but compressed-block contents are still trusted by the GPU
  decode kernels (`T11b`). **Do not decode untrusted compressed data.**

## Python Quickstart

```python
import numpy as np
import slabitz

frame = np.zeros((512, 512), dtype=np.uint16)
frame[200:220, 200:220] = 42

with slabitz.Decoder(device=0) as dec:
    # Encode on the GPU (ZSTD or LZ4)
    chunk = dec.encode(frame, codec=slabitz.ZSTD)

    # Inspect filter-32008 header and decode back on the GPU
    codec = slabitz.codec_of_chunk(chunk)
    out, err = dec.decode([chunk], codec, frame.dtype, frame.shape)
    dec.wait()
    assert (err.get() == 0).all()
    assert np.array_equal(out.get()[0], frame)
```

Runnable MVP demos and benchmarks live in `examples/`:

- [`examples/quickstart.py`](examples/quickstart.py): basic GPU encode + decode
  round-trip for ZSTD and LZ4.
- [`examples/hdf5_roundtrip.py`](examples/hdf5_roundtrip.py): GPU encode ->
  write raw chunks with `h5py` / `hdf5plugin.Bitshuffle` -> CPU & GPU readback.
- [`examples/bench_simple.py`](examples/bench_simple.py): simple encode/decode
  throughput comparison against CPU `hdf5plugin`.

See `docs/` (`mkdocs.yml`) for the user guide, API reference, and benchmark
tables.

## Build and Test

### Anywhere: pixi

The checkout is git-only. Pixi, its package cache, build temp files and the
environment live in a separate workspace (typically a gitignored `workspace/`
symlink next to the checkout):

```bash
WS=/path/to/workspace
mkdir -p $WS && git clone <this repo> $WS/slabitz
$WS/slabitz/tools/setup_workspace.sh --workspace $WS [--examples DIR]

. $WS/env.sh && cd $WS/slabitz
CUDA_ARCH=sm_61 pixi run test    # fetch slangc, build kernels + lib, run tests
```

Set `CUDA_ARCH` to your GPU (`sm_50` Maxwell, `sm_61` Pascal, `sm_89` Ada/L40S).
The pixi environment (`pixi.toml`, `pixi.lock`) provides Python 3.12, numpy,
zstandard, h5py, hdf5plugin, bitshuffle, gcc 13, pytest, lz4, and CUDA 12.6.
The host only needs the NVIDIA driver (`libcuda.so.1`).

### At ESRF (shared root)

Development runs in `$SLABITZ_DEV_ROOT`:

```bash
cd $SLABITZ_DEV_ROOT/wt/<task> && . $SLABITZ_DEV_ROOT/env.sh
sbz build && sbz test            # kernels linked from shared cache, library built locally
gpu-run sbz test                 # run on an idle Slurm L40S
```

### Portable kernels (sm_35..sm_90 + PTX)

```bash
pixi run -e kepler build-sm35    # sm_35 cubins (NVRTC 11.8)
pixi run build-fatbins           # one .fatbin per kernel in build/kernels/
SLABITZ_KERNEL_DIR=$PWD/build/kernels python3 examples/quickstart.py
```

### Without pixi

Needs x86_64 Linux (glibc >= 2.28), CUDA 12.x `nvcc`, C compiler, `make`,
`curl`, Python 3 with `numpy`, `zstandard`, `h5py`, `hdf5plugin`, `bitshuffle`:

```bash
tools/fetch_slangc.sh
CUDA_ARCH=sm_89 python3 tools/build_all.py
make
```

Runtime/test environment variables:
- `SLABITZ_KERNEL_DIR`: override kernel directory (default: `<libdir>/kernels` or `build/`).
- `SLABITZ_LIB`: override path to `libslabitz.so`.
- `SLABITZ_DEBUG_SYNC=1`: synchronize after each kernel stage.
- `PAC_LIB`: path to `pac-zstd/build/libpac.so` for encoder parity/ratio tests.
- `SLABITZ_SKIP_EXTERNAL=1`: skip `pac-zstd`'s external `check_rfc` / `build-compat` validators in `tests/run_zstd_encode.py` when not built.

## Public C API (`include/slabitz.h`)

`include/slabitz.h` provides a non-blocking, caller-owned-memory C API over the
CUDA driver backend:

- `sbz_ctx_create` / `sbz_ctx_destroy` / `sbz_ctx_error`
- `sbz_bshuf_decode_device` / `sbz_bshuf_decode` / `sbz_zstd_decode`
- `sbz_bshuf_encode_bound` / `sbz_bshuf_encode_device`
- `sbz_ctx_wait` / `sbz_ctx_query` / `sbz_ctx_alloc_count`

## Layout

| Path | Contents |
|---|---|
| `slabitz/` | Python package (`Decoder`, `decode`, `decode_device`, `encode`, `encode_device`, `encode_bound`) |
| `include/` | Public C API (`include/slabitz.h`) |
| `src/` | C host library: host frame walk, CUDA driver backend, decode & encode stage chains |
| `kernels/` | 30 Slang kernel entry points, `kernels/lib/` shared stages, and CPU references |
| `examples/` | Runnable Python demos and simple CPU-vs-GPU benchmark |
| `docs/` | User docs (`docs/*.md`), `history.md`, active tasks (`docs/dev/`), archived tasks (`docs/archive/`) |
| `tests/` | Stage, chain, fuzz, Python API, and encoder/decoder regression tests |
| `tools/` | Build scripts, `sbz.sh`, `slangc` fetch, DS header transform, synthetic data generator |
| `bench/` | L40S regression gate (`gate.py`) and pipeline benchmarks |
| `data/` | Committed pac fixtures (`data/pac/`) and compatibility corpus (`data/compat/`) |
| `third_party/` | Vendored DirectStorage headers and zstd educational decoder |

## Authors & License

- **Project direction:** @jonwright.
- **GPU decoder:** Ported and developed with Claude (Anthropic) and DeepSeek V4
  (served at ESRF).
- **GPU encoder:** Bitshuffle transpose and plane-aware LZ4/zstd encoders were
  developed by Neon and Hydrogen running in Antigravity on a home computer with
  an NVIDIA GeForce GTX 1050 Ti.
- **Code review & hardening audits:** DeepSeek V4.1 Flash identified the
  encoder safety, testing, and regression items fixed prior to merge.

Bug reports may be looked at.

MIT License, Copyright (c) 2026 European Synchrotron Radiation Facility (ESRF)
and Jon Wright; see `LICENSE` and `THIRD_PARTY_NOTICES.md`.
