File size: 10,387 Bytes
e18b8b5 | 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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | ---
license: mit
base_model:
- microsoft/Phi-3-medium-128k-instruct
base_model_relation: quantized
---
# Microsoft Phi-3-medium-128k-instruct - TevunahAi Ultra-Hybrid GPTQ with EoRA
## Model Details
| Property | Value |
|----------|-------|
| **Base Model** | microsoft/Phi-3-medium-128k-instruct |
| **Architecture** | Phi-3 Dense Decoder-only Transformer |
| **Parameters** | 14B |
| **Context Length** | 128,000 tokens |
| **Training Data** | 4.8T tokens (10% multilingual) |
| **Quantization** | TevunahAi Ultra-Hybrid GPTQ + EoRA |
| **Original Size** | ~28 GB (BF16) |
| **Quantized Size** | ~8-10 GB |
| **Compression** | ~65-70% reduction |
| **License** | MIT |
## Architecture Breakdown
Microsoft Phi-3-medium-128k-instruct is a long-context language model trained on 4.8 trillion tokens:
### Layer Composition (40 total layers)
- **40 Transformer Decoder Layers**: Dense attention architecture
- **40 Attention Heads**: GQA with 10 KV heads (4:1 ratio)
- **Hidden Size**: 5,120
- **Intermediate Size**: 17,920
- **Vocab Size**: 32,064
- **SiLU (Swish) Activation**: Smooth activation function
- **RMSNorm + RoPE**: theta=10000 with SU scaling for 128K context
- **Fused Projections**: qkv_proj and gate_up_proj for efficiency
### Why This Matters
- **128K token context**: Process extremely long documents, entire codebases, books
- **4.8T training tokens**: Extensive pretraining for broad capabilities
- **10% multilingual**: Support for multiple languages
- **SFT + DPO alignment**: Supervised fine-tuning with preference optimization
- **MIT License**: Permissive open source license
## Quantization Strategy
### TevunahAi Ultra-Hybrid Mixed-Precision with EoRA Error Recovery
This quantization uses **EoRA (Error-optimized Low-Rank Adaptation)** - NVIDIA's technique for recovering quantization error through learned low-rank adapters applied during the quantization process.
| Component | Precision | EoRA Rank | Rationale |
|-----------|-----------|-----------|-----------|
| Attention qkv_proj (all 40 layers) | INT8 | 128 | Critical for long-context quality |
| Attention o_proj (all 40 layers) | INT8 | 128 | Output projection quality |
| MLP gate_up_proj (layers 0-31) | INT4 | 128 | Maximum compression in early/middle layers |
| MLP down_proj (layers 0-31) | INT4 | 128 | Compression with error recovery |
| MLP gate_up_proj (layers 32-39) | INT8 | 128 | Higher precision near output |
| MLP down_proj (layers 32-39) | INT8 | 128 | Output quality critical |
| Embeddings | FP16 | - | Preserved for token accuracy |
| LM Head | FP16 | - | Preserved for output quality |
### Why INT8 Attention Everywhere?
For a 128K context model, attention quality is paramount:
- **Long-range dependencies**: Attention must work accurately over 128K tokens
- **INT8 preserves precision**: Critical for maintaining context coherence
- **EoRA-128 recovery**: Additional error correction on all attention layers
### Why Tiered MLP Precision?
- **Layers 0-31 (INT4)**: Early/middle layers are more compressible
- **Layers 32-39 (INT8)**: Final 8 layers directly affect output quality
- **EoRA-128 on all**: Error recovery maintains quality across compression levels
### Calibration
- **1,500 samples** (6x industry standard of 256)
- **2,048 sequence length** (optimized for 32GB VRAM)
- **Diverse datasets**: Orca-Math, Code-Feedback, UltraChat, SlimOrca
- **Long-context focus**: Calibration designed for extended context use
## Performance Benchmarks
### Qualitative Tests (7/7 passed)
| Test | Result | Details |
|------|--------|---------|
| Basic Inference | โ
PASS | Coherent self-introduction |
| Math Reasoning | โ
PASS | Train speed problem - 240 miles correct |
| Code Generation | โ
PASS | Sieve of Eratosthenes - 6/6 elements |
| Long Context | โ
PASS | Multi-fact retrieval (2/3 correct) |
| Multilingual | โ
PASS | French + Spanish translations |
| Logic Puzzle | โ
PASS | Box labeling - correct reasoning |
| Summarization | โ
PASS | AI impact - 5/5 topics covered |
### Quantized Model Benchmarks (lm-eval-harness, 0-shot)
| Task | Score | Metric | Stderr |
|------|-------|--------|--------|
| HellaSwag | **71.00%** | acc_norm | ยฑ4.56% |
| Winogrande | **76.00%** | acc | ยฑ4.29% |
| ARC-Challenge | **48.00%** | acc_norm | ยฑ5.02% |
| TruthfulQA MC2 | **50.93%** | acc | ยฑ4.39% |
*Quick test with 100 samples per task. Full benchmark recommended for production validation.*
### Inference Performance
| Metric | Value |
|--------|-------|
| **VRAM Usage** | 10.22 GB |
| **Generation Speed** | 10-23 tok/s |
| **Tests Passed** | 7/7 |
*Note: Speed varies based on response length. No flash attention used during testing.*
## Usage
### GPTQModel (Recommended)
```python
from gptqmodel import GPTQModel
from transformers import AutoTokenizer
model = GPTQModel.from_quantized(
"TevunahAi/Phi-3-medium-128k-instruct-TevunahAi-GPTQ",
device_map="auto",
trust_remote_code=True,
)
tokenizer = AutoTokenizer.from_pretrained(
"TevunahAi/Phi-3-medium-128k-instruct-TevunahAi-GPTQ",
trust_remote_code=True
)
messages = [
{"role": "user", "content": "Explain the theory of relativity in simple terms."},
]
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
)
inputs = tokenizer([text], return_tensors="pt").to(model.device)
outputs = model.generate(
**inputs,
max_new_tokens=500,
temperature=0.0,
do_sample=False,
)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
```
### Transformers
```python
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
model = AutoModelForCausalLM.from_pretrained(
"TevunahAi/Phi-3-medium-128k-instruct-TevunahAi-GPTQ",
device_map="auto",
trust_remote_code=True
)
tokenizer = AutoTokenizer.from_pretrained(
"TevunahAi/Phi-3-medium-128k-instruct-TevunahAi-GPTQ",
trust_remote_code=True
)
pipe = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
)
messages = [
{"role": "user", "content": "Write a Python function to calculate fibonacci numbers."},
]
output = pipe(messages, max_new_tokens=500, return_full_text=False)
print(output[0]['generated_text'])
```
### vLLM (Production)
```bash
pip install -U vllm
vllm serve TevunahAi/Phi-3-medium-128k-instruct-TevunahAi-GPTQ \
--max-num-seqs 4 \
--max-model-len 32768 \
--trust-remote-code
```
### Chat Format
```
<|user|>
Your question here<|end|>
<|assistant|>
```
### Installation
```bash
pip install gptqmodel transformers>=4.48
```
## Long-Context Capabilities
Phi-3-medium-128k excels at tasks requiring extended context:
### Document Analysis
```python
messages = [
{"role": "user", "content": f"""
Here is a research paper:
{very_long_document}
Please summarize the key findings and methodology.
"""},
]
```
### Multi-Document QA
```python
messages = [
{"role": "user", "content": f"""
Document 1: {doc1}
Document 2: {doc2}
Document 3: {doc3}
Compare and contrast the approaches described in these documents.
"""},
]
```
### Codebase Analysis
```python
messages = [
{"role": "user", "content": f"""
Here is the source code for a project:
{entire_codebase}
Explain the architecture and identify potential improvements.
"""},
]
```
## Memory Requirements
### Inference (quantized model)
| Context Length | VRAM Required |
|----------------|---------------|
| Short (4K) | **10-11 GB** |
| Medium (16K) | 12-14 GB |
| Long (32K) | 16-20 GB |
| Extended (64K) | 24-32 GB |
| Full (128K) | 40+ GB |
**Tested on**: RTX 5000 Ada (32GB) - 10.22 GB active VRAM during inference
### Quantization (reproduction)
- **GPU**: RTX 5000 Ada 32GB
- **CPU**: Dual Xeon (224 cores for Hessian computation)
- **RAM**: 64GB+ recommended
- **Method**: GPU forward passes + CPU Hessian
## Quantization Details
| Specification | Value |
|--------------|-------|
| Method | GPTQ + Ultra-Hybrid + EoRA |
| Quantizer | GPTQModel |
| EoRA Rank | 128 (all layers) |
| Calibration Samples | 1,500 (6x industry standard) |
| Sequence Length | 2,048 tokens |
| Group Size | 128 |
| desc_act | False |
| sym | True (symmetric quantization) |
| Bits (default) | 4 |
| Layer Rules | 160 custom precision rules |
## Use Cases
**Ideal for:**
- ๐ **Long document analysis** (128K context)
- ๐ **Book summarization and QA**
- ๐ป **Codebase understanding**
- ๐ฌ **Research paper analysis**
- ๐ **Multilingual tasks** (10% multilingual training)
- ๐งฎ **Mathematical reasoning**
- ๐ง **Resource-constrained deployment** (28GB โ 10GB)
## Technical Specifications
| Specification | Value |
|--------------|-------|
| Model Family | Microsoft Phi-3 |
| Variant | medium-128k-instruct |
| Total Parameters | 14B |
| Total Layers | 40 |
| Hidden Size | 5,120 |
| Intermediate Size | 17,920 |
| Attention Heads | 40 |
| KV Heads | 10 (GQA) |
| Activation | SiLU (Swish) |
| Normalization | RMSNorm |
| Position Encoding | RoPE (theta=10000, SU scaling) |
| Context Length | 128,000 |
| Vocab Size | 32,064 |
| Training Tokens | 4.8T |
| Multilingual | 10% |
| Post-training | SFT + DPO |
## Expected Performance
Based on the precision strategy:
| Task Type | Expected Retention |
|-----------|-------------------|
| Long-context QA | 97-99% |
| Math Reasoning | 97-99% |
| Code Generation | 96-98% |
| Multilingual | 96-98% |
| General Chat | 98-99% |
## Acknowledgments
- **Microsoft Research** for developing the Phi-3 model family
- **NVIDIA** for the EoRA (Error-optimized Low-Rank Adaptation) technique used in this quantization
- **GPTQModel team** for the excellent quantization framework
## License
**MIT License** - Permissive open source license allowing commercial use, modification, and distribution.
## Citation
```bibtex
@software{phi3_medium_128k_gptq_2025,
title = {Microsoft Phi-3-medium-128k-instruct - TevunahAi Ultra-Hybrid GPTQ with EoRA},
author = {TevunahAi},
year = {2025},
note = {Ultra-Hybrid GPTQ with EoRA-128 for long-context quality retention},
url = {https://huggingface.co/TevunahAi/Phi-3-medium-128k-instruct-TevunahAi-GPTQ}
}
@misc{phi3_2024,
title = {Phi-3 Technical Report: A Highly Capable Language Model Locally on Your Phone},
author = {Microsoft},
year = {2024},
url = {https://huggingface.co/microsoft/Phi-3-medium-128k-instruct}
}
```
https://huggingface.co/TevunahAi |