|
| 1 | +import numpy as np |
| 2 | +import pytfmbs |
| 3 | +import pytest |
| 4 | + |
| 5 | +def pack_pt5(trits): |
| 6 | + packed = [] |
| 7 | + padding = (5 - (len(trits) % 5)) % 5 |
| 8 | + trits_padded = np.append(trits, [0] * padding) |
| 9 | + for i in range(0, len(trits_padded), 5): |
| 10 | + chunk = trits_padded[i:i+5] |
| 11 | + val = 0 |
| 12 | + for j, t in enumerate(chunk): |
| 13 | + ut = 1 if t == 1 else 2 if t == -1 else 0 |
| 14 | + val += ut * (3**j) |
| 15 | + packed.append(val) |
| 16 | + while len(packed) % 4 != 0: |
| 17 | + packed.append(0) |
| 18 | + return bytes(packed) |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def fabric(): |
| 22 | + return pytfmbs.Fabric() |
| 23 | + |
| 24 | +def test_conv3d(fabric): |
| 25 | + depth = 5 |
| 26 | + lanes = 15 |
| 27 | + stride = 1 |
| 28 | + conv_stride_val = 2 # (exec_hints >> 20) & 0x3 + 1 -> 1 + 1 = 2 |
| 29 | + |
| 30 | + weights = np.random.choice([-1, 0, 1], (depth, lanes)) |
| 31 | + inputs = np.random.choice([-1, 0, 1], (1024, lanes)) |
| 32 | + |
| 33 | + w_packed = b"".join([pack_pt5(weights[d]) for d in range(depth)]) |
| 34 | + i_packed = b"".join([pack_pt5(inputs[d]) for d in range(1024)]) |
| 35 | + |
| 36 | + fabric.load(0x1000, w_packed) |
| 37 | + fabric.load(0x2000, i_packed) |
| 38 | + |
| 39 | + # TFMBS_KERNEL_CONV3D = 0x07 |
| 40 | + # conv_stride bits (21:20) = 1 (means conv_stride=2) |
| 41 | + exec_hints = 0x07 | (1 << 20) |
| 42 | + |
| 43 | + tfd = { |
| 44 | + "base_addr": 0x1000, |
| 45 | + "frame_len": depth, |
| 46 | + "lane_count": lanes, |
| 47 | + "exec_hints": exec_hints, |
| 48 | + "tile_mask": 0x1 |
| 49 | + } |
| 50 | + |
| 51 | + fabric.run(tfd) |
| 52 | + fabric_results = np.array(fabric.results(0)) |
| 53 | + |
| 54 | + # Reference calculation |
| 55 | + expected = np.zeros(lanes, dtype=int) |
| 56 | + for d in range(depth): |
| 57 | + idx = d * stride * conv_stride_val * conv_stride_val |
| 58 | + expected += weights[d] * inputs[idx] |
| 59 | + |
| 60 | + assert np.array_equal(fabric_results, expected) |
| 61 | + |
| 62 | +def test_lstm_with_bias_persistence(fabric): |
| 63 | + # Test that BIAS_EN prevents clearing the accumulator |
| 64 | + depth = 10 |
| 65 | + lanes = 15 |
| 66 | + |
| 67 | + weights1 = np.random.choice([-1, 0, 1], (depth, lanes)) |
| 68 | + inputs1 = np.random.choice([-1, 0, 1], (depth, lanes)) |
| 69 | + |
| 70 | + w1_packed = b"".join([pack_pt5(weights1[d]) for d in range(depth)]) |
| 71 | + i1_packed = b"".join([pack_pt5(inputs1[d]) for d in range(depth)]) |
| 72 | + |
| 73 | + fabric.load(0x1000, w1_packed) |
| 74 | + fabric.load(0x2000, i1_packed) |
| 75 | + |
| 76 | + # TFMBS_KERNEL_LSTM = 0x08 |
| 77 | + tfd1 = { |
| 78 | + "base_addr": 0x1000, |
| 79 | + "frame_len": depth, |
| 80 | + "lane_count": lanes, |
| 81 | + "exec_hints": 0x08, # No BIAS_EN, should clear |
| 82 | + "tile_mask": 0x1 |
| 83 | + } |
| 84 | + |
| 85 | + fabric.run(tfd1) |
| 86 | + res1 = np.array(fabric.results(0)) |
| 87 | + |
| 88 | + expected1 = np.sum(weights1 * inputs1, axis=0) |
| 89 | + assert np.array_equal(res1, expected1) |
| 90 | + |
| 91 | + # Second run WITH BIAS_EN |
| 92 | + weights2 = np.random.choice([-1, 0, 1], (depth, lanes)) |
| 93 | + inputs2 = np.random.choice([-1, 0, 1], (depth, lanes)) |
| 94 | + |
| 95 | + w2_packed = b"".join([pack_pt5(weights2[d]) for d in range(depth)]) |
| 96 | + i2_packed = b"".join([pack_pt5(inputs2[d]) for d in range(depth)]) |
| 97 | + |
| 98 | + fabric.load(0x1000, w2_packed) |
| 99 | + fabric.load(0x2000, i2_packed) |
| 100 | + |
| 101 | + # TFMBS_HINT_BIAS_EN = 0x10000 |
| 102 | + tfd2 = { |
| 103 | + "base_addr": 0x1000, |
| 104 | + "frame_len": depth, |
| 105 | + "lane_count": lanes, |
| 106 | + "exec_hints": 0x08 | 0x10000, # BIAS_EN set |
| 107 | + "tile_mask": 0x1 |
| 108 | + } |
| 109 | + |
| 110 | + fabric.run(tfd2) |
| 111 | + res2 = np.array(fabric.results(0)) |
| 112 | + |
| 113 | + expected2 = expected1 + np.sum(weights2 * inputs2, axis=0) |
| 114 | + assert np.array_equal(res2, expected2) |
| 115 | + |
| 116 | +def test_attn_basic(fabric): |
| 117 | + # Basic check that ATTN kernel works (currently same as DOT in mock) |
| 118 | + depth = 8 |
| 119 | + lanes = 15 |
| 120 | + |
| 121 | + weights = np.random.choice([-1, 0, 1], (depth, lanes)) |
| 122 | + inputs = np.random.choice([-1, 0, 1], (depth, lanes)) |
| 123 | + |
| 124 | + w_packed = b"".join([pack_pt5(weights[d]) for d in range(depth)]) |
| 125 | + i_packed = b"".join([pack_pt5(inputs[d]) for d in range(depth)]) |
| 126 | + |
| 127 | + fabric.load(0x1000, w_packed) |
| 128 | + fabric.load(0x2000, i_packed) |
| 129 | + |
| 130 | + # TFMBS_KERNEL_ATTN = 0x09 |
| 131 | + tfd = { |
| 132 | + "base_addr": 0x1000, |
| 133 | + "frame_len": depth, |
| 134 | + "lane_count": lanes, |
| 135 | + "exec_hints": 0x09, |
| 136 | + "tile_mask": 0x1 |
| 137 | + } |
| 138 | + |
| 139 | + fabric.run(tfd) |
| 140 | + res = np.array(fabric.results(0)) |
| 141 | + expected = np.sum(weights * inputs, axis=0) |
| 142 | + assert np.array_equal(res, expected) |
0 commit comments