interplane.Get Omega
Read in your language

omega-dnn

aien-dev/omega-dnn

Brought by drakestapleton

branch main Branches Tags History

omega-dnn / om / dnn.om

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