Files
leleka/scripts/smoke-static-cli.py
T

201 lines
8.3 KiB
Python

#!/usr/bin/env python3
"""CI-only fixture for the standalone Linux CLI; no model assets are retained."""
import hashlib
import json
import pathlib
import struct
import subprocess
import sys
import tempfile
def fixture() -> bytes:
header = bytearray(b"GGUF" + struct.pack("<IQQ", 3, 2, 1))
def string(value: str) -> None:
encoded = value.encode()
header.extend(struct.pack("<Q", len(encoded)))
header.extend(encoded)
string("general.architecture")
header.extend(struct.pack("<I", 8))
string("qwen35")
for name, kind, offset, elements in (
("dense", 0, 128, 32),
("expert", 21, 0, 256),
):
string(name)
header.extend(struct.pack("<IQIQ", 1, elements, kind, offset))
header.extend(bytes((-len(header)) % 32))
payload = bytearray(256)
payload[:2] = struct.pack("<H", 0x3C00)
for index in range(64):
payload[index + 2] = index * 3 % 256
payload[74] = 0x55
for index in range(32):
payload[128 + 4 * index : 132 + 4 * index] = struct.pack(
"<f", (-1.0, -0.25, 0.25, 1.0)[index % 4]
)
return bytes(header + payload)
def shard(name: str, kind: int, elements: int, payload: bytes) -> bytes:
header = bytearray(b"GGUF" + struct.pack("<IQQ", 3, 1, 1))
def string(value: str) -> None:
encoded = value.encode()
header.extend(struct.pack("<Q", len(encoded)))
header.extend(encoded)
string("general.architecture")
header.extend(struct.pack("<I", 8))
string("qwen35")
string(name)
header.extend(struct.pack("<IQIQ", 1, elements, kind, 0))
header.extend(bytes((-len(header)) % 32))
return bytes(header + payload)
def main() -> None:
binary = pathlib.Path(sys.argv[1]).resolve()
with tempfile.TemporaryDirectory(prefix="strata-static-smoke-") as temporary:
root = pathlib.Path(temporary)
source = root / "mixed.gguf"
archive = root / "mixed.kar"
source.write_bytes(fixture())
derive = subprocess.run(
[binary, "model", "derive-mixed-identity", source],
capture_output=True,
text=True,
check=True,
)
identity = json.loads(derive.stdout)
expected_source = "c407838a4718093d8f2699209c7cac8b998a47b937e5afe59e4c5a0af09033a7"
assert identity["derived_f32_sha256"] == expected_source
prepare = subprocess.run(
[binary, "model", "prepare-mixed-preview", source, "--output", archive, "--levels", "2"],
capture_output=True,
text=True,
check=True,
)
assert "not eligible for serving" in prepare.stdout
expected_archive = "4053e787d46b2cf2bca3d67e38d06e1ee96a157e80e3155c8dbdbf36835df88c"
assert hashlib.sha256(archive.read_bytes()).hexdigest() == expected_archive
provenance = json.loads((root / "mixed.kar.provenance.json").read_text())
assert provenance["original_gguf_sha256"] == hashlib.sha256(source.read_bytes()).hexdigest()
assert provenance["derived_f32_sha256"] == expected_source
assert provenance["archive_sha256"] == expected_archive
verify = subprocess.run(
[binary, "model", "verify-mixed-preview", archive, "--source", source],
capture_output=True,
text=True,
check=True,
)
assert "payload CRCs match" in verify.stdout
dense = b"".join(
struct.pack("<f", (-1.0, -0.25, 0.25, 1.0)[index % 4])
for index in range(32)
)
iq3 = bytearray(110)
iq3[:2] = struct.pack("<H", 0x3C00)
for index in range(64):
iq3[index + 2] = index * 3 % 256
iq3[74] = 0x55
shards = {
"a-dense.gguf": shard("dense", 0, 32, dense),
"z-expert.gguf": shard("expert", 21, 256, bytes(iq3)),
}
for name, data in shards.items():
(root / name).write_bytes(data)
manifest = root / "shards.json"
manifest.write_text(json.dumps({
"schema_version": 1,
"files": [
{"name": name, "url": f"https://example.invalid/{name}",
"size": len(data), "sha256": hashlib.sha256(data).hexdigest()}
for name, data in reversed(list(shards.items()))
],
}))
streamed = root / "streamed.kar"
prepare_streamed = subprocess.run(
[binary, "model", "prepare-sharded-streamed", manifest,
"--source-dir", root, "--output", streamed, "--levels", "2"],
capture_output=True, text=True, check=True,
)
assert "EXPERIMENTAL" in prepare_streamed.stdout
assert "archive bytes:" in prepare_streamed.stdout
assert "source GGUF bytes:" in prepare_streamed.stdout
assert "archive/source ratio:" in prepare_streamed.stdout
assert "accounted bits per weight:" in prepare_streamed.stdout
assert "not eligible for serving" in prepare_streamed.stdout
streamed_provenance = json.loads(
(root / "streamed.kar.provenance.json").read_text()
)
assert streamed_provenance["schema"] == "strata-next/sharded-stream-provenance-v4"
assert streamed_provenance["derivation"] == "strata-next/gguf-shards-to-f32-le-v4:ggml-3cf03257"
assert streamed_provenance["partitioned_tensors"] == []
assert streamed_provenance["levels"] == 2
assert streamed_provenance["derived_f32_sha256"] == expected_source
assert streamed_provenance["archive_sha256"] == hashlib.sha256(streamed.read_bytes()).hexdigest()
assert streamed_provenance["archive_sha256"] == (
"47a564c5874d1cedbbce7feabfe4a4e04c3ddd3c7d222669a089d2f7f5530c9a"
), streamed_provenance["archive_sha256"]
verify_streamed = subprocess.run(
[binary, "model", "verify-sharded-streamed", streamed,
"--manifest", manifest, "--source-dir", root, "--levels", "2"],
capture_output=True, text=True, check=True,
)
assert "payload CRCs match" in verify_streamed.stdout
ensured = root / "ensured.kar"
ensure_args = [
binary, "model", "ensure-sharded-streamed", manifest,
"--source-dir", root, "--output", ensured, "--levels", "2",
]
first = subprocess.run(ensure_args, capture_output=True, text=True, check=True)
assert "created and verified" in first.stdout
assert "archive/source ratio:" in first.stdout
assert ensured.read_bytes() == streamed.read_bytes()
second = subprocess.run(ensure_args, capture_output=True, text=True, check=True)
assert "reused verified" in second.stdout
assert "archive/source ratio:" in second.stdout
wrong_level = subprocess.run(
ensure_args[:-1] + ["3"], capture_output=True, text=True,
)
assert wrong_level.returncode != 0
assert ensured.read_bytes() == streamed.read_bytes()
iq4_name = "b-iq4.gguf"
iq4 = shard("iq4", 20, 32, bytes(18))
(root / iq4_name).write_bytes(iq4)
quantized_manifest = root / "quantized-shards.json"
quantized_manifest.write_text(json.dumps({
"schema_version": 1,
"files": [
{"name": name, "url": f"https://example.invalid/{name}",
"size": len(data), "sha256": hashlib.sha256(data).hexdigest()}
for name, data in [(iq4_name, iq4), ("z-expert.gguf", shards["z-expert.gguf"])]
],
}))
quantized_archive = root / "quantized.kar"
subprocess.run(
[binary, "model", "prepare-sharded-streamed", quantized_manifest,
"--source-dir", root, "--output", quantized_archive, "--levels", "2"],
capture_output=True, text=True, check=True,
)
subprocess.run(
[binary, "model", "verify-sharded-streamed", quantized_archive,
"--manifest", quantized_manifest, "--source-dir", root, "--levels", "2"],
capture_output=True, text=True, check=True,
)
quantized_sha256 = hashlib.sha256(quantized_archive.read_bytes()).hexdigest()
assert quantized_sha256 == (
"a02dac16ea74f753a3f9d73d1b820e79f9934ad35af49f2bac580419f6bdf75a"
), quantized_sha256
print("static CLI mixed and streamed sharded GGUF/KAR smokes passed")
if __name__ == "__main__":
main()