interplane.Get Omega
Read in your language

aien-train

AIEN training programme: language-model history, GB10 constraints and the route to training on the project engine. Plans and runnable checks are distinguished.

Brought by drakestapleton

branch main Branches Tags History

aien-train / native / omegallm / math.om

186 lines · 5050 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note omegallm math.om (aien-train#52): first-party f64 exp and log, splitmix64, FNV-1a 64,
3note little-endian byte packing and the stdout writer. First unit; see docs/omegallm/README.md.
4note Constants (Wolfram, 2026-11-01):
5note splitmix64: IntegerString[11400714819323198485,16] -> 9e3779b97f4a7c15,
6note IntegerString[13787848793156543929,16] -> bf58476d1ce4e5b9,
7note IntegerString[10723151780598845931,16] -> 94d049bb133111eb.
8note FNV-1a 64: IntegerString[14695981039346656037,16] -> cbf29ce484222325, prime 100000001b3.
9note 1/ln2: N[1/Log[2]] bits 4609176140021203710 -> 1.4426950408889634.
10note ln2 hi = 0x3FE62E42FEE00000 (low 21 bits zero, so k*hi is exact for |k| < 2^20)
11note = 0.6931471803691238; lo = N[Log[2]-hi] -> 1.9082149292705877*^-10.
12note overflow: N[Log[$MaxMachineNumber]] -> 709.782712893384; underflow to 0 below
13note N[Log[2^-1075]] -> -745.1332191019412.
14note 1/k! for k=2..13 and 1/(2k+1) for k=1..11: Table[ToString[N[1/k!],InputForm],{k,2,13}],
15note Table[ToString[N[1/(2k+1)],InputForm],{k,1,11}] (values below).
16note Taylor remainder of exp at |r| <= ln2/2: N[0.3466^14/14!] -> 4.14*^-18 (degree 13).
17note atanh series remainder at s <= 3-2Sqrt[2]: N[(3-2Sqrt[2])^2]^11/23 -> 6.25*^-19 (to s^23).
18note bit patterns: 2^52 = 4503599627370496, +inf 9218868437227405312, -inf 18442240474082181120,
19note NaN 9221120237041090560, exponent 1023 field 4607182418800017408, 2^54 = 18014398509481984.
20
21const OMOUT ulong 4096
22global oout uchar 4096
23global oon ulong 1
24note og: run geometry shared by all units (slots listed in model.om).
25global og ulong 64
26
27fn smx x ulong to ulong
28z = add x 11400714819323198485
29z = mul xor z shr z 30 13787848793156543929
30z = mul xor z shr z 27 10723151780598845931
31ret xor z shr z 31
32end
33
34note e^x for every f64 x. Cody-Waite reduction x = k ln2 + r with |r| <= ln2/2, then the Taylor
35note polynomial of degree 13 in Horner form, then scaling by 2^k1 * 2^k2 (k1 = k/2) so every
36note intermediate is normal and a subnormal result is rounded once.
37fn fexp x f64 to f64
38if ne x x
39ret x
40end
41if gt x 709.782712893384
42ret bitcast f64 ulong 9218868437227405312
43end
44if lt x -745.1332191019412
45ret 0.0f64
46end
47kf = mul x 1.4426950408889634
48if ge kf 0.0
49kf = add kf 0.5
50else
51kf = sub kf 0.5
52end
53k = long kf
54fk = f64 k
55r = sub sub x mul fk 0.6931471803691238 mul fk 1.9082149292705877e-10
56p = add mul r 1.6059043836821613e-10 2.08767569878681e-9
57p = add mul p r 2.505210838544172e-8
58p = add mul p r 2.755731922398589e-7
59p = add mul p r 2.7557319223985893e-6
60p = add mul p r 0.0000248015873015873
61p = add mul p r 0.0001984126984126984
62p = add mul p r 0.001388888888888889
63p = add mul p r 0.008333333333333333
64p = add mul p r 0.041666666666666664
65p = add mul p r 0.16666666666666666
66p = add mul p r 0.5
67p = add mul p r 1.0
68p = add mul p r 1.0
69k1 = div k 2
70k2 = sub k k1
71s1 = bitcast f64 shl ulong add k1 1023 52
72s2 = bitcast f64 shl ulong add k2 1023 52
73ret mul mul p s1 s2
74end
75
76note natural log for every f64 x: NaN for x < 0 or NaN, -inf for 0, +inf for +inf. x = m 2^e with
77note m in [sqrt(1/2), sqrt 2]; log m = 2 atanh(s), s = (m-1)/(m+1), series to s^23.
78fn flog x f64 to f64
79if ne x x
80ret x
81end
82if lt x 0.0
83ret bitcast f64 ulong 9221120237041090560
84end
85if eq x 0.0
86ret bitcast f64 ulong 18442240474082181120
87end
88b = bitcast ulong x
89if eq b 9218868437227405312
90ret x
91end
92e = long 0
93if lt b 4503599627370496
94b = bitcast ulong mul x 18014398509481984.0
95e = long -54
96end
97e = add e sub long shr b 52 1023
98m = bitcast f64 or and b 4503599627370495 4607182418800017408
99if gt m 1.4142135623730951
100m = mul m 0.5
101e = add e 1
102end
103f = sub m 1.0
104s = div f add f 2.0
105z = mul s s
106q = add mul z 0.043478260869565216 0.047619047619047616
107q = add mul q z 0.05263157894736842
108q = add mul q z 0.058823529411764705
109q = add mul q z 0.06666666666666667
110q = add mul q z 0.07692307692307693
111q = add mul q z 0.09090909090909091
112q = add mul q z 0.1111111111111111
113q = add mul q z 0.14285714285714285
114q = add mul q z 0.2
115q = add mul q z 0.3333333333333333
116q = add mul q z 1.0
117lm = mul add s s q
118fe = f64 e
119ret add mul fe 0.6931471803691238 add lm mul fe 1.9082149292705877e-10
120end
121
122note round(v * 10^6) half away from zero (CONTRACT section 7).
123fn rmic v f64 to long
124x = mul v 1000000.0
125q = long x
126fr = sub x f64 q
127if ge fr 0.5
128q = add q 1
129end
130if le fr -0.5
131q = sub q 1
132end
133ret q
134end
135
136fn oputc c ulong to void
137n = get oon 0
138if ge n OMOUT
139oflush
140n = ulong 0
141end
142put oout n uchar and c 255
143put oon 0 add n 1
144end
145
146fn oputu v ulong to void
147d = new uchar 24 0
148n = ulong 0
149x = v
150while true bound 21
151put d n uchar add rem x 10 48
152n = add n 1
153x = div x 10
154if eq x 0
155break
156end
157end
158for i ulong n
159oputc ulong get d sub sub n 1 i
160end
161end
162
163fn oputl v long to void
164if lt v 0
165oputc 45
166if eq v -9223372036854775808
167oputu 9223372036854775808
168else
169oputu ulong neg v
170end
171else
172oputu ulong v
173end
174end
175
176note 16 lowercase hex digits.
177fn oputx v ulong to void
178for i ulong 16
179h = and shr v mul sub 15 i 4 15
180if lt h 10
181oputc add h 48
182else
183oputc add h 87
184end
185end
186end