Skip to content

Commit 3a7815d

Browse files
committed
refactor(systems): remove dead code and tighten microturboquant helpers
- Remove unused reconstruction_mse function (never called from main) - Collapse orthogonality_error from 3-level accumulator loop to max-of-generator - Replace double-lookup bigram pattern (in + []) with single bigram_idx.get() call - Fix misleading docstring on gaussian_sample (claimed Box-Muller; just wraps random.gauss) - Fix stale comment in random_rotation (referenced a variable that was renamed) - Inline transient diff variable in inner_product_mse accumulator Observed outputs unchanged: synthetic 4-bit ratio 1.61x, 8-bit 1.83x, QJL mean signed error +0.0085. Net -15 LOC (457 to 442). verify.py still passes.
1 parent d191732 commit 3a7815d

1 file changed

Lines changed: 15 additions & 30 deletions

File tree

03-systems/microturboquant.py

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ def inner(a: list[float], b: list[float]) -> float:
9999

100100

101101
def gaussian_sample() -> float:
102-
"""Standard normal via Box-Muller. random.gauss(0, 1) also works but explicit
103-
here to keep the only-stdlib dependency surface visible."""
102+
"""Return one draw from the standard normal N(0, 1). Named wrapper so callers
103+
read as 'gaussian_sample()' rather than 'random.gauss(0.0, 1.0)'."""
104104
return random.gauss(0.0, 1.0)
105105

106106

@@ -119,8 +119,8 @@ def random_rotation(dim: int) -> list[list[float]]:
119119
columns[k] = columns[k] / ||columns[k]||
120120
where g_k is the k-th standard-Gaussian column before orthonormalization.
121121
"""
122-
# Generate random Gaussian columns. We store them as rows of `columns_as_rows`
123-
# (easier indexing in Python) and transpose at the end.
122+
# Generate a D-by-D matrix of i.i.d. Gaussians. Stored row-major; we extract
123+
# columns on demand via `raw[i][k]` for k in the Gram-Schmidt loop below.
124124
raw = [[gaussian_sample() for _ in range(dim)] for _ in range(dim)]
125125

126126
# Gram-Schmidt in place, column by column.
@@ -146,13 +146,11 @@ def orthogonality_error(R: list[list[float]]) -> float:
146146
"""Return max |R^T R - I|_ij. Should be below ~1e-10 for any correct rotation."""
147147
dim = len(R)
148148
Rt = transpose(R)
149-
worst = 0.0
150-
for i in range(dim):
151-
for j in range(dim):
152-
dot = sum(Rt[i][k] * R[k][j] for k in range(dim))
153-
expected = 1.0 if i == j else 0.0
154-
worst = max(worst, abs(dot - expected))
155-
return worst
149+
return max(
150+
abs(sum(Rt[i][k] * R[k][j] for k in range(dim)) - (1.0 if i == j else 0.0))
151+
for i in range(dim)
152+
for j in range(dim)
153+
)
156154

157155

158156
# === SCALAR QUANTIZERS ===
@@ -278,10 +276,10 @@ def sample_name_embeddings(names: list[str], count: int, dim: int) -> list[list[
278276
sparse_counts: dict[int, float] = {}
279277
padded = "." + name + "."
280278
for a, b in zip(padded, padded[1:]):
281-
bigram = a + b
282-
if bigram in bigram_idx:
283-
idx = bigram_idx[bigram]
284-
sparse_counts[idx] = sparse_counts.get(idx, 0.0) + 1.0
279+
idx = bigram_idx.get(a + b)
280+
if idx is None:
281+
continue
282+
sparse_counts[idx] = sparse_counts.get(idx, 0.0) + 1.0
285283
if not sparse_counts:
286284
continue
287285
# Sparse matvec: only sum over non-zero entries of sparse_counts.
@@ -330,24 +328,11 @@ def inner_product_mse(
330328
for _ in range(pair_count):
331329
i = random.randrange(n)
332330
j = random.randrange(n)
333-
true_ip = inner(originals[i], originals[j])
334-
approx_ip = inner(approximations[i], approximations[j])
335-
diff = true_ip - approx_ip
336-
total += diff * diff
331+
err = inner(originals[i], originals[j]) - inner(approximations[i], approximations[j])
332+
total += err * err
337333
return total / pair_count
338334

339335

340-
def reconstruction_mse(originals: list[list[float]], approximations: list[list[float]]) -> float:
341-
"""Average L2 reconstruction error per coordinate. Secondary metric."""
342-
total = 0.0
343-
coord_count = 0
344-
for x, x_hat in zip(originals, approximations):
345-
for a, b in zip(x, x_hat):
346-
total += (a - b) ** 2
347-
coord_count += 1
348-
return total / coord_count if coord_count else 0.0
349-
350-
351336
# === RATE-DISTORTION BENCHMARK ===
352337

353338
def encode_all(

0 commit comments

Comments
 (0)