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 / omegese45 / core / model.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-only |
| 2 | note omegese45 core model (aien-train #45, pipeline lane): a dense next-token model |
| 3 | note over R45 ids. One hidden ReLU layer, 41 linear outputs, squared error to the one-hot |
| 4 | note target, plain per-position SGD. Forward and backward are written out here because the |
| 5 | note native tape (omega-mv native/autodiff664/tape.om @965fb762) holds at most 32 nodes of |
| 6 | note at most 64 elements in a 2048-float arena (docs/omegese45/PIPELINE.md, "Why not the tape"). |
| 7 | note Causal input for position t, built only from ids at positions < t: |
| 8 | note head slot p (0 <= p < 30, p < t): one-hot of id[p] feature p * 41 + id |
| 9 | note tail slot k (0 <= k < 64, k < t): one-hot of id[t-1-k] feature (30 + k) * 41 + id |
| 10 | note Arch: OPIN 3854 inputs, OPH 256 hidden, OPV 41 outputs. Parameters, payload order: |
| 11 | note W1[f][j] f < 3854, j < 256 (row-major), b1[256], W2[j][v] (row-major), b2[41]. |
| 12 | note Init algorithm 1: splitmix64 from the init seed; W1 then W2 in payload order, each |
| 13 | note value u = ((r >> 40) - 2^23) * 2^-23 scaled by 1/8 (W1) or 1/16 (W2); biases 0. |
| 14 | note Every value is a dyadic rational, exact in binary32. Needs vocab.om (ocv) before it. |
| 15 | const OPV ulong 41 |
| 16 | const OPHS ulong 30 |
| 17 | const OPTW ulong 64 |
| 18 | const OPIN ulong 3854 |
| 19 | const OPH ulong 256 |
| 20 | const OPW1N ulong 986624 |
| 21 | const OPW2N ulong 10496 |
| 22 | const OPNP ulong 997417 |
| 23 | const OPMAXL ulong 176 |
| 24 | global opw1 f32 986624 |
| 25 | global opb1 f32 256 |
| 26 | global opw2 f32 10496 |
| 27 | global opb2 f32 41 |
| 28 | global opseq uchar 256 |
| 29 | global opsl ulong 1 |
| 30 | global opmk uchar 256 |
| 31 | global opfx ulong 94 |
| 32 | global opfn ulong 1 |
| 33 | global ophp f32 256 |
| 34 | global oph f32 256 |
| 35 | global opy f32 41 |
| 36 | global opgy f32 41 |
| 37 | global opgh f32 256 |
| 38 | global oprs ulong 1 |
| 39 | |
| 40 | note splitmix64 (Steele, Lea, Flood 2014). |
| 41 | fn opnext to ulong |
| 42 | x = add get oprs 0 11400714819323198485 |
| 43 | put oprs 0 x |
| 44 | z = xor x shr x 30 |
| 45 | z = mul z 13787848793156543929 |
| 46 | z = xor z shr z 27 |
| 47 | z = mul z 10723151780598845931 |
| 48 | ret xor z shr z 31 |
| 49 | end |
| 50 | |
| 51 | note u in [-1, 1) on the 2^-23 grid, from the top 24 bits of the next generator value. |
| 52 | fn opunif to f32 |
| 53 | k = long shr opnext 40 |
| 54 | ret mul f32 sub k 8388608 0.00000011920928955078125f32 |
| 55 | end |
| 56 | |
| 57 | fn opinit seed ulong to void |
| 58 | put oprs 0 seed |
| 59 | for i ulong OPW1N |
| 60 | put opw1 i mul opunif 0.125f32 |
| 61 | end |
| 62 | for i ulong OPW2N |
| 63 | put opw2 i mul opunif 0.0625f32 |
| 64 | end |
| 65 | for j ulong OPH |
| 66 | put opb1 j 0.0f32 |
| 67 | end |
| 68 | for v ulong OPV |
| 69 | put opb2 v 0.0f32 |
| 70 | end |
| 71 | end |
| 72 | |
| 73 | note Parameter i of the payload order. |
| 74 | fn opgetp i ulong to f32 |
| 75 | if lt i OPW1N |
| 76 | ret get opw1 i |
| 77 | end |
| 78 | j = sub i OPW1N |
| 79 | if lt j OPH |
| 80 | ret get opb1 j |
| 81 | end |
| 82 | j = sub j OPH |
| 83 | if lt j OPW2N |
| 84 | ret get opw2 j |
| 85 | end |
| 86 | ret get opb2 sub j OPW2N |
| 87 | end |
| 88 | |
| 89 | fn opsetp i ulong x f32 to void |
| 90 | if lt i OPW1N |
| 91 | put opw1 i x |
| 92 | ret |
| 93 | end |
| 94 | j = sub i OPW1N |
| 95 | if lt j OPH |
| 96 | put opb1 j x |
| 97 | ret |
| 98 | end |
| 99 | j = sub j OPH |
| 100 | if lt j OPW2N |
| 101 | put opw2 j x |
| 102 | ret |
| 103 | end |
| 104 | put opb2 sub j OPW2N x |
| 105 | end |
| 106 | |
| 107 | note Active features of position t of opseq (ids at positions < t only). |
| 108 | fn opfeat t ulong to void |
| 109 | n = ulong 0 |
| 110 | hm = t |
| 111 | if gt hm OPHS |
| 112 | hm = OPHS |
| 113 | end |
| 114 | for p ulong hm |
| 115 | id = ulong get opseq p |
| 116 | if ne id 0 |
| 117 | put opfx n add mul p OPV id |
| 118 | n = add n 1 |
| 119 | end |
| 120 | end |
| 121 | km = t |
| 122 | if gt km OPTW |
| 123 | km = OPTW |
| 124 | end |
| 125 | for k ulong km |
| 126 | id = ulong get opseq sub sub t 1 k |
| 127 | if ne id 0 |
| 128 | put opfx n add mul add OPHS k OPV id |
| 129 | n = add n 1 |
| 130 | end |
| 131 | end |
| 132 | put opfn 0 n |
| 133 | end |
| 134 | |
| 135 | fn opfwd to void |
| 136 | for j ulong OPH |
| 137 | put ophp j get opb1 j |
| 138 | end |
| 139 | for f ulong get opfn 0 |
| 140 | r = mul get opfx f OPH |
| 141 | for j ulong OPH |
| 142 | put ophp j add get ophp j get opw1 add r j |
| 143 | end |
| 144 | end |
| 145 | for v ulong OPV |
| 146 | put opy v get opb2 v |
| 147 | end |
| 148 | for j ulong OPH |
| 149 | x = get ophp j |
| 150 | put oph j 0.0f32 |
| 151 | if gt x 0.0f32 |
| 152 | put oph j x |
| 153 | r = mul j OPV |
| 154 | for v ulong OPV |
| 155 | put opy v add get opy v mul x get opw2 add r v |
| 156 | end |
| 157 | end |
| 158 | end |
| 159 | end |
| 160 | |
| 161 | note Squared error to the one-hot of tg; dL/dy into opgy. Returns L. |
| 162 | fn oploss tg ulong to f32 |
| 163 | l = 0.0f32 |
| 164 | for v ulong OPV |
| 165 | d = get opy v |
| 166 | if eq v tg |
| 167 | d = sub d 1.0f32 |
| 168 | end |
| 169 | put opgy v mul 2.0f32 d |
| 170 | l = add l mul d d |
| 171 | end |
| 172 | ret l |
| 173 | end |
| 174 | |
| 175 | note dL/dhp into opgh (ReLU derivative 1 where hp > 0), from W2 before its update. |
| 176 | fn opbwd to void |
| 177 | for j ulong OPH |
| 178 | s = 0.0f32 |
| 179 | if gt get ophp j 0.0f32 |
| 180 | r = mul j OPV |
| 181 | for v ulong OPV |
| 182 | s = add s mul get opw2 add r v get opgy v |
| 183 | end |
| 184 | end |
| 185 | put opgh j s |
| 186 | end |
| 187 | end |
| 188 | |
| 189 | note p = p - lr * dL/dp for every parameter with a nonzero gradient path. |
| 190 | fn opupd lr f32 to void |
| 191 | for j ulong OPH |
| 192 | x = get oph j |
| 193 | if gt x 0.0f32 |
| 194 | r = mul j OPV |
| 195 | for v ulong OPV |
| 196 | q = add r v |
| 197 | g = mul lr mul x get opgy v |
| 198 | put opw2 q sub get opw2 q g |
| 199 | end |
| 200 | end |
| 201 | put opb1 j sub get opb1 j mul lr get opgh j |
| 202 | end |
| 203 | for v ulong OPV |
| 204 | put opb2 v sub get opb2 v mul lr get opgy v |
| 205 | end |
| 206 | for f ulong get opfn 0 |
| 207 | r = mul get opfx f OPH |
| 208 | for j ulong OPH |
| 209 | q = add r j |
| 210 | g = mul lr get opgh j |
| 211 | put opw1 q sub get opw1 q g |
| 212 | end |
| 213 | end |
| 214 | end |
| 215 | |
| 216 | note Target of position t: the id at t. |
| 217 | fn optgt t ulong to ulong |
| 218 | ret ulong get opseq t |
| 219 | end |
| 220 | |
| 221 | note A/P mask of opseq: 1 at every position of a message whose frame id is act or rep. |
| 222 | note Messages are split at `end` (id 5), which occurs only as a message terminator in R45. |
| 223 | fn opmask to void |
| 224 | n = get opsl 0 |
| 225 | fr = ulong 0 |
| 226 | st = true |
| 227 | for t ulong n |
| 228 | id = ulong get opseq t |
| 229 | if st |
| 230 | fr = id |
| 231 | end |
| 232 | put opmk t 0 |
| 233 | if or eq fr 2 eq fr 4 |
| 234 | put opmk t 1 |
| 235 | end |
| 236 | st = eq id 5 |
| 237 | end |
| 238 | end |
| 239 | |
| 240 | note One position: features, forward, loss, backward. Returns L. |
| 241 | fn opgrad t ulong to f32 |
| 242 | opfeat t |
| 243 | opfwd |
| 244 | l = oploss optgt t |
| 245 | opbwd |
| 246 | ret l |
| 247 | end |
| 248 | |
| 249 | note Train on the current episode: phase 1 every position, phase 2 the A/P positions. |
| 250 | note Returns the summed loss of the trained positions (before each update). |
| 251 | fn optrep ph ulong lr f32 to f32 |
| 252 | s = 0.0f32 |
| 253 | opmask |
| 254 | for t ulong get opsl 0 |
| 255 | if or eq ph 1 eq get opmk t 1 |
| 256 | s = add s opgrad t |
| 257 | opupd lr |
| 258 | end |
| 259 | end |
| 260 | ret s |
| 261 | end |
| 262 | |
| 263 | note Argmax of opy over ids with ocmk[id] = 1 (all = true ignores the mask); first wins. |
| 264 | fn opargmx all bool to ulong |
| 265 | b = ulong 0 |
| 266 | bv = 0.0f32 |
| 267 | have = false |
| 268 | for v ulong OPV |
| 269 | if or all eq get ocmk v 1 |
| 270 | x = get opy v |
| 271 | if or not have gt x bv |
| 272 | b = v |
| 273 | bv = x |
| 274 | have = true |
| 275 | end |
| 276 | end |
| 277 | end |
| 278 | ret b |
| 279 | end |