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 / main.om
| 1 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | note gpu/forge/main.om: FORGE CLI entrypoint. |
| 3 | note Reads realization requests on stdin (KAT, PROFILES, PLAN, REQ) and emits |
| 4 | note deterministic plans, physical envelope refusals, and canonical receipts on stdout. |
| 5 | note Compose as: si/directives/om2.om hardware.om envelope.om provider.om realize.om main.om |
| 6 | |
| 7 | text fg_m_kat_pass "FORGE_KAT PASS 10d63d05f888eaba4fe473c5f17febe22f462f543ae0c2b005a9a8e5417e09fd\n" |
| 8 | text fg_m_kat_fail "FORGE_KAT FAIL\n" |
| 9 | text fg_m_bad "FORGE_ERROR malformed_request\n" |
| 10 | text fg_m_prov_hdr "PROVIDER id=" |
| 11 | text fg_m_cmd_kat "KAT" |
| 12 | text fg_m_cmd_prof "PROFILES" |
| 13 | text fg_m_cmd_plan "PLAN" |
| 14 | text fg_m_cmd_req "REQ" |
| 15 | text fg_m_sub_any "ANY" |
| 16 | |
| 17 | fn fg_is_space c uchar to bool |
| 18 | if eq c 32 |
| 19 | ret true |
| 20 | end |
| 21 | if eq c 9 |
| 22 | ret true |
| 23 | end |
| 24 | if eq c 13 |
| 25 | ret true |
| 26 | end |
| 27 | ret false |
| 28 | end |
| 29 | |
| 30 | fn fg_tok_eq tok view uchar 64 tlen ulong lit view uchar * to bool |
| 31 | if ne tlen len lit |
| 32 | ret false |
| 33 | end |
| 34 | i = ulong 0 |
| 35 | while lt i tlen bound 64 |
| 36 | if ne get tok i get lit i |
| 37 | ret false |
| 38 | end |
| 39 | i = add i 1 |
| 40 | end |
| 41 | ret true |
| 42 | end |
| 43 | |
| 44 | fn fg_next_tok line view uchar * cur edit ulong 1 tok edit uchar 64 to ulong |
| 45 | n = len line |
| 46 | p = get cur 0 |
| 47 | while lt p n bound 16384 |
| 48 | if not fg_is_space get line p |
| 49 | break |
| 50 | end |
| 51 | p = add p 1 |
| 52 | end |
| 53 | tlen = ulong 0 |
| 54 | while lt p n bound 16384 |
| 55 | c = get line p |
| 56 | if fg_is_space c |
| 57 | break |
| 58 | end |
| 59 | if lt tlen 64 |
| 60 | put tok tlen c |
| 61 | tlen = add tlen 1 |
| 62 | end |
| 63 | p = add p 1 |
| 64 | end |
| 65 | put cur 0 p |
| 66 | ret tlen |
| 67 | end |
| 68 | |
| 69 | fn fg_parse_u64 tok view uchar 64 tlen ulong ok edit uint 1 to ulong |
| 70 | if eq tlen 0 |
| 71 | put ok 0 uint 0 |
| 72 | ret ulong 0 |
| 73 | end |
| 74 | v = ulong 0 |
| 75 | i = ulong 0 |
| 76 | while lt i tlen bound 64 |
| 77 | c = get tok i |
| 78 | if lt c 48 |
| 79 | put ok 0 uint 0 |
| 80 | ret ulong 0 |
| 81 | end |
| 82 | if gt c 57 |
| 83 | put ok 0 uint 0 |
| 84 | ret ulong 0 |
| 85 | end |
| 86 | d = ulong sub c 48 |
| 87 | if gt v div sub FG_U64_MAX d 10 |
| 88 | put ok 0 uint 0 |
| 89 | ret ulong 0 |
| 90 | end |
| 91 | v = add mul v 10 d |
| 92 | i = add i 1 |
| 93 | end |
| 94 | put ok 0 uint 1 |
| 95 | ret v |
| 96 | end |
| 97 | |
| 98 | fn fg_parse_op tok view uchar 64 tlen ulong to uint |
| 99 | if fg_tok_eq tok tlen fg_t_op_mv |
| 100 | ret FG_OP_MATVEC |
| 101 | end |
| 102 | if fg_tok_eq tok tlen fg_t_op_mm |
| 103 | ret FG_OP_MATMUL |
| 104 | end |
| 105 | if fg_tok_eq tok tlen fg_t_op_cv |
| 106 | ret FG_OP_CONV |
| 107 | end |
| 108 | if fg_tok_eq tok tlen fg_t_op_ew |
| 109 | ret FG_OP_ELEMENTWISE |
| 110 | end |
| 111 | if fg_tok_eq tok tlen fg_t_op_rd |
| 112 | ret FG_OP_REDUCTION |
| 113 | end |
| 114 | if fg_tok_eq tok tlen fg_t_op_sp |
| 115 | ret FG_OP_SPIKE_INT |
| 116 | end |
| 117 | if fg_tok_eq tok tlen fg_t_op_tr |
| 118 | ret FG_OP_TRANSFORM |
| 119 | end |
| 120 | if fg_tok_eq tok tlen fg_t_op_sm |
| 121 | ret FG_OP_SAMPLE |
| 122 | end |
| 123 | if fg_tok_eq tok tlen fg_t_op_gn |
| 124 | ret FG_OP_GENERAL |
| 125 | end |
| 126 | ret uint 0 |
| 127 | end |
| 128 | |
| 129 | fn fg_parse_dt tok view uchar 64 tlen ulong to uint |
| 130 | if fg_tok_eq tok tlen fg_t_dt_f32 |
| 131 | ret FG_DT_FP32 |
| 132 | end |
| 133 | if fg_tok_eq tok tlen fg_t_dt_f16 |
| 134 | ret FG_DT_FP16 |
| 135 | end |
| 136 | if fg_tok_eq tok tlen fg_t_dt_bf16 |
| 137 | ret FG_DT_BF16 |
| 138 | end |
| 139 | if fg_tok_eq tok tlen fg_t_dt_f8 |
| 140 | ret FG_DT_FP8 |
| 141 | end |
| 142 | if fg_tok_eq tok tlen fg_t_dt_i8 |
| 143 | ret FG_DT_INT8 |
| 144 | end |
| 145 | if fg_tok_eq tok tlen fg_t_dt_i32 |
| 146 | ret FG_DT_INT32 |
| 147 | end |
| 148 | if fg_tok_eq tok tlen fg_t_dt_u64 |
| 149 | ret FG_DT_U64 |
| 150 | end |
| 151 | if fg_tok_eq tok tlen fg_t_dt_f64 |
| 152 | ret FG_DT_FP64 |
| 153 | end |
| 154 | ret uint 0 |
| 155 | end |
| 156 | |
| 157 | fn fg_parse_sub tok view uchar 64 tlen ulong ok edit uint 1 to uint |
| 158 | put ok 0 uint 1 |
| 159 | if fg_tok_eq tok tlen fg_m_sub_any |
| 160 | ret uint 0 |
| 161 | end |
| 162 | if fg_tok_eq tok tlen fg_t_sub_cpu |
| 163 | ret FG_SUB_DIGITAL_CPU |
| 164 | end |
| 165 | if fg_tok_eq tok tlen fg_t_sub_gpu |
| 166 | ret FG_SUB_DIGITAL_GPU |
| 167 | end |
| 168 | if fg_tok_eq tok tlen fg_t_sub_ana |
| 169 | ret FG_SUB_ANALOG_IN_MEMORY |
| 170 | end |
| 171 | if fg_tok_eq tok tlen fg_t_sub_neu |
| 172 | ret FG_SUB_NEUROMORPHIC |
| 173 | end |
| 174 | if fg_tok_eq tok tlen fg_t_sub_fpg |
| 175 | ret FG_SUB_FPGA_CGRA |
| 176 | end |
| 177 | if fg_tok_eq tok tlen fg_t_sub_opt |
| 178 | ret FG_SUB_OPTICAL_PHOTONIC |
| 179 | end |
| 180 | put ok 0 uint 0 |
| 181 | ret uint 0 |
| 182 | end |
| 183 | |
| 184 | fn fg_cli_profiles to void |
| 185 | p = new ForgeProvider 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 186 | n = fg_reg_count |
| 187 | i = uint 0 |
| 188 | while lt i n bound 16 |
| 189 | if fg_reg_get i p |
| 190 | fg_write_all 1 fg_m_prov_hdr |
| 191 | fg_write_u64 1 ulong get p prov_id |
| 192 | fg_write_all 1 fg_t_sp_hw |
| 193 | fg_emit_hw_name 1 get p hw_id |
| 194 | fg_write_all 1 fg_t_sp_sub |
| 195 | fg_emit_sub_name 1 get p substrate_class |
| 196 | fg_write_all 1 fg_t_sp_qual |
| 197 | fg_emit_qual_name 1 get p qual_status |
| 198 | fg_write_all 1 fg_t_sp_low |
| 199 | fg_emit_low_name 1 get p lowering_kind |
| 200 | fg_write_all 1 fg_t_nl |
| 201 | end |
| 202 | i = add i 1 |
| 203 | end |
| 204 | ret |
| 205 | end |
| 206 | |
| 207 | fn fg_cli_run_plan req view ForgeContract bud view ForgeBudget to void |
| 208 | plan = new ForgePlan 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
| 209 | env = new ForgeEnvCheck 0 0 0 0 0 0 0 0 0.0f64 0.0f64 0.0f64 0.0f64 0.0f64 0.0f64 0 |
| 210 | seals = new ForgeSeals 0 0 0 0 |
| 211 | rc = fg_realize_plan req bud plan env seals |
| 212 | if ne rc FG_ADMIT_OK |
| 213 | fg_emit_receipt 1 req plan env seals |
| 214 | ret |
| 215 | end |
| 216 | fg_emit_receipt 1 req plan env seals |
| 217 | ret |
| 218 | end |
| 219 | |
| 220 | fn fg_cli_parse_plan line view uchar * cur edit ulong 1 seq ulong to bool |
| 221 | tok = new uchar 64 0 |
| 222 | ok = new uint 1 0 |
| 223 | req = new ForgeContract seq 0 0 0 0 0 1 0 0 0 32 0 FG_ERRK_EXACT 0 1 0 |
| 224 | bud = new ForgeBudget 0.0f64 0.0f64 0.0f64 45000 1200 1000000 |
| 225 | tl = fg_next_tok line cur tok |
| 226 | op = fg_parse_op tok tl |
| 227 | if eq op 0 |
| 228 | ret false |
| 229 | end |
| 230 | put req op_class op |
| 231 | tl = fg_next_tok line cur tok |
| 232 | dt = fg_parse_dt tok tl |
| 233 | if eq dt 0 |
| 234 | ret false |
| 235 | end |
| 236 | put req dtype dt |
| 237 | tl = fg_next_tok line cur tok |
| 238 | dm = fg_parse_u64 tok tl ok |
| 239 | if eq get ok 0 0 |
| 240 | ret false |
| 241 | end |
| 242 | put req dim_m dm |
| 243 | tl = fg_next_tok line cur tok |
| 244 | dn = fg_parse_u64 tok tl ok |
| 245 | if eq get ok 0 0 |
| 246 | ret false |
| 247 | end |
| 248 | put req dim_n dn |
| 249 | tl = fg_next_tok line cur tok |
| 250 | dk = fg_parse_u64 tok tl ok |
| 251 | if eq get ok 0 0 |
| 252 | ret false |
| 253 | end |
| 254 | put req dim_k dk |
| 255 | fg_cli_run_plan req bud |
| 256 | ret true |
| 257 | end |
| 258 | |
| 259 | fn fg_cli_read_u32 line view uchar * cur edit ulong 1 ok edit uint 1 to uint |
| 260 | tok = new uchar 64 0 |
| 261 | tl = fg_next_tok line cur tok |
| 262 | v = fg_parse_u64 tok tl ok |
| 263 | if eq get ok 0 0 |
| 264 | ret uint 0 |
| 265 | end |
| 266 | if gt v ulong 0xffffffff |
| 267 | put ok 0 uint 0 |
| 268 | ret uint 0 |
| 269 | end |
| 270 | ret uint v |
| 271 | end |
| 272 | |
| 273 | fn fg_cli_read_u64 line view uchar * cur edit ulong 1 ok edit uint 1 to ulong |
| 274 | tok = new uchar 64 0 |
| 275 | tl = fg_next_tok line cur tok |
| 276 | ret fg_parse_u64 tok tl ok |
| 277 | end |
| 278 | |
| 279 | fn fg_cli_parse_req_tail line view uchar * cur edit ulong 1 req edit ForgeContract bud edit ForgeBudget to bool |
| 280 | ok = new uint 1 0 |
| 281 | tok = new uchar 64 0 |
| 282 | put req cta_req fg_cli_read_u32 line cur ok |
| 283 | if eq get ok 0 0 |
| 284 | ret false |
| 285 | end |
| 286 | put req thr_req fg_cli_read_u32 line cur ok |
| 287 | if eq get ok 0 0 |
| 288 | ret false |
| 289 | end |
| 290 | put req smem_req fg_cli_read_u32 line cur ok |
| 291 | if eq get ok 0 0 |
| 292 | ret false |
| 293 | end |
| 294 | put req gpr_req fg_cli_read_u32 line cur ok |
| 295 | if eq get ok 0 0 |
| 296 | ret false |
| 297 | end |
| 298 | put req extra_ws fg_cli_read_u64 line cur ok |
| 299 | if eq get ok 0 0 |
| 300 | ret false |
| 301 | end |
| 302 | tmc = fg_cli_read_u32 line cur ok |
| 303 | if eq get ok 0 0 |
| 304 | ret false |
| 305 | end |
| 306 | if gt tmc uint 200000 |
| 307 | ret false |
| 308 | end |
| 309 | put bud temp_mc int tmc |
| 310 | lat = fg_cli_read_u64 line cur ok |
| 311 | if eq get ok 0 0 |
| 312 | ret false |
| 313 | end |
| 314 | put bud lat_us f64 lat |
| 315 | eng = fg_cli_read_u64 line cur ok |
| 316 | if eq get ok 0 0 |
| 317 | ret false |
| 318 | end |
| 319 | put bud energy_mj f64 eng |
| 320 | pw = fg_cli_read_u64 line cur ok |
| 321 | if eq get ok 0 0 |
| 322 | ret false |
| 323 | end |
| 324 | put bud power_w f64 pw |
| 325 | tl = fg_next_tok line cur tok |
| 326 | subc = fg_parse_sub tok tl ok |
| 327 | if eq get ok 0 0 |
| 328 | ret false |
| 329 | end |
| 330 | put req req_substrate subc |
| 331 | put req req_qual fg_cli_read_u32 line cur ok |
| 332 | if eq get ok 0 0 |
| 333 | ret false |
| 334 | end |
| 335 | put req req_wave_cap fg_cli_read_u32 line cur ok |
| 336 | if eq get ok 0 0 |
| 337 | ret false |
| 338 | end |
| 339 | ret true |
| 340 | end |
| 341 | |
| 342 | fn fg_cli_parse_req line view uchar * cur edit ulong 1 to bool |
| 343 | tok = new uchar 64 0 |
| 344 | ok = new uint 1 0 |
| 345 | req = new ForgeContract 0 0 0 0 0 0 1 0 0 0 0 0 FG_ERRK_EXACT 0 1 0 |
| 346 | bud = new ForgeBudget 0.0f64 0.0f64 0.0f64 45000 1200 1000000 |
| 347 | put req req_id fg_cli_read_u64 line cur ok |
| 348 | if eq get ok 0 0 |
| 349 | ret false |
| 350 | end |
| 351 | tl = fg_next_tok line cur tok |
| 352 | op = fg_parse_op tok tl |
| 353 | if eq op 0 |
| 354 | ret false |
| 355 | end |
| 356 | put req op_class op |
| 357 | tl = fg_next_tok line cur tok |
| 358 | dt = fg_parse_dt tok tl |
| 359 | if eq dt 0 |
| 360 | ret false |
| 361 | end |
| 362 | put req dtype dt |
| 363 | put req dim_m fg_cli_read_u64 line cur ok |
| 364 | if eq get ok 0 0 |
| 365 | ret false |
| 366 | end |
| 367 | put req dim_n fg_cli_read_u64 line cur ok |
| 368 | if eq get ok 0 0 |
| 369 | ret false |
| 370 | end |
| 371 | put req dim_k fg_cli_read_u64 line cur ok |
| 372 | if eq get ok 0 0 |
| 373 | ret false |
| 374 | end |
| 375 | put req batch fg_cli_read_u64 line cur ok |
| 376 | if eq get ok 0 0 |
| 377 | ret false |
| 378 | end |
| 379 | if not fg_cli_parse_req_tail line cur req bud |
| 380 | ret false |
| 381 | end |
| 382 | fg_cli_run_plan req bud |
| 383 | ret true |
| 384 | end |
| 385 | |
| 386 | fn fg_cli_handle_line line view uchar * seq ulong to int |
| 387 | cur = new ulong 1 0 |
| 388 | tok = new uchar 64 0 |
| 389 | tl = fg_next_tok line cur tok |
| 390 | if eq tl 0 |
| 391 | ret int 0 |
| 392 | end |
| 393 | if eq get tok 0 35 |
| 394 | ret int 0 |
| 395 | end |
| 396 | if fg_tok_eq tok tl fg_m_cmd_kat |
| 397 | if fg_hw_v1_kat |
| 398 | fg_write_all 1 fg_m_kat_pass |
| 399 | ret int 0 |
| 400 | end |
| 401 | fg_write_all 1 fg_m_kat_fail |
| 402 | ret int 1 |
| 403 | end |
| 404 | if fg_tok_eq tok tl fg_m_cmd_prof |
| 405 | fg_cli_profiles |
| 406 | ret int 0 |
| 407 | end |
| 408 | if fg_tok_eq tok tl fg_m_cmd_plan |
| 409 | if fg_cli_parse_plan line cur seq |
| 410 | ret int 0 |
| 411 | end |
| 412 | fg_write_all 1 fg_m_bad |
| 413 | ret int 2 |
| 414 | end |
| 415 | if fg_tok_eq tok tl fg_m_cmd_req |
| 416 | if fg_cli_parse_req line cur |
| 417 | ret int 0 |
| 418 | end |
| 419 | fg_write_all 1 fg_m_bad |
| 420 | ret int 2 |
| 421 | end |
| 422 | fg_write_all 1 fg_m_bad |
| 423 | ret int 2 |
| 424 | end |
| 425 | |
| 426 | fn main to int |
| 427 | fg_reg_load_defaults |
| 428 | inbuf = new uchar 16384 0 |
| 429 | got = ulong 0 |
| 430 | while lt got 16384 bound 16384 |
| 431 | k = read 0 inbuf got sub 16384 got |
| 432 | if lt k 0 |
| 433 | ret int 3 |
| 434 | end |
| 435 | if eq k 0 |
| 436 | break |
| 437 | end |
| 438 | got = add got ulong k |
| 439 | end |
| 440 | pos = ulong 0 |
| 441 | seq = ulong 1 |
| 442 | rc = int 0 |
| 443 | while lt pos got bound 16384 |
| 444 | start = pos |
| 445 | while lt pos got bound 16384 |
| 446 | if eq get inbuf pos 10 |
| 447 | break |
| 448 | end |
| 449 | pos = add pos 1 |
| 450 | end |
| 451 | llen = sub pos start |
| 452 | if gt llen 0 |
| 453 | lrc = fg_cli_handle_line part inbuf start llen seq |
| 454 | if ne lrc 0 |
| 455 | rc = lrc |
| 456 | end |
| 457 | seq = add seq 1 |
| 458 | end |
| 459 | if lt pos got |
| 460 | pos = add pos 1 |
| 461 | end |
| 462 | end |
| 463 | ret rc |
| 464 | end |