mirror of
https://github.com/hasso5703/alzeo.git
synced 2026-12-06 12:54:19 +01:00
175 lines
8.6 KiB
Python
175 lines
8.6 KiB
Python
#!/usr/bin/env python3
|
||
"""The embeddings lane (install-embeddings.sh): Perplexity's pplx-embed-v1-0.6b on this repo's own server (pplx-server.py), on
|
||
the CPU beside every lane. Its installer, its unit and its first Load have the Clipstral lane's shape: their tests are the two
|
||
lanes' (vector_lanes.py). What is this lane's own is here."""
|
||
import base64
|
||
import json
|
||
import math
|
||
import pathlib
|
||
import re
|
||
import struct
|
||
import subprocess
|
||
import sys
|
||
import unittest
|
||
|
||
REPO = pathlib.Path(__file__).resolve().parents[1]
|
||
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
|
||
import vector_lanes # noqa: E402
|
||
|
||
LANE = vector_lanes.EMBED
|
||
TEXT, MODEL, REV = LANE.text, LANE.model, LANE.rev
|
||
DOC = REPO / "docs" / "embed-lane.md"
|
||
PAGE = REPO / "docs" / "models" / "pplx-embed-v1-0.6b.md"
|
||
|
||
|
||
def flat(path):
|
||
return " ".join(path.read_text().replace("’", "'").split())
|
||
|
||
|
||
class ItsUnit(vector_lanes.UnitTests, unittest.TestCase):
|
||
L = LANE
|
||
|
||
|
||
class ItsCode(vector_lanes.CodeTests, unittest.TestCase):
|
||
L = LANE
|
||
|
||
|
||
class ItsFirstLoad(vector_lanes.FirstLoadTests, unittest.TestCase):
|
||
L = LANE
|
||
|
||
|
||
class ItsProofAtTheEndOfARun(vector_lanes.ProofRunTests, unittest.TestCase):
|
||
L = LANE
|
||
|
||
|
||
class ItsFirstLoadBegun(vector_lanes.BegunTests, unittest.TestCase):
|
||
L = LANE
|
||
|
||
|
||
class ItsPortThatMoves(vector_lanes.PortTests, unittest.TestCase):
|
||
L = LANE
|
||
|
||
|
||
class TheCacheItFetchesInto(vector_lanes.Staged, unittest.TestCase):
|
||
"""A box whose cache lives on another disk (docs/external-disk.md: HF_CACHE=/mnt/ssd/huggingface ./install.sh) keeps the model
|
||
there, read the way the lanes beside it read theirs."""
|
||
L = LANE
|
||
|
||
def unit_mounting(self, unit, cache):
|
||
(self.units / unit).write_text(f"ExecStart=/bin/bash -c 'exec /usr/bin/docker run --rm --name x -v {cache}:/root/.cache/huggingface:ro x'\n")
|
||
|
||
def cache(self, **env):
|
||
r = subprocess.run(["bash", str(self.L.installer), "--stage", str(self.out)], capture_output=True, text=True, timeout=60,
|
||
env={"PATH": "/usr/bin:/bin", "HOME": str(self.home), "LANES_UNIT_DIR": str(self.units), **env})
|
||
self.assertEqual(r.returncode, 0, r.stdout + r.stderr)
|
||
return re.search(r"source=(\S+?)/hub/", self.out.read_text()).group(1)
|
||
|
||
def test_the_text_lanes_its_own_unit_and_the_environment(self):
|
||
self.assertEqual(self.cache(), str(self.home / ".cache" / "huggingface"))
|
||
self.unit_mounting("qwen38-sglang.service", "/mnt/ssd/hf")
|
||
self.assertEqual(self.cache(), "/mnt/ssd/hf")
|
||
self.unit_mounting(LANE.unit, "/mnt/other/hf")
|
||
self.assertEqual(self.cache(), "/mnt/other/hf", "its own unit keeps priority over the text lanes'")
|
||
self.assertEqual(self.cache(HF_CACHE="/srv/hf"), "/srv/hf", "HF_CACHE in the environment wins over every unit")
|
||
|
||
def test_a_box_that_ran_the_lane_on_tei_keeps_its_cache_and_its_port(self):
|
||
"""v1.60.0's unit mounted the whole cache and named its port --port too: the first unit of this server reads both there."""
|
||
(self.units / LANE.unit).write_text(
|
||
"ExecStart=/bin/bash -c 'for f in x; do :; done; \\\n exec /usr/bin/docker run --rm --name qwen38-embed \\\n"
|
||
" -v /mnt/old/hf:/root/.cache/huggingface:ro \\\n ghcr.io/huggingface/text-embeddings-inference@sha256:0 \\\n"
|
||
" --hostname 127.0.0.1 --port 30128'\n")
|
||
self.assertEqual(self.cache(), "/mnt/old/hf")
|
||
self.assertIn("--port 30128", self.out.read_text())
|
||
|
||
|
||
class ThePins(unittest.TestCase):
|
||
def test_the_model_is_pplx_embed_v1_06b_at_its_pinned_revision(self):
|
||
self.assertEqual(MODEL, "perplexity-ai/pplx-embed-v1-0.6b")
|
||
self.assertEqual(REV, "2c4d510dd4a732063c31a0f70193e35067b51fd8")
|
||
|
||
def test_its_files_are_pinned_by_their_sha256_and_their_size(self):
|
||
for key, name, size in (("FILE", "model.safetensors", 2384233112), ("TOKENIZER", "tokenizer.json", 11422837)):
|
||
f, pin = LANE.pin(key).split("=")
|
||
sha, n = pin.split(":")
|
||
self.assertEqual((f, int(n)), (name, size))
|
||
self.assertRegex(sha, r"^[0-9a-f]{64}$")
|
||
# nothing of the repository's Python, nor the ONNX export TEI served in v1.60.0
|
||
self.assertNotIn(".py=", TEXT)
|
||
self.assertNotIn("onnx/", "\n".join(ln for ln in TEXT.splitlines() if not ln.startswith("#")))
|
||
|
||
def test_the_room_its_fetch_asks_is_its_files(self):
|
||
total = sum(int(LANE.pin(k).rsplit(":", 1)[1]) for k in ("FILE", "TOKENIZER"))
|
||
self.assertEqual(int(LANE.pin("GB")), math.ceil(total / 1e9))
|
||
|
||
def test_the_uninstall_keeps_the_model_and_says_so(self):
|
||
self.assertIn("the model is kept", TEXT)
|
||
|
||
|
||
class ItsReference(unittest.TestCase):
|
||
"""pplx-reference.json, which the proof at the end of an install asks the lane for on every box, was written by the model's own
|
||
ONNX export (Perplexity's file, run by onnxruntime), never by the code it checks. Its vectors are the int8 ones the card
|
||
publishes, which TEI gave in v1.60.0: the mean they are rounded from is another vector (cosine 0.995 to 0.9998)."""
|
||
|
||
def test_it_is_the_pinned_models_and_whole(self):
|
||
ref = json.loads((REPO / "pplx-reference.json").read_text())
|
||
self.assertEqual(ref["model"], "pplx-embed-v1-0.6b")
|
||
self.assertEqual(ref["revision"], REV)
|
||
self.assertEqual(ref["repository"], MODEL)
|
||
self.assertIn("ONNX", ref["by"])
|
||
self.assertGreaterEqual(ref["least"]["texts"], 0.9995)
|
||
self.assertNotIn("images", ref["least"])
|
||
texts = [t["text"] for t in ref["texts"]]
|
||
self.assertGreaterEqual(len(texts), 7)
|
||
self.assertEqual(len(set(texts)), len(texts))
|
||
for t in ref["texts"]:
|
||
v = struct.unpack("<1024f", base64.b64decode(t["vector"]))
|
||
self.assertAlmostEqual(math.sqrt(sum(x * x for x in v)), 1.0, places=4)
|
||
step = min(abs(x) for x in v if x) # the vector's 1: every value is a whole number of it, 127 at the most
|
||
self.assertLess(max(abs(x / step - round(x / step)) for x in v), 1e-3)
|
||
self.assertLessEqual(max(abs(round(x / step)) for x in v), 127)
|
||
# several lengths and scripts: a padded batch, a text of more than one window of the attention's bf16 blocks
|
||
self.assertGreater(max(map(len, texts)), 20 * min(map(len, texts)))
|
||
self.assertTrue(any(ord(c) > 0x3000 for t in texts for c in t))
|
||
|
||
|
||
class ItsPages(unittest.TestCase):
|
||
def test_the_licence_is_named(self):
|
||
self.assertIn("Its licence is MIT", flat(PAGE))
|
||
base = (REPO / "dashboard" / "static" / "js" / "base.js").read_text()
|
||
card = re.search(r"\[EMBED_UNIT\]: \{title: 'pplx-embed v1 0\.6B'.*?desc: '([^']+)'", base, re.S)
|
||
self.assertIn("(MIT)", card.group(1))
|
||
|
||
def test_why_the_gpu_path_is_not_the_lane_yet(self):
|
||
"""x42en's GPU path (llama.cpp, the override of the causal attention) was tried on the reference box on 2026-11-24 and does not
|
||
hold there yet: a request of several texts came back with every value null, and a new CUDA context beside the 27B
|
||
failed at its start on a fragmented memory. They are credited for the request, the model, the GGUF they pinned, the
|
||
override and their measures."""
|
||
for page in (DOC, PAGE):
|
||
for said in ("not yet", "null", "out of memory", "x42en", "bowang0911", "qwen3.attention.causal=bool:false", "discussion #69"):
|
||
self.assertIn(said, flat(page), (page.name, said))
|
||
|
||
def test_a_64_gb_box_refuses_it(self):
|
||
for page in (DOC, PAGE):
|
||
self.assertIn("A 64 GB DGX Spark refuses it", flat(page))
|
||
|
||
def test_a_stopped_lane_leaves_the_route_to_the_text_engine(self):
|
||
self.assertIn("a stopped lane leaves the route to the text engine, whose own answer is a 400", flat(DOC))
|
||
self.assertNotIn("a stopped lane answers 503", flat(DOC))
|
||
|
||
def test_the_units_are_the_repos(self):
|
||
for f in (LANE.installer, DOC, PAGE, REPO / "README.md"):
|
||
self.assertIsNone(re.search(r"\bMio\b|\bGio\b|\bcosinus\b", f.read_text()), f.name)
|
||
|
||
def test_no_page_says_tei_serves_it(self):
|
||
"""TEI served the lane in v1.60.0. Where a page names it, it is as what served then, or what the vectors were compared with."""
|
||
for f in (PAGE, REPO / "README.md", REPO / "dashboard" / "static" / "js" / "base.js", REPO / "install.sh", REPO / "prepare-lane.sh"):
|
||
self.assertIsNone(re.search(r"served by Hugging Face|on TEI\b|TEI’s image|TEI's image|TEI on the CPU", f.read_text()), f.name)
|
||
|
||
def test_the_cores_are_those_lscpu_lists(self):
|
||
self.assertEqual(LANE.pin("CPUS"), "0-4,10-14")
|
||
self.assertIn("read from lscpu", flat(DOC))
|
||
|
||
|
||
if __name__ == "__main__":
|
||
unittest.main()
|