Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/seed/tilelab/tilelab.om # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / seed / tilelab / tilelab.om` 435 lines ยท 8569 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note seed/tilelab/tilelab.om: OmegaTileLab. Native tile planner and streaming-softmax algebra (host program, seeded Omega). 3 note Run: tilelab 1 M N K [REGS] plan a BF16 GEMM tile schedule for CUDApp omega_gpu_gemm (rt ct wm wn band) 4 note tilelab 2 [MUT] streaming (m,s,p) merge over 32 scores vs the direct softmax-weighted sum, f64 5 note MUT 0 correct, 1 tile-local normalisation, 2 merge without rescale, 3 wrong tree order 6 note Exit: 0 ok, 1 numeric tolerance failed, 2 refused (shape, limit or no configuration fits), 3 usage. 7 note The GEMM model is calibrated on GB10 chip timings of the CUDApp kernel (no shared memory: each warp loads its own 8 note 16rt x 8ct operands, band orders CTAs in column bands so packed B stays in L2). BM = 16 rt wm, BN = 8 ct wn, 9 note Kp = K rounded up to 32, mt = ceil(M/BM), nt = ceil(N/BN). l2_bytes = 2 Kp (M nt + N mt) (operand bytes per CTA tile); 10 note dram_bytes = 2 M Kp ceil(nt/band) + 2 Kp N when band >= 2 and the band of B, 2 Kp min(N, band BN) bytes, fits a 11 note 32 MiB L2 budget; otherwise (band < 2 or the band overflows) 2 M Kp nt + 2 Kp N mt. 12 note cost_ns = l2_bytes / 1827.5440 + 2 M N Kp / 37166.0364 + dram_bytes / 1890.5139, constants and the 32 MiB budget 13 note fitted to 96 GB10 sweep medians (seed/tilelab/WOLFRAM.md). An empirical RANKING, not a time prediction and not 14 note physical bandwidths. Held out per shape, its top 2 plus the default reach the best of 32 configurations on two of 15 note three shapes and come within 1.9% on the third, so the planner names the top 2 and the CUDApp default 16 note (rt2 ct4 wm2 wn4 band8) as `measure` lines and the chip decides between them. 17 om2 18 text msg "tilelab: usage: tilelab 1 M N K [REGS] | tilelab 2 [MUT]\nrefused: dimension zero or above 16384\nrefused: no configuration fits the register budget\ncfg rt=\n ct=\n wm=\n wn=\n band=\n bm=\n bn=\n regs=\n ctas=\n l2_bytes=\n cost_ns=\npick \nrefused: unknown mode\nsmx direct_plus100_e9=\n seq_err_ppt=\n tree_err_ppt=\n one_err_ppt=\n tol_ppt=\nsmx FAIL\nsmx OK\nmut=\nmeasure \n dram_bytes=\n" 19 global dig uchar 32 20 global ab uchar 256 21 global argbad uchar 1 22 global xs f64 32 23 global vs f64 32 24 global ms f64 64 25 global ss f64 64 26 global ps f64 64 27 28 note ---- output ---- 29 fn say id ulong to void 30 k = ulong 1 31 s = ulong 0 32 for i ulong len msg 33 if eq get msg i 10 34 if eq k id 35 r = write 1 msg s sub i s 36 ret 37 end 38 k = add k 1 39 s = add i 1 40 end 41 end 42 exit 99 43 end 44 fn newline to void 45 put dig 0 10 46 r = write 1 dig 0 1 47 end 48 fn num v ulong to void 49 x = v 50 n = ulong 0 51 while ge x 10 bound 64 52 put dig n uchar add 48 rem x 10 53 x = div x 10 54 n = add n 1 55 end 56 put dig n uchar add 48 x 57 n = add n 1 58 for i ulong n 59 r = write 1 dig sub sub n i 1 1 60 end 61 end 62 63 fn argdec i ulong to ulong 64 note Keep parse status separate: every ulong, including the old failure sentinel, is valid decimal input. 65 put argbad 0 0 66 r = argread i ab 0 20 67 if lt r 1 68 put argbad 0 1 69 ret 0xffffffffffffffff 70 end 71 v = ulong 0 72 for k ulong ulong r 73 c = get ab k 74 if or lt c 48 gt c 57 75 put argbad 0 1 76 ret 0xffffffffffffffff 77 end 78 digit = ulong sub c 48 79 note Refuse before either unsigned operation could wrap. 80 if gt v div sub ulong 0xffffffffffffffff digit 10 81 put argbad 0 1 82 ret 0xffffffffffffffff 83 end 84 v = add mul v 10 digit 85 end 86 ret v 87 end 88 note ---- planner ---- 89 fn cdiv a ulong b ulong to ulong 90 ret div add a sub b 1 b 91 end 92 fn pw2 v ulong to ulong 93 ret shl ulong 1 v 94 end 95 fn crt i ulong to ulong 96 ret pw2 rem i 3 97 end 98 fn cct i ulong to ulong 99 ret pw2 rem div i 3 3 100 end 101 fn cwm i ulong to ulong 102 ret pw2 rem div i 9 3 103 end 104 fn cwn i ulong to ulong 105 ret pw2 rem div i 27 3 106 end 107 fn cband i ulong to ulong 108 bj = div i 81 109 if eq bj 0 110 ret 0 111 end 112 ret pw2 add bj 1 113 end 114 fn cregs i ulong to ulong 115 rt = crt i 116 ct = cct i 117 ret mul 4 add add mul 4 rt mul 2 ct mul rt ct 118 end 119 fn cbm i ulong to ulong 120 ret mul mul 16 crt i cwm i 121 end 122 fn cbn i ulong to ulong 123 ret mul mul 8 cct i cwn i 124 end 125 fn cctas i ulong m ulong n ulong to ulong 126 ret mul cdiv m cbm i cdiv n cbn i 127 end 128 fn cl2 i ulong m ulong n ulong kp ulong to ulong 129 x = mul m cdiv n cbn i 130 y = mul n cdiv m cbm i 131 ret mul mul 2 kp add x y 132 end 133 fn cdram i ulong m ulong n ulong kp ulong to ulong 134 a = mul mul 2 m kp 135 b = mul mul 2 kp n 136 nt = cdiv n cbn i 137 bd = cband i 138 bw = mul bd cbn i 139 if gt bw n 140 bw = n 141 end 142 bb = mul mul 2 kp bw 143 if or lt bd 2 gt bb `33554432` 144 ret add mul a nt mul b cdiv m cbm i 145 end 146 ret add mul a cdiv nt bd b 147 end 148 fn ccost i ulong m ulong n ulong kp ulong to f64 149 t2 = div f64 cl2 i m n kp 1827.`5440f64` 150 tc = div f64 mul mul 2 m mul n kp 37166.`0364f64` 151 td = div f64 cdram i m n kp 1890.`5139f64` 152 ret add add t2 tc td 153 end 154 fn show i ulong m ulong n ulong kp ulong to void 155 say 4 156 num crt i 157 say 5 158 num cct i 159 say 6 160 num cwm i 161 say 7 162 num cwn i 163 say 8 164 num cband i 165 say 9 166 num cbm i 167 say 10 168 num cbn i 169 say 11 170 num cregs i 171 say 12 172 num cctas i m n 173 say 13 174 num cl2 i m n kp 175 say 26 176 num cdram i m n kp 177 say 14 178 num ulong ccost i m n kp 179 newline 180 end 181 fn cfgline i ulong to void 182 say 4 183 num crt i 184 say 5 185 num cct i 186 say 6 187 num cwm i 188 say 7 189 num cwn i 190 say 8 191 num cband i 192 newline 193 end 194 fn pick bi ulong to void 195 say 15 196 cfgline bi 197 end 198 fn measure i ulong to void 199 say 25 200 cfgline i 201 end 202 fn plan m ulong n ulong k ulong regs ulong to int 203 if or or eq m 0 eq n 0 eq k 0 204 say 2 205 newline 206 ret 2 207 end 208 if or or gt m 16384 gt n 16384 gt k 16384 209 say 2 210 newline 211 ret 2 212 end 213 kp = mul cdiv k 32 32 214 best = 1.`0e300f64` 215 next = 1.`0e300f64` 216 bi = ulong 9999 217 b2 = ulong 9999 218 for i ulong 405 219 if le cregs i regs 220 show i m n kp 221 c = ccost i m n kp 222 if lt c best 223 next = best 224 b2 = bi 225 best = c 226 bi = i 227 else 228 if lt c next 229 next = c 230 b2 = i 231 end 232 end 233 end 234 end 235 if eq bi 9999 236 say 3 237 newline 238 ret 2 239 end 240 pick bi 241 measure bi 242 if ne b2 9999 243 measure b2 244 end 245 dflt = ulong 232 246 if and le cregs dflt regs and ne dflt bi ne dflt b2 247 measure dflt 248 end 249 ret 0 250 end 251 252 note ---- streaming softmax ---- 253 note exp by argument halving: exp(y) = exp(y / 256)^256, Taylor to 14 terms 254 fn ex y f64 to f64 255 z = div y 256.0f64 256 t = 1.0f64 257 s = 1.0f64 258 for i uint 14 259 t = div mul t z f64 add i 1 260 s = add s t 261 end 262 for j uint 8 263 s = mul s s 264 end 265 ret s 266 end 267 fn vabs d f64 to f64 268 if lt d 0.0f64 269 ret sub 0.0f64 d 270 end 271 ret d 272 end 273 fn vmax x1 f64 x2 f64 to f64 274 if gt x1 x2 275 ret x1 276 end 277 ret x2 278 end 279 note summary of tile t over scores [lo, hi): m = max, s = sum e^(x-m), p = sum v e^(x-m) 280 fn tile t uint lo uint hi uint to void 281 m = get xs lo 282 for i uint sub hi lo 283 m = vmax m get xs add lo i 284 end 285 s = 0.0f64 286 p = 0.0f64 287 for i uint sub hi lo 288 w = ex sub get xs add lo i m 289 s = add s w 290 p = add p mul w get vs add lo i 291 end 292 put ms t m 293 put ss t s 294 put ps t p 295 end 296 note merge summary b into a (slot a), mutant 2 skips the rescale 297 fn merge a uint b uint mut ulong to void 298 m1 = get ms a 299 m2 = get ms b 300 m = vmax m1 m2 301 e1 = ex sub m1 m 302 e2 = ex sub m2 m 303 if eq mut 2 304 e1 = 1.0f64 305 e2 = 1.0f64 306 end 307 put ss a add mul e1 get ss a mul e2 get ss b 308 put ps a add mul e1 get ps a mul e2 get ps b 309 put ms a m 310 end 311 fn ppt d f64 r f64 to ulong 312 e = div vabs d vabs r 313 if not lt e 1.`0e30f64` 314 ret 0xffffffffffffffff 315 end 316 ret ulong mul e 1.`0e12f64` 317 end 318 fn fixture to void 319 for i uint 32 320 put xs i div sub f64 rem add mul i 37 11 64 32.0f64 4.0f64 321 put vs i div sub f64 rem add mul i 53 7 41 20.0f64 8.0f64 322 end 323 end 324 fn direct to f64 325 m = get xs 0 326 for i uint 32 327 m = vmax m get xs i 328 end 329 s = 0.0f64 330 p = 0.0f64 331 for i uint 32 332 w = ex sub get xs i m 333 s = add s w 334 p = add p mul w get vs i 335 end 336 ret div p s 337 end 338 fn tiles base uint to void 339 for t uint 4 340 tile add base t mul t 8 add mul t 8 8 341 end 342 end 343 fn seqfold mut ulong to f64 344 tiles 0 345 merge 0 1 mut 346 merge 0 2 mut 347 merge 0 3 mut 348 ret div get ps 0 get ss 0 349 end 350 fn treefold mut ulong to f64 351 tiles 8 352 merge 8 9 mut 353 merge 10 11 mut 354 if eq mut 3 355 merge 9 11 mut 356 else 357 merge 8 10 mut 358 end 359 ret div get ps 8 get ss 8 360 end 361 note mutant 1: each tile normalised on its own, the four outputs averaged 362 fn localnorm to f64 363 tiles 16 364 one = 0.0f64 365 for t uint 4 366 one = add one div div get ps add 16 t get ss add 16 t 4.0f64 367 end 368 ret one 369 end 370 fn smx mut ulong to int 371 fixture 372 ref = direct 373 sq = ppt sub seqfold mut ref ref 374 tq = ppt sub treefold mut ref ref 375 oq = ppt sub localnorm ref ref 376 say 17 377 num ulong mul add ref 100.0f64 1.0e9f64 378 say 18 379 num sq 380 say 19 381 num tq 382 say 20 383 num oq 384 say 21 385 num `2000000` 386 newline 387 bad = or gt sq `2000000` gt tq `2000000` 388 if eq mut 1 389 bad = gt oq `2000000` 390 end 391 if bad 392 say 22 393 newline 394 ret 1 395 end 396 say 23 397 newline 398 ret 0 399 end 400 401 fn main to int 402 if lt argcnt 2 403 say 1 404 newline 405 ret 3 406 end 407 sel = argdec 1 408 if eq sel 1 409 if lt argcnt 5 410 say 1 411 newline 412 ret 3 413 end 414 regs = ulong 100 415 if ge argcnt 6 416 regs = argdec 5 417 if ne get argbad 0 0 418 say 1 419 newline 420 ret 3 421 end 422 end 423 ret plan argdec 2 argdec 3 argdec 4 regs 424 end 425 if eq sel 2 426 mut = ulong 0 427 if ge argcnt 3 428 mut = argdec 2 429 end 430 ret smx mut 431 end 432 say 16 433 newline 434 ret 3 435 end