bdrgon's picture
Validate Gemma exports on full drum corpus and publish results
448ab1d
Raw History Blame Contribute Delete
2.74 kB
import unittest
import tempfile
from pathlib import Path
import numpy as np
from validation import bucket_key, decode_audio, divergence, nearest_indices, neighbor_agreement, passes, summary
class Validation(unittest.TestCase):
def test_cosine_is_scale_invariant_but_absolute_error_is_not(self):
a = np.array([1., 2.], dtype=np.float32)
result = divergence(a, 2 * a)
self.assertAlmostEqual(result["cosine"], 1.)
self.assertEqual(result["max_abs"], 2.)
self.assertFalse(passes(result, "upstream -> fp32"))
def test_bad_outputs_cannot_pass(self):
for bad in (np.array([np.nan, 1.]), np.zeros(2), np.ones(3)):
with self.assertRaises(ValueError):
divergence(np.ones(2), bad)
self.assertIsNone(summary([])["max"])
def test_neighbors_exclude_self_and_break_exact_ties_in_row_order(self):
values = np.array([[1., 0.], [1., 0.], [0., 1.]], dtype=np.float32)
np.testing.assert_array_equal(nearest_indices(values, 2), [[1, 2], [0, 2], [0, 1]])
def test_tower_buckets_keep_shape_and_mask(self):
key = bucket_key([1, 2, 1536], [True, False])
self.assertNotEqual(key, bucket_key([1, 2, 1536], [True, True]))
self.assertNotEqual(key, bucket_key([1, 3, 1536], [True, False]))
def test_empty_dataset_audio_is_explicit_error(self):
with self.assertRaisesRegex(ValueError, "zero audio bytes"):
decode_audio({"audio": {"bytes": b""}})
def test_neighbor_coverage_counts_singletons_and_small_buckets(self):
scratch = Path(__file__).resolve().parents[1] / ".validation-cache"
scratch.mkdir(exist_ok=True)
with tempfile.TemporaryDirectory(dir=scratch) as directory:
root = Path(directory)
values = np.array([1., 0., .8, .2, 1., 0., 0.], dtype="<f4")
for variant in ("fp32", "int8", "int4"):
values.tofile(root / f"{variant}.f32")
records = [
{"status": "ok", "shape": [1, 2], "mask": [], "offset": 0, "count": 2},
{"status": "ok", "shape": [1, 2], "mask": [], "offset": 2, "count": 2},
{"status": "ok", "shape": [1, 3], "mask": [], "offset": 4, "count": 3},
{"status": "input_error"},
]
result = neighbor_agreement(records, root)
for row in result.values():
self.assertEqual(row["eligible"], 2)
self.assertEqual(row["singleton_excluded"], 1)
self.assertEqual(row["top1_agreement"], 1.)
self.assertEqual(row["top10_eligible"], 0)
self.assertIsNone(row["mean_overlap_at_10"])
if __name__ == "__main__":
unittest.main()