Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,20 @@
|
|
| 1 |
# app.py
|
| 2 |
# Coherent_Compute_Engine — RFTSystems
|
| 3 |
-
# Real, on-machine benchmark + tamper-evident receipt download (SHA-256).
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
# Notes:
|
| 5 |
-
# -
|
| 6 |
-
# -
|
|
|
|
| 7 |
|
| 8 |
import os
|
| 9 |
import json
|
| 10 |
import time
|
| 11 |
import math
|
|
|
|
| 12 |
import hashlib
|
| 13 |
import platform
|
| 14 |
import datetime as dt
|
|
@@ -17,7 +23,7 @@ from pathlib import Path
|
|
| 17 |
import numpy as np
|
| 18 |
import gradio as gr
|
| 19 |
|
| 20 |
-
# Optional: Numba
|
| 21 |
try:
|
| 22 |
import numba as nb
|
| 23 |
NUMBA_OK = True
|
|
@@ -25,7 +31,8 @@ except Exception:
|
|
| 25 |
nb = None
|
| 26 |
NUMBA_OK = False
|
| 27 |
|
| 28 |
-
|
|
|
|
| 29 |
RESULTS_DIR = Path("results")
|
| 30 |
RESULTS_DIR.mkdir(exist_ok=True)
|
| 31 |
|
|
@@ -43,12 +50,19 @@ def canon_json_bytes(obj) -> bytes:
|
|
| 43 |
def sha256_hex(b: bytes) -> str:
|
| 44 |
return hashlib.sha256(b).hexdigest()
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
def write_receipt(payload: dict) -> str:
|
| 47 |
"""
|
| 48 |
Writes a JSON receipt to disk and returns the filepath for Gradio download.
|
| 49 |
-
Receipt
|
| 50 |
"""
|
| 51 |
-
# hash without integrity
|
|
|
|
|
|
|
|
|
|
| 52 |
b0 = canon_json_bytes(payload)
|
| 53 |
h = sha256_hex(b0)
|
| 54 |
|
|
@@ -59,18 +73,31 @@ def write_receipt(payload: dict) -> str:
|
|
| 59 |
}
|
| 60 |
|
| 61 |
b1 = canon_json_bytes(payload)
|
| 62 |
-
|
|
|
|
| 63 |
safe_ts = ts.replace(":", "").replace(".", "").replace("Z", "")
|
| 64 |
fname = f"receipt_{safe_ts}_{h[:12]}.json"
|
| 65 |
path = RESULTS_DIR / fname
|
| 66 |
path.write_bytes(b1)
|
| 67 |
return str(path)
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
# ----------------------------
|
| 70 |
-
#
|
| 71 |
# ----------------------------
|
| 72 |
def _np_step(Psi, E, L, scale=1.0):
|
| 73 |
-
# numerically tame, branchless-ish
|
| 74 |
phase = 0.997 * Psi + 0.003 * E
|
| 75 |
drive = np.tanh(phase * scale)
|
| 76 |
Psi_n = 0.999 * Psi + 0.001 * drive
|
|
@@ -79,8 +106,6 @@ def _np_step(Psi, E, L, scale=1.0):
|
|
| 79 |
return Psi_n, E_n, L_n
|
| 80 |
|
| 81 |
def coherence_abs(Psi0: np.ndarray, Psi1: np.ndarray) -> float:
|
| 82 |
-
# Normalized dot product (magnitude used)
|
| 83 |
-
# (If values are constant, den can go tiny — guard it.)
|
| 84 |
v0 = Psi0.astype(np.float64, copy=False)
|
| 85 |
v1 = Psi1.astype(np.float64, copy=False)
|
| 86 |
num = float(np.dot(v0, v1))
|
|
@@ -88,7 +113,6 @@ def coherence_abs(Psi0: np.ndarray, Psi1: np.ndarray) -> float:
|
|
| 88 |
return abs(num / den)
|
| 89 |
|
| 90 |
def mean_energy(E: np.ndarray) -> float:
|
| 91 |
-
# bounded to keep metric stable across runs
|
| 92 |
return float(np.mean(np.clip(E, 0.0, 1.5)))
|
| 93 |
|
| 94 |
def run_engine_numpy(n: int, steps: int, seed: int, scale: float):
|
|
@@ -97,7 +121,6 @@ def run_engine_numpy(n: int, steps: int, seed: int, scale: float):
|
|
| 97 |
E = rng.random(n, dtype=np.float32)
|
| 98 |
L = rng.random(n, dtype=np.float32)
|
| 99 |
|
| 100 |
-
# capture Psi for coherence (small sample for speed)
|
| 101 |
sample = min(n, 200_000)
|
| 102 |
Psi0 = Psi[:sample].copy()
|
| 103 |
|
|
@@ -106,32 +129,30 @@ def run_engine_numpy(n: int, steps: int, seed: int, scale: float):
|
|
| 106 |
Psi, E, L = _np_step(Psi, E, L, scale=scale)
|
| 107 |
t1 = time.perf_counter()
|
| 108 |
|
| 109 |
-
Psi1 = Psi[:sample].copy()
|
| 110 |
elapsed = t1 - t0
|
|
|
|
| 111 |
|
| 112 |
-
# “items” = per-oscillator update of [Psi,E,L] per step
|
| 113 |
items = int(n) * int(steps)
|
| 114 |
throughput_Bps = (items / elapsed) / 1e9
|
| 115 |
|
| 116 |
-
coh = coherence_abs(Psi0, Psi1)
|
| 117 |
-
eng = mean_energy(E[:sample])
|
| 118 |
-
|
| 119 |
return {
|
| 120 |
"engine": "numpy",
|
| 121 |
"oscillators": int(n),
|
| 122 |
"steps": int(steps),
|
| 123 |
"elapsed_s": float(elapsed),
|
| 124 |
"throughput_Bps": float(throughput_Bps),
|
| 125 |
-
"coherence_abs": float(
|
| 126 |
-
"mean_energy": float(
|
| 127 |
}
|
| 128 |
|
| 129 |
# ----------------------------
|
| 130 |
-
# Baselines (optional)
|
| 131 |
# ----------------------------
|
| 132 |
def run_baseline_python(n: int, steps: int, seed: int):
|
| 133 |
-
#
|
| 134 |
n = min(n, 200_000)
|
|
|
|
|
|
|
| 135 |
rng = np.random.default_rng(seed)
|
| 136 |
Psi = rng.random(n).tolist()
|
| 137 |
E = rng.random(n).tolist()
|
|
@@ -151,31 +172,28 @@ def run_baseline_python(n: int, steps: int, seed: int):
|
|
| 151 |
return outPsi, outE, outL
|
| 152 |
|
| 153 |
t0 = time.perf_counter()
|
| 154 |
-
for _ in range(
|
| 155 |
Psi, E, L = step(Psi, E, L)
|
| 156 |
t1 = time.perf_counter()
|
| 157 |
-
elapsed = t1 - t0
|
| 158 |
|
| 159 |
-
|
|
|
|
| 160 |
throughput_Bps = (items / elapsed) / 1e9
|
| 161 |
|
| 162 |
-
# Coherence proxy (cheap)
|
| 163 |
-
Psi0 = np.array(Psi[:min(n, 50_000)], dtype=np.float32)
|
| 164 |
-
Psi1 = Psi0 # can't compare pre/post cheaply here without extra memory
|
| 165 |
-
coh = 1.0
|
| 166 |
-
eng = float(np.mean(np.clip(np.array(E[:min(n, 50_000)], dtype=np.float32), 0.0, 1.5)))
|
| 167 |
-
|
| 168 |
return {
|
| 169 |
"engine": "python_loop",
|
| 170 |
"oscillators": int(n),
|
| 171 |
-
"steps": int(
|
| 172 |
"elapsed_s": float(elapsed),
|
| 173 |
"throughput_Bps": float(throughput_Bps),
|
| 174 |
-
"coherence_abs":
|
| 175 |
-
"mean_energy": float(
|
| 176 |
-
"note": "Python
|
| 177 |
}
|
| 178 |
|
|
|
|
|
|
|
|
|
|
| 179 |
if NUMBA_OK:
|
| 180 |
@nb.njit(fastmath=True)
|
| 181 |
def _numba_kernel(Psi, E, L, scale):
|
|
@@ -199,76 +217,93 @@ if NUMBA_OK:
|
|
| 199 |
sample = min(n, 200_000)
|
| 200 |
Psi0 = Psi[:sample].copy()
|
| 201 |
|
| 202 |
-
# warmup
|
| 203 |
-
|
|
|
|
| 204 |
|
| 205 |
t0 = time.perf_counter()
|
| 206 |
for _ in range(steps):
|
| 207 |
_numba_kernel(Psi, E, L, scale)
|
| 208 |
t1 = time.perf_counter()
|
| 209 |
|
| 210 |
-
Psi1 = Psi[:sample].copy()
|
| 211 |
elapsed = t1 - t0
|
|
|
|
| 212 |
|
| 213 |
items = int(n) * int(steps)
|
| 214 |
throughput_Bps = (items / elapsed) / 1e9
|
| 215 |
|
| 216 |
-
coh = coherence_abs(Psi0, Psi1)
|
| 217 |
-
eng = mean_energy(E[:sample])
|
| 218 |
-
|
| 219 |
return {
|
| 220 |
"engine": "numba",
|
| 221 |
"oscillators": int(n),
|
| 222 |
"steps": int(steps),
|
| 223 |
"elapsed_s": float(elapsed),
|
| 224 |
"throughput_Bps": float(throughput_Bps),
|
| 225 |
-
"coherence_abs": float(
|
| 226 |
-
"mean_energy": float(
|
| 227 |
}
|
| 228 |
|
| 229 |
# ----------------------------
|
| 230 |
-
#
|
| 231 |
# ----------------------------
|
| 232 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
n = int(n_oscillators)
|
| 234 |
s = int(steps)
|
| 235 |
seed = int(seed)
|
| 236 |
scale = float(scale)
|
| 237 |
|
| 238 |
-
# Safety rails
|
| 239 |
-
# (Users can still push, but this avoids accidental hard-crashes.)
|
| 240 |
n = max(100_000, min(n, 40_000_000))
|
| 241 |
s = max(10, min(s, 5000))
|
| 242 |
|
| 243 |
-
#
|
| 244 |
if NUMBA_OK:
|
| 245 |
primary = run_engine_numba(n, s, seed, scale)
|
| 246 |
else:
|
| 247 |
primary = run_engine_numpy(n, s, seed, scale)
|
| 248 |
|
|
|
|
| 249 |
baselines = {}
|
| 250 |
-
if
|
| 251 |
baselines["numpy"] = run_engine_numpy(min(n, 8_000_000), min(s, 2000), seed, scale)
|
| 252 |
baselines["python_loop"] = run_baseline_python(min(n, 500_000), min(s, 200), seed)
|
| 253 |
|
| 254 |
-
#
|
| 255 |
-
|
| 256 |
-
"timestamp_utc":
|
| 257 |
-
"
|
| 258 |
-
"
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
"
|
| 262 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
"inputs": {
|
| 264 |
"oscillators": int(n),
|
| 265 |
"steps": int(s),
|
| 266 |
-
"seed": seed,
|
| 267 |
-
"scale": scale,
|
| 268 |
-
"include_baselines": bool(
|
| 269 |
-
},
|
| 270 |
-
"definition": {
|
| 271 |
-
"item": "1 item = one per-oscillator coherent state update of [Psi, E, L] per step (as implemented in this Space)."
|
| 272 |
},
|
| 273 |
"results": {
|
| 274 |
"primary": primary,
|
|
@@ -276,64 +311,57 @@ def run_and_receipt(n_oscillators, steps, seed, scale, include_baseline):
|
|
| 276 |
},
|
| 277 |
}
|
| 278 |
|
| 279 |
-
receipt_path = write_receipt(
|
| 280 |
-
|
| 281 |
-
#
|
| 282 |
-
|
| 283 |
-
"
|
| 284 |
-
"
|
| 285 |
-
"
|
| 286 |
-
"
|
| 287 |
-
"
|
| 288 |
-
"
|
| 289 |
-
"
|
| 290 |
-
"
|
| 291 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
}
|
|
|
|
| 293 |
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
ui[f"Baseline: {k} (B/s)"] = f'{v["throughput_Bps"]:.3f}'
|
| 298 |
-
ui[f"Baseline: {k} Engine"] = v["engine"]
|
| 299 |
|
| 300 |
return ui, receipt_path
|
| 301 |
|
| 302 |
# ----------------------------
|
| 303 |
-
# UI
|
| 304 |
# ----------------------------
|
| 305 |
CSS = """
|
| 306 |
-
:root {
|
| 307 |
-
|
| 308 |
-
}
|
| 309 |
-
.
|
| 310 |
-
max-width: 980px !important;
|
| 311 |
-
}
|
| 312 |
-
#titlebar h1 {
|
| 313 |
-
font-size: 2.05rem;
|
| 314 |
-
letter-spacing: -0.02em;
|
| 315 |
-
}
|
| 316 |
-
.rft-card {
|
| 317 |
-
border-radius: 16px !important;
|
| 318 |
-
border: 1px solid rgba(255,255,255,0.08) !important;
|
| 319 |
-
}
|
| 320 |
"""
|
| 321 |
|
| 322 |
with gr.Blocks(css=CSS, theme=gr.themes.Soft()) as demo:
|
| 323 |
gr.Markdown(
|
| 324 |
"""
|
| 325 |
-
<div id="titlebar">
|
| 326 |
-
<h1>Coherent Compute Engine</h1>
|
| 327 |
-
</div>
|
| 328 |
|
| 329 |
**What this Space does**
|
| 330 |
-
|
| 331 |
|
| 332 |
**What an “item” is**
|
| 333 |
One coherent update of **[Ψ, E, L]** per oscillator per step.
|
| 334 |
|
| 335 |
**Verification**
|
| 336 |
-
Every run generates a tamper-evident
|
| 337 |
"""
|
| 338 |
)
|
| 339 |
|
|
@@ -350,28 +378,28 @@ Every run generates a tamper-evident **receipt (JSON)** with a SHA-256 hash you
|
|
| 350 |
)
|
| 351 |
seed_box = gr.Number(value=7, precision=0, label="Seed")
|
| 352 |
scale_box = gr.Number(value=1.0, precision=3, label="Scale (stability knob)")
|
| 353 |
-
|
| 354 |
value=False,
|
| 355 |
-
label="Include baselines (
|
| 356 |
info="Baselines are measured live too. Python loop is safety-capped."
|
| 357 |
)
|
| 358 |
run_btn = gr.Button("Run Engine", variant="primary")
|
| 359 |
|
| 360 |
with gr.Column(scale=1):
|
| 361 |
results_json = gr.JSON(label="Results")
|
| 362 |
-
receipt_file = gr.File(label="Receipt (
|
| 363 |
|
| 364 |
run_btn.click(
|
| 365 |
fn=run_and_receipt,
|
| 366 |
-
inputs=[n_slider, steps_slider, seed_box, scale_box,
|
| 367 |
outputs=[results_json, receipt_file],
|
| 368 |
)
|
| 369 |
|
| 370 |
gr.Markdown(
|
| 371 |
"""
|
| 372 |
**Notes**
|
| 373 |
-
- This runs on the Hugging Face Space runtime machine.
|
| 374 |
-
- If the Space is under load, throughput will vary
|
| 375 |
"""
|
| 376 |
)
|
| 377 |
|
|
|
|
| 1 |
# app.py
|
| 2 |
# Coherent_Compute_Engine — RFTSystems
|
| 3 |
+
# Real, on-machine coherent throughput benchmark + tamper-evident receipt download (SHA-256).
|
| 4 |
+
#
|
| 5 |
+
# What an “item” is:
|
| 6 |
+
# 1 item = one per-oscillator coherent state update of [Psi, E, L] per step.
|
| 7 |
+
#
|
| 8 |
# Notes:
|
| 9 |
+
# - Measurements reflect the Hugging Face Space runtime hardware (not the visitor’s local machine).
|
| 10 |
+
# - Includes optional baselines (NumPy vectorised + tiny Python loop) computed live.
|
| 11 |
+
# - Produces a downloadable receipt JSON with canonical hashing for verification.
|
| 12 |
|
| 13 |
import os
|
| 14 |
import json
|
| 15 |
import time
|
| 16 |
import math
|
| 17 |
+
import csv
|
| 18 |
import hashlib
|
| 19 |
import platform
|
| 20 |
import datetime as dt
|
|
|
|
| 23 |
import numpy as np
|
| 24 |
import gradio as gr
|
| 25 |
|
| 26 |
+
# Optional: Numba acceleration (used if available)
|
| 27 |
try:
|
| 28 |
import numba as nb
|
| 29 |
NUMBA_OK = True
|
|
|
|
| 31 |
nb = None
|
| 32 |
NUMBA_OK = False
|
| 33 |
|
| 34 |
+
APP_NAME = "Coherent_Compute_Engine"
|
| 35 |
+
APP_VERSION = "v1.0.0"
|
| 36 |
RESULTS_DIR = Path("results")
|
| 37 |
RESULTS_DIR.mkdir(exist_ok=True)
|
| 38 |
|
|
|
|
| 50 |
def sha256_hex(b: bytes) -> str:
|
| 51 |
return hashlib.sha256(b).hexdigest()
|
| 52 |
|
| 53 |
+
def utc_now_iso() -> str:
|
| 54 |
+
# Avoid deprecated utcnow warning in newer Python versions
|
| 55 |
+
return dt.datetime.now(dt.timezone.utc).isoformat().replace("+00:00", "Z")
|
| 56 |
+
|
| 57 |
def write_receipt(payload: dict) -> str:
|
| 58 |
"""
|
| 59 |
Writes a JSON receipt to disk and returns the filepath for Gradio download.
|
| 60 |
+
Receipt includes its own SHA-256 of the canonical JSON (tamper-evident).
|
| 61 |
"""
|
| 62 |
+
# hash without integrity field
|
| 63 |
+
payload = dict(payload) # copy
|
| 64 |
+
payload.pop("integrity", None)
|
| 65 |
+
|
| 66 |
b0 = canon_json_bytes(payload)
|
| 67 |
h = sha256_hex(b0)
|
| 68 |
|
|
|
|
| 73 |
}
|
| 74 |
|
| 75 |
b1 = canon_json_bytes(payload)
|
| 76 |
+
|
| 77 |
+
ts = payload.get("timestamp_utc", utc_now_iso())
|
| 78 |
safe_ts = ts.replace(":", "").replace(".", "").replace("Z", "")
|
| 79 |
fname = f"receipt_{safe_ts}_{h[:12]}.json"
|
| 80 |
path = RESULTS_DIR / fname
|
| 81 |
path.write_bytes(b1)
|
| 82 |
return str(path)
|
| 83 |
|
| 84 |
+
def append_csv_row(row: dict, csv_path: Path):
|
| 85 |
+
headers = [
|
| 86 |
+
"timestamp_utc","engine","device_note","oscillators","steps",
|
| 87 |
+
"elapsed_s","throughput_Bps","coherence_abs","mean_energy",
|
| 88 |
+
"python","platform","cpu_count_logical","numba_available","seed","scale"
|
| 89 |
+
]
|
| 90 |
+
need_header = not csv_path.exists()
|
| 91 |
+
with csv_path.open("a", newline="") as f:
|
| 92 |
+
w = csv.DictWriter(f, fieldnames=headers)
|
| 93 |
+
if need_header:
|
| 94 |
+
w.writeheader()
|
| 95 |
+
w.writerow({k: row.get(k, "") for k in headers})
|
| 96 |
+
|
| 97 |
# ----------------------------
|
| 98 |
+
# RFT-lite coherent kernel
|
| 99 |
# ----------------------------
|
| 100 |
def _np_step(Psi, E, L, scale=1.0):
|
|
|
|
| 101 |
phase = 0.997 * Psi + 0.003 * E
|
| 102 |
drive = np.tanh(phase * scale)
|
| 103 |
Psi_n = 0.999 * Psi + 0.001 * drive
|
|
|
|
| 106 |
return Psi_n, E_n, L_n
|
| 107 |
|
| 108 |
def coherence_abs(Psi0: np.ndarray, Psi1: np.ndarray) -> float:
|
|
|
|
|
|
|
| 109 |
v0 = Psi0.astype(np.float64, copy=False)
|
| 110 |
v1 = Psi1.astype(np.float64, copy=False)
|
| 111 |
num = float(np.dot(v0, v1))
|
|
|
|
| 113 |
return abs(num / den)
|
| 114 |
|
| 115 |
def mean_energy(E: np.ndarray) -> float:
|
|
|
|
| 116 |
return float(np.mean(np.clip(E, 0.0, 1.5)))
|
| 117 |
|
| 118 |
def run_engine_numpy(n: int, steps: int, seed: int, scale: float):
|
|
|
|
| 121 |
E = rng.random(n, dtype=np.float32)
|
| 122 |
L = rng.random(n, dtype=np.float32)
|
| 123 |
|
|
|
|
| 124 |
sample = min(n, 200_000)
|
| 125 |
Psi0 = Psi[:sample].copy()
|
| 126 |
|
|
|
|
| 129 |
Psi, E, L = _np_step(Psi, E, L, scale=scale)
|
| 130 |
t1 = time.perf_counter()
|
| 131 |
|
|
|
|
| 132 |
elapsed = t1 - t0
|
| 133 |
+
Psi1 = Psi[:sample].copy()
|
| 134 |
|
|
|
|
| 135 |
items = int(n) * int(steps)
|
| 136 |
throughput_Bps = (items / elapsed) / 1e9
|
| 137 |
|
|
|
|
|
|
|
|
|
|
| 138 |
return {
|
| 139 |
"engine": "numpy",
|
| 140 |
"oscillators": int(n),
|
| 141 |
"steps": int(steps),
|
| 142 |
"elapsed_s": float(elapsed),
|
| 143 |
"throughput_Bps": float(throughput_Bps),
|
| 144 |
+
"coherence_abs": float(coherence_abs(Psi0, Psi1)),
|
| 145 |
+
"mean_energy": float(mean_energy(E[:sample])),
|
| 146 |
}
|
| 147 |
|
| 148 |
# ----------------------------
|
| 149 |
+
# Baselines (optional, live)
|
| 150 |
# ----------------------------
|
| 151 |
def run_baseline_python(n: int, steps: int, seed: int):
|
| 152 |
+
# Safety caps for hosted runtimes
|
| 153 |
n = min(n, 200_000)
|
| 154 |
+
steps = min(steps, 10)
|
| 155 |
+
|
| 156 |
rng = np.random.default_rng(seed)
|
| 157 |
Psi = rng.random(n).tolist()
|
| 158 |
E = rng.random(n).tolist()
|
|
|
|
| 172 |
return outPsi, outE, outL
|
| 173 |
|
| 174 |
t0 = time.perf_counter()
|
| 175 |
+
for _ in range(steps):
|
| 176 |
Psi, E, L = step(Psi, E, L)
|
| 177 |
t1 = time.perf_counter()
|
|
|
|
| 178 |
|
| 179 |
+
elapsed = t1 - t0
|
| 180 |
+
items = int(n) * int(steps)
|
| 181 |
throughput_Bps = (items / elapsed) / 1e9
|
| 182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
return {
|
| 184 |
"engine": "python_loop",
|
| 185 |
"oscillators": int(n),
|
| 186 |
+
"steps": int(steps),
|
| 187 |
"elapsed_s": float(elapsed),
|
| 188 |
"throughput_Bps": float(throughput_Bps),
|
| 189 |
+
"coherence_abs": 1.0,
|
| 190 |
+
"mean_energy": float(np.mean(np.clip(np.array(E[:min(n, 50_000)], dtype=np.float32), 0.0, 1.5))),
|
| 191 |
+
"note": "Python baseline is safety-capped (n<=200k, steps<=10).",
|
| 192 |
}
|
| 193 |
|
| 194 |
+
# ----------------------------
|
| 195 |
+
# Numba engine (if available)
|
| 196 |
+
# ----------------------------
|
| 197 |
if NUMBA_OK:
|
| 198 |
@nb.njit(fastmath=True)
|
| 199 |
def _numba_kernel(Psi, E, L, scale):
|
|
|
|
| 217 |
sample = min(n, 200_000)
|
| 218 |
Psi0 = Psi[:sample].copy()
|
| 219 |
|
| 220 |
+
# compile warmup on tiny arrays
|
| 221 |
+
tiny = min(n, 1024)
|
| 222 |
+
_numba_kernel(Psi[:tiny], E[:tiny], L[:tiny], scale)
|
| 223 |
|
| 224 |
t0 = time.perf_counter()
|
| 225 |
for _ in range(steps):
|
| 226 |
_numba_kernel(Psi, E, L, scale)
|
| 227 |
t1 = time.perf_counter()
|
| 228 |
|
|
|
|
| 229 |
elapsed = t1 - t0
|
| 230 |
+
Psi1 = Psi[:sample].copy()
|
| 231 |
|
| 232 |
items = int(n) * int(steps)
|
| 233 |
throughput_Bps = (items / elapsed) / 1e9
|
| 234 |
|
|
|
|
|
|
|
|
|
|
| 235 |
return {
|
| 236 |
"engine": "numba",
|
| 237 |
"oscillators": int(n),
|
| 238 |
"steps": int(steps),
|
| 239 |
"elapsed_s": float(elapsed),
|
| 240 |
"throughput_Bps": float(throughput_Bps),
|
| 241 |
+
"coherence_abs": float(coherence_abs(Psi0, Psi1)),
|
| 242 |
+
"mean_energy": float(mean_energy(E[:sample])),
|
| 243 |
}
|
| 244 |
|
| 245 |
# ----------------------------
|
| 246 |
+
# Runner + UI helpers
|
| 247 |
# ----------------------------
|
| 248 |
+
def format_ui(primary: dict, baselines: dict | None):
|
| 249 |
+
ui = {
|
| 250 |
+
"Throughput (B/s)": f'{primary["throughput_Bps"]:.3f} B/s',
|
| 251 |
+
"Coherence (|C|)": f'{primary["coherence_abs"]:.5f}',
|
| 252 |
+
"Mean Energy": f'{primary["mean_energy"]:.5f}',
|
| 253 |
+
"Elapsed Time (s)": f'{primary["elapsed_s"]:.2f}',
|
| 254 |
+
"Oscillators": f'{primary["oscillators"]:,}',
|
| 255 |
+
"Steps": f'{primary["steps"]:,}',
|
| 256 |
+
"Engine": primary["engine"],
|
| 257 |
+
"CPU Cores Available": os.cpu_count(),
|
| 258 |
+
}
|
| 259 |
+
if baselines:
|
| 260 |
+
for name, b in baselines.items():
|
| 261 |
+
ui[f"Baseline: {name} (B/s)"] = f'{b["throughput_Bps"]:.3f}'
|
| 262 |
+
ui[f"Baseline: {name} Engine"] = b["engine"]
|
| 263 |
+
return ui
|
| 264 |
+
|
| 265 |
+
def run_and_receipt(n_oscillators, steps, seed, scale, include_baselines):
|
| 266 |
n = int(n_oscillators)
|
| 267 |
s = int(steps)
|
| 268 |
seed = int(seed)
|
| 269 |
scale = float(scale)
|
| 270 |
|
| 271 |
+
# Safety rails (prevents accidental crash / OOM on Space)
|
|
|
|
| 272 |
n = max(100_000, min(n, 40_000_000))
|
| 273 |
s = max(10, min(s, 5000))
|
| 274 |
|
| 275 |
+
# primary engine
|
| 276 |
if NUMBA_OK:
|
| 277 |
primary = run_engine_numba(n, s, seed, scale)
|
| 278 |
else:
|
| 279 |
primary = run_engine_numpy(n, s, seed, scale)
|
| 280 |
|
| 281 |
+
# optional baselines
|
| 282 |
baselines = {}
|
| 283 |
+
if include_baselines:
|
| 284 |
baselines["numpy"] = run_engine_numpy(min(n, 8_000_000), min(s, 2000), seed, scale)
|
| 285 |
baselines["python_loop"] = run_baseline_python(min(n, 500_000), min(s, 200), seed)
|
| 286 |
|
| 287 |
+
# build receipt payload
|
| 288 |
+
payload = {
|
| 289 |
+
"timestamp_utc": utc_now_iso(),
|
| 290 |
+
"app": {"name": APP_NAME, "version": APP_VERSION},
|
| 291 |
+
"definition": {
|
| 292 |
+
"item": "1 item = one per-oscillator coherent state update of [Psi, E, L] per step (as implemented here)."
|
| 293 |
+
},
|
| 294 |
+
"runtime": {
|
| 295 |
+
"device_note": "Hugging Face Space runtime machine (results are not from the visitor's local CPU).",
|
| 296 |
+
"platform": platform.platform(),
|
| 297 |
+
"python": platform.python_version(),
|
| 298 |
+
"cpu_count_logical": os.cpu_count(),
|
| 299 |
+
"numba_available": bool(NUMBA_OK),
|
| 300 |
+
},
|
| 301 |
"inputs": {
|
| 302 |
"oscillators": int(n),
|
| 303 |
"steps": int(s),
|
| 304 |
+
"seed": int(seed),
|
| 305 |
+
"scale": float(scale),
|
| 306 |
+
"include_baselines": bool(include_baselines),
|
|
|
|
|
|
|
|
|
|
| 307 |
},
|
| 308 |
"results": {
|
| 309 |
"primary": primary,
|
|
|
|
| 311 |
},
|
| 312 |
}
|
| 313 |
|
| 314 |
+
receipt_path = write_receipt(payload)
|
| 315 |
+
|
| 316 |
+
# append csv row (primary only)
|
| 317 |
+
csv_row = {
|
| 318 |
+
"timestamp_utc": payload["timestamp_utc"],
|
| 319 |
+
"engine": primary["engine"],
|
| 320 |
+
"device_note": payload["runtime"]["device_note"],
|
| 321 |
+
"oscillators": primary["oscillators"],
|
| 322 |
+
"steps": primary["steps"],
|
| 323 |
+
"elapsed_s": primary["elapsed_s"],
|
| 324 |
+
"throughput_Bps": primary["throughput_Bps"],
|
| 325 |
+
"coherence_abs": primary["coherence_abs"],
|
| 326 |
+
"mean_energy": primary["mean_energy"],
|
| 327 |
+
"python": payload["runtime"]["python"],
|
| 328 |
+
"platform": payload["runtime"]["platform"],
|
| 329 |
+
"cpu_count_logical": payload["runtime"]["cpu_count_logical"],
|
| 330 |
+
"numba_available": payload["runtime"]["numba_available"],
|
| 331 |
+
"seed": seed,
|
| 332 |
+
"scale": scale,
|
| 333 |
}
|
| 334 |
+
append_csv_row(csv_row, RESULTS_DIR / "runs.csv")
|
| 335 |
|
| 336 |
+
# UI output
|
| 337 |
+
ui = format_ui(primary, baselines if include_baselines else None)
|
| 338 |
+
ui["Receipt SHA-256 (in file)"] = payload["integrity"]["sha256"] if "integrity" in payload else "written in receipt"
|
|
|
|
|
|
|
| 339 |
|
| 340 |
return ui, receipt_path
|
| 341 |
|
| 342 |
# ----------------------------
|
| 343 |
+
# UI
|
| 344 |
# ----------------------------
|
| 345 |
CSS = """
|
| 346 |
+
:root { --rft-accent: #ff7a18; }
|
| 347 |
+
.gradio-container { max-width: 980px !important; }
|
| 348 |
+
#titlebar h1 { font-size: 2.05rem; letter-spacing: -0.02em; }
|
| 349 |
+
.rft-card { border-radius: 16px !important; border: 1px solid rgba(255,255,255,0.08) !important; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 350 |
"""
|
| 351 |
|
| 352 |
with gr.Blocks(css=CSS, theme=gr.themes.Soft()) as demo:
|
| 353 |
gr.Markdown(
|
| 354 |
"""
|
| 355 |
+
<div id="titlebar"><h1>Coherent Compute Engine</h1></div>
|
|
|
|
|
|
|
| 356 |
|
| 357 |
**What this Space does**
|
| 358 |
+
Runs a real coherent state-update benchmark and reports measured throughput, stability proxy, and energy behavior. No precomputed results.
|
| 359 |
|
| 360 |
**What an “item” is**
|
| 361 |
One coherent update of **[Ψ, E, L]** per oscillator per step.
|
| 362 |
|
| 363 |
**Verification**
|
| 364 |
+
Every run generates a tamper-evident receipt (JSON) with a SHA-256 hash you can download.
|
| 365 |
"""
|
| 366 |
)
|
| 367 |
|
|
|
|
| 378 |
)
|
| 379 |
seed_box = gr.Number(value=7, precision=0, label="Seed")
|
| 380 |
scale_box = gr.Number(value=1.0, precision=3, label="Scale (stability knob)")
|
| 381 |
+
include_baselines = gr.Checkbox(
|
| 382 |
value=False,
|
| 383 |
+
label="Include baselines (NumPy + tiny Python loop)",
|
| 384 |
info="Baselines are measured live too. Python loop is safety-capped."
|
| 385 |
)
|
| 386 |
run_btn = gr.Button("Run Engine", variant="primary")
|
| 387 |
|
| 388 |
with gr.Column(scale=1):
|
| 389 |
results_json = gr.JSON(label="Results")
|
| 390 |
+
receipt_file = gr.File(label="Receipt (download)")
|
| 391 |
|
| 392 |
run_btn.click(
|
| 393 |
fn=run_and_receipt,
|
| 394 |
+
inputs=[n_slider, steps_slider, seed_box, scale_box, include_baselines],
|
| 395 |
outputs=[results_json, receipt_file],
|
| 396 |
)
|
| 397 |
|
| 398 |
gr.Markdown(
|
| 399 |
"""
|
| 400 |
**Notes**
|
| 401 |
+
- This runs on the Hugging Face Space runtime machine. Your browser is just the UI.
|
| 402 |
+
- If the Space is under load, throughput will vary; the receipt captures the environment at run time.
|
| 403 |
"""
|
| 404 |
)
|
| 405 |
|