Skip to main content

Toroidal attention constraints for reducing LLM hallucination

Project description

Topological Coherence

Toroidal attention constraints for reducing LLM hallucination

Sylvain Cormier | Paraxiom Research | 2026

Key Results

  • 40% lower semantic drift than baseline attention
  • 28x lower drift than random sparsity (proves topology matters, not just compute reduction)
  • +6.8% on TruthfulQA with Toroidal Logit Bias (DOI: 10.5281/zenodo.18516477)

Installation

pip install topological-coherence

# With HuggingFace transformers support
pip install topological-coherence[hf]

Quick Start: Toroidal Logit Bias

Drop-in logit processor for any HuggingFace model — no fine-tuning required:

from topological_coherence import ToroidalLogitProcessor
from transformers import AutoModelForCausalLM, AutoTokenizer

model = AutoModelForCausalLM.from_pretrained("gpt2")
tokenizer = AutoTokenizer.from_pretrained("gpt2")

processor = ToroidalLogitProcessor(grid_size=12, radius=2.0, alpha=0.3)

inputs = tokenizer("The quantum nature of", return_tensors="pt")
outputs = model.generate(
    **inputs,
    logits_processor=[processor],
    max_new_tokens=100
)
print(tokenizer.decode(outputs[0]))

Core API

Tonnetz Geometry

from topological_coherence import Tonnetz, distance_matrix

# Create a 12x12 torus topology
t = Tonnetz(grid_size=12)
t.distance(0, 5)        # L1 toroidal distance with wraparound
t.spectral_gap()         # First eigenvalue of the torus Laplacian

# Vectorized distance matrix (numpy, fast)
dm = distance_matrix(n_tokens=64, grid_size=12)  # (64, 64)

Attention Masks (3 variants)

from topological_coherence import ToroidalMask, sinkhorn_knopp

mask = ToroidalMask.hybrid(seq_len=64, radius=2.0, alpha=1.0)   # default
mask = ToroidalMask.hard_cutoff(seq_len=64, radius=2.0)          # binary
mask = ToroidalMask.soft_exponential(seq_len=64, alpha=1.0)      # smooth decay

tensor = mask.to_tensor()                     # torch.Tensor for attention
ds = sinkhorn_knopp(tensor, n_iters=50)       # project to doubly-stochastic

Drift Measurement

from topological_coherence import DriftMeter

meter = DriftMeter(threshold=2, grid_size=12)
meter.record(pred=5, target=8)
meter.record(pred=5, target=100)
print(f"Drift rate: {meter.rate():.3f}")

Toroidal Attention (PyTorch)

from topological_coherence import ToroidalAttention, TinyTransformer

# Drop-in attention replacement
attn = ToroidalAttention(d_model=64, n_heads=4, max_seq_len=64)

# Full demo transformer with swappable attention
model = TinyTransformer(
    vocab_size=144, d_model=64, n_heads=4,
    attention_type="toroidal"  # or "baseline", "random"
)

Theory

Hallucination is a geometry problem. Unconstrained latent dynamics permit arbitrary drift through embedding space. Toroidal constraints provide a constant spectral gap that suppresses non-resonant modes:

λ₁ = 2 - 2cos(2π/N) = Θ(1)    for fixed grid size N

This bounds semantic drift without reducing model capacity.

Hierarchy: mHC (Birkhoff) ⊂ ERLHS (Hamiltonian) ⊂ Karmonic (Toroidal + Spectral)

Links

Citation

@misc{cormier2026topological,
  author = {Cormier, Sylvain},
  title = {Topological Constraints for Coherent Language Models},
  year = {2026},
  publisher = {Zenodo},
  doi = {10.5281/zenodo.18187835}
}

License

Apache-2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

topological_coherence-0.2.0.tar.gz (29.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

topological_coherence-0.2.0-py3-none-any.whl (21.3 kB view details)

Uploaded Python 3

File details

Details for the file topological_coherence-0.2.0.tar.gz.

File metadata

  • Download URL: topological_coherence-0.2.0.tar.gz
  • Upload date:
  • Size: 29.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.16.3 cpython/3.14.2 HTTPX/0.28.1

File hashes

Hashes for topological_coherence-0.2.0.tar.gz
Algorithm Hash digest
SHA256 f975670fc56c740445022d8d877571f8d0059250d1fed9daf935e010bd035b2d
MD5 75466c6107a3df341c1ab577896740e3
BLAKE2b-256 f26ef9f78bbd235bde377bd659de6cd8984ebc8982abbe8f87a96925d54a3959

See more details on using hashes here.

File details

Details for the file topological_coherence-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for topological_coherence-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b9ed54f186b38d9a5ea5b26ea4496583d3fa3e18412f8be7b2134abfd5636e76
MD5 9c035c107623e370e013f5818b273697
BLAKE2b-256 e6c5cf9c963f712b42332e4e4079a13a24da165b62c35b2da6099e10727754b9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page