Spaces:
Running
on
Zero
Running
on
Zero
tries to improve the generation paramaters
Browse files- README_SMOLLM3_FEATURES.md +227 -0
- README_TORCHAO.md +103 -96
- app.py +131 -48
- test_pre_quantized_model.py +91 -0
- test_smollm3_features.py +71 -0
README_SMOLLM3_FEATURES.md
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# SmolLM3 Features Implementation
|
| 2 |
+
|
| 3 |
+
This document describes the SmolLM3 features implemented in the Petite Elle L'Aime 3 chat interface.
|
| 4 |
+
|
| 5 |
+
## 🎯 SmolLM3 Features
|
| 6 |
+
|
| 7 |
+
### 1. Thinking Mode
|
| 8 |
+
|
| 9 |
+
SmolLM3 supports extended thinking mode with reasoning traces. The implementation includes:
|
| 10 |
+
|
| 11 |
+
- **Automatic thinking flags**: System prompts automatically get `/think` or `/no_think` flags
|
| 12 |
+
- **Manual control**: Users can manually add thinking flags to system prompts
|
| 13 |
+
- **UI toggle**: Checkbox to enable/disable thinking mode
|
| 14 |
+
- **Response cleaning**: Thinking tags are properly cleaned from responses
|
| 15 |
+
|
| 16 |
+
#### Usage Examples:
|
| 17 |
+
|
| 18 |
+
```python
|
| 19 |
+
# With thinking enabled (default)
|
| 20 |
+
system_prompt = "Tu es TonicIA, un assistant francophone rigoureux et bienveillant./think"
|
| 21 |
+
|
| 22 |
+
# With thinking disabled
|
| 23 |
+
system_prompt = "Tu es TonicIA, un assistant francophone rigoureux et bienveillant./no_think"
|
| 24 |
+
|
| 25 |
+
# Manual control in UI
|
| 26 |
+
enable_thinking = True # or False
|
| 27 |
+
```
|
| 28 |
+
|
| 29 |
+
### 2. Tool Calling
|
| 30 |
+
|
| 31 |
+
SmolLM3 supports both XML and Python tool calling formats:
|
| 32 |
+
|
| 33 |
+
#### XML Tools (Default)
|
| 34 |
+
```json
|
| 35 |
+
[
|
| 36 |
+
{
|
| 37 |
+
"name": "get_weather",
|
| 38 |
+
"description": "Get the weather in a city",
|
| 39 |
+
"parameters": {
|
| 40 |
+
"type": "object",
|
| 41 |
+
"properties": {
|
| 42 |
+
"city": {
|
| 43 |
+
"type": "string",
|
| 44 |
+
"description": "The city to get the weather for"
|
| 45 |
+
}
|
| 46 |
+
}
|
| 47 |
+
}
|
| 48 |
+
}
|
| 49 |
+
]
|
| 50 |
+
```
|
| 51 |
+
|
| 52 |
+
#### Python Tools
|
| 53 |
+
```python
|
| 54 |
+
# Tools are called as Python functions in <code> tags
|
| 55 |
+
# Example: <code>get_weather(city="Paris")</code>
|
| 56 |
+
```
|
| 57 |
+
|
| 58 |
+
### 3. Generation Parameters
|
| 59 |
+
|
| 60 |
+
Following SmolLM3 recommendations:
|
| 61 |
+
|
| 62 |
+
- **Temperature**: 0.6 (recommended default)
|
| 63 |
+
- **Top-p**: 0.95 (recommended default)
|
| 64 |
+
- **Repetition Penalty**: 1.1 (recommended default)
|
| 65 |
+
- **Max tokens**: 2048 (configurable up to 32,768)
|
| 66 |
+
- **Context length**: Up to 65,536 tokens (extensible to 128k/256k with YaRN)
|
| 67 |
+
|
| 68 |
+
### 4. Long Context Processing
|
| 69 |
+
|
| 70 |
+
The model supports:
|
| 71 |
+
- **Base context**: 65,536 tokens
|
| 72 |
+
- **Extended context**: Up to 256k tokens with YaRN scaling
|
| 73 |
+
- **YaRN configuration**: Available for longer inputs
|
| 74 |
+
|
| 75 |
+
```json
|
| 76 |
+
{
|
| 77 |
+
"rope_scaling": {
|
| 78 |
+
"factor": 2.0,
|
| 79 |
+
"original_max_position_embeddings": 65536,
|
| 80 |
+
"type": "yarn"
|
| 81 |
+
}
|
| 82 |
+
}
|
| 83 |
+
```
|
| 84 |
+
|
| 85 |
+
## 🔧 Implementation Details
|
| 86 |
+
|
| 87 |
+
### Chat Template Integration
|
| 88 |
+
|
| 89 |
+
The app uses SmolLM3's chat template with proper thinking and tool calling:
|
| 90 |
+
|
| 91 |
+
```python
|
| 92 |
+
def create_prompt(system_message, user_message, enable_thinking=True, tools=None, use_xml_tools=True):
|
| 93 |
+
formatted_messages = []
|
| 94 |
+
|
| 95 |
+
# Handle thinking flags
|
| 96 |
+
if system_message and system_message.strip():
|
| 97 |
+
has_think_flag = "/think" in system_message
|
| 98 |
+
has_no_think_flag = "/no_think" in system_message
|
| 99 |
+
|
| 100 |
+
if not enable_thinking and not has_no_think_flag:
|
| 101 |
+
system_message += "/no_think"
|
| 102 |
+
elif enable_thinking and not has_think_flag and not has_no_think_flag:
|
| 103 |
+
system_message += "/think"
|
| 104 |
+
formatted_messages.append({"role": "system", "content": system_message})
|
| 105 |
+
|
| 106 |
+
formatted_messages.append({"role": "user", "content": user_message})
|
| 107 |
+
|
| 108 |
+
# Apply chat template with SmolLM3 features
|
| 109 |
+
template_kwargs = {
|
| 110 |
+
"tokenize": False,
|
| 111 |
+
"add_generation_prompt": True,
|
| 112 |
+
"enable_thinking": enable_thinking
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
# Add tool calling
|
| 116 |
+
if tools and len(tools) > 0:
|
| 117 |
+
if use_xml_tools:
|
| 118 |
+
template_kwargs["xml_tools"] = tools
|
| 119 |
+
else:
|
| 120 |
+
template_kwargs["python_tools"] = tools
|
| 121 |
+
|
| 122 |
+
return tokenizer.apply_chat_template(formatted_messages, **template_kwargs)
|
| 123 |
+
```
|
| 124 |
+
|
| 125 |
+
### Tool Call Detection
|
| 126 |
+
|
| 127 |
+
The app detects and formats tool calls in responses:
|
| 128 |
+
|
| 129 |
+
```python
|
| 130 |
+
# Handle tool calls if present
|
| 131 |
+
if parsed_tools and ("<tool_call>" in assistant_response or "<code>" in assistant_response):
|
| 132 |
+
if "<tool_call>" in assistant_response:
|
| 133 |
+
tool_call_match = re.search(r'<tool_call>(.*?)</tool_call>', assistant_response, re.DOTALL)
|
| 134 |
+
if tool_call_match:
|
| 135 |
+
tool_call = tool_call_match.group(1)
|
| 136 |
+
assistant_response += f"\n\n🔧 Tool Call Detected: {tool_call}\n\nNote: This is a simulated tool call."
|
| 137 |
+
elif "<code>" in assistant_response:
|
| 138 |
+
code_match = re.search(r'<code>(.*?)</code>', assistant_response, re.DOTALL)
|
| 139 |
+
if code_match:
|
| 140 |
+
code_call = code_match.group(1)
|
| 141 |
+
assistant_response += f"\n\n🐍 Python Tool Call: {code_call}\n\nNote: This is a simulated Python tool call."
|
| 142 |
+
```
|
| 143 |
+
|
| 144 |
+
## 🎮 UI Features
|
| 145 |
+
|
| 146 |
+
### Advanced Settings Panel
|
| 147 |
+
|
| 148 |
+
- **Temperature slider**: 0.01 to 1.0 (default: 0.6)
|
| 149 |
+
- **Top-p slider**: 0.1 to 1.0 (default: 0.95)
|
| 150 |
+
- **Repetition Penalty slider**: 1.0 to 2.0 (default: 1.1)
|
| 151 |
+
- **Max length slider**: 10 to 32,768 tokens (default: 2048)
|
| 152 |
+
- **Thinking mode checkbox**: Enable/disable reasoning traces
|
| 153 |
+
- **Tool calling checkbox**: Enable/disable function calling
|
| 154 |
+
- **XML vs Python tools**: Choose tool calling format
|
| 155 |
+
- **Tool definition editor**: JSON editor for custom tools
|
| 156 |
+
|
| 157 |
+
### Default Tool Set
|
| 158 |
+
|
| 159 |
+
The app includes two default tools for demonstration:
|
| 160 |
+
|
| 161 |
+
1. **get_weather**: Get weather information for a city
|
| 162 |
+
2. **calculate**: Perform mathematical calculations
|
| 163 |
+
|
| 164 |
+
## 🚀 Usage Examples
|
| 165 |
+
|
| 166 |
+
### Basic Chat with Thinking
|
| 167 |
+
```python
|
| 168 |
+
system_prompt = "Tu es TonicIA, un assistant francophone rigoureux et bienveillant./think"
|
| 169 |
+
user_message = "Explique-moi la gravité en termes simples."
|
| 170 |
+
```
|
| 171 |
+
|
| 172 |
+
### Chat with Tool Calling
|
| 173 |
+
```python
|
| 174 |
+
tools = [
|
| 175 |
+
{
|
| 176 |
+
"name": "get_weather",
|
| 177 |
+
"description": "Get the weather in a city",
|
| 178 |
+
"parameters": {
|
| 179 |
+
"type": "object",
|
| 180 |
+
"properties": {
|
| 181 |
+
"city": {"type": "string", "description": "The city name"}
|
| 182 |
+
}
|
| 183 |
+
}
|
| 184 |
+
}
|
| 185 |
+
]
|
| 186 |
+
|
| 187 |
+
user_message = "Quel temps fait-il à Paris aujourd'hui?"
|
| 188 |
+
```
|
| 189 |
+
|
| 190 |
+
### Agentic Usage
|
| 191 |
+
```python
|
| 192 |
+
# The model can call tools automatically based on user requests
|
| 193 |
+
# Example: "Calculate 15 * 23" will trigger the calculate tool
|
| 194 |
+
# Example: "What's the weather in London?" will trigger the get_weather tool
|
| 195 |
+
```
|
| 196 |
+
|
| 197 |
+
## 📋 Requirements
|
| 198 |
+
|
| 199 |
+
- **Transformers**: v4.53.0+ (required for SmolLM3 support)
|
| 200 |
+
- **PyTorch**: Latest version
|
| 201 |
+
- **Gradio**: For the web interface
|
| 202 |
+
- **Hugging Face Spaces**: For deployment
|
| 203 |
+
|
| 204 |
+
## 🔄 Migration from Previous Version
|
| 205 |
+
|
| 206 |
+
The updated app includes:
|
| 207 |
+
|
| 208 |
+
1. **SmolLM3-compatible generation parameters**
|
| 209 |
+
2. **Thinking mode with proper flag handling**
|
| 210 |
+
3. **Tool calling support (XML and Python)**
|
| 211 |
+
4. **Extended context support**
|
| 212 |
+
5. **Improved response cleaning**
|
| 213 |
+
|
| 214 |
+
## 🎯 Best Practices
|
| 215 |
+
|
| 216 |
+
1. **Use recommended parameters**: temperature=0.6, top_p=0.95, repetition_penalty=1.1
|
| 217 |
+
2. **Enable thinking for complex reasoning tasks**
|
| 218 |
+
3. **Use tool calling for structured tasks**
|
| 219 |
+
4. **Keep context within limits**: 65k tokens base, 256k with YaRN
|
| 220 |
+
5. **Test tool definitions before deployment**
|
| 221 |
+
6. **Adjust repetition penalty**: Use 1.0-1.2 for creative tasks, 1.1-1.3 for factual responses
|
| 222 |
+
|
| 223 |
+
## 🔗 References
|
| 224 |
+
|
| 225 |
+
- [SmolLM3 Model Card](https://huggingface.co/HuggingFaceTB/SmolLM3-3B)
|
| 226 |
+
- [SmolLM3 Documentation](https://huggingface.co/docs/transformers/model_doc/smol-lm-3)
|
| 227 |
+
- [Tool Calling Guide](https://huggingface.co/docs/transformers/chat_templating#tool-use)
|
README_TORCHAO.md
CHANGED
|
@@ -1,46 +1,39 @@
|
|
| 1 |
-
#
|
| 2 |
|
| 3 |
-
This project
|
| 4 |
|
| 5 |
## Key Changes Made
|
| 6 |
|
| 7 |
-
### 1.
|
| 8 |
|
| 9 |
-
The app now
|
| 10 |
|
| 11 |
```python
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 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 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
)
|
|
|
|
| 41 |
```
|
| 42 |
|
| 43 |
-
###
|
| 44 |
|
| 45 |
The most important fix is using `cache_implementation="static"` for generation:
|
| 46 |
|
|
@@ -54,108 +47,92 @@ output_ids = model.generate(
|
|
| 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
|
| 58 |
)
|
| 59 |
```
|
| 60 |
|
| 61 |
-
##
|
| 62 |
|
| 63 |
-
###
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
| 71 |
|
| 72 |
-
###
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
## Testing the Implementation
|
| 76 |
|
| 77 |
-
Run the test script to verify
|
| 78 |
|
| 79 |
```bash
|
| 80 |
-
python
|
| 81 |
```
|
| 82 |
|
| 83 |
This will test:
|
| 84 |
-
-
|
| 85 |
- Text generation with proper cache implementation
|
| 86 |
-
-
|
| 87 |
|
| 88 |
## Performance Benefits
|
| 89 |
|
| 90 |
-
1. **Memory Reduction**:
|
| 91 |
-
2. **Faster
|
| 92 |
-
3. **
|
| 93 |
-
4. **
|
| 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
|
| 102 |
|
| 103 |
### Issue: Slow inference
|
| 104 |
**Solution**:
|
| 105 |
1. Use `cache_implementation="static"`
|
| 106 |
2. Consider using `torch.compile` for additional speedup
|
| 107 |
-
3.
|
| 108 |
-
|
| 109 |
-
## Advanced Configuration
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
You can quantize different layers with different configs:
|
| 114 |
-
|
| 115 |
-
```python
|
| 116 |
-
from torchao.quantization import ModuleFqnToConfig
|
| 117 |
|
| 118 |
-
|
| 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 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 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. **
|
| 146 |
-
2. **
|
| 147 |
-
3. **Memory**:
|
| 148 |
-
4. **Performance**:
|
| 149 |
|
| 150 |
## Troubleshooting
|
| 151 |
|
| 152 |
-
### Check
|
| 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():
|
|
@@ -169,4 +146,34 @@ import torch
|
|
| 169 |
print(f"GPU Memory: {torch.cuda.memory_allocated() / 1024**3:.2f} GB")
|
| 170 |
```
|
| 171 |
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Pre-Quantized Model Implementation
|
| 2 |
|
| 3 |
+
This project uses a **pre-quantized int4 model** for efficient deployment. The model is already quantized and stored in the `int4` subfolder, so we don't need to apply additional quantization during loading.
|
| 4 |
|
| 5 |
## Key Changes Made
|
| 6 |
|
| 7 |
+
### 1. Loading Pre-Quantized Model
|
| 8 |
|
| 9 |
+
The app now correctly loads the pre-quantized model without trying to re-quantize it:
|
| 10 |
|
| 11 |
```python
|
| 12 |
+
def load_model():
|
| 13 |
+
"""Load the pre-quantized model and tokenizer"""
|
| 14 |
+
global model, tokenizer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
try:
|
| 17 |
+
# Load tokenizer from int4 subfolder
|
| 18 |
+
tokenizer = AutoTokenizer.from_pretrained(MAIN_MODEL_ID, subfolder="int4")
|
| 19 |
+
|
| 20 |
+
# Load pre-quantized model without additional quantization config
|
| 21 |
+
model_kwargs = {
|
| 22 |
+
"device_map": "auto" if DEVICE == "cuda" else "cpu",
|
| 23 |
+
"torch_dtype": torch.float32, # Use float32 for compatibility
|
| 24 |
+
"trust_remote_code": True,
|
| 25 |
+
"low_cpu_mem_usage": True,
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
model = AutoModelForCausalLM.from_pretrained(MAIN_MODEL_ID, subfolder="int4", **model_kwargs)
|
| 29 |
+
|
| 30 |
+
return True
|
| 31 |
+
except Exception as e:
|
| 32 |
+
logger.error(f"Error loading model: {e}")
|
| 33 |
+
return False
|
| 34 |
```
|
| 35 |
|
| 36 |
+
### 2. Proper Inference with Cache Implementation
|
| 37 |
|
| 38 |
The most important fix is using `cache_implementation="static"` for generation:
|
| 39 |
|
|
|
|
| 47 |
attention_mask=inputs['attention_mask'],
|
| 48 |
pad_token_id=tokenizer.eos_token_id,
|
| 49 |
eos_token_id=tokenizer.eos_token_id,
|
| 50 |
+
cache_implementation="static" # CRITICAL for quantized models
|
| 51 |
)
|
| 52 |
```
|
| 53 |
|
| 54 |
+
## Why This Approach Works
|
| 55 |
|
| 56 |
+
### Avoiding Quantization Conflicts
|
| 57 |
+
|
| 58 |
+
The warning you saw:
|
| 59 |
+
```
|
| 60 |
+
You passed `quantization_config` or equivalent parameters to `from_pretrained` but the model you're loading already has a `quantization_config` attribute. The `quantization_config` from the model will be used.
|
| 61 |
+
```
|
| 62 |
|
| 63 |
+
This happens because:
|
| 64 |
+
1. Your model in the `int4` subfolder is already quantized
|
| 65 |
+
2. When you try to apply TorchAO quantization to an already quantized model, it conflicts
|
| 66 |
+
3. The solution is to load the pre-quantized model directly without additional quantization
|
| 67 |
|
| 68 |
+
### Benefits of Pre-Quantized Models
|
| 69 |
+
|
| 70 |
+
1. **No Quantization Overhead**: The model is already optimized
|
| 71 |
+
2. **Consistent Performance**: No runtime quantization variations
|
| 72 |
+
3. **Memory Efficient**: Already compressed for deployment
|
| 73 |
+
4. **Faster Loading**: No quantization step during loading
|
| 74 |
|
| 75 |
## Testing the Implementation
|
| 76 |
|
| 77 |
+
Run the test script to verify the pre-quantized model works:
|
| 78 |
|
| 79 |
```bash
|
| 80 |
+
python test_pre_quantized_model.py
|
| 81 |
```
|
| 82 |
|
| 83 |
This will test:
|
| 84 |
+
- Loading the pre-quantized model without conflicts
|
| 85 |
- Text generation with proper cache implementation
|
| 86 |
+
- Verification of quantization status
|
| 87 |
|
| 88 |
## Performance Benefits
|
| 89 |
|
| 90 |
+
1. **Memory Reduction**: Pre-quantized models use ~50% less memory
|
| 91 |
+
2. **Faster Loading**: No quantization step during model loading
|
| 92 |
+
3. **Consistent Performance**: No quantization variations between runs
|
| 93 |
+
4. **Optimized Kernels**: Pre-quantized models use optimized inference kernels
|
| 94 |
|
| 95 |
## Common Issues and Solutions
|
| 96 |
|
| 97 |
+
### Issue: Quantization config warning
|
| 98 |
+
**Solution**: Don't apply additional quantization to pre-quantized models
|
| 99 |
+
|
| 100 |
### Issue: Model outputs incorrect or garbled text
|
| 101 |
**Solution**: Ensure `cache_implementation="static"` is used in generation
|
| 102 |
|
| 103 |
### Issue: Memory errors during loading
|
| 104 |
+
**Solution**: Use `low_cpu_mem_usage=True` and appropriate device mapping
|
| 105 |
|
| 106 |
### Issue: Slow inference
|
| 107 |
**Solution**:
|
| 108 |
1. Use `cache_implementation="static"`
|
| 109 |
2. Consider using `torch.compile` for additional speedup
|
| 110 |
+
3. Monitor memory usage
|
|
|
|
|
|
|
| 111 |
|
| 112 |
+
## Model Structure
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
+
Your model repository should have this structure:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
```
|
| 116 |
+
Tonic/petite-elle-L-aime-3-sft/
|
| 117 |
+
├── int4/
|
| 118 |
+
│ ├── config.json
|
| 119 |
+
│ ├── pytorch_model.bin
|
| 120 |
+
│ ├── tokenizer.json
|
| 121 |
+
│ └── ...
|
| 122 |
+
├── README.md
|
| 123 |
+
└── ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
```
|
| 125 |
|
| 126 |
## Deployment Notes
|
| 127 |
|
| 128 |
+
1. **No Additional Quantization**: The model is already quantized
|
| 129 |
+
2. **Cache Implementation**: Always use `cache_implementation="static"`
|
| 130 |
+
3. **Memory Monitoring**: Pre-quantized models use less memory
|
| 131 |
+
4. **Performance**: Optimized for deployment without quantization overhead
|
| 132 |
|
| 133 |
## Troubleshooting
|
| 134 |
|
| 135 |
+
### Check Model Quantization
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
```python
|
| 137 |
# Check if model is quantized
|
| 138 |
for name, module in model.named_modules():
|
|
|
|
| 146 |
print(f"GPU Memory: {torch.cuda.memory_allocated() / 1024**3:.2f} GB")
|
| 147 |
```
|
| 148 |
|
| 149 |
+
### Verify Model Loading
|
| 150 |
+
```python
|
| 151 |
+
# Check model config
|
| 152 |
+
print(f"Model dtype: {model.dtype}")
|
| 153 |
+
print(f"Model device: {model.device}")
|
| 154 |
+
```
|
| 155 |
+
|
| 156 |
+
## Alternative: TorchAO Quantization
|
| 157 |
+
|
| 158 |
+
If you want to use TorchAO quantization instead of pre-quantized models:
|
| 159 |
+
|
| 160 |
+
1. **Load the base model** (not from int4 subfolder)
|
| 161 |
+
2. **Apply TorchAO quantization** during loading
|
| 162 |
+
3. **Use appropriate quantization configs** for your device
|
| 163 |
+
|
| 164 |
+
```python
|
| 165 |
+
from transformers import TorchAoConfig
|
| 166 |
+
from torchao.quantization import Int4WeightOnlyConfig
|
| 167 |
+
|
| 168 |
+
quant_config = Int4WeightOnlyConfig(group_size=128)
|
| 169 |
+
quantization_config = TorchAoConfig(quant_type=quant_config)
|
| 170 |
+
|
| 171 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 172 |
+
model_id, # Not subfolder="int4"
|
| 173 |
+
quantization_config=quantization_config,
|
| 174 |
+
device_map="auto",
|
| 175 |
+
torch_dtype=torch.float32,
|
| 176 |
+
)
|
| 177 |
+
```
|
| 178 |
+
|
| 179 |
+
This implementation ensures proper handling of pre-quantized models without quantization conflicts, with the critical `cache_implementation="static"` parameter for correct generation.
|
app.py
CHANGED
|
@@ -1,8 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 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
|
|
@@ -23,27 +21,59 @@ model = None
|
|
| 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
|
| 27 |
presentation1 = """
|
| 28 |
### 🎯 Features
|
| 29 |
- **Multilingual Support**: English, French, Italian, Portuguese, Chinese, Arabic
|
| 30 |
-
- **
|
| 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 |
-
* **
|
| 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 = """
|
| 43 |
## Join us :
|
| 44 |
🌟TeamTonic🌟 is always making cool demos! Join our active builder's 🛠️community 👻 [](https://discord.gg/qdfnvSPcqP) On 🤗Huggingface:[MultiTransformer](https://huggingface.co/MultiTransformer) On 🌐Github: [Tonic-AI](https://github.com/tonic-ai) & contribute to🌟 [Build Tonic](https://git.tonic-ai.com/contribute)🤗Big thanks to Yuvi Sharma and all the folks at huggingface for the community grant 🤗
|
| 45 |
"""
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
def download_chat_template():
|
| 49 |
"""Download the chat template from the main repository"""
|
|
@@ -66,20 +96,8 @@ def download_chat_template():
|
|
| 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
|
| 83 |
global model, tokenizer
|
| 84 |
|
| 85 |
try:
|
|
@@ -90,18 +108,14 @@ def load_model():
|
|
| 90 |
tokenizer.chat_template = chat_template
|
| 91 |
logger.info("Chat template downloaded and set successfully")
|
| 92 |
|
| 93 |
-
logger.info(f"Loading
|
| 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.
|
| 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,7 +124,7 @@ def load_model():
|
|
| 110 |
if tokenizer.pad_token_id is None:
|
| 111 |
tokenizer.pad_token_id = tokenizer.eos_token_id
|
| 112 |
|
| 113 |
-
logger.info("
|
| 114 |
return True
|
| 115 |
|
| 116 |
except Exception as e:
|
|
@@ -119,21 +133,39 @@ def load_model():
|
|
| 119 |
return False
|
| 120 |
|
| 121 |
|
| 122 |
-
def create_prompt(system_message, user_message, enable_thinking=True):
|
| 123 |
-
"""Create prompt using the model's chat template"""
|
| 124 |
try:
|
| 125 |
formatted_messages = []
|
| 126 |
if system_message and system_message.strip():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
formatted_messages.append({"role": "system", "content": system_message})
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
return prompt
|
| 139 |
|
|
@@ -142,14 +174,23 @@ def create_prompt(system_message, user_message, enable_thinking=True):
|
|
| 142 |
return ""
|
| 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
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
if not full_prompt:
|
| 155 |
return "Error: Failed to create prompt."
|
|
@@ -166,6 +207,7 @@ def generate_response(message, history, system_message, max_tokens, temperature,
|
|
| 166 |
max_new_tokens=max_tokens,
|
| 167 |
temperature=temperature,
|
| 168 |
top_p=top_p,
|
|
|
|
| 169 |
do_sample=do_sample,
|
| 170 |
attention_mask=inputs['attention_mask'],
|
| 171 |
pad_token_id=tokenizer.eos_token_id,
|
|
@@ -178,6 +220,19 @@ def generate_response(message, history, system_message, max_tokens, temperature,
|
|
| 178 |
if not enable_thinking:
|
| 179 |
assistant_response = re.sub(r'<think>.*?</think>', '', assistant_response, flags=re.DOTALL)
|
| 180 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
assistant_response = assistant_response.strip()
|
| 182 |
|
| 183 |
return assistant_response
|
|
@@ -188,14 +243,20 @@ def user(user_message, history):
|
|
| 188 |
history = []
|
| 189 |
return "", history + [{"role": "user", "content": user_message}]
|
| 190 |
|
| 191 |
-
def bot(history, system_prompt, max_length, temperature, top_p, advanced_checkbox, enable_thinking):
|
| 192 |
"""Generate bot response"""
|
| 193 |
if not history:
|
| 194 |
return history
|
| 195 |
user_message = history[-1]["content"] if history else ""
|
| 196 |
|
| 197 |
do_sample = advanced_checkbox
|
| 198 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
history.append({"role": "assistant", "content": bot_message})
|
| 200 |
return history
|
| 201 |
|
|
@@ -241,25 +302,41 @@ with gr.Blocks() as demo:
|
|
| 241 |
max_length = gr.Slider(
|
| 242 |
label="📏 Longueur de la réponse",
|
| 243 |
minimum=10,
|
| 244 |
-
maximum=556,
|
| 245 |
-
value=
|
| 246 |
step=1
|
| 247 |
)
|
| 248 |
temperature = gr.Slider(
|
| 249 |
label="🌡️ Température",
|
| 250 |
minimum=0.01,
|
| 251 |
maximum=1.0,
|
| 252 |
-
value=0.
|
| 253 |
step=0.01
|
| 254 |
)
|
| 255 |
top_p = gr.Slider(
|
| 256 |
label="⚛️ Top-p (Echantillonnage)",
|
| 257 |
minimum=0.1,
|
| 258 |
maximum=1.0,
|
| 259 |
-
value=0.95,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
step=0.01
|
| 261 |
)
|
| 262 |
enable_thinking = gr.Checkbox(label="Mode Réflexion", value=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
|
| 264 |
generate_button = gr.Button(value="🤖 Petite Elle L'Aime 3")
|
| 265 |
|
|
@@ -273,7 +350,7 @@ with gr.Blocks() as demo:
|
|
| 273 |
queue=False
|
| 274 |
).then(
|
| 275 |
bot,
|
| 276 |
-
[chatbot, system_prompt, max_length, temperature, top_p, advanced_checkbox, enable_thinking],
|
| 277 |
chatbot
|
| 278 |
)
|
| 279 |
|
|
@@ -282,6 +359,12 @@ with gr.Blocks() as demo:
|
|
| 282 |
inputs=[advanced_checkbox],
|
| 283 |
outputs=[advanced_settings]
|
| 284 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 285 |
|
| 286 |
if __name__ == "__main__":
|
| 287 |
demo.queue()
|
|
|
|
| 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
|
|
|
|
| 21 |
tokenizer = None
|
| 22 |
DEFAULT_SYSTEM_PROMPT = "Tu es TonicIA, un assistant francophone rigoureux et bienveillant."
|
| 23 |
title = "# 🤖 Petite Elle L'Aime 3 - Chat Interface"
|
| 24 |
+
description = "A fine-tuned version of SmolLM3-3B optimized for French conversations. This is the pre-quantized int4 version for efficient deployment."
|
| 25 |
presentation1 = """
|
| 26 |
### 🎯 Features
|
| 27 |
- **Multilingual Support**: English, French, Italian, Portuguese, Chinese, Arabic
|
| 28 |
+
- **Pre-Quantized Int4**: Optimized for deployment with memory reduction
|
| 29 |
- **Interactive Chat Interface**: Real-time conversation with the model
|
| 30 |
- **Customizable System Prompt**: Define the assistant's personality and behavior
|
| 31 |
- **Thinking Mode**: Enable reasoning mode with thinking tags
|
| 32 |
+
- **Tool Calling**: Support for function calling with XML and Python tools
|
| 33 |
"""
|
| 34 |
presentation2 = """### 🎯 Fonctionnalités
|
| 35 |
* **Support multilingue** : Anglais, Français, Italien, Portugais, Chinois, Arabe
|
| 36 |
+
* **Pré-quantifié Int4** : Optimisé pour un déploiement avec réduction de mémoire
|
| 37 |
* **Interface de chat interactive** : Conversation en temps réel avec le modèle
|
| 38 |
* **Invite système personnalisable** : Définissez la personnalité et le comportement de l'assistant
|
| 39 |
* **Mode Réflexion** : Activez le mode raisonnement avec des balises de réflexion
|
| 40 |
+
* **Appel d'outils** : Support pour l'appel de fonctions avec XML et Python
|
| 41 |
"""
|
| 42 |
joinus = """
|
| 43 |
## Join us :
|
| 44 |
🌟TeamTonic🌟 is always making cool demos! Join our active builder's 🛠️community 👻 [](https://discord.gg/qdfnvSPcqP) On 🤗Huggingface:[MultiTransformer](https://huggingface.co/MultiTransformer) On 🌐Github: [Tonic-AI](https://github.com/tonic-ai) & contribute to🌟 [Build Tonic](https://git.tonic-ai.com/contribute)🤗Big thanks to Yuvi Sharma and all the folks at huggingface for the community grant 🤗
|
| 45 |
"""
|
| 46 |
|
| 47 |
+
# Default tool definition for demonstration
|
| 48 |
+
DEFAULT_TOOLS = [
|
| 49 |
+
{
|
| 50 |
+
"name": "get_weather",
|
| 51 |
+
"description": "Get the weather in a city",
|
| 52 |
+
"parameters": {
|
| 53 |
+
"type": "object",
|
| 54 |
+
"properties": {
|
| 55 |
+
"city": {
|
| 56 |
+
"type": "string",
|
| 57 |
+
"description": "The city to get the weather for"
|
| 58 |
+
}
|
| 59 |
+
}
|
| 60 |
+
}
|
| 61 |
+
},
|
| 62 |
+
{
|
| 63 |
+
"name": "calculate",
|
| 64 |
+
"description": "Perform mathematical calculations",
|
| 65 |
+
"parameters": {
|
| 66 |
+
"type": "object",
|
| 67 |
+
"properties": {
|
| 68 |
+
"expression": {
|
| 69 |
+
"type": "string",
|
| 70 |
+
"description": "Mathematical expression to evaluate"
|
| 71 |
+
}
|
| 72 |
+
}
|
| 73 |
+
}
|
| 74 |
+
}
|
| 75 |
+
]
|
| 76 |
+
|
| 77 |
|
| 78 |
def download_chat_template():
|
| 79 |
"""Download the chat template from the main repository"""
|
|
|
|
| 96 |
return None
|
| 97 |
|
| 98 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
def load_model():
|
| 100 |
+
"""Load the pre-quantized model and tokenizer"""
|
| 101 |
global model, tokenizer
|
| 102 |
|
| 103 |
try:
|
|
|
|
| 108 |
tokenizer.chat_template = chat_template
|
| 109 |
logger.info("Chat template downloaded and set successfully")
|
| 110 |
|
| 111 |
+
logger.info(f"Loading pre-quantized int4 model from {MAIN_MODEL_ID}/int4")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
+
# Load the pre-quantized model without additional quantization config
|
| 114 |
model_kwargs = {
|
| 115 |
"device_map": "auto" if DEVICE == "cuda" else "cpu",
|
| 116 |
+
"torch_dtype": torch.float32, # Use float32 for compatibility
|
| 117 |
"trust_remote_code": True,
|
| 118 |
"low_cpu_mem_usage": True,
|
|
|
|
| 119 |
}
|
| 120 |
|
| 121 |
logger.info(f"Model loading parameters: {model_kwargs}")
|
|
|
|
| 124 |
if tokenizer.pad_token_id is None:
|
| 125 |
tokenizer.pad_token_id = tokenizer.eos_token_id
|
| 126 |
|
| 127 |
+
logger.info("Pre-quantized model loaded successfully")
|
| 128 |
return True
|
| 129 |
|
| 130 |
except Exception as e:
|
|
|
|
| 133 |
return False
|
| 134 |
|
| 135 |
|
| 136 |
+
def create_prompt(system_message, user_message, enable_thinking=True, tools=None, use_xml_tools=True):
|
| 137 |
+
"""Create prompt using the model's chat template with SmolLM3 features"""
|
| 138 |
try:
|
| 139 |
formatted_messages = []
|
| 140 |
if system_message and system_message.strip():
|
| 141 |
+
# Check if thinking flags are already present
|
| 142 |
+
has_think_flag = "/think" in system_message
|
| 143 |
+
has_no_think_flag = "/no_think" in system_message
|
| 144 |
+
|
| 145 |
+
# Add thinking flag to system message if needed
|
| 146 |
+
if not enable_thinking and not has_no_think_flag:
|
| 147 |
+
system_message += "/no_think"
|
| 148 |
+
elif enable_thinking and not has_think_flag and not has_no_think_flag:
|
| 149 |
+
system_message += "/think"
|
| 150 |
formatted_messages.append({"role": "system", "content": system_message})
|
| 151 |
+
|
| 152 |
+
formatted_messages.append({"role": "user", "content": user_message})
|
| 153 |
+
|
| 154 |
+
# Apply chat template with SmolLM3 features
|
| 155 |
+
template_kwargs = {
|
| 156 |
+
"tokenize": False,
|
| 157 |
+
"add_generation_prompt": True,
|
| 158 |
+
"enable_thinking": enable_thinking
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
# Add tool calling if tools are provided
|
| 162 |
+
if tools and len(tools) > 0:
|
| 163 |
+
if use_xml_tools:
|
| 164 |
+
template_kwargs["xml_tools"] = tools
|
| 165 |
+
else:
|
| 166 |
+
template_kwargs["python_tools"] = tools
|
| 167 |
+
|
| 168 |
+
prompt = tokenizer.apply_chat_template(formatted_messages, **template_kwargs)
|
| 169 |
|
| 170 |
return prompt
|
| 171 |
|
|
|
|
| 174 |
return ""
|
| 175 |
|
| 176 |
@spaces.GPU(duration=94)
|
| 177 |
+
def generate_response(message, history, system_message, max_tokens, temperature, top_p, repetition_penalty, do_sample, enable_thinking=True, tools=None, use_xml_tools=True):
|
| 178 |
+
"""Generate response using the pre-quantized model with SmolLM3 features"""
|
| 179 |
global model, tokenizer
|
| 180 |
|
| 181 |
if model is None or tokenizer is None:
|
| 182 |
return "Error: Model not loaded. Please wait for the model to load."
|
| 183 |
|
| 184 |
+
# Parse tools from string if provided
|
| 185 |
+
parsed_tools = None
|
| 186 |
+
if tools and tools.strip():
|
| 187 |
+
try:
|
| 188 |
+
parsed_tools = json.loads(tools)
|
| 189 |
+
except json.JSONDecodeError as e:
|
| 190 |
+
logger.error(f"Error parsing tools JSON: {e}")
|
| 191 |
+
return "Error: Invalid tool definition JSON format."
|
| 192 |
+
|
| 193 |
+
full_prompt = create_prompt(system_message, message, enable_thinking, parsed_tools, use_xml_tools)
|
| 194 |
|
| 195 |
if not full_prompt:
|
| 196 |
return "Error: Failed to create prompt."
|
|
|
|
| 207 |
max_new_tokens=max_tokens,
|
| 208 |
temperature=temperature,
|
| 209 |
top_p=top_p,
|
| 210 |
+
repetition_penalty=repetition_penalty,
|
| 211 |
do_sample=do_sample,
|
| 212 |
attention_mask=inputs['attention_mask'],
|
| 213 |
pad_token_id=tokenizer.eos_token_id,
|
|
|
|
| 220 |
if not enable_thinking:
|
| 221 |
assistant_response = re.sub(r'<think>.*?</think>', '', assistant_response, flags=re.DOTALL)
|
| 222 |
|
| 223 |
+
# Handle tool calls if present
|
| 224 |
+
if parsed_tools and ("<tool_call>" in assistant_response or "<code>" in assistant_response):
|
| 225 |
+
if "<tool_call>" in assistant_response:
|
| 226 |
+
tool_call_match = re.search(r'<tool_call>(.*?)</tool_call>', assistant_response, re.DOTALL)
|
| 227 |
+
if tool_call_match:
|
| 228 |
+
tool_call = tool_call_match.group(1)
|
| 229 |
+
assistant_response += f"\n\n🔧 Tool Call Detected: {tool_call}\n\nNote: This is a simulated tool call. In a real scenario, the tool would be executed and its output would be used to generate a final response."
|
| 230 |
+
elif "<code>" in assistant_response:
|
| 231 |
+
code_match = re.search(r'<code>(.*?)</code>', assistant_response, re.DOTALL)
|
| 232 |
+
if code_match:
|
| 233 |
+
code_call = code_match.group(1)
|
| 234 |
+
assistant_response += f"\n\n🐍 Python Tool Call: {code_call}\n\nNote: This is a simulated Python tool call. In a real scenario, the function would be executed and its output would be used to generate a final response."
|
| 235 |
+
|
| 236 |
assistant_response = assistant_response.strip()
|
| 237 |
|
| 238 |
return assistant_response
|
|
|
|
| 243 |
history = []
|
| 244 |
return "", history + [{"role": "user", "content": user_message}]
|
| 245 |
|
| 246 |
+
def bot(history, system_prompt, max_length, temperature, top_p, repetition_penalty, advanced_checkbox, enable_thinking, tools, use_xml_tools, use_tools):
|
| 247 |
"""Generate bot response"""
|
| 248 |
if not history:
|
| 249 |
return history
|
| 250 |
user_message = history[-1]["content"] if history else ""
|
| 251 |
|
| 252 |
do_sample = advanced_checkbox
|
| 253 |
+
|
| 254 |
+
tools_to_use = tools if use_tools else None
|
| 255 |
+
|
| 256 |
+
bot_message = generate_response(
|
| 257 |
+
user_message, history, system_prompt, max_length, temperature, top_p, repetition_penalty,
|
| 258 |
+
do_sample, enable_thinking, tools_to_use, use_xml_tools
|
| 259 |
+
)
|
| 260 |
history.append({"role": "assistant", "content": bot_message})
|
| 261 |
return history
|
| 262 |
|
|
|
|
| 302 |
max_length = gr.Slider(
|
| 303 |
label="📏 Longueur de la réponse",
|
| 304 |
minimum=10,
|
| 305 |
+
maximum=556, # maximum=32768,
|
| 306 |
+
value=56,
|
| 307 |
step=1
|
| 308 |
)
|
| 309 |
temperature = gr.Slider(
|
| 310 |
label="🌡️ Température",
|
| 311 |
minimum=0.01,
|
| 312 |
maximum=1.0,
|
| 313 |
+
value=0.6, # Updated to SmolLM3 recommended
|
| 314 |
step=0.01
|
| 315 |
)
|
| 316 |
top_p = gr.Slider(
|
| 317 |
label="⚛️ Top-p (Echantillonnage)",
|
| 318 |
minimum=0.1,
|
| 319 |
maximum=1.0,
|
| 320 |
+
value=0.95,
|
| 321 |
+
step=0.01
|
| 322 |
+
)
|
| 323 |
+
repetition_penalty = gr.Slider(
|
| 324 |
+
label="🔄 Répétition Penalty",
|
| 325 |
+
minimum=1.0,
|
| 326 |
+
maximum=2.0,
|
| 327 |
+
value=1.1,
|
| 328 |
step=0.01
|
| 329 |
)
|
| 330 |
enable_thinking = gr.Checkbox(label="Mode Réflexion", value=True)
|
| 331 |
+
use_tools = gr.Checkbox(label="🔧 Enable Tool Calling", value=False)
|
| 332 |
+
use_xml_tools = gr.Checkbox(label="📋 Use XML Tools (vs Python)", value=True)
|
| 333 |
+
with gr.Column(visible=False) as tool_options:
|
| 334 |
+
tools = gr.Code(
|
| 335 |
+
label="Tool Definition (JSON)",
|
| 336 |
+
value=json.dumps(DEFAULT_TOOLS, indent=2),
|
| 337 |
+
lines=15,
|
| 338 |
+
language="json"
|
| 339 |
+
)
|
| 340 |
|
| 341 |
generate_button = gr.Button(value="🤖 Petite Elle L'Aime 3")
|
| 342 |
|
|
|
|
| 350 |
queue=False
|
| 351 |
).then(
|
| 352 |
bot,
|
| 353 |
+
[chatbot, system_prompt, max_length, temperature, top_p, repetition_penalty, advanced_checkbox, enable_thinking, tools, use_xml_tools, use_tools],
|
| 354 |
chatbot
|
| 355 |
)
|
| 356 |
|
|
|
|
| 359 |
inputs=[advanced_checkbox],
|
| 360 |
outputs=[advanced_settings]
|
| 361 |
)
|
| 362 |
+
|
| 363 |
+
use_tools.change(
|
| 364 |
+
fn=lambda x: gr.update(visible=x),
|
| 365 |
+
inputs=[use_tools],
|
| 366 |
+
outputs=[tool_options]
|
| 367 |
+
)
|
| 368 |
|
| 369 |
if __name__ == "__main__":
|
| 370 |
demo.queue()
|
test_pre_quantized_model.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Test script for pre-quantized model inference
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import torch
|
| 7 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 8 |
+
import logging
|
| 9 |
+
|
| 10 |
+
# Set up logging
|
| 11 |
+
logging.basicConfig(level=logging.INFO)
|
| 12 |
+
logger = logging.getLogger(__name__)
|
| 13 |
+
|
| 14 |
+
def test_pre_quantized_model():
|
| 15 |
+
"""Test the pre-quantized model loading and generation"""
|
| 16 |
+
|
| 17 |
+
model_id = "Tonic/petite-elle-L-aime-3-sft"
|
| 18 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 19 |
+
|
| 20 |
+
logger.info(f"Testing pre-quantized model on device: {device}")
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
# Load tokenizer
|
| 24 |
+
logger.info("Loading tokenizer...")
|
| 25 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, subfolder="int4")
|
| 26 |
+
if tokenizer.pad_token_id is None:
|
| 27 |
+
tokenizer.pad_token_id = tokenizer.eos_token_id
|
| 28 |
+
|
| 29 |
+
# Load pre-quantized model
|
| 30 |
+
logger.info("Loading pre-quantized model...")
|
| 31 |
+
model_kwargs = {
|
| 32 |
+
"device_map": "auto" if device == "cuda" else "cpu",
|
| 33 |
+
"torch_dtype": torch.float32,
|
| 34 |
+
"trust_remote_code": True,
|
| 35 |
+
"low_cpu_mem_usage": True,
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, subfolder="int4", **model_kwargs)
|
| 39 |
+
|
| 40 |
+
# Test generation
|
| 41 |
+
test_prompt = "Bonjour, comment allez-vous?"
|
| 42 |
+
inputs = tokenizer(test_prompt, return_tensors="pt")
|
| 43 |
+
|
| 44 |
+
if device == "cuda":
|
| 45 |
+
inputs = {k: v.cuda() for k, v in inputs.items()}
|
| 46 |
+
|
| 47 |
+
logger.info("Generating response...")
|
| 48 |
+
with torch.no_grad():
|
| 49 |
+
output_ids = model.generate(
|
| 50 |
+
inputs['input_ids'],
|
| 51 |
+
max_new_tokens=50,
|
| 52 |
+
temperature=0.7,
|
| 53 |
+
top_p=0.95,
|
| 54 |
+
do_sample=True,
|
| 55 |
+
attention_mask=inputs['attention_mask'],
|
| 56 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 57 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 58 |
+
cache_implementation="static" # Important for quantized models
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
response = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 62 |
+
assistant_response = response[len(test_prompt):].strip()
|
| 63 |
+
|
| 64 |
+
logger.info("✅ Pre-quantized model test successful!")
|
| 65 |
+
logger.info(f"Input: {test_prompt}")
|
| 66 |
+
logger.info(f"Output: {assistant_response}")
|
| 67 |
+
|
| 68 |
+
# Check model quantization status
|
| 69 |
+
logger.info("Checking model quantization status...")
|
| 70 |
+
quantized_layers = 0
|
| 71 |
+
total_layers = 0
|
| 72 |
+
for name, module in model.named_modules():
|
| 73 |
+
if hasattr(module, 'weight'):
|
| 74 |
+
total_layers += 1
|
| 75 |
+
if module.weight.dtype != torch.float32:
|
| 76 |
+
quantized_layers += 1
|
| 77 |
+
logger.info(f"Quantized layer: {name} - {module.weight.dtype}")
|
| 78 |
+
|
| 79 |
+
logger.info(f"Quantized layers: {quantized_layers}/{total_layers}")
|
| 80 |
+
|
| 81 |
+
# Clean up
|
| 82 |
+
del model
|
| 83 |
+
torch.cuda.empty_cache() if device == "cuda" else None
|
| 84 |
+
|
| 85 |
+
except Exception as e:
|
| 86 |
+
logger.error(f"❌ Pre-quantized model test failed: {e}")
|
| 87 |
+
import traceback
|
| 88 |
+
traceback.print_exc()
|
| 89 |
+
|
| 90 |
+
if __name__ == "__main__":
|
| 91 |
+
test_pre_quantized_model()
|
test_smollm3_features.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Test script for SmolLM3 features in the Petite Elle L'Aime 3 app
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import json
|
| 7 |
+
import sys
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
# Add the current directory to the path so we can import from app.py
|
| 11 |
+
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
| 12 |
+
|
| 13 |
+
def test_smollm3_features():
|
| 14 |
+
"""Test the SmolLM3 features implementation"""
|
| 15 |
+
|
| 16 |
+
# Test tool definitions
|
| 17 |
+
test_tools = [
|
| 18 |
+
{
|
| 19 |
+
"name": "get_weather",
|
| 20 |
+
"description": "Get the weather in a city",
|
| 21 |
+
"parameters": {
|
| 22 |
+
"type": "object",
|
| 23 |
+
"properties": {
|
| 24 |
+
"city": {
|
| 25 |
+
"type": "string",
|
| 26 |
+
"description": "The city to get the weather for"
|
| 27 |
+
}
|
| 28 |
+
}
|
| 29 |
+
}
|
| 30 |
+
}
|
| 31 |
+
]
|
| 32 |
+
|
| 33 |
+
print("✅ Test tool definition format:")
|
| 34 |
+
print(json.dumps(test_tools, indent=2))
|
| 35 |
+
|
| 36 |
+
# Test thinking flags
|
| 37 |
+
test_system_prompts = [
|
| 38 |
+
"Tu es TonicIA, un assistant francophone rigoureux et bienveillant./think",
|
| 39 |
+
"Tu es TonicIA, un assistant francophone rigoureux et bienveillant./no_think",
|
| 40 |
+
"Tu es TonicIA, un assistant francophone rigoureux et bienveillant."
|
| 41 |
+
]
|
| 42 |
+
|
| 43 |
+
print("\n✅ Test system prompts with thinking flags:")
|
| 44 |
+
for i, prompt in enumerate(test_system_prompts, 1):
|
| 45 |
+
print(f"{i}. {prompt}")
|
| 46 |
+
|
| 47 |
+
# Test generation parameters
|
| 48 |
+
recommended_params = {
|
| 49 |
+
"temperature": 0.6,
|
| 50 |
+
"top_p": 0.95,
|
| 51 |
+
"repetition_penalty": 1.1,
|
| 52 |
+
"max_new_tokens": 2048,
|
| 53 |
+
"do_sample": True
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
print("\n✅ SmolLM3 recommended generation parameters:")
|
| 57 |
+
for param, value in recommended_params.items():
|
| 58 |
+
print(f" {param}: {value}")
|
| 59 |
+
|
| 60 |
+
print("\n✅ SmolLM3 features implemented:")
|
| 61 |
+
print(" - Thinking mode with /think and /no_think flags")
|
| 62 |
+
print(" - Tool calling with XML and Python tools")
|
| 63 |
+
print(" - Recommended generation parameters")
|
| 64 |
+
print(" - Long context support (up to 32,768 tokens)")
|
| 65 |
+
print(" - Agentic usage with tool calling")
|
| 66 |
+
|
| 67 |
+
return True
|
| 68 |
+
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
test_smollm3_features()
|
| 71 |
+
print("\n🎉 All SmolLM3 features are properly configured!")
|