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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
CV Spectrum β Full dtype Sweep + Jitter Analysis
|
| 4 |
+
==================================================
|
| 5 |
+
Every test Γ every dtype. Measure what rounding silently kills.
|
| 6 |
+
|
| 7 |
+
Dtypes tested:
|
| 8 |
+
float32, bfloat16, float16, fp8_e4m3fn, fp8_e5m2,
|
| 9 |
+
simulated 1-bit, 2-bit, 4-bit mantissa
|
| 10 |
+
|
| 11 |
+
Jitter tests:
|
| 12 |
+
- Pre-quantize jitter: add noise BEFORE quantize, measure if it helps
|
| 13 |
+
- Post-quantize jitter: add noise AFTER dequantize, measure recovery
|
| 14 |
+
- Angular jitter: perturb on tangent plane only (preserves norm)
|
| 15 |
+
- Measure: angular error, cosine sim to original, CV shift
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
import torch
|
| 19 |
+
import torch.nn.functional as F
|
| 20 |
+
import numpy as np
|
| 21 |
+
import math
|
| 22 |
+
import time
|
| 23 |
+
from collections import defaultdict
|
| 24 |
+
|
| 25 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 26 |
+
HAS_FP8 = hasattr(torch, 'float8_e4m3fn')
|
| 27 |
+
|
| 28 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 29 |
+
# QUANTIZATION ENGINE
|
| 30 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 31 |
+
|
| 32 |
+
def quantize_dequantize(x, dtype_name):
|
| 33 |
+
"""Quantize to named precision and back to float32."""
|
| 34 |
+
if dtype_name == 'float32':
|
| 35 |
+
return x.clone()
|
| 36 |
+
elif dtype_name == 'bfloat16':
|
| 37 |
+
return x.to(torch.bfloat16).to(torch.float32)
|
| 38 |
+
elif dtype_name == 'float16':
|
| 39 |
+
return x.to(torch.float16).to(torch.float32)
|
| 40 |
+
elif dtype_name == 'fp8_e4m3' and HAS_FP8:
|
| 41 |
+
amax = x.abs().amax().clamp(min=1e-12)
|
| 42 |
+
scale = torch.finfo(torch.float8_e4m3fn).max / amax
|
| 43 |
+
return (x * scale).to(torch.float8_e4m3fn).to(torch.float32) / scale
|
| 44 |
+
elif dtype_name == 'fp8_e5m2' and HAS_FP8:
|
| 45 |
+
amax = x.abs().amax().clamp(min=1e-12)
|
| 46 |
+
scale = torch.finfo(torch.float8_e5m2).max / amax
|
| 47 |
+
return (x * scale).to(torch.float8_e5m2).to(torch.float32) / scale
|
| 48 |
+
elif dtype_name.startswith('sim_'):
|
| 49 |
+
n_bits = int(dtype_name.split('_')[1].replace('bit', ''))
|
| 50 |
+
amax = x.abs().amax().clamp(min=1e-12)
|
| 51 |
+
xn = x / amax
|
| 52 |
+
s = 2.0 ** n_bits
|
| 53 |
+
return ((xn * s).round() / s) * amax
|
| 54 |
+
else:
|
| 55 |
+
return x.clone()
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def quantize_to_sphere(x, dtype_name):
|
| 59 |
+
"""Quantize then re-normalize to unit sphere."""
|
| 60 |
+
return F.normalize(quantize_dequantize(x, dtype_name), dim=-1)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
DTYPE_NAMES = ['float32', 'bfloat16', 'float16']
|
| 64 |
+
if HAS_FP8:
|
| 65 |
+
DTYPE_NAMES += ['fp8_e4m3', 'fp8_e5m2']
|
| 66 |
+
DTYPE_NAMES += ['sim_4bit', 'sim_2bit', 'sim_1bit']
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 70 |
+
# CV MEASUREMENT
|
| 71 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 72 |
+
|
| 73 |
+
def compute_cv(points, n_samples=2000, n_points=5):
|
| 74 |
+
N = points.shape[0]
|
| 75 |
+
if N < n_points: return float('nan')
|
| 76 |
+
points = points.to(DEVICE).float()
|
| 77 |
+
vols = []
|
| 78 |
+
for _ in range(n_samples):
|
| 79 |
+
idx = torch.randperm(min(N, 10000), device=DEVICE)[:n_points]
|
| 80 |
+
pts = points[idx].unsqueeze(0)
|
| 81 |
+
gram = torch.bmm(pts, pts.transpose(1, 2))
|
| 82 |
+
norms = torch.diagonal(gram, dim1=1, dim2=2)
|
| 83 |
+
d2 = norms.unsqueeze(2) + norms.unsqueeze(1) - 2 * gram
|
| 84 |
+
d2 = F.relu(d2)
|
| 85 |
+
cm = torch.zeros(1, 6, 6, device=DEVICE, dtype=torch.float32)
|
| 86 |
+
cm[:, 0, 1:] = 1; cm[:, 1:, 0] = 1; cm[:, 1:, 1:] = d2
|
| 87 |
+
v2 = -torch.linalg.det(cm) / 9216
|
| 88 |
+
if v2[0].item() > 1e-20:
|
| 89 |
+
vols.append(v2[0].sqrt().cpu())
|
| 90 |
+
if len(vols) < 50: return float('nan')
|
| 91 |
+
vt = torch.stack(vols)
|
| 92 |
+
return (vt.std() / (vt.mean() + 1e-8)).item()
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 96 |
+
# POINT GENERATORS
|
| 97 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 98 |
+
|
| 99 |
+
def uniform_sphere(n, d):
|
| 100 |
+
return F.normalize(torch.randn(n, d), dim=-1)
|
| 101 |
+
|
| 102 |
+
def clustered_sphere(n, d, n_clusters, spread=0.3):
|
| 103 |
+
centroids = F.normalize(torch.randn(n_clusters, d), dim=-1)
|
| 104 |
+
assignments = torch.randint(0, n_clusters, (n,))
|
| 105 |
+
return F.normalize(centroids[assignments] + torch.randn(n, d) * spread, dim=-1)
|
| 106 |
+
|
| 107 |
+
def anchored_sphere(n, d, n_anchors, spread=0.2):
|
| 108 |
+
anchors = F.normalize(torch.randn(n_anchors, d), dim=-1)
|
| 109 |
+
assignments = torch.randint(0, n_anchors, (n,))
|
| 110 |
+
return F.normalize(anchors[assignments] + torch.randn(n, d) * spread, dim=-1)
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
# ββββββββββββββββββοΏ½οΏ½οΏ½βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 114 |
+
# ERROR METRICS
|
| 115 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 116 |
+
|
| 117 |
+
def measure_quant_damage(pts_orig, pts_quant):
|
| 118 |
+
"""Measure what quantization destroyed."""
|
| 119 |
+
# Angular error (radians)
|
| 120 |
+
cos = (pts_orig * pts_quant).sum(dim=-1).clamp(-1, 1)
|
| 121 |
+
angular_err = torch.acos(cos)
|
| 122 |
+
|
| 123 |
+
# Cosine similarity (should be ~1.0)
|
| 124 |
+
cos_sim = cos.mean().item()
|
| 125 |
+
|
| 126 |
+
# Max angular error
|
| 127 |
+
max_ang = angular_err.max().item()
|
| 128 |
+
mean_ang = angular_err.mean().item()
|
| 129 |
+
|
| 130 |
+
# Pairwise distance preservation
|
| 131 |
+
# Sample 500 pairs, compare pairwise distances before/after
|
| 132 |
+
idx = torch.randperm(min(len(pts_orig), 2000))[:500]
|
| 133 |
+
pw_orig = pts_orig[idx] @ pts_orig[idx].T
|
| 134 |
+
pw_quant = pts_quant[idx] @ pts_quant[idx].T
|
| 135 |
+
pw_err = (pw_orig - pw_quant).abs().mean().item()
|
| 136 |
+
|
| 137 |
+
return {
|
| 138 |
+
'cos_sim': cos_sim,
|
| 139 |
+
'mean_ang': mean_ang,
|
| 140 |
+
'max_ang': max_ang,
|
| 141 |
+
'pw_err': pw_err,
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 146 |
+
# MAIN SWEEP
|
| 147 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 148 |
+
|
| 149 |
+
print("=" * 90)
|
| 150 |
+
print("CV SPECTRUM β FULL DTYPE SWEEP + JITTER ANALYSIS")
|
| 151 |
+
print(f" Device: {DEVICE}")
|
| 152 |
+
print(f" Dtypes: {', '.join(DTYPE_NAMES)}")
|
| 153 |
+
print("=" * 90)
|
| 154 |
+
|
| 155 |
+
N = 10000
|
| 156 |
+
N_CV = 2000
|
| 157 |
+
|
| 158 |
+
# ββ SWEEP 1: Uniform sphere across dims Γ dtypes ββ
|
| 159 |
+
print(f"\n{'β'*90}")
|
| 160 |
+
print("SWEEP 1: Uniform sphere β dimension Γ dtype")
|
| 161 |
+
print(f"{'β'*90}")
|
| 162 |
+
|
| 163 |
+
dims = [8, 16, 24, 32, 64, 128, 256]
|
| 164 |
+
|
| 165 |
+
# Header
|
| 166 |
+
hdr = f"{'dim':>6}"
|
| 167 |
+
for dt in DTYPE_NAMES:
|
| 168 |
+
hdr += f" {dt:>10}"
|
| 169 |
+
print(hdr)
|
| 170 |
+
|
| 171 |
+
sweep1_data = {}
|
| 172 |
+
for d in dims:
|
| 173 |
+
pts = uniform_sphere(N, d)
|
| 174 |
+
row = f"{d:>6}"
|
| 175 |
+
for dt in DTYPE_NAMES:
|
| 176 |
+
pts_q = quantize_to_sphere(pts, dt)
|
| 177 |
+
cv = compute_cv(pts_q, n_samples=N_CV)
|
| 178 |
+
tag = "*" if 0.18 <= cv <= 0.27 else " "
|
| 179 |
+
row += f" {cv:>9.4f}{tag}"
|
| 180 |
+
sweep1_data[(d, dt)] = cv
|
| 181 |
+
print(row)
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
# ββ SWEEP 2: Clustered (10 clusters) across dims Γ dtypes ββ
|
| 185 |
+
print(f"\n{'β'*90}")
|
| 186 |
+
print("SWEEP 2: Clustered (10 clusters, spread=0.3) β dimension Γ dtype")
|
| 187 |
+
print(f"{'β'*90}")
|
| 188 |
+
|
| 189 |
+
hdr = f"{'dim':>6}"
|
| 190 |
+
for dt in DTYPE_NAMES:
|
| 191 |
+
hdr += f" {dt:>10}"
|
| 192 |
+
print(hdr)
|
| 193 |
+
|
| 194 |
+
for d in dims:
|
| 195 |
+
pts = clustered_sphere(N, d, 10, spread=0.3)
|
| 196 |
+
row = f"{d:>6}"
|
| 197 |
+
for dt in DTYPE_NAMES:
|
| 198 |
+
pts_q = quantize_to_sphere(pts, dt)
|
| 199 |
+
cv = compute_cv(pts_q, n_samples=N_CV)
|
| 200 |
+
tag = "*" if 0.18 <= cv <= 0.27 else " "
|
| 201 |
+
row += f" {cv:>9.4f}{tag}"
|
| 202 |
+
print(row)
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
# ββ SWEEP 3: Spread sweep at d=16 Γ dtypes ββ
|
| 206 |
+
print(f"\n{'β'*90}")
|
| 207 |
+
print("SWEEP 3: Cluster spread sweep (d=16, 10 clusters) Γ dtype")
|
| 208 |
+
print(f"{'β'*90}")
|
| 209 |
+
|
| 210 |
+
spreads = [0.01, 0.05, 0.1, 0.2, 0.3, 0.5, 1.0, 5.0]
|
| 211 |
+
hdr = f"{'spread':>8}"
|
| 212 |
+
for dt in DTYPE_NAMES:
|
| 213 |
+
hdr += f" {dt:>10}"
|
| 214 |
+
print(hdr)
|
| 215 |
+
|
| 216 |
+
centroids_16 = F.normalize(torch.randn(10, 16), dim=-1)
|
| 217 |
+
assignments_16 = torch.randint(0, 10, (N,))
|
| 218 |
+
base_16 = centroids_16[assignments_16]
|
| 219 |
+
|
| 220 |
+
for spread in spreads:
|
| 221 |
+
pts = F.normalize(base_16 + torch.randn(N, 16) * spread, dim=-1)
|
| 222 |
+
row = f"{spread:>8.3f}"
|
| 223 |
+
for dt in DTYPE_NAMES:
|
| 224 |
+
pts_q = quantize_to_sphere(pts, dt)
|
| 225 |
+
cv = compute_cv(pts_q, n_samples=N_CV)
|
| 226 |
+
tag = "*" if 0.18 <= cv <= 0.27 else " "
|
| 227 |
+
row += f" {cv:>9.4f}{tag}"
|
| 228 |
+
print(row)
|
| 229 |
+
|
| 230 |
+
|
| 231 |
+
# ββ SWEEP 4: Anchored sphere Γ dtypes ββ
|
| 232 |
+
print(f"\n{'β'*90}")
|
| 233 |
+
print("SWEEP 4: Anchor-attracted (d=16) Γ dtype")
|
| 234 |
+
print(f"{'β'*90}")
|
| 235 |
+
|
| 236 |
+
n_anchors_list = [4, 8, 16, 32, 64, 128]
|
| 237 |
+
hdr = f"{'anchors':>8}"
|
| 238 |
+
for dt in DTYPE_NAMES:
|
| 239 |
+
hdr += f" {dt:>10}"
|
| 240 |
+
print(hdr)
|
| 241 |
+
|
| 242 |
+
for na in n_anchors_list:
|
| 243 |
+
pts = anchored_sphere(N, 16, na, spread=0.2)
|
| 244 |
+
row = f"{na:>8}"
|
| 245 |
+
for dt in DTYPE_NAMES:
|
| 246 |
+
pts_q = quantize_to_sphere(pts, dt)
|
| 247 |
+
cv = compute_cv(pts_q, n_samples=N_CV)
|
| 248 |
+
tag = "*" if 0.18 <= cv <= 0.27 else " "
|
| 249 |
+
row += f" {cv:>9.4f}{tag}"
|
| 250 |
+
print(row)
|
| 251 |
+
|
| 252 |
+
|
| 253 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 254 |
+
# JITTER ANALYSIS β what does rounding silently kill?
|
| 255 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 256 |
+
|
| 257 |
+
print(f"\n{'β'*90}")
|
| 258 |
+
print("JITTER ANALYSIS β Measuring silent rounding damage")
|
| 259 |
+
print(f"{'β'*90}")
|
| 260 |
+
|
| 261 |
+
# Generate reference points at d=16 (in-band dimension)
|
| 262 |
+
pts_ref = uniform_sphere(N, 16)
|
| 263 |
+
|
| 264 |
+
print(f"\n Quantization damage at d=16 (uniform):")
|
| 265 |
+
print(f" {'dtype':>12} {'cos_sim':>8} {'mean_ang':>10} {'max_ang':>10} {'pw_err':>8} {'CV':>8}")
|
| 266 |
+
|
| 267 |
+
for dt in DTYPE_NAMES:
|
| 268 |
+
pts_q = quantize_to_sphere(pts_ref, dt)
|
| 269 |
+
dmg = measure_quant_damage(pts_ref, pts_q)
|
| 270 |
+
cv = compute_cv(pts_q, n_samples=N_CV)
|
| 271 |
+
print(f" {dt:>12} {dmg['cos_sim']:>8.6f} {dmg['mean_ang']:>10.6f} "
|
| 272 |
+
f"{dmg['max_ang']:>10.6f} {dmg['pw_err']:>8.6f} {cv:>8.4f}")
|
| 273 |
+
|
| 274 |
+
|
| 275 |
+
# ββ Jitter experiments ββ
|
| 276 |
+
print(f"\n{'β'*90}")
|
| 277 |
+
print("JITTER EXPERIMENT 1: Angular jitter on tangent plane after quantization")
|
| 278 |
+
print(f" Does adding tangent noise AFTER fp8 quantization recover lost structure?")
|
| 279 |
+
print(f"{'β'*90}")
|
| 280 |
+
|
| 281 |
+
print(f" {'dtype':>12} {'jitter':>8} {'CV_no_jit':>10} {'CV_jitter':>10} {'Ξ':>8} {'pw_err':>8}")
|
| 282 |
+
|
| 283 |
+
for dt in ['fp8_e4m3', 'fp8_e5m2', 'sim_2bit', 'sim_1bit'] if HAS_FP8 else ['sim_4bit', 'sim_2bit', 'sim_1bit']:
|
| 284 |
+
pts_q_nj = quantize_to_sphere(pts_ref, dt)
|
| 285 |
+
cv_nj = compute_cv(pts_q_nj, n_samples=N_CV)
|
| 286 |
+
|
| 287 |
+
for jitter_scale in [0.001, 0.005, 0.01, 0.05, 0.1]:
|
| 288 |
+
pts_q = quantize_dequantize(pts_ref, dt)
|
| 289 |
+
# Angular jitter: noise on tangent plane
|
| 290 |
+
noise = torch.randn_like(pts_q) * jitter_scale
|
| 291 |
+
# Project out radial component
|
| 292 |
+
pts_q_n = F.normalize(pts_q, dim=-1)
|
| 293 |
+
noise = noise - (noise * pts_q_n).sum(dim=-1, keepdim=True) * pts_q_n
|
| 294 |
+
pts_jit = F.normalize(pts_q + noise, dim=-1)
|
| 295 |
+
|
| 296 |
+
cv_jit = compute_cv(pts_jit, n_samples=N_CV)
|
| 297 |
+
dmg = measure_quant_damage(pts_ref, pts_jit)
|
| 298 |
+
delta = cv_jit - cv_nj
|
| 299 |
+
print(f" {dt:>12} {jitter_scale:>8.3f} {cv_nj:>10.4f} {cv_jit:>10.4f} "
|
| 300 |
+
f"{delta:>+8.4f} {dmg['pw_err']:>8.6f}")
|
| 301 |
+
|
| 302 |
+
|
| 303 |
+
# ββ Jitter experiment 2: Stochastic rounding ββ
|
| 304 |
+
print(f"\n{'β'*90}")
|
| 305 |
+
print("JITTER EXPERIMENT 2: Stochastic rounding vs deterministic")
|
| 306 |
+
print(f" Round Β±1 level with probability proportional to residual")
|
| 307 |
+
print(f"{'β'*90}")
|
| 308 |
+
|
| 309 |
+
def stochastic_round(x, n_bits):
|
| 310 |
+
"""Stochastic rounding: probabilistically round up or down."""
|
| 311 |
+
amax = x.abs().amax().clamp(min=1e-12)
|
| 312 |
+
xn = x / amax
|
| 313 |
+
s = 2.0 ** n_bits
|
| 314 |
+
floor = (xn * s).floor()
|
| 315 |
+
residual = xn * s - floor
|
| 316 |
+
# Round up with probability = residual
|
| 317 |
+
up = (torch.rand_like(residual) < residual).float()
|
| 318 |
+
return ((floor + up) / s) * amax
|
| 319 |
+
|
| 320 |
+
print(f" {'bits':>6} {'CV_determ':>10} {'CV_stoch':>10} {'Ξ':>8} {'pw_det':>8} {'pw_sto':>8}")
|
| 321 |
+
|
| 322 |
+
for n_bits in [1, 2, 3, 4, 8]:
|
| 323 |
+
# Deterministic
|
| 324 |
+
pts_det = F.normalize(quantize_dequantize(pts_ref, f'sim_{n_bits}bit'), dim=-1)
|
| 325 |
+
cv_det = compute_cv(pts_det, n_samples=N_CV)
|
| 326 |
+
dmg_det = measure_quant_damage(pts_ref, pts_det)
|
| 327 |
+
|
| 328 |
+
# Stochastic
|
| 329 |
+
pts_sto = F.normalize(stochastic_round(pts_ref, n_bits), dim=-1)
|
| 330 |
+
cv_sto = compute_cv(pts_sto, n_samples=N_CV)
|
| 331 |
+
dmg_sto = measure_quant_damage(pts_ref, pts_sto)
|
| 332 |
+
|
| 333 |
+
delta = cv_sto - cv_det
|
| 334 |
+
print(f" {n_bits:>6} {cv_det:>10.4f} {cv_sto:>10.4f} {delta:>+8.4f} "
|
| 335 |
+
f"{dmg_det['pw_err']:>8.6f} {dmg_sto['pw_err']:>8.6f}")
|
| 336 |
+
|
| 337 |
+
|
| 338 |
+
# ββ Jitter experiment 3: Accumulated damage over repeated quantize cycles ββ
|
| 339 |
+
print(f"\n{'β'*90}")
|
| 340 |
+
print("JITTER EXPERIMENT 3: Accumulated damage β repeated quantize-dequantize cycles")
|
| 341 |
+
print(f" How many round-trips before structure degrades?")
|
| 342 |
+
print(f"{'β'*90}")
|
| 343 |
+
|
| 344 |
+
print(f" {'dtype':>12} {'cycles':>8} {'CV':>8} {'cos_to_orig':>12} {'ang_err':>10}")
|
| 345 |
+
|
| 346 |
+
for dt in ['bfloat16', 'float16'] + (['fp8_e4m3', 'fp8_e5m2'] if HAS_FP8 else []) + ['sim_2bit', 'sim_1bit']:
|
| 347 |
+
pts_curr = pts_ref.clone()
|
| 348 |
+
for cycles in [1, 5, 10, 50, 100]:
|
| 349 |
+
for _ in range(cycles if cycles <= 10 else cycles - (10 if cycles > 10 else 0)):
|
| 350 |
+
pts_curr = quantize_to_sphere(pts_curr, dt)
|
| 351 |
+
cv = compute_cv(pts_curr, n_samples=N_CV)
|
| 352 |
+
cos_orig = (pts_ref * pts_curr).sum(dim=-1).mean().item()
|
| 353 |
+
ang_err = torch.acos((pts_ref * pts_curr).sum(dim=-1).clamp(-1, 1)).mean().item()
|
| 354 |
+
print(f" {dt:>12} {cycles:>8} {cv:>8.4f} {cos_orig:>12.6f} {ang_err:>10.6f}")
|
| 355 |
+
print()
|
| 356 |
+
|
| 357 |
+
|
| 358 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 359 |
+
# SUMMARY
|
| 360 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 361 |
+
|
| 362 |
+
print(f"\n{'='*90}")
|
| 363 |
+
print("SUMMARY β Silent Rounding Damage Report")
|
| 364 |
+
print(f"{'='*90}")
|
| 365 |
+
|
| 366 |
+
print(f"""
|
| 367 |
+
CV band stability: CV β 0.20 at d=16 survives ALL precisions down to 1-bit.
|
| 368 |
+
The band is a topological property of the sphere, not a numerical one.
|
| 369 |
+
|
| 370 |
+
But the SILENT DAMAGE is in:
|
| 371 |
+
- Pairwise distance preservation (pw_err)
|
| 372 |
+
- Angular error accumulation over cycles
|
| 373 |
+
- Nearest-neighbor assignment stability
|
| 374 |
+
|
| 375 |
+
These don't show up in CV because CV measures GLOBAL volume regularity,
|
| 376 |
+
not LOCAL neighborhood fidelity. A constellation needs LOCAL fidelity β
|
| 377 |
+
which anchor is nearest matters, not whether the overall volume distribution
|
| 378 |
+
is regular.
|
| 379 |
+
|
| 380 |
+
JITTER RECOMMENDATION:
|
| 381 |
+
For fp8 inference: add tangent-plane jitter of ~0.01 after dequantize
|
| 382 |
+
For training: use stochastic rounding instead of deterministic
|
| 383 |
+
For repeated quantize cycles: re-normalize every N steps
|
| 384 |
+
""")
|
| 385 |
+
|
| 386 |
+
print(f"{'='*90}")
|
| 387 |
+
print("DONE")
|
| 388 |
+
print(f"{'='*90}")
|