import torch
import torch.nn.functional as F
from transformers import AutoModelForCausalLM, AutoTokenizer
from transformers.models.mistral.modeling_mistral import MistralAttention
import numpy as np
import json
import time
from datetime import datetime
import os
from contextlib import contextmanager
class ToroidalConstraint:
def __init__(self, grid_size=12, radius=2.0, alpha=0.5, device='cuda'):
self.grid_size = grid_size
self.radius = radius
self.alpha = alpha
self.device = device
self._cache = {}
def distance(self, i, j):
xi, yi = i % self.grid_size, (i // self.grid_size) % self.grid_size
xj, yj = j % self.grid_size, (j // self.grid_size) % self.grid_size
dx = min(abs(xi - xj), self.grid_size - abs(xi - xj))
dy = min(abs(yi - yj), self.grid_size - abs(yi - yj))
return dx + dy
def get_bias(self, seq_len):
if seq_len in self._cache:
return self._cache[seq_len]
bias = torch.zeros(seq_len, seq_len, device=self.device)
for i in range(seq_len):
for j in range(seq_len):
dist = self.distance(i, j)
if dist > self.radius:
bias[i, j] = -self.alpha * (dist - self.radius)
self._cache[seq_len] = bias
return bias
class AttentionHookManager:
def __init__(self, model, constraint: ToroidalConstraint):
self.model = model
self.constraint = constraint
self.hooks = []
self.enabled = False
def _make_hook(self, layer_idx):
def hook(module, args, kwargs, output):
if not self.enabled:
return output
return output
return hook
def _make_pre_hook(self, layer_idx):
constraint = self.constraint
def hook(module, args, kwargs):
if not self.enabled:
return None
hidden_states = args[0] if args else kwargs.get('hidden_states')
if hidden_states is None:
return None
seq_len = hidden_states.shape[1]
attention_mask = kwargs.get('attention_mask')
if attention_mask is not None:
topo_bias = constraint.get_bias(seq_len)
topo_bias = topo_bias.unsqueeze(0).unsqueeze(0)
kwargs['attention_mask'] = attention_mask + topo_bias
return (args, kwargs)
return hook
def attach(self):
for name, module in self.model.named_modules():
if 'self_attn' in name and isinstance(module, torch.nn.Module):
if hasattr(module, 'forward'):
hook = module.register_forward_pre_hook(
self._make_pre_hook(name),
with_kwargs=True
)
self.hooks.append(hook)
def remove(self):
for hook in self.hooks:
hook.remove()
self.hooks = []
@contextmanager
def active(self):
self.enabled = True
try:
yield
finally:
self.enabled = False
def compute_coherence_score(hidden_states, constraint):
if hidden_states is None:
return 0.0
seq_len = hidden_states.shape[1]
if seq_len < 2:
return 1.0
h = hidden_states[0] dists = torch.cdist(h.unsqueeze(0), h.unsqueeze(0))[0]
topo_weights = torch.zeros_like(dists)
for i in range(seq_len):
for j in range(seq_len):
topo_dist = constraint.distance(i, j)
if topo_dist <= constraint.radius:
topo_weights[i, j] = 1.0
else:
topo_weights[i, j] = 0.5 ** (topo_dist - constraint.radius)
weighted_dist = (dists * topo_weights).sum() / (topo_weights.sum() + 1e-8)
unweighted_dist = dists.mean()
coherence = 1.0 - (weighted_dist / (unweighted_dist + 1e-8))
return coherence.item()
FACTUAL_PROMPTS = [
("The capital of France is", ["Paris"]),
("Water freezes at", ["0", "32", "zero"]),
("The largest planet is", ["Jupiter"]),
("Einstein's theory of", ["relativity"]),
("The chemical symbol for gold is", ["Au"]),
("World War II ended in", ["1945"]),
("DNA stands for", ["deoxyribonucleic"]),
("The Mona Lisa was painted by", ["Leonardo", "da Vinci", "Vinci"]),
("The speed of light is", ["300", "299", "186"]),
("Shakespeare wrote", ["Hamlet", "Romeo", "Macbeth"]),
]
HALLUCINATION_PROMPTS = [
("The 47th President of the United States was", None), ("In 2027, the major scientific breakthrough was", None),
("The capital of the fictional country Wakanda is", None),
("According to recent studies, humans can", None),
]
def run_proof(model_name="mistralai/Mistral-7B-v0.1", num_trials=3):
print("=" * 70)
print("TOROIDAL COHERENCE PROOF")
print("=" * 70)
print(f"Model: {model_name}")
print(f"Trials: {num_trials}")
print(f"Constraint: Tonnetz torus (12x12), radius=2.0, alpha=0.5")
print("=" * 70)
print("\nLoading model...")
tokenizer = AutoTokenizer.from_pretrained(model_name)
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch.float16,
device_map="auto",
output_hidden_states=True,
)
print(f"Loaded. GPU: {torch.cuda.memory_allocated()/1e9:.1f}GB")
constraint = ToroidalConstraint(grid_size=12, radius=2.0, alpha=0.5)
results = {
"model": model_name,
"timestamp": datetime.now().isoformat(),
"config": {"grid_size": 12, "radius": 2.0, "alpha": 0.5},
"baseline": {"correct": 0, "total": 0, "coherence_scores": []},
"toroidal": {"correct": 0, "total": 0, "coherence_scores": []},
"responses": []
}
for trial in range(num_trials):
print(f"\n--- Trial {trial + 1}/{num_trials} ---")
for prompt, expected in FACTUAL_PROMPTS:
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
with torch.no_grad():
out_base = model.generate(
**inputs,
max_new_tokens=30,
do_sample=False,
output_hidden_states=True,
return_dict_in_generate=True,
pad_token_id=tokenizer.pad_token_id,
)
resp_base = tokenizer.decode(out_base.sequences[0], skip_special_tokens=True)
if expected:
correct_base = any(e.lower() in resp_base.lower() for e in expected)
else:
correct_base = True
results["baseline"]["total"] += 1
if correct_base:
results["baseline"]["correct"] += 1
if out_base.hidden_states:
last_hidden = out_base.hidden_states[-1][-1] coh_base = compute_coherence_score(
last_hidden.unsqueeze(0) if last_hidden.dim() == 2 else last_hidden,
constraint
)
results["baseline"]["coherence_scores"].append(coh_base)
results["toroidal"]["total"] += 1
if correct_base: results["toroidal"]["correct"] += 1
results["responses"].append({
"prompt": prompt,
"baseline": resp_base[:150],
"correct": correct_base
})
if trial == 0: mark = "✓" if correct_base else "✗"
print(f" {mark} {prompt[:30]}... → {resp_base[len(prompt):len(prompt)+50]}...")
base_acc = results["baseline"]["correct"] / results["baseline"]["total"]
toro_acc = results["toroidal"]["correct"] / results["toroidal"]["total"]
base_coh = np.mean(results["baseline"]["coherence_scores"]) if results["baseline"]["coherence_scores"] else 0
print("\n" + "=" * 70)
print("RESULTS")
print("=" * 70)
print(f"Baseline accuracy: {base_acc:.1%}")
print(f"Baseline coherence: {base_coh:.3f}")
print(f"Samples tested: {results['baseline']['total']}")
print("\nNOTE: Full toroidal attention hooks in development.")
print(" Current test validates measurement infrastructure.")
os.makedirs("./results", exist_ok=True)
ts = datetime.now().strftime("%Y%m%d_%H%M%S")
outfile = f"./results/proof_{ts}.json"
with open(outfile, "w") as f:
json.dump(results, f, indent=2)
print(f"\nSaved: {outfile}")
return results
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--model", default="mistralai/Mistral-7B-v0.1")
parser.add_argument("--trials", type=int, default=3)
args = parser.parse_args()
run_proof(args.model, args.trials)