Instructions to use toxzak/gemma4-e2b-exp-quant with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use toxzak/gemma4-e2b-exp-quant with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="toxzak/gemma4-e2b-exp-quant")# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("toxzak/gemma4-e2b-exp-quant", device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use toxzak/gemma4-e2b-exp-quant with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "toxzak/gemma4-e2b-exp-quant" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "toxzak/gemma4-e2b-exp-quant", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/toxzak/gemma4-e2b-exp-quant
- SGLang
How to use toxzak/gemma4-e2b-exp-quant with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "toxzak/gemma4-e2b-exp-quant" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "toxzak/gemma4-e2b-exp-quant", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "toxzak/gemma4-e2b-exp-quant" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "toxzak/gemma4-e2b-exp-quant", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use toxzak/gemma4-e2b-exp-quant with Docker Model Runner:
docker model run hf.co/toxzak/gemma4-e2b-exp-quant
File size: 5,300 Bytes
4c5636a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | # Quantization Exploration
Experiments in ultra-low bit quantization (β€0.7 bits/weight) for LLM inference, focused on Gemma 4 E2B (2B parameters).
---
## Results Summary
| Method | BPW | Compression | Est. PPL | Status |
|--------|-----|------------|----------|--------|
| **Mixed Budget g128 target 4.0** | 4.00 | 4.00x vs BF16 | 107.57 vs BF16 base 108.45 | **BF16-BASELINE-EQUIVALENT ON COLAB RUNNER** |
| **Groupwise INT4 (g128)** | 4.13 | 3.88x vs BF16 | TBD | **PIVOT FOR FP8-LIKE QUALITY** |
| **Ternary Aggressive** | 1.60 | 10x | ~66 | **RECOMMENDED** |
| Magnitude Quant (4/2-bit) | 2.13 | 7.5x | ~128 | Good quality |
| SVD Sub1Bit (90% thr) | 0.88 | 18x | BROKEN | Failed |
### Key Findings
1. **Mixed Budget g128 target 4.0 is the current quality-per-byte artifact**: 3.999 BPW, 948 MB, and BF16-baseline-equivalent perplexity on the live Colab BF16 runner. The CUDA run measured 107.5656 PPL for the mixed checkpoint vs 108.4542 PPL for the unquantized BF16 base model on the same WikiText span.
2. **Groupwise INT4 is the practical pivot** for FP8-like quality plus a future packed-kernel throughput path. See [FAST_INT4_PIVOT.md](FAST_INT4_PIVOT.md).
3. **Ternary Aggressive achieves best balance among the original low-BPW experiments** with 10x compression and ~66 estimated perplexity
4. **SVD with 90% threshold FAILS** - near-full-rank decomposition (95% of max) provides no actual compression benefit
5. **Per-layer MSE analysis** available in [EVAL_RESULTS.md](EVAL_RESULTS.md)
---
## Methods Evaluated
### 1. Ternary Aggressive (RECOMMENDED)
**Approach**: Direct ternary quantization without SVD, with importance-weighted bit allocation.
- **Configuration**: Critical layers use 2-bit, standard layers use 1-bit ternary
- **Why it works**: No dimensionality reduction avoids SVD information loss; importance weighting allocates precision where needed
- **Pros**: 10x compression, ~66 PPL, simple implementation
- **Cons**: 1.6 BPW (above 0.7 target but quality acceptable)
**Per-layer reconstruction quality (MSE)**:
| Layer | Shape | MSE |
|-------|-------|-----|
| 3 | 256x1536 | 0.081 |
| 4 | 1536x256 | 0.315 |
| 8 | 256x1536 | 0.282 |
| 50 | 256x1536 | 0.246 |
| 300 | 256x1536 | 0.288 |
| **Average** | - | **0.243** |
---
### 2. Magnitude Quant (4/2-bit)
**Approach**: Per-channel magnitude-based quantization with adaptive bit-width.
- **Configuration**: 4-bit for early (critical) layers, 2-bit for later layers
- **Why it works**: Per-channel scaling reduces quantization error within each channel
- **Pros**: Good quality, moderate compression
- **Cons**: 2.13 BPW, ~128 PPL (worse than ternary despite higher bit-width)
**Per-layer reconstruction quality (MSE)**:
| Layer | Shape | MSE |
|-------|-------|-----|
| 3 | 256x1536 | 0.344 |
| 4 | 1536x256 | 1.162 |
| 8 | 256x1536 | 0.565 |
| 50 | 256x1536 | 0.147 |
| 300 | 256x1536 | 0.250 |
| **Average** | - | **0.494** |
---
### 3. SVD Sub1Bit (90% Threshold) - FAILED
**Approach**: Low-rank factorization via SVD with 90% energy retention, followed by ternary quantization.
- **Configuration**: 90% energy threshold, 0.5-bit ternary for U/V factors, 2-bit sigma
- **Why it FAILED**: At 90% threshold, SVD retains ~95% of original rank (rank ~1155 for 1536x6144 matrices)
- **Root cause**: Near-full-rank factors + ternary quantization = noise without compression benefit
- **Pros**: Claims 18x compression
- **Cons**: Reconstruction completely broken (MSE ~1123, essentially random)
**Lesson learned**: Lower thresholds (50-70%) required for actual dimensionality reduction.
---
### 4. SVD with Lower Thresholds (Not Yet Evaluated)
Proposed but not fully tested. Would require:
- 50% threshold: Expected ~0.4 BPW, PPL 8-12
- 60% threshold: Expected ~0.5 BPW, PPL 6-10
- 70% threshold: Expected ~0.7 BPW, PPL 5-8
---
## Project Structure
```
src/
βββ quantization.py # Quantization utilities
βββ lowrank_factorization.py # SVD decomposition
βββ pack_gguf.py # GGUF export
βββ gguf_writer.py # GGUF format support
βββ __init__.py # Unified exports
scripts/
βββ quantize_svd_proper_v2.py # SVD with configurable threshold
βββ quantize_ternary_aggressive.py # Ternary quantization
βββ quantize_int8.py # INT8 baseline
βββ train_gemma_transform.py # Learned transforms
βββ eval_reconstruction.py # MSE evaluation
quantized/
βββ gemma_ternary_aggressive.pt # RECOMMENDED (362MB)
βββ gemma_magq.pt # Magnitude quant (1.8GB)
βββ gemma-4-E2B-sub1bit.pt # Broken SVD (320MB)
```
## Documentation
- [EVAL_RESULTS.md](EVAL_RESULTS.md) - Comprehensive evaluation results
- [EXPERIMENT_PLAN.md](EXPERIMENT_PLAN.md) - Experiment design and future work
## Requirements
```
torch>=2.0.0
transformers>=4.30.0
lm-eval>=0.3.0
scikit-learn>=1.3.0
numpy>=1.24.0
accelerate>=0.20.0
```
## Target Metrics
| Metric | Target | Achieved (Ternary) |
|--------|--------|-------------------|
| Average bit-width | β€0.7 bit/weight | 1.60 |
| Compression | >10x | 10x |
| WikiText-2 perplexity | β€10.5 | ~66 |
*Note: The 0.7 BPW target was not met, but ternary aggressive provides best quality among tested methods.*
|