File size: 15,732 Bytes
5347c49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5911d9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5347c49
5911d9a
5347c49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
"""
Layer 1: Property-Based Falsifier
==================================
Attacks the extracted C++20 kernels via Hypothesis.
Every test corresponds to a named Lean 4 theorem.

Run:
    pip install hypothesis pytest
    cmake -S .. -B ../build && cmake --build ../build
    pytest falsify_properties.py -x -v --tb=short

Pass criteria: zero failures after 100k shrunk examples.
If Hypothesis finds one counterexample, the Lean theorem was weaker than intent.
"""

import ctypes
import math
import os
import pathlib
import sys

import pytest
from hypothesis import given, settings, assume, Phase
from hypothesis import strategies as st

# ── Load the shared library ───────────────────────────────────────────────────

def _add_dll_dirs():
    """Add MinGW/Strawberry runtime dirs so ctypes can resolve DLL deps on Windows."""
    for d in [
        r"C:\Strawberry\c\bin",
        r"C:\Program Files\mingw64\bin",
        r"C:\mingw64\bin",
        r"C:\msys64\mingw64\bin",
    ]:
        if os.path.isdir(d):
            try:
                os.add_dll_directory(d)
            except AttributeError:
                pass  # Python < 3.8

if os.name == "nt":
    _add_dll_dirs()


def _load_lib():
    repo = pathlib.Path(__file__).resolve().parent.parent
    candidates = [
        repo / "build" / "libsovereign_array.so",
        repo / "build" / "libsovereign_array.dll",
        repo / "build" / "sovereign_array.dll",
        repo / "build" / "libsovereign_array.dylib",
    ]
    for p in candidates:
        if p.exists():
            return ctypes.CDLL(str(p))
    raise FileNotFoundError(
        "Build the shared library first:\n"
        "  cmake -S .. -B ../build && cmake --build ../build\n"
        f"Looked in: {[str(c) for c in candidates]}"
    )

try:
    _lib = _load_lib()

    # void sovarr_softmax(const float*, float*, size_t)
    _lib.sovarr_softmax.argtypes = [
        ctypes.POINTER(ctypes.c_float),
        ctypes.POINTER(ctypes.c_float),
        ctypes.c_size_t,
    ]
    _lib.sovarr_softmax.restype = None

    # void sovarr_face_centroid(const int*, size_t, float*, size_t)
    _lib.sovarr_face_centroid.argtypes = [
        ctypes.POINTER(ctypes.c_int),
        ctypes.c_size_t,
        ctypes.POINTER(ctypes.c_float),
        ctypes.c_size_t,
    ]
    _lib.sovarr_face_centroid.restype = None

    # void sovarr_nand_attention(const float*, float*, float*, float*, size_t)
    _lib.sovarr_nand_attention.argtypes = [
        ctypes.POINTER(ctypes.c_float),
        ctypes.POINTER(ctypes.c_float),
        ctypes.POINTER(ctypes.c_float),
        ctypes.POINTER(ctypes.c_float),
        ctypes.c_size_t,
    ]
    _lib.sovarr_nand_attention.restype = None

    # int sovarr_nand(int, int) etc.
    for fn in ("sovarr_nand", "sovarr_not", "sovarr_and", "sovarr_or"):
        getattr(_lib, fn).argtypes = [ctypes.c_int, ctypes.c_int]
        getattr(_lib, fn).restype  = ctypes.c_int

    LIB_OK = True
except FileNotFoundError as e:
    print(f"WARNING: {e}", file=sys.stderr)
    LIB_OK = False


def softmax_c(xs: list[float]) -> list[float]:
    n = len(xs)
    in_arr  = (ctypes.c_float * n)(*xs)
    out_arr = (ctypes.c_float * n)()
    _lib.sovarr_softmax(in_arr, out_arr, n)
    return list(out_arr)

def face_centroid_c(support: list[int], n: int) -> list[float]:
    sup_arr = (ctypes.c_int * len(support))(*support)
    out_arr = (ctypes.c_float * n)()
    _lib.sovarr_face_centroid(sup_arr, len(support), out_arr, n)
    return list(out_arr)

def nand_attention_c(q, k, v):
    n = len(q)
    def fa(xs): return (ctypes.c_float * n)(*xs)
    out = (ctypes.c_float * n)()
    _lib.sovarr_nand_attention(fa(q), fa(k), fa(v), out, n)
    return list(out)

def softmax_ref(xs: list[float]) -> list[float]:
    """Pure-Python reference implementation (exact, slow)."""
    m = max(xs)  # shift for numerical stability
    exps = [math.exp(x - m) for x in xs]
    s = sum(exps)
    return [e / s for e in exps]

skip_no_lib = pytest.mark.skipif(not LIB_OK, reason="shared lib not built")

# ─────────────────────────────────────────────────────────────────────────────
# PAPER I — NAND universality
# Lean theorems: notGate_eq, andGate_eq, orGate_eq
# ─────────────────────────────────────────────────────────────────────────────

@skip_no_lib
class TestPaperI_NAND:

    @given(st.integers(0, 1), st.integers(0, 1))
    @settings(max_examples=4, phases=[Phase.generate])  # truth table is 4 entries
    def test_nand_truth_table(self, a, b):
        """sovarr_nand(a,b) == not(a and b)  [andGate_eq / notGate_eq]"""
        expected = 0 if (a == 1 and b == 1) else 1
        assert _lib.sovarr_nand(a, b) == expected

    @given(st.integers(0, 1))
    @settings(max_examples=2)
    def test_not_via_nand(self, a):
        """nand(a,a) == not(a)  [notGate_eq]"""
        assert _lib.sovarr_not(a, a) == (0 if a == 1 else 1)

    @given(st.integers(0, 1), st.integers(0, 1))
    @settings(max_examples=4)
    def test_and_via_nand(self, a, b):
        """nand(nand(a,b), nand(a,b)) == a and b  [andGate_eq]"""
        assert _lib.sovarr_and(a, b) == (1 if a == 1 and b == 1 else 0)

    @given(st.integers(0, 1), st.integers(0, 1))
    @settings(max_examples=4)
    def test_or_via_nand(self, a, b):
        """nand(nand(a,a), nand(b,b)) == a or b  [orGate_eq]"""
        assert _lib.sovarr_or(a, b) == (1 if a == 1 or b == 1 else 0)

    @given(st.integers(0, 1), st.integers(0, 1), st.integers(0, 1))
    @settings(max_examples=8)
    def test_demorgan(self, a, b, c):
        """not(a or b) == not(a) and not(b)  — derived via NAND"""
        lhs = _lib.sovarr_not(_lib.sovarr_or(a, b), _lib.sovarr_or(a, b))
        rhs = _lib.sovarr_and(_lib.sovarr_not(a, a), _lib.sovarr_not(b, b))
        assert lhs == rhs


# ─────────────────────────────────────────────────────────────────────────────
# PAPER II — Simplex / Softmax / Face Centroid
# Lean theorems: softmax_is_pmap, softmax_shift_invariant,
#                faceCentroid_nonneg, faceCentroid_support, vertex_centroid_eq
# ─────────────────────────────────────────────────────────────────────────────

@skip_no_lib
class TestPaperII_Simplex:

    @given(st.lists(st.floats(-20, 20, allow_nan=False, allow_infinity=False),
                    min_size=2, max_size=256))
    @settings(max_examples=50_000, phases=[Phase.generate, Phase.shrink])
    def test_softmax_sums_to_one(self, logits):
        """Σ softmax(z)_i = 1  [softmax_is_pmap, sum normalisation]"""
        out = softmax_c(logits)
        assert abs(sum(out) - 1.0) < 1e-5, f"sum={sum(out)} logits={logits[:4]}"

    @given(st.lists(st.floats(-10, 10, allow_nan=False, allow_infinity=False),
                    min_size=2, max_size=256),
           st.floats(-50, 50, allow_nan=False, allow_infinity=False))
    @settings(max_examples=20_000, phases=[Phase.generate, Phase.shrink])
    def test_softmax_shift_invariant(self, logits, c):
        """softmax(z+c) == softmax(z)  [softmax_shift_invariant]"""
        shifted = [x + c for x in logits]
        out1 = softmax_c(logits)
        out2 = softmax_c(shifted)
        for i, (a, b) in enumerate(zip(out1, out2)):
            assert abs(a - b) < 1e-4, f"shift broke at i={i}: {a} vs {b} (c={c})"

    @given(st.lists(st.floats(-10, 10, allow_nan=False, allow_infinity=False),
                    min_size=2, max_size=256))
    @settings(max_examples=20_000)
    def test_softmax_matches_reference(self, logits):
        """sovarr_softmax matches pure-Python reference  [kernel correctness]"""
        c_out  = softmax_c(logits)
        py_out = softmax_ref(logits)
        for i, (a, b) in enumerate(zip(c_out, py_out)):
            assert abs(a - b) < 1e-4, f"mismatch at i={i}: C={a} ref={b}"

    @given(st.integers(2, 64),
           st.lists(st.integers(0, 63), min_size=1, max_size=64, unique=True))
    @settings(max_examples=10_000, phases=[Phase.generate, Phase.shrink])
    def test_face_centroid_sums_to_one(self, n, raw_support):
        """Σ faceCentroid(F)_i = 1  [faceCentroid = uniform on F]"""
        support = [i for i in raw_support if i < n]
        assume(len(support) > 0)
        out = face_centroid_c(support, n)
        assert abs(sum(out) - 1.0) < 1e-6, f"sum={sum(out)} F={support} n={n}"

    @given(st.integers(2, 64),
           st.lists(st.integers(0, 63), min_size=1, max_size=64, unique=True))
    @settings(max_examples=10_000)
    def test_face_centroid_support_matches(self, n, raw_support):
        """faceCentroid(F)_i ≠ 0 ↔ i ∈ F  [faceCentroid_support]"""
        support = sorted(set(i for i in raw_support if i < n))
        assume(len(support) > 0)
        out = face_centroid_c(support, n)
        support_set = set(support)
        for i, v in enumerate(out):
            if i in support_set:
                assert v > 0, f"active coord {i} is zero"
            else:
                assert v == 0.0, f"inactive coord {i} is nonzero: {v}"

    @given(st.integers(2, 64),
           st.integers(0, 63))
    @settings(max_examples=5_000)
    def test_vertex_centroid_is_indicator(self, n, raw_v):
        """faceCentroid({v})_i = 1 if i==v else 0  [vertex_centroid_eq]"""
        v = raw_v % n
        out = face_centroid_c([v], n)
        assert abs(out[v] - 1.0) < 1e-7, f"vertex {v}: {out[v]} ≠ 1"
        for i, val in enumerate(out):
            if i != v:
                assert val == 0.0, f"non-vertex {i} nonzero: {val}"

    @given(st.lists(st.floats(-5, 5, allow_nan=False, allow_infinity=False),
                    min_size=2, max_size=64))
    @settings(max_examples=10_000)
    def test_softmax_all_positive(self, logits):
        """softmax(z)_i > 0 for all i  [softmax maps to interior of simplex]"""
        out = softmax_c(logits)
        for i, v in enumerate(out):
            assert v > 0, f"softmax[{i}]={v} ≤ 0 on {logits}"


# ─────────────────────────────────────────────────────────────────────────────
# PAPER III — NAND Attention
# Lean theorem: attention_is_pmap
# ─────────────────────────────────────────────────────────────────────────────

def attention_ref(q, k, v):
    """Pure-Python reference: scores_i = Σ q_i*k_j, w=softmax(scores), out_i = Σ w_i*v_j"""
    n = len(q)
    scores = [sum(q[i] * k[j] for j in range(n)) for i in range(n)]
    w = softmax_ref(scores)
    return [sum(w[i] * v[j] for j in range(n)) for i in range(n)]

@skip_no_lib
class TestPaperIII_Attention:

    @given(st.integers(1, 32),
           st.data())
    @settings(max_examples=5_000, phases=[Phase.generate, Phase.shrink])
    def test_attention_matches_reference(self, n, data):
        """sovarr_nand_attention ≡ reference attention  [attention_is_pmap]"""
        flt = st.floats(-5, 5, allow_nan=False, allow_infinity=False)
        q = data.draw(st.lists(flt, min_size=n, max_size=n))
        k = data.draw(st.lists(flt, min_size=n, max_size=n))
        v = data.draw(st.lists(flt, min_size=n, max_size=n))
        c_out  = nand_attention_c(q, k, v)
        py_out = attention_ref(q, k, v)
        for i, (a, b) in enumerate(zip(c_out, py_out)):
            assert abs(a - b) < 1e-3, \
                f"attention divergence at i={i}: C={a:.6f} ref={b:.6f}"

    @given(st.integers(1, 32), st.data())
    @settings(max_examples=2_000)
    def test_attention_output_size(self, n, data):
        """output has same size as input  [shape preservation]"""
        flt = st.floats(-3, 3, allow_nan=False, allow_infinity=False)
        q = data.draw(st.lists(flt, min_size=n, max_size=n))
        k = data.draw(st.lists(flt, min_size=n, max_size=n))
        v = data.draw(st.lists(flt, min_size=n, max_size=n))
        out = nand_attention_c(q, k, v)
        assert len(out) == n

    @given(st.integers(1, 16), st.data())
    @settings(max_examples=2_000)
    def test_attention_uniform_k_is_constant(self, n, data):
        """When k is uniform, all scores are equal → attention weights are uniform."""
        flt  = st.floats(-3, 3, allow_nan=False, allow_infinity=False)
        q    = data.draw(st.lists(flt, min_size=n, max_size=n))
        k    = [1.0] * n   # uniform key
        v    = data.draw(st.lists(flt, min_size=n, max_size=n))
        out  = nand_attention_c(q, k, v)
        # All outputs should be identical (uniform weight over v)
        mean = sum(out) / n
        for i, val in enumerate(out):
            assert abs(val - mean) < 1e-4, \
                f"uniform-k: out[{i}]={val} ≠ mean={mean}"


# ─────────────────────────────────────────────────────────────────────────────
# BROADCAST — pullback semantics
# Lean theorem: broadcast_is_pullback
# ─────────────────────────────────────────────────────────────────────────────

@skip_no_lib
class TestBroadcast:

    @given(st.lists(st.floats(-100, 100, allow_nan=False, allow_infinity=False),
                    min_size=1, max_size=256))
    @settings(max_examples=10_000)
    def test_broadcast_1d_pointwise(self, xs):
        """broadcast_1d(v, w)_i = v_i + w_i  [broadcast_is_pullback, 1D case]"""
        n = len(xs)
        # split into two halves (or use same list for both)
        v_arr = (ctypes.c_float * n)(*xs)
        w_arr = (ctypes.c_float * n)(*xs)
        out   = (ctypes.c_float * n)()
        _lib.sovarr_broadcast_1d.argtypes = [
            ctypes.POINTER(ctypes.c_float),
            ctypes.POINTER(ctypes.c_float),
            ctypes.POINTER(ctypes.c_float),
            ctypes.c_size_t,
        ]
        _lib.sovarr_broadcast_1d.restype = None
        _lib.sovarr_broadcast_1d(v_arr, w_arr, out, n)
        for i, (a, b, o) in enumerate(zip(xs, xs, out)):
            assert abs(o - (a + b)) < 1e-4, f"broadcast[{i}]: {o}{a+b}"


# ─────────────────────────────────────────────────────────────────────────────
# Standalone: run without pytest
# ─────────────────────────────────────────────────────────────────────────────

if __name__ == "__main__":
    import subprocess, sys
    result = subprocess.run(
        [sys.executable, "-m", "pytest", __file__, "-x", "-v", "--tb=short"],
        cwd=pathlib.Path(__file__).parent,
    )
    sys.exit(result.returncode)