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 / adam.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-only |
| 2 | note omegallm adam.om (aien-train#52): init, global-norm clip, Adam with bias correction, the LR |
| 3 | note schedule, gradient blocks (CONTRACT-V1 section 5) and one training step (CONTRACT section 5). |
| 4 | note Needs math.om, data.om, model.om. |
| 5 | note Exact operation order (agreed with the oracle lane, recorded in CONTRACT section 5): |
| 6 | note n = sqrt(sum_i g_i^2) (i ascending); if n > 1: c = 1/n, g_i = g_i c. |
| 7 | note b^(s+1) by s+1 repeated multiplications starting from 1.0; bc = 1 - b^(s+1). |
| 8 | note m = 0.9 m + (1.0 - 0.9) g; v = 0.99 v + (1.0 - 0.99)(g g); |
| 9 | note p = p - (lr_s (m/bc1)) / (sqrt(v/bc2) + 1e-8). |
| 10 | note lr_s = (lr (s+1))/100 for s < 100, else lr (1 - (0.9 (s-100))/max(1, TOTAL-100)). |
| 11 | |
| 12 | note Mutant c (bias correction removed): 1 in the product, 0 in the mutant. |
| 13 | const OMUTC ulong 1 |
| 14 | |
| 15 | note Mutant d (gradient blocks combined in reverse order, G-1 first): 1 in the product, 0 in the |
| 16 | note mutant. |
| 17 | const OMUTD ulong 1 |
| 18 | |
| 19 | note per-sequence losses of the current batch |
| 20 | global olos f64 64 |
| 21 | note ofreeze: when > 0, parameters i < ofreeze[0] are frozen (gradient zeroed, m/v/w untouched). |
| 22 | global ofreeze ulong 1 |
| 23 | |
| 24 | note init (CONTRACT section 5): r_i = (splitmix64(S+i) >> 11) 2^-53 (N[2^-53] -> |
| 25 | note 1.1102230246251565*^-16); w = (2 r - 1) 0.034641016151377546 (N[0.02 Sqrt[3]]); wo and w2 |
| 26 | note times 1/sqrt(2L); gains 1; m = v = 0. |
| 27 | fn oinit to void |
| 28 | p = get og 8 |
| 29 | sd = get og 40 |
| 30 | for q ulong ocn p |
| 31 | i = mul q OCH |
| 32 | ie = oce q p |
| 33 | while lt i ie bound 999999 |
| 34 | r = mul f64 shr smx add sd i 11 1.1102230246251565e-16 |
| 35 | owp i mul sub add r r 1.0 0.034641016151377546 |
| 36 | i = add i 1 |
| 37 | end |
| 38 | end |
| 39 | d = get og 2 |
| 40 | ri = div 1.0f64 fsqrt f64 mul 2 get og 5 |
| 41 | for l ulong get og 5 |
| 42 | oiset opg1 l d 1.0 |
| 43 | oiset opg2 l d 1.0 |
| 44 | oiscale opwo l mul d d ri |
| 45 | oiscale opw2 l mul d get og 4 ri |
| 46 | end |
| 47 | oiset get og 33 d 1.0 |
| 48 | ozerom mul 2 p |
| 49 | end |
| 50 | |
| 51 | fn ozerom n ulong to void |
| 52 | for q ulong ocn n |
| 53 | i = mul q OCH |
| 54 | ie = oce q n |
| 55 | while lt i ie bound 999999 |
| 56 | omvp i 0.0f64 |
| 57 | i = add i 1 |
| 58 | end |
| 59 | end |
| 60 | end |
| 61 | |
| 62 | fn oiset o ulong n ulong v f64 to void |
| 63 | for i ulong n |
| 64 | owp add o i v |
| 65 | end |
| 66 | end |
| 67 | |
| 68 | fn oiscale o ulong n ulong v f64 to void |
| 69 | for i ulong n |
| 70 | owp add o i mul owg add o i v |
| 71 | end |
| 72 | end |
| 73 | |
| 74 | fn olrs s ulong lr f64 to f64 |
| 75 | if lt s 100 |
| 76 | ret div mul lr f64 add s 1 100.0 |
| 77 | end |
| 78 | den = ulong 1 |
| 79 | if gt get og 42 101 |
| 80 | den = sub get og 42 100 |
| 81 | end |
| 82 | ret mul lr sub 1.0 div mul 0.9 f64 sub s 100 f64 den |
| 83 | end |
| 84 | |
| 85 | fn opowb b f64 s ulong to f64 |
| 86 | pw = 1.0f64 |
| 87 | for i ulong add s 1 |
| 88 | pw = mul pw b |
| 89 | end |
| 90 | ret pw |
| 91 | end |
| 92 | |
| 93 | note clip the gradient to global norm 1 and apply one Adam update at step og[41] |
| 94 | fn oadam to void |
| 95 | p = get og 8 |
| 96 | fz = get ofreeze 0 |
| 97 | ss = 0.0f64 |
| 98 | for q ulong ocn p |
| 99 | i = mul q OCH |
| 100 | ie = oce q p |
| 101 | while lt i ie bound 999999 |
| 102 | if lt i fz |
| 103 | ocsp i 0.0f64 |
| 104 | else |
| 105 | g = ocsg i |
| 106 | ss = add ss mul g g |
| 107 | end |
| 108 | i = add i 1 |
| 109 | end |
| 110 | end |
| 111 | nr = fsqrt ss |
| 112 | if gt nr 1.0 |
| 113 | oclip div 1.0 nr |
| 114 | end |
| 115 | s = get og 41 |
| 116 | lrs = olrs s bitcast f64 get og 43 |
| 117 | bc1 = sub 1.0 opowb 0.9 s |
| 118 | bc2 = sub 1.0 opowb 0.99 s |
| 119 | if eq OMUTC 0 |
| 120 | bc1 = 1.0f64 |
| 121 | bc2 = 1.0f64 |
| 122 | end |
| 123 | put ofs 3 lrs |
| 124 | put ofs 4 bc1 |
| 125 | put ofs 5 bc2 |
| 126 | for q2 ulong ocn p |
| 127 | k = mul q2 OCH |
| 128 | ke = oce q2 p |
| 129 | while lt k ke bound 999999 |
| 130 | oadam1 k |
| 131 | k = add k 1 |
| 132 | end |
| 133 | end |
| 134 | end |
| 135 | |
| 136 | note g_i = g_i c for i < P |
| 137 | fn oclip c f64 to void |
| 138 | p = get og 8 |
| 139 | for q ulong ocn p |
| 140 | j = mul q OCH |
| 141 | je = oce q p |
| 142 | while lt j je bound 999999 |
| 143 | ocsp j mul ocsg j c |
| 144 | j = add j 1 |
| 145 | end |
| 146 | end |
| 147 | end |
| 148 | |
| 149 | note moments of parameter i: m = 0.9 m + (1 - 0.9) g; v = 0.99 v + (1 - 0.99) g^2 |
| 150 | fn oadam1 i ulong to void |
| 151 | if lt i get ofreeze 0 |
| 152 | ret |
| 153 | end |
| 154 | g = ogrg add get og 10 i |
| 155 | mo = add get og 11 i |
| 156 | m = omvg mo |
| 157 | m = mul 0.9 m |
| 158 | a = sub 1.0f64 0.9 |
| 159 | m = add m mul a g |
| 160 | omvp mo m |
| 161 | vo = add get og 12 i |
| 162 | v = omvg vo |
| 163 | v = mul 0.99 v |
| 164 | a = sub 1.0f64 0.99 |
| 165 | v = add v mul a mul g g |
| 166 | omvp vo v |
| 167 | oadam2 i m v |
| 168 | end |
| 169 | |
| 170 | note p[i] -= lr (m/bc1) / (sqrt(v/bc2) + 1e-8) with lr, bc1, bc2 in ofs 3, 4, 5 |
| 171 | fn oadam2 i ulong m f64 v f64 to void |
| 172 | u = div m get ofs 4 |
| 173 | u = mul get ofs 3 u |
| 174 | w = div v get ofs 5 |
| 175 | w = add fsqrt w 0.00000001 |
| 176 | u = div u w |
| 177 | w = owg i |
| 178 | owp i sub w u |
| 179 | end |
| 180 | |
| 181 | note one training step at step og[41] on TRAIN; returns the batch loss at the pre-update |
| 182 | note parameters and increments the step. ogradw (lio.om, eio.om) runs the K workers. |
| 183 | fn ostep to f64 |
| 184 | s = get og 41 |
| 185 | obatch s |
| 186 | ls = ogradw |
| 187 | oadam |
| 188 | put og 41 add s 1 |
| 189 | ret ls |
| 190 | end |
| 191 | |
| 192 | note gradient of the step loss for the batch in obx/oby (no clip, no update); returns the loss. |
| 193 | note CONTRACT-V1 section 5: G = og[44] blocks of R = B/G sequences; block j is accumulated from |
| 194 | note zero for b ascending; GRD = block 0, then GRD[i] = GRD[i] + block j[i] for j ascending. |
| 195 | note G = 1 is the v0 gradient bit for bit. Every block is accumulated in BLK (CONTRACT-V3 section 2) |
| 196 | note and block 0 copied into GRD, the same bits as accumulating it in GRD. |
| 197 | fn ograd to f64 |
| 198 | gg = get og 44 |
| 199 | for i ulong gg |
| 200 | j = i |
| 201 | if eq OMUTD 0 |
| 202 | j = sub sub gg 1 i |
| 203 | end |
| 204 | oblkc j |
| 205 | ogfold eq i 0 |
| 206 | end |
| 207 | ret olsum |
| 208 | end |
| 209 | |
| 210 | note block j into BLK (gradient pool [P, 2P), zeroed first); losses into olos |
| 211 | fn oblkc j ulong to void |
| 212 | p = get og 8 |
| 213 | put og 10 p |
| 214 | ozerog p p |
| 215 | bb = get og 6 |
| 216 | sc = div 1.0f64 f64 mul get og 1 bb |
| 217 | r = div bb get og 44 |
| 218 | b = mul j r |
| 219 | for i ulong r |
| 220 | put olos b ofwd b |
| 221 | obwd b sc |
| 222 | b = add b 1 |
| 223 | end |
| 224 | put og 10 0 |
| 225 | end |
| 226 | |
| 227 | note BLK into GRD: copied for the first block, else GRD[i] = GRD[i] + BLK[i] (ogadd) |
| 228 | fn ogfold first bool to void |
| 229 | if not first |
| 230 | ogadd |
| 231 | ret |
| 232 | end |
| 233 | p = get og 8 |
| 234 | for q ulong ocn p |
| 235 | i = mul q OCH |
| 236 | ie = oce q p |
| 237 | while lt i ie bound 999999 |
| 238 | ocsp i ogrg add p i |
| 239 | i = add i 1 |
| 240 | end |
| 241 | end |
| 242 | end |
| 243 | |
| 244 | note GRD[i] = GRD[i] + BLK[i] for i ascending |
| 245 | fn ogadd to void |
| 246 | p = get og 8 |
| 247 | for q ulong ocn p |
| 248 | i = mul q OCH |
| 249 | ie = oce q p |
| 250 | while lt i ie bound 999999 |
| 251 | ogadd1 i p |
| 252 | i = add i 1 |
| 253 | end |
| 254 | end |
| 255 | end |
| 256 | |
| 257 | note GRD[i] += BLK[i] (BLK at P) |
| 258 | fn ogadd1 i ulong p ulong to void |
| 259 | x = ogrg add p i |
| 260 | ocsp i add ocsg i x |
| 261 | end |
| 262 | |
| 263 | note step loss: sum of olos[b] for b ascending from 0.0, divided by f64 B |
| 264 | fn olsum to f64 |
| 265 | ls = 0.0f64 |
| 266 | bb = get og 6 |
| 267 | for b ulong bb |
| 268 | ls = add ls get olos b |
| 269 | end |
| 270 | ret div ls f64 bb |
| 271 | end |
| 272 | |
| 273 | note adamck: Adam self-check on the small shape. With a constant gradient g from zero moments, |
| 274 | note exact arithmetic gives m/bc1 = g and v/bc2 = g^2 at every step, so after K steps |
| 275 | note p = -sum_s lr_s g/(|g| + 1e-8). Prints `A K maxrel_bits_hex maxrel_e15` and exits 41 when |
| 276 | note maxrel > 1e-12. Without bias correction (mutant c) the step-1 ratio is exactly 1 and the step-2 |
| 277 | note ratio is 1.347; the query and the predicted 0.63909 are in docs/omegallm/README.md, "Sizing". |
| 278 | fn cmdadamck to void |
| 279 | put og 0 256 |
| 280 | put og 1 8 |
| 281 | put og 2 16 |
| 282 | put og 3 2 |
| 283 | put og 4 32 |
| 284 | put og 5 2 |
| 285 | put og 6 2 |
| 286 | if not ogeom |
| 287 | exit 12 |
| 288 | end |
| 289 | put og 42 200 |
| 290 | put og 43 bitcast ulong 0.001f64 |
| 291 | put og 41 0 |
| 292 | p = get og 8 |
| 293 | for i ulong p |
| 294 | x = sub f64 rem i 7 3.0 |
| 295 | x = mul x 0.0001 |
| 296 | ogrp add get og 10 i x |
| 297 | end |
| 298 | for k ulong 5 |
| 299 | oadam |
| 300 | put og 41 add get og 41 1 |
| 301 | end |
| 302 | mx = 0.0f64 |
| 303 | for j ulong p |
| 304 | mx = fmax mx oadamex j |
| 305 | end |
| 306 | oputc 65 |
| 307 | oputc 32 |
| 308 | oputu 5 |
| 309 | oputc 32 |
| 310 | oputx bitcast ulong mx |
| 311 | oputc 32 |
| 312 | oputl long mul mx 1000000000000000.0 |
| 313 | oputc 10 |
| 314 | oflush |
| 315 | if gt mx 0.000000000001 |
| 316 | exit 41 |
| 317 | end |
| 318 | end |
| 319 | |
| 320 | fn oadamex j ulong to f64 |
| 321 | g = ogrg add get og 10 j |
| 322 | w = 0.0f64 |
| 323 | for s ulong 5 |
| 324 | w = sub w div mul olrs s bitcast f64 get og 43 g add fabs g 0.00000001 |
| 325 | end |
| 326 | d = fabs sub owg j w |
| 327 | if eq w 0.0 |
| 328 | ret d |
| 329 | end |
| 330 | ret div d fabs w |
| 331 | end |