interplane.Get Omega
Read in your language

ggml-omega

aien-dev/ggml-omega

Brought by drakestapleton

branch main Branches Tags History

ggml-omega / kernels / math.om

157 lines · 3559 bytes Raw Blame History
1note math.om: private f64/f16 helpers prepended to the ggml-omega kernel units that need them
2note (exp, log, pow, sin, cos, f16 <-> f32). Constants are binary64 bit patterns checked in Wolfram
3note Language (ImportByteArray of the bits, difference to Log[2], 1/Log[2], Pi/2, 2/Pi, Sqrt[2]):
4note ln2 hi+lo -1.2e-26, pio2 1+2+3 -8.5e-32; the hi parts end in >= 21 zero bits, so k*hi is exact for |k| < 2^21.
5const cln2h ulong 4604418534311723008
6const cln2l ulong 4461442080421002358
7const civln2 ulong 4609176140021203710
8const cpio21 ulong 4609753056924401664
9const cpio22 ulong 4454258360616747008
10const cpio23 ulong 4297306550709518336
11const civpio2 ulong 4603909380684499075
12const csqrt2 ulong 4609047870845172685
13note rnd: x rounded to the nearest whole number (halves away from zero), as long.
14fn rnd x f64 to long
15if lt x 0.0
16ret long sub x 0.5
17end
18ret long add x 0.5
19end
20note ex64: e^x. Inputs below -200 give 0 (every f32 result there is 0), above 709 are clamped (f32 inf).
21fn ex64 x f64 to f64
22if ne x x
23ret x
24end
25if lt x -200.0
26ret 0.0
27end
28y = x
29if gt y 709.0
30y = 709.0
31end
32k = rnd mul y bitcast f64 civln2
33kf = f64 k
34r = sub sub y mul kf bitcast f64 cln2h mul kf bitcast f64 cln2l
35p = f64 1.0
36for i ulong 13
37n = f64 sub 13 i
38p = add 1.0 div mul r p n
39end
40sc = bitcast f64 shl ulong add k 1023 52
41ret mul p sc
42end
43note ln64: natural log of a positive normal x.
44fn ln64 x f64 to f64
45b = bitcast ulong x
46e = sub long shr b 52 1023
47m = bitcast f64 or and b 4503599627370495 shl ulong 1023 52
48if gt m bitcast f64 csqrt2
49m = div m 2.0
50e = add e 1
51end
52s = div sub m 1.0 add m 1.0
53s2 = mul s s
54t = f64 0.0
55for i ulong 12
56n = f64 add mul 2 sub 11 i 1
57t = add div 1.0 n mul s2 t
58end
59ef = f64 e
60ret add mul ef bitcast f64 cln2h add mul ef bitcast f64 cln2l mul mul 2.0 s t
61end
62note sinr / cosr: Taylor series on |r| <= pi/4.
63fn sinr r f64 to f64
64r2 = mul r r
65t = f64 1.0
66for i ulong 9
67n = f64 sub 9 i
68t = sub 1.0 div mul r2 t mul mul 2.0 n add mul 2.0 n 1.0
69end
70ret mul r t
71end
72fn cosr r f64 to f64
73r2 = mul r r
74t = f64 1.0
75for i ulong 10
76n = f64 sub 10 i
77t = sub 1.0 div mul r2 t mul sub mul 2.0 n 1.0 mul 2.0 n
78end
79ret t
80end
81note sc64: sin (c 0) or cos (c 1) of x, reduced by k * pi/2 in three parts.
82fn sc64 x f64 c ulong to f64
83if ne x x
84ret x
85end
86k = rnd mul x bitcast f64 civpio2
87kf = f64 k
88r = sub sub sub x mul kf bitcast f64 cpio21 mul kf bitcast f64 cpio22 mul kf bitcast f64 cpio23
89q = add ulong and k 3 c
90q = and q 3
91if eq q 0
92ret sinr r
93end
94if eq q 1
95ret cosr r
96end
97if eq q 2
98ret neg sinr r
99end
100ret neg cosr r
101end
102note h2f: binary16 bits to f32.
103fn h2f h ushort to f32
104v = uint h
105s = shl and shr v 15 1 31
106e = and shr v 10 31
107m = and v 1023
108if eq e 0
109z = mul f32 m 0.000000059604644775390625f32
110if eq s 0
111ret z
112end
113ret neg z
114end
115if eq e 31
116ret bitcast f32 or or s 2139095040 shl m 13
117end
118ret bitcast f32 or or s shl add e 112 23 shl m 13
119end
120note rne: v >> sh (sh 1 .. 31) rounded to nearest, ties to even.
121fn rne v uint sh uint to uint
122r = shr v sh
123hf = shl uint 1 sub sh 1
124rm = and v sub shl hf 1 1
125up = gt rm hf
126if and eq rm hf eq and r 1 1
127up = true
128end
129if up
130ret add r 1
131end
132ret r
133end
134note f2h: f32 to binary16 bits, round to nearest even (as FCVT).
135fn f2h x f32 to ushort
136b = bitcast uint x
137s = and shr b 16 32768
138e = long and shr b 23 255
139m = and b 8388607
140if eq e 255
141if eq m 0
142ret ushort or s 31744
143end
144ret ushort or or s 32256 shr m 13
145end
146ee = sub e 112
147if ge ee 31
148ret ushort or s 31744
149end
150if le ee 0
151if lt ee -10
152ret ushort s
153end
154ret ushort or s rne or m 8388608 uint sub 14 ee
155end
156ret ushort or s rne or shl uint ee 23 m 13
157end