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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
TWAVE β Token-Level Wavefront Tracker
|
| 3 |
+
Bose-Einstein Condensate Thermodynamic Control for LLM Inference
|
| 4 |
+
|
| 5 |
+
Implements:
|
| 6 |
+
- Landau-Ginzburg free energy functional F[Ο]
|
| 7 |
+
- Bogoliubov excitation spectrum E(k)
|
| 8 |
+
- Healing length ΞΎ as hallucination localization scale
|
| 9 |
+
- Jarzynski equality for non-equilibrium reflection trigger
|
| 10 |
+
- CK-PLUG Confidence Gain as concrete ΞΌ_ret coupling
|
| 11 |
+
- Stochastic resonance optimal T_eff β 0.8 T_c
|
| 12 |
+
|
| 13 |
+
Physics foundation:
|
| 14 |
+
Kim 2602.08216 (Thermodynamic Isomorphism)
|
| 15 |
+
Arnold et al. (Phase Transitions in Output Distributions)
|
| 16 |
+
Qian et al. 2604.10219 (Cognitive Pivot Points / RVTD)
|
| 17 |
+
CK-PLUG 2503.15888 (Confidence Gain = H(parametric) - H(retrieval))
|
| 18 |
+
BEC standard physics (Bogoliubov, healing length)
|
| 19 |
+
Jarzynski equality (fluctuation theorem for non-equilibrium work)
|
| 20 |
+
"""
|
| 21 |
+
import math
|
| 22 |
+
from typing import List, Tuple, Optional, Dict, Any
|
| 23 |
+
from dataclasses import dataclass, field
|
| 24 |
+
import numpy as np
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
@dataclass
|
| 28 |
+
class TokenState:
|
| 29 |
+
"""Thermodynamic state at a single token position."""
|
| 30 |
+
position: int
|
| 31 |
+
entropy: float # H_i β Shannon entropy (nats)
|
| 32 |
+
entropy_max: float # log(vocab_size) β max possible entropy
|
| 33 |
+
coherence: float # 1 - H_i/H_max (condensate fraction)
|
| 34 |
+
visual_attention_mass: float # A_i^vis (from V-STAR, 0-1)
|
| 35 |
+
reward_density: float # Ο_i = r_i / i (intensive reward)
|
| 36 |
+
critique_confidence: float # c_i (from Critique-out-Loud)
|
| 37 |
+
retrieval_mu: float # ΞΌ_ret(x) β CK-PLUG derived chemical potential
|
| 38 |
+
psi: float # |Ο| β BEC order parameter amplitude
|
| 39 |
+
free_energy_density: float # f(x) β Landau-Ginzburg density
|
| 40 |
+
temperature_eff: float # T_eff(x) β local effective temperature
|
| 41 |
+
specific_heat: float # C_V^(i) β susceptibility
|
| 42 |
+
E_excitation: float # Bogoliubov excitation energy
|
| 43 |
+
k_local: float # Local wavenumber |βΟ|
|
| 44 |
+
jarzynski_work: float # W_i β cumulative non-equilibrium work
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
@dataclass
|
| 48 |
+
class GenerationTrajectory:
|
| 49 |
+
"""Complete tracked generation with thermodynamic metadata."""
|
| 50 |
+
tokens: List[int]
|
| 51 |
+
states: List[TokenState]
|
| 52 |
+
total_work: float
|
| 53 |
+
jarzynski_bound: float
|
| 54 |
+
reflection_triggers: List[int] # Positions where reflection fired
|
| 55 |
+
grounded_score: float # Overall retrieval grounding quality
|
| 56 |
+
hallucination_risk: float # 0-1 composite risk score
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
class TWAVETracker:
|
| 60 |
+
"""
|
| 61 |
+
Token-Level Wavefront Expansion Tracker.
|
| 62 |
+
Monitors generation as a 1D statistical field evolving under
|
| 63 |
+
Landau-Ginzburg dynamics with BEC order parameter Ο(x).
|
| 64 |
+
"""
|
| 65 |
+
|
| 66 |
+
def __init__(
|
| 67 |
+
self,
|
| 68 |
+
T_c: float = 1.0, # Critical temperature (calibrated per-model)
|
| 69 |
+
mu_0: float = 0.5, # Base retrieval chemical potential
|
| 70 |
+
kappa: float = 0.1, # Healing cost coefficient (Δ§Β²/2m analog)
|
| 71 |
+
b_quartic: float = 0.5, # Quartic stability term
|
| 72 |
+
gamma_drift: float = 0.1, # Field evolution step size
|
| 73 |
+
jarzynski_threshold: float = -4.6, # Fluctuation theorem bound (99%)
|
| 74 |
+
tau_transition: float = 0.5, # CGβΞΌ_ret transition width
|
| 75 |
+
vocab_size: int = 32000,
|
| 76 |
+
):
|
| 77 |
+
self.T_c = T_c
|
| 78 |
+
self.mu_0 = mu_0
|
| 79 |
+
self.kappa = kappa
|
| 80 |
+
self.b = b_quartic
|
| 81 |
+
self.gamma = gamma_drift
|
| 82 |
+
self.jarzynski_threshold = jarzynski_threshold
|
| 83 |
+
self.tau = tau_transition
|
| 84 |
+
self.vocab_size = vocab_size
|
| 85 |
+
self.H_max = math.log(vocab_size)
|
| 86 |
+
|
| 87 |
+
def compute_entropy(self, probs: np.ndarray) -> float:
|
| 88 |
+
"""Shannon entropy in nats: H = -Ξ£ p_i ln p_i."""
|
| 89 |
+
p = probs[probs > 0]
|
| 90 |
+
return float(-np.sum(p * np.log(p)))
|
| 91 |
+
|
| 92 |
+
def compute_coherence(self, entropy: float) -> float:
|
| 93 |
+
"""Condensate fraction: Ο coherence = 1 - H/H_max."""
|
| 94 |
+
return max(0.0, 1.0 - entropy / self.H_max)
|
| 95 |
+
|
| 96 |
+
def compute_chemical_potential(self, CG: float) -> float:
|
| 97 |
+
"""
|
| 98 |
+
Map CK-PLUG Confidence Gain to ΞΌ_ret.
|
| 99 |
+
ΞΌ_ret = ΞΌ_0 * tanh(CG / Ο)
|
| 100 |
+
CG >> 0 β retrieval supports β ΞΌ_ret β ΞΌ_0
|
| 101 |
+
CG β 0 β neutral β ΞΌ_ret β 0
|
| 102 |
+
CG << 0 β retrieval conflicts β ΞΌ_ret β -ΞΌ_0
|
| 103 |
+
"""
|
| 104 |
+
return self.mu_0 * math.tanh(CG / self.tau)
|
| 105 |
+
|
| 106 |
+
def compute_order_parameter(self, coherence: float, mu_ret: float) -> float:
|
| 107 |
+
"""
|
| 108 |
+
BEC ground state amplitude (minimizing Landau-Ginzburg functional):
|
| 109 |
+
Ο_0 = β[(a_0(T_c - T_eff) + ΞΌ_ret) / b]
|
| 110 |
+
where a(T) = coherence - T_c (since coherence β 1 - entropy β 1/T)
|
| 111 |
+
"""
|
| 112 |
+
a = coherence - self.T_c
|
| 113 |
+
numerator = max(0.0, a + mu_ret)
|
| 114 |
+
return math.sqrt(numerator / self.b)
|
| 115 |
+
|
| 116 |
+
def compute_free_energy_density(self, psi: float, coherence: float, mu_ret: float) -> float:
|
| 117 |
+
"""
|
| 118 |
+
Landau-Ginzburg free energy density:
|
| 119 |
+
f = a(T)|Ο|Β² + (b/2)|Ο|β΄ - ΞΌ_ret|Ο|Β²
|
| 120 |
+
"""
|
| 121 |
+
a = coherence - self.T_c
|
| 122 |
+
return a * psi**2 + 0.5 * self.b * psi**4 - mu_ret * psi**2
|
| 123 |
+
|
| 124 |
+
def compute_bogoliubov_energy(self, psi: float, k_local: float, mu_ret: float) -> float:
|
| 125 |
+
"""
|
| 126 |
+
Bogoliubov excitation spectrum:
|
| 127 |
+
E(k) = β[(ΞΊkΒ²)Β² + (c_sΒ·k)Β²] where c_s = β(ΞΌ/m) = β(ΞΌ_ret)
|
| 128 |
+
In our analogy: ΞΊ = kappa, c_sΒ² = mu_ret
|
| 129 |
+
"""
|
| 130 |
+
c_s_sq = max(0.0, mu_ret)
|
| 131 |
+
term1 = (self.kappa * k_local**2)**2
|
| 132 |
+
term2 = c_s_sq * k_local**2
|
| 133 |
+
return math.sqrt(term1 + term2)
|
| 134 |
+
|
| 135 |
+
def compute_healing_length(self, T_eff: float, mu_ret: float) -> float:
|
| 136 |
+
"""
|
| 137 |
+
Healing length: ΞΎ = Δ§ / β(2mΒ·ΞΌ) = 1 / β(2Β·mu_ret) [in token units]
|
| 138 |
+
As T_eff β T_c or ΞΌ_ret β 0: ΞΎ β β (hallucination propagates globally)
|
| 139 |
+
"""
|
| 140 |
+
mu = max(1e-10, mu_ret)
|
| 141 |
+
return 1.0 / math.sqrt(2.0 * mu)
|
| 142 |
+
|
| 143 |
+
def compute_specific_heat(self, entropy_history: List[float]) -> float:
|
| 144 |
+
"""
|
| 145 |
+
Specific heat analog: C_V = ββ¨Hβ©/βT_eff.
|
| 146 |
+
Approximated numerically from entropy history.
|
| 147 |
+
"""
|
| 148 |
+
if len(entropy_history) < 3:
|
| 149 |
+
return 0.0
|
| 150 |
+
# Finite difference: dH/dt β (H_{t} - H_{t-2}) / 2
|
| 151 |
+
dH = entropy_history[-1] - entropy_history[-3]
|
| 152 |
+
# Effective temperature from mean entropy
|
| 153 |
+
H_mean = sum(entropy_history[-3:]) / 3.0
|
| 154 |
+
T_eff = self.T_c * (1.0 - H_mean / self.H_max)
|
| 155 |
+
if abs(T_eff) < 1e-10:
|
| 156 |
+
return 0.0
|
| 157 |
+
return dH / (2.0 * T_eff)
|
| 158 |
+
|
| 159 |
+
def compute_jarzynski_work(self, log_prob_policy: float, log_prob_ref: float) -> float:
|
| 160 |
+
"""
|
| 161 |
+
Non-equilibrium information work per token:
|
| 162 |
+
W_i = -log[Ο_ΞΈ(x_i|x_{<i}) / Ο_ref(x_i|x_{<i})]
|
| 163 |
+
"""
|
| 164 |
+
return -(log_prob_policy - log_prob_ref)
|
| 165 |
+
|
| 166 |
+
def check_jarzynski_bound(self, cumulative_work: float, beta_eff: float) -> bool:
|
| 167 |
+
"""
|
| 168 |
+
Jarzynski reflection criterion:
|
| 169 |
+
Returns True if trajectory violates fluctuation theorem
|
| 170 |
+
(hallucination detected with 99% confidence).
|
| 171 |
+
"""
|
| 172 |
+
if beta_eff <= 0:
|
| 173 |
+
return False
|
| 174 |
+
jarzynski = math.exp(-beta_eff * cumulative_work)
|
| 175 |
+
threshold = math.exp(-beta_eff * self.jarzynski_threshold)
|
| 176 |
+
return jarzynski < threshold
|
| 177 |
+
|
| 178 |
+
def update_state(
|
| 179 |
+
self,
|
| 180 |
+
position: int,
|
| 181 |
+
probs: np.ndarray,
|
| 182 |
+
log_prob_policy: float,
|
| 183 |
+
log_prob_ref: float,
|
| 184 |
+
CG: float, # CK-PLUG Confidence Gain
|
| 185 |
+
visual_attention: float = 1.0, # A_i^vis (1.0 = full attention)
|
| 186 |
+
reward_density: float = 0.0,
|
| 187 |
+
critique_confidence: float = 0.5,
|
| 188 |
+
prev_psi: float = 0.0,
|
| 189 |
+
) -> TokenState:
|
| 190 |
+
"""
|
| 191 |
+
Compute full thermodynamic state at token position i.
|
| 192 |
+
"""
|
| 193 |
+
# 1. Entropy and coherence
|
| 194 |
+
H = self.compute_entropy(probs)
|
| 195 |
+
coherence = self.compute_coherence(H)
|
| 196 |
+
|
| 197 |
+
# 2. Effective temperature (stochastic resonance optimal point)
|
| 198 |
+
# T_eff = T_c * (1 - H/H_max) = T_c * coherence
|
| 199 |
+
T_eff = self.T_c * coherence
|
| 200 |
+
|
| 201 |
+
# 3. Chemical potential from CK-PLUG
|
| 202 |
+
mu_ret = self.compute_chemical_potential(CG)
|
| 203 |
+
|
| 204 |
+
# 4. Order parameter Ο
|
| 205 |
+
psi = self.compute_order_parameter(coherence, mu_ret)
|
| 206 |
+
|
| 207 |
+
# 5. Local wavenumber (gradient of Ο)
|
| 208 |
+
k_local = abs(psi - prev_psi) if prev_psi > 0 else 0.0
|
| 209 |
+
|
| 210 |
+
# 6. Free energy density
|
| 211 |
+
f_density = self.compute_free_energy_density(psi, coherence, mu_ret)
|
| 212 |
+
|
| 213 |
+
# 7. Bogoliubov excitation energy
|
| 214 |
+
E_exc = self.compute_bogoliubov_energy(psi, k_local, mu_ret)
|
| 215 |
+
|
| 216 |
+
# 8. Specific heat
|
| 217 |
+
# (requires entropy history β computed externally and passed back)
|
| 218 |
+
C_V = 0.0 # Placeholder; set by caller with history
|
| 219 |
+
|
| 220 |
+
# 9. Jarzynski work
|
| 221 |
+
W_i = self.compute_jarzynski_work(log_prob_policy, log_prob_ref)
|
| 222 |
+
|
| 223 |
+
return TokenState(
|
| 224 |
+
position=position,
|
| 225 |
+
entropy=H,
|
| 226 |
+
entropy_max=self.H_max,
|
| 227 |
+
coherence=coherence,
|
| 228 |
+
visual_attention_mass=visual_attention,
|
| 229 |
+
reward_density=reward_density,
|
| 230 |
+
critique_confidence=critique_confidence,
|
| 231 |
+
retrieval_mu=mu_ret,
|
| 232 |
+
psi=psi,
|
| 233 |
+
free_energy_density=f_density,
|
| 234 |
+
temperature_eff=T_eff,
|
| 235 |
+
specific_heat=C_V,
|
| 236 |
+
E_excitation=E_exc,
|
| 237 |
+
k_local=k_local,
|
| 238 |
+
jarzynski_work=W_i,
|
| 239 |
+
)
|
| 240 |
+
|
| 241 |
+
def evaluate_stability(self, state: TokenState) -> Dict[str, Any]:
|
| 242 |
+
"""
|
| 243 |
+
Evaluate generation stability at current token.
|
| 244 |
+
Returns actionable flags for the inference loop.
|
| 245 |
+
"""
|
| 246 |
+
flags = {
|
| 247 |
+
"stable": True,
|
| 248 |
+
"reflection_triggered": False,
|
| 249 |
+
"hallucination_risk": "low",
|
| 250 |
+
"action": "continue",
|
| 251 |
+
}
|
| 252 |
+
|
| 253 |
+
# Critical point: T_eff > T_c or ΞΌ_ret < 0 (adversarial retrieval)
|
| 254 |
+
if state.temperature_eff > self.T_c:
|
| 255 |
+
flags["stable"] = False
|
| 256 |
+
flags["hallucination_risk"] = "critical"
|
| 257 |
+
flags["action"] = "reflection"
|
| 258 |
+
flags["reflection_triggered"] = True
|
| 259 |
+
return flags
|
| 260 |
+
|
| 261 |
+
# Bogoliubov gap check: E_exc < ΞΌ_ret * 0.95 β near-critical
|
| 262 |
+
gap_threshold = state.retrieval_mu * 0.95 if state.retrieval_mu > 0 else 0.01
|
| 263 |
+
if state.E_excitation < gap_threshold:
|
| 264 |
+
flags["stable"] = False
|
| 265 |
+
flags["hallucination_risk"] = "high"
|
| 266 |
+
flags["action"] = "grounding_boost"
|
| 267 |
+
return flags
|
| 268 |
+
|
| 269 |
+
# Healing length divergence: ΞΎ > 10 tokens β global perturbation
|
| 270 |
+
xi = self.compute_healing_length(state.temperature_eff, state.retrieval_mu)
|
| 271 |
+
if xi > 10.0:
|
| 272 |
+
flags["hallucination_risk"] = "elevated"
|
| 273 |
+
flags["action"] = "local_grounding"
|
| 274 |
+
|
| 275 |
+
# Visual attention collapse (RVTD detection)
|
| 276 |
+
if state.visual_attention_mass < 0.1:
|
| 277 |
+
flags["hallucination_risk"] = "high"
|
| 278 |
+
flags["action"] = "visual_reground"
|
| 279 |
+
|
| 280 |
+
return flags
|
| 281 |
+
|
| 282 |
+
def build_trajectory(self, states: List[TokenState]) -> GenerationTrajectory:
|
| 283 |
+
"""Assemble tracked trajectory from individual token states."""
|
| 284 |
+
total_work = sum(s.jarzynski_work for s in states)
|
| 285 |
+
# Beta_eff from mean entropy
|
| 286 |
+
H_mean = sum(s.entropy for s in states) / max(1, len(states))
|
| 287 |
+
T_mean = self.T_c * (1.0 - H_mean / self.H_max)
|
| 288 |
+
beta_eff = 1.0 / max(T_mean, 0.01)
|
| 289 |
+
|
| 290 |
+
# Check Jarzynski bound
|
| 291 |
+
bound_violated = self.check_jarzynski_bound(total_work, beta_eff)
|
| 292 |
+
|
| 293 |
+
# Reflection triggers
|
| 294 |
+
triggers = [i for i, s in enumerate(states) if s.temperature_eff > self.T_c]
|
| 295 |
+
|
| 296 |
+
# Composite hallucination risk
|
| 297 |
+
risks = []
|
| 298 |
+
for s in states:
|
| 299 |
+
risk = 0.0
|
| 300 |
+
if s.temperature_eff > self.T_c * 0.8:
|
| 301 |
+
risk += 0.3
|
| 302 |
+
if s.retrieval_mu < 0:
|
| 303 |
+
risk += 0.3
|
| 304 |
+
if s.visual_attention_mass < 0.2:
|
| 305 |
+
risk += 0.2
|
| 306 |
+
if s.E_excitation < s.retrieval_mu * 0.95:
|
| 307 |
+
risk += 0.2
|
| 308 |
+
risks.append(min(risk, 1.0))
|
| 309 |
+
avg_risk = sum(risks) / max(1, len(risks))
|
| 310 |
+
|
| 311 |
+
# Grounding score
|
| 312 |
+
grounding = sum(max(0, s.retrieval_mu) for s in states) / max(1, len(states))
|
| 313 |
+
|
| 314 |
+
return GenerationTrajectory(
|
| 315 |
+
tokens=[0] * len(states), # Placeholder
|
| 316 |
+
states=states,
|
| 317 |
+
total_work=total_work,
|
| 318 |
+
jarzynski_bound=math.exp(-beta_eff * self.jarzynski_threshold) if bound_violated else 0.0,
|
| 319 |
+
reflection_triggers=triggers,
|
| 320 |
+
grounded_score=grounding,
|
| 321 |
+
hallucination_risk=avg_risk,
|
| 322 |
+
)
|
| 323 |
+
|
| 324 |
+
|
| 325 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 326 |
+
# Optimal Temperature Calculator (Stochastic Resonance)
|
| 327 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 328 |
+
|
| 329 |
+
class StochasticResonance:
|
| 330 |
+
"""
|
| 331 |
+
Compute optimal effective temperature for reasoning tasks.
|
| 332 |
+
Based on Kramers escape rate: k_escape β exp(-ΞE_barrier / k_B T_eff)
|
| 333 |
+
"""
|
| 334 |
+
|
| 335 |
+
@staticmethod
|
| 336 |
+
def optimal_temperature(
|
| 337 |
+
barrier_height: float, # ΞE_barrier β estimated from task complexity
|
| 338 |
+
target_escape_rate: float, # Desired exploration rate (0-1)
|
| 339 |
+
T_c: float = 1.0,
|
| 340 |
+
) -> float:
|
| 341 |
+
"""
|
| 342 |
+
Solve for T_eff such that escape rate matches target.
|
| 343 |
+
k = exp(-ΞE/T) β T = ΞE / -ln(k)
|
| 344 |
+
"""
|
| 345 |
+
if target_escape_rate <= 0 or target_escape_rate >= 1:
|
| 346 |
+
return 0.1 * T_c
|
| 347 |
+
T_opt = barrier_height / (-math.log(target_escape_rate))
|
| 348 |
+
# Clamp to BEC stable region: 0.1 T_c < T < 0.95 T_c
|
| 349 |
+
return max(0.1 * T_c, min(0.95 * T_c, T_opt))
|
| 350 |
+
|
| 351 |
+
@staticmethod
|
| 352 |
+
def barrier_estimate(complexity_score: float) -> float:
|
| 353 |
+
"""
|
| 354 |
+
Estimate energy barrier from task complexity (0-1 from Sulphur enhancer).
|
| 355 |
+
Simple tasks: low barrier (easy to escape local minima)
|
| 356 |
+
Complex reasoning: high barrier (need more thermal energy)
|
| 357 |
+
"""
|
| 358 |
+
return 0.5 + 2.0 * complexity_score # Range: 0.5 - 2.5
|
| 359 |
+
|
| 360 |
+
@staticmethod
|
| 361 |
+
def recommend_temperature(complexity: float, T_c: float = 1.0) -> float:
|
| 362 |
+
"""
|
| 363 |
+
Recommend T_eff for a given task complexity.
|
| 364 |
+
Low complexity (factual lookup): T β 0.3 T_c (low exploration)
|
| 365 |
+
High complexity (creative reasoning): T β 0.8 T_c (optimal SR)
|
| 366 |
+
"""
|
| 367 |
+
barrier = StochasticResonance.barrier_estimate(complexity)
|
| 368 |
+
# Target escape rate: 0.1 for simple, 0.5 for complex
|
| 369 |
+
target = 0.1 + 0.4 * complexity
|
| 370 |
+
return StochasticResonance.optimal_temperature(barrier, target, T_c)
|