|
| 1 | +// TERNARY WEIGHT PACKING - I2_S Format (2-bit per weight) |
| 2 | +// Convert F32 weights to pw ternary {-1, 0, +1} |
| 3 | +// Memory savings: 16x (32-bit to 2-bit) |
| 4 | +// phi^2 + 1/phi^2 = 3 = TRINITY | KOSCHEI IS IMMORTAL |
| 5 | + |
| 6 | +const std = @import("std"); |
| 7 | + |
| 8 | +pub const PHI: f64 = 1.618033988749895; |
| 9 | + |
| 10 | +// CONSTANTS |
| 11 | + |
| 12 | +/// Trit encoding: 00=0, 01=+1, 10=-1, 11=reserved |
| 13 | +pub const TRIT_ZERO: u2 = 0b00; |
| 14 | +pub const TRIT_PLUS: u2 = 0b01; |
| 15 | +pub const TRIT_MINUS: u2 = 0b10; |
| 16 | + |
| 17 | +/// Block size for I2_S format (with scale) |
| 18 | +pub const I2S_BLOCK_SIZE: usize = 256; |
| 19 | + |
| 20 | +/// Sign lookup table for decoding |
| 21 | +pub const SIGN_LUT: [4]f32 = .{ 0.0, 1.0, -1.0, 0.0 }; |
| 22 | + |
| 23 | +// WEIGHT QUANTIZATION |
| 24 | + |
| 25 | +/// Quantize F32 weight to ternary {-1, 0, +1} |
| 26 | +pub inline fn quantizeToTrit(value: f32, threshold: f32) u2 { |
| 27 | + if (value > threshold) return TRIT_PLUS; |
| 28 | + if (value < -threshold) return TRIT_MINUS; |
| 29 | + return TRIT_ZERO; |
| 30 | +} |
| 31 | + |
| 32 | +/// Pack 4 trits into a single byte |
| 33 | +pub inline fn pack4Trits(t0: u2, t1: u2, t2: u2, t3: u2) u8 { |
| 34 | + return @as(u8, t0) | (@as(u8, t1) << 2) | (@as(u8, t2) << 4) | (@as(u8, t3) << 6); |
| 35 | +} |
| 36 | + |
| 37 | +/// Unpack 4 trits from a byte |
| 38 | +pub inline fn unpack4Trits(byte: u8) [4]u2 { |
| 39 | + return .{ |
| 40 | + @truncate(byte & 0x3), |
| 41 | + @truncate((byte >> 2) & 0x3), |
| 42 | + @truncate((byte >> 4) & 0x3), |
| 43 | + @truncate((byte >> 6) & 0x3), |
| 44 | + }; |
| 45 | +} |
| 46 | + |
| 47 | +// PACKED TERNARY WEIGHTS |
| 48 | + |
| 49 | +/// Packed ternary weight matrix |
| 50 | +pub const PackedTernaryWeights = struct { |
| 51 | + allocator: std.mem.Allocator, |
| 52 | + data: []u8, |
| 53 | + scales: []f32, |
| 54 | + rows: usize, |
| 55 | + cols: usize, |
| 56 | + |
| 57 | + /// Memory usage in bytes |
| 58 | + pub fn memoryUsage(self: PackedTernaryWeights) usize { |
| 59 | + return self.data.len + self.scales.len * @sizeOf(f32); |
| 60 | + } |
| 61 | + |
| 62 | + /// Memory savings vs F32 |
| 63 | + pub fn memorySavings(self: PackedTernaryWeights) f32 { |
| 64 | + const f32_size = self.rows * self.cols * @sizeOf(f32); |
| 65 | + const pw_size = self.memoryUsage(); |
| 66 | + return @as(f32, @floatFromInt(f32_size)) / @as(f32, @floatFromInt(pw_size)); |
| 67 | + } |
| 68 | + |
| 69 | + pub fn deinit(self: *PackedTernaryWeights) void { |
| 70 | + self.allocator.free(self.data); |
| 71 | + self.allocator.free(self.scales); |
| 72 | + } |
| 73 | +}; |
| 74 | + |
| 75 | +/// Pack F32 weights to ternary format |
| 76 | +pub fn packWeights( |
| 77 | + allocator: std.mem.Allocator, |
| 78 | + weights: []const f32, |
| 79 | + rows: usize, |
| 80 | + cols: usize, |
| 81 | +) !PackedTernaryWeights { |
| 82 | + const cols_pw = (cols + 3) / 4; |
| 83 | + const total_pw = rows * cols_pw; |
| 84 | + |
| 85 | + const data = try allocator.alloc(u8, total_pw); |
| 86 | + const scales = try allocator.alloc(f32, rows); |
| 87 | + |
| 88 | + var row: usize = 0; |
| 89 | + while (row < rows) : (row += 1) { |
| 90 | + const row_start = row * cols; |
| 91 | + const row_weights = weights[row_start..row_start + cols]; |
| 92 | + |
| 93 | + var max_abs: f32 = 0.0; |
| 94 | + for (row_weights) |w| { |
| 95 | + const abs_w = @abs(w); |
| 96 | + if (abs_w > max_abs) max_abs = abs_w; |
| 97 | + } |
| 98 | + |
| 99 | + const threshold = max_abs * 0.5; |
| 100 | + scales[row] = max_abs; |
| 101 | + |
| 102 | + const pw_row_start = row * cols_pw; |
| 103 | + var col: usize = 0; |
| 104 | + var byte_idx: usize = 0; |
| 105 | + |
| 106 | + while (col < cols) { |
| 107 | + const t0 = if (col < cols) quantizeToTrit(row_weights[col], threshold) else TRIT_ZERO; |
| 108 | + const t1 = if (col + 1 < cols) quantizeToTrit(row_weights[col + 1], threshold) else TRIT_ZERO; |
| 109 | + const t2 = if (col + 2 < cols) quantizeToTrit(row_weights[col + 2], threshold) else TRIT_ZERO; |
| 110 | + const t3 = if (col + 3 < cols) quantizeToTrit(row_weights[col + 3], threshold) else TRIT_ZERO; |
| 111 | + |
| 112 | + data[pw_row_start + byte_idx] = pack4Trits(t0, t1, t2, t3); |
| 113 | + |
| 114 | + col += 4; |
| 115 | + byte_idx += 1; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return PackedTernaryWeights{ |
| 120 | + .allocator = allocator, |
| 121 | + .data = data, |
| 122 | + .scales = scales, |
| 123 | + .rows = rows, |
| 124 | + .cols = cols, |
| 125 | + }; |
| 126 | +} |
| 127 | + |
| 128 | +// SIMD TERNARY MATMUL |
| 129 | + |
| 130 | +const Vec8f32 = @Vector(8, f32); |
| 131 | + |
| 132 | +/// Decode 8 trits from 2 bytes to f32 signs |
| 133 | +inline fn decode8TritsF32(byte0: u8, byte1: u8) Vec8f32 { |
| 134 | + return .{ |
| 135 | + SIGN_LUT[(byte0 >> 0) & 0x3], |
| 136 | + SIGN_LUT[(byte0 >> 2) & 0x3], |
| 137 | + SIGN_LUT[(byte0 >> 4) & 0x3], |
| 138 | + SIGN_LUT[(byte0 >> 6) & 0x3], |
| 139 | + SIGN_LUT[(byte1 >> 0) & 0x3], |
| 140 | + SIGN_LUT[(byte1 >> 2) & 0x3], |
| 141 | + SIGN_LUT[(byte1 >> 4) & 0x3], |
| 142 | + SIGN_LUT[(byte1 >> 6) & 0x3], |
| 143 | + }; |
| 144 | +} |
| 145 | + |
| 146 | +/// SIMD ternary matrix-vector multiply |
| 147 | +pub fn ternaryMatVecSIMD( |
| 148 | + output: []f32, |
| 149 | + data: []const u8, |
| 150 | + scales: []const f32, |
| 151 | + input: []const f32, |
| 152 | + rows: usize, |
| 153 | + cols: usize, |
| 154 | +) void { |
| 155 | + const cols_pw = (cols + 3) / 4; |
| 156 | + |
| 157 | + var row: usize = 0; |
| 158 | + while (row < rows) : (row += 1) { |
| 159 | + var sum_vec: Vec8f32 = @splat(0.0); |
| 160 | + var sum_scalar: f32 = 0.0; |
| 161 | + const row_start = row * cols_pw; |
| 162 | + const scale = scales[row]; |
| 163 | + |
| 164 | + var col: usize = 0; |
| 165 | + |
| 166 | + while (col + 8 <= cols) { |
| 167 | + const byte_idx = row_start + col / 4; |
| 168 | + if (byte_idx + 1 >= data.len) break; |
| 169 | + |
| 170 | + const in_vec: Vec8f32 = input[col..][0..8].*; |
| 171 | + const signs = decode8TritsF32(data[byte_idx], data[byte_idx + 1]); |
| 172 | + sum_vec += in_vec * signs; |
| 173 | + col += 8; |
| 174 | + } |
| 175 | + |
| 176 | + sum_scalar = @reduce(.Add, sum_vec); |
| 177 | + |
| 178 | + while (col < cols) : (col += 1) { |
| 179 | + const byte_idx = row_start + col / 4; |
| 180 | + if (byte_idx >= data.len) break; |
| 181 | + |
| 182 | + const shift: u3 = @intCast((col % 4) * 2); |
| 183 | + const trit = (data[byte_idx] >> shift) & 0x3; |
| 184 | + sum_scalar += input[col] * SIGN_LUT[trit]; |
| 185 | + } |
| 186 | + |
| 187 | + output[row] = sum_scalar * scale; |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +// TESTS |
| 192 | + |
| 193 | +test "trit encoding" { |
| 194 | + try std.testing.expectEqual(TRIT_ZERO, quantizeToTrit(0.0, 0.5)); |
| 195 | + try std.testing.expectEqual(TRIT_PLUS, quantizeToTrit(1.0, 0.5)); |
| 196 | + try std.testing.expectEqual(TRIT_MINUS, quantizeToTrit(-1.0, 0.5)); |
| 197 | +} |
| 198 | + |
| 199 | +test "pack and unpack trits" { |
| 200 | + const pw = pack4Trits(TRIT_ZERO, TRIT_PLUS, TRIT_MINUS, TRIT_ZERO); |
| 201 | + const unpw = unpack4Trits(pw); |
| 202 | + |
| 203 | + try std.testing.expectEqual(TRIT_ZERO, unpw[0]); |
| 204 | + try std.testing.expectEqual(TRIT_PLUS, unpw[1]); |
| 205 | + try std.testing.expectEqual(TRIT_MINUS, unpw[2]); |
| 206 | + try std.testing.expectEqual(TRIT_ZERO, unpw[3]); |
| 207 | +} |
| 208 | + |
| 209 | +test "pack weights" { |
| 210 | + const allocator = std.testing.allocator; |
| 211 | + const weights = [_]f32{ 1.0, -1.0, 0.0, 0.5, -0.8, 0.9, -0.3, 0.1 }; |
| 212 | + |
| 213 | + var pw = try packWeights(allocator, &weights, 2, 4); |
| 214 | + defer pw.deinit(); |
| 215 | + |
| 216 | + try std.testing.expectEqual(@as(usize, 2), pw.rows); |
| 217 | + try std.testing.expectEqual(@as(usize, 4), pw.cols); |
| 218 | + |
| 219 | + // Small matrices have high overhead, just check it works |
| 220 | + const savings = pw.memorySavings(); |
| 221 | + try std.testing.expect(savings > 0.5); |
| 222 | +} |
| 223 | + |
| 224 | +test "ternary matmul correctness" { |
| 225 | + const allocator = std.testing.allocator; |
| 226 | + const weights = [_]f32{ 1.0, -1.0, 0.0, 1.0, -1.0, 1.0, -1.0, 0.0 }; |
| 227 | + const input = [_]f32{ 1.0, 2.0, 3.0, 4.0 }; |
| 228 | + var output: [2]f32 = undefined; |
| 229 | + |
| 230 | + var pw = try packWeights(allocator, &weights, 2, 4); |
| 231 | + defer pw.deinit(); |
| 232 | + |
| 233 | + ternaryMatVecSIMD(&output, pw.data, pw.scales, &input, pw.rows, pw.cols); |
| 234 | + |
| 235 | + try std.testing.expect(@abs(output[0]) > 0.0); |
| 236 | + try std.testing.expect(@abs(output[1]) > 0.0); |
| 237 | +} |
| 238 | + |
| 239 | +test "memory savings for 1536x1536 matrix" { |
| 240 | + const allocator = std.testing.allocator; |
| 241 | + |
| 242 | + // Typical hidden size matrix |
| 243 | + const rows: usize = 1536; |
| 244 | + const cols: usize = 1536; |
| 245 | + const weights = try allocator.alloc(f32, rows * cols); |
| 246 | + defer allocator.free(weights); |
| 247 | + |
| 248 | + // Fill with random-ish values |
| 249 | + for (weights, 0..) |*w, i| { |
| 250 | + w.* = @as(f32, @floatFromInt(i % 3)) - 1.0; // -1, 0, 1 |
| 251 | + } |
| 252 | + |
| 253 | + var pw = try packWeights(allocator, weights, rows, cols); |
| 254 | + defer pw.deinit(); |
| 255 | + |
| 256 | + const f32_size = rows * cols * @sizeOf(f32); |
| 257 | + const pw_size = pw.memoryUsage(); |
| 258 | + const savings = pw.memorySavings(); |
| 259 | + |
| 260 | + std.debug.print("\n=== Memory Savings Test (1536x1536) ===\n", .{}); |
| 261 | + std.debug.print("F32 size: {d} bytes ({d:.2} MB)\n", .{ f32_size, @as(f32, @floatFromInt(f32_size)) / 1024.0 / 1024.0 }); |
| 262 | + std.debug.print("Packed size: {d} bytes ({d:.2} MB)\n", .{ pw_size, @as(f32, @floatFromInt(pw_size)) / 1024.0 / 1024.0 }); |
| 263 | + std.debug.print("Savings: {d:.1}x\n", .{savings}); |
| 264 | + |
| 265 | + // Should be ~13x savings (32-bit to 2-bit + scale overhead) |
| 266 | + try std.testing.expect(savings > 10.0); |
| 267 | +} |
0 commit comments