aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / om / qwen-native / blocks / math.om
| 1 | note Scalar operators from Core om/flint/ops.om, names scoped for composition. |
| 2 | fn qb_exp x f64 to f64 |
| 3 | if lt x -708.0f64 |
| 4 | ret 0.0f64 |
| 5 | end |
| 6 | if gt x 709.0f64 |
| 7 | ret div 1.0f64 0.0f64 |
| 8 | end |
| 9 | kf = mul x 1.442695040888963407f64 |
| 10 | if lt kf 0.0f64 |
| 11 | kf = sub kf 0.5f64 |
| 12 | else |
| 13 | kf = add kf 0.5f64 |
| 14 | end |
| 15 | k = long kf |
| 16 | fk = f64 k |
| 17 | r = sub sub x mul fk 0.6931471803691238165f64 mul fk 1.908214929270587816e-10f64 |
| 18 | p = 1.0f64 |
| 19 | for j ulong 13 |
| 20 | p = add 1.0f64 mul div r f64 sub 13 j p |
| 21 | end |
| 22 | s = bitcast f64 shl ulong add k 1023 52 |
| 23 | ret mul p s |
| 24 | end |
| 25 | |
| 26 | fn qb_sig x f64 to f64 |
| 27 | ret div 1.0f64 add 1.0f64 qb_exp neg x |
| 28 | end |
| 29 | |
| 30 | note sin (c false) or cos (c true) of x in [0, 2^20): k = round(x 2 / pi), reduction by |
| 31 | note pio2_hi (33 bits) and pio2_lo, Taylor through degree 21 on |r| <= pi / 4 |
| 32 | fn qb_sc x f64 c bool to f64 |
| 33 | k = ulong add mul x 0.6366197723675813431f64 0.5f64 |
| 34 | fk = f64 k |
| 35 | r = sub sub x mul fk 1.570796326734125614f64 mul fk 6.077100506506192601e-11f64 |
| 36 | z = mul r r |
| 37 | s = 1.0f64 |
| 38 | co = 1.0f64 |
| 39 | for j ulong 10 |
| 40 | m = f64 mul 2 sub 10 j |
| 41 | s = sub 1.0f64 mul div z mul m add m 1.0f64 s |
| 42 | co = sub 1.0f64 mul div z mul sub m 1.0f64 m co |
| 43 | end |
| 44 | s = mul r s |
| 45 | q = k |
| 46 | if c |
| 47 | q = add q 1 |
| 48 | end |
| 49 | q = and q 3 |
| 50 | if eq q 0 |
| 51 | ret s |
| 52 | end |
| 53 | if eq q 1 |
| 54 | ret co |
| 55 | end |
| 56 | if eq q 2 |
| 57 | ret neg s |
| 58 | end |
| 59 | ret neg co |
| 60 | end |
| 61 | |
| 62 | note log1p(exp(x)) = max(x,0) + 2 atanh(t/(2+t)), t=exp(-abs(x)). |
| 63 | note The series argument has magnitude at most one third. |
| 64 | fn qb_softplus x f32 to f32 |
| 65 | if gt x 20.0f32 |
| 66 | ret x |
| 67 | end |
| 68 | t = qb_exp neg fabs f64 x |
| 69 | u = div t add 2.0f64 t |
| 70 | v = mul u u |
| 71 | p = 0.0f64 |
| 72 | for j ulong 14 |
| 73 | p = add div 1.0f64 f64 sub 27 mul 2 j mul p v |
| 74 | end |
| 75 | ret f32 add f64 fmax x 0.0f32 mul mul 2.0f64 u p |
| 76 | end |
| 77 | |
| 78 | global qb_next ulong 64 |
| 79 | global qb_model ulong 64 |
| 80 | |
| 81 | fn qb_check model ulong layer ulong pos ulong full bool to void |
| 82 | if and and ne model 27 ne model 35 ne model 38 |
| 83 | trap 5 |
| 84 | end |
| 85 | if or or ge layer 64 and eq model 35 ge layer 40 and eq model 38 ge layer 48 |
| 86 | trap 5 |
| 87 | end |
| 88 | if ne full eq rem layer 4 3 |
| 89 | trap 5 |
| 90 | end |
| 91 | if ge pos 8192 |
| 92 | trap 5 |
| 93 | end |
| 94 | if ne pos 0 |
| 95 | if or ne pos get qb_next layer ne model get qb_model layer |
| 96 | trap 5 |
| 97 | end |
| 98 | end |
| 99 | end |