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