Qwantize
Optimal quantization methods for block-scaled formats.
Formats
- NVFP4 — FP4 E2M1 with FP8 E4M3 scales (block sizes 16, 32)
- MXFP4 — FP4 E2M1 with UE8M0 (power-of-2) scales (block sizes 16, 32)
Methods
Each format supports multiple scale selection strategies:
| Method | Description |
|---|---|
| Naive | Standard heuristic: s = snap(amax / Q_MAX) |
| SSE-Optimal | Bounded search minimizing sum of squared quantization error |
| Hessian-Optimal | Bounded search minimizing Hessian-weighted error r^T H r using activations |
All methods have both pure-PyTorch (reference) and Triton (GPU-accelerated) implementations.
Install
pip install qwantize
Requires PyTorch (>=2.0) and Triton (>=3.0).
Usage
from qwantize import nvfp4_naive, nvfp4_optimal, nvfp4_dequantize, compute_metrics
# W has shape (..., block_size) where block_size is 16 or 32
W_blocked = W.reshape(M, K // 32, 32)
# Quantize: returns (scales, quants)
scales, quants = nvfp4_optimal(W_blocked, dim=-1)
# Dequantize
W_dq = nvfp4_dequantize(scales, quants, dim=-1)
# Or get dequantized output directly
scales, quants, W_dq = nvfp4_optimal(W_blocked, dim=-1, return_dequant=True)
# Compute metrics: ||Q(W)-W||/||W|| and ||XW_q^T - XW^T||/||XW^T||
metrics = compute_metrics(W, W_dq.reshape(M, K), X)
Triton-accelerated versions:
from qwantize import nvfp4_optimal_triton, nvfp4_optimal_hessian_triton
scales, quants, W_dq = nvfp4_optimal_triton(W_blocked, dim=-1, return_dequant=True)
# Hessian-aware (requires activations X)
scales, quants, W_dq = nvfp4_optimal_hessian_triton(W_blocked, dim=-1, return_dequant=True, X=X)
Benchmarks
Benchmarked on the down_proj weight of the first decoder layer from Qwen3-4B, with activations from WikiText-2 (max_seq_len=512, num_samples=2048).
python bench/full_bench.py
NVFP4 (block size 16)
| Method | Weight Error | Output Error | Triton Speedup |
|---|---|---|---|
| Naive | 10.05% | 6.89% | 1.7x |
| SSE-Optimal | 8.74% | 6.04% | 7.0x |
| H-Optimal | 9.35% | 5.31% | 1.8x |
MXFP4 (block size 16)
| Method | Weight Error | Output Error | Triton Speedup |
|---|---|---|---|
| Naive | 11.77% | 8.48% | 1.7x |
| SSE-Optimal | 11.02% | 7.67% | 33x |
| H-Optimal | 11.10% | 7.62% | — |
Documentation
Full documentation: qwantize.readthedocs.io
Build locally:
pip install -r docs/requirements.txt
cd docs && make html
Contact
- Author: Ayoub Ghriss, research@ayghri.me
Metadata
Release files for qwantize 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| qwantize-0.1.1.tar.gz | 15.1 kB | Without provenance | Details for qwantize-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| qwantize-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for qwantize-0.1.1-py3-none-any.whl |
Total release size: 36.8 kB
Release files / qwantize-0.1.1.tar.gz
| Download URL | qwantize-0.1.1.tar.gz |
|---|---|
| Size | 15.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ef1de56020bc042e9c1a4fd25df046870584f3d5ab20e3e71fdfdc7ff035f712
|
|
BLAKE2b-256 checksum How to use checksums |
b6cbad91163ed7bbfe313c994779b246a2ae7dbfc66ccd63c7016bdfb7fadf6c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.3.2 CPython/3.13.11 Linux/6.12.74-gentoo-x86_64
|
Release files / qwantize-0.1.1-py3-none-any.whl
| Download URL | qwantize-0.1.1-py3-none-any.whl |
|---|---|
| Size | 21.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1e9676a5a1322b1fbdc70990598a65d242ce27050c16324cf82c2389adac27c6
|
|
BLAKE2b-256 checksum How to use checksums |
7bf9a9af2ca40f3260250ff0c16cc7015d5ef770a6ec0ba14e6d2fd04225fd19
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.3.2 CPython/3.13.11 Linux/6.12.74-gentoo-x86_64
|
Security for qwantize 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.