Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,27 @@
|
|
| 1 |
# app.py
|
| 2 |
-
import math, json, random, time, threading
|
| 3 |
from dataclasses import dataclass, asdict
|
| 4 |
from typing import List, Tuple, Dict, Any, Optional
|
|
|
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
import plotly.graph_objs as go
|
| 7 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# =========================
|
| 10 |
# UX THEME & STYLES
|
| 11 |
# =========================
|
| 12 |
CUSTOM_CSS = """
|
| 13 |
-
:root {
|
| 14 |
-
--radius-2xl: 20px;
|
| 15 |
-
}
|
| 16 |
.gradio-container {max-width: 1400px !important}
|
| 17 |
#header-card {border-radius: var(--radius-2xl); box-shadow: 0 6px 24px rgba(0,0,0,0.08)}
|
| 18 |
#viz-card, #right-card, #table-card {border-radius: var(--radius-2xl); box-shadow: 0 6px 24px rgba(0,0,0,0.06)}
|
|
@@ -84,26 +93,121 @@ def crossover(a: Genome, b: Genome, rng: random.Random) -> Genome:
|
|
| 84 |
)
|
| 85 |
|
| 86 |
# =========================
|
| 87 |
-
# FITNESS
|
| 88 |
-
# Swap this later for real PIQA/HellaSwag evaluation
|
| 89 |
# =========================
|
| 90 |
def rastrigin(x: np.ndarray) -> float:
|
| 91 |
A, n = 10.0, x.shape[0]
|
| 92 |
return A * n + np.sum(x**2 - A * np.cos(2 * math.pi * x))
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
def fitness_hook(genome: Genome, dataset: str, explore: float) -> float:
|
| 95 |
-
"""
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
# =========================
|
| 109 |
# PROJECTION & VIZ
|
|
@@ -160,8 +264,7 @@ def approx_params(g: Genome) -> int:
|
|
| 160 |
# per-layer ~ (4 + 2*ffn_mult) * d_model^2
|
| 161 |
per_layer = (4.0 + 2.0 * float(g.ffn_mult)) * (g.d_model ** 2)
|
| 162 |
total = per_layer * g.n_layers
|
| 163 |
-
# tiny bump for memory
|
| 164 |
-
total += 1000 * g.memory_tokens
|
| 165 |
return int(total)
|
| 166 |
|
| 167 |
# =========================
|
|
@@ -303,13 +406,13 @@ def poll_state():
|
|
| 303 |
)
|
| 304 |
else:
|
| 305 |
stats_md = "Waiting… click **Start Evolution**."
|
| 306 |
-
import pandas as pd
|
| 307 |
df = pd.DataFrame(top)
|
| 308 |
return sphere, history, stats_md, df
|
| 309 |
|
| 310 |
def export_snapshot():
|
|
|
|
| 311 |
with runner.lock:
|
| 312 |
-
payload =
|
| 313 |
path = "evo_snapshot.json"
|
| 314 |
with open(path, "w", encoding="utf-8") as f:
|
| 315 |
f.write(payload)
|
|
@@ -334,7 +437,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=CUSTOM_CSS) as demo:
|
|
| 334 |
label="Dataset",
|
| 335 |
choices=["Demo (Surrogate)", "PIQA (Phase 2)", "HellaSwag (Phase 2)", "WikiText Perplexity (Phase 2)"],
|
| 336 |
value="Demo (Surrogate)",
|
| 337 |
-
info="Demo is instant.
|
| 338 |
)
|
| 339 |
pop = gr.Slider(8, 80, value=24, step=2, label="Population size")
|
| 340 |
gens = gr.Slider(5, 200, value=60, step=1, label="Max generations")
|
|
@@ -350,7 +453,6 @@ with gr.Blocks(theme=gr.themes.Soft(), css=CUSTOM_CSS) as demo:
|
|
| 350 |
|
| 351 |
with gr.Group(elem_id="right-card"):
|
| 352 |
stats_md = gr.Markdown("Waiting…")
|
| 353 |
-
|
| 354 |
export_btn = gr.Button("Export Snapshot (JSON)")
|
| 355 |
export_file = gr.File(label="Download snapshot", visible=False)
|
| 356 |
|
|
|
|
| 1 |
# app.py
|
| 2 |
+
import math, json, random, time, threading
|
| 3 |
from dataclasses import dataclass, asdict
|
| 4 |
from typing import List, Tuple, Dict, Any, Optional
|
| 5 |
+
from functools import lru_cache
|
| 6 |
+
|
| 7 |
import numpy as np
|
| 8 |
import plotly.graph_objs as go
|
| 9 |
import gradio as gr
|
| 10 |
+
import pandas as pd
|
| 11 |
+
|
| 12 |
+
# New deps for proxy fitness
|
| 13 |
+
import torch
|
| 14 |
+
import torch.nn as nn
|
| 15 |
+
import torch.optim as optim
|
| 16 |
+
|
| 17 |
+
# Local utils (add this file next to app.py)
|
| 18 |
+
from data_utils import load_piqa, load_hellaswag, hash_vectorize
|
| 19 |
|
| 20 |
# =========================
|
| 21 |
# UX THEME & STYLES
|
| 22 |
# =========================
|
| 23 |
CUSTOM_CSS = """
|
| 24 |
+
:root { --radius-2xl: 20px; }
|
|
|
|
|
|
|
| 25 |
.gradio-container {max-width: 1400px !important}
|
| 26 |
#header-card {border-radius: var(--radius-2xl); box-shadow: 0 6px 24px rgba(0,0,0,0.08)}
|
| 27 |
#viz-card, #right-card, #table-card {border-radius: var(--radius-2xl); box-shadow: 0 6px 24px rgba(0,0,0,0.06)}
|
|
|
|
| 93 |
)
|
| 94 |
|
| 95 |
# =========================
|
| 96 |
+
# PROXY FITNESS (Phase 2a)
|
|
|
|
| 97 |
# =========================
|
| 98 |
def rastrigin(x: np.ndarray) -> float:
|
| 99 |
A, n = 10.0, x.shape[0]
|
| 100 |
return A * n + np.sum(x**2 - A * np.cos(2 * math.pi * x))
|
| 101 |
|
| 102 |
+
class TinyMLP(nn.Module):
|
| 103 |
+
"""Small MLP whose capacity depends on the genome (so evolution matters)."""
|
| 104 |
+
def __init__(self, in_dim: int, genome: Genome):
|
| 105 |
+
super().__init__()
|
| 106 |
+
h1 = max(64, int(0.25 * genome.d_model))
|
| 107 |
+
h2 = max(32, int(genome.ffn_mult * 32))
|
| 108 |
+
self.net = nn.Sequential(
|
| 109 |
+
nn.Linear(in_dim, h1), nn.ReLU(),
|
| 110 |
+
nn.Linear(h1, h2), nn.ReLU(),
|
| 111 |
+
nn.Linear(h2, 1)
|
| 112 |
+
)
|
| 113 |
+
def forward(self, x):
|
| 114 |
+
return self.net(x).squeeze(-1)
|
| 115 |
+
|
| 116 |
+
@lru_cache(maxsize=4)
|
| 117 |
+
def _cached_dataset(name: str):
|
| 118 |
+
if name.startswith("PIQA"):
|
| 119 |
+
return load_piqa(subset=800, seed=42)
|
| 120 |
+
if name.startswith("HellaSwag"):
|
| 121 |
+
return load_hellaswag(subset=800, seed=42)
|
| 122 |
+
return None # Demo uses surrogate
|
| 123 |
+
|
| 124 |
+
def _train_eval_proxy(genome: Genome, dataset_name: str, explore: float, device: str = "cpu") -> Optional[float]:
|
| 125 |
+
data = _cached_dataset(dataset_name)
|
| 126 |
+
if data is None:
|
| 127 |
+
return None
|
| 128 |
+
Xtr_txt, ytr, Xva_txt, yva = data
|
| 129 |
+
|
| 130 |
+
# Hash vectorize to fixed dimension (fast, no tokenizer)
|
| 131 |
+
nfeat = 4096
|
| 132 |
+
Xtr = hash_vectorize(Xtr_txt, n_features=nfeat, seed=1234)
|
| 133 |
+
Xva = hash_vectorize(Xva_txt, n_features=nfeat, seed=5678)
|
| 134 |
+
|
| 135 |
+
# to torch tensors
|
| 136 |
+
Xtr_t = torch.from_numpy(Xtr)
|
| 137 |
+
ytr_t = torch.from_numpy(ytr.astype(np.float32))
|
| 138 |
+
Xva_t = torch.from_numpy(Xva)
|
| 139 |
+
yva_t = torch.from_numpy(yva.astype(np.float32))
|
| 140 |
+
|
| 141 |
+
model = TinyMLP(nfeat, genome).to(device)
|
| 142 |
+
opt = optim.AdamW(model.parameters(), lr=2e-3)
|
| 143 |
+
lossf = nn.BCEWithLogitsLoss()
|
| 144 |
+
|
| 145 |
+
# small, fast loop
|
| 146 |
+
model.train()
|
| 147 |
+
steps = 120
|
| 148 |
+
bs = 256
|
| 149 |
+
N = Xtr_t.size(0)
|
| 150 |
+
for _ in range(steps):
|
| 151 |
+
idx = torch.randint(0, N, (bs,))
|
| 152 |
+
xb = Xtr_t[idx].to(device)
|
| 153 |
+
yb = ytr_t[idx].to(device)
|
| 154 |
+
logits = model(xb)
|
| 155 |
+
loss = lossf(logits, yb)
|
| 156 |
+
opt.zero_grad()
|
| 157 |
+
loss.backward()
|
| 158 |
+
torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
|
| 159 |
+
opt.step()
|
| 160 |
+
|
| 161 |
+
# eval
|
| 162 |
+
model.eval()
|
| 163 |
+
with torch.no_grad():
|
| 164 |
+
logits = model(Xva_t.to(device))
|
| 165 |
+
probs = torch.sigmoid(logits).cpu().numpy()
|
| 166 |
+
|
| 167 |
+
# Turn rows into accuracy
|
| 168 |
+
if dataset_name.startswith("PIQA"):
|
| 169 |
+
# rows in pairs [A,B]; label vector marks which row is positive
|
| 170 |
+
probs = probs.reshape(-1, 2)
|
| 171 |
+
yva2 = yva.reshape(-1, 2)
|
| 172 |
+
pred = (probs[:, 0] > probs[:, 1]).astype(np.int64)
|
| 173 |
+
truth = (yva2[:, 0] == 1).astype(np.int64) # 1 means first row is correct
|
| 174 |
+
acc = float((pred == truth).mean())
|
| 175 |
+
else:
|
| 176 |
+
# HellaSwag: groups of 4; pick argmax
|
| 177 |
+
probs = probs.reshape(-1, 4)
|
| 178 |
+
yva2 = yva.reshape(-1, 4)
|
| 179 |
+
pred = probs.argmax(axis=1)
|
| 180 |
+
truth = yva2.argmax(axis=1)
|
| 181 |
+
acc = float((pred == truth).mean())
|
| 182 |
+
|
| 183 |
+
# Fitness = error + tiny parsimony + small exploration noise (minimize)
|
| 184 |
+
parsimony = 0.00000002 * (genome.d_model**2 * genome.n_layers) + 0.0001 * genome.memory_tokens
|
| 185 |
+
noise = np.random.normal(scale=0.01 * max(0.0, min(1.0, explore)))
|
| 186 |
+
fitness = (1.0 - acc) + parsimony + noise
|
| 187 |
+
return float(max(0.0, min(1.5, fitness)))
|
| 188 |
+
|
| 189 |
def fitness_hook(genome: Genome, dataset: str, explore: float) -> float:
|
| 190 |
+
"""Selects the correct fitness path based on dropdown."""
|
| 191 |
+
if dataset == "Demo (Surrogate)":
|
| 192 |
+
v = genome.vector() * 2 - 1
|
| 193 |
+
base = rastrigin(v)
|
| 194 |
+
parsimony = 0.001 * (genome.d_model + 50*genome.n_layers + 20*genome.n_heads + 100*genome.memory_tokens)
|
| 195 |
+
noise = np.random.normal(scale=0.05 * max(0.0, min(1.0, explore)))
|
| 196 |
+
return float(base + parsimony + noise)
|
| 197 |
+
|
| 198 |
+
if dataset.startswith("PIQA"):
|
| 199 |
+
fit = _train_eval_proxy(genome, "PIQA", explore)
|
| 200 |
+
if fit is not None:
|
| 201 |
+
return fit
|
| 202 |
+
|
| 203 |
+
if dataset.startswith("HellaSwag"):
|
| 204 |
+
fit = _train_eval_proxy(genome, "HellaSwag", explore)
|
| 205 |
+
if fit is not None:
|
| 206 |
+
return fit
|
| 207 |
+
|
| 208 |
+
# fallback to surrogate if something went wrong
|
| 209 |
+
v = genome.vector() * 2 - 1
|
| 210 |
+
return float(rastrigin(v))
|
| 211 |
|
| 212 |
# =========================
|
| 213 |
# PROJECTION & VIZ
|
|
|
|
| 264 |
# per-layer ~ (4 + 2*ffn_mult) * d_model^2
|
| 265 |
per_layer = (4.0 + 2.0 * float(g.ffn_mult)) * (g.d_model ** 2)
|
| 266 |
total = per_layer * g.n_layers
|
| 267 |
+
total += 1000 * g.memory_tokens # tiny bump for memory pathways (illustrative)
|
|
|
|
| 268 |
return int(total)
|
| 269 |
|
| 270 |
# =========================
|
|
|
|
| 406 |
)
|
| 407 |
else:
|
| 408 |
stats_md = "Waiting… click **Start Evolution**."
|
|
|
|
| 409 |
df = pd.DataFrame(top)
|
| 410 |
return sphere, history, stats_md, df
|
| 411 |
|
| 412 |
def export_snapshot():
|
| 413 |
+
from json import dumps
|
| 414 |
with runner.lock:
|
| 415 |
+
payload = dumps(runner.state, default=lambda o: o, indent=2)
|
| 416 |
path = "evo_snapshot.json"
|
| 417 |
with open(path, "w", encoding="utf-8") as f:
|
| 418 |
f.write(payload)
|
|
|
|
| 437 |
label="Dataset",
|
| 438 |
choices=["Demo (Surrogate)", "PIQA (Phase 2)", "HellaSwag (Phase 2)", "WikiText Perplexity (Phase 2)"],
|
| 439 |
value="Demo (Surrogate)",
|
| 440 |
+
info="Demo is instant. PIQA/HellaSwag run a tiny CPU MLP proxy for real dataset fitness."
|
| 441 |
)
|
| 442 |
pop = gr.Slider(8, 80, value=24, step=2, label="Population size")
|
| 443 |
gens = gr.Slider(5, 200, value=60, step=1, label="Max generations")
|
|
|
|
| 453 |
|
| 454 |
with gr.Group(elem_id="right-card"):
|
| 455 |
stats_md = gr.Markdown("Waiting…")
|
|
|
|
| 456 |
export_btn = gr.Button("Export Snapshot (JSON)")
|
| 457 |
export_file = gr.File(label="Download snapshot", visible=False)
|
| 458 |
|