|
| 1 | +"""Test shared memory ops: tt.sync, tt.shared_store, tt.shared_load. |
| 2 | +
|
| 3 | +Verifies: |
| 4 | + - Basic shared memory round-trip: store then load |
| 5 | + - Tiled 2-row-per-block matvec using shared memory for x vector reuse |
| 6 | + - Correctness against NumPy for various matrix sizes |
| 7 | +""" |
| 8 | + |
| 9 | +import sys |
| 10 | +import os |
| 11 | +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'python')) |
| 12 | + |
| 13 | +import numpy as np |
| 14 | +import tiny_ton as tt |
| 15 | + |
| 16 | + |
| 17 | +def compare(name, got, expected, atol=1e-4): |
| 18 | + ok = np.allclose(got, expected, atol=atol) |
| 19 | + print(f' {name}: {"PASS" if ok else "FAIL"}') |
| 20 | + if not ok: |
| 21 | + print(f' got: {got}') |
| 22 | + print(f' expected: {expected}') |
| 23 | + return ok |
| 24 | + |
| 25 | + |
| 26 | +# --------------------------------------------------------------------------- |
| 27 | +# Kernel 1: shared memory round-trip (store → sync → load) |
| 28 | +# --------------------------------------------------------------------------- |
| 29 | + |
| 30 | +@tt.jit |
| 31 | +def shmem_roundtrip(src, dst, N, BLOCK: tt.constexpr): |
| 32 | + tid = tt.arange(0, BLOCK) |
| 33 | + mask = tid < N |
| 34 | + val = tt.load(src + tid, mask=mask) |
| 35 | + tt.shared_store(tid, val) |
| 36 | + tt.sync() |
| 37 | + out = tt.shared_load(tid) |
| 38 | + tt.store(dst + tid, out, mask=mask) |
| 39 | + |
| 40 | + |
| 41 | +# --------------------------------------------------------------------------- |
| 42 | +# Kernel 2: single-row linear (baseline, no shared memory) |
| 43 | +# --------------------------------------------------------------------------- |
| 44 | + |
| 45 | +@tt.jit |
| 46 | +def linear_kernel(W_ptr, x_ptr, y_ptr, in_features, BLOCK: tt.constexpr): |
| 47 | + pid = tt.program_id(0) |
| 48 | + tid = tt.arange(0, BLOCK) |
| 49 | + mask = tid < in_features |
| 50 | + w = tt.load(W_ptr + pid * in_features + tid, mask=mask) |
| 51 | + x = tt.load(x_ptr + tid, mask=mask) |
| 52 | + dot = tt.reduce_sum(w * x) |
| 53 | + tt.store(y_ptr + pid, dot) |
| 54 | + |
| 55 | + |
| 56 | +# --------------------------------------------------------------------------- |
| 57 | +# Kernel 3: tiled 2-row-per-block linear (shared memory for x reuse) |
| 58 | +# --------------------------------------------------------------------------- |
| 59 | + |
| 60 | +@tt.jit |
| 61 | +def tiled_linear_kernel(W_ptr, x_ptr, y_ptr, in_features, BLOCK: tt.constexpr): |
| 62 | + pid = tt.program_id(0) |
| 63 | + tid = tt.arange(0, BLOCK) |
| 64 | + mask = tid < in_features |
| 65 | + |
| 66 | + x_val = tt.load(x_ptr + tid, mask=mask) |
| 67 | + tt.shared_store(tid, x_val) |
| 68 | + tt.sync() |
| 69 | + x_sh = tt.shared_load(tid) |
| 70 | + |
| 71 | + w0 = tt.load(W_ptr + (pid * 2) * in_features + tid, mask=mask) |
| 72 | + w1 = tt.load(W_ptr + (pid * 2 + 1) * in_features + tid, mask=mask) |
| 73 | + dot0 = tt.reduce_sum(w0 * x_sh) |
| 74 | + dot1 = tt.reduce_sum(w1 * x_sh) |
| 75 | + tt.store(y_ptr + pid * 2, dot0) |
| 76 | + tt.store(y_ptr + pid * 2 + 1, dot1) |
| 77 | + |
| 78 | + |
| 79 | +# --------------------------------------------------------------------------- |
| 80 | +# Tests |
| 81 | +# --------------------------------------------------------------------------- |
| 82 | + |
| 83 | +def test_shmem_roundtrip(): |
| 84 | + print('--- shared memory round-trip ---') |
| 85 | + all_ok = True |
| 86 | + for N in [4, 16, 27]: |
| 87 | + x = np.random.randn(N).astype(np.float32) |
| 88 | + out = np.zeros(N, dtype=np.float32) |
| 89 | + shmem_roundtrip[(1,)](x.copy(), out, N, N) |
| 90 | + ok = compare(f'N={N}', out, x) |
| 91 | + all_ok = all_ok and ok |
| 92 | + return all_ok |
| 93 | + |
| 94 | + |
| 95 | +def test_single_row_linear(): |
| 96 | + print('--- single-row linear (baseline) ---') |
| 97 | + all_ok = True |
| 98 | + for out_features, in_features in [(4, 4), (8, 16), (6, 27)]: |
| 99 | + W = np.random.randn(out_features, in_features).astype(np.float32) |
| 100 | + x = np.random.randn(in_features).astype(np.float32) |
| 101 | + expected = W @ x |
| 102 | + y = np.zeros(out_features, dtype=np.float32) |
| 103 | + BLOCK = max(in_features, 4) |
| 104 | + linear_kernel[(out_features,)]( |
| 105 | + W.flatten().copy(), x.copy(), y, in_features, BLOCK) |
| 106 | + ok = compare(f'{out_features}x{in_features}', y, expected) |
| 107 | + all_ok = all_ok and ok |
| 108 | + return all_ok |
| 109 | + |
| 110 | + |
| 111 | +def test_tiled_linear(): |
| 112 | + print('--- tiled 2-row-per-block linear (shared memory) ---') |
| 113 | + all_ok = True |
| 114 | + for out_features, in_features in [(4, 4), (8, 16), (6, 27)]: |
| 115 | + W = np.random.randn(out_features, in_features).astype(np.float32) |
| 116 | + x = np.random.randn(in_features).astype(np.float32) |
| 117 | + expected = W @ x |
| 118 | + y = np.zeros(out_features, dtype=np.float32) |
| 119 | + BLOCK = max(in_features, 4) |
| 120 | + n_blocks = out_features // 2 |
| 121 | + tiled_linear_kernel[(n_blocks,)]( |
| 122 | + W.flatten().copy(), x.copy(), y, in_features, BLOCK) |
| 123 | + ok = compare(f'{out_features}x{in_features} tiled', y, expected) |
| 124 | + all_ok = all_ok and ok |
| 125 | + return all_ok |
| 126 | + |
| 127 | + |
| 128 | +def test_tiled_vs_baseline(): |
| 129 | + print('--- tiled vs baseline match ---') |
| 130 | + W = np.random.randn(8, 16).astype(np.float32) |
| 131 | + x = np.random.randn(16).astype(np.float32) |
| 132 | + |
| 133 | + y_baseline = np.zeros(8, dtype=np.float32) |
| 134 | + linear_kernel[(8,)](W.flatten().copy(), x.copy(), y_baseline, 16, 16) |
| 135 | + |
| 136 | + y_tiled = np.zeros(8, dtype=np.float32) |
| 137 | + tiled_linear_kernel[(4,)](W.flatten().copy(), x.copy(), y_tiled, 16, 16) |
| 138 | + |
| 139 | + return compare('8x16 baseline==tiled', y_tiled, y_baseline) |
| 140 | + |
| 141 | + |
| 142 | +if __name__ == '__main__': |
| 143 | + np.random.seed(42) |
| 144 | + results = [ |
| 145 | + test_shmem_roundtrip(), |
| 146 | + test_single_row_linear(), |
| 147 | + test_tiled_linear(), |
| 148 | + test_tiled_vs_baseline(), |
| 149 | + ] |
| 150 | + print() |
| 151 | + if all(results): |
| 152 | + print('All tests PASSED') |
| 153 | + else: |
| 154 | + print('SOME TESTS FAILED') |
| 155 | + sys.exit(1) |
0 commit comments