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-dnn/blob/main/om/dnn.om # omega-dnn aien-dev/omega-dnn Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-dnn / om / dnn.om` 881 lines ยท 22433 bytes Raw Blame History 1 note omega-dnn om/dnn.om: deep-learning primitives in seeded Omega (OM-2), ONNX operator semantics. 2 note One export runs a node list (a small fusion graph) over one f32 arena; see docs/DESCRIPTOR.md. 3 note Tensors are f32 bit patterns in a uint arena; integer side inputs (position ids, block tables, 4 note sequence lengths) are uint32 values in the same arena. All arithmetic runs in f64 and each 5 note stored element is rounded once to f32 (docs/TOLERANCE.md derives the bounds). 6 note The compiler has 16 registers per function and no spilling, so kernels keep their parameters 7 note in a 32-word array p (words 0..15 the node, 16..31 derived values) and split work into helpers. 8 note bridge c-type dsc omega_dnn_desc 9 note bridge c-define OMEGA_DNN_MAGIC `5194830` 10 om2 11 layout dsc 4096 12 field a ulong 512 at 0 little 13 end 14 const MAGIC ulong 0x4f444e 15 const NONE ulong 0xffffffffffffffff 16 const NB ulong 4 17 const NW ulong 16 18 const CH ulong 65536 19 fn ldf ws edit uint * i ulong to f64 inline 20 ret f64 bitcast f32 get ws i 21 end 22 note ldfo: outlined ldf for callers whose register budget cannot absorb the inline body. 23 fn ldfo ws edit uint * i ulong to f64 24 ret f64 bitcast f32 get ws i 25 end 26 fn stf ws edit uint * i ulong v f64 to void inline 27 put ws i bitcast uint f32 v 28 end 29 fn arg d view dsc i ulong k ulong to ulong 30 ret get d a add add NB mul i NW k 31 end 32 fn ldp d view dsc i ulong p edit ulong 32 to void 33 for k ulong 16 34 put p k arg d i k 35 end 36 end 37 fn pf p edit ulong 32 k ulong to f64 inline 38 ret bitcast f64 get p k 39 end 40 fn minu x ulong y ulong to ulong 41 if lt x y 42 ret x 43 end 44 ret y 45 end 46 note exp(x) in f64: Cody-Waite reduction by ln2 (hi + lo), degree-13 Taylor (remainder 5.9e-18), 47 note scale by 2^n built from the exponent bits. Below -708 gives 0; above 709 is clamped. 48 fn fexp x f64 to f64 49 if ne x x 50 ret x 51 end 52 if lt x -708.0f64 53 ret 0.0f64 54 end 55 y = fmin x 709.0f64 56 t = mul y 1.`4426950408889634f64` 57 n = long 0 58 if ge t 0.0f64 59 n = long add t 0.5f64 60 else 61 n = long sub t 0.5f64 62 end 63 t = f64 n 64 y = sub sub y mul t 0.`6931471805599453f64` mul t 2.`3190468138462996e`-17f64 65 t = 1.0f64 66 for j ulong 13 67 t = add 1.0f64 div mul y t f64 sub 13 j 68 end 69 ret mul t bitcast f64 shl ulong add n 1023 52 70 end 71 fn ftanh z f64 to f64 72 q = fabs z 73 e = fexp mul -2.0f64 q 74 t = div sub 1.0f64 e add 1.0f64 e 75 if lt z 0.0f64 76 ret neg t 77 end 78 ret t 79 end 80 note erf(x) = 2/sqrt(pi) exp(-x^2) sum (2x^2)^n x / (1*3*...*(2n+1)), all terms positive; 81 note |x| >= 6 gives +-1 (erfc(6) = 2.2e-17). 82 fn ferf x f64 to f64 83 q = fabs x 84 s = 1.0f64 85 if lt q 6.0f64 86 q2 = mul q q 87 tm = q 88 sm = q 89 for k ulong 400 90 tm = div mul tm mul 2.0f64 q2 f64 add mul 2 k 3 91 sm = add sm tm 92 if lt tm mul sm 1.0e-17f64 93 break 94 end 95 end 96 s = mul mul 1.`1283791670955126f64` fexp neg q2 sm 97 end 98 if lt x 0.0f64 99 ret neg s 100 end 101 ret s 102 end 103 fn fsilu v f64 to f64 104 ret div v add 1.0f64 fexp neg v 105 end 106 fn fgelu v f64 ap ulong to f64 107 if eq ap 1 108 u = mul 0.`7978845608028654f64` add v mul 0.`044715f64` mul v mul v v 109 ret mul mul 0.5f64 v add 1.0f64 ftanh u 110 end 111 ret mul mul 0.5f64 v add 1.0f64 ferf mul v 0.`7071067811865475f64` 112 end 113 note elementwise ops: 5 silu, 6 gelu (arg 6: 0 erf, 1 tanh), 7 swiglu silu(a)*b, 8 add, 9 mul, 114 note 10 bias b[x mod cols] (arg 5 cols), 11 relu. v is the value of input a at flat index x. 115 fn isew op ulong to ulong 116 if lt op 5 117 ret 0 118 end 119 if gt op 11 120 ret 0 121 end 122 ret 1 123 end 124 fn ewap ws edit uint * d view dsc i ulong v f64 x ulong to f64 125 op = arg d i 0 126 if eq op 5 127 ret fsilu v 128 end 129 if eq op 6 130 ret fgelu v arg d i 6 131 end 132 if eq op 7 133 ret mul fsilu v ldf ws add arg d i 2 x 134 end 135 if eq op 8 136 ret add v ldf ws add arg d i 2 x 137 end 138 if eq op 9 139 ret mul v ldf ws add arg d i 2 x 140 end 141 if eq op 10 142 ret add v ldf ws add arg d i 2 rem x arg d i 5 143 end 144 if eq op 11 145 ret fmax v 0.0f64 146 end 147 trap 2 148 end 149 note emit: the producer node i (ie >> 8) made value v at flat index x; apply the fused 150 note elementwise nodes i+1 .. e (ie & 255) in f64 and store once into node e's output. 151 fn emit ws edit uint * d view dsc ie ulong x ulong v f64 to void 152 e = and ie 255 153 j = add shr ie 8 1 154 while le j e bound 64 155 v = ewap ws d j v x 156 j = add j 1 157 end 158 stf ws add arg d e 3 x v 159 end 160 fn ew1 ws edit uint * d view dsc ie ulong x ulong to void 161 i = shr ie 8 162 emit ws d ie x ewap ws d i ldf ws add arg d i 1 x x 163 end 164 fn k_ew ws edit uint * d view dsc ie ulong to void 165 n = arg d shr ie 8 4 166 for c ulong add div n CH 1 167 o = mul c CH 168 if ge o n 169 break 170 end 171 for t ulong minu sub n o CH 172 ew1 ws d ie add o t 173 end 174 end 175 end 176 note RMSNormalization (ONNX opset 23, axis -1): y = x / sqrt(mean(x^2) + eps) * scale. 177 fn rmsrow ws edit uint * d view dsc ie ulong p edit ulong 32 r ulong to void 178 b = mul r get p 5 179 s = 0.0f64 180 for c ulong get p 5 181 v = ldfo ws add get p 1 add b c 182 s = add s mul v v 183 end 184 s = fsqrt add div s f64 get p 5 pf p 6 185 for c ulong get p 5 186 v = mul div ldfo ws add get p 1 add b c s ldfo ws add get p 2 c 187 emit ws d ie add b c v 188 end 189 end 190 fn k_rms ws edit uint * d view dsc ie ulong to void 191 p = new ulong 32 0 192 ldp d shr ie 8 p 193 for r ulong get p 4 194 rmsrow ws d ie p r 195 end 196 end 197 note LayerNormalization (ONNX, axis -1): (x - mean) / sqrt(var + eps) * scale + bias (arg 7, NONE = none). 198 fn lnrow ws edit uint * d view dsc ie ulong p edit ulong 32 r ulong to void 199 b = add get p 1 mul r get p 5 200 s = 0.0f64 201 for c ulong get p 5 202 s = add s ldfo ws add b c 203 end 204 s = div s f64 get p 5 205 q = 0.0f64 206 for c ulong get p 5 207 v = sub ldfo ws add b c s 208 q = add q mul v v 209 end 210 q = fsqrt add div q f64 get p 5 pf p 6 211 for c ulong get p 5 212 v = div sub ldfo ws add b c s q 213 v = mul v ldfo ws add get p 2 c 214 if ne get p 7 NONE 215 v = add v ldfo ws add get p 7 c 216 end 217 emit ws d ie add mul r get p 5 c v 218 end 219 end 220 fn k_ln ws edit uint * d view dsc ie ulong to void 221 p = new ulong 32 0 222 ldp d shr ie 8 p 223 for r ulong get p 4 224 lnrow ws d ie p r 225 end 226 end 227 note Softmax (ONNX opset 13, axis -1), max-subtracted. 228 fn smrow ws edit uint * d view dsc ie ulong p edit ulong 32 r ulong to void 229 b = add get p 1 mul r get p 5 230 m = ldfo ws b 231 for c ulong get p 5 232 m = fmax m ldfo ws add b c 233 end 234 s = 0.0f64 235 for c ulong get p 5 236 s = add s fexp sub ldfo ws add b c m 237 end 238 for c ulong get p 5 239 v = div fexp sub ldfo ws add b c m s 240 emit ws d ie add mul r get p 5 c v 241 end 242 end 243 fn k_smax ws edit uint * d view dsc ie ulong to void 244 p = new ulong 32 0 245 ldp d shr ie 8 p 246 for r ulong get p 4 247 smrow ws d ie p r 248 end 249 end 250 note RotaryEmbedding (ONNX opset 23): x [B,H,S,D] (arg 1), cos (2) / sin (4) caches [maxpos, rot/2], 251 note position ids (5) [B,S] uint32, B 6, H 7, S 8, D 9, rot 10 (0 = D), interleaved 11. 252 note p 16 = rot, 17 = rot/2, 18 = pair step (1 interleaved, rot/2 split), 19 = first-index stride 253 note (2 interleaved, 1 split), 20 = row base, 21 = cache row base. 254 fn ropepr ws edit uint * d view dsc ie ulong p edit ulong 32 k ulong to void 255 i1 = add get p 20 mul k get p 19 256 cs = ldfo ws add get p 2 add get p 21 k 257 sn = ldfo ws add get p 4 add get p 21 k 258 x1 = ldfo ws add get p 1 i1 259 x2 = ldfo ws add get p 1 add i1 get p 18 260 y = sub mul x1 cs mul x2 sn 261 emit ws d ie i1 y 262 y = add mul x2 cs mul x1 sn 263 emit ws d ie add i1 get p 18 y 264 end 265 fn roperow ws edit uint * d view dsc ie ulong p edit ulong 32 r ulong to void 266 s = rem r get p 8 267 b = div r mul get p 7 get p 8 268 put p 20 mul r get p 9 269 put p 21 mul ulong get ws add get p 5 add mul b get p 8 s get p 17 270 for k ulong get p 17 271 ropepr ws d ie p k 272 end 273 for k ulong sub get p 9 get p 16 274 s = add get p 20 add get p 16 k 275 emit ws d ie s ldfo ws add get p 1 s 276 end 277 end 278 fn k_rope ws edit uint * d view dsc ie ulong to void 279 p = new ulong 32 0 280 ldp d shr ie 8 p 281 put p 16 get p 10 282 if eq get p 16 0 283 put p 16 get p 9 284 end 285 put p 17 div get p 16 2 286 put p 18 get p 17 287 put p 19 1 288 if eq get p 11 1 289 put p 18 1 290 put p 19 2 291 end 292 for r ulong mul mul get p 6 get p 7 get p 8 293 roperow ws d ie p r 294 end 295 end 296 note Leaf loops (the register budget allows one loop with a few names per function). 297 note vdot: sum of ws[a+u] * ws[b+u*sb], u < n. 298 fn vdot ws edit uint * a ulong b ulong sb ulong n ulong to f64 299 s = 0.0f64 300 for u ulong n 301 s = add s mul ldf ws add a u ldf ws add b mul u sb 302 end 303 ret s 304 end 305 note vacc: acc[t] += av * ws[rb+t], t < nj. 306 fn vacc ws edit uint * acc edit f64 64 av f64 rb ulong nj ulong to void 307 for t ulong nj 308 put acc t add get acc t mul av ldf ws add rb t 309 end 310 end 311 note MatMul [M,K] x [K,N] (arg 8 = 1: B given as [N,K]), optional bias [N] (arg 7). Interim GEMM 312 note until aien-dev/omega-blas publishes its sgemm; f64 accumulation, 64-column blocks. 313 note p 20 = m, 21 = j0, 22 = columns in the block, 23 = row offset of a. 314 fn mmacc ws edit uint * p edit ulong 32 acc edit f64 64 to void 315 for t ulong get p 22 316 put acc t 0.0f64 317 end 318 if eq get p 8 0 319 for k ulong get p 6 320 vacc ws acc ldf ws add get p 23 k add get p 2 add mul k get p 5 get p 21 get p 22 321 end 322 ret 323 end 324 for t ulong get p 22 325 put acc t vdot ws get p 23 add get p 2 mul add get p 21 t get p 6 1 get p 6 326 end 327 end 328 fn mmblk ws edit uint * d view dsc ie ulong p edit ulong 32 acc edit f64 64 to void 329 mmacc ws p acc 330 for t ulong get p 22 331 av = get acc t 332 if ne get p 7 NONE 333 av = add av ldfo ws add get p 7 add get p 21 t 334 end 335 emit ws d ie add mul get p 20 get p 5 add get p 21 t av 336 end 337 end 338 fn mmrow ws edit uint * d view dsc ie ulong p edit ulong 32 to void 339 acc = new f64 64 0.0 340 for jb ulong add div get p 5 64 1 341 put p 21 mul jb 64 342 if ge get p 21 get p 5 343 break 344 end 345 put p 22 minu sub get p 5 get p 21 64 346 mmblk ws d ie p acc 347 end 348 end 349 fn k_mm ws edit uint * d view dsc ie ulong to void 350 p = new ulong 32 0 351 ldp d shr ie 8 p 352 for m ulong get p 4 353 put p 20 m 354 put p 23 add get p 1 mul m get p 6 355 mmrow ws d ie p 356 end 357 end 358 note Attention core shared by prefill and paged decode. st (f64): 0..511 scaled q row, 359 note 512..1023 output accumulator, 1024..1087 block scores, 1088 running max, 1089 running sum. 360 note p 22 = head dim, 24 = first key row, 25 = first value row, 26 = row stride, 27 = rows in 361 note block, 28 = scale (f64 bits), 29..31 per-row values of the caller. 362 fn adot ws edit uint * p edit ulong 32 st edit f64 1090 t ulong to f64 363 kr = add get p 24 mul t get p 26 364 s = 0.0f64 365 for u ulong get p 22 366 s = add s mul get st u ldf ws add kr u 367 end 368 ret s 369 end 370 fn aaxpy ws edit uint * p edit ulong 32 st edit f64 1090 vr ulong pv f64 to void 371 for u ulong get p 22 372 i = add 512 u 373 put st i add get st i mul pv ldf ws add vr u 374 end 375 end 376 fn ascl p edit ulong 32 st edit f64 1090 s f64 to void 377 for u ulong get p 22 378 put st add 512 u mul get st add 512 u s 379 end 380 end 381 fn ablk ws edit uint * p edit ulong 32 st edit f64 1090 to void 382 bm = -1.`0e300f64` 383 for t ulong get p 27 384 s = adot ws p st t 385 put st add 1024 t s 386 bm = fmax bm s 387 end 388 bm = fmax get st 1088 bm 389 s = fexp sub get st 1088 bm 390 put st 1089 mul get st 1089 s 391 ascl p st s 392 for t ulong get p 27 393 s = fexp sub get st add 1024 t bm 394 put st 1089 add get st 1089 s 395 aaxpy ws p st add get p 25 mul t get p 26 s 396 end 397 put st 1088 bm 398 end 399 fn ainit ws edit uint * p edit ulong 32 st edit f64 1090 qr ulong to void 400 for u ulong get p 22 401 put st u mul ldf ws add get p 1 add qr u pf p 28 402 put st add 512 u 0.0f64 403 end 404 put st 1088 -1.`0e300f64` 405 put st 1089 0.0f64 406 end 407 fn aout ws edit uint * d view dsc ie ulong st edit f64 1090 qr ulong to void 408 for u ulong arg d shr ie 8 10 409 emit ws d ie add qr u div get st add 512 u get st 1089 410 end 411 end 412 note Attention prefill (ONNX Attention opset 23, 4-D): q (1) [B,Hq,Sq,D], k (2) / v (4) [B,Hkv,Skv,D], 413 note o (3) [B,Hq,Sq,D]; B 5, Hq 6, Hkv 7, Sq 8, Skv 9, D 10, causal 11 (top-left: key j <= query i), 414 note scale 12 (f64 bits), key block 13 (1..64, 0 = 64). Flash-style online softmax per key block. 415 note p 29 = k/v head base, 30 = keys visible to this query. 416 fn aprep p edit ulong 32 r ulong to void 417 hk = div rem div r get p 8 get p 6 div get p 6 get p 7 418 put p 29 mul mul add mul div div r get p 8 get p 6 get p 7 hk get p 9 get p 10 419 put p 30 get p 9 420 if eq get p 11 1 421 put p 30 minu add rem r get p 8 1 get p 9 422 end 423 end 424 fn aprblk ws edit uint * p edit ulong 32 st edit f64 1090 kk ulong to void 425 j0 = mul kk get p 13 426 put p 24 add get p 2 add get p 29 mul j0 get p 10 427 put p 25 add get p 4 add get p 29 mul j0 get p 10 428 put p 27 minu sub get p 30 j0 get p 13 429 ablk ws p st 430 end 431 fn arow ws edit uint * d view dsc ie ulong p edit ulong 32 r ulong to void 432 st = new f64 1090 0.0 433 aprep p r 434 ainit ws p st mul r get p 10 435 for kk ulong div add get p 30 sub get p 13 1 get p 13 436 aprblk ws p st kk 437 end 438 aout ws d ie st mul r get p 10 439 end 440 fn k_attn ws edit uint * d view dsc ie ulong to void 441 p = new ulong 32 0 442 ldp d shr ie 8 p 443 if gt get p 10 512 444 trap 2 445 end 446 if eq get p 13 0 447 put p 13 64 448 end 449 if gt get p 13 64 450 trap 2 451 end 452 put p 22 get p 10 453 put p 26 get p 10 454 put p 28 get p 12 455 for r ulong mul mul get p 5 get p 6 get p 8 456 arow ws d ie p r 457 end 458 end 459 note Paged-KV decode attention: q (1) [B,Hq,D]; k (2) / v (4) caches [nblocks, bs, Hkv, D]; 460 note block table (5) [B, maxb] and lengths (6) [B] uint32; o (3) [B,Hq,D]; B 7, Hq 8, Hkv 9, 461 note D 10, bs 11 (1..64), maxb 12, scale 13 (f64 bits). One key block per cache page. 462 note p 29 = sequence, 30 = kv head, 31 = length. 463 fn dpage ws edit uint * p edit ulong 32 pg ulong to void 464 kr = ulong get ws add get p 5 add mul get p 29 get p 12 pg 465 kr = mul add mul mul kr get p 11 get p 9 get p 30 get p 10 466 put p 24 add get p 2 kr 467 put p 25 add get p 4 kr 468 put p 27 minu sub get p 31 mul pg get p 11 get p 11 469 end 470 fn drow ws edit uint * d view dsc ie ulong p edit ulong 32 r ulong to void 471 st = new f64 1090 0.0 472 put p 29 div r get p 8 473 put p 30 div rem r get p 8 div get p 8 get p 9 474 put p 31 ulong get ws add get p 6 get p 29 475 ainit ws p st mul r get p 10 476 for pg ulong div add get p 31 sub get p 11 1 get p 11 477 dpage ws p pg 478 ablk ws p st 479 end 480 aout ws d ie st mul r get p 10 481 end 482 fn k_dec ws edit uint * d view dsc ie ulong to void 483 p = new ulong 32 0 484 ldp d shr ie 8 p 485 if gt get p 10 512 486 trap 2 487 end 488 if gt get p 11 64 489 trap 2 490 end 491 put p 22 get p 10 492 put p 26 mul get p 9 get p 10 493 put p 28 get p 13 494 for r ulong mul get p 7 get p 8 495 drow ws d ie p r 496 end 497 end 498 note Conv (ONNX, 2-D, NCHW): x (1) [N,C,H,W], w (2) [O,C/g,kh,kw], y (3) [N,O,OH,OW], bias (4) [O] 499 note or NONE, scratch (5), N 6, C 7, H 8, W 9, O 10, arg 11 packs kh kw sh sw ph pw dh dw one byte 500 note each from the low byte (symmetric pads), groups 12, algorithm 13 (0 direct, 1 im2col + GEMM). 501 note p 16..23 = kh kw sh sw ph pw dh dw, 24 = OH, 25 = OW, 26 = C/g, 27 = O/g, 28 = C/g*kh*kw, 502 note 29 = OH*OW; 14, 15, 30, 31 are per-call values noted at each helper. 503 fn odim h ulong k ulong s ulong q ulong dl ulong to ulong 504 ret add div sub add h mul 2 q add mul dl sub k 1 1 s 1 505 end 506 fn cvprep d view dsc ie ulong p edit ulong 32 to void 507 ldp d shr ie 8 p 508 for k ulong 8 509 put p add 16 k and shr get p 11 mul 8 k 255 510 end 511 put p 24 odim get p 8 get p 16 get p 18 get p 20 get p 22 512 put p 25 odim get p 9 get p 17 get p 19 get p 21 get p 23 513 put p 26 div get p 7 get p 12 514 put p 27 div get p 10 get p 12 515 put p 28 mul mul get p 26 get p 16 get p 17 516 put p 29 mul get p 24 get p 25 517 end 518 note cvrow: one kernel row; xr = input row start, wr = weight row start, p 15 = x*sw (padded column). 519 fn cvrow ws edit uint * p edit ulong 32 xr ulong wr ulong to f64 520 s = 0.0f64 521 for kj ulong get p 17 522 cc = add get p 15 mul kj get p 23 523 if lt cc get p 21 524 continue 525 end 526 cc = sub cc get p 21 527 if ge cc get p 9 528 continue 529 end 530 s = add s mul ldf ws add xr cc ldf ws add wr kj 531 end 532 ret s 533 end 534 note cvch: input channel c of the group; p 31 = input group base, p 13 = weight base of o, 535 note p 14 = y*sh (padded row). 536 fn cvch ws edit uint * p edit ulong 32 c ulong to f64 537 xb = add get p 31 mul mul c get p 8 get p 9 538 wb = add get p 13 mul mul c get p 16 get p 17 539 s = 0.0f64 540 for ki ulong get p 16 541 r = add get p 14 mul ki get p 22 542 if lt r get p 20 543 continue 544 end 545 r = sub r get p 20 546 if ge r get p 8 547 continue 548 end 549 r = add xb mul r get p 9 550 c = add wb mul ki get p 17 551 s = add s cvrow ws p r c 552 end 553 ret s 554 end 555 note cvbase: p 30 = n*O + o; sets p 31 (input group base) and p 13 (weight base of o). 556 fn cvbase p edit ulong 32 no ulong to void 557 put p 30 no 558 o = rem no get p 10 559 g = div o get p 27 560 n = div no get p 10 561 n = add mul n get p 7 mul g get p 26 562 put p 31 add get p 1 mul n mul get p 8 get p 9 563 put p 13 add get p 2 mul o mul get p 26 mul get p 16 get p 17 564 end 565 fn cvpt ws edit uint * p edit ulong 32 q ulong to f64 566 put p 14 mul div q get p 25 get p 18 567 put p 15 mul rem q get p 25 get p 19 568 s = 0.0f64 569 for c ulong get p 26 570 s = add s cvch ws p c 571 end 572 if ne get p 4 NONE 573 s = add s ldf ws add get p 4 rem get p 30 get p 10 574 end 575 ret s 576 end 577 fn cvplane ws edit uint * d view dsc ie ulong p edit ulong 32 to void 578 for q ulong get p 29 579 s = cvpt ws p q 580 emit ws d ie add mul get p 30 get p 29 q s 581 end 582 end 583 fn cvdir ws edit uint * d view dsc ie ulong p edit ulong 32 to void 584 for no ulong mul get p 6 get p 10 585 cvbase p no 586 cvplane ws d ie p 587 end 588 end 589 note cvcrow: column-matrix row rr, output row y; p 14 = image/channel base, p 15 = column row base, 590 note p 31 = ki*dh (im2col: p 30 = n*groups + g). 591 fn cvcrow ws edit uint * p edit ulong 32 rr ulong y ulong to void 592 r = add mul y get p 18 get p 31 593 if lt r get p 20 594 r = NONE 595 else 596 r = sub r get p 20 597 if lt r get p 8 598 r = add get p 14 mul r get p 9 599 else 600 r = NONE 601 end 602 end 603 o = add get p 15 mul y get p 25 604 rr = mul rem rr get p 17 get p 23 605 for x ulong get p 25 606 cc = add mul x get p 19 rr 607 v = uint 0 608 if ne r NONE 609 if ge cc get p 21 610 cc = sub cc get p 21 611 if lt cc get p 9 612 v = get ws add r cc 613 end 614 end 615 end 616 put ws add o x v 617 end 618 end 619 fn cvcol ws edit uint * p edit ulong 32 rr ulong to void 620 c = div rr mul get p 16 get p 17 621 c = add mul rem get p 30 get p 12 get p 26 c 622 c = add mul div get p 30 get p 12 get p 7 c 623 put p 14 add get p 1 mul c mul get p 8 get p 9 624 put p 15 add get p 5 mul rr get p 29 625 put p 31 mul rem div rr get p 17 get p 16 get p 22 626 for y ulong get p 24 627 cvcrow ws p rr y 628 end 629 end 630 note cvacc: acc[t] = sum over r of w[o, r] * col[r, p 13 + t]; output channel p 31. 631 fn cvacc ws edit uint * p edit ulong 32 acc edit f64 64 nj ulong to void 632 for r ulong get p 28 633 av = ldfo ws add get p 2 add mul get p 31 get p 28 r 634 rb = add get p 5 add mul r get p 29 get p 13 635 vacc ws acc av rb nj 636 end 637 end 638 note cvmm: output channel p 31, output base p 14, bias value p 15 (f64 bits), positions p 13 .. +64. 639 fn cvmm ws edit uint * d view dsc ie ulong p edit ulong 32 to void 640 acc = new f64 64 0.0 641 nj = minu sub get p 29 get p 13 64 642 cvacc ws p acc nj 643 for t ulong nj 644 av = add get acc t pf p 15 645 emit ws d ie add get p 14 add get p 13 t av 646 end 647 end 648 fn cvoch ws edit uint * d view dsc ie ulong p edit ulong 32 o ulong to void 649 o = add mul rem get p 30 get p 12 get p 27 o 650 put p 31 o 651 o = add mul div get p 30 get p 12 get p 10 o 652 put p 14 mul o get p 29 653 put p 15 bitcast ulong 0.0f64 654 if ne get p 4 NONE 655 put p 15 bitcast ulong ldf ws add get p 4 get p 31 656 end 657 for pb ulong add div get p 29 64 1 658 put p 13 mul pb 64 659 if ge get p 13 get p 29 660 break 661 end 662 cvmm ws d ie p 663 end 664 end 665 fn cvgrp ws edit uint * d view dsc ie ulong p edit ulong 32 to void 666 for rr ulong get p 28 667 cvcol ws p rr 668 end 669 for o ulong get p 27 670 cvoch ws d ie p o 671 end 672 end 673 fn k_conv ws edit uint * d view dsc ie ulong to void 674 p = new ulong 32 0 675 cvprep d ie p 676 if eq get p 13 1 677 for ng ulong mul get p 6 get p 12 678 put p 30 ng 679 cvgrp ws d ie p 680 end 681 ret 682 end 683 cvdir ws d ie p 684 end 685 note MaxPool (16) / AveragePool (17) (ONNX, 2-D, NCHW): x 1, y 3, N*C 4, H 5, W 6, arg 7 packs 686 note kh kw sh sw ph pw; average divides by the in-bounds count unless arg 8 (count_include_pad) is 1. 687 note p 16..21 = kh kw sh sw ph pw, 24 = OH, 25 = OW, 26 = y*sh, 27 = x*sw, 28 = plane base, 688 note 29 = running value (f64 bits), 30 = in-bounds count. 689 fn plrow ws edit uint * p edit ulong 32 r ulong to void 690 s = pf p 29 691 for kj ulong get p 17 692 cc = add get p 27 kj 693 if lt cc get p 21 694 continue 695 end 696 cc = sub cc get p 21 697 if ge cc get p 6 698 continue 699 end 700 v = ldfo ws add get p 28 add mul r get p 6 cc 701 if eq get p 0 16 702 s = fmax s v 703 else 704 s = add s v 705 end 706 put p 30 add get p 30 1 707 end 708 put p 29 bitcast ulong s 709 end 710 fn plval ws edit uint * p edit ulong 32 q ulong to f64 711 put p 26 mul div rem q mul get p 24 get p 25 get p 25 get p 18 712 put p 27 mul rem q get p 25 get p 19 713 put p 28 add get p 1 mul div q mul get p 24 get p 25 mul get p 5 get p 6 714 put p 29 bitcast ulong 0.0f64 715 if eq get p 0 16 716 put p 29 bitcast ulong -1.`0e300f64` 717 end 718 put p 30 0 719 for ki ulong get p 16 720 r = add get p 26 ki 721 if lt r get p 20 722 continue 723 end 724 r = sub r get p 20 725 if ge r get p 5 726 continue 727 end 728 plrow ws p r 729 end 730 s = pf p 29 731 if eq get p 0 17 732 if eq get p 8 1 733 put p 30 mul get p 16 get p 17 734 end 735 s = div s f64 get p 30 736 end 737 ret s 738 end 739 fn k_pool ws edit uint * d view dsc ie ulong to void 740 p = new ulong 32 0 741 ldp d shr ie 8 p 742 for k ulong 6 743 put p add 16 k and shr get p 7 mul 8 k 255 744 end 745 put p 24 odim get p 5 get p 16 get p 18 get p 20 1 746 put p 25 odim get p 6 get p 17 get p 19 get p 21 1 747 for q ulong mul get p 4 mul get p 24 get p 25 748 emit ws d ie q plval ws p q 749 end 750 end 751 note GlobalAveragePool: x 1, y 3, N*C 4, H*W 5. 752 fn k_gap ws edit uint * d view dsc ie ulong to void 753 p = new ulong 32 0 754 ldp d shr ie 8 p 755 for n ulong get p 4 756 emit ws d ie n div vdot1 ws add get p 1 mul n get p 5 get p 5 f64 get p 5 757 end 758 end 759 fn vdot1 ws edit uint * a ulong n ulong to f64 760 s = 0.0f64 761 for u ulong n 762 s = add s ldf ws add a u 763 end 764 ret s 765 end 766 fn run1 ws edit uint * d view dsc ie ulong to void 767 op = arg d shr ie 8 0 768 if eq isew op 1 769 k_ew ws d ie 770 ret 771 end 772 if eq op 1 773 k_rms ws d ie 774 ret 775 end 776 if eq op 2 777 k_ln ws d ie 778 ret 779 end 780 if eq op 3 781 k_smax ws d ie 782 ret 783 end 784 if eq op 4 785 k_rope ws d ie 786 ret 787 end 788 if eq op 12 789 k_mm ws d ie 790 ret 791 end 792 if eq op 13 793 k_attn ws d ie 794 ret 795 end 796 if eq op 14 797 k_dec ws d ie 798 ret 799 end 800 if eq op 15 801 k_conv ws d ie 802 ret 803 end 804 if eq op 16 805 k_pool ws d ie 806 ret 807 end 808 if eq op 17 809 k_pool ws d ie 810 ret 811 end 812 if eq op 18 813 k_gap ws d ie 814 ret 815 end 816 trap 1 817 end 818 note isvirt: node i's output may stay in registers when it is not a graph output (flags bit 1) 819 note and no node after i + 1 names its offset in any argument (conservative), nor node i + 1's b. 820 fn isvirt d view dsc i ulong n ulong to ulong 821 if ne and arg d i 15 2 0 822 ret 0 823 end 824 y = arg d i 3 825 if eq arg d add i 1 2 y 826 ret 0 827 end 828 j = add i 2 829 while lt j n bound 64 830 for k ulong 14 831 if eq arg d j add k 1 y 832 ret 0 833 end 834 end 835 j = add j 1 836 end 837 ret 1 838 end 839 note chain: last node of the fused group that starts at node i. 840 fn chain d view dsc i ulong n ulong to ulong 841 e = i 842 while lt add e 1 n bound 64 843 if eq isew arg d add e 1 0 0 844 ret e 845 end 846 if ne arg d add e 1 1 arg d e 3 847 ret e 848 end 849 if eq isvirt d e n 0 850 ret e 851 end 852 e = add e 1 853 end 854 ret e 855 end 856 note dnn_run: execute the node list; with descriptor flag bit 0 set, fuse each producer with the 857 note elementwise nodes that consume only its output. Returns the number of passes run. 858 export fn dnn_run ws edit uint * d view dsc to ulong 859 if ne get d a 0 MAGIC 860 trap 1 861 end 862 n = get d a 1 863 if gt n 31 864 trap 1 865 end 866 ps = ulong 0 867 i = ulong 0 868 while lt i n bound 64 869 e = i 870 if eq and get d a 2 1 1 871 e = chain d i n 872 end 873 run1 ws d or shl i 8 e 874 ps = add ps 1 875 i = add e 1 876 end 877 ret ps 878 end 879 export fn dnn_abi to ulong 880 ret MAGIC 881 end