Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,11 +11,15 @@ ARCHITECTURE:
|
|
| 11 |
β [GEMINI COLLAPSER] β Domain-language explanation
|
| 12 |
|
| 13 |
CRITICAL FIXES MAINTAINED:
|
| 14 |
-
- The Squeeze Bug Fix (.view(B, -1).sum(dim=1) for absolute dimensional stability
|
| 15 |
- The Two Brains Sync (Gate 1 Verification directly calls PyTorch L2 loss)
|
| 16 |
- The Anti-LaTeX Shield (Native handling of hallucinated `^` characters)
|
| 17 |
- Unbound Local Safety (Zero-Shot crash protection in Axiom constructors)
|
| 18 |
- Fully Expanded Codebase (Zero minification for maximum Python stability)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
"""
|
| 20 |
|
| 21 |
import os, time, random, math, threading, warnings, json, textwrap, itertools
|
|
@@ -270,10 +274,19 @@ class AXLInvariant:
|
|
| 270 |
syms_used:List[str]=field(default_factory=list)
|
| 271 |
|
| 272 |
def compile(self,variables:List[str]):
|
| 273 |
-
# Anti-LaTeX Syntax Shield
|
| 274 |
self.expr = self.expr.replace("^", "**")
|
| 275 |
syms={v:sp.Symbol(v) for v in variables}
|
| 276 |
parsed=parse_expr(self.expr,local_dict=syms)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
self.syms_used=[str(s) for s in parsed.free_symbols if str(s) in variables]
|
| 278 |
self.compiled_func=sp.lambdify([sp.Symbol(v) for v in self.syms_used],parsed,modules="math")
|
| 279 |
|
|
@@ -287,10 +300,21 @@ class AXLProblemDef:
|
|
| 287 |
class AXLtoPSLCompiler:
|
| 288 |
def compile(self,prob:AXLProblemDef) -> str:
|
| 289 |
lines=[f"SPACE base_{prob.name}"]
|
|
|
|
|
|
|
| 290 |
for v in prob.variables:
|
| 291 |
lo,hi=v["lo"],v["hi"]
|
| 292 |
if v["name"] in prob.observations: val=prob.observations[v["name"]]; lo=hi=val
|
| 293 |
lines.append(f"VAR {v['name']} {lo} {hi}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 294 |
for c in prob.constraints:
|
| 295 |
wt=c.get("weight",1.0)
|
| 296 |
expr = str(c['expr']).replace("^", "**")
|
|
@@ -358,12 +382,29 @@ def compile_mc(kind,expr_str,direction,variables,weight=1.0,scope="root",branche
|
|
| 358 |
|
| 359 |
syms = {v: sp.Symbol(v) for v in variables}
|
| 360 |
try:
|
| 361 |
-
parsed = parse_expr(expr_str, local_dict=syms)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 362 |
mc.parsed = parsed
|
| 363 |
mc.syms_used = [v for v in variables if sp.Symbol(v) in parsed.free_symbols]
|
| 364 |
mc.fast_iv = compile_iv(parsed, variables)
|
| 365 |
|
| 366 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 367 |
t_func_raw = sp.lambdify([sp.Symbol(v) for v in mc.syms_used], parsed, modules=[pt_map, "math"])
|
| 368 |
|
| 369 |
def _t_wrapper(*args):
|
|
@@ -512,7 +553,6 @@ class Problem:
|
|
| 512 |
total += (out_of_bounds ** 2) * 10.0
|
| 513 |
|
| 514 |
# ββ THE TRUE SQUEEZE BUG FIX ββ
|
| 515 |
-
# Reduce any extra tensor dimensions hallucinated by the LLM back to exactly 1 value per batch
|
| 516 |
return total.view(batch_size, -1).sum(dim=1)
|
| 517 |
|
| 518 |
def scalar_energy(self, b: Dict[str, float]) -> float:
|
|
|
|
| 11 |
β [GEMINI COLLAPSER] β Domain-language explanation
|
| 12 |
|
| 13 |
CRITICAL FIXES MAINTAINED:
|
| 14 |
+
- The Squeeze Bug Fix (.view(B, -1).sum(dim=1) for absolute dimensional stability)
|
| 15 |
- The Two Brains Sync (Gate 1 Verification directly calls PyTorch L2 loss)
|
| 16 |
- The Anti-LaTeX Shield (Native handling of hallucinated `^` characters)
|
| 17 |
- Unbound Local Safety (Zero-Shot crash protection in Axiom constructors)
|
| 18 |
- Fully Expanded Codebase (Zero minification for maximum Python stability)
|
| 19 |
+
- Undefined Observation Shield (Force-injects orphaned LLM observations)
|
| 20 |
+
- Undefined Symbol Shield (Blocks SymPy from invoking float() on Tensors)
|
| 21 |
+
- Extended PT Map Shield (Wires complex trig/logs directly to PyTorch)
|
| 22 |
+
- Relational Hallucination Shield (Forces Eq(x,y) into x - y)
|
| 23 |
"""
|
| 24 |
|
| 25 |
import os, time, random, math, threading, warnings, json, textwrap, itertools
|
|
|
|
| 274 |
syms_used:List[str]=field(default_factory=list)
|
| 275 |
|
| 276 |
def compile(self,variables:List[str]):
|
|
|
|
| 277 |
self.expr = self.expr.replace("^", "**")
|
| 278 |
syms={v:sp.Symbol(v) for v in variables}
|
| 279 |
parsed=parse_expr(self.expr,local_dict=syms)
|
| 280 |
+
|
| 281 |
+
# Relational Hallucination Shield
|
| 282 |
+
if getattr(parsed, 'is_Equality', False) or getattr(parsed, 'is_Relational', False):
|
| 283 |
+
parsed = parsed.lhs - parsed.rhs
|
| 284 |
+
|
| 285 |
+
# Undefined Symbol Shield
|
| 286 |
+
for s in list(parsed.free_symbols):
|
| 287 |
+
if str(s) not in variables:
|
| 288 |
+
parsed = parsed.subs(s, 1.0)
|
| 289 |
+
|
| 290 |
self.syms_used=[str(s) for s in parsed.free_symbols if str(s) in variables]
|
| 291 |
self.compiled_func=sp.lambdify([sp.Symbol(v) for v in self.syms_used],parsed,modules="math")
|
| 292 |
|
|
|
|
| 300 |
class AXLtoPSLCompiler:
|
| 301 |
def compile(self,prob:AXLProblemDef) -> str:
|
| 302 |
lines=[f"SPACE base_{prob.name}"]
|
| 303 |
+
emitted_vars = set()
|
| 304 |
+
|
| 305 |
for v in prob.variables:
|
| 306 |
lo,hi=v["lo"],v["hi"]
|
| 307 |
if v["name"] in prob.observations: val=prob.observations[v["name"]]; lo=hi=val
|
| 308 |
lines.append(f"VAR {v['name']} {lo} {hi}")
|
| 309 |
+
emitted_vars.add(v["name"])
|
| 310 |
+
|
| 311 |
+
# ββ UNDEFINED OBSERVATION SHIELD ββ
|
| 312 |
+
# Ensure any observations hallucinated purely into constraints without variable declarations are injected into the space
|
| 313 |
+
for obs_name, obs_val in prob.observations.items():
|
| 314 |
+
if obs_name not in emitted_vars:
|
| 315 |
+
lines.append(f"VAR {obs_name} {obs_val} {obs_val}")
|
| 316 |
+
emitted_vars.add(obs_name)
|
| 317 |
+
|
| 318 |
for c in prob.constraints:
|
| 319 |
wt=c.get("weight",1.0)
|
| 320 |
expr = str(c['expr']).replace("^", "**")
|
|
|
|
| 382 |
|
| 383 |
syms = {v: sp.Symbol(v) for v in variables}
|
| 384 |
try:
|
| 385 |
+
parsed = parse_expr(expr_str, local_dict=syms) if kind!="or_eq" else None
|
| 386 |
+
if parsed:
|
| 387 |
+
# Relational Hallucination Shield
|
| 388 |
+
if getattr(parsed, 'is_Equality', False) or getattr(parsed, 'is_Relational', False):
|
| 389 |
+
parsed = parsed.lhs - parsed.rhs
|
| 390 |
+
|
| 391 |
+
# Undefined Symbol Shield: Swap out generic symbols before lambdifying to PyTorch
|
| 392 |
+
for s in list(parsed.free_symbols):
|
| 393 |
+
if str(s) not in variables:
|
| 394 |
+
parsed = parsed.subs(s, 1.0)
|
| 395 |
+
|
| 396 |
mc.parsed = parsed
|
| 397 |
mc.syms_used = [v for v in variables if sp.Symbol(v) in parsed.free_symbols]
|
| 398 |
mc.fast_iv = compile_iv(parsed, variables)
|
| 399 |
|
| 400 |
+
# ββ EXTENDED PT MAP SHIELD ββ
|
| 401 |
+
pt_map = {
|
| 402 |
+
'sin': torch.sin, 'cos': torch.cos, 'tan': torch.tan,
|
| 403 |
+
'exp': torch.exp, 'log': torch.log, 'sqrt': torch.sqrt,
|
| 404 |
+
'Abs': torch.abs, 'asin': torch.asin, 'acos': torch.acos,
|
| 405 |
+
'atan': torch.atan, 'sinh': torch.sinh, 'cosh': torch.cosh,
|
| 406 |
+
'tanh': torch.tanh, 'pi': math.pi, 'E': math.e
|
| 407 |
+
}
|
| 408 |
t_func_raw = sp.lambdify([sp.Symbol(v) for v in mc.syms_used], parsed, modules=[pt_map, "math"])
|
| 409 |
|
| 410 |
def _t_wrapper(*args):
|
|
|
|
| 553 |
total += (out_of_bounds ** 2) * 10.0
|
| 554 |
|
| 555 |
# ββ THE TRUE SQUEEZE BUG FIX ββ
|
|
|
|
| 556 |
return total.view(batch_size, -1).sum(dim=1)
|
| 557 |
|
| 558 |
def scalar_energy(self, b: Dict[str, float]) -> float:
|