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 / om / gpu / dsv41 / decoder / rope.om

192 lines · 4108 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note Precompute interleaved FP32 (cos, sin) RoPE tables for context 20480.
3note Base table: theta=10000, no YaRN (layers 0, 1, 37, 38, 39).
4note Compressed table: theta=160000, YaRN factor=16, beta_fast=32, beta_slow=1,
5note original_max_position_embeddings=65536 (layers 2..36).
6fn drPi to f64
7 ret bitcast f64 ulong 0x400921fb54442d18
8end
9fn drTwoPi to f64
10 ret bitcast f64 ulong 0x401921fb54442d18
11end
12fn drHalfPi to f64
13 ret bitcast f64 ulong 0x3ff921fb54442d18
14end
15fn drLn2 to f64
16 ret bitcast f64 ulong 0x3fe62e42fefa39ef
17end
18
19fn drExp x f64 to f64
20 k = long 0
21 r = x
22 while gt r drLn2 bound 2000
23 r = sub r drLn2
24 k = add k 1
25 end
26 while lt r neg drLn2 bound 2000
27 r = add r drLn2
28 k = sub k 1
29 end
30 term = f64 1.0f64
31 acc = f64 1.0f64
32 for i ulong 20
33 term = div mul term r f64 add i 1
34 acc = add acc term
35 end
36 while gt k 0 bound 2000
37 acc = mul acc 2.0f64
38 k = sub k 1
39 end
40 while lt k 0 bound 2000
41 acc = mul acc 0.5f64
42 k = add k 1
43 end
44 ret acc
45end
46
47fn drLn x f64 to f64
48 v = x
49 k = long 0
50 while ge v 2.0f64 bound 2000
51 v = mul v 0.5f64
52 k = add k 1
53 end
54 while lt v 1.0f64 bound 2000
55 v = mul v 2.0f64
56 k = sub k 1
57 end
58 y = div sub v 1.0f64 add v 1.0f64
59 y2 = mul y y
60 p = y
61 acc = f64 0.0f64
62 for i ulong 24
63 denom = f64 add mul i 2 1
64 acc = add acc div p denom
65 p = mul p y2
66 end
67 ret add mul 2.0f64 acc mul f64 k drLn2
68end
69
70fn drCosR r f64 to f64
71 r2 = mul r r
72 term = f64 1.0f64
73 acc = f64 1.0f64
74 for i ulong 10
75 d1 = f64 mul add i 1 2
76 d0 = sub d1 1.0f64
77 term = div mul neg term r2 mul d0 d1
78 acc = add acc term
79 end
80 ret acc
81end
82
83fn drSinR r f64 to f64
84 r2 = mul r r
85 term = r
86 acc = r
87 for i ulong 10
88 d1 = f64 mul add i 1 2
89 d2 = add d1 1.0f64
90 term = div mul neg term r2 mul d1 d2
91 acc = add acc term
92 end
93 ret acc
94end
95
96fn drCosSin angle f64 pair edit f64 2 to void
97 q = long div angle drTwoPi
98 r = sub angle mul f64 q drTwoPi
99 if gt r drPi
100 r = sub r drTwoPi
101 end
102 if lt r neg drPi
103 r = add r drTwoPi
104 end
105 signC = f64 1.0f64
106 if gt r drHalfPi
107 r = sub drPi r
108 signC = neg 1.0f64
109 end
110 if lt r neg drHalfPi
111 r = sub neg drPi r
112 signC = neg 1.0f64
113 end
114 put pair 0 mul signC drCosR r
115 put pair 1 drSinR r
116end
117
118fn drPutF32 buf edit uchar * off ulong v f32 to void
119 u = bitcast uint v
120 for j ulong 4
121 put buf add off j uchar and shr u uint mul j 8 255
122 end
123end
124
125fn drYarnBounds theta f64 bounds edit f64 2 to void
126 lnTheta = drLn theta
127 lowV = div mul 64.0f64 drLn div 65536.0f64 mul 32.0f64 drTwoPi mul 2.0f64 lnTheta
128 highV = div mul 64.0f64 drLn div 65536.0f64 drTwoPi mul 2.0f64 lnTheta
129 lowI = long lowV
130 if lt lowV f64 lowI
131 lowI = sub lowI 1
132 end
133 if lt lowI 0
134 lowI = 0
135 end
136 highI = long highV
137 if gt highV f64 highI
138 highI = add highI 1
139 end
140 if gt highI 63
141 highI = 63
142 end
143 put bounds 0 f64 lowI
144 put bounds 1 f64 highI
145end
146
147fn drComputeFreqs freqs edit f64 32 theta f64 yarn bool to void
148 bounds = new f64 2 0.0f64
149 lnTheta = drLn theta
150 if yarn
151 drYarnBounds theta bounds
152 end
153 low = get bounds 0
154 span = sub get bounds 1 low
155 if lt span 0.001f64
156 span = 0.001f64
157 end
158 for j ulong 32
159 baseF = drExp div mul neg f64 mul j 2 lnTheta 64.0f64
160 if yarn
161 ramp = div sub f64 j low span
162 if lt ramp 0.0f64
163 ramp = 0.0f64
164 end
165 if gt ramp 1.0f64
166 ramp = 1.0f64
167 end
168 smooth = sub 1.0f64 ramp
169 baseF = add mul div baseF 16.0f64 sub 1.0f64 smooth mul baseF smooth
170 end
171 put freqs j baseF
172 end
173end
174
175fn drFillTable mem long dest ulong theta f64 yarn bool to bool
176 freqs = new f64 32 0.0f64
177 pair = new f64 2 0.0f64
178 row = new uchar 256 0
179 drComputeFreqs freqs theta yarn
180 for pos ulong 20480
181 for j ulong 32
182 ang = mul f64 pos get freqs j
183 drCosSin ang pair
184 drPutF32 row mul j 8 f32 get pair 0
185 drPutF32 row add mul j 8 4 f32 get pair 1
186 end
187 if ne mapwrite mem add dest mul pos 256 row 0 256 256
188 ret false
189 end
190 end
191 ret true
192end