diff --git a/clipstral-reference.json b/clipstral-reference.json index a14f6db5..7fa5c58e 100644 --- a/clipstral-reference.json +++ b/clipstral-reference.json @@ -1,5 +1,5 @@ { - "what": "texts and images, each with the vector the published code of mistralai/Mistral-Multimodal-Embed-1B gave for it, in float32 on the CPU, in a container with no network: what install-clipstral.sh's proof asks the lane for (clipstral-check.py). Vectors are base64 of 1,024 little-endian float32s.", + "what": "texts and images, each with the vector the published code of mistralai/Mistral-Multimodal-Embed-1B gave for it, in float32 on the CPU, in a container with no network: what install-clipstral.sh's proof asks the lane for (vectors-check.py). Vectors are base64 of 1,024 little-endian float32s.", "repository": "mistralai/Mistral-Multimodal-Embed-1B", "revision": "4d0e936674cff0439f1727058fcf98fc15066090", "model": "mistral-multimodal-embed-1b", diff --git a/clipstral_model.py b/clipstral_model.py index b3c2fd95..3a4c6348 100644 --- a/clipstral_model.py +++ b/clipstral_model.py @@ -8,7 +8,7 @@ Two towers that give one 1,024-dimensional space, each vector of norm 1: Written for this lane from the model's configuration, its weights' names and shapes, and its published description; the weights are read, the repository's own Python is never run by this lane. Its vectors are the published code's, which -wrote clipstral-reference.json once, in a container with no network (docs/clipstral-lane.md): clipstral-check.py compares. +wrote clipstral-reference.json once, in a container with no network (docs/clipstral-lane.md): vectors-check.py compares. The published model also encodes videos; this lane does not, yet. """ import torch diff --git a/docs/clipstral-lane.md b/docs/clipstral-lane.md index 7add2998..1909eebc 100644 --- a/docs/clipstral-lane.md +++ b/docs/clipstral-lane.md @@ -66,7 +66,7 @@ The vectors are proved, not assumed. The published code was run once, in a conta GPU, in float32. Against it, this module in float32: the same tokens, texts' vectors equal to the last bit, the pixels of 64 sizes of image within 9e-7, images' vectors of cosine 1.000000. What the published code gave for a few texts and images is kept in [clipstral-reference.json](../clipstral-reference.json), and `./install-clipstral.sh` ends by asking the lane for -the same and comparing ([clipstral-check.py](../clipstral-check.py), which anyone can run against a lane that serves): a +the same and comparing ([vectors-check.py](../vectors-check.py), which anyone can run against a lane that serves): a cosine of 0.9999 at least for a text, 0.9995 for an image. ## Installing, loading, removing diff --git a/docs/models/mistral-multimodal-embed-1b.md b/docs/models/mistral-multimodal-embed-1b.md index ac8fa03c..f16653df 100644 --- a/docs/models/mistral-multimodal-embed-1b.md +++ b/docs/models/mistral-multimodal-embed-1b.md @@ -40,6 +40,6 @@ words, ask [pplx-embed](pplx-embed-v1-0.6b.md). It also encodes videos; this lan - [docs/clipstral-lane.md](../clipstral-lane.md), the lane's page: its requests, its limits, how its vectors are proved and measured, its memory, its cores and its rights, its pins - [clipstral_model.py](../../clipstral_model.py), the model, and [clipstral-server.py](../../clipstral-server.py), its server -- [clipstral-check.py](../../clipstral-check.py) and [clipstral-reference.json](../../clipstral-reference.json), its proof +- [vectors-check.py](../../vectors-check.py) and [clipstral-reference.json](../../clipstral-reference.json), its proof - [install-clipstral.sh](../../install-clipstral.sh), its pins (the image by digest, the model at its revision, its files by their sha256) and the flags of its unit diff --git a/install-clipstral.sh b/install-clipstral.sh index 30cf0ace..82defd7d 100755 --- a/install-clipstral.sh +++ b/install-clipstral.sh @@ -182,7 +182,7 @@ if [ "$ACTION" = uninstall ]; then exit 0 fi -for f in "${CODE_FILES[@]}" clipstral-check.py clipstral-reference.json "$UNIT.template"; do +for f in "${CODE_FILES[@]}" vectors-check.py clipstral-reference.json "$UNIT.template"; do [ -f "$HERE/$f" ] || die "$f is missing next to this script (a partial copy?): run it from a whole clone of the repository." done @@ -323,7 +323,7 @@ for i in $(seq 1 45); do done [ "$ready" -eq 1 ] || { { sudo journalctl -u "$UNIT" --since "$SINCE" --no-pager || true; } | tail -40; die "the lane did not answer /health within 3 min. The journal above says why."; } echo "up after ~$(( (i - 1) * 4 )) s" -python3 -I "$HERE/clipstral-check.py" "http://127.0.0.1:$PORT" "$HERE/clipstral-reference.json" \ +python3 -I "$HERE/vectors-check.py" "http://127.0.0.1:$PORT" "$HERE/clipstral-reference.json" \ || { { sudo journalctl -u "$UNIT" --since "$SINCE" --no-pager || true; } | tail -20; die "what came back is not what the published model gives (the lines above): the lane is installed, and not proved."; } step "Done: the Mistral Multimodal Embed 1B lane is installed and proved it embeds, on 127.0.0.1:$PORT" diff --git a/tests/test_clipstral_lane.py b/tests/test_clipstral_lane.py index b674aa97..daa1e173 100644 --- a/tests/test_clipstral_lane.py +++ b/tests/test_clipstral_lane.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 """The Mistral Multimodal Embed 1B lane (install-clipstral.sh): the unit its --stage writes, its pins, the code its unit -runs, the room its first Load asks, and the check that proves its vectors (clipstral-check.py). +runs, the room its first Load asks, and the check that proves its vectors (vectors-check.py). This repo's own server on the CPU, in the 27B lane's image, beside every lane: no Conflicts=, no key, 127.0.0.1 alone, no GPU, a container that writes nothing and has no rights but to read, capped by its cgroup and pinned to the A725 cores. @@ -33,7 +33,7 @@ CODE = ("clipstral-server.py", "clipstral_model.py", "vectors_server.py", "cpu_l CODE_ID = hashlib.sha256(b"".join((REPO / f).read_bytes() for f in CODE)).hexdigest()[:12] REPO_DIR = "models--" + PINS["CLIPSTRAL_MODEL"].replace("/", "--") PREFLIGHT = r'snap="([^"]+)"; for f in ([^;]+); do \[ -f "\$snap/\$f" \]' -_spec = importlib.util.spec_from_file_location("clipstral_check", REPO / "clipstral-check.py") +_spec = importlib.util.spec_from_file_location("clipstral_check", REPO / "vectors-check.py") CHECK = importlib.util.module_from_spec(_spec) _spec.loader.exec_module(CHECK) @@ -357,7 +357,7 @@ def b64(v): class ItsProof(unittest.TestCase): - """clipstral-check.py: the vectors that come back are the published code's, or the install says it is not proved.""" + """vectors-check.py: the vectors that come back are the published code's, or the install says it is not proved.""" REF = {"model": "m", "least": {"texts": 0.999, "images": 0.99}, "texts": [{"text": "a", "vector": b64(vec(1, 2))}, {"text": "é", "vector": b64(vec(0, 1))}], "images": [{"name": "a disc", "png": base64.b64encode(b"png").decode(), "vector": b64(vec(3, 1, 4))}]} @@ -384,10 +384,10 @@ class ItsProof(unittest.TestCase): self.addCleanup(srv.server_close) self.addCleanup(srv.shutdown) lines = [] - os.environ.pop("CLIPSTRAL_KEY", None) + os.environ.pop("VECTORS_KEY", None) if key: - os.environ["CLIPSTRAL_KEY"] = key - self.addCleanup(os.environ.pop, "CLIPSTRAL_KEY", None) + os.environ["VECTORS_KEY"] = key + self.addCleanup(os.environ.pop, "VECTORS_KEY", None) return CHECK.check(f"http://127.0.0.1:{srv.server_port}/", self.REF, lines.append), lines, seen def test_the_same_vectors_are_proved_in_one_mixed_request(self): @@ -421,7 +421,7 @@ class ItsProof(unittest.TestCase): def test_a_key_is_read_from_the_environment_alone(self): _, _, seen = self.ask(self.GOOD, key="k1") self.assertEqual(seen["auth"], "Bearer k1") - self.assertNotIn("argv[3]", (REPO / "clipstral-check.py").read_text()) + self.assertNotIn("argv[3]", (REPO / "vectors-check.py").read_text()) def test_a_text_and_an_image_have_their_own_floor(self): self.assertFalse(self.ask([vec(1, 2.4), *self.GOOD[1:]])[0], "0.999 is asked of a text here, and it is 0.998") @@ -456,12 +456,12 @@ class ItsProof(unittest.TestCase): def test_an_install_without_what_it_needs_beside_it_says_which(self): end = TEXT.index("is missing next to this script") loop = TEXT[TEXT.rindex("for f in ", 0, end):end] - for name in ('"${CODE_FILES[@]}"', "clipstral-check.py", "clipstral-reference.json", '"$UNIT.template"'): + for name in ('"${CODE_FILES[@]}"', "vectors-check.py", "clipstral-reference.json", '"$UNIT.template"'): self.assertIn(name, loop) self.assertIn("CODE_FILES=(" + " ".join(CODE) + ")", TEXT) def test_the_installer_ends_on_it(self): - self.assertIn('python3 -I "$HERE/clipstral-check.py" "http://127.0.0.1:$PORT" "$HERE/clipstral-reference.json"', TEXT) + self.assertIn('python3 -I "$HERE/vectors-check.py" "http://127.0.0.1:$PORT" "$HERE/clipstral-reference.json"', TEXT) self.assertIn("the lane is installed, and not proved", TEXT) diff --git a/clipstral-check.py b/vectors-check.py similarity index 65% rename from clipstral-check.py rename to vectors-check.py index d307958b..eb852cea 100644 --- a/clipstral-check.py +++ b/vectors-check.py @@ -1,13 +1,15 @@ #!/usr/bin/env python3 -"""Whether a Clipstral server gives the published model's vectors: the proof of install-clipstral.sh, and a check for anyone. +"""Whether a CPU embedding lane gives the vectors its model is published with: the proof of install-clipstral.sh and +install-embeddings.sh, and a check for anyone. - clipstral-check.py http://127.0.0.1:30030 clipstral-reference.json + vectors-check.py http://127.0.0.1:30030 clipstral-reference.json + vectors-check.py http://127.0.0.1:30028 pplx-reference.json -clipstral-reference.json holds texts and images, each with the vector the published code gave for it, in float32, at the -revision it names (docs/clipstral-lane.md says how it was made). They are asked in one request, mixed, as a client would; -each vector that comes back must have 1,024 values, norm 1, and a cosine with its reference at or above `least`, a text's -and an image's apart. One line each, 0 when all of them do. A box's key, for a proxy, is read from the environment (CLIPSTRAL_KEY), never from the command -line. Standard library only. +A reference holds texts, and images for a model that reads them, each with the vector that what its `by` names gave for it, in +float32, at the revision it names (docs/clipstral-lane.md and docs/embed-lane.md say how each was made). They are asked in one +request, mixed, as a client would; each vector that comes back must have the reference's dimensions, norm 1, and a cosine with +its reference at or above `least`, a text's and an image's apart. One line each, 0 when all of them do. A box's key, for a proxy, +is read from the environment (VECTORS_KEY), never from the command line. Standard library only. """ import base64 import json @@ -18,8 +20,6 @@ import sys import urllib.error import urllib.request -DIM = 1024 - def floats(b64): raw = base64.b64decode(b64) @@ -34,14 +34,15 @@ def cosine(a, b): def check(base, ref, say=print, timeout=300): """True when every reference comes back.""" + images, by = ref.get("images", []), ref.get("by", "the published code") asked = [t["text"] for t in ref["texts"]] + [{"type": "image_url", "image_url": "data:image/png;base64," + i["png"]} - for i in ref["images"]] - names = [repr(t["text"][:40]) for t in ref["texts"]] + [i["name"] for i in ref["images"]] - want = [floats(x["vector"]) for x in (*ref["texts"], *ref["images"])] - least = [ref["least"]["texts"]] * len(ref["texts"]) + [ref["least"]["images"]] * len(ref["images"]) + for i in images] + names = [repr(t["text"][:40]) for t in ref["texts"]] + [i["name"] for i in images] + want = [floats(x["vector"]) for x in (*ref["texts"], *images)] + least = [ref["least"]["texts"]] * len(ref["texts"]) + [ref["least"].get("images", 1)] * len(images) headers = {"Content-Type": "application/json"} - if os.environ.get("CLIPSTRAL_KEY"): - headers["Authorization"] = "Bearer " + os.environ["CLIPSTRAL_KEY"] + if os.environ.get("VECTORS_KEY"): + headers["Authorization"] = "Bearer " + os.environ["VECTORS_KEY"] req = urllib.request.Request(base.rstrip("/") + "/v1/embeddings", headers=headers, data=json.dumps( {"model": ref["model"], "input": asked, "encoding_format": "base64"}).encode()) try: @@ -63,7 +64,7 @@ def check(base, ref, say=print, timeout=300): for i, (name, w, floor) in enumerate(zip(names, want, least)): try: v = floats(got[i]) - if len(v) != DIM: + if len(v) != len(w): raise ValueError(f"{len(v)} values") norm = math.sqrt(math.fsum(x * x for x in v)) c = cosine(v, w) if norm else 0.0 @@ -73,7 +74,7 @@ def check(base, ref, say=print, timeout=300): continue good = c >= floor and abs(norm - 1) < 1e-3 ok = ok and good - say(f" {'ok ' if good else 'NOT'} cosine {c:.6f} with the published code, {name}") + say(f" {'ok ' if good else 'NOT'} cosine {c:.6f} with {by}, {name}") return ok