omega-helix
This project has not described itself yet.
Brought by drakestapleton
omega-helix / native / univ / umath.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note native/univ/umath.om: First-party f64 exp, log, sin, cos, silu, gelu, IEEE-754 bf16/f16/f32 |
| 3 | note converters, splitmix64, FNV-1a 64, and buffered stdout for Omega Helix Universal (OLLMCK04). |
| 4 | note All math constants verified via WolframLanguageEvaluator (2026-11-25). |
| 5 | |
| 6 | const UMOUT ulong 4096 |
| 7 | global uout uchar 4096 |
| 8 | global uon ulong 1 |
| 9 | |
| 10 | note ug: universal geometry & config slots (64 ulongs) |
| 11 | note 0 V 1 T 2 D 3 H 4 H_kv 5 F 6 L 7 B |
| 12 | note 8 E=D/H 9 KVD=H_kv*E 10 P 11 LV (layer apps) |
| 13 | note 12 arch_flags (1=RoPE, 2=SwiGLU, 4=untied_lm, 8=QK_norm, 16=AdamW_cos, 32=GELU, 64=GeGLU) |
| 14 | note 13 rope_theta_milli 14 rms_eps_bits 15 wd_bits 16 warmup |
| 15 | note 17 LP 18 LC 19 NP 20 seed 21 step 22 total 23 lr_bits |
| 16 | note 24 CT (active context len <= T) 25 tok_bytes (1=u8, 2=u16, 4=u32) 26 data_arg 27 data_N |
| 17 | note 28 has_rope 29 is_gated 30 has_untied 31 has_qknorm |
| 18 | note 32 WPE_OFF 33 LAY0 34 LSTR 35 GF_OFF 36 LMH_OFF |
| 19 | note 37 ASTR (per-app activation stride) 38 PSTR (H*T*T) |
| 20 | note 39 ACT_HF 40 ACT_SF 41 ACT_Z 42 ACT_PR 43 ACT_DH 44 ACT_DA |
| 21 | note 45 ACT_DQ 46 ACT_DK 47 ACT_DV 48 ACT_DO 49 ACT_DZ 50 ACT_DF1 51 ACT_DF2 52 ACT_END |
| 22 | note 53 per-layer field offsets: wq=D, wk=D+D*D, wv=wk+D*KVD, wo=wv+D*KVD |
| 23 | note 54 O_QN (rel to layer) 55 O_KN 56 O_G2 57 O_W1 58 O_WUP 59 O_W2 |
| 24 | note 60 magic_ver (4=OLLMCK04) 61 freeze_layers (for growpro/trainf; 0=none) |
| 25 | global ug ulong 64 |
| 26 | |
| 27 | fn usmx x ulong to ulong |
| 28 | z = add x 11400714819323198485 |
| 29 | z = mul xor z shr z 30 13787848793156543929 |
| 30 | z = mul xor z shr z 27 10723151780598845931 |
| 31 | ret xor z shr z 31 |
| 32 | end |
| 33 | |
| 34 | fn ufexp x f64 to f64 |
| 35 | if ne x x |
| 36 | ret x |
| 37 | end |
| 38 | if gt x 709.782712893384 |
| 39 | ret bitcast f64 ulong 9218868437227405312 |
| 40 | end |
| 41 | if lt x -745.1332191019412 |
| 42 | ret 0.0f64 |
| 43 | end |
| 44 | kf = mul x 1.4426950408889634 |
| 45 | if ge kf 0.0 |
| 46 | kf = add kf 0.5 |
| 47 | else |
| 48 | kf = sub kf 0.5 |
| 49 | end |
| 50 | k = long kf |
| 51 | fk = f64 k |
| 52 | r = sub sub x mul fk 0.6931471803691238 mul fk 1.9082149292705877e-10 |
| 53 | p = add mul r 1.6059043836821613e-10 2.08767569878681e-9 |
| 54 | p = add mul p r 2.505210838544172e-8 |
| 55 | p = add mul p r 2.755731922398589e-7 |
| 56 | p = add mul p r 2.7557319223985893e-6 |
| 57 | p = add mul p r 0.0000248015873015873 |
| 58 | p = add mul p r 0.0001984126984126984 |
| 59 | p = add mul p r 0.001388888888888889 |
| 60 | p = add mul p r 0.008333333333333333 |
| 61 | p = add mul p r 0.041666666666666664 |
| 62 | p = add mul p r 0.16666666666666666 |
| 63 | p = add mul p r 0.5 |
| 64 | p = add mul p r 1.0 |
| 65 | p = add mul p r 1.0 |
| 66 | k1 = div k 2 |
| 67 | k2 = sub k k1 |
| 68 | s1 = bitcast f64 shl ulong add k1 1023 52 |
| 69 | s2 = bitcast f64 shl ulong add k2 1023 52 |
| 70 | ret mul mul p s1 s2 |
| 71 | end |
| 72 | |
| 73 | fn uflog x f64 to f64 |
| 74 | if ne x x |
| 75 | ret x |
| 76 | end |
| 77 | if lt x 0.0 |
| 78 | ret bitcast f64 ulong 9221120237041090560 |
| 79 | end |
| 80 | if eq x 0.0 |
| 81 | ret bitcast f64 ulong 18442240474082181120 |
| 82 | end |
| 83 | b = bitcast ulong x |
| 84 | if eq b 9218868437227405312 |
| 85 | ret x |
| 86 | end |
| 87 | e = long 0 |
| 88 | if lt b 4503599627370496 |
| 89 | b = bitcast ulong mul x 18014398509481984.0 |
| 90 | e = long -54 |
| 91 | end |
| 92 | e = add e sub long shr b 52 1023 |
| 93 | m = bitcast f64 or and b 4503599627370495 4607182418800017408 |
| 94 | if gt m 1.4142135623730951 |
| 95 | m = mul m 0.5 |
| 96 | e = add e 1 |
| 97 | end |
| 98 | f = sub m 1.0 |
| 99 | s = div f add f 2.0 |
| 100 | z = mul s s |
| 101 | q = add mul z 0.043478260869565216 0.047619047619047616 |
| 102 | q = add mul q z 0.05263157894736842 |
| 103 | q = add mul q z 0.058823529411764705 |
| 104 | q = add mul q z 0.06666666666666667 |
| 105 | q = add mul q z 0.07692307692307693 |
| 106 | q = add mul q z 0.09090909090909091 |
| 107 | q = add mul q z 0.1111111111111111 |
| 108 | q = add mul q z 0.14285714285714285 |
| 109 | q = add mul q z 0.2 |
| 110 | q = add mul q z 0.3333333333333333 |
| 111 | q = add mul q z 1.0 |
| 112 | lm = mul add s s q |
| 113 | fe = f64 e |
| 114 | ret add mul fe 0.6931471803691238 add lm mul fe 1.9082149292705877e-10 |
| 115 | end |
| 116 | |
| 117 | note sin(r) for |r| <= pi/4 (Wolfram Taylor coefficients 1/3!..1/15!) |
| 118 | fn ufsin_p r f64 to f64 |
| 119 | z = mul r r |
| 120 | p = add mul z -7.647163731819816e-13 1.6059043836821614e-10 |
| 121 | p = add mul p z -2.505210838544172e-8 |
| 122 | p = add mul p z 2.755731922398589e-6 |
| 123 | p = add mul p z -0.0001984126984126984 |
| 124 | p = add mul p z 0.008333333333333333 |
| 125 | p = add mul p z -0.16666666666666666 |
| 126 | ret add r mul mul r z p |
| 127 | end |
| 128 | |
| 129 | note cos(r) for |r| <= pi/4 (Wolfram Taylor coefficients 1/2!..1/14!) |
| 130 | fn ufcos_p r f64 to f64 |
| 131 | z = mul r r |
| 132 | p = add mul z -1.1470745597729724e-11 2.0876756987868098e-9 |
| 133 | p = add mul p z -2.7557319223985893e-7 |
| 134 | p = add mul p z 0.0000248015873015873 |
| 135 | p = add mul p z -0.0013888888888888889 |
| 136 | p = add mul p z 0.041666666666666664 |
| 137 | p = add mul p z -0.5 |
| 138 | ret add 1.0 mul z p |
| 139 | end |
| 140 | |
| 141 | note Cody-Waite range reduction x = k*(pi/2) + r, |r| <= pi/4; mode 0 = sin, mode 1 = cos |
| 142 | fn uftrig x f64 mode ulong to f64 |
| 143 | kf = mul x 0.63661977236758134 |
| 144 | if ge kf 0.0 |
| 145 | kf = add kf 0.5 |
| 146 | else |
| 147 | kf = sub kf 0.5 |
| 148 | end |
| 149 | k = long kf |
| 150 | fk = f64 k |
| 151 | r = sub sub x mul fk 1.5707963267341256 mul fk 6.0771005065061926e-11 |
| 152 | q = and ulong add k long mode 3 |
| 153 | if eq q 0 |
| 154 | ret ufsin_p r |
| 155 | end |
| 156 | if eq q 1 |
| 157 | ret ufcos_p r |
| 158 | end |
| 159 | if eq q 2 |
| 160 | ret neg ufsin_p r |
| 161 | end |
| 162 | ret neg ufcos_p r |
| 163 | end |
| 164 | |
| 165 | fn ufsin x f64 to f64 |
| 166 | ret uftrig x 0 |
| 167 | end |
| 168 | |
| 169 | fn ufcos x f64 to f64 |
| 170 | ret uftrig x 1 |
| 171 | end |
| 172 | |
| 173 | note sigmoid(x) = 1 / (1 + exp(-x)), numerically stable for all x |
| 174 | fn ufsig x f64 to f64 |
| 175 | if ge x 0.0 |
| 176 | ret div 1.0 add 1.0 ufexp neg x |
| 177 | end |
| 178 | e = ufexp x |
| 179 | ret div e add 1.0 e |
| 180 | end |
| 181 | |
| 182 | note tanh(x) numerically stable |
| 183 | fn uftanh x f64 to f64 |
| 184 | if gt x 20.0 |
| 185 | ret 1.0 |
| 186 | end |
| 187 | if lt x -20.0 |
| 188 | ret -1.0 |
| 189 | end |
| 190 | e = ufexp mul 2.0 x |
| 191 | ret div sub e 1.0 add e 1.0 |
| 192 | end |
| 193 | |
| 194 | note GELU(x) = 0.5*x*(1 + tanh(sqrt(2/pi)*(x + 0.044715*x^3))) |
| 195 | fn ufgelu x f64 to f64 |
| 196 | c = mul 0.79788456080286536 add x mul 0.044715 mul mul x x x |
| 197 | ret mul 0.5 mul x add 1.0 uftanh c |
| 198 | end |
| 199 | |
| 200 | note d/dx GELU(x) (Wolfram-verified) |
| 201 | fn ufgelu_d x f64 to f64 |
| 202 | x2 = mul x x |
| 203 | c = mul 0.79788456080286536 add x mul 0.044715 mul x2 x |
| 204 | th = uftanh c |
| 205 | sech2 = sub 1.0 mul th th |
| 206 | dc = mul 0.79788456080286536 add 1.0 mul 0.134145 x2 |
| 207 | ret add mul 0.5 add 1.0 th mul mul 0.5 x mul sech2 dc |
| 208 | end |
| 209 | |
| 210 | note IEEE-754 binary16 (u16 bits) -> f64 (handles normal, subnormal, zero, inf, NaN) |
| 211 | fn uf16_to_f64 h ulong to f64 |
| 212 | sgn = shl and shr h 15 1 63 |
| 213 | exp = and shr h 10 31 |
| 214 | man = and h 1023 |
| 215 | if eq exp 0 |
| 216 | if eq man 0 |
| 217 | ret bitcast f64 sgn |
| 218 | end |
| 219 | v = mul f64 man 5.9604644775390625e-8 |
| 220 | if ne sgn 0 |
| 221 | ret neg v |
| 222 | end |
| 223 | ret v |
| 224 | end |
| 225 | if eq exp 31 |
| 226 | if eq man 0 |
| 227 | ret bitcast f64 or sgn 9218868437227405312 |
| 228 | end |
| 229 | ret bitcast f64 or sgn 9221120237041090560 |
| 230 | end |
| 231 | e64 = add sub exp 15 1023 |
| 232 | ret bitcast f64 or sgn or shl e64 52 shl man 42 |
| 233 | end |
| 234 | |
| 235 | note BF16 (u16 bits) -> f64 (exact widening via f32) |
| 236 | fn ubf16_to_f64 b ulong to f64 |
| 237 | uw = uint shl and b 65535 16 |
| 238 | ret f64 bitcast f32 uw |
| 239 | end |
| 240 | |
| 241 | note f64 -> BF16 (u16 bits, round-to-nearest-even) |
| 242 | fn uf64_to_bf16 v f64 to ulong |
| 243 | bits = ulong bitcast uint f32 v |
| 244 | exp = and shr bits 23 255 |
| 245 | if eq exp 255 |
| 246 | if ne and bits 8388607 0 |
| 247 | ret or shr bits 16 64 |
| 248 | end |
| 249 | ret shr bits 16 |
| 250 | end |
| 251 | lsb = and shr bits 16 1 |
| 252 | rb = add 32767 lsb |
| 253 | ret and shr add bits rb 16 65535 |
| 254 | end |
| 255 | |
| 256 | fn urmic v f64 to long |
| 257 | x = mul v 1000000.0 |
| 258 | q = long x |
| 259 | fr = sub x f64 q |
| 260 | if ge fr 0.5 |
| 261 | q = add q 1 |
| 262 | end |
| 263 | if le fr -0.5 |
| 264 | q = sub q 1 |
| 265 | end |
| 266 | ret q |
| 267 | end |
| 268 | |
| 269 | fn uflush to void |
| 270 | n = get uon 0 |
| 271 | p = ulong 0 |
| 272 | while lt p n bound 4097 |
| 273 | w = write 1 uout p sub n p |
| 274 | if le w 0 |
| 275 | exit 70 |
| 276 | end |
| 277 | p = add p ulong w |
| 278 | end |
| 279 | put uon 0 0 |
| 280 | end |
| 281 | |
| 282 | fn uputc c ulong to void |
| 283 | n = get uon 0 |
| 284 | if ge n UMOUT |
| 285 | uflush |
| 286 | n = ulong 0 |
| 287 | end |
| 288 | put uout n uchar and c 255 |
| 289 | put uon 0 add n 1 |
| 290 | end |
| 291 | |
| 292 | fn uputu v ulong to void |
| 293 | d = new uchar 24 0 |
| 294 | n = ulong 0 |
| 295 | x = v |
| 296 | while true bound 21 |
| 297 | put d n uchar add rem x 10 48 |
| 298 | n = add n 1 |
| 299 | x = div x 10 |
| 300 | if eq x 0 |
| 301 | break |
| 302 | end |
| 303 | end |
| 304 | for i ulong n |
| 305 | uputc ulong get d sub sub n 1 i |
| 306 | end |
| 307 | end |
| 308 | |
| 309 | fn uputl v long to void |
| 310 | if lt v 0 |
| 311 | uputc 45 |
| 312 | if eq v -9223372036854775808 |
| 313 | uputu 9223372036854775808 |
| 314 | else |
| 315 | uputu ulong neg v |
| 316 | end |
| 317 | else |
| 318 | uputu ulong v |
| 319 | end |
| 320 | end |
| 321 | |
| 322 | fn uputx v ulong to void |
| 323 | for i ulong 16 |
| 324 | h = and shr v mul sub 15 i 4 15 |
| 325 | if lt h 10 |
| 326 | uputc add h 48 |
| 327 | else |
| 328 | uputc add h 87 |
| 329 | end |
| 330 | end |
| 331 | end |