Wiself/Voice
52.6k
1"""Live tests: cheap real-HF reads. Needs VOICE_TEST_LIVE=1. No big downloads here."""2import json3import struct4import sys5import tempfile6import unittest7from pathlib import Path8 9sys.path.insert(0, str(Path(__file__).resolve().parent))10from _helpers import ABL_FILE, ABL_REPO, BASE_FILE, BASE_REPO, CATGIRL_REPO # noqa: E40211from _helpers import needs_live, voice # noqa: E40212 13import numpy as np14 15 16def shard_url(repo, filename):17 return f"https://huggingface.co/{repo}/resolve/main/{filename}"18 19 20@needs_live21class TestHeadResolve(unittest.TestCase):22 def test_catgirl_head_resolves(self):23 idx = voice._hf_model_index(CATGIRL_REPO)24 self.assertIsNotNone(idx)25 names = list((idx.get("weight_map") or {}).keys())26 self.assertGreater(len(names), 100)27 found = voice.detect_output_tensor(names, None)28 self.assertIsNotNone(found)29 self.assertTrue(found.endswith(("embed_tokens.weight", "lm_head.weight",30 "output.weight")), found)31 32 def test_catgirl_tied_config(self):33 cfg = voice._hf_config(CATGIRL_REPO)34 self.assertIsNotNone(cfg)35 self.assertTrue(cfg.get("tie_word_embeddings"))36 37 def test_abliterated_gguf_head_resolves(self):38 src = voice.open_source(shard_url(ABL_REPO, ABL_FILE))39 self.assertEqual(src.kind, "gguf")40 names = src.names()41 # Qwen3 GGUF ties the head: token_embd.weight IS the output projection42 self.assertIn("token_embd.weight", names)43 ref = src.ref("token_embd.weight")44 self.assertEqual(ref.dtype, "Q8_0")45 self.assertEqual(len(ref.shape), 2)46 47 48@needs_live49class TestSlices(unittest.TestCase):50 def test_safetensors_head_slice_decodes(self):51 idx = voice._hf_model_index(CATGIRL_REPO)52 wm = idx["weight_map"]53 name = voice.detect_output_tensor(list(wm.keys()), None)54 url = shard_url(CATGIRL_REPO, wm[name])55 raw, st, _ = voice.http_range(url, 0, 7)56 self.assertEqual(st, 206)57 hs = struct.unpack("<Q", raw)[0]58 raw, st, _ = voice.http_range(url, 8, 8 + hs - 1)59 info = json.loads(raw)[name]60 begin, end = info["data_offsets"]61 n = min(1 << 20, end - begin)62 d = Path(tempfile.mkdtemp(prefix="live-slice-"))63 combined = voice.download_range(url, 8 + hs + begin, 8 + hs + begin + n - 1,64 d, None, label="live-head-slice", n_parts=2)65 arr = voice.decode_to_f32(combined.read_bytes(), info["dtype"],66 (n // voice.dtype_bytes(info["dtype"]),))67 self.assertTrue(bool(np.isfinite(arr.astype(np.float32)).all()))68 self.assertGreater(float(np.abs(arr).max()), 0.0)69 70 def test_remote_gguf_small_tensor_reads(self):71 # smallest tensor in the file: proves remote GGUF header + ranged read + dequant72 src = voice.open_source(shard_url(ABL_REPO, ABL_FILE))73 refs = [(n, src.ref(n)) for n in src.names()]74 refs = [(n, r) for n, r in refs if len(r.shape) <= 2]75 n, r = min(refs, key=lambda nr: int(np.prod(nr[1].shape)))76 arr = src.read_f32(n)77 self.assertEqual(tuple(arr.shape), tuple(r.shape))78 self.assertTrue(bool(np.isfinite(arr).all()))79 80 81if __name__ == "__main__":82 unittest.main()83 