aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / x86 / model / llama.om
| 1 | target x86linux |
| 2 | note Native decoder for the two pinned TinyStories Llama checkpoints. |
| 3 | note Inference, attention, normalization, sampling and generation are Omega. |
| 4 | note Input args: prompt count, new-token ceiling, checkpoint words, token ids. |
| 5 | note Read-only checkpoint uses the llama2.c version-0 f32 layout. |
| 6 | note Workspace is private, scratch begins at word 1024; first page is output. |
| 7 | note fexp is reused from aien-dev/omega-dnn om/dnn.om (Apache-2.0 with LLVM exception), with f32 rounding at use. |
| 8 | om2 |
| 9 | |
| 10 | fn fexp x f64 to f64 |
| 11 | if ne x x |
| 12 | ret x |
| 13 | end |
| 14 | if lt x -708.0f64 |
| 15 | ret 0.0f64 |
| 16 | end |
| 17 | y = fmin x 709.0f64 |
| 18 | t = mul y 1.4426950408889634f64 |
| 19 | n = long 0 |
| 20 | if ge t 0.0f64 |
| 21 | n = long add t 0.5f64 |
| 22 | else |
| 23 | n = long sub t 0.5f64 |
| 24 | end |
| 25 | t = f64 n |
| 26 | y = sub sub y mul t 0.6931471805599453f64 mul t 2.3190468138462996e-17f64 |
| 27 | t = 1.0f64 |
| 28 | for j ulong 13 |
| 29 | t = add 1.0f64 div mul y t f64 sub 13 j |
| 30 | end |
| 31 | ret mul t bitcast f64 shl ulong add n 1023 52 |
| 32 | end |
| 33 | |
| 34 | fn wf w view uint 16000000 i ulong to f32 inline |
| 35 | ret bitcast f32 get w i |
| 36 | end |
| 37 | fn sf s edit uint 1048576 i ulong to f32 |
| 38 | ret bitcast f32 get s i |
| 39 | end |
| 40 | fn sf_fast s edit uint 1048576 i ulong to f32 inline |
| 41 | ret bitcast f32 get s i |
| 42 | end |
| 43 | fn putf s edit uint 1048576 i ulong x f32 to void inline |
| 44 | put s i bitcast uint x |
| 45 | end |
| 46 | |
| 47 | fn setup w view uint 16000000 c edit ulong 64 nw ulong to void |
| 48 | for i ulong 7 |
| 49 | put c i ulong get w i |
| 50 | end |
| 51 | if eq get c 0 64 |
| 52 | if ne get c 1 172 |
| 53 | trap 3 |
| 54 | end |
| 55 | if ne get c 2 5 |
| 56 | trap 3 |
| 57 | end |
| 58 | if ne get c 3 8 |
| 59 | trap 3 |
| 60 | end |
| 61 | if ne get c 4 4 |
| 62 | trap 3 |
| 63 | end |
| 64 | if ne get c 5 512 |
| 65 | trap 3 |
| 66 | end |
| 67 | if ne get c 6 512 |
| 68 | trap 3 |
| 69 | end |
| 70 | else |
| 71 | if ne get c 0 288 |
| 72 | trap 3 |
| 73 | end |
| 74 | if ne get c 1 768 |
| 75 | trap 3 |
| 76 | end |
| 77 | if ne get c 2 6 |
| 78 | trap 3 |
| 79 | end |
| 80 | if ne get c 3 6 |
| 81 | trap 3 |
| 82 | end |
| 83 | if ne get c 4 6 |
| 84 | trap 3 |
| 85 | end |
| 86 | if ne get c 5 32000 |
| 87 | trap 3 |
| 88 | end |
| 89 | if ne get c 6 256 |
| 90 | trap 3 |
| 91 | end |
| 92 | end |
| 93 | put c 7 div mul get c 0 get c 4 get c 3 |
| 94 | put c 8 div get c 0 get c 3 |
| 95 | put c 9 64 |
| 96 | put c 10 7 |
| 97 | put c 11 add get c 10 mul get c 5 get c 0 |
| 98 | put c 12 add get c 11 mul get c 2 get c 0 |
| 99 | put c 13 add get c 12 mul get c 2 mul get c 0 get c 0 |
| 100 | put c 14 add get c 13 mul get c 2 mul get c 0 get c 7 |
| 101 | put c 15 add get c 14 mul get c 2 mul get c 0 get c 7 |
| 102 | put c 16 add get c 15 mul get c 2 mul get c 0 get c 0 |
| 103 | put c 17 add get c 16 mul get c 2 get c 0 |
| 104 | put c 18 add get c 17 mul get c 2 mul get c 0 get c 1 |
| 105 | put c 19 add get c 18 mul get c 2 mul get c 0 get c 1 |
| 106 | put c 20 add get c 19 mul get c 2 mul get c 0 get c 1 |
| 107 | put c 21 add get c 20 get c 0 |
| 108 | put c 22 add get c 21 div mul get c 6 get c 8 2 |
| 109 | if ne nw add get c 22 div mul get c 6 get c 8 2 |
| 110 | trap 3 |
| 111 | end |
| 112 | put c 32 1024 |
| 113 | put c 33 add get c 32 get c 0 |
| 114 | put c 34 add get c 33 get c 0 |
| 115 | put c 35 add get c 34 get c 0 |
| 116 | put c 36 add get c 35 get c 1 |
| 117 | put c 37 add get c 36 get c 1 |
| 118 | put c 38 add get c 37 get c 0 |
| 119 | put c 39 add get c 38 mul get c 2 mul get c 9 get c 7 |
| 120 | put c 40 add get c 39 mul get c 2 mul get c 9 get c 7 |
| 121 | put c 41 add get c 40 mul get c 3 get c 9 |
| 122 | if gt add get c 41 get c 5 1048576 |
| 123 | trap 3 |
| 124 | end |
| 125 | end |
| 126 | |
| 127 | fn dot w view uint 16000000 s edit uint 1048576 wi ulong si ulong n ulong to f32 |
| 128 | v = 0.0f32 |
| 129 | for j ulong n |
| 130 | v = add v mul wf w add wi j sf_fast s add si j |
| 131 | end |
| 132 | ret v |
| 133 | end |
| 134 | fn linear w view uint 16000000 s edit uint 1048576 wi ulong si ulong di ulong shape ulong to void |
| 135 | n = shr shape 32 |
| 136 | m = and shape 0xffffffff |
| 137 | for i ulong m |
| 138 | putf s add di i dot w s add wi mul i n si n |
| 139 | end |
| 140 | end |
| 141 | fn rms w view uint 16000000 s edit uint 1048576 wi ulong si ulong di ulong n ulong to void |
| 142 | v = 0.0f32 |
| 143 | for j ulong n |
| 144 | x = sf s add si j |
| 145 | v = add v mul x x |
| 146 | end |
| 147 | v = div 1.0f32 fsqrt add div v f32 n 0.00001f32 |
| 148 | for j ulong n |
| 149 | putf s add di j mul wf w add wi j mul v sf s add si j |
| 150 | end |
| 151 | end |
| 152 | fn copyw w view uint 16000000 s edit uint 1048576 wi ulong di ulong n ulong to void |
| 153 | for j ulong n |
| 154 | put s add di j get w add wi j |
| 155 | end |
| 156 | end |
| 157 | fn residual s edit uint 1048576 di ulong si ulong n ulong to void |
| 158 | for j ulong n |
| 159 | putf s add di j add sf s add di j sf s add si j |
| 160 | end |
| 161 | end |
| 162 | |
| 163 | fn rotate_values s edit uint 1048576 i ulong cs f32 sn f32 to void |
| 164 | a = sf s i |
| 165 | b = sf s add i 1 |
| 166 | putf s i sub mul a cs mul b sn |
| 167 | putf s add i 1 add mul a sn mul b cs |
| 168 | end |
| 169 | fn rotate w view uint 16000000 s edit uint 1048576 c view ulong 64 base ulong n ulong to void |
| 170 | for pair ulong div n 2 |
| 171 | i = mul pair 2 |
| 172 | off = mul get c 49 get c 8 |
| 173 | off = add off rem i get c 8 |
| 174 | off = div off 2 |
| 175 | cs = wf w add get c 21 off |
| 176 | sn = wf w add get c 22 off |
| 177 | rotate_values s add base i cs sn |
| 178 | end |
| 179 | end |
| 180 | fn softmax s edit uint 1048576 base ulong n ulong to void |
| 181 | m = sf s base |
| 182 | for j ulong n |
| 183 | m = fmax m sf s add base j |
| 184 | end |
| 185 | total = 0.0f32 |
| 186 | for j ulong n |
| 187 | v = f32 fexp f64 sub sf s add base j m |
| 188 | putf s add base j v |
| 189 | total = add total v |
| 190 | end |
| 191 | for j ulong n |
| 192 | putf s add base j div sf s add base j total |
| 193 | end |
| 194 | end |
| 195 | fn score s edit uint 1048576 a ulong b ulong n ulong to f32 |
| 196 | v = 0.0f32 |
| 197 | for j ulong n |
| 198 | v = add v mul sf_fast s add a j sf_fast s add b j |
| 199 | end |
| 200 | ret div v fsqrt f32 n |
| 201 | end |
| 202 | fn weighted s edit uint 1048576 a ulong k ulong n ulong stride ulong to f32 |
| 203 | v = 0.0f32 |
| 204 | for t ulong n |
| 205 | v = add v mul sf_fast s add a t sf_fast s add k mul t stride |
| 206 | end |
| 207 | ret v |
| 208 | end |
| 209 | fn attend s edit uint 1048576 c view ulong 64 head ulong to void |
| 210 | q = mul get c 48 get c 9 |
| 211 | q = mul q get c 7 |
| 212 | k = div get c 3 get c 4 |
| 213 | k = mul div head k get c 8 |
| 214 | k = add add k q get c 38 |
| 215 | q = add get c 37 mul head get c 8 |
| 216 | a = add get c 40 mul head get c 9 |
| 217 | for t ulong add get c 49 1 |
| 218 | putf s add a t score s q add k mul t get c 7 get c 8 |
| 219 | end |
| 220 | softmax s a add get c 49 1 |
| 221 | k = add k sub get c 39 get c 38 |
| 222 | q = add get c 33 mul head get c 8 |
| 223 | for j ulong get c 8 |
| 224 | v = weighted s a add k j add get c 49 1 get c 7 |
| 225 | putf s add q j v |
| 226 | end |
| 227 | end |
| 228 | fn swiglu s edit uint 1048576 a ulong b ulong n ulong to void |
| 229 | for j ulong n |
| 230 | v = sf s add a j |
| 231 | v = mul v div 1.0f32 add 1.0f32 f32 fexp f64 neg v |
| 232 | putf s add a j mul v sf s add b j |
| 233 | end |
| 234 | end |
| 235 | |
| 236 | fn layer w view uint 16000000 s edit uint 1048576 c view ulong 64 to void |
| 237 | rms w s add get c 11 mul get c 48 get c 0 get c 32 get c 33 get c 0 |
| 238 | off = ulong 0 |
| 239 | dest = ulong 0 |
| 240 | shape = ulong 0 |
| 241 | cache = mul get c 48 get c 9 |
| 242 | cache = mul add cache get c 49 get c 7 |
| 243 | off = mul get c 48 mul get c 0 get c 0 |
| 244 | off = add off get c 12 |
| 245 | dest = get c 37 |
| 246 | shape = or shl get c 0 32 get c 0 |
| 247 | linear w s off get c 33 dest shape |
| 248 | off = mul get c 48 mul get c 0 get c 7 |
| 249 | off = add off get c 13 |
| 250 | dest = add get c 38 cache |
| 251 | shape = or shl get c 0 32 get c 7 |
| 252 | linear w s off get c 33 dest shape |
| 253 | off = mul get c 48 mul get c 0 get c 7 |
| 254 | off = add off get c 14 |
| 255 | dest = add get c 39 cache |
| 256 | shape = or shl get c 0 32 get c 7 |
| 257 | linear w s off get c 33 dest shape |
| 258 | rotate w s c get c 37 get c 0 |
| 259 | rotate w s c add get c 38 cache get c 7 |
| 260 | for head ulong get c 3 |
| 261 | attend s c head |
| 262 | end |
| 263 | off = mul get c 48 mul get c 0 get c 0 |
| 264 | off = add off get c 15 |
| 265 | dest = get c 34 |
| 266 | shape = or shl get c 0 32 get c 0 |
| 267 | linear w s off get c 33 dest shape |
| 268 | residual s get c 32 get c 34 get c 0 |
| 269 | off = add get c 16 mul get c 48 get c 0 |
| 270 | rms w s off get c 32 get c 33 get c 0 |
| 271 | off = mul get c 48 mul get c 0 get c 1 |
| 272 | off = add off get c 17 |
| 273 | dest = get c 35 |
| 274 | shape = or shl get c 0 32 get c 1 |
| 275 | linear w s off get c 33 dest shape |
| 276 | off = mul get c 48 mul get c 0 get c 1 |
| 277 | off = add off get c 19 |
| 278 | dest = get c 36 |
| 279 | shape = or shl get c 0 32 get c 1 |
| 280 | linear w s off get c 33 dest shape |
| 281 | swiglu s get c 35 get c 36 get c 1 |
| 282 | off = mul get c 48 mul get c 1 get c 0 |
| 283 | off = add off get c 18 |
| 284 | dest = get c 33 |
| 285 | shape = or shl get c 1 32 get c 0 |
| 286 | linear w s off get c 35 dest shape |
| 287 | residual s get c 32 get c 33 get c 0 |
| 288 | end |
| 289 | fn forward w view uint 16000000 s edit uint 1048576 c edit ulong 64 token ulong pos ulong to ulong |
| 290 | if ge token get c 5 |
| 291 | trap 3 |
| 292 | end |
| 293 | put c 49 pos |
| 294 | copyw w s add get c 10 mul token get c 0 get c 32 get c 0 |
| 295 | for l ulong get c 2 |
| 296 | put c 48 l |
| 297 | layer w s c |
| 298 | end |
| 299 | rms w s get c 20 get c 32 get c 32 get c 0 |
| 300 | linear w s get c 10 get c 32 get c 41 or shl get c 0 32 get c 5 |
| 301 | best = ulong 0 |
| 302 | for i ulong get c 5 |
| 303 | if gt sf s add get c 41 i sf s add get c 41 best |
| 304 | best = i |
| 305 | end |
| 306 | end |
| 307 | ret best |
| 308 | end |
| 309 | |
| 310 | fn run args view ulong 512 s edit uint 1048576 count ulong w view uint 16000000 to ulong |
| 311 | if lt count 32 |
| 312 | trap 3 |
| 313 | end |
| 314 | c = new ulong 64 0 |
| 315 | setup w c get args 2 |
| 316 | nt = get args 0 |
| 317 | nn = get args 1 |
| 318 | if eq nt 0 |
| 319 | trap 3 |
| 320 | end |
| 321 | if eq nn 0 |
| 322 | trap 3 |
| 323 | end |
| 324 | if gt nt 32 |
| 325 | trap 3 |
| 326 | end |
| 327 | if gt nn 32 |
| 328 | trap 3 |
| 329 | end |
| 330 | if gt add nt nn 65 |
| 331 | trap 3 |
| 332 | end |
| 333 | if ne count mul add nt 3 8 |
| 334 | trap 3 |
| 335 | end |
| 336 | put s 0 0 |
| 337 | put s 1 uint get c 41 |
| 338 | put s 2 uint get c 5 |
| 339 | put s 3 uint get c 9 |
| 340 | token = get args 3 |
| 341 | for pos ulong sub add nt nn 1 |
| 342 | token = forward w s c token pos |
| 343 | if lt add pos 1 nt |
| 344 | token = get args add pos 4 |
| 345 | else |
| 346 | if eq token 1 |
| 347 | break |
| 348 | end |
| 349 | i = ulong get s 0 |
| 350 | put s add 16 i uint token |
| 351 | put s 0 uint add i 1 |
| 352 | end |
| 353 | end |
| 354 | ret mul add ulong get s 0 16 4 |
| 355 | end |