File size: 20,794 Bytes
7b4d453 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 | """
Constellation Bottleneck β Full Analysis
==========================================
Paste directly after the training cell.
Uses `model` already in memory.
"""
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import math
import os
from torchvision import datasets, transforms
from torchvision.utils import save_image, make_grid
DEVICE = "cuda"
os.makedirs("analysis_bn", exist_ok=True)
def compute_cv(points, n_samples=1500, n_points=5):
N = points.shape[0]
if N < n_points: return float('nan')
points = F.normalize(points.to(DEVICE).float(), dim=-1)
vols = []
for _ in range(n_samples):
idx = torch.randperm(min(N, 5000), device=DEVICE)[:n_points]
pts = points[idx].unsqueeze(0)
gram = torch.bmm(pts, pts.transpose(1, 2))
norms = torch.diagonal(gram, dim1=1, dim2=2)
d2 = norms.unsqueeze(2) + norms.unsqueeze(1) - 2 * gram
d2 = F.relu(d2)
cm = torch.zeros(1, 6, 6, device=DEVICE, dtype=torch.float32)
cm[:, 0, 1:] = 1; cm[:, 1:, 0] = 1; cm[:, 1:, 1:] = d2
v2 = -torch.linalg.det(cm) / 9216
if v2[0].item() > 1e-20:
vols.append(v2[0].sqrt().cpu())
if len(vols) < 50: return float('nan')
vt = torch.stack(vols)
return (vt.std() / (vt.mean() + 1e-8)).item()
def eff_dim(x):
x_c = x - x.mean(0, keepdim=True)
n = min(512, x.shape[0])
_, S, _ = torch.linalg.svd(x_c[:n].float(), full_matrices=False)
p = S / S.sum()
return p.pow(2).sum().reciprocal().item()
CLASS_NAMES = ['plane','auto','bird','cat','deer','dog','frog','horse','ship','truck']
model.eval()
bn = model.bottleneck
print("=" * 80)
print("CONSTELLATION BOTTLENECK β FULL ANALYSIS")
print(f" Params: {sum(p.numel() for p in model.parameters()):,}")
print(f" Bottleneck: {sum(p.numel() for p in bn.parameters()):,}")
print("=" * 80)
# Load test data
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5,)*3, (0.5,)*3),
])
test_ds = datasets.CIFAR10('./data', train=False, download=True, transform=transform)
test_loader = torch.utils.data.DataLoader(test_ds, batch_size=256, shuffle=False)
images_test, labels_test = next(iter(test_loader))
images_test = images_test.to(DEVICE)
labels_test = labels_test.to(DEVICE)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# TEST 1: BOTTLENECK DIAGNOSTICS
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
print(f"\n{'β'*80}")
print("TEST 1: Bottleneck Diagnostics")
print(f"{'β'*80}")
drift = bn.drift().detach()
home = F.normalize(bn.home, dim=-1).detach()
curr = F.normalize(bn.anchors, dim=-1).detach()
P, A, d = home.shape
print(f" Patches: {P}, Anchors/patch: {A}, Patch dim: {d}")
print(f" Drift: mean={drift.mean():.6f} rad ({math.degrees(drift.mean()):.2f}Β°)")
print(f" std={drift.std():.6f} min={drift.min():.6f} max={drift.max():.6f}")
print(f" max degrees: {math.degrees(drift.max()):.2f}Β°")
print(f" Skip gate: {bn.skip_gate.sigmoid().item():.4f}")
print(f" Near 0.29154: {(drift - 0.29154).abs().lt(0.05).float().mean().item():.1%}")
# Per-patch drift
print(f"\n Per-patch drift:")
for p in range(P):
d_p = drift[p].mean().item()
d_max = drift[p].max().item()
marker = " β 0.29" if abs(d_p - 0.29154) < 0.05 else ""
marker2 = " β MAX near 0.29" if abs(d_max - 0.29154) < 0.05 else ""
print(f" P{p:2d}: mean={d_p:.4f} ({math.degrees(d_p):.1f}Β°) "
f"max={d_max:.4f} ({math.degrees(d_max):.1f}Β°){marker}{marker2}")
# Anchor pairwise spread
print(f"\n Anchor spread per patch:")
for p in range(min(8, P)):
sim = (curr[p] @ curr[p].T)
sim.fill_diagonal_(0)
print(f" P{p}: mean_cos={sim.mean():.4f} max={sim.max():.4f} min={sim.min():.4f}")
# Anchor effective dimensionality
print(f"\n Anchor effective dimensionality:")
for p in range(min(8, P)):
_, S, _ = torch.linalg.svd(curr[p].float(), full_matrices=False)
pr = S / S.sum()
ed = pr.pow(2).sum().reciprocal().item()
print(f" P{p}: eff_dim={ed:.1f} / {A}")
# Drift histogram β where do anchors cluster?
all_drifts = drift.flatten().cpu().numpy()
print(f"\n Drift distribution:")
bins = [0.0, 0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40]
hist, _ = np.histogram(all_drifts, bins=bins)
for i in range(len(bins)-1):
bar = "β" * hist[i]
print(f" {bins[i]:.2f}-{bins[i+1]:.2f}: {hist[i]:3d} {bar}")
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# TEST 2: SPHERE REPRESENTATION β CV OF BOTTLENECK EMBEDDINGS
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
print(f"\n{'β'*80}")
print("TEST 2: Sphere Representation β CV of bottleneck embeddings")
print(f" These live on S^15. Does CV approach 0.20?")
print(f"{'β'*80}")
# Hook to capture sphere embeddings
sphere_embeddings = {}
tri_profiles = {}
def hook_sphere(module, input, output):
# The forward method: proj_in β norm β reshape β normalize
# We need to grab AFTER L2 norm. Hook the full bottleneck
# and manually compute the sphere embedding.
pass
# Manually extract sphere embeddings at different timesteps
print(f"\n {'t':>6} {'CV_sphere':>10} {'CV_tri':>10} {'eff_d_sph':>10} "
f"{'eff_d_tri':>10} {'sph_norm':>10}")
for t_val in [0.0, 0.1, 0.25, 0.5, 0.75, 0.9, 1.0]:
B = images_test.shape[0]
t = torch.full((B,), t_val, device=DEVICE)
eps = torch.randn_like(images_test)
t_b = t.view(B, 1, 1, 1)
x_t = (1 - t_b) * images_test + t_b * eps
with torch.no_grad():
# Run encoder manually
cond = model.time_emb(t) + model.class_emb(labels_test)
h = model.in_conv(x_t)
skips = [h]
for i in range(len(model.channel_mults)):
for block in model.enc[i]:
if isinstance(block, nn.Sequential):
h = block[0](h); h = block[1](h, cond)
else:
h = block(h, cond)
skips.append(h)
if i < len(model.enc_down):
h = model.enc_down[i](h)
# Get sphere embedding
h_flat = h.reshape(B, -1)
emb = bn.proj_in(h_flat)
emb = bn.proj_in_norm(emb)
patches = emb.reshape(B, bn.n_patches, bn.patch_dim)
patches_n = F.normalize(patches, dim=-1)
# CV of sphere embeddings (flatten patches back to one vector)
sphere_flat = patches_n.reshape(B, -1) # (B, 256) on product of spheres
cv_sphere = compute_cv(sphere_flat, n_samples=1000)
ed_sphere = eff_dim(sphere_flat)
norm_sph = sphere_flat.norm(dim=-1).mean().item()
# Triangulation profile
tri = bn.triangulate(patches_n) # (B, 768)
cv_tri = compute_cv(tri, n_samples=1000)
ed_tri = eff_dim(tri)
# Per-patch CV
if t_val == 0.0:
print(f"\n Per-patch CV at t=0 (should be β0.20 if d=16):")
for p in range(min(8, bn.n_patches)):
patch_p = patches_n[:, p, :] # (B, 16) on S^15
cv_p = compute_cv(patch_p, n_samples=1000)
print(f" Patch {p}: CV={cv_p:.4f}")
print()
print(f" {t_val:>6.2f} {cv_sphere:>10.4f} {cv_tri:>10.4f} {ed_sphere:>10.1f} "
f"{ed_tri:>10.1f} {norm_sph:>10.4f}")
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# TEST 3: PER-CLASS ANCHOR ROUTING
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
print(f"\n{'β'*80}")
print("TEST 3: Per-Class Anchor Routing")
print(f"{'β'*80}")
# Collect per-class nearest anchors across all patches
class_nearest = {c: [] for c in range(10)}
anchors_n = F.normalize(bn.anchors.detach(), dim=-1)
for images_b, labels_b in test_loader:
images_b = images_b.to(DEVICE)
labels_b = labels_b.to(DEVICE)
B = images_b.shape[0]
t = torch.zeros(B, device=DEVICE) # clean images
with torch.no_grad():
cond = model.time_emb(t) + model.class_emb(labels_b)
h = model.in_conv(images_b)
for i in range(len(model.channel_mults)):
for block in model.enc[i]:
if isinstance(block, nn.Sequential):
h = block[0](h); h = block[1](h, cond)
else:
h = block(h, cond)
if i < len(model.enc_down):
h = model.enc_down[i](h)
h_flat = h.reshape(B, -1)
emb = bn.proj_in_norm(bn.proj_in(h_flat))
patches = F.normalize(emb.reshape(B, bn.n_patches, bn.patch_dim), dim=-1)
# Nearest anchor per patch
cos = torch.einsum('bpd,pad->bpa', patches, anchors_n) # (B, P, A)
nearest = cos.argmax(dim=-1) # (B, P)
for i in range(B):
c = labels_b[i].item()
class_nearest[c].append(nearest[i].cpu())
if sum(len(v) for v in class_nearest.values()) > 5000:
break
# Show routing for first 4 patches
for p_idx in range(min(4, bn.n_patches)):
print(f"\n Patch {p_idx} β nearest anchor per class:")
print(f" {'class':>10}", end="")
for a in range(A):
print(f" {a:>4}", end="")
print()
for c in range(10):
if not class_nearest[c]:
continue
nearest_all = torch.stack(class_nearest[c]) # (N, P)
nearest_p = nearest_all[:, p_idx]
counts = torch.bincount(nearest_p, minlength=A).float()
counts = counts / counts.sum()
row = f" {CLASS_NAMES[c]:>10}"
for a in range(A):
pct = counts[a].item()
if pct > 0.15:
row += f" {pct:>3.0%}β"
elif pct > 0.05:
row += f" {pct:>3.0%}β"
else:
row += f" {pct:>3.0%}"
#row += f" {pct:>3.0%}"
print(row)
# Are anchor patterns class-specific?
print(f"\n Anchor routing entropy per class (lower = more concentrated):")
for c in range(10):
if not class_nearest[c]:
continue
nearest_all = torch.stack(class_nearest[c])
# Average across patches
total_entropy = 0
for p_idx in range(bn.n_patches):
counts = torch.bincount(nearest_all[:, p_idx], minlength=A).float()
counts = counts / counts.sum()
entropy = -(counts * (counts + 1e-8).log()).sum().item()
total_entropy += entropy
avg_entropy = total_entropy / bn.n_patches
max_entropy = math.log(A)
print(f" {CLASS_NAMES[c]:>10}: H={avg_entropy:.3f} / {max_entropy:.3f} "
f"({avg_entropy/max_entropy:.1%} of max)")
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# TEST 4: SKIP GATE ANALYSIS
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
print(f"\n{'β'*80}")
print("TEST 4: Skip Gate β how much goes through constellation vs skip?")
print(f"{'β'*80}")
gate = bn.skip_gate.sigmoid().item()
print(f" Skip gate value: {gate:.4f}")
print(f" Skip path: {gate:.1%}")
print(f" Constellation path: {1-gate:.1%}")
print(f" Skip proj params: {sum(p.numel() for p in [bn.skip_proj.weight, bn.skip_proj.bias]):,}")
print(f" Patchwork params: {sum(p.numel() for p in bn.patchwork.parameters()):,}")
print(f"\n β skip_proj is Linear(16384, 16384) = "
f"{bn.skip_proj.weight.numel():,} params")
print(f" β This single layer is {bn.skip_proj.weight.numel()/1e6:.0f}M params β "
f"larger than the rest of the model combined")
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# TEST 5: GENERATION β PER CLASS
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
print(f"\n{'β'*80}")
print("TEST 5: Generation Quality")
print(f"{'β'*80}")
print(f" {'class':>10} {'intra_cos':>10} {'std':>8} {'CV':>8} {'norm':>8}")
all_gen = []
for c in range(10):
imgs, _ = sample(model, 64, 50, class_label=c)
imgs = (imgs + 1) / 2 # to [0,1]
all_gen.append(imgs)
flat = imgs.reshape(64, -1)
flat_n = F.normalize(flat, dim=-1)
sim = flat_n @ flat_n.T
mask = ~torch.eye(64, device=DEVICE, dtype=torch.bool)
intra = sim[mask].mean().item()
std = sim[mask].std().item()
cv = compute_cv(flat, 500)
norm = flat.norm(dim=-1).mean().item()
print(f" {CLASS_NAMES[c]:>10} {intra:>10.4f} {std:>8.4f} {cv:>8.4f} {norm:>8.2f}")
save_image(make_grid(imgs[:16], nrow=4), f"analysis_bn/class_{CLASS_NAMES[c]}.png")
# All classes grid
all_grid = torch.cat([g[:4] for g in all_gen])
save_image(make_grid(all_grid, nrow=10), "analysis_bn/all_classes.png")
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# TEST 6: ABLATION β SKIP ONLY vs CONSTELLATION ONLY
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
print(f"\n{'β'*80}")
print("TEST 6: Ablation β Skip-only vs Constellation-only")
print(f"{'β'*80}")
original_gate = bn.skip_gate.data.clone()
# A) Full model (as trained)
torch.manual_seed(999)
with torch.no_grad():
imgs_full, _ = sample(model, 32, 50, class_label=3)
# B) Skip only (gate β +100, sigmoid β 1.0)
bn.skip_gate.data.fill_(100.0)
torch.manual_seed(999)
with torch.no_grad():
imgs_skip, _ = sample(model, 32, 50, class_label=3)
# C) Constellation only (gate β -100, sigmoid β 0.0)
bn.skip_gate.data.fill_(-100.0)
torch.manual_seed(999)
with torch.no_grad():
imgs_const, _ = sample(model, 32, 50, class_label=3)
# Restore
bn.skip_gate.data.copy_(original_gate)
imgs_full_01 = (imgs_full + 1) / 2
imgs_skip_01 = (imgs_skip + 1) / 2
imgs_const_01 = (imgs_const + 1) / 2
# Compare
for name, imgs in [('skip_only', imgs_skip), ('const_only', imgs_const)]:
delta = (imgs_full - imgs).abs()
pixel_diff = delta.mean().item()
cos = F.cosine_similarity(
imgs_full.reshape(32, -1), imgs.reshape(32, -1)).mean().item()
print(f" {name:>15}: pixel_Ξ={pixel_diff:.6f} cos_sim={cos:.6f} "
f"max_Ξ={delta.max():.4f}")
# Save comparison: top=full, mid=skip_only, bot=constellation_only
comparison = torch.cat([imgs_full_01[:8], imgs_skip_01[:8], imgs_const_01[:8]])
save_image(make_grid(comparison, nrow=8), "analysis_bn/ablation_skip_vs_const.png")
print(f" β Saved (top=full, mid=skip_only, bot=constellation_only)")
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# TEST 7: VELOCITY FIELD
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
print(f"\n{'β'*80}")
print("TEST 7: Velocity Field Quality")
print(f"{'β'*80}")
print(f" {'t':>6} {'v_norm':>10} {'vΒ·target':>10} {'mse':>10}")
for t_val in [0.05, 0.1, 0.25, 0.5, 0.75, 0.9, 0.95]:
B = 128
imgs_v = images_test[:B]
labs_v = labels_test[:B]
t = torch.full((B,), t_val, device=DEVICE)
eps = torch.randn_like(imgs_v)
t_b = t.view(B, 1, 1, 1)
x_t = (1 - t_b) * imgs_v + t_b * eps
v_target = eps - imgs_v
with torch.no_grad():
v_pred = model(x_t, t, labs_v)
v_norm = v_pred.reshape(B, -1).norm(dim=-1).mean().item()
v_cos = F.cosine_similarity(
v_pred.reshape(B, -1), v_target.reshape(B, -1)).mean().item()
mse = F.mse_loss(v_pred, v_target).item()
print(f" {t_val:>6.2f} {v_norm:>10.2f} {v_cos:>10.4f} {mse:>10.4f}")
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# TEST 8: ODE TRAJECTORY β CV THROUGH GENERATION
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
print(f"\n{'β'*80}")
print("TEST 8: ODE Trajectory β geometry through generation")
print(f"{'β'*80}")
n_steps = 50
B_traj = 256
x = torch.randn(B_traj, 3, 32, 32, device=DEVICE)
labels_traj = torch.randint(0, 10, (B_traj,), device=DEVICE)
dt = 1.0 / n_steps
print(f" {'step':>6} {'t':>6} {'x_norm':>10} {'x_std':>10} {'CV':>8}")
for step in range(n_steps):
t_val = 1.0 - step * dt
t = torch.full((B_traj,), t_val, device=DEVICE)
with torch.no_grad(), torch.amp.autocast("cuda", dtype=torch.bfloat16):
v = model(x, t, labels_traj)
x = x - v.float() * dt
if step in [0, 1, 5, 10, 20, 30, 40, 49]:
xf = x.reshape(B_traj, -1)
print(f" {step:>6} {t_val:>6.2f} {xf.norm(dim=-1).mean().item():>10.2f} "
f"{x.std().item():>10.4f} {compute_cv(xf, 500):>8.4f}")
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# TEST 9: INTER vs INTRA CLASS
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
print(f"\n{'β'*80}")
print("TEST 9: Inter vs Intra Class Separation")
print(f"{'β'*80}")
intra_sims = []
inter_sims = []
for c in range(10):
flat = F.normalize(all_gen[c].reshape(64, -1), dim=-1)
sim = flat @ flat.T
mask = ~torch.eye(64, device=DEVICE, dtype=torch.bool)
intra_sims.append(sim[mask].mean().item())
for i in range(10):
for j in range(i+1, 10):
fi = F.normalize(all_gen[i].reshape(64, -1), dim=-1)
fj = F.normalize(all_gen[j].reshape(64, -1), dim=-1)
inter_sims.append((fi @ fj.T).mean().item())
print(f" Intra-class cos: {np.mean(intra_sims):.4f} Β± {np.std(intra_sims):.4f}")
print(f" Inter-class cos: {np.mean(inter_sims):.4f} Β± {np.std(inter_sims):.4f}")
ratio = np.mean(intra_sims) / (np.mean(inter_sims) + 1e-8)
print(f" Separation ratio: {ratio:.3f}Γ")
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# SUMMARY
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
print(f"\n{'='*80}")
print("ANALYSIS COMPLETE")
print(f"{'='*80}")
print(f"""
Files in analysis_bn/:
class_*.png per-class samples
all_classes.png 4 per class grid
ablation_skip_vs_const.png top=full, mid=skip, bot=constellation
Key questions answered:
1. Does per-patch CV β 0.20? (Test 2)
β If yes, the bottleneck lives at the natural S^15 dimension
2. Is anchor routing class-specific? (Test 3)
β If entropy varies by class, constellation routes differently
3. Does the skip path dominate? (Tests 4 & 6)
β If skip_only β full, the 268M skip_proj IS the model
4. Does constellation-only work at all? (Test 6)
β The real test of whether geometric encoding carries signal
""")
print("=" * 80) |