Files
nvfp4-megamoe-kernel/test_mapping.py
2026-05-23 20:02:35 +00:00

50 lines
1.5 KiB
Python

#!/usr/bin/env python3
"""
Test SMEM-P coordinate mapping for collisions.
Mapping: (m,n) -> ((m, n%16), 0, ((n//16)%4, n//64), 0)
Offset: m*64 + (n%16) + ((n//16)%4)*16 + (n//64)*8192
"""
def compute_offset(m, n):
n0 = n % 16
n1 = (n // 16) % 4
n2 = n // 64
offset = m * 64 + n0 + n1 * 16 + n2 * 8192
return offset, (m, n0, n1, n2)
# Test bijection
offsets = set()
collisions = []
for m in range(128):
for n in range(128):
offset, _ = compute_offset(m, n)
if offset in offsets:
collisions.append(((m,n), offset))
offsets.add(offset)
print(f"Total positions: {128*128} = {128*128}")
print(f"Unique offsets: {len(offsets)}")
print(f"Collisions: {len(collisions)}")
if collisions:
print("First few collisions:", collisions[:5])
# Check offset range
min_offset = min(offsets)
max_offset = max(offsets)
print(f"Offset range: {min_offset} .. {max_offset}")
print(f"Expected range: 0 .. 16383")
# Test specific coordinates
test_coords = [(0,0), (0,1), (0,15), (0,16), (0,63), (0,64), (0,127),
(1,0), (1,1), (1,127),
(127,0), (127,63), (127,64), (127,127)]
print("\nSample offsets:")
for m,n in test_coords:
offset, decomp = compute_offset(m,n)
print(f"({m},{n}) -> {decomp} = offset {offset}")
# Verify formula matches layout stride
# Layout: ((128,16),1,(4,2),1):((64,1),0,(16,8192),0)
# Stride for (m,n0,n1,n2): m*64 + n0*1 + n1*16 + n2*8192
print("\nLayout stride check: OK" if max_offset == 16383 and len(offsets) == 16384 else "Layout mismatch")