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 / train / blas / om / blas_test.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note train/blas/om/blas_test.om: pure-Omega functional verification and negative controls for omega-blas (blas.om). |
| 3 | note Compose after train/blas/om/blas.om (which begins with om2). |
| 4 | |
| 5 | text bt_pass "PASS omega-blas host verification (formats, gelu, pack/unpack, gemm, gemv, copy32, info)\n" |
| 6 | text bt_fail "FAIL omega-blas host verification\n" |
| 7 | |
| 8 | fn bt_f32_near a uint b uint tol f32 to bool |
| 9 | fa = bitcast f32 a |
| 10 | fb = bitcast f32 b |
| 11 | d = fabs sub fa fb |
| 12 | ret le d tol |
| 13 | end |
| 14 | |
| 15 | fn bt_test_formats to bool |
| 16 | note f16: 1.0 = 0x3c00, -2.0 = 0xc000, 0.5 = 0x3800, 0.0 = 0x0000 |
| 17 | s16 = new ushort 4 0 |
| 18 | put s16 0 ushort 0x3c00 |
| 19 | put s16 1 ushort 0xc000 |
| 20 | put s16 2 ushort 0x3800 |
| 21 | put s16 3 ushort 0x0000 |
| 22 | d32 = new uint 4 0 |
| 23 | widen16 ulong 0 s16 d32 |
| 24 | if ne get d32 0 bitcast uint 1.0f32 |
| 25 | ret false |
| 26 | end |
| 27 | if ne get d32 1 bitcast uint -2.0f32 |
| 28 | ret false |
| 29 | end |
| 30 | if ne get d32 2 bitcast uint 0.5f32 |
| 31 | ret false |
| 32 | end |
| 33 | if ne get d32 3 bitcast uint 0.0f32 |
| 34 | ret false |
| 35 | end |
| 36 | note round-trip narrow16 f16 |
| 37 | r16 = new ushort 4 0 |
| 38 | narrow16 ulong 0 d32 r16 |
| 39 | for i ulong 4 |
| 40 | if ne get r16 i get s16 i |
| 41 | ret false |
| 42 | end |
| 43 | end |
| 44 | note bf16: 1.0 = 0x3f80, -3.5 = 0xc060 |
| 45 | put s16 0 ushort 0x3f80 |
| 46 | put s16 1 ushort 0xc060 |
| 47 | put s16 2 ushort 0x0000 |
| 48 | put s16 3 ushort 0x3f00 |
| 49 | widen16 ulong 1 s16 d32 |
| 50 | if ne get d32 0 bitcast uint 1.0f32 |
| 51 | ret false |
| 52 | end |
| 53 | if ne get d32 1 bitcast uint -3.5f32 |
| 54 | ret false |
| 55 | end |
| 56 | if ne get d32 2 bitcast uint 0.0f32 |
| 57 | ret false |
| 58 | end |
| 59 | if ne get d32 3 bitcast uint 0.5f32 |
| 60 | ret false |
| 61 | end |
| 62 | narrow16 ulong 1 d32 r16 |
| 63 | for i ulong 4 |
| 64 | if ne get r16 i get s16 i |
| 65 | ret false |
| 66 | end |
| 67 | end |
| 68 | note fp8 e4m3: 1.0 = 0x38 (exp=7, bias=7, man=0), -2.0 = 0xc0 (sign=1, exp=8, man=0) |
| 69 | s8 = new uchar 2 0 |
| 70 | put s8 0 uchar 0x38 |
| 71 | put s8 1 uchar 0xc0 |
| 72 | d8 = new uint 2 0 |
| 73 | widen8 ulong 0 s8 d8 |
| 74 | if ne get d8 0 bitcast uint 1.0f32 |
| 75 | ret false |
| 76 | end |
| 77 | if ne get d8 1 bitcast uint -2.0f32 |
| 78 | ret false |
| 79 | end |
| 80 | note fp8 e5m2: 1.0 = 0x3c (exp=15, bias=15, man=0), -2.0 = 0xc0 (sign=1, exp=16, man=0) |
| 81 | put s8 0 uchar 0x3c |
| 82 | put s8 1 uchar 0xc0 |
| 83 | widen8 ulong 1 s8 d8 |
| 84 | if ne get d8 0 bitcast uint 1.0f32 |
| 85 | ret false |
| 86 | end |
| 87 | if ne get d8 1 bitcast uint -2.0f32 |
| 88 | ret false |
| 89 | end |
| 90 | ret true |
| 91 | end |
| 92 | |
| 93 | fn bt_test_gelu to bool |
| 94 | in_v = new uint 3 0 |
| 95 | out_v = new uint 3 0 |
| 96 | put in_v 0 bitcast uint 0.0f32 |
| 97 | put in_v 1 bitcast uint 1.0f32 |
| 98 | put in_v 2 bitcast uint -1.0f32 |
| 99 | gelu32 in_v out_v |
| 100 | note gelu(0) = 0, gelu(1) ~= 0.841192, gelu(-1) ~= -0.158808 |
| 101 | if not bt_f32_near get out_v 0 bitcast uint 0.0f32 0.0001f32 |
| 102 | ret false |
| 103 | end |
| 104 | if not bt_f32_near get out_v 1 bitcast uint 0.841192f32 0.001f32 |
| 105 | ret false |
| 106 | end |
| 107 | if not bt_f32_near get out_v 2 bitcast uint -0.158808f32 0.001f32 |
| 108 | ret false |
| 109 | end |
| 110 | ret true |
| 111 | end |
| 112 | |
| 113 | fn bt_test_gemm to bool |
| 114 | note 2x2 column-major A = [[1, 3], [2, 4]] -> [1, 2, 3, 4] |
| 115 | note 2x2 column-major B = [[5, 7], [6, 8]] -> [5, 6, 7, 8] |
| 116 | note A*B = [[1*5+3*6, 1*7+3*8], [2*5+4*6, 2*7+4*8]] = [[23, 31], [34, 46]] -> col-major [23, 34, 31, 46] |
| 117 | a = new uint 4 0 |
| 118 | put a 0 bitcast uint 1.0f32 |
| 119 | put a 1 bitcast uint 2.0f32 |
| 120 | put a 2 bitcast uint 3.0f32 |
| 121 | put a 3 bitcast uint 4.0f32 |
| 122 | b = new uint 4 0 |
| 123 | put b 0 bitcast uint 5.0f32 |
| 124 | put b 1 bitcast uint 6.0f32 |
| 125 | put b 2 bitcast uint 7.0f32 |
| 126 | put b 3 bitcast uint 8.0f32 |
| 127 | w = new uint 16 0 |
| 128 | pka = new uchar 64 0 |
| 129 | put pka 0 uchar 2 |
| 130 | put pka 8 uchar 2 |
| 131 | put pka 16 uchar 1 |
| 132 | put pka 32 uchar 2 |
| 133 | put pka 40 uchar 2 |
| 134 | pack32 pka a w |
| 135 | note pack B^T (rows=n=2, cols=k=2, si=ld=2, sq=1, dst=4, w=2*2-2=2) |
| 136 | pkb = new uchar 64 0 |
| 137 | put pkb 0 uchar 2 |
| 138 | put pkb 8 uchar 2 |
| 139 | put pkb 16 uchar 2 |
| 140 | put pkb 32 uchar 1 |
| 141 | put pkb 40 uchar 2 |
| 142 | put pkb 48 uchar 4 |
| 143 | pack32 pkb b w |
| 144 | note gemm_k: m=2, n=2, k=2, ldc=2, offc=0, alpha=1.0, beta=0.0, act=0, wsa=0, wsb=4, mp=1, np=1 |
| 145 | c = new uint 4 0 |
| 146 | gp_buf = new uchar 128 0 |
| 147 | put gp_buf 0 uchar 2 |
| 148 | put gp_buf 8 uchar 2 |
| 149 | put gp_buf 16 uchar 2 |
| 150 | put gp_buf 24 uchar 2 |
| 151 | note alpha = 1.0f32 = 0x3f800000 little endian at offset 40 |
| 152 | put gp_buf 40 uchar 0x00 |
| 153 | put gp_buf 41 uchar 0x00 |
| 154 | put gp_buf 42 uchar 0x80 |
| 155 | put gp_buf 43 uchar 0x3f |
| 156 | note wsb = 4 at offset 64, mp = 1 at 80, np = 1 at 88 |
| 157 | put gp_buf 64 uchar 4 |
| 158 | put gp_buf 80 uchar 1 |
| 159 | put gp_buf 88 uchar 1 |
| 160 | gemm_k gp_buf w c |
| 161 | if not bt_f32_near get c 0 bitcast uint 23.0f32 0.001f32 |
| 162 | ret false |
| 163 | end |
| 164 | if not bt_f32_near get c 1 bitcast uint 34.0f32 0.001f32 |
| 165 | ret false |
| 166 | end |
| 167 | if not bt_f32_near get c 2 bitcast uint 31.0f32 0.001f32 |
| 168 | ret false |
| 169 | end |
| 170 | if not bt_f32_near get c 3 bitcast uint 46.0f32 0.001f32 |
| 171 | ret false |
| 172 | end |
| 173 | note Re-run with beta = 2.0f32 (0x40000000) and alpha = 1.0f32 -> 3 * C = [69, 102, 93, 138] |
| 174 | put gp_buf 44 uchar 0x00 |
| 175 | put gp_buf 45 uchar 0x00 |
| 176 | put gp_buf 46 uchar 0x00 |
| 177 | put gp_buf 47 uchar 0x40 |
| 178 | gemm_k gp_buf w c |
| 179 | if not bt_f32_near get c 0 bitcast uint 69.0f32 0.001f32 |
| 180 | ret false |
| 181 | end |
| 182 | if not bt_f32_near get c 3 bitcast uint 138.0f32 0.001f32 |
| 183 | ret false |
| 184 | end |
| 185 | ret true |
| 186 | end |
| 187 | |
| 188 | fn bt_test_gemv_copy to bool |
| 189 | note 2x2 col-major A = [[1, 3], [2, 4]], x = [10, 20], y = [1, 2], alpha=1, beta=1 |
| 190 | note y_new = [1 + 1*10 + 3*20, 2 + 2*10 + 4*20] = [71, 102] |
| 191 | a = new uint 4 0 |
| 192 | put a 0 bitcast uint 1.0f32 |
| 193 | put a 1 bitcast uint 2.0f32 |
| 194 | put a 2 bitcast uint 3.0f32 |
| 195 | put a 3 bitcast uint 4.0f32 |
| 196 | w = new uint 8 0 |
| 197 | put w 0 bitcast uint 10.0f32 |
| 198 | put w 1 bitcast uint 20.0f32 |
| 199 | put w 2 bitcast uint 1.0f32 |
| 200 | put w 3 bitcast uint 2.0f32 |
| 201 | gv_buf = new uchar 80 0 |
| 202 | put gv_buf 0 uchar 2 |
| 203 | put gv_buf 8 uchar 2 |
| 204 | put gv_buf 16 uchar 2 |
| 205 | note alpha = 1.0f32 at 40, beta = 1.0f32 at 44, wx=0 at 48, wy=2 at 56 |
| 206 | put gv_buf 42 uchar 0x80 |
| 207 | put gv_buf 43 uchar 0x3f |
| 208 | put gv_buf 46 uchar 0x80 |
| 209 | put gv_buf 47 uchar 0x3f |
| 210 | put gv_buf 56 uchar 2 |
| 211 | gemv_k gv_buf a w |
| 212 | if not bt_f32_near get w 2 bitcast uint 71.0f32 0.001f32 |
| 213 | ret false |
| 214 | end |
| 215 | if not bt_f32_near get w 3 bitcast uint 102.0f32 0.001f32 |
| 216 | ret false |
| 217 | end |
| 218 | note copy32 reverse stride: d[0..2] = a[2, 0] |
| 219 | d = new uint 2 0 |
| 220 | cp_buf = new uchar 48 0 |
| 221 | put cp_buf 0 uchar 2 |
| 222 | put cp_buf 8 uchar 2 |
| 223 | note is = -2 (0xfffffffffffffffe) at offset 16 |
| 224 | put cp_buf 16 uchar 0xfe |
| 225 | for k ulong 7 |
| 226 | put cp_buf add k ulong 17 uchar 0xff |
| 227 | end |
| 228 | note od = 0 at 24, id = 1 at 32 |
| 229 | put cp_buf 32 uchar 1 |
| 230 | copy32 cp_buf a d |
| 231 | if ne get d 0 get a 2 |
| 232 | ret false |
| 233 | end |
| 234 | if ne get d 1 get a 0 |
| 235 | ret false |
| 236 | end |
| 237 | ret true |
| 238 | end |
| 239 | |
| 240 | fn bt_test_info to bool |
| 241 | note Valid gemm_info: ta='N'(78), tb='T'(84), m=4, n=4, k=4, lda=4, ldb=4, ldc=4 -> 0 |
| 242 | ck_buf = new uchar 80 0 |
| 243 | put ck_buf 0 uchar 78 |
| 244 | put ck_buf 8 uchar 84 |
| 245 | put ck_buf 16 uchar 4 |
| 246 | put ck_buf 24 uchar 4 |
| 247 | put ck_buf 32 uchar 4 |
| 248 | put ck_buf 40 uchar 4 |
| 249 | put ck_buf 48 uchar 4 |
| 250 | put ck_buf 56 uchar 4 |
| 251 | put ck_buf 64 uchar 1 |
| 252 | put ck_buf 72 uchar 1 |
| 253 | if ne gemm_info ck_buf ulong 0 |
| 254 | ret false |
| 255 | end |
| 256 | note Negative control: bad ta='X'(88) -> info 1 |
| 257 | put ck_buf 0 uchar 88 |
| 258 | if ne gemm_info ck_buf ulong 1 |
| 259 | ret false |
| 260 | end |
| 261 | note Restore ta='N', bad lda=2 < m=4 -> info 8 |
| 262 | put ck_buf 0 uchar 78 |
| 263 | put ck_buf 40 uchar 2 |
| 264 | if ne gemm_info ck_buf ulong 8 |
| 265 | ret false |
| 266 | end |
| 267 | note gemv_info: restore lda=4 -> 0; incx=0 -> info 8 |
| 268 | put ck_buf 40 uchar 4 |
| 269 | if ne gemv_info ck_buf ulong 0 |
| 270 | ret false |
| 271 | end |
| 272 | put ck_buf 64 uchar 0 |
| 273 | if ne gemv_info ck_buf ulong 8 |
| 274 | ret false |
| 275 | end |
| 276 | ret true |
| 277 | end |
| 278 | |
| 279 | fn main to int |
| 280 | ok = true |
| 281 | if not bt_test_formats |
| 282 | ok = false |
| 283 | end |
| 284 | if not bt_test_gelu |
| 285 | ok = false |
| 286 | end |
| 287 | if not bt_test_gemm |
| 288 | ok = false |
| 289 | end |
| 290 | if not bt_test_gemv_copy |
| 291 | ok = false |
| 292 | end |
| 293 | if not bt_test_info |
| 294 | ok = false |
| 295 | end |
| 296 | if not ok |
| 297 | k = write int 1 bt_fail ulong 0 len bt_fail |
| 298 | if lt k long 0 |
| 299 | ret int 1 |
| 300 | end |
| 301 | ret int 1 |
| 302 | end |
| 303 | k = write int 1 bt_pass ulong 0 len bt_pass |
| 304 | if lt k long 0 |
| 305 | ret int 1 |
| 306 | end |
| 307 | ret int 0 |
| 308 | end |