|
| 1 | +const std = @import("std"); |
| 2 | + |
| 3 | +pub const PHI: f64 = 1.6180339887498948482; |
| 4 | +pub const PHI_SQ: f64 = PHI * PHI; |
| 5 | +pub const PHI_INV: f64 = 1.0 / PHI; |
| 6 | +pub const PHI_INV_SQ: f64 = 1.0 / PHI_SQ; |
| 7 | +pub const TRINITY: f64 = PHI_SQ + PHI_INV_SQ; |
| 8 | + |
| 9 | +pub const ALPHA_PHI: f64 = PHI - 1.5; |
| 10 | + |
| 11 | +pub const FIBONACCI = [_]u32{ |
| 12 | + 1, 1, 2, 3, 5, 8, 13, 21, |
| 13 | + 34, 55, 89, 144, 233, 377, 610, 987, |
| 14 | +}; |
| 15 | + |
| 16 | +pub const D_MODEL: u32 = 144; |
| 17 | +pub const N_HEADS: u32 = 8; |
| 18 | +pub const D_HEAD: u32 = D_MODEL / N_HEADS; |
| 19 | +pub const D_FFN: u32 = 233; |
| 20 | +pub const N_LAYERS: u32 = 7; |
| 21 | +pub const VOCAB: u32 = 50257; |
| 22 | + |
| 23 | +pub const GAUGE_INIT_STD: f64 = ALPHA_PHI; |
| 24 | +pub const HIGGS_INIT_STD: f64 = ALPHA_PHI * PHI_INV; |
| 25 | +pub const LEPTON_INIT_STD: f64 = ALPHA_PHI * PHI_INV_SQ; |
| 26 | +pub const COSMOLOGY_INIT_STD: f64 = ALPHA_PHI * PHI_INV * PHI_INV_SQ; |
| 27 | + |
| 28 | +pub const LR_INIT: f64 = ALPHA_PHI; |
| 29 | +pub const LR_WARMUP_STEPS: u32 = 21; |
| 30 | +pub const LR_TAU: f64 = 228.9; |
| 31 | + |
| 32 | +pub fn phiLrSchedule(step: u32, total_steps: u32) f64 { |
| 33 | + if (step < LR_WARMUP_STEPS) { |
| 34 | + return LR_INIT * @as(f64, @floatFromInt(step)) / @as(f64, @floatFromInt(LR_WARMUP_STEPS)); |
| 35 | + } |
| 36 | + const t = @as(f64, @floatFromInt(step)) / @as(f64, @floatFromInt(total_steps)); |
| 37 | + return LR_INIT * std.math.pow(f64, PHI, -t / LR_TAU * total_steps / LR_TAU); |
| 38 | +} |
| 39 | + |
| 40 | +pub fn trinityInitStd(layer_kind: enum { gauge, higgs, lepton, cosmology }) f64 { |
| 41 | + return switch (layer_kind) { |
| 42 | + .gauge => GAUGE_INIT_STD, |
| 43 | + .higgs => HIGGS_INIT_STD, |
| 44 | + .lepton => LEPTON_INIT_STD, |
| 45 | + .cosmology => COSMOLOGY_INIT_STD, |
| 46 | + }; |
| 47 | +} |
| 48 | + |
| 49 | +test "Trinity Identity: PHI^2 + PHI^(-2) = 3" { |
| 50 | + try std.testing.expectApproxEqAbs(@as(f64, 3.0), TRINITY, 1e-12); |
| 51 | +} |
| 52 | + |
| 53 | +test "ALPHA_PHI = PHI - 1.5 = 0.118034" { |
| 54 | + try std.testing.expectApproxEqAbs(@as(f64, 0.118033988749895), ALPHA_PHI, 1e-12); |
| 55 | +} |
| 56 | + |
| 57 | +test "Fibonacci: 144 * PHI = 233" { |
| 58 | + const result = @as(f64, @floatFromInt(FIBONACCI[11])) * PHI; |
| 59 | + try std.testing.expectApproxEqAbs(@as(f64, 233.0), result, 0.1); |
| 60 | +} |
| 61 | + |
| 62 | +test "Architecture: d_model=144, n_heads=8, d_head=18" { |
| 63 | + try std.testing.expectEqual(@as(u32, 144), D_MODEL); |
| 64 | + try std.testing.expectEqual(@as(u32, 8), N_HEADS); |
| 65 | + try std.testing.expectEqual(@as(u32, 18), D_HEAD); |
| 66 | + try std.testing.expectEqual(@as(u32, 233), D_FFN); |
| 67 | +} |
| 68 | + |
| 69 | +test "Trinity init stds decrease by 1/PHI" { |
| 70 | + try std.testing.expect(GAUGE_INIT_STD > HIGGS_INIT_STD); |
| 71 | + try std.testing.expect(HIGGS_INIT_STD > LEPTON_INIT_STD); |
| 72 | + try std.testing.expect(LEPTON_INIT_STD > COSMOLOGY_INIT_STD); |
| 73 | + const ratio = GAUGE_INIT_STD / HIGGS_INIT_STD; |
| 74 | + try std.testing.expectApproxEqAbs(PHI, ratio, 1e-10); |
| 75 | +} |
| 76 | + |
| 77 | +test "LR schedule: warmup then decay" { |
| 78 | + const lr_0 = phiLrSchedule(0, 10000); |
| 79 | + try std.testing.expect(lr_0 < LR_INIT); |
| 80 | + const lr_warmup = phiLrSchedule(LR_WARMUP_STEPS, 10000); |
| 81 | + try std.testing.expectApproxEqAbs(LR_INIT, lr_warmup, 1e-10); |
| 82 | +} |
0 commit comments