|
| 1 | +import pytest |
| 2 | +import torch |
| 3 | +import base64 |
| 4 | +from toploc.poly import ( |
| 5 | + find_injective_modulus, |
| 6 | + build_proofs, |
| 7 | + build_proofs_base64, |
| 8 | + ProofPoly, |
| 9 | +) |
| 10 | + |
| 11 | + |
| 12 | +def test_find_injective_modulus(): |
| 13 | + """Test finding injective modulus""" |
| 14 | + x = torch.randint(0, 4_000_000_000, (100,)).tolist() |
| 15 | + modulus = find_injective_modulus(x) |
| 16 | + assert isinstance(modulus, int) |
| 17 | + # Check that all values are unique under modulus |
| 18 | + modded = [i % modulus for i in x] |
| 19 | + assert len(set(modded)) == len(x) |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def sample_poly(): |
| 24 | + return ProofPoly([1, 2, 3, 4], 65497) |
| 25 | + |
| 26 | + |
| 27 | +def test_proof_poly_init(sample_poly): |
| 28 | + """Test initialization of ProofPoly""" |
| 29 | + assert sample_poly.coeffs == [1, 2, 3, 4] |
| 30 | + assert sample_poly.modulus == 65497 |
| 31 | + |
| 32 | + |
| 33 | +def test_proof_poly_call(sample_poly): |
| 34 | + """Test polynomial evaluation""" |
| 35 | + x = 42 |
| 36 | + result = sample_poly(x) |
| 37 | + assert isinstance(result, int) |
| 38 | + assert result == (1 + 2 * x + 3 * x**2 + 4 * x**3) % 65497 |
| 39 | + |
| 40 | + |
| 41 | +def test_proof_poly_len(sample_poly): |
| 42 | + """Test length of polynomial""" |
| 43 | + assert len(sample_poly) == 4 |
| 44 | + |
| 45 | + |
| 46 | +def test_proof_poly_null(): |
| 47 | + """Test null polynomial creation""" |
| 48 | + length = 5 |
| 49 | + null_poly = ProofPoly.null(length) |
| 50 | + assert len(null_poly) == length |
| 51 | + assert null_poly.modulus == 0 |
| 52 | + assert null_poly.coeffs == [0] * length |
| 53 | + |
| 54 | + |
| 55 | +def test_proof_poly_from_points_list(): |
| 56 | + """Test creation from list points""" |
| 57 | + x = [1, 2, 3] |
| 58 | + y = [4, 5, 6] |
| 59 | + poly = ProofPoly.from_points(x, y) |
| 60 | + assert isinstance(poly, ProofPoly) |
| 61 | + assert len(poly.coeffs) > 0 |
| 62 | + |
| 63 | + |
| 64 | +def test_proof_poly_from_points_tensor(): |
| 65 | + """Test creation from tensor points""" |
| 66 | + x = torch.tensor([1, 2, 3]) |
| 67 | + y = torch.tensor([4, 5, 6]) |
| 68 | + poly = ProofPoly.from_points(x, y) |
| 69 | + assert isinstance(poly, ProofPoly) |
| 70 | + assert len(poly.coeffs) == 3 |
| 71 | + assert poly(1) == 4 |
| 72 | + assert poly(2) == 5 |
| 73 | + assert poly(3) == 6 |
| 74 | + |
| 75 | + |
| 76 | +def test_proof_poly_from_points_bfloat16(): |
| 77 | + """Test creation from bfloat16 tensor""" |
| 78 | + x = torch.tensor([1, 2, 3]) |
| 79 | + y = torch.tensor([4, 5, 6], dtype=torch.bfloat16) |
| 80 | + poly = ProofPoly.from_points(x, y) |
| 81 | + assert isinstance(poly, ProofPoly) |
| 82 | + assert len(poly.coeffs) == 3 |
| 83 | + |
| 84 | + |
| 85 | +def test_proof_poly_to_base64(sample_poly): |
| 86 | + """Test base64 encoding""" |
| 87 | + encoded = sample_poly.to_base64() |
| 88 | + assert isinstance(encoded, str) |
| 89 | + # Verify it's valid base64 |
| 90 | + base64.b64decode(encoded) |
| 91 | + |
| 92 | + |
| 93 | +def test_proof_poly_to_bytes(sample_poly): |
| 94 | + """Test bytes conversion""" |
| 95 | + byte_data = sample_poly.to_bytes() |
| 96 | + assert isinstance(byte_data, bytes) |
| 97 | + assert len(byte_data) > 0 |
| 98 | + |
| 99 | + |
| 100 | +def test_proof_poly_from_bytes(sample_poly): |
| 101 | + """Test creation from bytes""" |
| 102 | + byte_data = sample_poly.to_bytes() |
| 103 | + reconstructed = ProofPoly.from_bytes(byte_data) |
| 104 | + assert reconstructed.coeffs == sample_poly.coeffs |
| 105 | + assert reconstructed.modulus == sample_poly.modulus |
| 106 | + |
| 107 | + |
| 108 | +def test_proof_poly_from_base64(sample_poly): |
| 109 | + """Test creation from base64""" |
| 110 | + encoded = sample_poly.to_base64() |
| 111 | + reconstructed = ProofPoly.from_base64(encoded) |
| 112 | + assert reconstructed.coeffs == sample_poly.coeffs |
| 113 | + assert reconstructed.modulus == sample_poly.modulus |
| 114 | + |
| 115 | + |
| 116 | +def test_proof_poly_repr(sample_poly): |
| 117 | + """Test string representation""" |
| 118 | + repr_str = repr(sample_poly) |
| 119 | + assert isinstance(repr_str, str) |
| 120 | + assert str(65497) in repr_str |
| 121 | + assert str([1, 2, 3, 4]) in repr_str |
| 122 | + |
| 123 | + |
| 124 | +@pytest.fixture |
| 125 | +def sample_activations(): |
| 126 | + DIM = 16 |
| 127 | + a = [torch.randn(3, DIM, dtype=torch.bfloat16)] |
| 128 | + for _ in range(3 * 2 + 1): |
| 129 | + a.append(torch.randn(DIM, dtype=torch.bfloat16)) |
| 130 | + return a |
| 131 | + |
| 132 | + |
| 133 | +def test_build_proofs(sample_activations): |
| 134 | + """Test building proofs""" |
| 135 | + proofs = build_proofs(sample_activations, decode_batching_size=2, topk=5) |
| 136 | + assert isinstance(proofs, list) |
| 137 | + assert all(isinstance(p, bytes) for p in proofs) |
| 138 | + assert len(proofs) == 5 |
| 139 | + |
| 140 | + |
| 141 | +def test_build_proofs_base64(sample_activations): |
| 142 | + """Test building base64 proofs""" |
| 143 | + proofs = build_proofs_base64(sample_activations, decode_batching_size=2, topk=5) |
| 144 | + assert isinstance(proofs, list) |
| 145 | + assert all(isinstance(p, str) for p in proofs) |
| 146 | + # Verify each proof is valid base64 |
| 147 | + for proof in proofs: |
| 148 | + base64.b64decode(proof) |
| 149 | + assert len(proofs) == 5 |
| 150 | + |
| 151 | + |
| 152 | +def test_build_proofs_skip_prefill(sample_activations): |
| 153 | + """Test building proofs with skip_prefill""" |
| 154 | + proofs = build_proofs( |
| 155 | + sample_activations, decode_batching_size=2, topk=5, skip_prefill=True |
| 156 | + ) |
| 157 | + assert isinstance(proofs, list) |
| 158 | + assert all(isinstance(p, bytes) for p in proofs) |
| 159 | + assert len(proofs) == 4 |
| 160 | + |
| 161 | + |
| 162 | +def test_build_proofs_error_handling(): |
| 163 | + """Test error handling in proof building""" |
| 164 | + invalid_activations = [ |
| 165 | + torch.randn(0, 16, dtype=torch.bfloat16), |
| 166 | + torch.randn(16, dtype=torch.bfloat16), |
| 167 | + ] |
| 168 | + proofs = build_proofs(invalid_activations, decode_batching_size=2, topk=5) |
| 169 | + assert isinstance(proofs, list) |
| 170 | + assert all(isinstance(p, bytes) for p in proofs) |
| 171 | + |
| 172 | + nullproof = ProofPoly.null(5).to_bytes() |
| 173 | + assert all(p == nullproof for p in proofs) |
| 174 | + |
| 175 | + |
| 176 | +def test_build_proofs_edge_cases(sample_activations): |
| 177 | + """Test edge cases for proof building""" |
| 178 | + # Test with minimal topk |
| 179 | + proofs_min = build_proofs(sample_activations, decode_batching_size=2, topk=1) |
| 180 | + assert len(proofs_min) > 0 |
| 181 | + |
| 182 | + # Test with large batching size |
| 183 | + proofs_large_batch = build_proofs( |
| 184 | + sample_activations, decode_batching_size=10, topk=5 |
| 185 | + ) |
| 186 | + assert len(proofs_large_batch) > 0 |
| 187 | + |
| 188 | + # Test with only one prefill activation |
| 189 | + proofs_one = build_proofs(sample_activations[:1], decode_batching_size=2, topk=5) |
| 190 | + assert len(proofs_one) == 1 |
| 191 | + |
| 192 | + # Test with only one activation and skip_prefill |
| 193 | + proofs_one_skip = build_proofs( |
| 194 | + sample_activations[:1], decode_batching_size=2, topk=5, skip_prefill=True |
| 195 | + ) |
| 196 | + assert len(proofs_one_skip) == 0 |
0 commit comments