caiovicentino/polarengine-vllm

PolarEngine: vLLM plugin for PolarQuant quantized LLM inference — 75% FP16 speed at 2.3x less VRAM

35

stars

12

commits

Python

primary language

Apr 13, 2026

updated

README

[!IMPORTANT] Naming notice (2026-04-10). The "PolarQuant" technique referenced throughout this README is being rebranded to HLWQ (Hadamard-Lloyd Weight Quantization). The change is only the name; the algorithm and the code in this repository are unchanged.

The rebrand resolves a name collision with an unrelated, earlier KV cache quantization method also named PolarQuant (Han et al., arXiv:2502.02617, 2025). HLWQ addresses weight quantization with a deterministic Walsh-Hadamard rotation and Lloyd-Max scalar codebook; Han et al.'s PolarQuant addresses KV cache quantization with a random polar transformation. The two methods are technically distinct.

The PyPI package polarquant will be superseded by hlwq; the polarquant package and this repository's name will continue to work during the transition period. Reference paper: arXiv:2603.29078 (v2 in preparation under the new name).

PolarEngine for vLLM

Custom quantization plugin for vLLM using PolarQuant -- optimal Gaussian quantization via Walsh-Hadamard rotation + Lloyd-Max centroids.

arXiv preprint: arXiv:2603.29078

Recommended path: For best quality-per-VRAM, use PolarQuant Q5 + torchao INT4 (43.1 tok/s, 6.5 GB VRAM, PPL 6.56). PolarEngine's custom Triton kernel is available for environments where torchao is not an option.


Results (Qwen3.5-9B, RTX PRO 6000 Blackwell)

Methodtok/sVRAMPPL (WikiText-2)Notes
FP16 baseline45.717.9 GB6.37Reference
PolarQuant Q5 + torchao INT443.16.5 GB6.56Recommended
torchao INT4 (absmax)43.36.3 GB6.68
BnB NF434.67.7 GB~6.7
PolarEngine v4 (Triton)34.27.9 GB6.89Custom kernel
PolarQuant Q5 dequant FP1645.918.1 GB6.39Near-lossless
PolarQuant MLX Q419.74.8 GB6.90Mac mini M4 16 GB

PolarQuant Ablation (Q5, Qwen3.5-9B)

ConfigurationPPLDelta vs FP16
Absmax Q5 (baseline)6.9030+0.53
+ Hadamard rotation6.4010+0.03
+ Lloyd-Max centroids6.9139+0.54
+ Both (PolarQuant Q5)6.3909+0.02

Hadamard rotation accounts for 98% of the improvement. The Walsh-Hadamard transform makes weight distributions approximately Gaussian, enabling near-optimal uniform quantization.


How It Works

PolarQuant quantization:

  1. Normalize weight blocks by L2 norm
  2. Rotate via Walsh-Hadamard Transform (makes weights Gaussian -- 98% of quality gain)
  3. Quantize using Lloyd-Max optimal centroids for N(0,1)
  4. Store codes (int8/nibble-packed) + per-block norms (fp16)

Inference keeps weights quantized in GPU VRAM:

  • Triton kernel does centroid lookup + GEMV in one operation
  • FWHT applied to input (not weights) -- 25x faster via matmul
  • FWHT cached across Q/K/V projections (69x total speedup)
  • INT4 nibble packing for Q3/Q4 layers (36% VRAM savings)

Installation

pip install polarengine-vllm

Or from source:

git clone https://github.com/caiovicentino/polarengine-vllm
cd polarengine-vllm
pip install -e .

Optional CUDA kernels (for CUDA graph support):

pip install -e ".[cuda]"

Quick Start

from transformers import AutoModelForCausalLM, AutoTokenizer
from torchao.quantization import quantize_, Int4WeightOnlyConfig
import torch

# Load PolarQuant Q5 model (auto-dequantizes to FP16)
model = AutoModelForCausalLM.from_pretrained(
    "caiovicentino1/Qwen3.5-9B-PolarQuant-Q5",
    dtype=torch.float16, device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained("caiovicentino1/Qwen3.5-9B-PolarQuant-Q5")

# Apply torchao INT4 for fast inference (43 tok/s, 6.5 GB VRAM)
quantize_(model, Int4WeightOnlyConfig(group_size=128))

inputs = tokenizer("Hello!", return_tensors="pt").to(model.device)
output = model.generate(**inputs, max_new_tokens=100)
print(tokenizer.decode(output[0], skip_special_tokens=True))

Option B: PolarEngine Triton Kernel

1. Quantize a model

python -m polarengine_vllm.quantize \
    --model Qwen/Qwen3.5-9B \
    --output ./Qwen3.5-9B-PolarEngine/

2. Serve with vLLM

vllm serve ./Qwen3.5-9B-PolarEngine/ --quantization polarengine

3. Use from Python

from vllm import LLM
model = LLM("./Qwen3.5-9B-PolarEngine/", quantization="polarengine")
output = model.generate("Explain quantum computing:")

Mixed-Bit Assignment

Layer TypeBitsRationale
gate/up proj (MLP)Q3Tolerant to quantization
down proj (MLP)Q4Moderate sensitivity
Q/K/V proj (Attention)Q5Higher precision for attention
O proj (Attention)Q6Output projection needs quality
EmbeddingsQ5Large, benefits from compression
LM HeadQ6Critical for token prediction
Norms, biases, routerFP16Too small to quantize

Architecture

Input x -> Pad -> FWHT(x) via matmul -> Triton GEMV Kernel -> Output
                  ^                        ^
          H128 (cached, 64KB)    codes + norms + centroids
                                 (quantized, in VRAM)

Published Models

ModelLinkNotes
Qwen3.5-9B PolarQuant Q5HuggingFaceRecommended, 9.1 GB
Qwen3.5-9B PolarQuant MLX 4-bitHuggingFaceApple Silicon
Qwen3.5-9B PolarEngine v4HuggingFaceTriton kernel

See the main EOQ repository for additional models and full documentation.


Citation

@article{vicentino2026polarquant,
    title={PolarQuant: Near-Lossless LLM Quantization via Walsh-Hadamard Rotation
           and Entropy-Optimal Coding},
    author={Vicentino, Caio},
    journal={arXiv preprint arXiv:2603.29078},
    year={2026}
}

License

Apache 2.0

Contributors

caiovicentino

12 commits

caiovicentino/polarengine-vllm

PolarEngine: vLLM plugin for PolarQuant quantized LLM inference — 75% FP16 speed at 2.3x less VRAM

35

stars

12

commits

Python

primary language

Apr 13, 2026

updated

README

[!IMPORTANT] Naming notice (2026-04-10). The "PolarQuant" technique referenced throughout this README is being rebranded to HLWQ (Hadamard-Lloyd Weight Quantization). The change is only the name; the algorithm and the code in this repository are unchanged.

The rebrand resolves a name collision with an unrelated, earlier KV cache quantization method also named PolarQuant (Han et al., arXiv:2502.02617, 2025). HLWQ addresses weight quantization with a deterministic Walsh-Hadamard rotation and Lloyd-Max scalar codebook; Han et al.'s PolarQuant addresses KV cache quantization with a random polar transformation. The two methods are technically distinct.

The PyPI package polarquant will be superseded by hlwq; the polarquant package and this repository's name will continue to work during the transition period. Reference paper: arXiv:2603.29078 (v2 in preparation under the new name).

PolarEngine for vLLM

Custom quantization plugin for vLLM using PolarQuant -- optimal Gaussian quantization via Walsh-Hadamard rotation + Lloyd-Max centroids.

arXiv preprint: arXiv:2603.29078

Recommended path: For best quality-per-VRAM, use PolarQuant Q5 + torchao INT4 (43.1 tok/s, 6.5 GB VRAM, PPL 6.56). PolarEngine's custom Triton kernel is available for environments where torchao is not an option.


Results (Qwen3.5-9B, RTX PRO 6000 Blackwell)

Methodtok/sVRAMPPL (WikiText-2)Notes
FP16 baseline45.717.9 GB6.37Reference
PolarQuant Q5 + torchao INT443.16.5 GB6.56Recommended
torchao INT4 (absmax)43.36.3 GB6.68
BnB NF434.67.7 GB~6.7
PolarEngine v4 (Triton)34.27.9 GB6.89Custom kernel
PolarQuant Q5 dequant FP1645.918.1 GB6.39Near-lossless
PolarQuant MLX Q419.74.8 GB6.90Mac mini M4 16 GB

PolarQuant Ablation (Q5, Qwen3.5-9B)

ConfigurationPPLDelta vs FP16
Absmax Q5 (baseline)6.9030+0.53
+ Hadamard rotation6.4010+0.03
+ Lloyd-Max centroids6.9139+0.54
+ Both (PolarQuant Q5)6.3909+0.02

Hadamard rotation accounts for 98% of the improvement. The Walsh-Hadamard transform makes weight distributions approximately Gaussian, enabling near-optimal uniform quantization.


How It Works

PolarQuant quantization:

  1. Normalize weight blocks by L2 norm
  2. Rotate via Walsh-Hadamard Transform (makes weights Gaussian -- 98% of quality gain)
  3. Quantize using Lloyd-Max optimal centroids for N(0,1)
  4. Store codes (int8/nibble-packed) + per-block norms (fp16)

Inference keeps weights quantized in GPU VRAM:

  • Triton kernel does centroid lookup + GEMV in one operation
  • FWHT applied to input (not weights) -- 25x faster via matmul
  • FWHT cached across Q/K/V projections (69x total speedup)
  • INT4 nibble packing for Q3/Q4 layers (36% VRAM savings)

Installation

pip install polarengine-vllm

Or from source:

git clone https://github.com/caiovicentino/polarengine-vllm
cd polarengine-vllm
pip install -e .

Optional CUDA kernels (for CUDA graph support):

pip install -e ".[cuda]"

Quick Start

from transformers import AutoModelForCausalLM, AutoTokenizer
from torchao.quantization import quantize_, Int4WeightOnlyConfig
import torch

# Load PolarQuant Q5 model (auto-dequantizes to FP16)
model = AutoModelForCausalLM.from_pretrained(
    "caiovicentino1/Qwen3.5-9B-PolarQuant-Q5",
    dtype=torch.float16, device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained("caiovicentino1/Qwen3.5-9B-PolarQuant-Q5")

# Apply torchao INT4 for fast inference (43 tok/s, 6.5 GB VRAM)
quantize_(model, Int4WeightOnlyConfig(group_size=128))

inputs = tokenizer("Hello!", return_tensors="pt").to(model.device)
output = model.generate(**inputs, max_new_tokens=100)
print(tokenizer.decode(output[0], skip_special_tokens=True))

Option B: PolarEngine Triton Kernel

1. Quantize a model

python -m polarengine_vllm.quantize \
    --model Qwen/Qwen3.5-9B \
    --output ./Qwen3.5-9B-PolarEngine/

2. Serve with vLLM

vllm serve ./Qwen3.5-9B-PolarEngine/ --quantization polarengine

3. Use from Python

from vllm import LLM
model = LLM("./Qwen3.5-9B-PolarEngine/", quantization="polarengine")
output = model.generate("Explain quantum computing:")

Mixed-Bit Assignment

Layer TypeBitsRationale
gate/up proj (MLP)Q3Tolerant to quantization
down proj (MLP)Q4Moderate sensitivity
Q/K/V proj (Attention)Q5Higher precision for attention
O proj (Attention)Q6Output projection needs quality
EmbeddingsQ5Large, benefits from compression
LM HeadQ6Critical for token prediction
Norms, biases, routerFP16Too small to quantize

Architecture

Input x -> Pad -> FWHT(x) via matmul -> Triton GEMV Kernel -> Output
                  ^                        ^
          H128 (cached, 64KB)    codes + norms + centroids
                                 (quantized, in VRAM)

Published Models

ModelLinkNotes
Qwen3.5-9B PolarQuant Q5HuggingFaceRecommended, 9.1 GB
Qwen3.5-9B PolarQuant MLX 4-bitHuggingFaceApple Silicon
Qwen3.5-9B PolarEngine v4HuggingFaceTriton kernel

See the main EOQ repository for additional models and full documentation.


Citation

@article{vicentino2026polarquant,
    title={PolarQuant: Near-Lossless LLM Quantization via Walsh-Hadamard Rotation
           and Entropy-Optimal Coding},
    author={Vicentino, Caio},
    journal={arXiv preprint arXiv:2603.29078},
    year={2026}
}

License

Apache 2.0

Contributors

caiovicentino

12 commits

Languages

Python

92.2%

Cuda

2.8%

Jupyter Notebook

2.7%

C

2.3%