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
omega-mv / src / reservoir / reservoir_readout.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note Pure seeded OM-2 binary64 ridge readout (Phi^T Phi + lambda I, L D L^T |
| 3 | note factorization, forward/back solve, apply) and NRMSE backing |
| 4 | note src/reservoir/omega_reservoir_readout.c. |
| 5 | om2 |
| 6 | |
| 7 | const res_dim ulong 257 |
| 8 | const res_max_out ulong 4 |
| 9 | const res_d_off ulong 66049 |
| 10 | const res_r_off ulong 66306 |
| 11 | |
| 12 | fn res_fin64 bits ulong to bool |
| 13 | ret lt and bits 0x7ff0000000000000 0x7ff0000000000000 |
| 14 | end |
| 15 | |
| 16 | fn res_design_val x view ulong * t ulong nf ulong col ulong to f64 |
| 17 | if eq col 0 |
| 18 | ret bitcast f64 ulong 0x3ff0000000000000 |
| 19 | end |
| 20 | ret bitcast f64 get x add mul t nf sub col 1 |
| 21 | end |
| 22 | |
| 23 | fn res_dot_xx x view ulong * meta view ulong 4 i ulong j ulong to f64 |
| 24 | s = bitcast f64 ulong 0 |
| 25 | for t ulong get meta 0 |
| 26 | xi = res_design_val x t get meta 1 i |
| 27 | xj = res_design_val x t get meta 1 j |
| 28 | s = add s mul xi xj |
| 29 | end |
| 30 | ret s |
| 31 | end |
| 32 | |
| 33 | fn res_dot_xy x view ulong * y view ulong * meta view ulong 4 io ulong to f64 |
| 34 | s = bitcast f64 ulong 0 |
| 35 | for t ulong get meta 0 |
| 36 | xi = res_design_val x t get meta 1 div io 4 |
| 37 | idx = mul t get meta 2 |
| 38 | idx = add idx rem io 4 |
| 39 | yo = bitcast f64 get y idx |
| 40 | s = add s mul xi yo |
| 41 | end |
| 42 | ret s |
| 43 | end |
| 44 | |
| 45 | fn res_check_fit_args x view ulong * y view ulong * meta view ulong 4 to long |
| 46 | ns = get meta 0 |
| 47 | nf = get meta 1 |
| 48 | no = get meta 2 |
| 49 | lam_b = get meta 3 |
| 50 | if or lt nf 1 gt nf 256 |
| 51 | ret -3 |
| 52 | end |
| 53 | if or lt no 1 gt no 4 |
| 54 | ret -3 |
| 55 | end |
| 56 | if or lt ns 1 not res_fin64 lam_b |
| 57 | ret -3 |
| 58 | end |
| 59 | if lt bitcast f64 lam_b bitcast f64 ulong 0 |
| 60 | ret -3 |
| 61 | end |
| 62 | for i ulong mul ns nf |
| 63 | if not res_fin64 get x i |
| 64 | ret -4 |
| 65 | end |
| 66 | end |
| 67 | for i ulong mul ns no |
| 68 | if not res_fin64 get y i |
| 69 | ret -4 |
| 70 | end |
| 71 | end |
| 72 | ret 0 |
| 73 | end |
| 74 | |
| 75 | fn res_fill_g x view ulong * ws edit ulong 67334 meta view ulong 4 to void |
| 76 | for i ulong add get meta 1 1 |
| 77 | for j ulong add i 1 |
| 78 | s = res_dot_xx x meta i j |
| 79 | if and ne i 0 eq j i |
| 80 | s = add s bitcast f64 get meta 3 |
| 81 | end |
| 82 | put ws add mul i res_dim j bitcast ulong s |
| 83 | end |
| 84 | end |
| 85 | end |
| 86 | |
| 87 | fn res_fill_r x view ulong * y view ulong * ws edit ulong 67334 meta view ulong 4 to void |
| 88 | for io ulong mul add get meta 1 1 4 |
| 89 | if lt rem io 4 get meta 2 |
| 90 | s = res_dot_xy x y meta io |
| 91 | put ws add res_r_off io bitcast ulong s |
| 92 | end |
| 93 | end |
| 94 | end |
| 95 | |
| 96 | export fn res_om_fit_normal x view ulong * y view ulong * ws edit ulong 67334 meta view ulong 4 to long |
| 97 | rc = res_check_fit_args x y meta |
| 98 | if ne rc 0 |
| 99 | ret rc |
| 100 | end |
| 101 | res_fill_g x ws meta |
| 102 | res_fill_r x y ws meta |
| 103 | ret 0 |
| 104 | end |
| 105 | |
| 106 | fn res_ldlt_pivot ws edit ulong 67334 j ulong to long |
| 107 | gjj = bitcast f64 get ws add mul j res_dim j |
| 108 | dj = gjj |
| 109 | for k ulong j |
| 110 | ljk = bitcast f64 get ws add mul j res_dim k |
| 111 | dk = bitcast f64 get ws add res_d_off k |
| 112 | dj = sub dj mul mul ljk ljk dk |
| 113 | end |
| 114 | dj_b = bitcast ulong dj |
| 115 | tol = mul bitcast f64 ulong 0x3d3c25c268497682 gjj |
| 116 | if or not res_fin64 dj_b le dj tol |
| 117 | ret -10 |
| 118 | end |
| 119 | put ws add res_d_off j dj_b |
| 120 | ret 0 |
| 121 | end |
| 122 | |
| 123 | fn res_ldlt_entry ws edit ulong 67334 row ulong j ulong to void |
| 124 | dj = bitcast f64 get ws add res_d_off j |
| 125 | s = bitcast f64 get ws add mul row res_dim j |
| 126 | for k ulong j |
| 127 | lik = bitcast f64 get ws add mul row res_dim k |
| 128 | ljk = bitcast f64 get ws add mul j res_dim k |
| 129 | dk = bitcast f64 get ws add res_d_off k |
| 130 | s = sub s mul mul lik ljk dk |
| 131 | end |
| 132 | put ws add mul row res_dim j bitcast ulong div s dj |
| 133 | end |
| 134 | |
| 135 | fn res_ldlt_col ws edit ulong 67334 p ulong j ulong to void |
| 136 | for i ulong sub p add j 1 |
| 137 | res_ldlt_entry ws add add j 1 i j |
| 138 | end |
| 139 | end |
| 140 | |
| 141 | fn res_solve_fwd_row ws edit ulong 67334 i ulong o ulong to void |
| 142 | s = bitcast f64 get ws add res_r_off add mul i res_max_out o |
| 143 | for k ulong i |
| 144 | lik = bitcast f64 get ws add mul i res_dim k |
| 145 | rk = bitcast f64 get ws add res_r_off add mul k res_max_out o |
| 146 | s = sub s mul lik rk |
| 147 | end |
| 148 | put ws add res_r_off add mul i res_max_out o bitcast ulong s |
| 149 | end |
| 150 | |
| 151 | fn res_solve_fwd ws edit ulong 67334 p ulong o ulong to void |
| 152 | for i ulong p |
| 153 | res_solve_fwd_row ws i o |
| 154 | end |
| 155 | for i ulong p |
| 156 | ri = bitcast f64 get ws add res_r_off add mul i res_max_out o |
| 157 | di = bitcast f64 get ws add res_d_off i |
| 158 | put ws add res_r_off add mul i res_max_out o bitcast ulong div ri di |
| 159 | end |
| 160 | end |
| 161 | |
| 162 | fn res_solve_bwd_row ws edit ulong 67334 p ulong ii ulong o ulong to long |
| 163 | s = bitcast f64 get ws add res_r_off add mul ii res_max_out o |
| 164 | k = add ii 1 |
| 165 | while lt k p |
| 166 | lki = bitcast f64 get ws add mul k res_dim ii |
| 167 | rk = bitcast f64 get ws add res_r_off add mul k res_max_out o |
| 168 | s = sub s mul lki rk |
| 169 | k = add k 1 |
| 170 | end |
| 171 | sb = bitcast ulong s |
| 172 | if not res_fin64 sb |
| 173 | ret -4 |
| 174 | end |
| 175 | put ws add res_r_off add mul ii res_max_out o sb |
| 176 | ret 0 |
| 177 | end |
| 178 | |
| 179 | fn res_solve_output ws edit ulong 67334 p ulong o ulong to long |
| 180 | res_solve_fwd ws p o |
| 181 | for step ulong p |
| 182 | rc = res_solve_bwd_row ws p sub sub p 1 step o |
| 183 | if ne rc 0 |
| 184 | ret rc |
| 185 | end |
| 186 | end |
| 187 | ret 0 |
| 188 | end |
| 189 | |
| 190 | export fn res_om_fit_solve ws edit ulong 67334 nf ulong no ulong to long |
| 191 | p = add nf 1 |
| 192 | for j ulong p |
| 193 | rc = res_ldlt_pivot ws j |
| 194 | if ne rc 0 |
| 195 | ret rc |
| 196 | end |
| 197 | res_ldlt_col ws p j |
| 198 | end |
| 199 | for o ulong no |
| 200 | rc = res_solve_output ws p o |
| 201 | if ne rc 0 |
| 202 | ret rc |
| 203 | end |
| 204 | end |
| 205 | ret 0 |
| 206 | end |
| 207 | |
| 208 | fn res_apply_one w view ulong 1028 x view ulong * nf ulong o ulong to ulong |
| 209 | s = bitcast f64 get w o |
| 210 | for f ulong nf |
| 211 | xf = bitcast f64 get x f |
| 212 | wf = bitcast f64 get w add mul add 1 f res_max_out o |
| 213 | s = add s mul xf wf |
| 214 | end |
| 215 | ret bitcast ulong s |
| 216 | end |
| 217 | |
| 218 | export fn res_om_readout_apply w view ulong 1028 x view ulong * y_out edit ulong * meta view ulong 2 to long |
| 219 | if or lt get meta 0 1 gt get meta 0 256 |
| 220 | ret -3 |
| 221 | end |
| 222 | if or lt get meta 1 1 gt get meta 1 4 |
| 223 | ret -3 |
| 224 | end |
| 225 | for f ulong get meta 0 |
| 226 | if not res_fin64 get x f |
| 227 | ret -4 |
| 228 | end |
| 229 | end |
| 230 | for o ulong get meta 1 |
| 231 | sb = res_apply_one w x get meta 0 o |
| 232 | if not res_fin64 sb |
| 233 | ret -4 |
| 234 | end |
| 235 | put y_out o sb |
| 236 | end |
| 237 | ret 0 |
| 238 | end |
| 239 | |
| 240 | fn res_nrmse_mean ref view ulong * pred view ulong * out_mean edit ulong 1 to long |
| 241 | sum = bitcast f64 ulong 0 |
| 242 | for i ulong len ref |
| 243 | rb = get ref i |
| 244 | pb = get pred i |
| 245 | if or not res_fin64 rb not res_fin64 pb |
| 246 | ret -4 |
| 247 | end |
| 248 | sum = add sum bitcast f64 rb |
| 249 | end |
| 250 | put out_mean 0 bitcast ulong div sum f64 len ref |
| 251 | ret 0 |
| 252 | end |
| 253 | |
| 254 | export fn res_om_nrmse ref view ulong * pred view ulong * out_bits edit ulong 1 to long |
| 255 | if or lt len ref 2 ne len pred len ref |
| 256 | ret -3 |
| 257 | end |
| 258 | rc = res_nrmse_mean ref pred out_bits |
| 259 | if ne rc 0 |
| 260 | ret rc |
| 261 | end |
| 262 | mean = bitcast f64 get out_bits 0 |
| 263 | num = bitcast f64 ulong 0 |
| 264 | den = bitcast f64 ulong 0 |
| 265 | for i ulong len ref |
| 266 | e = sub bitcast f64 get ref i bitcast f64 get pred i |
| 267 | c = sub bitcast f64 get ref i mean |
| 268 | num = add num mul e e |
| 269 | den = add den mul c c |
| 270 | end |
| 271 | if or not res_fin64 bitcast ulong num not res_fin64 bitcast ulong den |
| 272 | ret -10 |
| 273 | end |
| 274 | if le den bitcast f64 ulong 0 |
| 275 | ret -10 |
| 276 | end |
| 277 | put out_bits 0 bitcast ulong fsqrt div num den |
| 278 | ret 0 |
| 279 | end |