Download tests/test_codecs.py from kiruluta/SPECTRA-K3-HF-Scaling-Benchmark: direct link, hf CLI and curl.
- Browser
- Download file 3.54 kB
-
https://huggingface.co/kiruluta/SPECTRA-K3-HF-Scaling-Benchmark/resolve/main/tests/test_codecs.py
- Command line
-
hf download hf://kiruluta/SPECTRA-K3-HF-Scaling-Benchmark/tests/test_codecs.py
-
curl -L -o test_codecs.py https://huggingface.co/kiruluta/SPECTRA-K3-HF-Scaling-Benchmark/resolve/main/tests/test_codecs.py
3.54 kB
| import numpy as np | |
| import pytest | |
| from spectra_moe.codecs.lattice import ( | |
| E8Codebook, _e8_shell_points, dequantize_e8_codebook, e8_normalized_second_moment, | |
| get_codebook, nearest_e8, quantize_e8_codebook, | |
| ) | |
| from spectra_moe.codecs.rate import compare_at_rate, empirical_entropy, relative_error | |
| from spectra_moe.codecs.rvq import dequantize_rvq, kmeans, quantize_rvq, train_rvq | |
| from spectra_moe.codecs.scalar import ( | |
| apply_deadzone, dequantize_mxfp4, dequantize_scalar, quantize_mxfp4, quantize_scalar, | |
| ) | |
| def test_e8_theta_series(): | |
| """Shell counts must match the known E8 theta series 1, 240, 2160, 6720.""" | |
| assert len(_e8_shell_points(0)) == 1 | |
| assert len(_e8_shell_points(2)) == 241 | |
| assert len(_e8_shell_points(4)) == 2401 | |
| assert len(_e8_shell_points(6)) == 9121 | |
| def test_nearest_e8_returns_lattice_points(): | |
| P = nearest_e8(np.random.default_rng(0).normal(size=(200, 8)) * 2) | |
| frac = (P * 2) % 2 | |
| in_d8 = (np.abs(P.sum(axis=1) % 2) < 1e-9) & (np.abs(frac).sum(axis=1) < 1e-9) | |
| in_coset = np.all(np.abs(frac - 1) < 1e-9, axis=1) | |
| assert np.all(in_d8 | in_coset) | |
| def test_e8_second_moment_matches_theory(): | |
| """G(E8) ~ 0.0716821; beating Z^n's 1/12 is the whole point of using it.""" | |
| g = e8_normalized_second_moment(3000, seed=1) | |
| assert abs(g - 0.0716821) < 5e-3 | |
| assert g < 1 / 12 | |
| def test_e8_codebook_rate_is_exact(): | |
| cb = E8Codebook(8) | |
| assert cb.size == 256 | |
| assert cb.bits_per_weight == 1.0 | |
| def test_e8_quantization_roundtrip_shape(): | |
| x = np.random.default_rng(2).normal(size=(32, 40)) | |
| q = quantize_e8_codebook(x, index_bits=8, codebook=get_codebook(8)) | |
| assert dequantize_e8_codebook(q).shape == x.shape | |
| def test_e8_beats_scalar_on_gaussian_at_matched_rate(): | |
| """The space-filling gain, measured at equal *measured* bits/weight.""" | |
| x = np.random.default_rng(3).normal(size=(96, 96)) | |
| r = compare_at_rate(x, 1.5, scale_bits=8.0) | |
| assert r["e8"]["measured_bits"] <= r["scalar"]["measured_bits"] + 1e-6 | |
| assert r["e8"]["rel_error"] < r["scalar"]["rel_error"] | |
| def test_scalar_error_decreases_with_rate(bits): | |
| x = np.random.default_rng(4).normal(size=(64, 64)) | |
| lo = relative_error(x, dequantize_scalar(quantize_scalar(x, bits, 64))) | |
| hi = relative_error(x, dequantize_scalar(quantize_scalar(x, bits + 2, 64))) | |
| assert hi < lo | |
| def test_mxfp4_roundtrip_is_reasonable(): | |
| x = np.random.default_rng(5).normal(size=(64, 64)) | |
| assert relative_error(x, dequantize_mxfp4(quantize_mxfp4(x))) < 0.35 | |
| def test_deadzone_keeps_requested_density(): | |
| x = np.random.default_rng(6).normal(size=(50, 50)) | |
| out, density = apply_deadzone(x, 0.1) | |
| assert abs(density - 0.1) < 0.01 | |
| def test_kmeans_recovers_separated_clusters(): | |
| rng = np.random.default_rng(7) | |
| X = np.vstack([rng.normal(m, 0.1, size=(60, 2)) for m in (-5, 0, 5)]) | |
| _, labels = kmeans(X, 3, seed=0) | |
| assert len(set(labels[:60])) == 1 and len(set(labels[60:120])) == 1 | |
| def test_rvq_improves_with_more_books(): | |
| rng = np.random.default_rng(8) | |
| train = rng.normal(size=(600, 8)) | |
| x = rng.normal(size=(40, 40)) | |
| e1 = relative_error(x, dequantize_rvq(quantize_rvq(x, train_rvq(train, 1, 6.0, iters=8)))) | |
| e2 = relative_error(x, dequantize_rvq(quantize_rvq(x, train_rvq(train, 3, 6.0, iters=8)))) | |
| assert e2 < e1 | |
| def test_empirical_entropy_bounds(): | |
| assert empirical_entropy(np.zeros(100)) == 0.0 | |
| assert abs(empirical_entropy(np.arange(256)) - 8.0) < 1e-9 | |