nebulaforge-offline-resilie.../tests/test_qdiff.py

33 lines
1.1 KiB
Python

from nebulaforge.qdiff import create_qdiff, apply_qdiff, manifest_proofs
def test_qdiff_create_and_apply_identity():
base = [0.0, 1.0, 2.0, -1.0]
new = [0.0, 1.0, 2.0, -1.0]
bundle = create_qdiff(base, new)
out = apply_qdiff(base, bundle)
# exact equality for identical arrays
assert out == new
def test_qdiff_quantized_residuals_and_topk():
base = [0.0, 0.0, 0.0, 0.0]
new = [0.0, 10.0, -5.0, 2.0]
# keep only top-2 residuals
bundle = create_qdiff(base, new, top_k=2, chunk_size=8)
out = apply_qdiff(base, bundle)
# Reconstructed values should be close to original for top-k kept; others may be zero
# Check that at least two elements match closely
matches = sum(1 for a, b in zip(out, new) if abs(a - b) < 1e-2)
assert matches >= 2
def test_manifest_hashes():
base = [0.0] * 16
new = [i * 0.1 for i in range(16)]
bundle = create_qdiff(base, new, chunk_size=7)
proofs = manifest_proofs(bundle)
# every manifest entry should have a corresponding proof equal to the key
for k, v in proofs.items():
assert k == v