Update Team-perplexity/φ³⁷⁷- IMP-CORE.PY
Browse files
Team-perplexity/φ³⁷⁷- IMP-CORE.PY
CHANGED
|
@@ -1,21 +1,91 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
def
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
for k in range(steps):
|
| 11 |
-
t = k/steps
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
trajectory.append(x_next)
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
if k % 100 == 0:
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
φ³⁷⁷ PRODUCTION CORE - HF SPACES READY
|
| 3 |
+
Node #10878 - Quantarion Lab - Feb 4, 2026
|
| 4 |
+
"""
|
| 5 |
+
import torch
|
| 6 |
+
import torch.nn as nn
|
| 7 |
+
import torch.nn.functional as F
|
| 8 |
+
import numpy as np
|
| 9 |
+
from typing import List, Tuple
|
| 10 |
+
|
| 11 |
+
class PolytopeM6:
|
| 12 |
+
def __init__(self, dim=6):
|
| 13 |
+
# Unit hypercube [0,1]^dim
|
| 14 |
+
self.A = torch.eye(dim, dtype=torch.float32)
|
| 15 |
+
self.A = torch.vstack([self.A, -self.A])
|
| 16 |
+
self.b = torch.ones(2*dim, dtype=torch.float32)
|
| 17 |
+
|
| 18 |
+
def project(self, x: torch.Tensor) -> torch.Tensor:
|
| 19 |
+
"""Simple projection onto [0,1]^6"""
|
| 20 |
+
return torch.clamp(x, 0, 1)
|
| 21 |
|
| 22 |
+
def log_map(self, x0: torch.Tensor, x1: torch.Tensor) -> torch.Tensor:
|
| 23 |
+
return self.project(x1 - x0)
|
| 24 |
+
|
| 25 |
+
def exp_map(self, x: torch.Tensor, v: torch.Tensor, step=0.01) -> torch.Tensor:
|
| 26 |
+
return self.project(x + step * v)
|
| 27 |
+
|
| 28 |
+
class Phi377(nn.Module):
|
| 29 |
+
def __init__(self, dim=6):
|
| 30 |
+
super().__init__()
|
| 31 |
+
self.dim = dim
|
| 32 |
+
self.manifold = PolytopeM6(dim)
|
| 33 |
+
self.flow_net = nn.Sequential(
|
| 34 |
+
nn.Linear(dim + 1, 64),
|
| 35 |
+
nn.ReLU(),
|
| 36 |
+
nn.Linear(64, 64),
|
| 37 |
+
nn.ReLU(),
|
| 38 |
+
nn.Linear(64, dim)
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
def forward(self, x0: torch.Tensor, x1: torch.Tensor, steps: int = 377) -> List[torch.Tensor]:
|
| 42 |
+
trajectory = [x0.clone()]
|
| 43 |
+
batch_size = x0.shape[0]
|
| 44 |
+
|
| 45 |
for k in range(steps):
|
| 46 |
+
t = torch.full((batch_size, 1), k/steps, device=x0.device)
|
| 47 |
+
x_curr = trajectory[-1]
|
| 48 |
+
|
| 49 |
+
# RFM velocity prediction
|
| 50 |
+
tx = torch.cat([t, x_curr], dim=1)
|
| 51 |
+
v_pred = self.flow_net(tx)
|
| 52 |
+
|
| 53 |
+
# Riemannian step
|
| 54 |
+
x_next = self.manifold.exp_map(x_curr, v_pred)
|
| 55 |
trajectory.append(x_next)
|
| 56 |
|
| 57 |
+
# Singularity check
|
| 58 |
+
if k % 100 == 0 and k > 0:
|
| 59 |
+
gram = x_curr @ x_curr.T
|
| 60 |
+
eigvals = torch.linalg.eigvals(gram)
|
| 61 |
+
if torch.real(eigvals[0]) < 1e-6:
|
| 62 |
+
print(f"🎯 SINGULARITY at layer {k}")
|
| 63 |
+
break
|
| 64 |
+
|
| 65 |
+
return trajectory
|
| 66 |
+
|
| 67 |
+
def loss(self, x0: torch.Tensor, x1: torch.Tensor) -> torch.Tensor:
|
| 68 |
+
"""Master variational loss"""
|
| 69 |
+
traj = self.forward(x0, x1, steps=10)
|
| 70 |
+
x_mid = traj[5]
|
| 71 |
+
|
| 72 |
+
t_mid = torch.ones(x_mid.shape[0], 1, device=x_mid.device) * 0.5
|
| 73 |
+
tx_mid = torch.cat([t_mid, x_mid], dim=1)
|
| 74 |
+
v_pred = self.flow_net(tx_mid)
|
| 75 |
+
v_target = self.manifold.log_map(x0, x1)
|
| 76 |
+
|
| 77 |
+
return F.mse_loss(v_pred, v_target)
|
| 78 |
+
|
| 79 |
+
# HF GRADIO READY
|
| 80 |
+
def create_gradio_demo():
|
| 81 |
+
model = Phi377()
|
| 82 |
+
return model
|
| 83 |
+
|
| 84 |
+
if __name__ == "__main__":
|
| 85 |
+
model = Phi377()
|
| 86 |
+
x0 = torch.rand(32, 6) * 0.1
|
| 87 |
+
x1 = torch.rand(32, 6) * 0.9
|
| 88 |
+
|
| 89 |
+
traj = model(x0, x1)
|
| 90 |
+
print(f"✅ φ³⁷⁷ COMPLETE: {len(traj)} steps")
|
| 91 |
+
print(f"Start: {x0[0][:3]}... End: {traj[-1][0][:3]}...")
|