Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/train/omega-llm/native/adam.om # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / train / omega-llm / native / adam.om` 332 lines ยท 7134 bytes Raw Blame History 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(2 LV), LV the layer applications (L without a loop; og 56, model.om): each 27 note application adds two residual branches to the stream; gains 1; m = v = 0. 28 fn oinit to void 29 p = get og 8 30 sd = get og 40 31 for q ulong ocn p 32 i = mul q OCH 33 ie = oce q p 34 while lt i ie bound 999999 35 r = mul f64 shr smx add sd i 11 1.`1102230246251565e`-16 36 owp i mul sub add r r 1.0 0.`034641016151377546` 37 i = add i 1 38 end 39 end 40 d = get og 2 41 ri = div 1.0f64 fsqrt f64 mul 2 get og 56 42 for l ulong get og 5 43 oiset opg1 l d 1.0 44 oiset opg2 l d 1.0 45 oiscale opwo l mul d d ri 46 oiscale opw2 l mul d get og 4 ri 47 end 48 oiset get og 33 d 1.0 49 ozerom mul 2 p 50 end 51 52 fn ozerom n ulong to void 53 for q ulong ocn n 54 i = mul q OCH 55 ie = oce q n 56 while lt i ie bound 999999 57 omvp i 0.0f64 58 i = add i 1 59 end 60 end 61 end 62 63 fn oiset o ulong n ulong v f64 to void 64 for i ulong n 65 owp add o i v 66 end 67 end 68 69 fn oiscale o ulong n ulong v f64 to void 70 for i ulong n 71 owp add o i mul owg add o i v 72 end 73 end 74 75 fn olrs s ulong lr f64 to f64 76 if lt s 100 77 ret div mul lr f64 add s 1 100.0 78 end 79 den = ulong 1 80 if gt get og 42 101 81 den = sub get og 42 100 82 end 83 ret mul lr sub 1.0 div mul 0.9 f64 sub s 100 f64 den 84 end 85 86 fn opowb b f64 s ulong to f64 87 pw = 1.0f64 88 for i ulong add s 1 89 pw = mul pw b 90 end 91 ret pw 92 end 93 94 note clip the gradient to global norm 1 and apply one Adam update at step og[41] 95 fn oadam to void 96 p = get og 8 97 fz = get ofreeze 0 98 ss = 0.0f64 99 for q ulong ocn p 100 i = mul q OCH 101 ie = oce q p 102 while lt i ie bound 999999 103 if lt i fz 104 ocsp i 0.0f64 105 else 106 g = ocsg i 107 ss = add ss mul g g 108 end 109 i = add i 1 110 end 111 end 112 nr = fsqrt ss 113 if gt nr 1.0 114 oclip div 1.0 nr 115 end 116 s = get og 41 117 lrs = olrs s bitcast f64 get og 43 118 bc1 = sub 1.0 opowb 0.9 s 119 bc2 = sub 1.0 opowb 0.99 s 120 if eq OMUTC 0 121 bc1 = 1.0f64 122 bc2 = 1.0f64 123 end 124 put ofs 3 lrs 125 put ofs 4 bc1 126 put ofs 5 bc2 127 for q2 ulong ocn p 128 k = mul q2 OCH 129 ke = oce q2 p 130 while lt k ke bound 999999 131 oadam1 k 132 k = add k 1 133 end 134 end 135 end 136 137 note g_i = g_i c for i < P 138 fn oclip c f64 to void 139 p = get og 8 140 for q ulong ocn p 141 j = mul q OCH 142 je = oce q p 143 while lt j je bound 999999 144 ocsp j mul ocsg j c 145 j = add j 1 146 end 147 end 148 end 149 150 note moments of parameter i: m = 0.9 m + (1 - 0.9) g; v = 0.99 v + (1 - 0.99) g^2 151 fn oadam1 i ulong to void 152 if lt i get ofreeze 0 153 ret 154 end 155 g = ogrg add get og 10 i 156 mo = add get og 11 i 157 m = omvg mo 158 m = mul 0.9 m 159 a = sub 1.0f64 0.9 160 m = add m mul a g 161 omvp mo m 162 vo = add get og 12 i 163 v = omvg vo 164 v = mul 0.99 v 165 a = sub 1.0f64 0.99 166 v = add v mul a mul g g 167 omvp vo v 168 oadam2 i m v 169 end 170 171 note p[i] -= lr (m/bc1) / (sqrt(v/bc2) + 1e-8) with lr, bc1, bc2 in ofs 3, 4, 5 172 fn oadam2 i ulong m f64 v f64 to void 173 u = div m get ofs 4 174 u = mul get ofs 3 u 175 w = div v get ofs 5 176 w = add fsqrt w 0.`00000001` 177 u = div u w 178 w = owg i 179 owp i sub w u 180 end 181 182 note one training step at step og[41] on TRAIN; returns the batch loss at the pre-update 183 note parameters and increments the step. ogradw (lio.om, eio.om) runs the K workers. 184 fn ostep to f64 185 s = get og 41 186 obatch s 187 ls = ogradw 188 oadam 189 put og 41 add s 1 190 ret ls 191 end 192 193 note gradient of the step loss for the batch in obx/oby (no clip, no update); returns the loss. 194 note CONTRACT-V1 section 5: G = og[44] blocks of R = B/G sequences; block j is accumulated from 195 note zero for b ascending; GRD = block 0, then GRD[i] = GRD[i] + block j[i] for j ascending. 196 note G = 1 is the v0 gradient bit for bit. Every block is accumulated in BLK (CONTRACT-V3 section 2) 197 note and block 0 copied into GRD, the same bits as accumulating it in GRD. 198 fn ograd to f64 199 gg = get og 44 200 for i ulong gg 201 j = i 202 if eq OMUTD 0 203 j = sub sub gg 1 i 204 end 205 oblkc j 206 ogfold eq i 0 207 end 208 ret olsum 209 end 210 211 note block j into BLK (gradient pool [P, 2P), zeroed first); losses into olos 212 fn oblkc j ulong to void 213 p = get og 8 214 put og 10 p 215 ozerog p p 216 bb = get og 6 217 sc = div 1.0f64 f64 mul get og 1 bb 218 r = div bb get og 44 219 b = mul j r 220 for i ulong r 221 put olos b ofwd b 222 obwd b sc 223 b = add b 1 224 end 225 put og 10 0 226 end 227 228 note BLK into GRD: copied for the first block, else GRD[i] = GRD[i] + BLK[i] (ogadd) 229 fn ogfold first bool to void 230 if not first 231 ogadd 232 ret 233 end 234 p = get og 8 235 for q ulong ocn p 236 i = mul q OCH 237 ie = oce q p 238 while lt i ie bound 999999 239 ocsp i ogrg add p i 240 i = add i 1 241 end 242 end 243 end 244 245 note GRD[i] = GRD[i] + BLK[i] for i ascending 246 fn ogadd to void 247 p = get og 8 248 for q ulong ocn p 249 i = mul q OCH 250 ie = oce q p 251 while lt i ie bound 999999 252 ogadd1 i p 253 i = add i 1 254 end 255 end 256 end 257 258 note GRD[i] += BLK[i] (BLK at P) 259 fn ogadd1 i ulong p ulong to void 260 x = ogrg add p i 261 ocsp i add ocsg i x 262 end 263 264 note step loss: sum of olos[b] for b ascending from 0.0, divided by f64 B 265 fn olsum to f64 266 ls = 0.0f64 267 bb = get og 6 268 for b ulong bb 269 ls = add ls get olos b 270 end 271 ret div ls f64 bb 272 end 273 274 note adamck: Adam self-check on the small shape. With a constant gradient g from zero moments, 275 note exact arithmetic gives m/bc1 = g and v/bc2 = g^2 at every step, so after K steps 276 note p = -sum_s lr_s g/(|g| + 1e-8). Prints `A K maxrel_bits_hex maxrel_e15` and exits 41 when 277 note maxrel > 1e-12. Without bias correction (mutant c) the step-1 ratio is exactly 1 and the step-2 278 note ratio is 1.347; the query and the predicted 0.63909 are in docs/omegallm/README.md, "Sizing". 279 fn cmdadamck to void 280 put og 0 256 281 put og 1 8 282 put og 2 16 283 put og 3 2 284 put og 4 32 285 put og 5 2 286 put og 6 2 287 if not ogeom 288 exit 12 289 end 290 put og 42 200 291 put og 43 bitcast ulong 0.001f64 292 put og 41 0 293 p = get og 8 294 for i ulong p 295 x = sub f64 rem i 7 3.0 296 x = mul x 0.0001 297 ogrp add get og 10 i x 298 end 299 for k ulong 5 300 oadam 301 put og 41 add get og 41 1 302 end 303 mx = 0.0f64 304 for j ulong p 305 mx = fmax mx oadamex j 306 end 307 oputc 65 308 oputc 32 309 oputu 5 310 oputc 32 311 oputx bitcast ulong mx 312 oputc 32 313 oputl long mul mx `1000000000000000`.0 314 oputc 10 315 oflush 316 if gt mx 0.`000000000001` 317 exit 41 318 end 319 end 320 321 fn oadamex j ulong to f64 322 g = ogrg add get og 10 j 323 w = 0.0f64 324 for s ulong 5 325 w = sub w div mul olrs s bitcast f64 get og 43 g add fabs g 0.`00000001` 326 end 327 d = fabs sub owg j w 328 if eq w 0.0 329 ret d 330 end 331 ret div d fabs w 332 end