Initial RPS CUDA implementation
This commit is contained in:
commit
8d583036d0
33 changed files with 2968 additions and 0 deletions
149
tests/verify_rps_mvp_math.py
Normal file
149
tests/verify_rps_mvp_math.py
Normal file
|
|
@ -0,0 +1,149 @@
|
|||
#!/usr/bin/env python3
|
||||
"""Independent scalar verification of RPS CUDA MVP mathematical contracts.
|
||||
No NVIDIA/CUDA, OpenSSL C++ or macOS dependencies; Python 3.10+.
|
||||
This validates *math* only; it does not claim to validate the actual CUDA binary.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
import hashlib
|
||||
from itertools import product
|
||||
from random import Random
|
||||
|
||||
MASK32 = (1 << 32) - 1
|
||||
ORDERS = ('RPS', 'RSP', 'PRS', 'PSR', 'SRP', 'SPR')
|
||||
LUTS = ((0xF2, 0x0E), (0xFA, 0x0E), (0x32, 0xCE),
|
||||
(0x32, 0xEE), (0xFA, 0xAE), (0xBA, 0xEE))
|
||||
COLORS = ((0,0,0), (230,70,70), (70,150,230), (80,210,120))
|
||||
|
||||
|
||||
def lop3_bit(a: int, b: int, c: int, lut: int) -> int:
|
||||
return (lut >> ((a << 2) | (b << 1) | c)) & 1
|
||||
|
||||
|
||||
def scalar_step(code: int, nr: int, np: int, ns: int, priority: int) -> int:
|
||||
if code == 1:
|
||||
return 2 if np else 1
|
||||
if code == 2:
|
||||
return 3 if ns else 2
|
||||
if code == 3:
|
||||
return 1 if nr else 3
|
||||
present = {'R': nr, 'P': np, 'S': ns}
|
||||
for kind in ORDERS[priority]:
|
||||
if present[kind]:
|
||||
return {'R': 1, 'P': 2, 'S': 3}[kind]
|
||||
return 0
|
||||
|
||||
|
||||
def bit_logic_step(code: int, nr: int, np: int, ns: int, priority: int) -> int:
|
||||
a = code & 1
|
||||
b = (code >> 1) & 1
|
||||
x = lop3_bit(b, nr, np, 0xCA)
|
||||
w = lop3_bit(a, x, ns, 0xCA)
|
||||
ao = lop3_bit(a, b, w, 0xD8)
|
||||
bo = lop3_bit(a, b, w, 0x6C)
|
||||
lut_a, lut_b = LUTS[priority]
|
||||
fa = lop3_bit(nr, np, ns, lut_a)
|
||||
fb = lop3_bit(nr, np, ns, lut_b)
|
||||
e = lop3_bit(a, b, w, 0x03)
|
||||
an = lop3_bit(ao, e, fa, 0xF8)
|
||||
bn = lop3_bit(bo, e, fb, 0xF8)
|
||||
return an | (bn << 1)
|
||||
|
||||
|
||||
def test_luts() -> None:
|
||||
n = 0
|
||||
for code, nr, np, ns, priority in product(range(4), range(2), range(2), range(2), range(6)):
|
||||
assert bit_logic_step(code, nr, np, ns, priority) == scalar_step(code, nr, np, ns, priority)
|
||||
n += 1
|
||||
assert n == 192
|
||||
print('PASS: all 192 transition/LUT combinations')
|
||||
|
||||
|
||||
def test_shf() -> None:
|
||||
r = Random(20261118)
|
||||
for _ in range(10000):
|
||||
prev, center, nxt = (r.getrandbits(32) for _ in range(3))
|
||||
left = ((center << 1) | (prev >> 31)) & MASK32
|
||||
right = ((center >> 1) | (nxt << 31)) & MASK32
|
||||
# Scalar expected: left neighbor for local bit k is center[k-1],
|
||||
# except k=0, which is previous[31]. Right is symmetric.
|
||||
for k in range(32):
|
||||
exp_l = ((prev >> 31) & 1) if k == 0 else ((center >> (k-1)) & 1)
|
||||
exp_r = (nxt & 1) if k == 31 else ((center >> (k+1)) & 1)
|
||||
assert ((left >> k) & 1) == exp_l
|
||||
assert ((right >> k) & 1) == exp_r
|
||||
print('PASS: 10,000 pairs of SHF neighborhood boundaries (32 bits each)')
|
||||
|
||||
|
||||
def test_priority() -> None:
|
||||
vectors = {
|
||||
0: ('6674f8e1bf06b711', 5),
|
||||
1: ('6dfadca188a614c0', 0),
|
||||
2: ('c7e589e42b2fc13d', 3),
|
||||
4: ('6db8b412fa32d5d4', 4),
|
||||
5: ('32deeebde34fa78f', 1),
|
||||
8: ('2873390fd319c916', 2),
|
||||
42: ('7c62b6526f730efa', 4),
|
||||
12345: ('afab81b606544998', 0),
|
||||
(1 << 64)-1: ('88e38984a5df2ff7', 3),
|
||||
}
|
||||
prefix = b'RPS-PRIORITY-v1\x00'
|
||||
assert len(prefix) == 16
|
||||
for seed, (hex64, rem) in vectors.items():
|
||||
msg = prefix + seed.to_bytes(8, 'big')
|
||||
assert len(msg) == 24
|
||||
actual = hashlib.sha256(msg).digest()
|
||||
assert actual[:8].hex() == hex64
|
||||
assert int.from_bytes(actual[:8], 'big') % 6 == rem
|
||||
print(f'PASS: {len(vectors)} SHA-256 / priority vectors')
|
||||
|
||||
|
||||
def mix32(x: int) -> int:
|
||||
x &= MASK32
|
||||
x ^= x >> 16
|
||||
x = (x * 0x7FEB352D) & MASK32
|
||||
x ^= x >> 15
|
||||
x = (x * 0x846CA68B) & MASK32
|
||||
x ^= x >> 16
|
||||
return x
|
||||
|
||||
|
||||
def init_word(seed: int, i: int) -> tuple[int,int]:
|
||||
sl, sh = seed & MASK32, (seed >> 32) & MASK32
|
||||
lo = mix32(sl ^ i ^ ((sh * 0x9E3779B9) & MASK32) ^ 0xA341316C)
|
||||
hi = mix32(sh ^ i ^ ((sl * 0x85EBCA6B) & MASK32) ^ 0xC8013EA4)
|
||||
return lo, hi
|
||||
|
||||
|
||||
def test_init() -> None:
|
||||
assert init_word(42, 0) == init_word(42, 0)
|
||||
assert init_word(42, 0) != init_word(42, 1)
|
||||
assert init_word(0, 0) != init_word(42, 0)
|
||||
print('PASS: deterministic init-v1 smoke checks')
|
||||
for seed in (0, 42, (1 << 64)-1):
|
||||
print(f' GOLDEN init-v1 seed={seed} word[0] low/high = '
|
||||
f'{init_word(seed, 0)[0]:08x}/{init_word(seed, 0)[1]:08x}')
|
||||
|
||||
|
||||
def test_aggregation() -> None:
|
||||
for k in (1,2,4,8,16):
|
||||
for counts in ((k*k,0,0,0),(0,k*k,0,0),(0,0,k*k,0),(0,0,0,k*k)):
|
||||
a = k*k
|
||||
rgb = tuple((sum(counts[s] * COLORS[s][channel] for s in range(4)) + a//2)//a
|
||||
for channel in range(3))
|
||||
nonzero = next(i for i,c in enumerate(counts) if c)
|
||||
assert rgb == COLORS[nonzero]
|
||||
# 2x2: one pixel each kind. R/G/B sums: (380,430,420); round +2 before /4.
|
||||
counts = (1,1,1,1)
|
||||
r,g,b = tuple((sum(counts[s]*COLORS[s][channel] for s in range(4))+2)//4 for channel in range(3))
|
||||
assert (r,g,b) == (95,108,105)
|
||||
assert (b,g,r,255) == (105,108,95,255)
|
||||
print('PASS: integer aggregation and BGRA color order')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_luts()
|
||||
test_shf()
|
||||
test_priority()
|
||||
test_init()
|
||||
test_aggregation()
|
||||
print('PASS: standalone mathematical verifier (not CUDA execution)')
|
||||
Loading…
Add table
Add a link
Reference in a new issue