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 / runtime / rx_costmodel_core.om

240 lines · 5692 bytes Raw Blame History
1om2
2note rx_costmodel_core.om -- Pure OM-2 f64 linear algebra and Bayesian
3note sufficient-statistics updates for src/runtime/rx_costmodel.c.
4note f64 arrays are passed as bit-preserving ulong arrays (bitcast f64/ulong).
5
6const PRIOR_A f64 1.0
7const PRIOR_B f64 0.01
8const NOISE_FLOOR f64 0.0009
9
10fn unpack_sym tri view ulong 36 A edit f64 64 to void
11 k = ulong 0
12 for i ulong 8
13 j = i
14 while lt j 8 bound 8
15 v = bitcast f64 get tri k
16 put A add mul i 8 j v
17 put A add mul j 8 i v
18 k = add k 1
19 j = add j 1
20 end
21 end
22end
23
24fn chol_diag A view f64 64 L view f64 64 j ulong to f64
25 s = get A add mul j 8 j
26 for k ulong j bound 8
27 lk = get L add mul j 8 k
28 s = sub s mul lk lk
29 end
30 ret s
31end
32
33fn chol_off A view f64 64 L view f64 64 i ulong j ulong to f64
34 t = get A add mul i 8 j
35 for k ulong j bound 8
36 lik = get L add mul i 8 k
37 ljk = get L add mul j 8 k
38 t = sub t mul lik ljk
39 end
40 ret t
41end
42
43fn cholesky_8 tri view ulong 36 L edit f64 64 to long
44 A = new f64 64 0.0f64
45 unpack_sym tri A
46 for z ulong 64
47 put L z 0.0f64
48 end
49 for j ulong 8
50 s = chol_diag A L j
51 if not gt s 0.0f64
52 ret -1
53 end
54 ljj = fsqrt s
55 put L add mul j 8 j ljj
56 i = add j 1
57 while lt i 8 bound 8
58 t = chol_off A L i j
59 put L add mul i 8 j div t ljj
60 i = add i 1
61 end
62 end
63 ret 0
64end
65
66fn fwd_8 L view f64 64 b view f64 8 v edit f64 8 to void
67 for i ulong 8
68 s = get b i
69 for k ulong i bound 8
70 s = sub s mul get L add mul i 8 k get v k
71 end
72 put v i div s get L add mul i 8 i
73 end
74end
75
76fn bwd_8 L view f64 64 v view f64 8 x edit f64 8 to void
77 r = ulong 0
78 while lt r 8 bound 8
79 i = sub 7 r
80 s = get v i
81 k = add i 1
82 while lt k 8 bound 8
83 s = sub s mul get L add mul k 8 i get x k
84 k = add k 1
85 end
86 put x i div s get L add mul i 8 i
87 r = add r 1
88 end
89end
90
91fn dot_8 a view f64 8 b view f64 8 to f64
92 s = f64 0.0f64
93 for i ulong 8
94 s = add s mul get a i get b i
95 end
96 ret s
97end
98
99fn norm2_8 a view f64 8 to f64
100 s = f64 0.0f64
101 for i ulong 8
102 x = get a i
103 s = add s mul x x
104 end
105 ret s
106end
107
108fn post_sd n ulong yty_bits ulong quad f64 lev f64 dst edit ulong 4 to void
109 yty = bitcast f64 yty_bits
110 a = add PRIOR_A mul 0.5f64 f64 n
111 b = add PRIOR_B mul 0.5f64 sub yty quad
112 if lt b PRIOR_B
113 b = PRIOR_B
114 end
115 sigma2 = div b a
116 if lt sigma2 NOISE_FLOOR
117 sigma2 = NOISE_FLOOR
118 end
119 sd = fsqrt mul sigma2 add 1.0f64 lev
120 dof = mul 2.0f64 a
121 put dst 1 bitcast ulong sd
122 put dst 2 bitcast ulong dof
123end
124
125note Predictive Student-t for one (cell, arm).
126note meta[0] = n (ulong), meta[1] = bitcast(yty).
127note dst[0..2] = bitcast(mean, sd, dof). Returns 1 on success, 0 if no data / non-PD.
128export fn rx_cm_fit_predict tri view ulong 36 xty_u view ulong 8 phi_u view ulong 8 meta view ulong 2 dst edit ulong 4 to ulong
129 n = get meta 0
130 if eq n 0
131 ret 0
132 end
133 L = new f64 64 0.0f64
134 if ne cholesky_8 tri L 0
135 ret 0
136 end
137 xty = new f64 8 0.0f64
138 phi = new f64 8 0.0f64
139 for i ulong 8
140 put xty i bitcast f64 get xty_u i
141 put phi i bitcast f64 get phi_u i
142 end
143 v = new f64 8 0.0f64
144 mu = new f64 8 0.0f64
145 w = new f64 8 0.0f64
146 fwd_8 L xty v
147 bwd_8 L v mu
148 fwd_8 L phi w
149 put dst 0 bitcast ulong dot_8 mu phi
150 post_sd n get meta 1 norm2_8 v norm2_8 w dst
151 ret 1
152end
153
154note Rank-1 sufficient-statistics update: tri += phi * phi^T, xty += phi * y.
155export fn rx_cm_rank1_update tri edit ulong 36 xty edit ulong 8 phi view ulong 8 y_bits ulong to void
156 y = bitcast f64 y_bits
157 k = ulong 0
158 for i ulong 8
159 pi = bitcast f64 get phi i
160 j = i
161 while lt j 8 bound 8
162 pj = bitcast f64 get phi j
163 cur = bitcast f64 get tri k
164 put tri k bitcast ulong add cur mul pi pj
165 k = add k 1
166 j = add j 1
167 end
168 cx = bitcast f64 get xty i
169 put xty i bitcast ulong add cx mul pi y
170 end
171end
172
173note OGCM v1 stores the 40-byte header followed by cell statistics, power,
174note verification and synthesis costs, code sizes and the calibrated scale.
175note Runtime work/exploration counters are reset when a model is restored.
176const ogcm_magic ulong 1296254799
177const ogcm_body ulong 28040
178
179fn ogcm_read data view uchar * offset ulong width ulong to ulong
180 value = ulong 0
181 for i ulong width
182 value = or value shl ulong get data add offset i mul i 8
183 end
184 ret value
185end
186
187fn ogcm_put32 data edit uchar * offset ulong value ulong to void
188 for i ulong 4
189 put data add offset i uchar and shr value mul i 8 255
190 end
191end
192
193export fn rx_cm_pack_v1 model view uchar 28072 wire out uchar 28080 to void
194 ogcm_put32 wire 0 ogcm_magic
195 ogcm_put32 wire 4 1
196 ogcm_put32 wire 8 ogcm_read model 0 4
197 ogcm_put32 wire 12 8
198 ogcm_put32 wire 16 9
199 ogcm_put32 wire 20 8
200 ogcm_put32 wire 24 3
201 ogcm_put32 wire 28 0
202 for i ulong 8
203 put wire add 32 i get model add 8 i
204 end
205 for i ulong ogcm_body
206 put wire add 40 i get model add 16 i
207 end
208end
209
210export fn rx_cm_unpack_v1 wire view uchar 28080 model edit uchar 28072 to ulong
211 if or ne ogcm_read wire 0 4 ogcm_magic ne ogcm_read wire 4 4 1
212 ret 0
213 end
214 arms = ogcm_read wire 8 4
215 if or eq arms 0 gt arms 8
216 ret 0
217 end
218 if or ne ogcm_read wire 12 4 8 ne ogcm_read wire 16 4 9
219 ret 0
220 end
221 if or ne ogcm_read wire 20 4 8 ne ogcm_read wire 24 4 3
222 ret 0
223 end
224 scale = bitcast f64 ogcm_read wire 28072 8
225 if not and ge scale f64 1.0 lt scale f64 1000000.0
226 ret 0
227 end
228 ogcm_put32 model 0 arms
229 ogcm_put32 model 4 0
230 for i ulong 8
231 put model add 8 i get wire add 32 i
232 end
233 for i ulong ogcm_body
234 put model add 16 i get wire add 40 i
235 end
236 for i ulong 16
237 put model add 28056 i 0
238 end
239 ret 1
240end