Skip to content

Commit 9797baf

Browse files
authored
feat(igla): Trinity Constants + phi-LR Schedule (Modules 1 & 5) (#59)
Implements IGLA-GF16 Modules 1 and 5 from #3: - Trinity constants (PHI, ALPHA_PHI, TRINITY) - Fibonacci array and architecture constants (d_model=144, d_ffn=233) - Trinity weight init stds (gauge/higgs/lepton/cosmology) - phi-LR schedule with warmup - 6 tests Part of #3
1 parent e2c100d commit 9797baf

2 files changed

Lines changed: 95 additions & 0 deletions

File tree

build.zig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,21 @@ pub fn build(b: *std.Build) void {
111111

112112
const run_tests = b.addRunArtifact(formats_tests);
113113
const run_transcendent_tests = b.addRunArtifact(transcendent_tests);
114+
115+
const trinity_tests_root = b.createModule(.{
116+
.root_source_file = b.path("src/trinity_constants.zig"),
117+
.target = target,
118+
.optimize = optimize,
119+
});
120+
const trinity_tests = b.addTest(.{
121+
.name = "trinity-constants-tests",
122+
.root_module = trinity_tests_root,
123+
});
124+
const run_trinity_tests = b.addRunArtifact(trinity_tests);
125+
114126
const test_step = b.step("test", "Run all tests");
115127
test_step.dependOn(&run_tests.step);
116128
test_step.dependOn(&run_transcendent_tests.step);
117129
test_step.dependOn(&run_c_abi_tests.step);
130+
test_step.dependOn(&run_trinity_tests.step);
118131
}

src/trinity_constants.zig

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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

Comments
 (0)