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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# geofractal/model/layers/attention/cantor_multiheaded_fusion_fp64_v2.py
|
| 2 |
+
# FULLY OPTIMIZED - ZERO RUNTIME LOOPS, LRU CACHING, FP64 GEOMETRY
|
| 3 |
+
|
| 4 |
+
"""
|
| 5 |
+
CantorMultiheadFusion v2 - Production-Ready Optimized Implementation
|
| 6 |
+
=====================================================================
|
| 7 |
+
|
| 8 |
+
Optimization Summary:
|
| 9 |
+
β
ZERO runtime for-loops in forward pass
|
| 10 |
+
β
LRU cache with hot/warm/cold tiers
|
| 11 |
+
β
FP64 geometric computation β FP32 runtime
|
| 12 |
+
β
Vectorized Devil's Staircase (no level loop)
|
| 13 |
+
β
Vectorized route building (no position loop)
|
| 14 |
+
β
Vectorized weight computation (no sequence loop)
|
| 15 |
+
β
Pre-computed everything possible
|
| 16 |
+
β
Memory-efficient gather operations
|
| 17 |
+
β
Triton-ready kernel signatures
|
| 18 |
+
|
| 19 |
+
Precision Strategy:
|
| 20 |
+
- Cantor measure: FP64 (geometric precision for phase relationships)
|
| 21 |
+
- Distance matrices: FP64 compute β FP32 storage
|
| 22 |
+
- Routes: FP64 compute β int64 storage
|
| 23 |
+
- Runtime activations: FP32 (GPU optimized)
|
| 24 |
+
- Beatrix features: FP32 (sufficient for softmax)
|
| 25 |
+
|
| 26 |
+
Cache Tiers:
|
| 27 |
+
- HOT (VRAM): Common seq_lens [64, 128, 256, 512, 1024] - always resident
|
| 28 |
+
- WARM (LRU): Less common lengths - evictable under memory pressure
|
| 29 |
+
- COLD (RAMβVRAM): Large sequences >4096 - load on demand
|
| 30 |
+
|
| 31 |
+
License: MIT
|
| 32 |
+
"""
|
| 33 |
+
|
| 34 |
+
import torch
|
| 35 |
+
import torch.nn as nn
|
| 36 |
+
import torch.nn.functional as F
|
| 37 |
+
from torch import Tensor
|
| 38 |
+
from typing import Optional, Dict, Tuple, List, Literal, OrderedDict
|
| 39 |
+
from dataclasses import dataclass, field
|
| 40 |
+
from functools import lru_cache
|
| 41 |
+
from collections import OrderedDict as ODict
|
| 42 |
+
import math
|
| 43 |
+
import time
|
| 44 |
+
import warnings
|
| 45 |
+
|
| 46 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 47 |
+
# Configuration
|
| 48 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 49 |
+
|
| 50 |
+
# Cache tier definitions
|
| 51 |
+
HOT_CACHE_SIZES = frozenset([64, 128, 256, 512, 1024, 2048]) # Always in VRAM
|
| 52 |
+
WARM_CACHE_MAX_ENTRIES = 32 # LRU eviction threshold
|
| 53 |
+
COLD_THRESHOLD = 4096 # Sequences above this loaded on-demand
|
| 54 |
+
|
| 55 |
+
# Precision constants
|
| 56 |
+
GEOMETRIC_DTYPE = torch.float64 # For Cantor measure, distances
|
| 57 |
+
RUNTIME_DTYPE = torch.float32 # For activations, weights
|
| 58 |
+
INDEX_DTYPE = torch.int64 # For route indices
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
@dataclass
|
| 62 |
+
class CantorFusionConfigV2:
|
| 63 |
+
"""Configuration for optimized Cantor Multihead Sparse Fusion."""
|
| 64 |
+
|
| 65 |
+
# Architecture
|
| 66 |
+
dim: int = 512
|
| 67 |
+
num_heads: int = 8
|
| 68 |
+
head_dim: Optional[int] = None
|
| 69 |
+
|
| 70 |
+
# Simplex geometry
|
| 71 |
+
k_simplex: int = 4 # 5-vertex pentachoron
|
| 72 |
+
|
| 73 |
+
# Fusion parameters
|
| 74 |
+
fusion_window: int = 64
|
| 75 |
+
fusion_mode: Literal["weighted", "learned", "consciousness"] = "weighted"
|
| 76 |
+
|
| 77 |
+
# Beatrix staircase
|
| 78 |
+
staircase_tau: float = 0.25
|
| 79 |
+
staircase_base: int = 3
|
| 80 |
+
staircase_alpha: float = 0.5
|
| 81 |
+
|
| 82 |
+
# Optimization
|
| 83 |
+
use_beatrix_routing: bool = True
|
| 84 |
+
use_projection: bool = True
|
| 85 |
+
use_gating: bool = False
|
| 86 |
+
dropout: float = 0.1
|
| 87 |
+
residual: bool = True
|
| 88 |
+
residual_scale: float = 1.0
|
| 89 |
+
eps: float = 1e-8
|
| 90 |
+
|
| 91 |
+
# Cache configuration
|
| 92 |
+
hot_cache_sizes: Tuple[int, ...] = (64, 128, 256, 512, 1024, 2048)
|
| 93 |
+
warm_cache_max: int = 32
|
| 94 |
+
max_seq_len: int = 131_072
|
| 95 |
+
|
| 96 |
+
# Precision
|
| 97 |
+
geometric_dtype: torch.dtype = field(default=torch.float64, repr=False)
|
| 98 |
+
runtime_dtype: torch.dtype = field(default=torch.float32, repr=False)
|
| 99 |
+
|
| 100 |
+
def __post_init__(self):
|
| 101 |
+
if self.head_dim is None:
|
| 102 |
+
assert self.dim % self.num_heads == 0
|
| 103 |
+
self.head_dim = self.dim // self.num_heads
|
| 104 |
+
|
| 105 |
+
self.staircase_levels = self.k_simplex + 1
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 109 |
+
# LRU Cache for Tensors (GPU-aware)
|
| 110 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 111 |
+
|
| 112 |
+
class TensorLRUCache:
|
| 113 |
+
"""
|
| 114 |
+
LRU cache for GPU tensors with memory-aware eviction.
|
| 115 |
+
|
| 116 |
+
Separates hot (permanent) and warm (evictable) entries.
|
| 117 |
+
"""
|
| 118 |
+
|
| 119 |
+
def __init__(self, max_warm_entries: int = 32, hot_keys: frozenset = frozenset()):
|
| 120 |
+
self.max_warm = max_warm_entries
|
| 121 |
+
self.hot_keys = hot_keys
|
| 122 |
+
|
| 123 |
+
# Hot cache: never evicted
|
| 124 |
+
self._hot: Dict[str, Tensor] = {}
|
| 125 |
+
|
| 126 |
+
# Warm cache: LRU eviction
|
| 127 |
+
self._warm: ODict[str, Tensor] = ODict()
|
| 128 |
+
|
| 129 |
+
self._hits = 0
|
| 130 |
+
self._misses = 0
|
| 131 |
+
|
| 132 |
+
def _make_key(self, prefix: str, *args) -> str:
|
| 133 |
+
return f"{prefix}_{'_'.join(str(a) for a in args)}"
|
| 134 |
+
|
| 135 |
+
def get(self, key: str) -> Optional[Tensor]:
|
| 136 |
+
"""Get tensor from cache, updating LRU order for warm entries."""
|
| 137 |
+
if key in self._hot:
|
| 138 |
+
self._hits += 1
|
| 139 |
+
return self._hot[key]
|
| 140 |
+
|
| 141 |
+
if key in self._warm:
|
| 142 |
+
self._hits += 1
|
| 143 |
+
# Move to end (most recently used)
|
| 144 |
+
self._warm.move_to_end(key)
|
| 145 |
+
return self._warm[key]
|
| 146 |
+
|
| 147 |
+
self._misses += 1
|
| 148 |
+
return None
|
| 149 |
+
|
| 150 |
+
def put(self, key: str, tensor: Tensor, force_hot: bool = False) -> None:
|
| 151 |
+
"""Put tensor in cache, with automatic tier assignment."""
|
| 152 |
+
# Determine tier
|
| 153 |
+
is_hot = force_hot or any(str(h) in key for h in self.hot_keys)
|
| 154 |
+
|
| 155 |
+
if is_hot:
|
| 156 |
+
self._hot[key] = tensor
|
| 157 |
+
else:
|
| 158 |
+
# Evict if at capacity
|
| 159 |
+
while len(self._warm) >= self.max_warm:
|
| 160 |
+
evicted_key, evicted_tensor = self._warm.popitem(last=False)
|
| 161 |
+
del evicted_tensor # Allow GC
|
| 162 |
+
|
| 163 |
+
self._warm[key] = tensor
|
| 164 |
+
|
| 165 |
+
def get_or_compute(
|
| 166 |
+
self,
|
| 167 |
+
key: str,
|
| 168 |
+
compute_fn,
|
| 169 |
+
device: torch.device,
|
| 170 |
+
force_hot: bool = False
|
| 171 |
+
) -> Tensor:
|
| 172 |
+
"""Get from cache or compute and cache."""
|
| 173 |
+
cached = self.get(key)
|
| 174 |
+
if cached is not None:
|
| 175 |
+
# Ensure on correct device
|
| 176 |
+
if cached.device != device:
|
| 177 |
+
cached = cached.to(device)
|
| 178 |
+
self.put(key, cached, force_hot)
|
| 179 |
+
return cached
|
| 180 |
+
|
| 181 |
+
# Compute
|
| 182 |
+
tensor = compute_fn()
|
| 183 |
+
if tensor.device != device:
|
| 184 |
+
tensor = tensor.to(device)
|
| 185 |
+
|
| 186 |
+
self.put(key, tensor, force_hot)
|
| 187 |
+
return tensor
|
| 188 |
+
|
| 189 |
+
def clear_warm(self) -> None:
|
| 190 |
+
"""Clear warm cache (keep hot)."""
|
| 191 |
+
self._warm.clear()
|
| 192 |
+
|
| 193 |
+
def stats(self) -> Dict:
|
| 194 |
+
total = self._hits + self._misses
|
| 195 |
+
return {
|
| 196 |
+
'hot_entries': len(self._hot),
|
| 197 |
+
'warm_entries': len(self._warm),
|
| 198 |
+
'hits': self._hits,
|
| 199 |
+
'misses': self._misses,
|
| 200 |
+
'hit_rate': self._hits / max(1, total)
|
| 201 |
+
}
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 205 |
+
# Vectorized Devil's Staircase (NO LOOPS)
|
| 206 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 207 |
+
|
| 208 |
+
class VectorizedBeatrixStaircase:
|
| 209 |
+
"""
|
| 210 |
+
Fully vectorized Devil's Staircase computation.
|
| 211 |
+
|
| 212 |
+
Eliminates the level loop by computing all levels in parallel.
|
| 213 |
+
|
| 214 |
+
Mathematical basis:
|
| 215 |
+
C(x) = Ξ£_{k=1}^{L} bit_k(x) * 2^{-k}
|
| 216 |
+
|
| 217 |
+
Where bit_k is extracted via soft ternary decomposition:
|
| 218 |
+
y_k = (x * 3^k) mod 3
|
| 219 |
+
p_k = softmax(-||y_k - centers||Β² / Ο)
|
| 220 |
+
bit_k = p_k[right] + Ξ± * p_k[middle]
|
| 221 |
+
"""
|
| 222 |
+
|
| 223 |
+
def __init__(
|
| 224 |
+
self,
|
| 225 |
+
levels: int,
|
| 226 |
+
tau: float = 0.25,
|
| 227 |
+
base: int = 3,
|
| 228 |
+
alpha: float = 0.5
|
| 229 |
+
):
|
| 230 |
+
self.levels = levels
|
| 231 |
+
self.tau = tau
|
| 232 |
+
self.base = base
|
| 233 |
+
self.alpha = alpha
|
| 234 |
+
|
| 235 |
+
# Pre-compute constants (never changes)
|
| 236 |
+
self._scales = torch.tensor(
|
| 237 |
+
[base ** k for k in range(1, levels + 1)],
|
| 238 |
+
dtype=torch.float64
|
| 239 |
+
) # [L]
|
| 240 |
+
|
| 241 |
+
self._weights = torch.tensor(
|
| 242 |
+
[0.5 ** k for k in range(1, levels + 1)],
|
| 243 |
+
dtype=torch.float64
|
| 244 |
+
) # [L]
|
| 245 |
+
|
| 246 |
+
self._centers = torch.tensor([0.5, 1.5, 2.5], dtype=torch.float64) # [3]
|
| 247 |
+
self._log3 = math.log(3.0)
|
| 248 |
+
|
| 249 |
+
def to(self, device: torch.device) -> 'VectorizedBeatrixStaircase':
|
| 250 |
+
"""Move pre-computed constants to device."""
|
| 251 |
+
self._scales = self._scales.to(device)
|
| 252 |
+
self._weights = self._weights.to(device)
|
| 253 |
+
self._centers = self._centers.to(device)
|
| 254 |
+
return self
|
| 255 |
+
|
| 256 |
+
@torch.no_grad()
|
| 257 |
+
def compute_fp64(self, x: Tensor) -> Tuple[Tensor, Tensor]:
|
| 258 |
+
"""
|
| 259 |
+
Compute Devil's Staircase in FP64.
|
| 260 |
+
|
| 261 |
+
Args:
|
| 262 |
+
x: Positions in [0, 1], shape [S] or [B, S]
|
| 263 |
+
|
| 264 |
+
Returns:
|
| 265 |
+
cantor_measure: [S] or [B, S] in FP64
|
| 266 |
+
features: [S, L, 2] or [B, S, L, 2] in FP64
|
| 267 |
+
"""
|
| 268 |
+
# Ensure FP64
|
| 269 |
+
x = x.to(torch.float64)
|
| 270 |
+
device = x.device
|
| 271 |
+
|
| 272 |
+
# Move constants if needed
|
| 273 |
+
if self._scales.device != device:
|
| 274 |
+
self.to(device)
|
| 275 |
+
|
| 276 |
+
# Clamp to valid range
|
| 277 |
+
x = x.clamp(1e-10, 1.0 - 1e-10)
|
| 278 |
+
|
| 279 |
+
# Expand x for all levels: [..., 1] * [L] -> [..., L]
|
| 280 |
+
x_expanded = x.unsqueeze(-1) # [..., 1]
|
| 281 |
+
|
| 282 |
+
# Compute y_k = (x * 3^k) mod 3 for all levels at once
|
| 283 |
+
# Shape: [..., L]
|
| 284 |
+
y_all = (x_expanded * self._scales) % self.base
|
| 285 |
+
|
| 286 |
+
# Compute distances to centers for all levels
|
| 287 |
+
# y_all: [..., L], centers: [3] -> [..., L, 3]
|
| 288 |
+
d2_all = (y_all.unsqueeze(-1) - self._centers) ** 2
|
| 289 |
+
|
| 290 |
+
# Softmax probabilities: [..., L, 3]
|
| 291 |
+
logits = -d2_all / (self.tau + 1e-10)
|
| 292 |
+
p_all = F.softmax(logits, dim=-1)
|
| 293 |
+
|
| 294 |
+
# Extract bits: [..., L]
|
| 295 |
+
bits = p_all[..., 2] + self.alpha * p_all[..., 1]
|
| 296 |
+
|
| 297 |
+
# Compute Cantor measure: sum over levels with 2^{-k} weights
|
| 298 |
+
# bits: [..., L], weights: [L] -> [...]
|
| 299 |
+
cantor_measure = (bits * self._weights).sum(dim=-1)
|
| 300 |
+
|
| 301 |
+
# Compute entropy-based consciousness proxy: [..., L]
|
| 302 |
+
ent = -(p_all * p_all.clamp_min(1e-10).log()).sum(dim=-1)
|
| 303 |
+
pdf_proxy = 1.1 - ent / self._log3
|
| 304 |
+
|
| 305 |
+
# Stack features: [..., L, 2]
|
| 306 |
+
features = torch.stack([bits, pdf_proxy], dim=-1)
|
| 307 |
+
|
| 308 |
+
return cantor_measure, features
|
| 309 |
+
|
| 310 |
+
def compute_fp32(self, x: Tensor) -> Tuple[Tensor, Tensor]:
|
| 311 |
+
"""Compute in FP64, return in FP32."""
|
| 312 |
+
cantor, features = self.compute_fp64(x)
|
| 313 |
+
return cantor.float(), features.float()
|
| 314 |
+
|
| 315 |
+
|
| 316 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 317 |
+
# Vectorized Distance Matrix (NO LOOPS)
|
| 318 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 319 |
+
|
| 320 |
+
@torch.no_grad()
|
| 321 |
+
def compute_cantor_distance_matrix_fp64(
|
| 322 |
+
cantor_measure: Tensor,
|
| 323 |
+
normalize: bool = True
|
| 324 |
+
) -> Tensor:
|
| 325 |
+
"""
|
| 326 |
+
Compute pairwise Cantor distance matrix in FP64.
|
| 327 |
+
|
| 328 |
+
D[i,j] = |C(i) - C(j)|
|
| 329 |
+
|
| 330 |
+
Args:
|
| 331 |
+
cantor_measure: [S] Cantor measure values in FP64
|
| 332 |
+
normalize: Whether to normalize to [0, 1]
|
| 333 |
+
|
| 334 |
+
Returns:
|
| 335 |
+
distance_matrix: [S, S] in FP64
|
| 336 |
+
"""
|
| 337 |
+
# Ensure FP64
|
| 338 |
+
cm = cantor_measure.to(torch.float64)
|
| 339 |
+
|
| 340 |
+
# Pairwise absolute difference (vectorized)
|
| 341 |
+
# cm: [S], cm.unsqueeze: [S, 1] and [1, S] -> [S, S]
|
| 342 |
+
D = torch.abs(cm.unsqueeze(1) - cm.unsqueeze(0))
|
| 343 |
+
|
| 344 |
+
if normalize:
|
| 345 |
+
D = D / (D.max() + 1e-10)
|
| 346 |
+
|
| 347 |
+
return D
|
| 348 |
+
|
| 349 |
+
|
| 350 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 351 |
+
# Vectorized Route Building (NO LOOPS)
|
| 352 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 353 |
+
|
| 354 |
+
@torch.no_grad()
|
| 355 |
+
def compute_routes_from_distances_fp64(
|
| 356 |
+
distance_matrix: Tensor,
|
| 357 |
+
k: int
|
| 358 |
+
) -> Tensor:
|
| 359 |
+
"""
|
| 360 |
+
Compute k-nearest neighbor routes from distance matrix.
|
| 361 |
+
|
| 362 |
+
FULLY VECTORIZED - no position loop.
|
| 363 |
+
|
| 364 |
+
Args:
|
| 365 |
+
distance_matrix: [S, S] pairwise distances in FP64
|
| 366 |
+
k: Number of neighbors per position
|
| 367 |
+
|
| 368 |
+
Returns:
|
| 369 |
+
routes: [S, K] neighbor indices in int64
|
| 370 |
+
"""
|
| 371 |
+
# topk on each row (vectorized over all positions)
|
| 372 |
+
# Returns k smallest distances and their indices
|
| 373 |
+
_, routes = torch.topk(distance_matrix, k, dim=1, largest=False)
|
| 374 |
+
|
| 375 |
+
return routes.to(INDEX_DTYPE)
|
| 376 |
+
|
| 377 |
+
|
| 378 |
+
@torch.no_grad()
|
| 379 |
+
def compute_route_distances_fp64(
|
| 380 |
+
distance_matrix: Tensor,
|
| 381 |
+
routes: Tensor
|
| 382 |
+
) -> Tensor:
|
| 383 |
+
"""
|
| 384 |
+
Gather distances for computed routes.
|
| 385 |
+
|
| 386 |
+
Args:
|
| 387 |
+
distance_matrix: [S, S] pairwise distances
|
| 388 |
+
routes: [S, K] neighbor indices
|
| 389 |
+
|
| 390 |
+
Returns:
|
| 391 |
+
route_distances: [S, K] distances to each neighbor
|
| 392 |
+
"""
|
| 393 |
+
S, K = routes.shape
|
| 394 |
+
|
| 395 |
+
# Use gather to extract distances
|
| 396 |
+
# distance_matrix: [S, S], routes: [S, K]
|
| 397 |
+
# We want D[i, routes[i, j]] for all i, j
|
| 398 |
+
route_distances = torch.gather(distance_matrix, dim=1, index=routes)
|
| 399 |
+
|
| 400 |
+
return route_distances
|
| 401 |
+
|
| 402 |
+
|
| 403 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 404 |
+
# Vectorized Fusion Weights (NO LOOPS)
|
| 405 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 406 |
+
|
| 407 |
+
def compute_distance_weights_vectorized(
|
| 408 |
+
route_distances: Tensor,
|
| 409 |
+
eps: float = 1e-8
|
| 410 |
+
) -> Tensor:
|
| 411 |
+
"""
|
| 412 |
+
Compute inverse-distance fusion weights.
|
| 413 |
+
|
| 414 |
+
w[i,j] = 1 / (d[i, routes[i,j]] + eps)
|
| 415 |
+
|
| 416 |
+
Args:
|
| 417 |
+
route_distances: [S, K] or [B, H, S, K] distances
|
| 418 |
+
eps: Numerical stability
|
| 419 |
+
|
| 420 |
+
Returns:
|
| 421 |
+
weights: Same shape, normalized over K dimension
|
| 422 |
+
"""
|
| 423 |
+
# Inverse distance
|
| 424 |
+
weights = 1.0 / (route_distances + eps)
|
| 425 |
+
|
| 426 |
+
# Softmax normalization over neighbors
|
| 427 |
+
weights = F.softmax(weights, dim=-1)
|
| 428 |
+
|
| 429 |
+
return weights
|
| 430 |
+
|
| 431 |
+
|
| 432 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 433 |
+
# Optimized Sparse Gather
|
| 434 |
+
# ββββββββββββββββββββββββββββββββββββββοΏ½οΏ½βββββββββββββββββββββββββββ
|
| 435 |
+
|
| 436 |
+
def sparse_gather_optimized(
|
| 437 |
+
x: Tensor,
|
| 438 |
+
routes: Tensor
|
| 439 |
+
) -> Tensor:
|
| 440 |
+
"""
|
| 441 |
+
Gather neighbors according to routes.
|
| 442 |
+
|
| 443 |
+
Optimized implementation using torch.gather with minimal memory.
|
| 444 |
+
|
| 445 |
+
Args:
|
| 446 |
+
x: [B, H, S, D] input tensor
|
| 447 |
+
routes: [S, K] neighbor indices
|
| 448 |
+
|
| 449 |
+
Returns:
|
| 450 |
+
gathered: [B, H, S, K, D]
|
| 451 |
+
"""
|
| 452 |
+
B, H, S, D = x.shape
|
| 453 |
+
K = routes.shape[1]
|
| 454 |
+
|
| 455 |
+
# Expand routes for batch/head dimensions: [1, 1, S, K] -> [B, H, S, K]
|
| 456 |
+
routes_exp = routes.unsqueeze(0).unsqueeze(0).expand(B, H, -1, -1)
|
| 457 |
+
|
| 458 |
+
# Add dimension for head_dim: [B, H, S, K, 1] -> [B, H, S, K, D]
|
| 459 |
+
routes_gather = routes_exp.unsqueeze(-1).expand(-1, -1, -1, -1, D)
|
| 460 |
+
|
| 461 |
+
# Expand x for K neighbors: [B, H, S, 1, D] -> [B, H, S, K, D]
|
| 462 |
+
x_expanded = x.unsqueeze(3).expand(-1, -1, -1, K, -1)
|
| 463 |
+
|
| 464 |
+
# Gather along sequence dimension
|
| 465 |
+
gathered = torch.gather(x_expanded, dim=2, index=routes_gather)
|
| 466 |
+
|
| 467 |
+
return gathered
|
| 468 |
+
|
| 469 |
+
|
| 470 |
+
def sparse_weighted_sum(
|
| 471 |
+
gathered: Tensor,
|
| 472 |
+
weights: Tensor
|
| 473 |
+
) -> Tensor:
|
| 474 |
+
"""
|
| 475 |
+
Compute weighted sum over gathered neighbors.
|
| 476 |
+
|
| 477 |
+
Args:
|
| 478 |
+
gathered: [B, H, S, K, D]
|
| 479 |
+
weights: [B, H, S, K]
|
| 480 |
+
|
| 481 |
+
Returns:
|
| 482 |
+
output: [B, H, S, D]
|
| 483 |
+
"""
|
| 484 |
+
# einsum is most efficient here
|
| 485 |
+
return torch.einsum('bhskd,bhsk->bhsd', gathered, weights)
|
| 486 |
+
|
| 487 |
+
|
| 488 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 489 |
+
# Main Module
|
| 490 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 491 |
+
|
| 492 |
+
class CantorMultiheadFusionV2(nn.Module):
|
| 493 |
+
"""
|
| 494 |
+
Cantor Multihead Sparse Fusion - V2 Optimized
|
| 495 |
+
|
| 496 |
+
Key Optimizations:
|
| 497 |
+
1. ZERO for-loops in forward pass
|
| 498 |
+
2. LRU cache with hot/warm tiers
|
| 499 |
+
3. FP64 geometry β FP32 runtime
|
| 500 |
+
4. Vectorized all operations
|
| 501 |
+
5. Pre-computed routes and distances
|
| 502 |
+
6. Memory-efficient gather
|
| 503 |
+
|
| 504 |
+
Forward Complexity: O(n * k * d) where k << n
|
| 505 |
+
Memory: O(n * k * d) - no O(nΒ²) attention matrix
|
| 506 |
+
"""
|
| 507 |
+
|
| 508 |
+
def __init__(self, config: CantorFusionConfigV2):
|
| 509 |
+
super().__init__()
|
| 510 |
+
self.config = config
|
| 511 |
+
self.dim = config.dim
|
| 512 |
+
self.num_heads = config.num_heads
|
| 513 |
+
self.head_dim = config.head_dim
|
| 514 |
+
self.k = config.fusion_window
|
| 515 |
+
|
| 516 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 517 |
+
# Buffers (non-learnable, persistent)
|
| 518 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 519 |
+
|
| 520 |
+
self.register_buffer(
|
| 521 |
+
'residual_scale',
|
| 522 |
+
torch.tensor(config.residual_scale, dtype=RUNTIME_DTYPE),
|
| 523 |
+
persistent=True
|
| 524 |
+
)
|
| 525 |
+
|
| 526 |
+
self.register_buffer(
|
| 527 |
+
'eps',
|
| 528 |
+
torch.tensor(config.eps, dtype=RUNTIME_DTYPE),
|
| 529 |
+
persistent=True
|
| 530 |
+
)
|
| 531 |
+
|
| 532 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 533 |
+
# Beatrix Staircase Computer
|
| 534 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 535 |
+
|
| 536 |
+
self.staircase = VectorizedBeatrixStaircase(
|
| 537 |
+
levels=config.staircase_levels,
|
| 538 |
+
tau=config.staircase_tau,
|
| 539 |
+
base=config.staircase_base,
|
| 540 |
+
alpha=config.staircase_alpha
|
| 541 |
+
)
|
| 542 |
+
|
| 543 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 544 |
+
# LRU Cache
|
| 545 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 546 |
+
|
| 547 |
+
self.cache = TensorLRUCache(
|
| 548 |
+
max_warm_entries=config.warm_cache_max,
|
| 549 |
+
hot_keys=frozenset(config.hot_cache_sizes)
|
| 550 |
+
)
|
| 551 |
+
|
| 552 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 553 |
+
# Learnable Layers
|
| 554 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 555 |
+
|
| 556 |
+
# Input projection
|
| 557 |
+
if config.use_projection:
|
| 558 |
+
self.in_proj = nn.Linear(config.dim, config.dim, bias=False)
|
| 559 |
+
else:
|
| 560 |
+
self.in_proj = nn.Identity()
|
| 561 |
+
|
| 562 |
+
# Fusion weight network (for learned/consciousness modes)
|
| 563 |
+
if config.fusion_mode == "learned":
|
| 564 |
+
self.fusion_net = nn.Sequential(
|
| 565 |
+
nn.Linear(config.head_dim * 2, config.head_dim),
|
| 566 |
+
nn.ReLU(),
|
| 567 |
+
nn.Linear(config.head_dim, 1)
|
| 568 |
+
)
|
| 569 |
+
elif config.fusion_mode == "consciousness":
|
| 570 |
+
consciousness_dim = config.staircase_levels * 2
|
| 571 |
+
self.fusion_net = nn.Sequential(
|
| 572 |
+
nn.Linear(config.head_dim * 2 + consciousness_dim, config.head_dim // 2),
|
| 573 |
+
nn.GELU(),
|
| 574 |
+
nn.Linear(config.head_dim // 2, 1)
|
| 575 |
+
)
|
| 576 |
+
else:
|
| 577 |
+
self.fusion_net = None
|
| 578 |
+
|
| 579 |
+
# Optional gating
|
| 580 |
+
if config.use_gating:
|
| 581 |
+
self.gate = nn.Linear(config.dim, config.num_heads)
|
| 582 |
+
else:
|
| 583 |
+
self.gate = None
|
| 584 |
+
|
| 585 |
+
# Output projection
|
| 586 |
+
self.out_proj = nn.Linear(config.dim, config.dim, bias=True)
|
| 587 |
+
|
| 588 |
+
# Dropout
|
| 589 |
+
self.dropout = nn.Dropout(config.dropout)
|
| 590 |
+
|
| 591 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 592 |
+
# Pre-build hot cache
|
| 593 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 594 |
+
|
| 595 |
+
self._prebuild_hot_cache()
|
| 596 |
+
|
| 597 |
+
def _prebuild_hot_cache(self) -> None:
|
| 598 |
+
"""Pre-compute and cache structures for hot sequence lengths."""
|
| 599 |
+
print(f"[CantorFusionV2] Pre-building hot cache for {self.config.hot_cache_sizes}...")
|
| 600 |
+
start = time.time()
|
| 601 |
+
|
| 602 |
+
for seq_len in self.config.hot_cache_sizes:
|
| 603 |
+
if seq_len > self.config.max_seq_len:
|
| 604 |
+
continue
|
| 605 |
+
|
| 606 |
+
# Compute all structures in FP64
|
| 607 |
+
self._compute_and_cache_structures(seq_len, force_hot=True)
|
| 608 |
+
|
| 609 |
+
elapsed = time.time() - start
|
| 610 |
+
print(f"[CantorFusionV2] β Hot cache built in {elapsed:.2f}s")
|
| 611 |
+
print(f" Cache stats: {self.cache.stats()}")
|
| 612 |
+
|
| 613 |
+
@torch.no_grad()
|
| 614 |
+
def _compute_and_cache_structures(
|
| 615 |
+
self,
|
| 616 |
+
seq_len: int,
|
| 617 |
+
device: torch.device = torch.device('cpu'),
|
| 618 |
+
force_hot: bool = False
|
| 619 |
+
) -> Tuple[Tensor, Tensor, Tensor, Tensor]:
|
| 620 |
+
"""
|
| 621 |
+
Compute all geometric structures for a sequence length.
|
| 622 |
+
|
| 623 |
+
All computation in FP64 for geometric precision.
|
| 624 |
+
Storage in appropriate dtype (routes: int64, others: fp32).
|
| 625 |
+
|
| 626 |
+
Returns:
|
| 627 |
+
cantor_measure: [S] FP32
|
| 628 |
+
features: [S, L, 2] FP32
|
| 629 |
+
routes: [S, K] int64
|
| 630 |
+
route_distances: [S, K] FP32
|
| 631 |
+
"""
|
| 632 |
+
# Keys for cache
|
| 633 |
+
key_cantor = f"cantor_{seq_len}"
|
| 634 |
+
key_features = f"features_{seq_len}"
|
| 635 |
+
key_routes = f"routes_{seq_len}_{self.k}"
|
| 636 |
+
key_distances = f"route_dist_{seq_len}_{self.k}"
|
| 637 |
+
|
| 638 |
+
# Check if all cached
|
| 639 |
+
cached_cantor = self.cache.get(key_cantor)
|
| 640 |
+
if cached_cantor is not None:
|
| 641 |
+
return (
|
| 642 |
+
self.cache.get(key_cantor),
|
| 643 |
+
self.cache.get(key_features),
|
| 644 |
+
self.cache.get(key_routes),
|
| 645 |
+
self.cache.get(key_distances)
|
| 646 |
+
)
|
| 647 |
+
|
| 648 |
+
# Compute Cantor measure and features in FP64
|
| 649 |
+
positions = torch.linspace(0, 1, seq_len, dtype=torch.float64, device=device)
|
| 650 |
+
cantor_fp64, features_fp64 = self.staircase.compute_fp64(positions)
|
| 651 |
+
|
| 652 |
+
# Compute distance matrix in FP64
|
| 653 |
+
dist_matrix_fp64 = compute_cantor_distance_matrix_fp64(cantor_fp64)
|
| 654 |
+
|
| 655 |
+
# Compute routes (vectorized, no loops)
|
| 656 |
+
routes = compute_routes_from_distances_fp64(dist_matrix_fp64, self.k)
|
| 657 |
+
|
| 658 |
+
# Gather route distances
|
| 659 |
+
route_distances_fp64 = compute_route_distances_fp64(dist_matrix_fp64, routes)
|
| 660 |
+
|
| 661 |
+
# Convert to storage dtype
|
| 662 |
+
cantor_fp32 = cantor_fp64.float()
|
| 663 |
+
features_fp32 = features_fp64.float()
|
| 664 |
+
route_distances_fp32 = route_distances_fp64.float()
|
| 665 |
+
|
| 666 |
+
# Cache all
|
| 667 |
+
self.cache.put(key_cantor, cantor_fp32, force_hot)
|
| 668 |
+
self.cache.put(key_features, features_fp32, force_hot)
|
| 669 |
+
self.cache.put(key_routes, routes, force_hot)
|
| 670 |
+
self.cache.put(key_distances, route_distances_fp32, force_hot)
|
| 671 |
+
|
| 672 |
+
return cantor_fp32, features_fp32, routes, route_distances_fp32
|
| 673 |
+
|
| 674 |
+
def _get_cached_structures(
|
| 675 |
+
self,
|
| 676 |
+
seq_len: int,
|
| 677 |
+
device: torch.device
|
| 678 |
+
) -> Tuple[Tensor, Tensor, Tensor, Tensor]:
|
| 679 |
+
"""Get structures from cache, computing if necessary."""
|
| 680 |
+
key_cantor = f"cantor_{seq_len}"
|
| 681 |
+
|
| 682 |
+
# Try cache first
|
| 683 |
+
cached = self.cache.get(key_cantor)
|
| 684 |
+
if cached is not None and cached.device == device:
|
| 685 |
+
return (
|
| 686 |
+
self.cache.get(key_cantor),
|
| 687 |
+
self.cache.get(f"features_{seq_len}"),
|
| 688 |
+
self.cache.get(f"routes_{seq_len}_{self.k}"),
|
| 689 |
+
self.cache.get(f"route_dist_{seq_len}_{self.k}")
|
| 690 |
+
)
|
| 691 |
+
|
| 692 |
+
# Compute and cache
|
| 693 |
+
is_hot = seq_len in self.config.hot_cache_sizes
|
| 694 |
+
structures = self._compute_and_cache_structures(
|
| 695 |
+
seq_len, device=device, force_hot=is_hot
|
| 696 |
+
)
|
| 697 |
+
|
| 698 |
+
# Ensure on correct device
|
| 699 |
+
return tuple(t.to(device) for t in structures)
|
| 700 |
+
|
| 701 |
+
def forward(
|
| 702 |
+
self,
|
| 703 |
+
x: Tensor,
|
| 704 |
+
mask: Optional[Tensor] = None
|
| 705 |
+
) -> Dict[str, Tensor]:
|
| 706 |
+
"""
|
| 707 |
+
Forward pass with ZERO for-loops.
|
| 708 |
+
|
| 709 |
+
Args:
|
| 710 |
+
x: [B, S, D] input tensor
|
| 711 |
+
mask: Optional [B, S] attention mask
|
| 712 |
+
|
| 713 |
+
Returns:
|
| 714 |
+
Dict with 'output', 'cantor_measure', 'consciousness'
|
| 715 |
+
"""
|
| 716 |
+
B, S, D = x.shape
|
| 717 |
+
device = x.device
|
| 718 |
+
|
| 719 |
+
# Validate sequence length
|
| 720 |
+
if S > self.config.max_seq_len:
|
| 721 |
+
raise ValueError(f"Sequence length {S} exceeds max {self.config.max_seq_len}")
|
| 722 |
+
|
| 723 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 724 |
+
# Get pre-computed structures (from cache)
|
| 725 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 726 |
+
|
| 727 |
+
cantor_measure, features, routes, route_distances = \
|
| 728 |
+
self._get_cached_structures(S, device)
|
| 729 |
+
|
| 730 |
+
# Consciousness from features
|
| 731 |
+
consciousness = features[..., 1].mean(dim=-1) # [S]
|
| 732 |
+
|
| 733 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 734 |
+
# Input processing
|
| 735 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 736 |
+
|
| 737 |
+
# Residual connection
|
| 738 |
+
residual = x * self.residual_scale
|
| 739 |
+
|
| 740 |
+
# Input projection
|
| 741 |
+
x = self.in_proj(x)
|
| 742 |
+
|
| 743 |
+
# Reshape to heads: [B, S, D] -> [B, H, S, head_dim]
|
| 744 |
+
x = x.view(B, S, self.num_heads, self.head_dim).transpose(1, 2)
|
| 745 |
+
|
| 746 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 747 |
+
# Sparse gather (vectorized)
|
| 748 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 749 |
+
|
| 750 |
+
# Gather neighbors: [B, H, S, K, head_dim]
|
| 751 |
+
x_gathered = sparse_gather_optimized(x, routes)
|
| 752 |
+
|
| 753 |
+
# Apply mask if provided
|
| 754 |
+
if mask is not None:
|
| 755 |
+
# Gather mask values for neighbors
|
| 756 |
+
mask_gathered = torch.gather(
|
| 757 |
+
mask.unsqueeze(1).expand(-1, S, -1),
|
| 758 |
+
dim=2,
|
| 759 |
+
index=routes.unsqueeze(0).expand(B, -1, -1)
|
| 760 |
+
) # [B, S, K]
|
| 761 |
+
x_gathered = x_gathered * mask_gathered.unsqueeze(1).unsqueeze(-1)
|
| 762 |
+
|
| 763 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 764 |
+
# Compute fusion weights (mode-dependent)
|
| 765 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 766 |
+
|
| 767 |
+
if self.config.fusion_mode == "weighted":
|
| 768 |
+
# Distance-based weights (vectorized)
|
| 769 |
+
# route_distances: [S, K] -> [1, 1, S, K]
|
| 770 |
+
weights = compute_distance_weights_vectorized(
|
| 771 |
+
route_distances.unsqueeze(0).unsqueeze(0).expand(B, self.num_heads, -1, -1),
|
| 772 |
+
eps=self.eps.item()
|
| 773 |
+
)
|
| 774 |
+
|
| 775 |
+
elif self.config.fusion_mode == "learned":
|
| 776 |
+
# Learned weights from anchor + gathered pairs
|
| 777 |
+
x_anchor = x.unsqueeze(3).expand(-1, -1, -1, self.k, -1) # [B, H, S, K, D]
|
| 778 |
+
combined = torch.cat([x_anchor, x_gathered], dim=-1) # [B, H, S, K, 2D]
|
| 779 |
+
weights = self.fusion_net(combined).squeeze(-1) # [B, H, S, K]
|
| 780 |
+
weights = F.softmax(weights, dim=-1)
|
| 781 |
+
|
| 782 |
+
elif self.config.fusion_mode == "consciousness":
|
| 783 |
+
# Consciousness-aware learned weights
|
| 784 |
+
x_anchor = x.unsqueeze(3).expand(-1, -1, -1, self.k, -1)
|
| 785 |
+
|
| 786 |
+
# Expand features for neighbors: [S, L, 2] -> [B, H, S, K, L*2]
|
| 787 |
+
features_flat = features.view(S, -1) # [S, L*2]
|
| 788 |
+
features_exp = features_flat.unsqueeze(1).expand(-1, self.k, -1) # [S, K, L*2]
|
| 789 |
+
features_exp = features_exp.unsqueeze(0).unsqueeze(0).expand(B, self.num_heads, -1, -1, -1)
|
| 790 |
+
|
| 791 |
+
combined = torch.cat([x_anchor, x_gathered, features_exp], dim=-1)
|
| 792 |
+
weights = self.fusion_net(combined).squeeze(-1)
|
| 793 |
+
weights = F.softmax(weights, dim=-1)
|
| 794 |
+
|
| 795 |
+
else:
|
| 796 |
+
raise ValueError(f"Unknown fusion mode: {self.config.fusion_mode}")
|
| 797 |
+
|
| 798 |
+
# Apply dropout to weights
|
| 799 |
+
weights = self.dropout(weights)
|
| 800 |
+
|
| 801 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 802 |
+
# Weighted aggregation (vectorized)
|
| 803 |
+
# ββββββββββββββοΏ½οΏ½βββββββββββββββββββββββββββββββββ
|
| 804 |
+
|
| 805 |
+
# [B, H, S, K, D] x [B, H, S, K] -> [B, H, S, D]
|
| 806 |
+
fused = sparse_weighted_sum(x_gathered, weights)
|
| 807 |
+
|
| 808 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 809 |
+
# Optional gating
|
| 810 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 811 |
+
|
| 812 |
+
if self.gate is not None:
|
| 813 |
+
# Compute gate from original input (pre-projection)
|
| 814 |
+
gate_input = residual / self.residual_scale
|
| 815 |
+
gates = torch.sigmoid(self.gate(gate_input)) # [B, S, H]
|
| 816 |
+
gates = gates.transpose(1, 2).unsqueeze(-1) # [B, H, S, 1]
|
| 817 |
+
fused = fused * gates
|
| 818 |
+
|
| 819 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 820 |
+
# Output
|
| 821 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 822 |
+
|
| 823 |
+
# Reshape back: [B, H, S, D] -> [B, S, H*D]
|
| 824 |
+
fused = fused.transpose(1, 2).reshape(B, S, self.dim)
|
| 825 |
+
|
| 826 |
+
# Output projection
|
| 827 |
+
output = self.out_proj(fused)
|
| 828 |
+
output = self.dropout(output)
|
| 829 |
+
|
| 830 |
+
# Residual connection
|
| 831 |
+
if self.config.residual:
|
| 832 |
+
output = output + residual
|
| 833 |
+
|
| 834 |
+
return {
|
| 835 |
+
'output': output,
|
| 836 |
+
'cantor_measure': cantor_measure.unsqueeze(0).expand(B, -1),
|
| 837 |
+
'consciousness': consciousness.unsqueeze(0).expand(B, -1),
|
| 838 |
+
'weights': weights # For analysis
|
| 839 |
+
}
|
| 840 |
+
|
| 841 |
+
def get_cache_stats(self) -> Dict:
|
| 842 |
+
"""Get cache statistics."""
|
| 843 |
+
return self.cache.stats()
|
| 844 |
+
|
| 845 |
+
def clear_warm_cache(self) -> None:
|
| 846 |
+
"""Clear warm cache entries (keep hot)."""
|
| 847 |
+
self.cache.clear_warm()
|
| 848 |
+
|
| 849 |
+
def extra_repr(self) -> str:
|
| 850 |
+
return (
|
| 851 |
+
f'dim={self.dim}, heads={self.num_heads}, '
|
| 852 |
+
f'k={self.k}, mode={self.config.fusion_mode}, '
|
| 853 |
+
f'k_simplex={self.config.k_simplex}'
|
| 854 |
+
)
|
| 855 |
+
|
| 856 |
+
|
| 857 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 858 |
+
# Factory Function
|
| 859 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 860 |
+
|
| 861 |
+
def create_cantor_fusion_v2(
|
| 862 |
+
dim: int,
|
| 863 |
+
num_heads: int = 8,
|
| 864 |
+
fusion_window: int = 64,
|
| 865 |
+
fusion_mode: str = "weighted",
|
| 866 |
+
k_simplex: int = 4,
|
| 867 |
+
use_beatrix: bool = True,
|
| 868 |
+
use_gating: bool = False,
|
| 869 |
+
dropout: float = 0.1,
|
| 870 |
+
**kwargs
|
| 871 |
+
) -> CantorMultiheadFusionV2:
|
| 872 |
+
"""Create optimized Cantor fusion layer."""
|
| 873 |
+
config = CantorFusionConfigV2(
|
| 874 |
+
dim=dim,
|
| 875 |
+
num_heads=num_heads,
|
| 876 |
+
fusion_window=fusion_window,
|
| 877 |
+
fusion_mode=fusion_mode,
|
| 878 |
+
k_simplex=k_simplex,
|
| 879 |
+
use_beatrix_routing=use_beatrix,
|
| 880 |
+
use_gating=use_gating,
|
| 881 |
+
dropout=dropout,
|
| 882 |
+
**kwargs
|
| 883 |
+
)
|
| 884 |
+
return CantorMultiheadFusionV2(config)
|
| 885 |
+
|
| 886 |
+
|
| 887 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 888 |
+
# Tests
|
| 889 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 890 |
+
|
| 891 |
+
if __name__ == "__main__":
|
| 892 |
+
print("=" * 70)
|
| 893 |
+
print("CantorMultiheadFusion V2 - Optimized Tests")
|
| 894 |
+
print("=" * 70)
|
| 895 |
+
|
| 896 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 897 |
+
print(f"Device: {device}\n")
|
| 898 |
+
|
| 899 |
+
# Test 1: Vectorized Beatrix Staircase
|
| 900 |
+
print("[Test 1] Vectorized Beatrix Staircase")
|
| 901 |
+
staircase = VectorizedBeatrixStaircase(levels=5, tau=0.25)
|
| 902 |
+
x = torch.linspace(0, 1, 1000)
|
| 903 |
+
|
| 904 |
+
cantor, features = staircase.compute_fp64(x)
|
| 905 |
+
print(f" Input: {x.shape}, dtype={x.dtype}")
|
| 906 |
+
print(f" Cantor: {cantor.shape}, dtype={cantor.dtype}")
|
| 907 |
+
print(f" Features: {features.shape}, dtype={features.dtype}")
|
| 908 |
+
print(f" Cantor range: [{cantor.min():.4f}, {cantor.max():.4f}]")
|
| 909 |
+
print(f" Monotonic: {(cantor[1:] >= cantor[:-1]).float().mean():.2%}")
|
| 910 |
+
print(" β PASS\n")
|
| 911 |
+
|
| 912 |
+
# Test 2: Vectorized Distance Matrix
|
| 913 |
+
print("[Test 2] Vectorized Distance Matrix")
|
| 914 |
+
D = compute_cantor_distance_matrix_fp64(cantor[:100])
|
| 915 |
+
print(f" Shape: {D.shape}")
|
| 916 |
+
print(f" Symmetric: {torch.allclose(D, D.T)}")
|
| 917 |
+
print(f" Zero diagonal: {D.diagonal().abs().max().item() < 1e-10}")
|
| 918 |
+
print(" β PASS\n")
|
| 919 |
+
|
| 920 |
+
# Test 3: Vectorized Route Building
|
| 921 |
+
print("[Test 3] Vectorized Route Building")
|
| 922 |
+
routes = compute_routes_from_distances_fp64(D, k=16)
|
| 923 |
+
print(f" Routes shape: {routes.shape}")
|
| 924 |
+
print(f" Routes dtype: {routes.dtype}")
|
| 925 |
+
print(f" Self-included: {(routes[:, 0] == torch.arange(100)).float().mean():.2%}")
|
| 926 |
+
print(" β PASS\n")
|
| 927 |
+
|
| 928 |
+
# Test 4: Full Module
|
| 929 |
+
print("[Test 4] CantorMultiheadFusionV2 Forward")
|
| 930 |
+
config = CantorFusionConfigV2(
|
| 931 |
+
dim=256,
|
| 932 |
+
num_heads=8,
|
| 933 |
+
fusion_window=32,
|
| 934 |
+
fusion_mode="weighted",
|
| 935 |
+
k_simplex=4,
|
| 936 |
+
hot_cache_sizes=(64, 128, 256)
|
| 937 |
+
)
|
| 938 |
+
|
| 939 |
+
model = CantorMultiheadFusionV2(config).to(device)
|
| 940 |
+
x = torch.randn(2, 128, 256, device=device)
|
| 941 |
+
|
| 942 |
+
with torch.no_grad():
|
| 943 |
+
result = model(x)
|
| 944 |
+
|
| 945 |
+
print(f" Input: {x.shape}")
|
| 946 |
+
print(f" Output: {result['output'].shape}")
|
| 947 |
+
print(f" Cantor: {result['cantor_measure'].shape}")
|
| 948 |
+
print(f" Consciousness: {result['consciousness'].shape}")
|
| 949 |
+
print(f" Cache stats: {model.get_cache_stats()}")
|
| 950 |
+
print(" β PASS\n")
|
| 951 |
+
|
| 952 |
+
# Test 5: Gradient Flow
|
| 953 |
+
print("[Test 5] Gradient Flow")
|
| 954 |
+
x_grad = torch.randn(2, 64, 256, device=device, requires_grad=True)
|
| 955 |
+
result = model(x_grad)
|
| 956 |
+
loss = result['output'].sum()
|
| 957 |
+
loss.backward()
|
| 958 |
+
|
| 959 |
+
print(f" Gradient norm: {x_grad.grad.norm().item():.4f}")
|
| 960 |
+
print(f" Gradient finite: {torch.isfinite(x_grad.grad).all()}")
|
| 961 |
+
print(" β PASS\n")
|
| 962 |
+
|
| 963 |
+
# Test 6: Speed Benchmark
|
| 964 |
+
print("[Test 6] Speed Benchmark")
|
| 965 |
+
model.eval()
|
| 966 |
+
x_bench = torch.randn(4, 512, 256, device=device)
|
| 967 |
+
|
| 968 |
+
# Warmup
|
| 969 |
+
for _ in range(10):
|
| 970 |
+
with torch.no_grad():
|
| 971 |
+
_ = model(x_bench)
|
| 972 |
+
|
| 973 |
+
if device.type == "cuda":
|
| 974 |
+
torch.cuda.synchronize()
|
| 975 |
+
|
| 976 |
+
import time
|
| 977 |
+
|
| 978 |
+
start = time.time()
|
| 979 |
+
for _ in range(50):
|
| 980 |
+
with torch.no_grad():
|
| 981 |
+
_ = model(x_bench)
|
| 982 |
+
|
| 983 |
+
if device.type == "cuda":
|
| 984 |
+
torch.cuda.synchronize()
|
| 985 |
+
|
| 986 |
+
elapsed = (time.time() - start) / 50
|
| 987 |
+
throughput = 4 * 512 / elapsed
|
| 988 |
+
|
| 989 |
+
print(f" Batch: [4, 512, 256]")
|
| 990 |
+
print(f" Time per forward: {elapsed * 1000:.2f}ms")
|
| 991 |
+
print(f" Throughput: {throughput:.0f} tokens/sec")
|
| 992 |
+
print(" β PASS\n")
|
| 993 |
+
|
| 994 |
+
# Test 7: Cache Hit Rates
|
| 995 |
+
print("[Test 7] Cache Hit Rates")
|
| 996 |
+
|
| 997 |
+
# Simulate mixed workload
|
| 998 |
+
model.cache._hits = 0
|
| 999 |
+
model.cache._misses = 0
|
| 1000 |
+
|
| 1001 |
+
for seq_len in [64, 128, 64, 256, 64, 128, 512, 64]:
|
| 1002 |
+
x_test = torch.randn(1, seq_len, 256, device=device)
|
| 1003 |
+
with torch.no_grad():
|
| 1004 |
+
_ = model(x_test)
|
| 1005 |
+
|
| 1006 |
+
stats = model.get_cache_stats()
|
| 1007 |
+
print(f" Hot entries: {stats['hot_entries']}")
|
| 1008 |
+
print(f" Warm entries: {stats['warm_entries']}")
|
| 1009 |
+
print(f" Hit rate: {stats['hit_rate']:.2%}")
|
| 1010 |
+
print(" β PASS\n")
|
| 1011 |
+
|
| 1012 |
+
print("=" * 70)
|
| 1013 |
+
print("All tests passed! V2 optimizations verified.")
|
| 1014 |
+
print("=" * 70)
|