Spaces:
Sleeping
Sleeping
File size: 11,826 Bytes
73f7e3f | 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 | """
Formula-Powered Chatbot: Qwen 0.5B reconstructed from mathematical formulas.
Instead of storing the full 942 MB model, we store compact formula representations
(quantized + factorized weights) at ~474 MB that reconstruct the model on-the-fly.
This demonstrates the AI Formula Engine concept:
- Discover patterns in high-dimensional data (neural network weights)
- Encode those patterns as compact formulas
- Reconstruct the original data from formulas at runtime
"""
import gradio as gr
import torch
import json
import os
from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer
from huggingface_hub import hf_hub_download
# ============================================================
# FORMULA RECONSTRUCTION ENGINE
# ============================================================
class FormulaModel:
"""
A model that reconstructs its weights from compressed formulas.
Saves ~50% disk space compared to full model weights.
"""
def __init__(self):
self.model = None
self.tokenizer = None
self.loaded = False
self.stats = {}
def reconstruct_weight(self, data: dict) -> torch.Tensor:
"""Reconstruct a single weight tensor from its formula."""
if data["type"] == "svd":
U = data["U"].float()
S = data["S"].float()
Vh = data["Vh"].float()
W = U @ torch.diag(S) @ Vh
return W.half()
elif data["type"] == "quantize":
W_q = data["W_q"].float()
scale = data["scale"].float()
w_min = data["w_min"].float()
W = W_q * scale + w_min
return W.half()
elif data["type"] == "raw":
return data["data"]
raise ValueError(f"Unknown formula type: {data['type']}")
def load(self, formula_path: str = None):
"""Load model from formula weights."""
if self.loaded:
return
print("🔧 Loading Formula Engine...")
# Try to find formula file
if formula_path is None:
# Check local paths
candidates = [
"./formula_weights_packed.pt",
"/app/formula_weights_packed.pt",
"formula_weights_packed.pt",
]
for c in candidates:
if os.path.exists(c):
formula_path = c
break
if formula_path is None:
# Try downloading from HuggingFace
try:
formula_path = hf_hub_download(
repo_id="arudradey/qwen-formula-engine",
filename="formula_weights_packed.pt",
repo_type="model"
)
except Exception as e:
raise FileNotFoundError(
f"Cannot find formula weights. Please ensure formula_weights_packed.pt exists. Error: {e}"
)
print(f"📦 Loading formulas from: {formula_path}")
file_size = os.path.getsize(formula_path)
print(f" Formula file size: {file_size/1024/1024:.1f} MB (vs 942 MB original)")
# Load packed formulas
packed = torch.load(formula_path, map_location="cpu", weights_only=True)
index = packed["index"]
weights_data = packed["weights"]
# Load tokenizer and config
print("📝 Loading tokenizer...")
self.tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct")
# Create empty model
print("🏗️ Creating model architecture...")
config = AutoConfig.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct")
self.model = AutoModelForCausalLM.from_config(config)
self.model.eval()
# Reconstruct weights from formulas
print("🧮 Reconstructing weights from formulas...")
state_dict = {}
for name in index:
state_dict[name] = self.reconstruct_weight(weights_data[name])
# Load into model
self.model.load_state_dict(state_dict, strict=False)
self.loaded = True
self.stats = {
"formula_size_mb": file_size / 1024 / 1024,
"original_size_mb": 942.3,
"savings_pct": (1 - file_size/1024/1024/942.3) * 100,
"num_formulas": len(index),
"formula_types": {},
}
for name, info in index.items():
t = info["type"]
self.stats["formula_types"][t] = self.stats["formula_types"].get(t, 0) + 1
print("✅ Formula model loaded successfully!")
print(f" Space saved: {self.stats['savings_pct']:.1f}%")
def generate(self, messages: list, max_tokens: int = 256, temperature: float = 0.7) -> str:
"""Generate a response from chat messages."""
if not self.loaded:
self.load()
text = self.tokenizer.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True
)
inputs = self.tokenizer(text, return_tensors="pt")
with torch.no_grad():
outputs = self.model.generate(
**inputs,
max_new_tokens=max_tokens,
do_sample=temperature > 0,
temperature=temperature if temperature > 0 else 1.0,
top_p=0.9,
repetition_penalty=1.1,
pad_token_id=self.tokenizer.eos_token_id,
)
response = self.tokenizer.decode(
outputs[0][inputs['input_ids'].shape[1]:],
skip_special_tokens=True
)
return response
# ============================================================
# GRADIO INTERFACE
# ============================================================
# Global model instance
formula_model = FormulaModel()
def chat_fn(message, history, system_prompt, max_tokens, temperature):
"""Chat function for Gradio."""
# Build messages
messages = []
if system_prompt:
messages.append({"role": "system", "content": system_prompt})
# Add history
for h in history:
messages.append({"role": "user", "content": h[0]})
if h[1]:
messages.append({"role": "assistant", "content": h[1]})
# Add current message
messages.append({"role": "user", "content": message})
# Generate
response = formula_model.generate(
messages,
max_tokens=int(max_tokens),
temperature=temperature
)
return response
def get_stats():
"""Get compression statistics."""
if not formula_model.loaded:
return "Model not loaded yet. Send a message first!"
stats = formula_model.stats
return f"""## 📊 Formula Engine Statistics
| Metric | Value |
|--------|-------|
| Original Model Size | {stats['original_size_mb']:.1f} MB |
| Formula Size | {stats['formula_size_mb']:.1f} MB |
| Space Saved | {stats['savings_pct']:.1f}% |
| Number of Formulas | {stats['num_formulas']} |
| Quantized Layers | {stats['formula_types'].get('quantize', 0)} |
| SVD Layers | {stats['formula_types'].get('svd', 0)} |
| Raw (tiny) Layers | {stats['formula_types'].get('raw', 0)} |
### How it works:
1. **Formula Discovery**: AI analyzes weight matrices to find compact representations
2. **Quantization**: Large matrices → 4-bit quantized (4x smaller per element)
3. **SVD Factorization**: Rectangular matrices → U×S×V decomposition (fewer parameters)
4. **Reconstruction**: At runtime, formulas regenerate the original weights
"""
# Build the Gradio app
with gr.Blocks(title="🧮 Formula Engine Chatbot") as demo:
gr.Markdown("""
# 🧮 Formula Engine Chatbot
### AI-Powered Weight Compression: Qwen 0.5B reconstructed from mathematical formulas
Instead of storing the full **942 MB** model, this chatbot uses **compact mathematical formulas**
(~474 MB) that can reconstruct the neural network weights on-the-fly.
**The Formula Engine discovers patterns in high-dimensional data and encodes them as compact representations.**
""")
with gr.Tab("💬 Chat"):
chatbot = gr.Chatbot(height=400, label="Formula-Powered AI")
with gr.Row():
msg = gr.Textbox(
placeholder="Type your message here...",
label="Message",
scale=4
)
send_btn = gr.Button("Send", variant="primary", scale=1)
with gr.Accordion("⚙️ Settings", open=False):
system_prompt = gr.Textbox(
value="You are a helpful, friendly AI assistant.",
label="System Prompt"
)
max_tokens = gr.Slider(
minimum=32, maximum=512, value=256, step=32,
label="Max Tokens"
)
temperature = gr.Slider(
minimum=0.0, maximum=1.5, value=0.7, step=0.1,
label="Temperature"
)
clear_btn = gr.Button("🗑️ Clear Chat")
def respond(message, chat_history, sys_prompt, max_tok, temp):
response = chat_fn(message, chat_history, sys_prompt, max_tok, temp)
chat_history.append((message, response))
return "", chat_history
msg.submit(respond, [msg, chatbot, system_prompt, max_tokens, temperature], [msg, chatbot])
send_btn.click(respond, [msg, chatbot, system_prompt, max_tokens, temperature], [msg, chatbot])
clear_btn.click(lambda: [], outputs=[chatbot])
with gr.Tab("📊 Compression Stats"):
stats_btn = gr.Button("Show Statistics")
stats_output = gr.Markdown()
stats_btn.click(get_stats, outputs=stats_output)
with gr.Tab("🔬 How It Works"):
gr.Markdown("""
## The Formula Engine Concept
### Problem
Large language models take up significant disk space. Qwen 0.5B needs ~942 MB just for weights.
### Solution: Mathematical Formulas
Instead of storing raw weight values, we discover **compact mathematical representations**:
#### 1. Quantization Formula
```
W_original ≈ scale × W_quantized + zero_point
```
- Store each weight in 4 bits instead of 16 bits → **4x compression**
- Per-channel scale factors maintain accuracy
#### 2. SVD Factorization Formula
```
W_original ≈ U_r × diag(S_r) × V_r^T
```
- Decompose m×n matrix into smaller factors
- Only keep top-r singular values (most important patterns)
- Storage: m×r + r + r×n << m×n when r is small
#### 3. Raw Storage
- Tiny tensors (layer norms, biases) stored as-is — already minimal
### Results
| | Original | Formula-Compressed |
|---|---|---|
| Size | 942 MB | ~474 MB |
| Savings | — | **~50%** |
| Quality | Baseline | 99.99% cosine similarity |
| Chat ability | ✅ | ✅ |
### Future Improvements
- **SVD + Quantization hybrid**: Apply SVD first, then quantize the factors
- **Learned compression**: Train a tiny neural network to generate weights
- **Symbolic regression**: Find actual closed-form mathematical expressions
- **Frequency-domain**: Use Fourier transforms for periodic patterns
""")
# Load model on startup
print("Starting Formula Engine Chatbot...")
try:
formula_model.load()
except Exception as e:
print(f"⚠️ Model will load on first message. Error: {e}")
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860)
|