mirror of
https://github.com/hasso5703/alzeo.git
synced 2026-12-06 21:04:21 +01:00
91 lines
4.2 KiB
Python
91 lines
4.2 KiB
Python
#!/usr/bin/env python3
|
|
"""The pplx-embed model's module (pplx_model.py) against the published checkpoint's own list: every tensor it declares is there
|
|
under that name and in that shape, every tensor is read, and its constants are those of the published configuration. CI has no
|
|
torch: the module is read with the stand-ins of test_clipstral_model.py, and its arithmetic is proved on a box, against the
|
|
vectors of the repository's ONNX export (install-embeddings.sh)."""
|
|
import importlib.util
|
|
import json
|
|
import math
|
|
import pathlib
|
|
import sys
|
|
import types
|
|
import unittest
|
|
|
|
REPO = pathlib.Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(REPO / "tests"))
|
|
from test_clipstral_model import Linear, Module, a_torch # noqa: E402
|
|
|
|
PUBLISHED = json.loads((REPO / "tests/fixtures/pplx-tensors.json").read_text())
|
|
CONFIG = PUBLISHED["config"]
|
|
|
|
|
|
def the_module():
|
|
stand_ins = a_torch()
|
|
was = {k: sys.modules.get(k) for k in (*stand_ins, "cpu_layers")}
|
|
sys.modules.update(stand_ins)
|
|
try:
|
|
for name in ("cpu_layers", "pplx_model"):
|
|
spec = importlib.util.spec_from_file_location(name + "_read", REPO / f"{name}.py")
|
|
m = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(m)
|
|
if name == "cpu_layers":
|
|
sys.modules[name] = layers = m
|
|
return m, layers
|
|
finally:
|
|
for k, v in was.items():
|
|
if v is None:
|
|
sys.modules.pop(k, None)
|
|
else:
|
|
sys.modules[k] = v
|
|
|
|
|
|
M, L = the_module()
|
|
|
|
|
|
class TheWeightsItReads(unittest.TestCase):
|
|
def test_each_tensor_by_its_name_and_its_shape_and_none_left(self):
|
|
mine = dict(M.Pplx().tensors())
|
|
self.assertEqual({k: list(v) for k, v in mine.items()}, PUBLISHED["tensors"])
|
|
|
|
def test_its_constants_are_the_configuration_s(self):
|
|
for ours, name in ((M.DIM, "hidden_size"), (M.VOCAB, "vocab_size"), (M.LAYERS, "num_hidden_layers"), (M.HEADS, "num_attention_heads"),
|
|
(M.PAIRS, "num_key_value_heads"), (M.FFN, "intermediate_size"), (M.HEAD, "head_dim"), (M.THETA, "rope_theta"),
|
|
(M.EPS, "rms_norm_eps")):
|
|
self.assertEqual(ours, CONFIG[name], name)
|
|
self.assertEqual((CONFIG["use_bidirectional_attention"], CONFIG["attention_bias"], CONFIG["hidden_act"], CONFIG["sliding_window"]),
|
|
(True, False, "silu", None))
|
|
pooling = {k: v for k, v in PUBLISHED["pooling"].items() if k.startswith("pooling_mode_") and v}
|
|
self.assertEqual((pooling, PUBLISHED["pooling"]["include_prompt"]), ({"pooling_mode_mean_tokens": True}, True))
|
|
|
|
def test_every_product_goes_through_the_bf16_instructions(self):
|
|
plain = []
|
|
|
|
def walk(m, at=""):
|
|
for k, v in vars(m).items():
|
|
if isinstance(v, Linear) and not isinstance(v, L.Linear):
|
|
plain.append(at + k)
|
|
if isinstance(v, Module):
|
|
walk(v, f"{at}{k}.")
|
|
walk(M.Pplx())
|
|
self.assertEqual(plain, ["embed_tokens"], "the table of tokens is no product")
|
|
|
|
def test_its_vectors_are_the_int8_ones_its_card_publishes(self):
|
|
"""The mean of the last states is not the model's vector: its hyperbolic tangent in steps of 1/127 is, half to even
|
|
(st_quantize.py of the repository, read; the ONNX export's pooler_output_int8, which TEI serves, compared on a box)."""
|
|
was, M.torch = M.torch, types.SimpleNamespace(tanh=math.tanh, round=round)
|
|
try:
|
|
for mean, want in ((0.0, 0), (0.1, 13), (-0.1, -13), (0.5, 59), (3.0, 126), (9.0, 127), (-9.0, -127), (0.003, 0)):
|
|
self.assertEqual(M.int8(mean), want, mean)
|
|
finally:
|
|
M.torch = was
|
|
self.assertIn("return unit(int8((self.norm(x) * kept).sum(1) / kept.sum(1)))", (REPO / "pplx_model.py").read_text())
|
|
|
|
def test_nothing_of_the_repositorys_python_is_read(self):
|
|
text = "".join((REPO / f).read_text() for f in ("pplx_model.py", "pplx-server.py", "vectors_server.py", "cpu_layers.py"))
|
|
for word in ("trust_remote_code", "AutoModel", "transformers", "modeling.py"):
|
|
self.assertNotIn(word, text)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|