Spaces:
Running
on
Zero
Running
on
Zero
adds torchao
Browse files- README_TORCHAO.md +172 -0
- app.py +39 -15
- requirements.txt +1 -1
- test_torchao_inference.py +95 -0
README_TORCHAO.md
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# TorchAO Quantization Implementation
|
| 2 |
+
|
| 3 |
+
This project now uses **TorchAO** for proper quantization and inference. TorchAO is PyTorch's architecture optimization library that provides high-performance quantization techniques.
|
| 4 |
+
|
| 5 |
+
## Key Changes Made
|
| 6 |
+
|
| 7 |
+
### 1. Proper TorchAO Configuration
|
| 8 |
+
|
| 9 |
+
The app now uses the correct TorchAO quantization configurations:
|
| 10 |
+
|
| 11 |
+
```python
|
| 12 |
+
from transformers import TorchAoConfig
|
| 13 |
+
from torchao.quantization import Int4WeightOnlyConfig, Int8WeightOnlyConfig
|
| 14 |
+
from torchao.dtypes import Int4CPULayout
|
| 15 |
+
|
| 16 |
+
def get_quantization_config():
|
| 17 |
+
if DEVICE == "cuda":
|
| 18 |
+
# For CUDA, use Int8WeightOnlyConfig for better performance
|
| 19 |
+
quant_config = Int8WeightOnlyConfig(group_size=128)
|
| 20 |
+
else:
|
| 21 |
+
# For CPU, use Int4WeightOnlyConfig with CPU layout
|
| 22 |
+
quant_config = Int4WeightOnlyConfig(group_size=128, layout=Int4CPULayout())
|
| 23 |
+
|
| 24 |
+
return TorchAoConfig(quant_type=quant_config)
|
| 25 |
+
```
|
| 26 |
+
|
| 27 |
+
### 2. Correct Model Loading
|
| 28 |
+
|
| 29 |
+
The model is now loaded with proper TorchAO quantization:
|
| 30 |
+
|
| 31 |
+
```python
|
| 32 |
+
quantization_config = get_quantization_config()
|
| 33 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 34 |
+
model_id,
|
| 35 |
+
quantization_config=quantization_config,
|
| 36 |
+
device_map="auto" if device == "cuda" else "cpu",
|
| 37 |
+
torch_dtype=torch.bfloat16 if device == "cuda" else torch.float32,
|
| 38 |
+
trust_remote_code=True,
|
| 39 |
+
low_cpu_mem_usage=True,
|
| 40 |
+
)
|
| 41 |
+
```
|
| 42 |
+
|
| 43 |
+
### 3. Proper Inference with Cache Implementation
|
| 44 |
+
|
| 45 |
+
The most important fix is using `cache_implementation="static"` for generation:
|
| 46 |
+
|
| 47 |
+
```python
|
| 48 |
+
output_ids = model.generate(
|
| 49 |
+
inputs['input_ids'],
|
| 50 |
+
max_new_tokens=max_tokens,
|
| 51 |
+
temperature=temperature,
|
| 52 |
+
top_p=top_p,
|
| 53 |
+
do_sample=do_sample,
|
| 54 |
+
attention_mask=inputs['attention_mask'],
|
| 55 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 56 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 57 |
+
cache_implementation="static" # CRITICAL for TorchAO quantized models
|
| 58 |
+
)
|
| 59 |
+
```
|
| 60 |
+
|
| 61 |
+
## TorchAO Quantization Types
|
| 62 |
+
|
| 63 |
+
### For CUDA (GPU)
|
| 64 |
+
- **Int8WeightOnlyConfig**: Best performance for most use cases
|
| 65 |
+
- **Int8DynamicActivationInt8WeightConfig**: For more aggressive quantization
|
| 66 |
+
- **GemliteUIntXWeightOnlyConfig**: Optimized for H100/A100 GPUs
|
| 67 |
+
|
| 68 |
+
### For CPU
|
| 69 |
+
- **Int4WeightOnlyConfig with Int4CPULayout**: Optimized for CPU deployment
|
| 70 |
+
- **Int8WeightOnlyConfig**: Alternative for better compatibility
|
| 71 |
+
|
| 72 |
+
### For Sparsity (Advanced)
|
| 73 |
+
- **Int4WeightOnlyConfig with MarlinSparseLayout**: For 2:4 sparsity
|
| 74 |
+
|
| 75 |
+
## Testing the Implementation
|
| 76 |
+
|
| 77 |
+
Run the test script to verify TorchAO quantization is working:
|
| 78 |
+
|
| 79 |
+
```bash
|
| 80 |
+
python test_torchao_inference.py
|
| 81 |
+
```
|
| 82 |
+
|
| 83 |
+
This will test:
|
| 84 |
+
- Model loading with TorchAO quantization
|
| 85 |
+
- Text generation with proper cache implementation
|
| 86 |
+
- Memory usage optimization
|
| 87 |
+
|
| 88 |
+
## Performance Benefits
|
| 89 |
+
|
| 90 |
+
1. **Memory Reduction**: Up to 50% memory reduction with Int4 quantization
|
| 91 |
+
2. **Faster Inference**: Optimized kernels for quantized operations
|
| 92 |
+
3. **Better Compatibility**: Works with torch.compile for additional speedup
|
| 93 |
+
4. **Device Optimization**: Different configs for CUDA vs CPU
|
| 94 |
+
|
| 95 |
+
## Common Issues and Solutions
|
| 96 |
+
|
| 97 |
+
### Issue: Model outputs incorrect or garbled text
|
| 98 |
+
**Solution**: Ensure `cache_implementation="static"` is used in generation
|
| 99 |
+
|
| 100 |
+
### Issue: Memory errors during loading
|
| 101 |
+
**Solution**: Use appropriate quantization config for your device (Int4 for CPU, Int8 for CUDA)
|
| 102 |
+
|
| 103 |
+
### Issue: Slow inference
|
| 104 |
+
**Solution**:
|
| 105 |
+
1. Use `cache_implementation="static"`
|
| 106 |
+
2. Consider using `torch.compile` for additional speedup
|
| 107 |
+
3. Use appropriate group_size (128 is usually optimal)
|
| 108 |
+
|
| 109 |
+
## Advanced Configuration
|
| 110 |
+
|
| 111 |
+
### Per-Module Quantization
|
| 112 |
+
|
| 113 |
+
You can quantize different layers with different configs:
|
| 114 |
+
|
| 115 |
+
```python
|
| 116 |
+
from torchao.quantization import ModuleFqnToConfig
|
| 117 |
+
|
| 118 |
+
# Skip quantization for certain layers
|
| 119 |
+
config = ModuleFqnToConfig({
|
| 120 |
+
"_default": Int4WeightOnlyConfig(group_size=128),
|
| 121 |
+
"model.layers.0.self_attn.q_proj": None # Skip this layer
|
| 122 |
+
})
|
| 123 |
+
```
|
| 124 |
+
|
| 125 |
+
### Autoquantization
|
| 126 |
+
|
| 127 |
+
For automatic quantization selection:
|
| 128 |
+
|
| 129 |
+
```python
|
| 130 |
+
quantization_config = TorchAoConfig("autoquant", min_sqnr=None)
|
| 131 |
+
# After loading, call:
|
| 132 |
+
model.finalize_autoquant()
|
| 133 |
+
```
|
| 134 |
+
|
| 135 |
+
## Requirements
|
| 136 |
+
|
| 137 |
+
Make sure you have the latest TorchAO version:
|
| 138 |
+
|
| 139 |
+
```bash
|
| 140 |
+
pip install torchao>=0.10.0
|
| 141 |
+
```
|
| 142 |
+
|
| 143 |
+
## Deployment Notes
|
| 144 |
+
|
| 145 |
+
1. **Serialization**: TorchAO models should be saved with `safe_serialization=False`
|
| 146 |
+
2. **Device Compatibility**: Int4 models are device-specific, Int8 models are portable
|
| 147 |
+
3. **Memory**: Monitor memory usage during deployment
|
| 148 |
+
4. **Performance**: Use `cache_implementation="static"` for best performance
|
| 149 |
+
|
| 150 |
+
## Troubleshooting
|
| 151 |
+
|
| 152 |
+
### Check TorchAO Version
|
| 153 |
+
```python
|
| 154 |
+
import torchao
|
| 155 |
+
print(torchao.__version__)
|
| 156 |
+
```
|
| 157 |
+
|
| 158 |
+
### Verify Quantization
|
| 159 |
+
```python
|
| 160 |
+
# Check if model is quantized
|
| 161 |
+
for name, module in model.named_modules():
|
| 162 |
+
if hasattr(module, 'weight') and module.weight.dtype != torch.float32:
|
| 163 |
+
print(f"{name}: {module.weight.dtype}")
|
| 164 |
+
```
|
| 165 |
+
|
| 166 |
+
### Memory Usage
|
| 167 |
+
```python
|
| 168 |
+
import torch
|
| 169 |
+
print(f"GPU Memory: {torch.cuda.memory_allocated() / 1024**3:.2f} GB")
|
| 170 |
+
```
|
| 171 |
+
|
| 172 |
+
This implementation ensures proper TorchAO quantization for both loading and inference, with the critical `cache_implementation="static"` parameter for correct generation.
|
app.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
|
|
|
| 4 |
import re
|
| 5 |
import json
|
| 6 |
from typing import List, Dict, Any, Optional
|
|
@@ -9,6 +11,7 @@ import spaces
|
|
| 9 |
import os
|
| 10 |
import sys
|
| 11 |
import requests
|
|
|
|
| 12 |
|
| 13 |
# Set torch to use float32 for better compatibility with quantized models
|
| 14 |
torch.set_default_dtype(torch.float32)
|
|
@@ -20,20 +23,20 @@ model = None
|
|
| 20 |
tokenizer = None
|
| 21 |
DEFAULT_SYSTEM_PROMPT = "Tu es TonicIA, un assistant francophone rigoureux et bienveillant."
|
| 22 |
title = "# π€ Petite Elle L'Aime 3 - Chat Interface"
|
| 23 |
-
description = "A fine-tuned version of SmolLM3-3B optimized for French conversations. This is the
|
| 24 |
presentation1 = """
|
| 25 |
### π― Features
|
| 26 |
- **Multilingual Support**: English, French, Italian, Portuguese, Chinese, Arabic
|
| 27 |
-
- **
|
| 28 |
- **Interactive Chat Interface**: Real-time conversation with the model
|
| 29 |
- **Customizable System Prompt**: Define the assistant's personality and behavior
|
| 30 |
- **Thinking Mode**: Enable reasoning mode with thinking tags
|
| 31 |
"""
|
| 32 |
presentation2 = """### π― FonctionnalitΓ©s
|
| 33 |
* **Support multilingue** : Anglais, FranΓ§ais, Italien, Portugais, Chinois, Arabe
|
| 34 |
-
* **Quantification
|
| 35 |
* **Interface de chat interactive** : Conversation en temps réel avec le modèle
|
| 36 |
-
* **Invite système personnalisable** : Définissez la personnalité et le comportement de l
|
| 37 |
* **Mode RΓ©flexion** : Activez le mode raisonnement avec des balises de rΓ©flexion
|
| 38 |
"""
|
| 39 |
joinus = """
|
|
@@ -63,23 +66,42 @@ def download_chat_template():
|
|
| 63 |
return None
|
| 64 |
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
def load_model():
|
| 67 |
-
"""Load the model and tokenizer"""
|
| 68 |
global model, tokenizer
|
| 69 |
|
| 70 |
try:
|
| 71 |
logger.info(f"Loading tokenizer from {MAIN_MODEL_ID}")
|
| 72 |
tokenizer = AutoTokenizer.from_pretrained(MAIN_MODEL_ID, subfolder="int4")
|
| 73 |
chat_template = download_chat_template()
|
| 74 |
-
|
|
|
|
| 75 |
logger.info("Chat template downloaded and set successfully")
|
| 76 |
|
| 77 |
-
logger.info(f"Loading
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
model_kwargs = {
|
| 79 |
"device_map": "auto" if DEVICE == "cuda" else "cpu",
|
| 80 |
-
"torch_dtype": torch.float32,
|
| 81 |
"trust_remote_code": True,
|
| 82 |
"low_cpu_mem_usage": True,
|
|
|
|
| 83 |
}
|
| 84 |
|
| 85 |
logger.info(f"Model loading parameters: {model_kwargs}")
|
|
@@ -88,7 +110,7 @@ def load_model():
|
|
| 88 |
if tokenizer.pad_token_id is None:
|
| 89 |
tokenizer.pad_token_id = tokenizer.eos_token_id
|
| 90 |
|
| 91 |
-
logger.info("Model loaded successfully")
|
| 92 |
return True
|
| 93 |
|
| 94 |
except Exception as e:
|
|
@@ -121,11 +143,12 @@ def create_prompt(system_message, user_message, enable_thinking=True):
|
|
| 121 |
|
| 122 |
@spaces.GPU(duration=94)
|
| 123 |
def generate_response(message, history, system_message, max_tokens, temperature, top_p, do_sample, enable_thinking=True):
|
| 124 |
-
"""Generate response using the model"""
|
| 125 |
global model, tokenizer
|
| 126 |
|
| 127 |
if model is None or tokenizer is None:
|
| 128 |
return "Error: Model not loaded. Please wait for the model to load."
|
|
|
|
| 129 |
full_prompt = create_prompt(system_message, message, enable_thinking)
|
| 130 |
|
| 131 |
if not full_prompt:
|
|
@@ -136,6 +159,7 @@ def generate_response(message, history, system_message, max_tokens, temperature,
|
|
| 136 |
|
| 137 |
if DEVICE == "cuda":
|
| 138 |
inputs = {k: v.cuda() for k, v in inputs.items()}
|
|
|
|
| 139 |
with torch.no_grad():
|
| 140 |
output_ids = model.generate(
|
| 141 |
inputs['input_ids'],
|
|
@@ -145,8 +169,9 @@ def generate_response(message, history, system_message, max_tokens, temperature,
|
|
| 145 |
do_sample=do_sample,
|
| 146 |
attention_mask=inputs['attention_mask'],
|
| 147 |
pad_token_id=tokenizer.eos_token_id,
|
| 148 |
-
eos_token_id=tokenizer.eos_token_id
|
| 149 |
-
|
|
|
|
| 150 |
response = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 151 |
assistant_response = response[len(full_prompt):].strip()
|
| 152 |
assistant_response = re.sub(r'<\|im_start\|>.*?<\|im_end\|>', '', assistant_response, flags=re.DOTALL)
|
|
@@ -175,7 +200,7 @@ def bot(history, system_prompt, max_length, temperature, top_p, advanced_checkbo
|
|
| 175 |
return history
|
| 176 |
|
| 177 |
# Load model on startup
|
| 178 |
-
logger.info("Starting model loading process...")
|
| 179 |
load_model()
|
| 180 |
|
| 181 |
# Create Gradio interface
|
|
@@ -259,6 +284,5 @@ with gr.Blocks() as demo:
|
|
| 259 |
)
|
| 260 |
|
| 261 |
if __name__ == "__main__":
|
| 262 |
-
|
| 263 |
demo.queue()
|
| 264 |
demo.launch(ssr_mode=False, mcp_server=True)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TorchAoConfig
|
| 4 |
+
from torchao.quantization import Int4WeightOnlyConfig, Int8WeightOnlyConfig, Int8DynamicActivationInt8WeightConfig
|
| 5 |
+
from torchao.dtypes import Int4CPULayout
|
| 6 |
import re
|
| 7 |
import json
|
| 8 |
from typing import List, Dict, Any, Optional
|
|
|
|
| 11 |
import os
|
| 12 |
import sys
|
| 13 |
import requests
|
| 14 |
+
import accelerate
|
| 15 |
|
| 16 |
# Set torch to use float32 for better compatibility with quantized models
|
| 17 |
torch.set_default_dtype(torch.float32)
|
|
|
|
| 23 |
tokenizer = None
|
| 24 |
DEFAULT_SYSTEM_PROMPT = "Tu es TonicIA, un assistant francophone rigoureux et bienveillant."
|
| 25 |
title = "# π€ Petite Elle L'Aime 3 - Chat Interface"
|
| 26 |
+
description = "A fine-tuned version of SmolLM3-3B optimized for French conversations. This is the torchao quantized version for efficient deployment."
|
| 27 |
presentation1 = """
|
| 28 |
### π― Features
|
| 29 |
- **Multilingual Support**: English, French, Italian, Portuguese, Chinese, Arabic
|
| 30 |
+
- **TorchAO Quantization**: Optimized for deployment with memory reduction
|
| 31 |
- **Interactive Chat Interface**: Real-time conversation with the model
|
| 32 |
- **Customizable System Prompt**: Define the assistant's personality and behavior
|
| 33 |
- **Thinking Mode**: Enable reasoning mode with thinking tags
|
| 34 |
"""
|
| 35 |
presentation2 = """### π― FonctionnalitΓ©s
|
| 36 |
* **Support multilingue** : Anglais, FranΓ§ais, Italien, Portugais, Chinois, Arabe
|
| 37 |
+
* **Quantification TorchAO** : OptimisΓ© pour un dΓ©ploiement avec rΓ©duction de mΓ©moire
|
| 38 |
* **Interface de chat interactive** : Conversation en temps réel avec le modèle
|
| 39 |
+
* **Invite système personnalisable** : Définissez la personnalité et le comportement de l'assistant
|
| 40 |
* **Mode RΓ©flexion** : Activez le mode raisonnement avec des balises de rΓ©flexion
|
| 41 |
"""
|
| 42 |
joinus = """
|
|
|
|
| 66 |
return None
|
| 67 |
|
| 68 |
|
| 69 |
+
def get_quantization_config():
|
| 70 |
+
"""Get the appropriate quantization config based on device"""
|
| 71 |
+
if DEVICE == "cuda":
|
| 72 |
+
# For CUDA, use Int8WeightOnlyConfig for better performance
|
| 73 |
+
quant_config = Int8WeightOnlyConfig(group_size=128)
|
| 74 |
+
else:
|
| 75 |
+
# For CPU, use Int4WeightOnlyConfig with CPU layout
|
| 76 |
+
quant_config = Int4WeightOnlyConfig(group_size=128, layout=Int4CPULayout())
|
| 77 |
+
|
| 78 |
+
return TorchAoConfig(quant_type=quant_config)
|
| 79 |
+
|
| 80 |
+
|
| 81 |
def load_model():
|
| 82 |
+
"""Load the model and tokenizer with torchao quantization"""
|
| 83 |
global model, tokenizer
|
| 84 |
|
| 85 |
try:
|
| 86 |
logger.info(f"Loading tokenizer from {MAIN_MODEL_ID}")
|
| 87 |
tokenizer = AutoTokenizer.from_pretrained(MAIN_MODEL_ID, subfolder="int4")
|
| 88 |
chat_template = download_chat_template()
|
| 89 |
+
if chat_template:
|
| 90 |
+
tokenizer.chat_template = chat_template
|
| 91 |
logger.info("Chat template downloaded and set successfully")
|
| 92 |
|
| 93 |
+
logger.info(f"Loading model with torchao quantization from {MAIN_MODEL_ID}")
|
| 94 |
+
|
| 95 |
+
# Get quantization config
|
| 96 |
+
quantization_config = get_quantization_config()
|
| 97 |
+
logger.info(f"Using quantization config: {quantization_config}")
|
| 98 |
+
|
| 99 |
model_kwargs = {
|
| 100 |
"device_map": "auto" if DEVICE == "cuda" else "cpu",
|
| 101 |
+
"torch_dtype": torch.bfloat16 if DEVICE == "cuda" else torch.float32,
|
| 102 |
"trust_remote_code": True,
|
| 103 |
"low_cpu_mem_usage": True,
|
| 104 |
+
"quantization_config": quantization_config,
|
| 105 |
}
|
| 106 |
|
| 107 |
logger.info(f"Model loading parameters: {model_kwargs}")
|
|
|
|
| 110 |
if tokenizer.pad_token_id is None:
|
| 111 |
tokenizer.pad_token_id = tokenizer.eos_token_id
|
| 112 |
|
| 113 |
+
logger.info("Model loaded successfully with torchao quantization")
|
| 114 |
return True
|
| 115 |
|
| 116 |
except Exception as e:
|
|
|
|
| 143 |
|
| 144 |
@spaces.GPU(duration=94)
|
| 145 |
def generate_response(message, history, system_message, max_tokens, temperature, top_p, do_sample, enable_thinking=True):
|
| 146 |
+
"""Generate response using the torchao quantized model"""
|
| 147 |
global model, tokenizer
|
| 148 |
|
| 149 |
if model is None or tokenizer is None:
|
| 150 |
return "Error: Model not loaded. Please wait for the model to load."
|
| 151 |
+
|
| 152 |
full_prompt = create_prompt(system_message, message, enable_thinking)
|
| 153 |
|
| 154 |
if not full_prompt:
|
|
|
|
| 159 |
|
| 160 |
if DEVICE == "cuda":
|
| 161 |
inputs = {k: v.cuda() for k, v in inputs.items()}
|
| 162 |
+
|
| 163 |
with torch.no_grad():
|
| 164 |
output_ids = model.generate(
|
| 165 |
inputs['input_ids'],
|
|
|
|
| 169 |
do_sample=do_sample,
|
| 170 |
attention_mask=inputs['attention_mask'],
|
| 171 |
pad_token_id=tokenizer.eos_token_id,
|
| 172 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 173 |
+
cache_implementation="static" # Important for torchao quantized models
|
| 174 |
+
)
|
| 175 |
response = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 176 |
assistant_response = response[len(full_prompt):].strip()
|
| 177 |
assistant_response = re.sub(r'<\|im_start\|>.*?<\|im_end\|>', '', assistant_response, flags=re.DOTALL)
|
|
|
|
| 200 |
return history
|
| 201 |
|
| 202 |
# Load model on startup
|
| 203 |
+
logger.info("Starting model loading process with torchao quantization...")
|
| 204 |
load_model()
|
| 205 |
|
| 206 |
# Create Gradio interface
|
|
|
|
| 284 |
)
|
| 285 |
|
| 286 |
if __name__ == "__main__":
|
|
|
|
| 287 |
demo.queue()
|
| 288 |
demo.launch(ssr_mode=False, mcp_server=True)
|
requirements.txt
CHANGED
|
@@ -2,7 +2,7 @@ gradio>=5.38.2
|
|
| 2 |
torch>=2.0.0
|
| 3 |
transformers>=4.54.0
|
| 4 |
accelerate>=0.20.0
|
| 5 |
-
torchao>=0.
|
| 6 |
safetensors>=0.4.0
|
| 7 |
tokenizers>=0.21.2
|
| 8 |
pyyaml>=6.0
|
|
|
|
| 2 |
torch>=2.0.0
|
| 3 |
transformers>=4.54.0
|
| 4 |
accelerate>=0.20.0
|
| 5 |
+
torchao>=0.10.0
|
| 6 |
safetensors>=0.4.0
|
| 7 |
tokenizers>=0.21.2
|
| 8 |
pyyaml>=6.0
|
test_torchao_inference.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Test script for torchao quantization inference
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import torch
|
| 7 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TorchAoConfig
|
| 8 |
+
from torchao.quantization import Int4WeightOnlyConfig, Int8WeightOnlyConfig
|
| 9 |
+
from torchao.dtypes import Int4CPULayout
|
| 10 |
+
import logging
|
| 11 |
+
|
| 12 |
+
# Set up logging
|
| 13 |
+
logging.basicConfig(level=logging.INFO)
|
| 14 |
+
logger = logging.getLogger(__name__)
|
| 15 |
+
|
| 16 |
+
def test_torchao_quantization():
|
| 17 |
+
"""Test torchao quantization with different configurations"""
|
| 18 |
+
|
| 19 |
+
model_id = "Tonic/petite-elle-L-aime-3-sft"
|
| 20 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 21 |
+
|
| 22 |
+
logger.info(f"Testing torchao quantization on device: {device}")
|
| 23 |
+
|
| 24 |
+
# Test different quantization configs
|
| 25 |
+
configs_to_test = []
|
| 26 |
+
|
| 27 |
+
if device == "cuda":
|
| 28 |
+
configs_to_test.append(("Int8WeightOnlyConfig", Int8WeightOnlyConfig(group_size=128)))
|
| 29 |
+
else:
|
| 30 |
+
configs_to_test.append(("Int4WeightOnlyConfig CPU", Int4WeightOnlyConfig(group_size=128, layout=Int4CPULayout())))
|
| 31 |
+
|
| 32 |
+
for config_name, quant_config in configs_to_test:
|
| 33 |
+
logger.info(f"\nTesting {config_name}...")
|
| 34 |
+
|
| 35 |
+
try:
|
| 36 |
+
# Create quantization config
|
| 37 |
+
quantization_config = TorchAoConfig(quant_type=quant_config)
|
| 38 |
+
|
| 39 |
+
# Load tokenizer
|
| 40 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 41 |
+
if tokenizer.pad_token_id is None:
|
| 42 |
+
tokenizer.pad_token_id = tokenizer.eos_token_id
|
| 43 |
+
|
| 44 |
+
# Load model with quantization
|
| 45 |
+
model_kwargs = {
|
| 46 |
+
"device_map": "auto" if device == "cuda" else "cpu",
|
| 47 |
+
"torch_dtype": torch.bfloat16 if device == "cuda" else torch.float32,
|
| 48 |
+
"trust_remote_code": True,
|
| 49 |
+
"low_cpu_mem_usage": True,
|
| 50 |
+
"quantization_config": quantization_config,
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
logger.info(f"Loading model with {config_name}...")
|
| 54 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, **model_kwargs)
|
| 55 |
+
|
| 56 |
+
# Test generation
|
| 57 |
+
test_prompt = "Bonjour, comment allez-vous?"
|
| 58 |
+
inputs = tokenizer(test_prompt, return_tensors="pt")
|
| 59 |
+
|
| 60 |
+
if device == "cuda":
|
| 61 |
+
inputs = {k: v.cuda() for k, v in inputs.items()}
|
| 62 |
+
|
| 63 |
+
logger.info("Generating response...")
|
| 64 |
+
with torch.no_grad():
|
| 65 |
+
output_ids = model.generate(
|
| 66 |
+
inputs['input_ids'],
|
| 67 |
+
max_new_tokens=50,
|
| 68 |
+
temperature=0.7,
|
| 69 |
+
top_p=0.95,
|
| 70 |
+
do_sample=True,
|
| 71 |
+
attention_mask=inputs['attention_mask'],
|
| 72 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 73 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 74 |
+
cache_implementation="static" # Important for torchao
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
response = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 78 |
+
assistant_response = response[len(test_prompt):].strip()
|
| 79 |
+
|
| 80 |
+
logger.info(f"β
{config_name} test successful!")
|
| 81 |
+
logger.info(f"Input: {test_prompt}")
|
| 82 |
+
logger.info(f"Output: {assistant_response}")
|
| 83 |
+
|
| 84 |
+
# Clean up
|
| 85 |
+
del model
|
| 86 |
+
torch.cuda.empty_cache() if device == "cuda" else None
|
| 87 |
+
|
| 88 |
+
except Exception as e:
|
| 89 |
+
logger.error(f"β {config_name} test failed: {e}")
|
| 90 |
+
continue
|
| 91 |
+
|
| 92 |
+
logger.info("\nπ All torchao quantization tests completed!")
|
| 93 |
+
|
| 94 |
+
if __name__ == "__main__":
|
| 95 |
+
test_torchao_quantization()
|