CoolFace
Modelpublic

caiovicentino1/Gemma-4-31B-it-HLWQ-Q5-Vision

sourceHugging Faceapache-2.0updated 6mo agoView on Hugging Face
7likes20downloads
Model Card
[!IMPORTANT] Naming notice (2026-04-10). The "HLWQ" technique used in this model is being rebranded to HLWQ (Hadamard-Lloyd Weight Quantization). The change is only the name; the algorithm and the weights in this repository are unchanged. The rebrand resolves a name collision with an unrelated, earlier KV cache quantization method also named HLWQ (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 HLWQ addresses KV cache quantization with a random polar rotation. The two methods are technically distinct. Existing loaders that load this repository by ID continue to work without changes. Future model uploads will use the HLWQ name. Reference paper for this technique: arXiv:2603.29078 (v2 in preparation; v1 still uses the old name).

๐ŸงŠ Gemma-4-31B-it-HLWQ-Q5-Vision

Multimodal (Image+Text โ†’ Text) on consumer GPUs with HLWQ.

ComponentMethodResult
Text weightsHLWQ Q5 + torchao INT4~20 GB
Vision encoderBF16 (full quality)~1.1 GB
KV CacheHLWQ Q3 (5.3x compression)longer context

๐ŸŽฏ Key Results

MetricValue
VRAM21.9 GB
Speed24.9 tok/s
Text layers412 INT4
Vision layers190 BF16
CompressionBF16 62.5 GB โ†’ 21.9 GB (2.9x)
Text testโœ… "2+2 = 4"
Vision testโœ… "Golden Gate Bridge"

๐Ÿ“Š Charts

[image] [image] [image] [image]

๐Ÿ† GPU Support

GPUVRAMFits?
RTX 409024 GBโœ… Yes
RTX 509032 GBโœ… Comfortable
L424 GBโœ… Yes
A100 40GB40 GBโœ… Plenty
T416 GBโŒ Too small

๐Ÿš€ Quick Start

python
from transformers import AutoModelForMultimodalLM, AutoProcessor
import torch

MODEL = "google/gemma-4-31B-it"
processor = AutoProcessor.from_pretrained(MODEL)

# Load with streaming loader (see notebook for full code)
model = AutoModelForMultimodalLM.from_pretrained(
    MODEL, dtype=torch.bfloat16, device_map="cpu",
    attn_implementation="sdpa",
)
# Apply PQ5 dequant + INT4 per-module (text only, vision stays BF16)
# ... see notebook ...

# Image + Text inference
messages = [{
    "role": "user",
    "content": [
        {"type": "image", "url": "https://example.com/image.jpg"},
        {"type": "text", "text": "Describe this image."},
    ]
}]
inputs = processor.apply_chat_template(messages, tokenize=True, return_dict=True,
                                        return_tensors="pt", add_generation_prompt=True).to("cuda")
output = model.generate(**inputs, max_new_tokens=256)
print(processor.decode(output[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True))

๐Ÿ“Š KV Cache Compression

MethodBitsCompressiontok/s
FP16 (baseline)161.0x24.9
HLWQ Q444.0x24.9
HLWQ Q335.3x24.9
HLWQ Q228.0x24.9

๐Ÿ”ง Technical Details

  • โ€”Architecture: Gemma 4 (60 layers, 32 attn heads, 16 KV heads, head_dim=256)
  • โ€”Hybrid attention: Sliding window (1024) + global attention
  • โ€”Text quantization: Hadamard rotation (128ร—128) + Lloyd-Max Q5 + torchao INT4
  • โ€”Vision encoder: Preserved in BF16 (550M params) for full image quality
  • โ€”KV cache: Hadamard rotation (256ร—256) + Lloyd-Max Q3 + real bit-packing
  • โ€”Streaming loader: Per-module INT4 via nn.Sequential wrapper โ€” peak VRAM = final model only
  • โ€”Base model: google/gemma-4-31B-it (Apache 2.0)

๐Ÿ“– Citation

bibtex
@article{polarquant2025,
  title={HLWQ: Hadamard-Rotated Lloyd-Max Quantization for LLM Compression},
  author={Vicentino, Caio},
  journal={arXiv preprint arXiv:2603.29078},
  year={2025},
  url={https://arxiv.org/abs/2603.29078}
}

๐Ÿ”— Resources

๐Ÿ™ Acknowledgements

Built on Google's Gemma 4 (Apache 2.0). Quantization by HLWQ with torchao.


๐Ÿš€ Quick Start

Install

bash
pip install git+https://github.com/caiovicentino/polarengine-vllm.git

Load & Generate (1 line!)

python
from polarengine_vllm import HLWQModel

model = HLWQModel.from_pretrained("caiovicentino1/Gemma-4-31B-it-HLWQ-Q5-Vision")
print(model.generate("Hello, how are you?", max_new_tokens=100))

With KV Cache Compression (5.3x more context)

python
model = HLWQModel.from_pretrained("caiovicentino1/Gemma-4-31B-it-HLWQ-Q5-Vision", kv_cache_nbits=3)
# KV cache now uses 5.3x less memory โ€” fit longer conversations!
print(model.generate("Explain quantum computing in detail.", max_new_tokens=500))

Benchmark

bash
polarquant bench caiovicentino1/Gemma-4-31B-it-HLWQ-Q5-Vision --ppl --chart

Gradio Demo

bash
polarquant demo caiovicentino1/Gemma-4-31B-it-HLWQ-Q5-Vision --share

๐Ÿ“ฆ Method: HLWQ

Hadamard Rotation + Lloyd-Max Optimal Centroids

Unlike GGUF (uniform quantization), HLWQ places quantization levels where weight density is highest โ€” mathematically proven optimal for Gaussian-distributed neural network weights.

HLWQ Q5 (cos_sim > 0.996) > GGUF Q5_K_M (~0.99) at same size

๐Ÿ”— Links