Metadata-Version: 2.4
Name: quf
Version: 0.1.0
Summary: QUF: Quantized Unified Format for 4-bit quantized decoder-only LLMs
Author: vtrsharvin-sj
License-Expression: MIT
Project-URL: Homepage, https://github.com/vtrsharvin-sj/Quantized-Model-Format
Project-URL: Repository, https://github.com/vtrsharvin-sj/Quantized-Model-Format
Project-URL: Issues, https://github.com/vtrsharvin-sj/Quantized-Model-Format/issues
Keywords: quantization,llm,gptq,4-bit,pytorch
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: Microsoft :: Windows
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.24
Requires-Dist: torch>=2.7
Provides-Extra: cuda
Provides-Extra: convert
Requires-Dist: torch>=2.7; extra == "convert"
Requires-Dist: transformers; extra == "convert"
Requires-Dist: datasets; extra == "convert"
Requires-Dist: accelerate; extra == "convert"
Requires-Dist: gigatoken; extra == "convert"
Requires-Dist: tokenizers; extra == "convert"
Requires-Dist: safetensors; extra == "convert"
Requires-Dist: huggingface_hub; extra == "convert"
Provides-Extra: test
Requires-Dist: pytest>=7; extra == "test"
Requires-Dist: hypothesis>=6; extra == "test"
Dynamic: license-file

﻿# QUF: Quantized Unified Format

QUF is a single-file container (`.quf`) for a 4-bit quantized decoder-only LLM. One file holds the model config, tokenizer files, quantization scheme and all tensors. This package provides a reference writer and reader, a native GPTQ converter, a runtime with CPU and CUDA backends, and a benchmark suite.

QUF runs natively on Windows (and any platform with PyTorch). No WSL or Linux environment is needed.

Supported architectures: `Qwen2ForCausalLM` (first), with `Qwen3ForCausalLM` addable through the architecture allow-list. Weights are 4-bit, group size 128, packed 8 per `int32`, with fp16 scales. Embeddings and `lm_head` stay fp16.

## Quickstart

```
pip install quf
quf convert Qwen/Qwen2.5-0.5B out.quf
quf run out.quf --prompt "The capital of France is" --max-new-tokens 32 --temperature 0
quf bench out.quf --out results.json
```

`quf convert` needs the convert extra: `pip install "quf[convert]"`.

## Status: what is and is not verified

| Component | Status |
| --- | --- |
| Format writer/reader, quantizer, packer, CPU backend | Implemented. Covered by the CPU test suite. |
| Native GPTQ converter (`quf convert`) | Verified on native Windows with Qwen/Qwen2.5-0.5B ([docs/RESULTS.md](docs/RESULTS.md)). |
| `torch_gpu` backend on RTX 5080 Laptop (compute capability 12.0) | Verified: `quf run` and `quf bench` work on CUDA with a real converted Qwen2.5-0.5B. |
| `torch_fused` backend | Verified, but slower than `torch_gpu` on this GPU (uses less memory). |
| gigatoken on Windows | Verified: gigatoken 0.11.0 installs and runs, 36.8x faster than HF `tokenizers` on WikiText-2 with identical ids. |
| Perplexity target (3 percent) | **Measured and missed** on Qwen2.5-0.5B: +8.47% and +7.41% over fp16. |
| Marlin backend | Optional, never selected automatically, not verified on this hardware. |

Details are in [docs/ENV.md](docs/ENV.md) and [docs/RESULTS.md](docs/RESULTS.md).

## Installation

Requires Python 3.10 or newer.

```
pip install quf             # core: format, quantizer, runtime (torch is a base dependency)
pip install "quf[cuda]"     # same as core; kept so the extra name resolves
pip install "quf[convert]"  # adds transformers, datasets, accelerate, gigatoken, tokenizers,
                            # safetensors, huggingface_hub for `quf convert`
pip install "quf[test]"     # pytest and hypothesis
```

From a source checkout, use `pip install .` and the same extras.

The `quf` console script is installed with the package. Run `quf --help` to list subcommands: `info`, `convert`, `run`, `bench`.

For a CUDA build of PyTorch on an RTX 50-series GPU, install a CUDA 12.8 wheel (the version recorded in ENV.md is `2.11.0+cu128`). With no GPU, everything works on CPU.

## Convert

`quf convert` loads a Hugging Face Qwen checkpoint with transformers and quantizes it with QUF's own GPTQ implementation (4 bit, group size 128). It runs on native Windows, Linux and macOS.

```
quf convert Qwen/Qwen2.5-0.5B out.quf
quf convert ./local-model out.quf --dataset calib.txt --samples 128 --seq-len 512 --seed 0
```

Options: `--dataset` (text file, wikitext config or HF dataset id; default `wikitext-2-raw-v1`), `--samples` (128), `--seq-len` (512), `--seed` (0), `--overwrite`. The command prints the output size and compression ratio. It refuses to replace an existing output unless `--overwrite` is given, rejects unsupported architectures before heavy work, and deletes a partial file if conversion fails.

Inspect the result:

```
quf info out.quf
```

This prints the model config, quantization scheme, tensor count and file size. A corrupt file exits with status 1 and an error message.

## Run

```
quf run out.quf --prompt "Hello" --max-new-tokens 64 --temperature 0
```

Options: `--max-new-tokens` (64), `--temperature` (0 means greedy), `--top-k`, `--top-p`, `--seed` (0), `--no-stream`, `--device {auto,cpu,cuda}` (auto), `--backend {auto,cached,fused,marlin}` (auto), `--prefer-fused`.

Backends:

- `cpu`: used when no NVIDIA GPU is present.
- `torch_gpu` (`--backend cached`): dequantizes weights to fp16 at load. Default on GPU when the weights fit in free GPU memory.
- `torch_fused` (`--backend fused`): keeps packed int4 on the GPU. Chosen automatically when the cached weights do not fit, or with `--prefer-fused`.
- `marlin`: optional, only with `--backend marlin`, never automatic. Unverified on this hardware.

The choice and its reason are logged to stderr. `--device cuda` without an NVIDIA GPU exits with status 1. A prompt that exceeds the context length and GPU out-of-memory also exit with status 1.

## Benchmark

```
quf bench out.quf --out results.json
quf bench out.quf --out results.json --fp16-ref Qwen/Qwen2.5-0.5B --gguf model-q4.gguf --device auto
```

The results JSON records WikiText perplexity, file size, tokens per second, backend, device name and software versions. With `--fp16-ref`, the relative perplexity increase over fp16 is printed next to the documented target of at most 3 percent. The target is reported, not enforced, and was missed on Qwen2.5-0.5B (see below). `--gguf` adds a GGUF Q4 comparison. An lm-evaluation-harness adapter is in `quf/bench/lm_eval_adapter.py`.

## Measured results

Qwen/Qwen2.5-0.5B, converted natively on Windows (4 bit, group size 128), RTX 5080 Laptop GPU, PyTorch 2.11.0+cu128, WikiText-2 raw. Full details and raw JSON: [docs/RESULTS.md](docs/RESULTS.md).

| Metric | Value |
| --- | --- |
| fp16 perplexity | 13.07 |
| QUF perplexity (32 x 512 calibration) | 14.18 (+8.47%) |
| QUF perplexity (128 x 1024 calibration) | 14.04 (+7.41%) |
| 3% target | **Missed** (+7 to +8%) |
| File size | 469,483,072 bytes, about 2.1x smaller than fp16 weights (about 988 MB). Embeddings and `lm_head` stay fp16, which limits the ratio on a small model. |
| Decode, CPU | 9.6 tokens/s |
| Decode, RTX 5080 `torch_gpu` (default) | 20.7 tokens/s |
| Decode, RTX 5080 `torch_fused` | 9.1 tokens/s (slower, less memory) |
| gigatoken vs HF tokenizers | 0.23 s vs 8.44 s (36.8x, identical ids) |

The cause of the perplexity gap was not investigated. Qwen2.5-1.5B was not run. Speeds are single measurements and the decode loop is not optimized.

## Documentation

- [docs/FORMAT.md](docs/FORMAT.md): byte layout, header fields, tensor naming, dtypes and the canonical Packed_Int4 order.
- [docs/ENV.md](docs/ENV.md): environment results for native Windows and the backend selection rules.
- [docs/RESULTS.md](docs/RESULTS.md): measured accuracy, size and speed.

README.md, docs/ENV.md and docs/FORMAT.md ship with the package: they are in the source distribution and are installed to `share/doc/quf` by the wheel.

## Tests

```
pytest -m "not gpu and not convert"   # CPU suite, no NVIDIA GPU needed
pytest -m gpu                         # needs an NVIDIA GPU
pytest -m convert                     # needs the convert extra and downloads a model
```

Tests marked `gpu` are skipped automatically when no NVIDIA GPU is available.

## Links and license

Source and issues: https://github.com/vtrsharvin-sj/Quantized-Model-Format

Released under the MIT license. See [LICENSE](LICENSE).
