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 / gpu / forge / envelope.om
| 1 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | note gpu/forge/envelope.om: FORGE physical feasibility & roofline envelope checker. |
| 3 | note Validates working-set memory, CTA/wave sizing, shared memory, register pressure, |
| 4 | note theoretical compute vs bandwidth roofline (f64), thermal/power ceilings, and |
| 5 | note latency/energy budgets. Fail-closed with named physical violation codes. |
| 6 | note Composable OM-2 library module (compose after hardware.om). |
| 7 | |
| 8 | note ---- Physical Envelope Violation Codes ---- |
| 9 | const FG_ENV_OK uint 0 |
| 10 | const FG_ENV_ERR_INVALID_HW uint 1 |
| 11 | const FG_ENV_ERR_INVALID_DIMS uint 2 |
| 12 | const FG_ENV_ERR_UNSUPPORTED_OP uint 3 |
| 13 | const FG_ENV_ERR_UNSUPPORTED_DTYPE uint 4 |
| 14 | const FG_ENV_ERR_OOM_WORKING_SET uint 5 |
| 15 | const FG_ENV_ERR_SMEM_EXCEEDED uint 6 |
| 16 | const FG_ENV_ERR_GPR_EXCEEDED uint 7 |
| 17 | const FG_ENV_ERR_THREADS_EXCEEDED uint 8 |
| 18 | const FG_ENV_ERR_WAVE_SM_CAP uint 9 |
| 19 | const FG_ENV_ERR_THERMAL_CEILING uint 10 |
| 20 | const FG_ENV_ERR_POWER_CEILING uint 11 |
| 21 | const FG_ENV_ERR_LATENCY_BUDGET uint 12 |
| 22 | const FG_ENV_ERR_ENERGY_BUDGET uint 13 |
| 23 | |
| 24 | const FG_U64_MAX ulong 0xffffffffffffffff |
| 25 | const FG_GPU_REGS_PER_SM ulong 65536 |
| 26 | |
| 27 | note ---- Bit, Overflow & Dtype Helpers ---- |
| 28 | |
| 29 | fn fg_single_bit v uint to bool |
| 30 | if eq v 0 |
| 31 | ret false |
| 32 | end |
| 33 | ret eq and v sub v 1 0 |
| 34 | end |
| 35 | |
| 36 | fn fg_mul_ovf a ulong b ulong to bool |
| 37 | if eq a 0 |
| 38 | ret false |
| 39 | end |
| 40 | if eq b 0 |
| 41 | ret false |
| 42 | end |
| 43 | ret gt a div FG_U64_MAX b |
| 44 | end |
| 45 | |
| 46 | fn fg_add_ovf a ulong b ulong to bool |
| 47 | ret gt b sub FG_U64_MAX a |
| 48 | end |
| 49 | |
| 50 | fn fg_dtype_bytes dt uint to ulong |
| 51 | if eq dt FG_DT_FP8 |
| 52 | ret ulong 1 |
| 53 | end |
| 54 | if eq dt FG_DT_INT8 |
| 55 | ret ulong 1 |
| 56 | end |
| 57 | if eq dt FG_DT_FP16 |
| 58 | ret ulong 2 |
| 59 | end |
| 60 | if eq dt FG_DT_BF16 |
| 61 | ret ulong 2 |
| 62 | end |
| 63 | if eq dt FG_DT_FP32 |
| 64 | ret ulong 4 |
| 65 | end |
| 66 | if eq dt FG_DT_INT32 |
| 67 | ret ulong 4 |
| 68 | end |
| 69 | if eq dt FG_DT_U64 |
| 70 | ret ulong 8 |
| 71 | end |
| 72 | if eq dt FG_DT_FP64 |
| 73 | ret ulong 8 |
| 74 | end |
| 75 | ret ulong 0 |
| 76 | end |
| 77 | |
| 78 | fn fg_dtype_scale hcap view ForgeHwCap dt uint to f64 |
| 79 | has_tc = ne and get hcap feature_flags FG_FEAT_TENSOR_CORES 0 |
| 80 | if eq dt FG_DT_FP8 |
| 81 | if has_tc |
| 82 | ret 8.0f64 |
| 83 | end |
| 84 | ret 1.0f64 |
| 85 | end |
| 86 | if eq dt FG_DT_INT8 |
| 87 | if has_tc |
| 88 | ret 8.0f64 |
| 89 | end |
| 90 | ret 4.0f64 |
| 91 | end |
| 92 | if eq dt FG_DT_FP16 |
| 93 | if has_tc |
| 94 | ret 4.0f64 |
| 95 | end |
| 96 | ret 2.0f64 |
| 97 | end |
| 98 | if eq dt FG_DT_BF16 |
| 99 | if has_tc |
| 100 | ret 4.0f64 |
| 101 | end |
| 102 | ret 2.0f64 |
| 103 | end |
| 104 | if eq dt FG_DT_FP64 |
| 105 | ret 0.5f64 |
| 106 | end |
| 107 | ret 1.0f64 |
| 108 | end |
| 109 | |
| 110 | note Single-wave clamp helper: clamps CTA count to [1, sm_count] |
| 111 | fn fg_env_clamp_wave ctas uint sm_count uint to uint |
| 112 | if eq sm_count 0 |
| 113 | ret uint 0 |
| 114 | end |
| 115 | if eq ctas 0 |
| 116 | ret uint 1 |
| 117 | end |
| 118 | if gt ctas sm_count |
| 119 | ret sm_count |
| 120 | end |
| 121 | ret ctas |
| 122 | end |
| 123 | |
| 124 | fn fg_env_reset env edit ForgeEnvCheck to void |
| 125 | put env ok uint 0 |
| 126 | put env violation FG_ENV_OK |
| 127 | put env ws_bytes ulong 0 |
| 128 | put env traffic_bytes ulong 0 |
| 129 | put env flops ulong 0 |
| 130 | put env eff_ctas uint 0 |
| 131 | put env eff_thr uint 0 |
| 132 | put env waves uint 0 |
| 133 | put env arith_int 0.0f64 |
| 134 | put env comp_us 0.0f64 |
| 135 | put env bw_us 0.0f64 |
| 136 | put env roof_us 0.0f64 |
| 137 | put env est_power_w 0.0f64 |
| 138 | put env est_energy_mj 0.0f64 |
| 139 | put env compute_bound uint 0 |
| 140 | ret |
| 141 | end |
| 142 | |
| 143 | fn fg_env_fail env edit ForgeEnvCheck code uint to uint |
| 144 | put env ok uint 0 |
| 145 | put env violation code |
| 146 | ret code |
| 147 | end |
| 148 | |
| 149 | fn fg_env_wl_matmul req view ForgeContract eb ulong env edit ForgeEnvCheck to bool |
| 150 | dm = get req dim_m |
| 151 | dn = get req dim_n |
| 152 | dk = get req dim_k |
| 153 | db = get req batch |
| 154 | if fg_mul_ovf dm dk |
| 155 | ret false |
| 156 | end |
| 157 | mk = mul dm dk |
| 158 | if fg_mul_ovf dk dn |
| 159 | ret false |
| 160 | end |
| 161 | kn = mul dk dn |
| 162 | if fg_mul_ovf dm dn |
| 163 | ret false |
| 164 | end |
| 165 | mn = mul dm dn |
| 166 | if fg_add_ovf mk kn |
| 167 | ret false |
| 168 | end |
| 169 | elems = add mk kn |
| 170 | if fg_add_ovf elems mn |
| 171 | ret false |
| 172 | end |
| 173 | elems = add elems mn |
| 174 | if fg_mul_ovf elems db |
| 175 | ret false |
| 176 | end |
| 177 | elems = mul elems db |
| 178 | if fg_mul_ovf elems eb |
| 179 | ret false |
| 180 | end |
| 181 | put env traffic_bytes mul elems eb |
| 182 | if fg_mul_ovf mn dk |
| 183 | ret false |
| 184 | end |
| 185 | fl = mul mn dk |
| 186 | if fg_mul_ovf fl db |
| 187 | ret false |
| 188 | end |
| 189 | fl = mul fl db |
| 190 | if fg_mul_ovf fl 2 |
| 191 | ret false |
| 192 | end |
| 193 | put env flops mul fl 2 |
| 194 | ret true |
| 195 | end |
| 196 | |
| 197 | fn fg_env_wl_matvec req view ForgeContract eb ulong env edit ForgeEnvCheck to bool |
| 198 | dm = get req dim_m |
| 199 | dk = get req dim_k |
| 200 | db = get req batch |
| 201 | if fg_mul_ovf dm dk |
| 202 | ret false |
| 203 | end |
| 204 | mk = mul dm dk |
| 205 | if fg_add_ovf mk dk |
| 206 | ret false |
| 207 | end |
| 208 | elems = add mk dk |
| 209 | if fg_add_ovf elems dm |
| 210 | ret false |
| 211 | end |
| 212 | elems = add elems dm |
| 213 | if fg_mul_ovf elems db |
| 214 | ret false |
| 215 | end |
| 216 | elems = mul elems db |
| 217 | if fg_mul_ovf elems eb |
| 218 | ret false |
| 219 | end |
| 220 | put env traffic_bytes mul elems eb |
| 221 | if fg_mul_ovf mk db |
| 222 | ret false |
| 223 | end |
| 224 | fl = mul mk db |
| 225 | if fg_mul_ovf fl 2 |
| 226 | ret false |
| 227 | end |
| 228 | put env flops mul fl 2 |
| 229 | ret true |
| 230 | end |
| 231 | |
| 232 | fn fg_env_wl_elem req view ForgeContract eb ulong env edit ForgeEnvCheck to bool |
| 233 | dm = get req dim_m |
| 234 | dn = get req dim_n |
| 235 | db = get req batch |
| 236 | if fg_mul_ovf dm dn |
| 237 | ret false |
| 238 | end |
| 239 | mn = mul dm dn |
| 240 | if fg_mul_ovf mn db |
| 241 | ret false |
| 242 | end |
| 243 | bmn = mul mn db |
| 244 | put env flops bmn |
| 245 | if fg_mul_ovf bmn 3 |
| 246 | ret false |
| 247 | end |
| 248 | elems = mul bmn 3 |
| 249 | if fg_mul_ovf elems eb |
| 250 | ret false |
| 251 | end |
| 252 | put env traffic_bytes mul elems eb |
| 253 | ret true |
| 254 | end |
| 255 | |
| 256 | fn fg_env_wl_red req view ForgeContract eb ulong env edit ForgeEnvCheck to bool |
| 257 | dm = get req dim_m |
| 258 | dn = get req dim_n |
| 259 | db = get req batch |
| 260 | if fg_mul_ovf dm dn |
| 261 | ret false |
| 262 | end |
| 263 | mn = mul dm dn |
| 264 | if fg_mul_ovf mn db |
| 265 | ret false |
| 266 | end |
| 267 | put env flops mul mn db |
| 268 | if fg_add_ovf mn dm |
| 269 | ret false |
| 270 | end |
| 271 | elems = add mn dm |
| 272 | if fg_mul_ovf elems db |
| 273 | ret false |
| 274 | end |
| 275 | elems = mul elems db |
| 276 | if fg_mul_ovf elems eb |
| 277 | ret false |
| 278 | end |
| 279 | put env traffic_bytes mul elems eb |
| 280 | ret true |
| 281 | end |
| 282 | |
| 283 | note Compute overflow-safe working-set bytes, memory traffic bytes, and FLOPs |
| 284 | fn fg_env_calc_workload req view ForgeContract env edit ForgeEnvCheck to bool |
| 285 | op = get req op_class |
| 286 | eb = fg_dtype_bytes get req dtype |
| 287 | if eq eb 0 |
| 288 | ret false |
| 289 | end |
| 290 | ok = false |
| 291 | if eq op FG_OP_MATMUL |
| 292 | ok = fg_env_wl_matmul req eb env |
| 293 | else |
| 294 | if eq op FG_OP_MATVEC |
| 295 | ok = fg_env_wl_matvec req eb env |
| 296 | else |
| 297 | if eq op FG_OP_ELEMENTWISE |
| 298 | ok = fg_env_wl_elem req eb env |
| 299 | else |
| 300 | if eq op FG_OP_REDUCTION |
| 301 | ok = fg_env_wl_red req eb env |
| 302 | else |
| 303 | ok = fg_env_wl_matmul req eb env |
| 304 | end |
| 305 | end |
| 306 | end |
| 307 | end |
| 308 | if not ok |
| 309 | ret false |
| 310 | end |
| 311 | tr_fin = get env traffic_bytes |
| 312 | ex = get req extra_ws |
| 313 | if fg_add_ovf tr_fin ex |
| 314 | ret false |
| 315 | end |
| 316 | put env ws_bytes add tr_fin ex |
| 317 | ret true |
| 318 | end |
| 319 | |
| 320 | fn fg_env_auto_tile hid view ForgeHwId dt uint to ulong |
| 321 | if eq get hid substrate_class FG_SUB_DIGITAL_GPU |
| 322 | if or eq dt FG_DT_FP8 eq dt FG_DT_INT8 |
| 323 | ret ulong 128 |
| 324 | end |
| 325 | if or eq dt FG_DT_FP16 eq dt FG_DT_BF16 |
| 326 | ret ulong 128 |
| 327 | end |
| 328 | if or eq dt FG_DT_FP32 eq dt FG_DT_INT32 |
| 329 | ret ulong 64 |
| 330 | end |
| 331 | ret ulong 32 |
| 332 | end |
| 333 | ret ulong 64 |
| 334 | end |
| 335 | |
| 336 | note Validate CTA geometry, threads/CTA, shared memory, register pressure, and wave cap |
| 337 | fn fg_env_check_geom hid view ForgeHwId hcap view ForgeHwCap req view ForgeContract env edit ForgeEnvCheck to uint |
| 338 | if gt get req smem_req get hcap smem_cta |
| 339 | ret fg_env_fail env FG_ENV_ERR_SMEM_EXCEEDED |
| 340 | end |
| 341 | if gt get req gpr_req get hid max_gpr |
| 342 | ret fg_env_fail env FG_ENV_ERR_GPR_EXCEEDED |
| 343 | end |
| 344 | thr = get req thr_req |
| 345 | if eq thr 0 |
| 346 | if eq get hid substrate_class FG_SUB_DIGITAL_GPU |
| 347 | if ge get hid max_thr_cta 256 |
| 348 | thr = uint 256 |
| 349 | else |
| 350 | thr = get hid max_thr_cta |
| 351 | end |
| 352 | else |
| 353 | thr = uint 1 |
| 354 | end |
| 355 | end |
| 356 | if gt thr get hid max_thr_cta |
| 357 | ret fg_env_fail env FG_ENV_ERR_THREADS_EXCEEDED |
| 358 | end |
| 359 | note Per-SM register file pressure check on GPU (64K 32-bit regs/SM) |
| 360 | if eq get hid substrate_class FG_SUB_DIGITAL_GPU |
| 361 | if gt get req gpr_req 0 |
| 362 | regs_cta = mul ulong thr ulong get req gpr_req |
| 363 | if gt regs_cta FG_GPU_REGS_PER_SM |
| 364 | ret fg_env_fail env FG_ENV_ERR_GPR_EXCEEDED |
| 365 | end |
| 366 | end |
| 367 | end |
| 368 | put env eff_thr thr |
| 369 | sm = get hid sm_count |
| 370 | ctas = get req cta_req |
| 371 | if eq ctas 0 |
| 372 | ts = fg_env_auto_tile hid get req dtype |
| 373 | tiles_m = div add get req dim_m sub ts 1 ts |
| 374 | tiles_n = div add get req dim_n sub ts 1 ts |
| 375 | if fg_mul_ovf tiles_m tiles_n |
| 376 | ctas = sm |
| 377 | else |
| 378 | tot_t = mul tiles_m tiles_n |
| 379 | if gt tot_t ulong 65536 |
| 380 | ctas = uint 65536 |
| 381 | else |
| 382 | ctas = uint tot_t |
| 383 | end |
| 384 | end |
| 385 | if eq ctas 0 |
| 386 | ctas = uint 1 |
| 387 | end |
| 388 | if eq get req req_wave_cap 1 |
| 389 | ctas = fg_env_clamp_wave ctas sm |
| 390 | end |
| 391 | else |
| 392 | if eq get req req_wave_cap 1 |
| 393 | if gt ctas sm |
| 394 | ret fg_env_fail env FG_ENV_ERR_WAVE_SM_CAP |
| 395 | end |
| 396 | end |
| 397 | end |
| 398 | put env eff_ctas ctas |
| 399 | wv = div add ctas sub sm 1 sm |
| 400 | if eq wv 0 |
| 401 | wv = uint 1 |
| 402 | end |
| 403 | put env waves wv |
| 404 | ret FG_ENV_OK |
| 405 | end |
| 406 | |
| 407 | fn fg_env_calc_roofline hcap view ForgeHwCap env edit ForgeEnvCheck dt uint to void |
| 408 | scale = fg_dtype_scale hcap dt |
| 409 | peak_tf = mul get hcap peak_fp32_tf scale |
| 410 | fl_f = f64 get env flops |
| 411 | tr_f = f64 get env traffic_bytes |
| 412 | comp_us = div fl_f mul peak_tf 1.0e6f64 |
| 413 | bw_us = div tr_f mul get hcap bw_gbps 1.0e3f64 |
| 414 | roof_us = fmax comp_us bw_us |
| 415 | ai = 0.0f64 |
| 416 | if gt tr_f 0.0f64 |
| 417 | ai = div fl_f tr_f |
| 418 | end |
| 419 | put env arith_int ai |
| 420 | put env comp_us comp_us |
| 421 | put env bw_us bw_us |
| 422 | put env roof_us roof_us |
| 423 | is_cb = uint 0 |
| 424 | if ge comp_us bw_us |
| 425 | is_cb = uint 1 |
| 426 | end |
| 427 | put env compute_bound is_cb |
| 428 | ret |
| 429 | end |
| 430 | |
| 431 | fn fg_env_check_thermal hid view ForgeHwId hcap view ForgeHwCap bud view ForgeBudget env edit ForgeEnvCheck to uint |
| 432 | if gt get bud temp_mc get hcap max_temp_mc |
| 433 | ret fg_env_fail env FG_ENV_ERR_THERMAL_CEILING |
| 434 | end |
| 435 | sm_f = f64 get hid sm_count |
| 436 | cta_f = f64 get env eff_ctas |
| 437 | util = 1.0f64 |
| 438 | if lt cta_f sm_f |
| 439 | util = div cta_f sm_f |
| 440 | end |
| 441 | regime = 0.85f64 |
| 442 | if eq get env compute_bound 1 |
| 443 | regime = 1.0f64 |
| 444 | end |
| 445 | pw_frac = add 0.35f64 mul 0.65f64 util |
| 446 | est_pw = mul mul get hcap max_power_w pw_frac regime |
| 447 | put env est_power_w est_pw |
| 448 | pcap = get hcap max_power_w |
| 449 | if gt get bud power_w 0.0f64 |
| 450 | if lt get bud power_w pcap |
| 451 | pcap = get bud power_w |
| 452 | end |
| 453 | end |
| 454 | if gt est_pw pcap |
| 455 | ret fg_env_fail env FG_ENV_ERR_POWER_CEILING |
| 456 | end |
| 457 | est_mj = mul est_pw mul get env roof_us 1.0e-3f64 |
| 458 | put env est_energy_mj est_mj |
| 459 | if gt get bud lat_us 0.0f64 |
| 460 | if gt get env roof_us get bud lat_us |
| 461 | ret fg_env_fail env FG_ENV_ERR_LATENCY_BUDGET |
| 462 | end |
| 463 | end |
| 464 | if gt get bud energy_mj 0.0f64 |
| 465 | if gt est_mj get bud energy_mj |
| 466 | ret fg_env_fail env FG_ENV_ERR_ENERGY_BUDGET |
| 467 | end |
| 468 | end |
| 469 | put env ok uint 1 |
| 470 | put env violation FG_ENV_OK |
| 471 | ret FG_ENV_OK |
| 472 | end |
| 473 | |
| 474 | note Compute theoretical compute vs bandwidth roofline, thermal, power, latency & energy |
| 475 | fn fg_env_check_roofline hid view ForgeHwId hcap view ForgeHwCap bud view ForgeBudget env edit ForgeEnvCheck dt uint to uint |
| 476 | fg_env_calc_roofline hcap env dt |
| 477 | ret fg_env_check_thermal hid hcap bud env |
| 478 | end |
| 479 | |
| 480 | note Primary entry point: full fail-closed physical feasibility & roofline check |
| 481 | fn fg_env_check hid view ForgeHwId hcap view ForgeHwCap req view ForgeContract bud view ForgeBudget env edit ForgeEnvCheck to uint |
| 482 | fg_env_reset env |
| 483 | if ne fg_hw_validate hid hcap FG_HWVAL_OK |
| 484 | ret fg_env_fail env FG_ENV_ERR_INVALID_HW |
| 485 | end |
| 486 | if not fg_single_bit get req op_class |
| 487 | ret fg_env_fail env FG_ENV_ERR_INVALID_DIMS |
| 488 | end |
| 489 | if not fg_single_bit get req dtype |
| 490 | ret fg_env_fail env FG_ENV_ERR_INVALID_DIMS |
| 491 | end |
| 492 | if eq get req dim_m 0 |
| 493 | ret fg_env_fail env FG_ENV_ERR_INVALID_DIMS |
| 494 | end |
| 495 | if eq get req dim_n 0 |
| 496 | ret fg_env_fail env FG_ENV_ERR_INVALID_DIMS |
| 497 | end |
| 498 | if eq get req dim_k 0 |
| 499 | ret fg_env_fail env FG_ENV_ERR_INVALID_DIMS |
| 500 | end |
| 501 | if eq get req batch 0 |
| 502 | ret fg_env_fail env FG_ENV_ERR_INVALID_DIMS |
| 503 | end |
| 504 | if eq and get hcap op_flags get req op_class 0 |
| 505 | ret fg_env_fail env FG_ENV_ERR_UNSUPPORTED_OP |
| 506 | end |
| 507 | if eq and get hcap dtype_flags get req dtype 0 |
| 508 | ret fg_env_fail env FG_ENV_ERR_UNSUPPORTED_DTYPE |
| 509 | end |
| 510 | if not fg_env_calc_workload req env |
| 511 | ret fg_env_fail env FG_ENV_ERR_OOM_WORKING_SET |
| 512 | end |
| 513 | if gt get env ws_bytes get hcap mem_avail |
| 514 | ret fg_env_fail env FG_ENV_ERR_OOM_WORKING_SET |
| 515 | end |
| 516 | grc = fg_env_check_geom hid hcap req env |
| 517 | if ne grc FG_ENV_OK |
| 518 | ret grc |
| 519 | end |
| 520 | ret fg_env_check_roofline hid hcap bud env get req dtype |
| 521 | end |