Metadata-Version: 2.4
Name: piper-kernels
Version: 0.9.0rc4
Summary: Reusable PyTorch inference operators and Triton kernels.
Keywords: inference,pytorch,quantization,triton
License-Expression: Apache-2.0
License-File: LICENSE
License-File: NOTICE
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Dist: torch>=2.15
Requires-Dist: triton-windows>=3.9,<3.10 ; sys_platform == 'win32'
Requires-Python: >=3.14
Project-URL: Homepage, https://github.com/Boffee/piper-kernels
Project-URL: Repository, https://github.com/Boffee/piper-kernels.git
Project-URL: Issues, https://github.com/Boffee/piper-kernels/issues
Project-URL: Changelog, https://github.com/Boffee/piper-kernels/blob/main/CHANGELOG.md
Description-Content-Type: text/markdown

# Piper Kernels

Quantized attention, linear, and convolution operators for PyTorch inference.
Piper Kernels combines compact weight formats and GPU kernels with direct fused
operators (`projected_attention`, `ffn`, `linear_projections`, `conv3d`) that
avoid intermediate tensors and repeated preparation.
Portable PyTorch references define the corresponding numerical operations.

Use the operators directly or install quantized weights in PyTorch modules.
Applications own model loading, checkpoint naming, and device placement;
[Piper Offload](https://github.com/Boffee/piper-offload) uses these weight formats
for its model runtime.

## Installation

Requires Python 3.14 or newer, PyTorch 2.15 or newer, and Triton 3.9.

```bash
pip install piper-kernels
```

The package includes all quantized weight formats (ConvRot INT8, ConvRot Trellis,
and storage-only GGUF) and requires Triton unconditionally:

- **Linux:** install the CUDA or ROCm PyTorch build and its matching Triton 3.9
  package first (`triton` on CUDA or `pytorch-triton-rocm` on ROCm).
- **64-bit Windows:** `pip install piper-kernels` installs Triton 3.9 automatically
  through [`triton-windows`](https://github.com/triton-lang/triton-windows).
  Execution needs Windows 10 or 11, a supported GPU with a current driver, and
  the Visual C++ Redistributable for Visual Studio 2015–2022. For NVIDIA,
  `triton-windows` bundles the CUDA toolchain and TinyCC; no separate CUDA toolkit
  or Visual Studio installation is needed. For AMD, use
  [TheRock's ROCm PyTorch build](https://github.com/ROCm/TheRock/blob/main/RELEASES.md)
  and its matching GPU device packages; see
  [AMD validation coverage](docs/environments.md#rocm).

Quantized compute operators (`piper_kernels.attention`, `piper_kernels.ops`,
in-place weight updates) and GPU-driven Triton collectives
(`piper_kernels.collectives`) require a supported CUDA or ROCm GPU and raise
`ValueError` on CPU or unsupported GPUs before mutating caller-owned outputs,
KV pools, or state. Top-level `piper_kernels` exports the primary forward-pass
operators alongside the four domain namespaces (`attention`, `ops`, `weights`,
`collectives`); weight formats, storage/sharding protocols, and format codecs
live under `piper_kernels.weights` (`weights.trellis`, `weights.gguf`). CPU
support is retained for weight construction/storage/offload (`from_hp`,
`from_quantized`, `from_storage`, `as_storage`, `dequantize`, sharding,
row-slice planning, pinning), `PiperKVChunkPool` / `kv_chunk_storage_specs`
host staging, `merge_attention_lse`, `sparse_piper_coarse_residual`, row-only
quantized `F.embedding` lookups (including `mmap` host tables), plain dense
`torch.Tensor` linear/FFN/Conv3D calls, and `meta`/`FakeTensorMode` tracing.

## Quick start

This runs dense Piper Attention on a supported GPU (NVIDIA SM8x/SM12x or AMD
RDNA4). Inputs use `[batch, heads, sequence, head_dim]` layout:

```python
import torch
from piper_kernels import piper_attention

device = "cuda"
query, key, value = (
    torch.randn(1, 2, 128, 64, device=device, dtype=torch.bfloat16) for _ in range(3)
)

with torch.inference_mode():
    output = piper_attention(query, key, value, is_causal=False)

print(output.shape)  # torch.Size([1, 2, 128, 64])
```

Quantized attention approximates floating-point attention, and fused
projection/attention operators can change rounding. CPU and unsupported GPUs
raise `ValueError` (`meta` and CUDA `FakeTensorMode` shape tracing remain
supported). Measure numerical quality and full-operator latency on your
workload; see [attention](docs/attention.md) for input and backend support.

## Guides

| Task | Read |
|---|---|
| Choose and use dense, KV-decode, or sparse Piper attention | [Attention](docs/attention.md) |
| Construct, load, run, update, or shard quantized weights; run fused operators | [Weights and quantized operators](docs/weights.md) |
| Understand implementation boundaries, extend kernels, or run checks | [Development](docs/development.md) |
| Set up CUDA/ROCm environments, caches, and regressions | [Environments](docs/environments.md) |
| Measure performance and quality, tune, or inspect generated kernels | [Benchmarks](benchmarks/README.md) |

These docs describe the checked-out revision. Use the matching Git tag for an
installed release. [AGENTS.md](AGENTS.md) gives repository instructions for coding agents;
[VERSIONING.md](VERSIONING.md) covers compatibility and releases, and
[CHANGELOG.md](CHANGELOG.md) records changes between releases.

Licensed under the [Apache License, Version 2.0](LICENSE).
