interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / src / tensor / omega_tensor_cpu.om

637 lines · 12731 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note omega_tensor_cpu.om -- Pure seeded OM-2 CPU realization and E1 reduction seam for M20 OMEGA_TENSOR.
3
4const OT_OK long 0
5const OT_ERR_BAD_ARGS long -1
6const OT_ERR_RANK long -2
7const OT_ERR_SHAPE long -3
8const OT_ERR_DTYPE long -4
9const OT_ERR_STALE long -5
10const OT_ERR_BOUNDS long -6
11const OT_ERR_NOT_CONTIGUOUS long -7
12const OT_ERR_CAPACITY long -8
13const OT_ERR_AXIS long -9
14const OT_ERR_NUMERIC long -10
15const OT_ERR_REALIZATION long -11
16const OT_ERR_MASK long -12
17
18const OT_DT_F32 ulong 1
19const OT_DT_F16 ulong 2
20const OT_DT_BF16 ulong 3
21
22const OT_TU_SQRT ulong 0
23const OT_TU_EXP2 ulong 1
24const OT_TU_LOG2 ulong 2
25const OT_TU_SIGMOID ulong 3
26const OT_TU_TANH ulong 4
27const OT_TU_EXP ulong 5
28const OT_TU_LOG ulong 6
29const OT_TU_RELU ulong 7
30const OT_TU_RSQRT ulong 8
31const OT_TU_ERF ulong 9
32const OT_TU_GELU ulong 10
33const OT_TU_SIN ulong 11
34const OT_TU_COS ulong 12
35const OT_TU_NEG ulong 13
36const OT_TU_COUNT ulong 14
37
38const OT_TB_ADD ulong 0
39const OT_TB_SUB ulong 1
40const OT_TB_MUL ulong 2
41const OT_TB_DIV ulong 3
42const OT_TB_MIN ulong 4
43const OT_TB_MAX ulong 5
44const OT_TB_SEL_GE ulong 6
45const OT_TB_SEL_LT ulong 7
46const OT_TB_SEL_LE ulong 8
47const OT_TB_SEL_GT ulong 9
48const OT_TB_SEL_EQ ulong 10
49const OT_TB_SEL_NE ulong 11
50const OT_TB_SEL_NUM ulong 12
51const OT_TB_SEL_NAN ulong 13
52const OT_TB_SEL_LTU ulong 14
53const OT_TB_SEL_LEU ulong 15
54const OT_TB_SEL_GTU ulong 16
55const OT_TB_SEL_GEU ulong 17
56const OT_TB_SEL_EQU ulong 18
57const OT_TB_SEL_NEU ulong 19
58const OT_TB_CMP_EQ ulong 20
59const OT_TB_CMP_NE ulong 21
60const OT_TB_CMP_LT ulong 22
61const OT_TB_CMP_LE ulong 23
62const OT_TB_CMP_GT ulong 24
63const OT_TB_CMP_GE ulong 25
64const OT_TB_COUNT ulong 26
65
66const OT_TR_SUM ulong 0
67const OT_TR_MAX ulong 1
68const OT_TR_MIN ulong 2
69const OT_TR_MEAN ulong 3
70const OT_TR_COUNT ulong 4
71
72const OT_QNAN_BITS uint 0x7fc00000
73const OT_PINF_BITS uint 0x7f800000
74const OT_NINF_BITS uint 0xff800000
75const OT_NEG0_BITS uint 0x80000000
76const OT_ONE_BITS uint 0x3f800000
77
78fn ot_is_nan u uint to bool
79 ret gt and u uint 0x7fffffff OT_PINF_BITS
80end
81
82fn ot_relu_bits u uint to uint
83 if gt and u uint 0x7fffffff OT_PINF_BITS
84 ret OT_QNAN_BITS
85 end
86 if ne shr u 31 0
87 ret uint 0
88 end
89 ret u
90end
91
92fn ot_neg_bits u uint to uint
93 if gt and u uint 0x7fffffff OT_PINF_BITS
94 ret OT_QNAN_BITS
95 end
96 ret xor u OT_NEG0_BITS
97end
98
99fn ot_fmnmx_min a f32 b f32 to f32
100 ua = bitcast uint a
101 ub = bitcast uint b
102 if ot_is_nan ua
103 if ot_is_nan ub
104 ret bitcast f32 OT_QNAN_BITS
105 end
106 ret b
107 end
108 if ot_is_nan ub
109 ret a
110 end
111 if eq a b
112 if eq ua OT_NEG0_BITS
113 ret a
114 end
115 ret b
116 end
117 if lt a b
118 ret a
119 end
120 ret b
121end
122
123fn ot_fmnmx_max a f32 b f32 to f32
124 ua = bitcast uint a
125 ub = bitcast uint b
126 if ot_is_nan ua
127 if ot_is_nan ub
128 ret bitcast f32 OT_QNAN_BITS
129 end
130 ret b
131 end
132 if ot_is_nan ub
133 ret a
134 end
135 if eq a b
136 if eq ua uint 0
137 ret a
138 end
139 ret b
140 end
141 if gt a b
142 ret a
143 end
144 ret b
145end
146
147fn ot_cmp_pred op ulong a f32 b f32 to bool
148 unord = or ot_is_nan bitcast uint a ot_is_nan bitcast uint b
149 if or eq op OT_TB_SEL_GE eq op OT_TB_CMP_GE
150 if unord
151 ret false
152 end
153 ret ge a b
154 end
155 if or eq op OT_TB_SEL_LT eq op OT_TB_CMP_LT
156 if unord
157 ret false
158 end
159 ret lt a b
160 end
161 if or eq op OT_TB_SEL_LE eq op OT_TB_CMP_LE
162 if unord
163 ret false
164 end
165 ret le a b
166 end
167 if or eq op OT_TB_SEL_GT eq op OT_TB_CMP_GT
168 if unord
169 ret false
170 end
171 ret gt a b
172 end
173 if or eq op OT_TB_SEL_EQ eq op OT_TB_CMP_EQ
174 if unord
175 ret false
176 end
177 ret eq a b
178 end
179 if or eq op OT_TB_SEL_NE eq op OT_TB_CMP_NE
180 if unord
181 ret false
182 end
183 ret ne a b
184 end
185 if eq op OT_TB_SEL_NUM
186 ret not unord
187 end
188 if eq op OT_TB_SEL_NAN
189 ret unord
190 end
191 if eq op OT_TB_SEL_LTU
192 if unord
193 ret true
194 end
195 ret lt a b
196 end
197 if eq op OT_TB_SEL_LEU
198 if unord
199 ret true
200 end
201 ret le a b
202 end
203 if eq op OT_TB_SEL_GTU
204 if unord
205 ret true
206 end
207 ret gt a b
208 end
209 if eq op OT_TB_SEL_GEU
210 if unord
211 ret true
212 end
213 ret ge a b
214 end
215 if eq op OT_TB_SEL_EQU
216 if unord
217 ret true
218 end
219 ret eq a b
220 end
221 if unord
222 ret true
223 end
224 ret ne a b
225end
226
227fn ot_binary_scalar op ulong a f32 b f32 to f32
228 if eq op OT_TB_ADD
229 ret add a b
230 end
231 if eq op OT_TB_SUB
232 ret sub a b
233 end
234 if eq op OT_TB_MUL
235 ret mul a b
236 end
237 if eq op OT_TB_DIV
238 ret div a b
239 end
240 if eq op OT_TB_MIN
241 ret ot_fmnmx_min a b
242 end
243 if eq op OT_TB_MAX
244 ret ot_fmnmx_max a b
245 end
246 p = ot_cmp_pred op a b
247 if ge op OT_TB_CMP_EQ
248 if p
249 ret 1.0f32
250 end
251 ret 0.0f32
252 end
253 if p
254 ret a
255 end
256 ret b
257end
258
259note IEEE binary32 <-> bfloat16 (RNE) and binary32 <-> binary16 (RNE)
260fn ot_f32_to_bf16 u uint to uint
261 if gt and u uint 0x7fffffff OT_PINF_BITS
262 ret or uint 0x7fc0 and shr u 16 uint 0x8000
263 end
264 lsb = and shr u 16 uint 1
265 rb = add uint 0x7fff lsb
266 ret and shr add u rb 16 uint 0xffff
267end
268
269fn ot_bf16_to_f32 h uint to uint
270 ret shl h 16
271end
272
273fn ot_f32_to_f16 u uint to uint
274 sign = and shr u 16 uint 0x8000
275 exp = int and shr u 23 uint 0xff
276 mant = and u uint 0x7fffff
277 if eq exp 255
278 if ne mant 0
279 ret or sign uint 0x7e00
280 end
281 ret or sign uint 0x7c00
282 end
283 e = sub exp 112
284 if ge e 31
285 ret or sign uint 0x7c00
286 end
287 if le e 0
288 if lt e -10
289 ret sign
290 end
291 m = or mant uint 0x800000
292 sh = uint sub 14 e
293 half = shr m sh
294 rem_m = and m sub shl uint 1 sh uint 1
295 mid = shl uint 1 sub sh uint 1
296 if gt rem_m mid
297 half = add half uint 1
298 else
299 if eq rem_m mid
300 if ne and half uint 1 uint 0
301 half = add half uint 1
302 end
303 end
304 end
305 ret or sign half
306 end
307 h = or shl uint e 10 shr mant 13
308 rem_m = and mant uint 0x1fff
309 if gt rem_m uint 0x1000
310 h = add h uint 1
311 else
312 if eq rem_m uint 0x1000
313 if ne and h uint 1 uint 0
314 h = add h uint 1
315 end
316 end
317 end
318 ret or sign h
319end
320
321fn ot_f16_to_f32 h uint to uint
322 sign = shl and h uint 0x8000 16
323 exp = and shr h 10 uint 0x1f
324 mant = and h uint 0x3ff
325 if eq exp 31
326 if ne mant 0
327 ret OT_QNAN_BITS
328 end
329 ret or sign OT_PINF_BITS
330 end
331 if eq exp 0
332 if eq mant 0
333 ret sign
334 end
335 e = uint 113
336 m = mant
337 while eq and m uint 0x400 uint 0 bound 12
338 m = shl m 1
339 e = sub e uint 1
340 end
341 m = and m uint 0x3ff
342 ret or sign or shl e 23 shl m 13
343 end
344 ret or sign or shl add exp uint 112 23 shl mant 13
345end
346
347note Pure OM-2 F32 transcendentals (Cody-Waite + Horner FMA)
348fn ot_pow2i k int to f32
349 ret bitcast f32 shl uint add k 127 23
350end
351
352fn ot_scale2 m f32 k int to f32
353 if gt k 127
354 ret mul mul m ot_pow2i 127 ot_pow2i sub k 127
355 end
356 if lt k -126
357 ret mul mul m ot_pow2i add k 126 ot_pow2i -126
358 end
359 ret mul m ot_pow2i k
360end
361
362fn ot_math_exp x f32 to f32
363 if ot_is_nan bitcast uint x
364 ret bitcast f32 OT_QNAN_BITS
365 end
366 if gt x 88.722839f32
367 ret bitcast f32 OT_PINF_BITS
368 end
369 if lt x -103.97208f32
370 ret 0.0f32
371 end
372 magic = bitcast f32 uint 0x4b400000
373 t = add mul x 1.44269504f32 magic
374 kf = sub t magic
375 k = sub int bitcast uint t int 0x4b400000
376 r = fma sub 0.0f32 kf 0.69314575f32 x
377 r = fma sub 0.0f32 kf 0.0000014286068f32 r
378 p = f32 0.0001984127f32
379 p = fma p r 0.0013888889f32
380 p = fma p r 0.0083333338f32
381 p = fma p r 0.041666668f32
382 p = fma p r 0.16666667f32
383 p = fma p r 0.5f32
384 p = fma p r 1.0f32
385 m = fma p r 1.0f32
386 ret ot_scale2 m k
387end
388
389fn ot_math_exp2 x f32 to f32
390 if ot_is_nan bitcast uint x
391 ret bitcast f32 OT_QNAN_BITS
392 end
393 if ge x 128.0f32
394 ret bitcast f32 OT_PINF_BITS
395 end
396 if lt x -150.0f32
397 ret 0.0f32
398 end
399 ret ot_math_exp mul x 0.69314718f32
400end
401
402fn ot_math_log x f32 to f32
403 u = bitcast uint x
404 if ot_is_nan u
405 ret bitcast f32 OT_QNAN_BITS
406 end
407 if eq and u uint 0x7fffffff uint 0
408 ret bitcast f32 OT_NINF_BITS
409 end
410 if ne shr u 31 0
411 ret bitcast f32 OT_QNAN_BITS
412 end
413 if eq u OT_PINF_BITS
414 ret x
415 end
416 e = sub int shr u 23 127
417 m = bitcast f32 or and u uint 0x007fffff uint 0x3f800000
418 if gt m 1.41421356f32
419 m = mul m 0.5f32
420 e = add e 1
421 end
422 z = div sub m 1.0f32 add m 1.0f32
423 z2 = mul z z
424 p = f32 0.14285715f32
425 p = fma p z2 0.2f32
426 p = fma p z2 0.33333334f32
427 p = fma p z2 1.0f32
428 lnm = mul 2.0f32 mul z p
429 ret fma f32 e 0.69314718f32 lnm
430end
431
432fn ot_math_log2 x f32 to f32
433 ret mul ot_math_log x 1.44269504f32
434end
435
436fn ot_math_sigmoid x f32 to f32
437 if ot_is_nan bitcast uint x
438 ret bitcast f32 OT_QNAN_BITS
439 end
440 if ge x 0.0f32
441 em = ot_math_exp sub 0.0f32 x
442 ret div 1.0f32 add 1.0f32 em
443 end
444 ep = ot_math_exp x
445 ret div ep add 1.0f32 ep
446end
447
448fn ot_math_tanh x f32 to f32
449 if ot_is_nan bitcast uint x
450 ret bitcast f32 OT_QNAN_BITS
451 end
452 ax = fabs x
453 if gt ax 10.0f32
454 if lt x 0.0f32
455 ret -1.0f32
456 end
457 ret 1.0f32
458 end
459 e2 = ot_math_exp mul 2.0f32 ax
460 t = div sub e2 1.0f32 add e2 1.0f32
461 if lt x 0.0f32
462 ret sub 0.0f32 t
463 end
464 ret t
465end
466
467fn ot_math_erf x f32 to f32
468 if ot_is_nan bitcast uint x
469 ret bitcast f32 OT_QNAN_BITS
470 end
471 ax = fabs x
472 if ge ax 4.0f32
473 if lt x 0.0f32
474 ret -1.0f32
475 end
476 ret 1.0f32
477 end
478 t = div 1.0f32 fma 0.3275911f32 ax 1.0f32
479 p = f32 1.0614054f32
480 p = fma p t -1.4531520f32
481 p = fma p t 1.4214137f32
482 p = fma p t -0.28449674f32
483 p = fma p t 0.25482959f32
484 y = sub 1.0f32 mul mul p t ot_math_exp sub 0.0f32 mul ax ax
485 if lt x 0.0f32
486 ret sub 0.0f32 y
487 end
488 ret y
489end
490
491fn ot_math_sin x f32 to f32
492 if ot_is_nan bitcast uint x
493 ret bitcast f32 OT_QNAN_BITS
494 end
495 if gt fabs x 4194304.0f32
496 ret bitcast f32 OT_QNAN_BITS
497 end
498 magic = bitcast f32 uint 0x4b400000
499 t = add mul x 0.63661977f32 magic
500 kf = sub t magic
501 q = and sub int bitcast uint t int 0x4b400000 3
502 r = fma sub 0.0f32 kf 1.5707964f32 x
503 r2 = mul r r
504 res = f32 0.0f32
505 if eq and q 1 0
506 p = f32 -0.0001984127f32
507 p = fma p r2 0.0083333338f32
508 p = fma p r2 -0.16666667f32
509 res = fma mul p r2 r r
510 else
511 p = f32 0.00002480159f32
512 p = fma p r2 -0.0013888889f32
513 p = fma p r2 0.041666668f32
514 p = fma p r2 -0.5f32
515 res = fma p r2 1.0f32
516 end
517 if ge q 2
518 ret sub 0.0f32 res
519 end
520 ret res
521end
522
523fn ot_math_cos x f32 to f32
524 if ot_is_nan bitcast uint x
525 ret bitcast f32 OT_QNAN_BITS
526 end
527 if gt fabs x 4194304.0f32
528 ret bitcast f32 OT_QNAN_BITS
529 end
530 ret ot_math_sin add x 1.5707964f32
531end
532
533fn ot_unary_scalar op ulong x f32 to f32
534 if eq op OT_TU_SQRT
535 ret fsqrt x
536 end
537 if eq op OT_TU_RELU
538 ret bitcast f32 ot_relu_bits bitcast uint x
539 end
540 if eq op OT_TU_NEG
541 ret bitcast f32 ot_neg_bits bitcast uint x
542 end
543 if eq op OT_TU_EXP2
544 ret ot_math_exp2 x
545 end
546 if eq op OT_TU_LOG2
547 ret ot_math_log2 x
548 end
549 if eq op OT_TU_SIGMOID
550 ret ot_math_sigmoid x
551 end
552 if eq op OT_TU_TANH
553 ret ot_math_tanh x
554 end
555 if eq op OT_TU_EXP
556 ret ot_math_exp x
557 end
558 if eq op OT_TU_LOG
559 ret ot_math_log x
560 end
561 if eq op OT_TU_RSQRT
562 ret div 1.0f32 fsqrt x
563 end
564 if eq op OT_TU_ERF
565 ret ot_math_erf x
566 end
567 if eq op OT_TU_GELU
568 ret mul 0.5f32 mul x add 1.0f32 ot_math_erf mul x 0.70710678f32
569 end
570 if eq op OT_TU_SIN
571 ret ot_math_sin x
572 end
573 ret ot_math_cos x
574end
575
576note E1 reduction seam: RECURSIVE_TILE32_PAIRWISE_TREE_LANE_DELTA_16_8_4_2_1_PAD_IDENTITY_MIN_ONE_LEVEL
577global ot_red_a f32 16384
578global ot_red_b f32 16384
579
580fn ot_red_combine op ulong a f32 b f32 to f32
581 if eq op OT_TR_MAX
582 ret ot_fmnmx_max a b
583 end
584 if eq op OT_TR_MIN
585 ret ot_fmnmx_min a b
586 end
587 ret add a b
588end
589
590fn ot_red_tile32 op ulong tile edit f32 32 to f32
591 d = ulong 16
592 for s ulong 5
593 lim = sub 32 d
594 for i ulong lim
595 put tile i ot_red_combine op get tile i get tile add i d
596 end
597 d = shr d 1
598 end
599 ret get tile 0
600end
601
602note Caller populates ot_red_a[0..n) before calling ot_reduce_cpu.
603fn ot_reduce_cpu op ulong n ulong to f32
604 pad = bitcast f32 OT_NEG0_BITS
605 if or eq op OT_TR_MAX eq op OT_TR_MIN
606 pad = bitcast f32 OT_QNAN_BITS
607 end
608 len_c = n
609 tile = new f32 32 0.0f32
610 while true bound 16
611 nlen = div add len_c 31 32
612 for j ulong nlen bound 1024
613 base = mul j 32
614 for k ulong 32
615 idx = add base k
616 if lt idx len_c
617 put tile k get ot_red_a idx
618 else
619 put tile k pad
620 end
621 end
622 put ot_red_b j ot_red_tile32 op tile
623 end
624 for j ulong nlen bound 1024
625 put ot_red_a j get ot_red_b j
626 end
627 len_c = nlen
628 if le len_c 1
629 break
630 end
631 end
632 res = get ot_red_a 0
633 if eq op OT_TR_MEAN
634 ret div res f32 n
635 end
636 ret res
637end