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/patches/30dbca5bd25e4386 # 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 ## C3 reductions and scans merged `30dbca5bd25e4386` #616 · opened 2026-10-22 04:52 UTC · closed 2026-10-25 00:35 UTC · merged 2026-10-25 00:35 UTC Imported from GitHub aien-dev/omega-mv#616, by drakestapleton Labels: ready-to-merge Base `697539a577ee7594ba12d905062f2e09cb9cc77c` · Head `002b4c08e151fbf79929b534f1040c9ad71ab660` Merged on GitHub as `0bb36a0ccc6ebd65fa836d2174e87471138e76a3`; reachable from main `c7acd2607139eedfabeac16b6042cefe8ede431f` (observed 2026-11-18T16:35:49Z) drakestapleton wrote on 2026-10-22 04:52 UTC Row C3 of `docs/OMEGA.md`, first half: sums, maximum, minimum and prefix sums (running totals) over every length, bit-exact against a frozen-order host reference. Kernels come from Omega's own code generator (`src/omega_gpu_scan.c`); no CUDA, no external library. What this adds - `src/omega_gpu_scan.c/.h`: the code generator for the five reductions and the prefix scan, the tree order written down in the header (the E1 reduction contract), and `src/omega_gpu_scan_ref.c`, the host reference whose order of additions is fixed. - `tests/gpu_scan_test.c`: host-only, simulator and chip modes, plus three planted faults (a skipped tree step, a skipped scan step, a carry from the wrong tile). Evidence on the branch (`evidence/C3-SCAN/README.md`): host-only PASS 133 checks; simulator PASS 139 checks over every length 1..70 and all tile/block/grid edges to 270,000; chip PASS 8,665 checks over 1..2^28, 2^k-1, 2^k, 2^k+1 and 64 random lengths, with all three faults caught. Re-run 2026-10-25 on the current train tip (the C6 layer's host tier, which includes this branch): 402 kernels collected and compared, 0 differ, red decoder fails all 402. Chip re-run this pass: PASS 8,665, exit 0. No timing claim: the reduction speed row needs an exclusive window and was not measured here. Generated by AI during the L386 lane. drakestapleton wrote on 2026-10-24 10:48 UTC Updated candidate to `8018d70cfd2af6e65e5657a993f950268f8d5ca7`: integrated current main baseline and regenerated canonical crumb metadata, preserving native/compiler code semantics, dependency pins, contributor attribution and historical evidence. Pinned crumb verification reports157 current. GitHub now reports conflict-free against this PR's base. Latest check readback: evidence-immutable PASS; AIEN Verification remains NOT_RUN because there is no signed Spark bundle for this exact head. CodeQL PASS on this exact head. This is not main merge acceptance or GPU/runtime qualification. Retain prior logs under their original SHA and qualify this candidate separately. Full audit: https://github.com/aien-dev/aien-architecture-mv/issues/190#issuecomment-`6383285588` ### Apply it ``` git checkout -b patch-30dbca5bd25e4386 697539a577ee7594ba12d905062f2e09cb9cc77c curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/30dbca5bd25e4386.patch | git am ``` Raw patch (mbox) · Diff ### Provenance This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record: ``` github-import 1 aien-dev/omega-mv 616 002b4c08e151fbf79929b534f1040c9ad71ab660 ``` ``` {"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":616,"url":"https://github.com/aien-dev/omega-mv/pull/616","export_sha256":"7319147b350ad908bf0b731068ec3be7b8dee848041dc862f71455e8e4680140","imported_utc":"2026-11-18T16:35:49Z"} ``` ### Changes ``` diff --git a/.crumb b/.crumb index bff65f4b0..c7d214e62 100644 --- a/.crumb +++ b/.crumb @@ -42,68 +42,68 @@ "children": [ { "digest": "9f19f11674ca5dfc3c9c3ae35fe6106557aee9583210da13d257e1b9d88e052c", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "calibration", "purpose": "Directory calibration. Purpose not yet described by a human." }, { "digest": "c92de7796549ecf47bb28700d4b82e8ba3e4dd260cdb4caa61a3337c44e063d3", - "last_commit": "843424ce", + "last_commit": "843424cea", "name": "docs", "purpose": "Directory docs. Purpose not yet described by a human." }, { "digest": "536f7b8db9a92a1f586bad4379d47544a30d1b8e83c2d799c18e66cf8660c9ae", - "last_commit": "30bb116c", + "last_commit": "30bb116cc", "name": "doctrine", "purpose": "Directory doctrine. Purpose not yet described by a human." }, { "digest": "28ca50efe4c6219607f80199b70e50807127d121c66550511606dede047a6200", - "last_commit": "f9602043", + "last_commit": "f96020434", "name": "mk", "purpose": "Directory mk. Purpose not yet described by a human." }, { "digest": "590f1f3a26a12dfd9df281ec944b882b85fd4ac2ae07a88a03bb8bd0d7d62536", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "research", "purpose": "Directory research. Purpose not yet described by a human." }, { "digest": "6327473d41e10acbf43be0918d3557509322bfd7d6e075e114c79c2600a47305", - "last_commit": "43d8d20d", + "last_commit": "43d8d20da", "name": "spec", "purpose": "Directory spec. Purpose not yet described by a human." }, { - "digest": "4718c14bd4052ff822a315892aebc3c2229900b338191d98c95945b109be4e91", - "last_commit": "1719bc69", + "digest": "ba3fa7cfe0a7e2df29583c0450bf5f7b5789349b5bbe735d1448a50295a87ec8", + "last_commit": "6e177ce13", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "8fa4874233c906929082e779408ae009b381ade4590b930a2faf68c60fd7e48f", - "last_commit": "79b2b632", + "digest": "87757530a28d4408bc4410fd8f922088f3e3f1cbc8987c5879e92ca59fe8728a", + "last_commit": "6e177ce13", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, { "digest": "e519c1f13dc5134c5b6ab17f669745776880cb6e78c6efd2f716283760466c66", - "last_commit": "1a12d5d0", + "last_commit": "1a12d5d0b", "name": "tools", "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:e53d60955af1d03b4ada9e70e1c969877d2ccf7d74cd47d38cda6a65b37858e3", - "compiled_at": "2026-10-24T23:32:51Z", + "children_root": "sha256:25bb98707e3701d95854342be7572172b8e9151d409fbcaea73ddc1690591607", + "compiled_at": "2026-10-24T23:54:33Z", "compiler": "crumb-compile/1", - "digest": "sha256:fd24682c44204b438ba61e45410f87473e10352ca7dc4755eb4a04175326bd75", - "evidence_files": 11614, - "evidence_root": "sha256:e8361cf08ea15248d8a6e60b7e9c801908c7bb76d44196e72b06ce852ddf5bd6", - "files": 14914, - "generated_at_commit": "79b2b6329d552350efc62eecce2b298afa53fc10", - "source_tree": "sha256:e7e0d299c68fcd99850bc4678c27fcb03e0f43e7dadf050166aa54b84b6cd466" + "digest": "sha256:2aca06720f7a98be3c84475dcbf35c466d4bae33d6e93a2e07c0abb9e8d21f1e", + "evidence_files": 11619, + "evidence_root": "sha256:2a8475f2e1c377d2a0715da7b2b9de849865303f80c1776f351177440492ec9e", + "files": 14923, + "generated_at_commit": "961bde0e1306142feaff6e77f2a150653c4dfd0a", + "source_tree": "sha256:98bef521239fcbbae90d313839be15e5882adfbce6cc99f3045fe0c863cb6e52" }, "owner_lane": "", "provenance": { diff --git a/Makefile b/Makefile index aa4e7428f..a80ea0062 100644 --- a/Makefile +++ b/Makefile @@ -44,6 +44,8 @@ SRCS += src/omega_blackwell_codegen_router.c src/omega_gpu_router_api.c SRCS += src/omega_blackwell_codegen_gdn.c src/omega_gpu_gdn_api.c # OWN-30B W-L2 (2026-10-13): decode launch collapse, new files only: command list (N launches, one submit) + glue kernels SRCS += src/omega_gpu_cmdlist.c src/omega_blackwell_codegen_dec.c +# C3 (docs/OMEGA.md): reductions and prefix sums of any length, bit-exact against the frozen-order host reference. +SRCS += src/omega_gpu_scan.c src/omega_gpu_scan_ref.c SRCS += src/omega_gpu_mem_cached.c SRCS += src/omega_gpu_decode_graph.c # sc #642 (2026-10-18): one Qwen3.8-27B decode token as one command list (GDN + hd256 attention layers, dense MLP) @@ -2687,3 +2689,12 @@ test-gpu-q38-graph-host: $(GPU_Q38_GRAPH_TEST) GPU_EXACT_HEAD_TEST = $(OUT_DIR)/gpu_exact_head_test $(GPU_EXACT_HEAD_TEST): tests/gpu_exact_head_test.c tests/bw_warp_sim_k.h src/omega_blackwell_codegen_dec.h src/omega_gpu_bf16_gemv_api.h src/omega_gpu_fp8_api.h $(OUT_DIR)/libomega_gpu.a $(CC) $(CFLAGS) -ffp-contract=off -Itests -o $@ tests/gpu_exact_head_test.c $(OUT_DIR)/libomega_gpu.a -lpthread -lm +# C3 (docs/OMEGA.md): sum, max, min and prefix sum of any length 1..2^28, bit-exact against the frozen order. +# test-gpu-scan: host refusals, codegen, references vs exact sums and vs the E1 reference, then the warp simulator +# (both orders, mutants caught). The chip tier is "$(GPU_SCAN_TEST)" with no flag, under the GB10 lock. +GPU_SCAN_TEST = $(OUT_DIR)/gpu_scan_test +$(GPU_SCAN_TEST): tests/gpu_scan_test.c tests/bw_warp_sim.h src/omega_gpu_scan.h src/omega_numeric_reduce.c src/omega_numeric.c $(OUT_DIR)/libomega_gpu.a + $(CC) $(CFLAGS) -ffp-contract=off -Itests -o $@ tests/gpu_scan_test.c src/omega_numeric_reduce.c src/omega_numeric.c $(OUT_DIR)/libomega_gpu.a -lpthread -lm +.PHONY: test-gpu-scan +test-gpu-scan: $(GPU_SCAN_TEST) + ./$(GPU_SCAN_TEST) --sim diff --git a/evidence/C3-SCAN/01-host-only.log b/evidence/C3-SCAN/01-host-only.log new file mode 100644 index 000000000..a48ef0ff5 --- /dev/null +++ b/evidence/C3-SCAN/01-host-only.log @@ -0,0 +1,6 @@ +kernel 0: 45 insns, 768 bytes, 18 gprs +kernel 1: 45 insns, 768 bytes, 18 gprs +kernel 2: 45 insns, 768 bytes, 18 gprs +kernel 3: 79 insns, 1280 bytes, 26 gprs +kernel 4: 33 insns, 640 bytes, 18 gprs +C3 scan host: PASS (133 checks) diff --git a/evidence/C3-SCAN/02-sim.log b/evidence/C3-SCAN/02-sim.log new file mode 100644 index 000000000..940bdee3e --- /dev/null +++ b/evidence/C3-SCAN/02-sim.log @@ -0,0 +1,8 @@ +kernel 0: 45 insns, 768 bytes, 18 gprs +kernel 1: 45 insns, 768 bytes, 18 gprs +kernel 2: 45 insns, 768 bytes, 18 gprs +kernel 3: 79 insns, 1280 bytes, 26 gprs +kernel 4: 33 insns, 640 bytes, 18 gprs +simulator order 0: 340 runs, reductions wrong 0, prefix sums wrong 0 +simulator order 1: 336 runs, reductions wrong 0, prefix sums wrong 0 +C3 scan sim: PASS (139 checks) diff --git a/evidence/C3-SCAN/03-chip-20261022-a7d6463b.log b/evidence/C3-SCAN/03-chip-20261022-a7d6463b.log new file mode 100644 index 000000000..4af451bae --- /dev/null +++ b/evidence/C3-SCAN/03-chip-20261022-a7d6463b.log @@ -0,0 +1,45 @@ +Thu Oct 22 07:03:33 UTC 2026 +a7d6463b9df4485484e207f11fb7802d09aed627 +c417f88728d2ee771d0af1369aa1a46791b48c11d4c8d685eb9eef393e082719 build/gpu_scan_test +COMMAND: spark-window run timeout 3600 stdbuf -oL -eL build/gpu_scan_test +every n from 1 to 2048 (sum, max, min, prefix): PASS +ok: chip sum n = 1048576 inputs 2: OK, got 7fffffff want 7fc0105f, 4 launches, kernels 0.530 ms +ok: chip max n = 1048576 inputs 2: OK, got 7f800000 want 7f800000, 4 launches, kernels 0.617 ms +ok: chip min n = 1048576 inputs 2: OK, got ff800000 want ff800000, 4 launches, kernels 0.621 ms +ok: chip prefix n = 1048576 inputs 2: OK, 0 wrong (first at 0), 7 launches, kernels 1.020 ms +ok: chip sum n = 2097152 inputs 2: OK, got 7fffffff want 7fc01b96, 5 launches, kernels 0.981 ms +ok: chip max n = 2097152 inputs 2: OK, got 7f800000 want 7f800000, 5 launches, kernels 0.823 ms +ok: chip min n = 2097152 inputs 2: OK, got ff800000 want ff800000, 5 launches, kernels 1.631 ms +ok: chip prefix n = 2097152 inputs 2: OK, 0 wrong (first at 0), 9 launches, kernels 1.417 ms +ok: chip sum n = 4194304 inputs 2: OK, got 7fffffff want 7fc03205, 5 launches, kernels 1.052 ms +ok: chip max n = 4194304 inputs 2: OK, got 7f800000 want 7f800000, 5 launches, kernels 2.124 ms +ok: chip min n = 4194304 inputs 2: OK, got ff800000 want ff800000, 5 launches, kernels 1.010 ms +ok: chip prefix n = 4194304 inputs 2: OK, 0 wrong (first at 0), 9 launches, kernels 3.284 ms +ok: chip sum n = 8388608 inputs 2: OK, got 7fffffff want 7fc05ee2, 5 launches, kernels 1.579 ms +ok: chip max n = 8388608 inputs 2: OK, got 7f800000 want 7f800000, 5 launches, kernels 1.412 ms +ok: chip min n = 8388608 inputs 2: OK, got ff800000 want ff800000, 5 launches, kernels 2.365 ms +ok: chip prefix n = 8388608 inputs 2: OK, 0 wrong (first at 0), 9 launches, kernels 4.076 ms +ok: chip sum n = 16777216 inputs 2: OK, got 7fffffff want 7fc0b89e, 5 launches, kernels 2.558 ms +ok: chip max n = 16777216 inputs 2: OK, got 7f800000 want 7f800000, 5 launches, kernels 2.362 ms +ok: chip min n = 16777216 inputs 2: OK, got ff800000 want ff800000, 5 launches, kernels 2.660 ms +ok: chip prefix n = 16777216 inputs 2: OK, 0 wrong (first at 0), 9 launches, kernels 3.808 ms +ok: chip sum n = 33554432 inputs 2: OK, got 7fffffff want 7fc06c14, 5 launches, kernels 5.531 ms +ok: chip max n = 33554432 inputs 2: OK, got 7f800000 want 7f800000, 5 launches, kernels 5.973 ms +ok: chip min n = 33554432 inputs 2: OK, got ff800000 want ff800000, 5 launches, kernels 5.058 ms +ok: chip prefix n = 33554432 inputs 2: OK, 0 wrong (first at 0), 9 launches, kernels 6.689 ms +ok: chip sum n = 67108864 inputs 2: OK, got 7fffffff want 7fc0d301, 6 launches, kernels 10.649 ms +ok: chip max n = 67108864 inputs 2: OK, got 7f800000 want 7f800000, 6 launches, kernels 9.341 ms +ok: chip min n = 67108864 inputs 2: OK, got ff800000 want ff800000, 6 launches, kernels 9.005 ms +ok: chip prefix n = 67108864 inputs 2: OK, 0 wrong (first at 0), 11 launches, kernels 13.186 ms +ok: chip sum n = 134217728 inputs 2: OK, got 7fffffff want 7fc0a0da, 6 launches, kernels 17.443 ms +ok: chip max n = 134217728 inputs 2: OK, got 7f800000 want 7f800000, 6 launches, kernels 17.061 ms +ok: chip min n = 134217728 inputs 2: OK, got ff800000 want ff800000, 6 launches, kernels 18.621 ms +ok: chip prefix n = 134217728 inputs 2: OK, 0 wrong (first at 0), 11 launches, kernels 24.440 ms +ok: chip sum n = 268435456 inputs 2: OK, got 7fffffff want 7fc03c8d, 6 launches, kernels 36.363 ms +ok: chip max n = 268435456 inputs 2: OK, got 7f800000 want 7f800000, 6 launches, kernels 35.326 ms +ok: chip min n = 268435456 inputs 2: OK, got ff800000 want ff800000, 6 launches, kernels 35.078 ms +ok: chip prefix n = 268435456 inputs 2: OK, 0 wrong (first at 0), 11 launches, kernels 46.719 ms +2^k - 1, 2^k, 2^k + 1 up to 2^28: PASS +64 random lengths: PASS +C3 scan chip: PASS (8665 checks) +EXIT_CODE=0 diff --git a/evidence/C3-SCAN/04-chip-20261024-f16b56a58.log b/evidence/C3-SCAN/04-chip-20261024-f16b56a58.log new file mode 100644 index 000000000..a3a0b7d00 --- /dev/null +++ b/evidence/C3-SCAN/04-chip-20261024-f16b56a58.log @@ -0,0 +1,52 @@ +Sat Oct 24 11:26:17 PM UTC 2026 +f16b56a58383e6d635b9bda97be0be207c67f2e3 +physics pin 6d7cf0d4d8eb2cda7b512100ff6058e25dbb3ddf (PHYSICS_DIR=/home/drakestapleton/workspace/l386-physics) +3400c91e5d91d1a17369a96c2b4642609518ae955beb579404ff20d6ebb2a9a0 build/gpu_scan_test +CAMERA /dev/nvidia0 holders before: ,873959,2935091,3489680 +CAMERA 18091: {"object":"list","data":[{"id":"qwen3.6-35b-a3b-fp8","object":"model","created": +WINDOW: 20261024T232617Z-b940cf +COMMAND: spark-window run ./build/gpu_scan_test +every n from 1 to 2048 (sum, max, min, prefix): PASS +ok: chip sum n = 1048576 inputs 2: OK, got 7fffffff want 7fc0105f, 4 launches, kernels 0.516 ms +ok: chip max n = 1048576 inputs 2: OK, got 7f800000 want 7f800000, 4 launches, kernels 0.497 ms +ok: chip min n = 1048576 inputs 2: OK, got ff800000 want ff800000, 4 launches, kernels 0.513 ms +ok: chip prefix n = 1048576 inputs 2: OK, 0 wrong (first at 0), 7 launches, kernels 1.019 ms +ok: chip sum n = 2097152 inputs 2: OK, got 7fffffff want 7fc01b96, 5 launches, kernels 0.826 ms +ok: chip max n = 2097152 inputs 2: OK, got 7f800000 want 7f800000, 5 launches, kernels 0.723 ms +ok: chip min n = 2097152 inputs 2: OK, got ff800000 want ff800000, 5 launches, kernels 0.826 ms +ok: chip prefix n = 2097152 inputs 2: OK, 0 wrong (first at 0), 9 launches, kernels 1.229 ms +ok: chip sum n = 4194304 inputs 2: OK, got 7fffffff want 7fc03205, 5 launches, kernels 1.034 ms +ok: chip max n = 4194304 inputs 2: OK, got 7f800000 want 7f800000, 5 launches, kernels 0.996 ms +ok: chip min n = 4194304 inputs 2: OK, got ff800000 want ff800000, 5 launches, kernels 1.029 ms +ok: chip prefix n = 4194304 inputs 2: OK, 0 wrong (first at 0), 9 launches, kernels 1.610 ms +ok: chip sum n = 8388608 inputs 2: OK, got 7fffffff want 7fc05ee2, 5 launches, kernels 1.439 ms +ok: chip max n = 8388608 inputs 2: OK, got 7f800000 want 7f800000, 5 launches, kernels 1.442 ms +ok: chip min n = 8388608 inputs 2: OK, got ff800000 want ff800000, 5 launches, kernels 1.439 ms +ok: chip prefix n = 8388608 inputs 2: OK, 0 wrong (first at 0), 9 launches, kernels 2.358 ms +ok: chip sum n = 16777216 inputs 2: OK, got 7fffffff want 7fc0b89e, 5 launches, kernels 2.252 ms +ok: chip max n = 16777216 inputs 2: OK, got 7f800000 want 7f800000, 5 launches, kernels 2.152 ms +ok: chip min n = 16777216 inputs 2: OK, got ff800000 want ff800000, 5 launches, kernels 2.252 ms +ok: chip prefix n = 16777216 inputs 2: OK, 0 wrong (first at 0), 9 launches, kernels 3.480 ms +ok: chip sum n = 33554432 inputs 2: OK, got 7fffffff want 7fc06c14, 5 launches, kernels 3.579 ms +ok: chip max n = 33554432 inputs 2: OK, got 7f800000 want 7f800000, 5 launches, kernels 3.475 ms +ok: chip min n = 33554432 inputs 2: OK, got ff800000 want ff800000, 5 launches, kernels 3.688 ms +ok: chip prefix n = 33554432 inputs 2: OK, 0 wrong (first at 0), 9 launches, kernels 5.833 ms +ok: chip sum n = 67108864 inputs 2: OK, got 7fffffff want 7fc0d301, 6 launches, kernels 6.533 ms +ok: chip max n = 67108864 inputs 2: OK, got 7f800000 want 7f800000, 6 launches, kernels 6.504 ms +ok: chip min n = 67108864 inputs 2: OK, got ff800000 want ff800000, 6 launches, kernels 6.442 ms +ok: chip prefix n = 67108864 inputs 2: OK, 0 wrong (first at 0), 11 launches, kernels 10.330 ms +ok: chip sum n = 134217728 inputs 2: OK, got 7fffffff want 7fc0a0da, 6 launches, kernels 12.133 ms +ok: chip max n = 134217728 inputs 2: OK, got 7f800000 want 7f800000, 6 launches, kernels 12.534 ms +ok: chip min n = 134217728 inputs 2: OK, got ff800000 want ff800000, 6 launches, kernels 12.128 ms +ok: chip prefix n = 134217728 inputs 2: OK, 0 wrong (first at 0), 11 launches, kernels 19.979 ms +ok: chip sum n = 268435456 inputs 2: OK, got 7fffffff want 7fc03c8d, 6 launches, kernels 23.837 ms +ok: chip max n = 268435456 inputs 2: OK, got 7f800000 want 7f800000, 6 launches, kernels 24.345 ms +ok: chip min n = 268435456 inputs 2: OK, got ff800000 want ff800000, 6 launches, kernels 24.327 ms +ok: chip prefix n = 268435456 inputs 2: OK, 0 wrong (first at 0), 11 launches, kernels 38.488 ms +2^k - 1, 2^k, 2^k + 1 up to 2^28: PASS +64 random lengths: PASS +C3 scan chip: PASS (8665 checks) +EXIT_CODE=0 + close.json + open.json +Sat Oct 24 11:26:56 PM UTC 2026 diff --git a/evidence/C3-SCAN/README.md b/evidence/C3-SCAN/README.md new file mode 100644 index 000000000..78bc955bf --- /dev/null +++ b/evidence/C3-SCAN/README.md @@ -0,0 +1,15 @@ +# C3 receipt, part 1: sums, maximum, minimum and running totals of any length + +Row C3 of `docs/OMEGA.md` asks for reductions and prefix sums (running totals) over every length from 1 to 2^28, +bit-exact against a host reference whose order of additions is frozen. The kernels come from Omega's own code +generator (`src/omega_gpu_scan.c`), with no CUDA. The order is written down in `src/omega_gpu_scan.h`; the reduction +order is the E1 contract (`docs/numeric/E1_REDUCTION_CONTRACT.md`). + +| Log | What it checks | Result | +|---|---|---| +| 01-host-only | bad calls refused; all five kernels (and each planted fault) go through our code generator and encoder; the host reference gives exact answers on integer inputs and equals the E1 reduction reference bit for bit on mixed, signed-zero, NaN and infinite inputs; the frozen tree differs from a left-to-right sum | PASS, 133 checks | +| 02-sim | the real kernels in the warp simulator, both scheduling orders, every length 1 to 70 and the tile, block and grid-stride edges up to 270,000; three planted faults (a skipped tree step, a skipped scan step, a carry from the wrong tile) each caught | PASS, 139 checks | +| 03-chip-20261022-a7d6463b | every n from 1 to 2048, 2^k - 1, 2^k, 2^k + 1 up to 2^28, 64 random lengths, the three faults caught on the GB10 | PASS (8,665 checks), run 2026-10-22 07:03Z on `c3/gemm` a7d6463b (an older head, before this branch was rebased onto main 0d72560a); the kernel generator, its order contract and the test (`src/omega_gpu_scan.c`, `src/omega_gpu_scan.h`, `tests/gpu_scan_test.c`) are byte-identical between that head and this tip; `src/omega_numeric.c` and `src/omega_numeric_reduce.c` differ only by the licence header main added; `tests/bw_warp_sim.h` gained main's warp visit-order knob (simulator only, no chip code); binary sha256 c417f887 28d2ee77...; log recovered unpushed from the dead worker's worktree `omega-c5`-era sibling `omega-c3-scan` (durable copy `~/handoffs/evidence-L386-chip-20261022/`). Re-run on the train tip: row 04. | +| 04-chip-20261024-f16b56a58 | the same chip battery (every n to 2048, 2^k - 1 / 2^k / 2^k + 1 to 2^28, 64 random lengths, three faults caught) on the GB10 | PASS (8,665 checks), 2026-10-24 23:26Z on the L386 train tip f16b56a58 (c3/gemm over this branch over main 0d72560a), binary sha256 3400c91e 5d91d1a1... (byte-identical to this branch's own build), 39 s wall, spark-window 20261024T232617Z-b940cf (gb10 shared, value check), chip shared with the live 35B server on 18091 and two other /dev/nvidia0 holders. | + +Command: `make test-gpu-scan` (host and simulator; `PHYSICS_DIR` must point at the physics checkout pinned in `physics.lock`). The chip run is `build/gpu_scan_test` with no flag, under the GB10 lock. 01 and 02 logs: 2026-10-24 23:10Z on this tip (c5cf3495b, binary sha256 3400c91e 5d91d1a1...), host 133 checks, simulator 139 checks. diff --git a/src/.crumb b/src/.crumb index 492b0dd03..5bd5f0ee5 100644 --- a/src/.crumb +++ b/src/.crumb @@ -62,140 +62,140 @@ "children": [ { "digest": "728ab6985dea994eb78a7eb2521384a911492d1c36819c17970a5dc3a8a0c5e2", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "algebra", "purpose": "Directory src/algebra. Purpose not yet described by a human." }, { "digest": "1881ed1d139a405a777d24d4b26f97bc8405dee0552d6ce21841ce4833560ce2", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "ans", "purpose": "ANS (Alignment Navigation System), Inertial Alignment v1: frozen reference atoms, drift filter o" }, { "digest": "92c3e2481f7457afd6c7717e6c1a75ebab950ec9114b48025dd137a6edb2975c", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "autodiff", "purpose": "M21 OMEGA_AUTODIFF: reverse-mode tape over M20 tensor CPU ops, caller-owned nodes and scratch, p" }, { "digest": "3bd51397fcd28cd54db65c1df72c320883798bc8550779b2b79c8498b28ffb0d", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "compiler", "purpose": "Directory src/compiler. Purpose not yet described by a human." }, { "digest": "36a66980895553ae73884798bc09a92141e1aeba5e5068a7d9d62a99bad02548", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "crumbline", "purpose": "Directory src/crumbline. Purpose not yet described by a human." }, { "digest": "c75df77ddb2bae35ac5e1225a0ae9dcef77b24608775c7b8d02dce1af0ec3ca4", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "dual_experiment", "purpose": "DUAL applicability experiment library (ARCH-0031 offline evidence lane): deterministic discrete " }, { "digest": "8856d5162c55cb33997201cd580638f80ea933b46e11c61e1913185d419731d7", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "estimation", "purpose": "Directory src/estimation. Purpose not yet described by a human." }, { "digest": "391e337c60cb8b2ae549595394482c23717c1757d83747a7446e81cd780750c1", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "fabric", "purpose": "Directory src/fabric. Purpose not yet described by a human." }, { "digest": "2d33eb528dafb5954bb027cdd9a7c7923594c177f17181194311155b4e1f40b3", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "language", "purpose": "Directory src/language. Purpose not yet described by a human." }, { "digest": "cd0551dbfb6fc354741efe9a3ad1808ffaaf6a8d7e9e7345851333d7a721cee0", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "osh", "purpose": "osh (Omega-native shell, arch#158) lexer unit: layout list, generator, osh_lex.osc, C reference " }, { "digest": "2b5dd2c64d4bbba8c41471dc1af5a0b00bfc1361a2c680d78b99fb30b5b547fe", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "path", "purpose": "Directory src/path. Purpose not yet described by a human." }, { "digest": "e0b91129cc9c188fc0e47b7f3189a3a9a09f9c55eecd018786794571ffc39aad", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "physics0", "purpose": "PD-0 hidden-equation benchmark (Physics-0 Discovery Engine cut 1): world, oracle, scorer, ladder" }, { "digest": "8b923fb5dc1de3b04afae043a4770f7b1fb27d959a8f597191ae2556d822a1cf", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "polyglot", "purpose": "Directory src/polyglot. Purpose not yet described by a human." }, { "digest": "076f486fdf51e12a26160e7f0bf7686fea611db54216f7fef81367620e7a6bdb", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "reservoir", "purpose": "Ionic-droplet reservoir simulator (omega#440): binary32 fixed-step RK4 of the authors' droplet m" }, { "digest": "c6af24b823d6a18a8ad1593003a214fcd577955d1d134bc4304a5c097a71cd35", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "runtime", "purpose": "Directory src/runtime. Purpose not yet described by a human." }, { "digest": "af5fcc64fe83dc35f782724372854febde5461f5eab8b327f3c3ed75f8572053", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "searchtrace", "purpose": "Directory src/searchtrace. Purpose not yet described by a human." }, { "digest": "b82288db7e732d10f954e921ac98c60e19579a3e58118cc12c49230587929ea2", - "last_commit": "ed8de573", + "last_commit": "ed8de5736", "name": "synapse", "purpose": "Synapse ADR 0003 J1 binary codec port: layout list, generator, syn_codec.osc (A1 WAL records + c" }, { "digest": "9723dd7c18d4e2c22db631680974e1025bccb9c309619424e2f4dfcc43cc7068", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "tensor", "purpose": "M20 OMEGA_TENSOR: immutable tensor semantic layer, views, generation-checked storage, CPU realiz" }, { "digest": "06d9fb18dddf7ab0f8ad02fffd15ce8b80808c77d0904609b6baae83c2b05ce5", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "train", "purpose": "Directory src/train. Purpose not yet described by a human." }, { "digest": "812a7faed07b21e066b732ed596eb0aea609379a2b2af0e8b61dd1ea05bef602", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "turing", "purpose": "Directory src/turing. Purpose not yet described by a human." }, { "digest": "b2e34b2ea0392059ac30d17e27289b1146e523db60a02ef825edef96036817ab", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "visor", "purpose": "Directory src/visor. Purpose not yet described by a human." } ], "children_root": "sha256:4460b4a6d5ed4054d462af660a6a02691350a35a8f44197e60ce605e243f698e", - "compiled_at": "2026-10-24T23:28:54Z", + "compiled_at": "2026-10-24T23:54:33Z", "compiler": "crumb-compile/1", - "digest": "sha256:4718c14bd4052ff822a315892aebc3c2229900b338191d98c95945b109be4e91", + "digest": "sha256:ba3fa7cfe0a7e2df29583c0450bf5f7b5789349b5bbe735d1448a50295a87ec8", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 558, - "generated_at_commit": "0d72560ad82c657b9b51b62119781b36484e2bdc", - "source_tree": "sha256:2c08e2cc72d20b23284775f67a56a7c759fe03ad159060f06ad765e7b76d1526" + "files": 561, + "generated_at_commit": "961bde0e1306142feaff6e77f2a150653c4dfd0a", + "source_tree": "sha256:286ee3dab8eced7c5c94e6383d494fd218b10140043352907a3eb7ab3f49e413" }, "owner_lane": "", "provenance": { diff --git a/src/omega_gpu_scan.c b/src/omega_gpu_scan.c new file mode 100644 index 000000000..0094072e3 --- /dev/null +++ b/src/omega_gpu_scan.c @@ -0,0 +1,332 @@ +/* omega_gpu_scan.c -- see omega_gpu_scan.h. Kernels from Omega's IR (emitter helpers of omega_bw_em.h, the + * chip-proven scheduling rules of the tensor-core generators), host references, the level driver, the chip calls. */ +#include "omega_gpu_scan.h" +#include "omega_bw_em.h" +#include "omega_gpu_session.h" + +#include +#include + +#define PAD_SUM 0x80000000u /* -0.0: x + -0.0 == x for every x */ +#define PAD_MINMAX 0x7fc00000u /* quiet NaN: max(NaN, x) == min(NaN, x) == x */ +#define POISON 0x7fa00badu /* a signalling NaN: no FADD or FMNMX writes it, so a word still holding it was never written */ +#define TIMEOUT_MS 20000ull + +/* ------------------------------------------------------------------ IR */ + +static void fmnmx(Em *e, BlackwellIROpcode op, int d, int a, int b) { + BlackwellIRInsn i = I0(op); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); +} +typedef struct { int bssy, bra; } Region; +/* the lanes with P0 set skip to the region's end; the warp joins again there */ +static Region skip_if_p0(Em *e) { Region r; r.bssy = bssy_fwd(e); r.bra = bra_if_p0_fwd(e); return r; } +static void region_end(Em *e, Region r) { int at = here(e); bsync(e); patch_bra(e, r.bra, at); patch_bra(e, r.bssy, at + 1); } + +static int gen(Em *e, const OmegaScanSpec *sp) { + const OmegaScanKind k = sp->kind; + const int red = k <= OMEGA_SCAN_K_RED_MIN; + int udesc = UV64(e), pin = V64(e), pout = V64(e), ptot = V64(e), len = V(e), tiles = V(e), warps = V(e); + int tid = V(e), cta = V(e), lane = V(e), gw = V(e), one = V(e), tile = V(e), el = V(e), v = V(e), t = V(e); + int a64 = V64(e), b64 = V64(e), rl = V(e), nz = V(e), c31 = V(e), m1 = V(e), off = V(e), tm1 = V(e); + int mask[5]; + for (unsigned s = 0; s < 5; s++) mask[s] = k == OMEGA_SCAN_K_TILE ? V(e) : NOREG; + if (e->err) return -1; + ldcu64(e, udesc, 0x358); + ldc64(e, pin, OMEGA_GPU_SCAN_A_IN); ldc64(e, pout, OMEGA_GPU_SCAN_A_OUT); + if (k == OMEGA_SCAN_K_TILE) ldc64(e, ptot, OMEGA_GPU_SCAN_A_TOT); + ldc32(e, len, OMEGA_GPU_SCAN_A_LEN); ldc32(e, tiles, OMEGA_GPU_SCAN_A_TILES); ldc32(e, warps, OMEGA_GPU_SCAN_A_WARPS); + s2r(e, tid, BW_SR_TID_X); s2r(e, cta, BW_SR_CTAID_X); + movi(e, one, 1); movi(e, m1, 0xffffffffu); movi(e, nz, PAD_SUM); + andi(e, lane, tid, 0, 31u); + shr(e, t, tid, 0, 5); + imadi(e, gw, cta, 0, OMEGA_GPU_SCAN_THREADS / 32u, t); /* global warp = cta * 8 + warp */ + if (k == OMEGA_SCAN_K_ADD) imadi(e, tile, gw, 0, 1u, one); /* the carry starts at tile 1 */ + else imadi(e, tile, gw, 0, 1u, NOREG); + if (k == OMEGA_SCAN_K_TILE) { + /* reversed lanes: lane L holds tile position 31 - L, so SHFL.DOWN by d reads position - d */ + movi(e, c31, 31u); + lop3(e, rl, lane, c31, NOREG, 0x3cu); /* 31 - L == L ^ 31 for L < 32 */ + /* mask[s] = all ones when position - d exists (L + d <= 31), else 0: ((L + d) >> 5) - 1 */ + for (unsigned s = 0; s < 5; s++) { + movi(e, t, 1u << s); + iadd(e, mask[s], lane, t); + shr(e, mask[s], mask[s], 0, 5); + imadi(e, mask[s], mask[s], 0, 1u, m1); + } + } + + const int top = here(e); + loop_begin(e); + isetp_ge_u32(e, tile, tiles); + const int done = bra_if_p0_fwd(e); + if (red) { + const BlackwellIROpcode op = k == OMEGA_SCAN_K_RED_MAX ? BW_IR_FMNMX_MAX : BW_IR_FMNMX_MIN; + imadi(e, el, tile, 0, 32u, lane); + movi(e, v, k == OMEGA_SCAN_K_RED_SUM ? PAD_SUM : PAD_MINMAX); + isetp_ge_u32(e, el, len); + { Region r = skip_if_p0(e); imadw(e, a64, el, 4u, pin); ldg32(e, v, a64, udesc, 1); region_end(e, r); } + for (uint32_t d = 16; d >= 1; d /= 2) { + if (sp->mutant == 1 && d == 1) break; + shfl_down(e, t, v, d); + if (k == OMEGA_SCAN_K_RED_SUM) fadd(e, v, v, t); else fmnmx(e, op, v, v, t); + } + isetp_ge_u32(e, lane, one); + { Region r = skip_if_p0(e); imadw(e, a64, tile, 4u, pout); stg(e, a64, 0, v, udesc); region_end(e, r); } + } else if (k == OMEGA_SCAN_K_TILE) { + imadi(e, el, tile, 0, 32u, rl); + movi(e, v, PAD_SUM); + isetp_ge_u32(e, el, len); + { Region r = skip_if_p0(e); imadw(e, a64, el, 4u, pin); ldg32(e, v, a64, udesc, 1); region_end(e, r); } + for (unsigned s = 0; s < 5; s++) { + if (sp->mutant == 2 && s == 4) break; + shfl_down(e, t, v, 1u << s); + lop3(e, t, t, mask[s], nz, 0xe2u); /* (t & mask) | (-0.0 & ~mask) */ + fadd(e, v, t, v); /* lower position on the left */ + } + isetp_ge_u32(e, el, len); + { Region r = skip_if_p0(e); imadw(e, a64, el, 4u, pout); stg(e, a64, 0, v, udesc); region_end(e, r); } + isetp_ge_u32(e, lane, one); /* lane 0 holds position 31: the tile total */ + { Region r = skip_if_p0(e); imadw(e, a64, tile, 4u, ptot); stg(e, a64, 0, v, udesc); region_end(e, r); } + } else { + imadi(e, el, tile, 0, 32u, lane); + isetp_ge_u32(e, el, len); + { + Region r = skip_if_p0(e); + if (sp->mutant == 3) imadi(e, tm1, tile, 0, 1u, NOREG); else imadi(e, tm1, tile, 0, 1u, m1); + imadw(e, b64, tm1, 4u, pout); imadw(e, a64, el, 4u, pin); + ldg32(e, off, b64, udesc, 1); ldg32(e, v, a64, udesc, 0); + fadd(e, v, off, v); + stg(e, a64, 0, v, udesc); + region_end(e, r); + } + } + iadd(e, tile, tile, warps); + bra(e, top); + loop_end(e); + patch_bra(e, done, here(e)); + tail(e); + return e->err; +} + +int omega_gpu_scan_codegen(const OmegaScanSpec *spec, BlackwellIRProgram *prog) { + if (!spec || !prog || (unsigned)spec->kind >= OMEGA_SCAN_K_COUNT) return -1; + omega_bw_ir_init(prog); + Em em; memset(&em, 0, sizeof em); em.p = prog; + if (gen(&em, spec) != 0 || em.err || em.depth != 0) return -1; + return omega_bw_regalloc_solve(prog); +} + +/* ------------------------------------------------------------------ the level driver */ + +uint32_t omega_gpu_scan_grid(size_t len) { + size_t tiles = (len + 31) / 32, ctas = (tiles + OMEGA_GPU_SCAN_THREADS / 32u - 1) / (OMEGA_GPU_SCAN_THREADS / 32u); + return ctas > OMEGA_GPU_SCAN_MAX_CTAS ? OMEGA_GPU_SCAN_MAX_CTAS : (uint32_t)(ctas ? ctas : 1); +} + +static void args_for(uint32_t a[OMEGA_GPU_SCAN_ARGS_WORDS], uint64_t in, uint64_t out, uint64_t tot, size_t len) { + memset(a, 0, OMEGA_GPU_SCAN_ARGS_WORDS * 4u); + a[0] = (uint32_t)in; a[1] = (uint32_t)(in >> 32); a[2] = (uint32_t)out; a[3] = (uint32_t)(out >> 32); + a[4] = (uint32_t)tot; a[5] = (uint32_t)(tot >> 32); + a[6] = (uint32_t)len; a[7] = (uint32_t)((len + 31) / 32); a[8] = omega_gpu_scan_grid(len) * (OMEGA_GPU_SCAN_THREADS / 32u); +} + +size_t omega_gpu_scan_reduce_scratch_words(size_t n) { + size_t w = 0; + do { n = (n + 31) / 32; w += n; } while (n > 1); + return w; +} + +size_t omega_gpu_scan_prefix_scratch_words(size_t n) { + size_t w = 0; + while (n > 32) { n = (n + 31) / 32; w += n; } + return w + 1; /* the last level's single total */ +} + +int omega_gpu_scan_drive_reduce(OmegaGpuRedOp op, size_t n, uint64_t in_va, uint64_t scratch_va, uint64_t *out_va, + OmegaScanLaunchFn launch, void *ctx, uint32_t *launches) { + if ((unsigned)op > OMEGA_GPU_RED_MIN || n == 0 || !launch || !out_va) return OMEGA_GPU_SCAN_BAD_ARGS; + if (n > OMEGA_GPU_SCAN_MAX_N) return OMEGA_GPU_SCAN_TOO_LARGE; + const OmegaScanKind k = (OmegaScanKind)(OMEGA_SCAN_K_RED_SUM + (int)op); + uint64_t in = in_va, out = scratch_va; + size_t len = n; + uint32_t a[OMEGA_GPU_SCAN_ARGS_WORDS]; + do { + args_for(a, in, out, 0, len); + if (launch(ctx, k, a, omega_gpu_scan_grid(len)) != 0) return OMEGA_GPU_SCAN_CHIP_FAIL; + if (launches) (*launches)++; + len = (len + 31) / 32; + in = out; out += len * 4u; + } while (len > 1); + *out_va = in; + return OMEGA_GPU_SCAN_OK; +} + +int omega_gpu_scan_drive_prefix(size_t n, uint64_t in_va, uint64_t out_va, uint64_t scratch_va, + OmegaScanLaunchFn launch, void *ctx, uint32_t *launches) { + if (n == 0 || !launch) return OMEGA_GPU_SCAN_BAD_ARGS; + if (n > OMEGA_GPU_SCAN_MAX_N) return OMEGA_GPU_SCAN_TOO_LARGE; + enum { MAXL = 8 }; /* 32^6 >= 2^28 */ + uint64_t buf[MAXL], tot[MAXL]; + size_t len[MAXL]; + unsigned L = 0; + uint64_t in = in_va, sp = scratch_va; + buf[0] = out_va; len[0] = n; + uint32_t a[OMEGA_GPU_SCAN_ARGS_WORDS]; + for (;;) { /* forward: scan every level, its totals become the next level (scanned in place) */ + size_t nt = (len[L] + 31) / 32; + tot[L] = sp; sp += nt * 4u; + args_for(a, in, buf[L], tot[L], len[L]); + if (launch(ctx, OMEGA_SCAN_K_TILE, a, omega_gpu_scan_grid(len[L])) != 0) return OMEGA_GPU_SCAN_CHIP_FAIL; + if (launches) (*launches)++; + if (nt == 1) break; + if (L + 1 >= MAXL) return OMEGA_GPU_SCAN_TOO_LARGE; + buf[L + 1] = tot[L]; len[L + 1] = nt; in = tot[L]; + L++; + } + while (L-- > 0) { /* back down: carry the scanned totals of level L + 1 into level L */ + args_for(a, buf[L], buf[L + 1], 0, len[L]); + if (launch(ctx, OMEGA_SCAN_K_ADD, a, omega_gpu_scan_grid(len[L])) != 0) return OMEGA_GPU_SCAN_CHIP_FAIL; + if (launches) (*launches)++; + } + return OMEGA_GPU_SCAN_OK; +} + +/* ------------------------------------------------------------------ the chip */ + +static struct { + int mutant; + OmegaGpuMem code[OMEGA_SCAN_K_COUNT]; + uint32_t gpr[OMEGA_SCAN_K_COUNT]; + int hooked; +} g; + +/* lock held; the session calls it right before it closes the device (also in omega_gpu_session_recover): the kernels go + * with the device, the next call builds them again on the new one */ +static void on_session_close(void) { + for (unsigned k = 0; k < OMEGA_SCAN_K_COUNT; k++) if (g.code[k].cpu) omega_gpu_session_free(&g.code[k]); + memset(g.code, 0, sizeof g.code); +} + +void omega_gpu_scan_test_set_mutant(int mutant) { + omega_gpu_session_lock(); + for (unsigned k = 0; k < OMEGA_SCAN_K_COUNT; k++) if (g.code[k].cpu) omega_gpu_session_free(&g.code[k]); + memset(g.code, 0, sizeof g.code); + g.mutant = mutant; + omega_gpu_session_unlock(); +} + +const char *omega_gpu_scan_rc_name(int rc) { + switch (rc) { + case OMEGA_GPU_SCAN_OK: return "OK"; + case OMEGA_GPU_SCAN_BAD_ARGS: return "BAD_ARGS"; + case OMEGA_GPU_SCAN_TOO_LARGE: return "TOO_LARGE"; + case OMEGA_GPU_SCAN_CODEGEN_FAIL: return "CODEGEN_FAIL"; + case OMEGA_GPU_SCAN_CHIP_FAIL: return "CHIP_FAIL"; + default: return "?"; + } +} + +/* lock held */ +static int kernel_locked(OmegaScanKind k) { + if (g.code[k].cpu) return OMEGA_GPU_SCAN_OK; + if (!g.hooked) { /* without the hook a kernel would outlive its device */ + if (omega_gpu_session_on_close(on_session_close) != 0) return OMEGA_GPU_SCAN_CHIP_FAIL; + g.hooked = 1; + } + BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog); + if (!prog) return OMEGA_GPU_SCAN_CODEGEN_FAIL; + OmegaScanSpec s = { k, g.mutant }; + int rc = OMEGA_GPU_SCAN_OK; + size_t need = 0; + if (omega_gpu_scan_codegen(&s, prog) != 0 || (need = omega_bw_program_code_bytes(prog)) == 0) rc = OMEGA_GPU_SCAN_CODEGEN_FAIL; + else if (omega_gpu_session_alloc_code(need, &g.code[k]) != 0) rc = OMEGA_GPU_SCAN_CHIP_FAIL; + else { + OmegaBlackwellKernel kk; + if (omega_blackwell_kernel_encode_into(prog, g.code[k].cpu, need, &kk) != 0 || kk.code_size != need) rc = OMEGA_GPU_SCAN_CODEGEN_FAIL; + else if (omega_gpu_session_sync_to_gpu(&g.code[k]) != 0) rc = OMEGA_GPU_SCAN_CHIP_FAIL; + else { g.gpr[k] = omega_bw_gpr_declared(kk.gpr_count); if (g.gpr[k] < 64u) g.gpr[k] = 64u; } + if (rc != OMEGA_GPU_SCAN_OK) { omega_gpu_session_free(&g.code[k]); memset(&g.code[k], 0, sizeof g.code[k]); } + } + omega_gpu_host_free(prog); + return rc; +} + +typedef struct { uint64_t elapsed_ns; } ChipCtx; + +/* lock held */ +static int chip_launch(void *ctx, OmegaScanKind k, const uint32_t args[OMEGA_GPU_SCAN_ARGS_WORDS], uint32_t grid_x) { + if (kernel_locked(k) != OMEGA_GPU_SCAN_OK) return -1; + OmegaGpuLaunch L = { + .code_va = g.code[k].va, .gpr_count = g.gpr[k], .threads_x = OMEGA_GPU_SCAN_THREADS, .threads_y = 1, + .grid_x = grid_x, .grid_y = 1, .num_elements = (size_t)grid_x * OMEGA_GPU_SCAN_THREADS, + .args = args, .n_args = OMEGA_GPU_SCAN_ARGS_WORDS, .timeout_ms = TIMEOUT_MS, + }; + uint64_t ns = 0; + if (omega_gpu_session_launch(&L, &ns, NULL) != 0) return -1; + ((ChipCtx *)ctx)->elapsed_ns += ns; + return 0; +} + +/* lock held: one data buffer [in n | out n | scratch], the input copied in, outputs poisoned */ +static int stage_locked(const float *x, size_t n, size_t out_words, size_t scratch_words, OmegaGpuMem *m) { + if (!omega_gpu_session_open()) return OMEGA_GPU_SCAN_CHIP_FAIL; + if (omega_gpu_session_alloc((n + out_words + scratch_words) * 4u, m) != 0) return OMEGA_GPU_SCAN_CHIP_FAIL; + uint32_t *w = m->cpu; + memcpy(w, x, n * 4u); + for (size_t i = n; i < n + out_words + scratch_words; i++) w[i] = POISON; + if (omega_gpu_session_sync_to_gpu(m) != 0) { omega_gpu_session_free(m); return OMEGA_GPU_SCAN_CHIP_FAIL; } + return OMEGA_GPU_SCAN_OK; +} + +int omega_gpu_reduce(OmegaGpuRedOp op, const float *x, size_t n, float *out, OmegaGpuScanInfo *info) { + uint64_t t0 = omega_gpu_session_now_ns(); + if (info) memset(info, 0, sizeof *info); + if ((unsigned)op > OMEGA_GPU_RED_MIN || !x || !out || n == 0) return OMEGA_GPU_SCAN_BAD_ARGS; + if (n > OMEGA_GPU_SCAN_MAX_N) return OMEGA_GPU_SCAN_TOO_LARGE; + omega_gpu_session_lock(); + OmegaGpuMem m; + const size_t sw = omega_gpu_scan_reduce_scratch_words(n); + int rc = stage_locked(x, n, 0, sw, &m); + if (rc == OMEGA_GPU_SCAN_OK) { + ChipCtx c = { 0 }; uint64_t res = 0; uint32_t nl = 0; + rc = omega_gpu_scan_drive_reduce(op, n, m.va, m.va + n * 4u, &res, chip_launch, &c, &nl); + if (rc == OMEGA_GPU_SCAN_OK && omega_gpu_session_sync_to_cpu(&m) != 0) rc = OMEGA_GPU_SCAN_CHIP_FAIL; + if (rc == OMEGA_GPU_SCAN_OK) { + uint32_t r; memcpy(&r, (const uint8_t *)m.cpu + (res - m.va), 4); + if (r == POISON) { omega_gpu_session_set_error("reduce: the result word was never written"); rc = OMEGA_GPU_SCAN_CHIP_FAIL; } + else memcpy(out, &r, 4); + } + if (info) { info->launches = nl; info->elapsed_ns = c.elapsed_ns; } + omega_gpu_session_free(&m); + } + omega_gpu_session_unlock(); + if (info) info->call_ns = omega_gpu_session_now_ns() - t0; + return rc; +} + +int omega_gpu_prefix_sum(const float *x, size_t n, float *y, OmegaGpuScanInfo *info) { + uint64_t t0 = omega_gpu_session_now_ns(); + if (info) memset(info, 0, sizeof *info); + if (!x || !y || n == 0) return OMEGA_GPU_SCAN_BAD_ARGS; + if (n > OMEGA_GPU_SCAN_MAX_N) return OMEGA_GPU_SCAN_TOO_LARGE; + omega_gpu_session_lock(); + OmegaGpuMem m; + int rc = stage_locked(x, n, n, omega_gpu_scan_prefix_scratch_words(n), &m); + if (rc == OMEGA_GPU_SCAN_OK) { + ChipCtx c = { 0 }; uint32_t nl = 0; + rc = omega_gpu_scan_drive_prefix(n, m.va, m.va + n * 4u, m.va + 2 * n * 4u, chip_launch, &c, &nl); + if (rc == OMEGA_GPU_SCAN_OK && omega_gpu_session_sync_to_cpu(&m) != 0) rc = OMEGA_GPU_SCAN_CHIP_FAIL; + if (rc == OMEGA_GPU_SCAN_OK) { + const uint32_t *w = (const uint32_t *)m.cpu + n; + size_t unwritten = 0; + for (size_t i = 0; i < n; i++) unwritten += w[i] == POISON; + if (unwritten) { omega_gpu_session_set_error("prefix sum: output words never written"); rc = OMEGA_GPU_SCAN_CHIP_FAIL; } + else memcpy(y, w, n * 4u); + } + if (info) { info->launches = nl; info->elapsed_ns = c.elapsed_ns; } + omega_gpu_session_free(&m); + } + omega_gpu_session_unlock(); + if (info) info->call_ns = omega_gpu_session_now_ns() - t0; + return rc; +} diff --git a/src/omega_gpu_scan.h b/src/omega_gpu_scan.h new file mode 100644 index 000000000..3c775cf6a --- /dev/null +++ b/src/omega_gpu_scan.h @@ -0,0 +1,110 @@ +/* omega_gpu_scan.h -- reductions and prefix sums of any length on the GB10 (row C3 of docs/OMEGA.md). + * + * The sum, the maximum and the minimum of n float values, and the inclusive prefix sum (the running total + * y[i] = x[0] + ... + x[i]), for every n from 1 to 2^28. The order of the additions is frozen: it depends only on n + * and the element index, never on the grid, the scheduling or the chip, so the chip result equals the host + * reference below bit for bit. + * + * Reductions (the order of docs/numeric/E1_REDUCTION_CONTRACT.md, omega_numeric_reduce.h): one level turns len values + * into ceil(len / 32) values; value j is the 32-lane tree over values [32j, 32j + 32) with lane i combining lane i + d + * for d = 16, 8, 4, 2, 1 (lower lane on the left), positions past len filled with the identity (-0.0 for the sum, a + * quiet NaN for the maximum and minimum, which those operations ignore). Levels repeat until one value is left; at + * least one level runs. + * + * Prefix sum, same tile structure: + * level scan inside each tile of 32 (positions past len filled with -0.0), for d = 1, 2, 4, 8, 16 in that order, + * every position i >= d takes v[i - d] + v[i] (both from before the step); the tile total T[j] is the + * value at position 31. + * carry when there is more than one tile, the totals T are prefix-summed by the same definition (recursively) + * into S, and every element of tile j >= 1 becomes S[j - 1] + (its tile value). + * Inputs are any float values. Results with no NaN are bit-exact; a NaN result is a NaN (the chip writes one canonical + * NaN, the host may keep another payload). + * + * On the chip each level is one launch of a kernel from Omega's own codegen (no CUDA): the reduction tree and the + * tile scan are warp shuffles (SHFL.DOWN; the scan holds each tile in reversed lane order so a shuffle down reads the + * lower element), the carry is a separate pass. omega_gpu_scan_drive_* run the levels through a launch callback, so + * the same driver runs on the chip (omega_gpu_reduce, omega_gpu_prefix_sum) and in the warp simulator + * (tests/gpu_scan_test.c). */ +#ifndef OMEGA_GPU_SCAN_H +#define OMEGA_GPU_SCAN_H + +#include +#include +#include "omega_blackwell_codegen.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define OMEGA_GPU_SCAN_MAX_N ((size_t)1 << 28) +#define OMEGA_GPU_SCAN_THREADS 256u /* 8 warps per CTA; each warp walks tiles gw, gw + warps, ... */ +#define OMEGA_GPU_SCAN_MAX_CTAS 1024u + +enum { + OMEGA_GPU_SCAN_OK = 0, + OMEGA_GPU_SCAN_BAD_ARGS = -1, /* NULL pointer, n == 0, unknown op */ + OMEGA_GPU_SCAN_TOO_LARGE = -2, /* n > OMEGA_GPU_SCAN_MAX_N */ + OMEGA_GPU_SCAN_CODEGEN_FAIL = -3, /* our codegen refused a kernel */ + OMEGA_GPU_SCAN_CHIP_FAIL = -4 /* device, allocation, launch, or an output word the chip never wrote */ +}; + +typedef enum { OMEGA_GPU_RED_SUM = 0, OMEGA_GPU_RED_MAX = 1, OMEGA_GPU_RED_MIN = 2 } OmegaGpuRedOp; + +/* The kernels. Argument words at constant bank 0x380 (OMEGA_GPU_SCAN_A_*). */ +typedef enum { + OMEGA_SCAN_K_RED_SUM = 0, OMEGA_SCAN_K_RED_MAX, OMEGA_SCAN_K_RED_MIN, /* one reduction level: in -> out[tile] */ + OMEGA_SCAN_K_TILE, /* one scan level: in -> out (may alias), tot[tile] */ + OMEGA_SCAN_K_ADD, /* carry: out[e] = S[tile - 1] + out[e], tiles >= 1 */ + OMEGA_SCAN_K_COUNT +} OmegaScanKind; +#define OMEGA_GPU_SCAN_A_IN 0x380u /* u64: level input (K_ADD: the level being carried into) */ +#define OMEGA_GPU_SCAN_A_OUT 0x388u /* u64: level output (K_ADD: S, the scanned totals) */ +#define OMEGA_GPU_SCAN_A_TOT 0x390u /* u64: tile totals (K_TILE only) */ +#define OMEGA_GPU_SCAN_A_LEN 0x398u /* u32: values in this level */ +#define OMEGA_GPU_SCAN_A_TILES 0x39cu /* u32: ceil(len / 32) */ +#define OMEGA_GPU_SCAN_A_WARPS 0x3a0u /* u32: warps in the grid (the tile stride) */ +#define OMEGA_GPU_SCAN_ARGS_WORDS 9u + +/* Mutants for the red checks (each must change some result): 1 the reduction tree skips d = 1, 2 the tile scan skips + * d = 16, 3 the carry adds S[tile] instead of S[tile - 1]. 0 = the real kernels. */ +typedef struct { OmegaScanKind kind; int mutant; } OmegaScanSpec; + +/* Builds the IR program of one kernel and allocates its registers; 0 or -1. */ +int omega_gpu_scan_codegen(const OmegaScanSpec *spec, BlackwellIRProgram *prog); + +/* Host references, the frozen order above. 0, or -1 for bad arguments or out of memory. */ +int omega_gpu_reduce_reference(OmegaGpuRedOp op, const float *x, size_t n, float *out); +int omega_gpu_prefix_sum_reference(const float *x, size_t n, float *y); + +/* The level driver. Device addresses are plain numbers (host pointers in the simulator). launch runs one kernel over + * grid_x CTAs of OMEGA_GPU_SCAN_THREADS threads with the given argument words and returns 0 when it completed. */ +typedef int (*OmegaScanLaunchFn)(void *ctx, OmegaScanKind kind, const uint32_t args[OMEGA_GPU_SCAN_ARGS_WORDS], uint32_t grid_x); +/* Scratch words a call needs beyond its input and output. */ +size_t omega_gpu_scan_reduce_scratch_words(size_t n); +size_t omega_gpu_scan_prefix_scratch_words(size_t n); +/* Reduction: the result lands in the scratch word the function returns through *out_va. */ +int omega_gpu_scan_drive_reduce(OmegaGpuRedOp op, size_t n, uint64_t in_va, uint64_t scratch_va, uint64_t *out_va, + OmegaScanLaunchFn launch, void *ctx, uint32_t *launches); +/* Prefix sum of in into out (out may equal in). */ +int omega_gpu_scan_drive_prefix(size_t n, uint64_t in_va, uint64_t out_va, uint64_t scratch_va, + OmegaScanLaunchFn launch, void *ctx, uint32_t *launches); +/* CTAs for a level of len values. */ +uint32_t omega_gpu_scan_grid(size_t len); + +typedef struct { + uint32_t launches; /* kernels run */ + uint64_t elapsed_ns; /* sum of launch-to-completion over the launches */ + uint64_t call_ns; /* the whole call, uploads and readback included */ +} OmegaGpuScanInfo; + +/* On the chip. x is read, out / y written; nothing else is kept between calls except the kernels. */ +int omega_gpu_reduce(OmegaGpuRedOp op, const float *x, size_t n, float *out, OmegaGpuScanInfo *info); +int omega_gpu_prefix_sum(const float *x, size_t n, float *y, OmegaGpuScanInfo *info); +/* Red checks: every later call builds its kernels with this mutant (clears the kernel cache). */ +void omega_gpu_scan_test_set_mutant(int mutant); +const char *omega_gpu_scan_rc_name(int rc); + +#ifdef __cplusplus +} +#endif +#endif /* OMEGA_GPU_SCAN_H */ diff --git a/src/omega_gpu_scan_ref.c b/src/omega_gpu_scan_ref.c new file mode 100644 index 000000000..ab5ee16a5 --- /dev/null +++ b/src/omega_gpu_scan_ref.c @@ -0,0 +1,75 @@ +/* omega_gpu_scan_ref.c -- the host references of omega_gpu_scan.h: the frozen order, in plain C on the CPU. + * Separate from the chip code so a CPU-only program can check them against the E1 reduction reference + * (tests/gpu_scan_ref_check.c). Additions only, so no fused multiply-add can change a bit. */ +#include "omega_gpu_scan.h" + +#include +#include + +#define PAD_SUM 0x80000000u /* -0.0 */ +#define PAD_MINMAX 0x7fc00000u /* quiet NaN */ + + +static float u2f(uint32_t u) { float f; memcpy(&f, &u, 4); return f; } + +static float combine(OmegaGpuRedOp op, float a, float b) { + if (op == OMEGA_GPU_RED_SUM) return a + b; + return op == OMEGA_GPU_RED_MAX ? __builtin_fmaxf(a, b) : __builtin_fminf(a, b); /* AArch64 FMAXNM / FMINNM: NaN ignored */ +} + +int omega_gpu_reduce_reference(OmegaGpuRedOp op, const float *x, size_t n, float *out) { + if ((unsigned)op > OMEGA_GPU_RED_MIN || !x || !out || n == 0) return -1; + const float pad = u2f(op == OMEGA_GPU_RED_SUM ? PAD_SUM : PAD_MINMAX); + float *cur = NULL; + size_t len = n; + const float *in = x; + do { + size_t nt = (len + 31) / 32; + float *next = malloc(nt * sizeof *next); + if (!next) { free(cur); return -1; } + for (size_t j = 0; j < nt; j++) { + float v[32]; + for (size_t i = 0; i < 32; i++) v[i] = j * 32 + i < len ? in[j * 32 + i] : pad; + for (size_t d = 16; d >= 1; d /= 2) + for (size_t i = 0; i + d < 32; i++) v[i] = combine(op, v[i], v[i + d]); /* ascending: v[i + d] is still old */ + next[j] = v[0]; + } + free(cur); + cur = next; in = next; len = nt; + } while (len > 1); + *out = cur[0]; + free(cur); + return 0; +} + +/* one scan level of len values: out (may alias in) and the tile totals */ +static void ref_scan_level(const float *in, float *out, float *tot, size_t len) { + for (size_t j = 0; j * 32 < len; j++) { + float v[32]; + for (size_t i = 0; i < 32; i++) v[i] = j * 32 + i < len ? in[j * 32 + i] : u2f(PAD_SUM); + for (size_t d = 1; d <= 16; d *= 2) + for (size_t i = 31; i >= d; i--) v[i] = v[i - d] + v[i]; /* descending: v[i - d] is still old */ + for (size_t i = 0; i < 32 && j * 32 + i < len; i++) out[j * 32 + i] = v[i]; + tot[j] = v[31]; + } +} + +static int ref_prefix(const float *in, float *out, size_t len) { + size_t nt = (len + 31) / 32; + float *tot = malloc(nt * sizeof *tot); + if (!tot) return -1; + ref_scan_level(in, out, tot, len); + int rc = 0; + if (nt > 1) { + rc = ref_prefix(tot, tot, nt); + for (size_t e = 32; rc == 0 && e < len; e++) out[e] = tot[e / 32 - 1] + out[e]; + } + free(tot); + return rc; +} + +int omega_gpu_prefix_sum_reference(const float *x, size_t n, float *y) { + if (!x || !y || n == 0) return -1; + return ref_prefix(x, y, n); +} + diff --git a/tests/.crumb b/tests/.crumb index c816853a1..a24ca6ab6 100644 --- a/tests/.crumb +++ b/tests/.crumb @@ -110,206 +110,206 @@ "children": [ { "digest": "2c1deaaa1bae6257789749edde641df61413250fe91ce6aa61a99dfb3b271306", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "algebra", "purpose": "Directory tests/algebra. Purpose not yet described by a human." }, { "digest": "63f086b68bb9ffc032cbd74931309ab34df3c8c341b57935e5db43d8bf475800", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "ans", "purpose": "Tests 1-10 for ANS (docs/ans/INERTIAL_ALIGNMENT_SPEC_V1.md): freeze/digest, hostile reference mu" }, { "digest": "45c5f4fd157769cf0dcc4b8acbd76b0953580bc328714bab8e1d65e9264a5664", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "brownian", "purpose": "Directory tests/brownian. Purpose not yet described by a human." }, { "digest": "1cd97575b2b10d9313ca0df4997391612056754778bb2851bf052c146144f15e", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "canonicalization", "purpose": "Directory tests/canonicalization. Purpose not yet described by a human." }, { "digest": "646c083a94914e5726634c33cc1e694349cfcd4f43817864ffec83e506a52d4e", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "compiler", "purpose": "Directory tests/compiler. Purpose not yet described by a human." }, { "digest": "9e372cfc9747befacdcf31bc8643356aaff861e81ee6640b4866170be67b5e6c", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "constraints", "purpose": "Directory tests/constraints. Purpose not yet described by a human." }, { "digest": "87a3efa5cd0aefa7821046f9a31594bfa0a109e95005a1e12f40c99a1bc6cd3d", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "crumbline", "purpose": "Directory tests/crumbline. Purpose not yet described by a human." }, { "digest": "10aa3424eded7768fee0bae0eedd062b9deac3279d2e5721439fd024493bc42b", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "dual_experiment", "purpose": "Tests for the DUAL applicability experiment: determinism (same seed, byte-identical results dige" }, { "digest": "758dde696afa847d9347fad76b0c4935e968f8af4551f0f9810ec2a37ad9847a", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "effect", "purpose": "Directory tests/effect. Purpose not yet described by a human." }, { "digest": "23e559d13ff664995baca31dc157a8cf5a860615f3ecb07a9a40fe4626b57ae3", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "estimation", "purpose": "Directory tests/estimation. Purpose not yet described by a human." }, { "digest": "b7b6391caf653c76707a21f9969126abcbc730d1243259219225fdb9bc0084e0", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "fabric", "purpose": "Directory tests/fabric. Purpose not yet described by a human." }, { "digest": "defc52a44ea0f65ba5de3468de786148ba58d292ffa217fb95629a28a569a977", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "identity", "purpose": "Directory tests/identity. Purpose not yet described by a human." }, { "digest": "6ba43d622b712a4215767cab29425016f29cce4b28eb9e4961fc67fd20e9d765", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "language", "purpose": "Directory tests/language. Purpose not yet described by a human." }, { "digest": "54f43a3416595ff5d4e64bd12afe013359e706651c39c78e2896a5799638d263", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "malformed", "purpose": "Directory tests/malformed. Purpose not yet described by a human." }, { "digest": "a03a4dd9a72f7b449342e562f6d9df1a3348fb03fb640b662956caf163dcd7e9", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "numeric_native", "purpose": "Simulated M16 driver for numeric resource-lifecycle tests. No device execution or numeric parity" }, { "digest": "d958af5413726ddc48a47721800d9766939129f480b26492332ba094577837cb", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "osh", "purpose": "osh lexer host driver, vendored aien-protocols PR#16 draft vectors (5ae5292)" }, { "digest": "12b31dc2cd4f6a6c27983db11b4140c597025c9124da5ddcbe6a9230ab4f8410", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "path", "purpose": "Directory tests/path. Purpose not yet described by a human." }, { "digest": "51f14936b27d3d21379cf05ea1b6b532cf3175a40844dac85f023a61e7a0912c", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "physics0", "purpose": "PD-0 gates: world self-test, oracle calibration, learner L0/L1/NULL, mutations, OSC differential" }, { "digest": "c8524317c751fa8f464cabc31b760cef262abba822f1843482a02e239daa81b3", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "polyglot", "purpose": "Directory tests/polyglot. Purpose not yet described by a human." }, { "digest": "6569e2cd6dbf6f792edb5d83bf0715df9dece42ca7bd9b3a291535de0492d3aa", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "program", "purpose": "Directory tests/program. Purpose not yet described by a human." }, { "digest": "c549b5b82fd026d664b35e4b1a5d7fd2aaafa13b60b541cfbba87d7522517b1d", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "r16_inventory", "purpose": "Directory tests/r16_inventory. Purpose not yet described by a human." }, { "digest": "855bd23e4c526fb49eeb430c4ea5967d3f2f9da3a5b3aa3ec9dae5b814b11a21", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "r16_negative", "purpose": "Directory tests/r16_negative. Purpose not yet described by a human." }, { "digest": "f21be04969cbf51de17118c4f81b103f712db56fd517a1f12dd0d3ac4ff3afe4", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "r16_surface", "purpose": "Directory tests/r16_surface. Purpose not yet described by a human." }, { "digest": "b0ba3448394d5f27958c38b7233ae389e5e437e327f0859749a774fb252d6b02", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "realize", "purpose": "Directory tests/realize. Purpose not yet described by a human." }, { "digest": "157018d66de5bfd149015654cf0bda1346a9cfcefe5fd552c1d54859cd9ece81", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "relations", "purpose": "Directory tests/relations. Purpose not yet described by a human." }, { "digest": "e5f68362c083510e34c41f65de3ba2d553eac8a52fbc3e4cc1f69314eac9cc32", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "reservoir", "purpose": "Reservoir tests (omega#440): Gate 1 simulator and readout checks against an independent binary64" }, { "digest": "39bb8b5bc5bb91af7db77cc15bcd326cdb083060f27077e9f60670cad9086a11", - "last_commit": "843424ce", + "last_commit": "843424cea", "name": "runtime", "purpose": "Directory tests/runtime. Purpose not yet described by a human." }, { "digest": "467652c09505ecbd82d96a5e0e7ea24b19d9216cfd1dad7c60fa8258e47594c0", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "searchtrace", "purpose": "Directory tests/searchtrace. Purpose not yet described by a human." }, { "digest": "e69616ad471704066a86bf2796776301179eada5c0aa36a7166b9aca788619f4", - "last_commit": "ed8de573", + "last_commit": "ed8de5736", "name": "synapse", "purpose": "J1 parity driver for syn_codec.osc against the sealed Rust A1 corpus (aien-dev/synapse j1-codec-" }, { "digest": "5c97916fbf20310eff2e502c526337cb093e912cb48f18d44b0345ed8a3c3240", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "train", "purpose": "Directory tests/train. Purpose not yet described by a human." }, { "digest": "7679bd1c49c7e89d8c26ccf8dfb91478f4e6e2f584745806fa8d26061dbdbb9c", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "turing", "purpose": "Directory tests/turing. Purpose not yet described by a human." }, { "digest": "003966bc697c6f85cfffbae00fae992a634bc386f4a44340daaa8ddcb5618ef6", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "visor", "purpose": "Directory tests/visor. Purpose not yet described by a human." } ], "children_root": "sha256:21fc25ed991f4066326a9270e13961ed8a45edc12064ce9e7846ab6782d5b72b", - "compiled_at": "2026-10-24T23:32:51Z", + "compiled_at": "2026-10-24T23:54:33Z", "compiler": "crumb-compile/1", - "digest": "sha256:8fa4874233c906929082e779408ae009b381ade4590b930a2faf68c60fd7e48f", + "digest": "sha256:87757530a28d4408bc4410fd8f922088f3e3f1cbc8987c5879e92ca59fe8728a", "evidence_files": 16, "evidence_root": "sha256:96d08097822ad95f4e562c19e0ba5df5a035774c48632f1832912c40a27bd27a", - "files": 1287, - "generated_at_commit": "79b2b6329d552350efc62eecce2b298afa53fc10", - "source_tree": "sha256:66aa30eb539fb8432bd7571a91659a1ae9a174746c98fb2eeda085a373778a65" + "files": 1288, + "generated_at_commit": "961bde0e1306142feaff6e77f2a150653c4dfd0a", + "source_tree": "sha256:cbf44d3eac6439851672ffdcf5046d84b972b1dbf2e29ca2fbfd420cbca74b83" }, "owner_lane": "", "provenance": { diff --git a/tests/gpu_scan_test.c b/tests/gpu_scan_test.c new file mode 100644 index 000000000..1e1b608fc --- /dev/null +++ b/tests/gpu_scan_test.c @@ -0,0 +1,379 @@ +/* gpu_scan_test.c -- row C3 of docs/OMEGA.md: reductions and prefix sums of any length, bit-exact against the + * frozen-order host reference (src/omega_gpu_scan.h). + * + * --host-only no device: refusals, every kernel through our codegen and encoder, the references checked against + * exact integer sums and against the E1 reduction reference (omega_reduce_reference) + * --sim the real kernels through the warp simulator (tests/bw_warp_sim.h), both scheduling orders, lengths + * that cross every tile, CTA and grid-stride edge; the three mutants must be caught + * (no flag) the GB10: every n from 1 to 2048, then 2^k - 1, 2^k, 2^k + 1 up to 2^28, then random lengths; + * bit-exact (a NaN result must be a NaN); the three mutants must be caught. + * --max-log2 K stops the power-of-two ladder at 2^K (default 28). + * Last line: "C3 scan : PASS ( checks)" or "... FAIL ( of )". */ +#include "omega_gpu_scan.h" +#include "omega_gpu_session.h" +#include "omega_numeric_reduce.h" +#include "bw_warp_sim.h" + +#include +#include +#include +#include + +static int g_checks, g_failed; +static void check(int ok, const char *what) { + g_checks++; + if (!ok) { g_failed++; printf("FAIL: %s\n", what); } +} + +static uint32_t f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } +static float u2f(uint32_t u) { float f; memcpy(&f, &u, 4); return f; } +static int is_nan(uint32_t u) { return (u & 0x7f800000u) == 0x7f800000u && (u & 0x007fffffu); } +static int same(float a, float b) { uint32_t x = f2u(a), y = f2u(b); return x == y || (is_nan(x) && is_nan(y)); } + +static uint64_t g_rng = 0x9e3779b97f4a7c15ull; +static uint32_t rnd(void) { g_rng = g_rng * 6364136223846793005ull + 1442695040888963407ull; return (uint32_t)(g_rng >> 33); } + +/* inputs: 0 small integers (every sum exact), 1 mixed magnitudes and signed zeros, 2 mixed plus NaN and infinities */ +static void fill(float *x, size_t n, int kind, uint64_t seed) { + g_rng = seed * 0x2545f4914f6cdd1dull + 1; + for (size_t i = 0; i < n; i++) { + if (kind == 0) { x[i] = (float)((int)(rnd() % 2001u) - 1000); continue; } + float m = (float)((rnd() & 0x7fffffu) | 1u) * (rnd() & 1 ? -1.0f : 1.0f); + int e = (int)(rnd() % 41u) - 20 - 23; + x[i] = m * u2f((uint32_t)(127 + e) << 23); + if (i % 97u == 5u) x[i] = 0.0f; + if (i % 131u == 7u) x[i] = -0.0f; + if (kind == 2 && i % 53u == 11u) x[i] = u2f(0x7fc00000u | (rnd() & 0xffffu)); + if (kind == 2 && i == n / 3) x[i] = u2f(0x7f800000u); + if (kind == 2 && i == n / 2 && n > 4) x[i] = u2f(0xff800000u); + } +} + +static const char *op_name(OmegaGpuRedOp op) { return op == OMEGA_GPU_RED_SUM ? "sum" : op == OMEGA_GPU_RED_MAX ? "max" : "min"; } + +/* ------------------------------------------------------------------ host only */ + +static int fake_fail(void *ctx, OmegaScanKind k, const uint32_t a[OMEGA_GPU_SCAN_ARGS_WORDS], uint32_t g) { + (void)ctx; (void)k; (void)a; (void)g; return -1; +} + +static void host_only(void) { + char w[200]; + float x[4] = { 1, 2, 3, 4 }, o; + check(omega_gpu_reduce(OMEGA_GPU_RED_SUM, x, 0, &o, NULL) == OMEGA_GPU_SCAN_BAD_ARGS, "reduce n = 0 refused"); + check(omega_gpu_reduce(OMEGA_GPU_RED_SUM, NULL, 4, &o, NULL) == OMEGA_GPU_SCAN_BAD_ARGS, "reduce NULL input refused"); + check(omega_gpu_reduce((OmegaGpuRedOp)7, x, 4, &o, NULL) == OMEGA_GPU_SCAN_BAD_ARGS, "reduce unknown op refused"); + check(omega_gpu_reduce(OMEGA_GPU_RED_SUM, x, OMEGA_GPU_SCAN_MAX_N + 1, &o, NULL) == OMEGA_GPU_SCAN_TOO_LARGE, "reduce 2^28 + 1 refused"); + check(omega_gpu_prefix_sum(x, 0, x, NULL) == OMEGA_GPU_SCAN_BAD_ARGS, "prefix n = 0 refused"); + check(omega_gpu_prefix_sum(x, OMEGA_GPU_SCAN_MAX_N + 1, x, NULL) == OMEGA_GPU_SCAN_TOO_LARGE, "prefix 2^28 + 1 refused"); + uint64_t r; + check(omega_gpu_scan_drive_reduce(OMEGA_GPU_RED_SUM, 100, 0, 0, &r, fake_fail, NULL, NULL) == OMEGA_GPU_SCAN_CHIP_FAIL, "a failed launch is reported (reduce)"); + check(omega_gpu_scan_drive_prefix(100, 0, 0, 0, fake_fail, NULL, NULL) == OMEGA_GPU_SCAN_CHIP_FAIL, "a failed launch is reported (prefix)"); + + for (int mut = 0; mut <= 3; mut++) + for (int k = 0; k < OMEGA_SCAN_K_COUNT; k++) { + BlackwellIRProgram *p = calloc(1, sizeof *p); + OmegaScanSpec s = { (OmegaScanKind)k, mut }; + int rc = omega_gpu_scan_codegen(&s, p); + size_t bytes = rc == 0 ? omega_bw_program_code_bytes(p) : 0; + uint8_t *code = bytes ? malloc(bytes) : NULL; + OmegaBlackwellKernel kk; memset(&kk, 0, sizeof kk); + int enc = code ? omega_blackwell_kernel_encode_into(p, code, bytes, &kk) : -1; + snprintf(w, sizeof w, "kernel %d mutant %d through codegen and encoder (%zu bytes, %u gprs)", k, mut, bytes, (unsigned)kk.gpr_count); + check(rc == 0 && enc == 0 && kk.code_size == bytes, w); + if (mut == 0 && rc == 0) printf("kernel %d: %u insns, %zu bytes, %u gprs\n", k, (unsigned)p->count, bytes, (unsigned)kk.gpr_count); + free(code); free(p); + } + + /* the references against exact sums: integers in [-1000, 1000], n <= 16384, every partial sum below 2^24 */ + static float a[16384], y[16384]; + const size_t ns[] = { 1, 2, 31, 32, 33, 63, 64, 65, 1023, 1024, 1025, 1057, 16384 }; + for (size_t t = 0; t < sizeof ns / sizeof ns[0]; t++) { + size_t n = ns[t]; + fill(a, n, 0, n); + double s = 0, mx = -1e9, mn = 1e9; + for (size_t i = 0; i < n; i++) { s += a[i]; if (a[i] > mx) mx = a[i]; if (a[i] < mn) mn = a[i]; } + float rs, rmx, rmn; + int ok = omega_gpu_reduce_reference(OMEGA_GPU_RED_SUM, a, n, &rs) == 0 && omega_gpu_reduce_reference(OMEGA_GPU_RED_MAX, a, n, &rmx) == 0 && + omega_gpu_reduce_reference(OMEGA_GPU_RED_MIN, a, n, &rmn) == 0; + snprintf(w, sizeof w, "reference sum/max/min exact at n = %zu", n); + check(ok && rs == (float)s && rmx == (float)mx && rmn == (float)mn, w); + ok = omega_gpu_prefix_sum_reference(a, n, y) == 0; + double run = 0; size_t bad = 0; + for (size_t i = 0; i < n; i++) { run += a[i]; bad += y[i] != (float)run; } + snprintf(w, sizeof w, "reference prefix sum exact at n = %zu (%zu wrong)", n, bad); + check(ok && bad == 0, w); + } + + /* the reduction reference is the E1 order: equal bit for bit to omega_reduce_reference on any input */ + static float b[300000]; + const size_t ms[] = { 1, 2, 17, 32, 33, 100, 1000, 1024, 1025, 4097, 32768, 32769, 299999 }; + for (int kind = 1; kind <= 2; kind++) + for (size_t t = 0; t < sizeof ms / sizeof ms[0]; t++) + for (int op = 0; op < 3; op++) { + size_t n = ms[t]; + fill(b, n, kind, n * 3 + (size_t)op); + float mine, e1; + int ok = omega_gpu_reduce_reference((OmegaGpuRedOp)op, b, n, &mine) == 0 && omega_reduce_reference((OmegaReduceOp)op, b, n, &e1) == 0; + snprintf(w, sizeof w, "reference %s equals the E1 reference, inputs %d, n = %zu (%08x vs %08x)", op_name(op), kind, n, f2u(mine), f2u(e1)); + check(ok && same(mine, e1), w); + } + + /* the frozen order is a real order: summing left to right gives a different answer on mixed inputs */ + fill(b, 4097, 1, 99); + float tree, seq = -0.0f; + for (size_t i = 0; i < 4097; i++) seq += b[i]; + omega_gpu_reduce_reference(OMEGA_GPU_RED_SUM, b, 4097, &tree); + check(f2u(tree) != f2u(seq), "the frozen tree differs from a left-to-right sum (the order is pinned, not luck)"); +} + +/* ------------------------------------------------------------------ the simulator */ + +typedef struct { + BlackwellIRProgram *prog[OMEGA_SCAN_K_COUNT]; + int order; + WsRange ranges[3]; + char err[260]; +} SimCtx; + +static int sim_launch(void *ctx, OmegaScanKind k, const uint32_t a[OMEGA_GPU_SCAN_ARGS_WORDS], uint32_t grid_x) { + SimCtx *c = ctx; + static WsSim s; + memset(&s, 0, sizeof s); + s.p = c->prog[k]; s.threads = OMEGA_GPU_SCAN_THREADS; s.order = c->order; + for (unsigned i = 0; i < OMEGA_GPU_SCAN_ARGS_WORDS; i++) s.cbank[0x380 / 4 + i] = a[i]; + memcpy(s.ranges, c->ranges, sizeof c->ranges); s.nranges = 3; + if (ws_run(&s, grid_x, 1) != 0) { snprintf(c->err, sizeof c->err, "kernel %d: %s at pc %d", (int)k, s.msg, s.err_pc); return -1; } + return 0; +} + +static int sim_build(SimCtx *c, int mutant) { + for (int k = 0; k < OMEGA_SCAN_K_COUNT; k++) { + c->prog[k] = calloc(1, sizeof *c->prog[k]); + OmegaScanSpec s = { (OmegaScanKind)k, mutant }; + if (!c->prog[k] || omega_gpu_scan_codegen(&s, c->prog[k]) != 0) return -1; + } + return 0; +} +static void sim_free(SimCtx *c) { for (int k = 0; k < OMEGA_SCAN_K_COUNT; k++) free(c->prog[k]); } + +#define POISON 0x7fa00badu +static void poison(float *p, size_t n) { for (size_t i = 0; i < n; i++) memcpy(&p[i], &(uint32_t){ POISON }, 4); } + +/* one reduction through the simulator; 0 when it ran, the result in *out */ +static int sim_reduce(SimCtx *c, OmegaGpuRedOp op, const float *x, size_t n, float *out) { + size_t sw = omega_gpu_scan_reduce_scratch_words(n); + float *scr = malloc(sw * 4u); + poison(scr, sw); + c->ranges[0] = (WsRange){ (uintptr_t)x, (uintptr_t)(x + n) }; + c->ranges[1] = (WsRange){ (uintptr_t)scr, (uintptr_t)(scr + sw) }; + c->ranges[2] = c->ranges[1]; + uint64_t res = 0; + int rc = omega_gpu_scan_drive_reduce(op, n, (uintptr_t)x, (uintptr_t)scr, &res, sim_launch, c, NULL); + if (rc == 0) { memcpy(out, (const void *)(uintptr_t)res, 4); if (f2u(*out) == POISON) rc = -9; } + free(scr); + return rc; +} + +static int sim_prefix(SimCtx *c, const float *x, size_t n, float *y) { + size_t sw = omega_gpu_scan_prefix_scratch_words(n); + float *scr = malloc(sw * 4u); + poison(scr, sw); poison(y, n); + c->ranges[0] = (WsRange){ (uintptr_t)x, (uintptr_t)(x + n) }; + c->ranges[1] = (WsRange){ (uintptr_t)y, (uintptr_t)(y + n) }; + c->ranges[2] = (WsRange){ (uintptr_t)scr, (uintptr_t)(scr + sw) }; + int rc = omega_gpu_scan_drive_prefix(n, (uintptr_t)x, (uintptr_t)y, (uintptr_t)scr, sim_launch, c, NULL); + for (size_t i = 0; rc == 0 && i < n; i++) if (f2u(y[i]) == POISON) rc = -9; + free(scr); + return rc; +} + +static size_t diff(const float *a, const float *b, size_t n, size_t *first) { + size_t d = 0; + for (size_t i = 0; i < n; i++) if (!same(a[i], b[i])) { if (!d) *first = i; d++; } + return d; +} + +static void sim_tier(void) { + char w[600]; + static size_t ns[200]; size_t nn = 0; + for (size_t n = 1; n <= 70; n++) ns[nn++] = n; + const size_t more[] = { 255, 256, 257, 1000, 1023, 1024, 1025, 2047, 2048, 2049, 32767, 32768, 32769, 40000, 270000 }; + for (size_t i = 0; i < sizeof more / sizeof more[0]; i++) ns[nn++] = more[i]; + float *x = malloc(270000 * 4u), *y = malloc(270000 * 4u), *ref = malloc(270000 * 4u); + SimCtx c; memset(&c, 0, sizeof c); + check(sim_build(&c, 0) == 0, "simulator: all kernels built"); + for (int order = 0; order <= 1; order++) { + c.order = order; + size_t bad_red = 0, bad_pre = 0, runs = 0; + for (size_t t = 0; t < nn; t++) { + size_t n = ns[t]; + if (order == 1 && n > 40000) continue; + int kind = (int)(t % 3); + fill(x, n, kind, n * 7 + (size_t)order); + for (int op = 0; op < 3; op++) { + float got, want; + omega_gpu_reduce_reference((OmegaGpuRedOp)op, x, n, &want); + int rc = sim_reduce(&c, (OmegaGpuRedOp)op, x, n, &got); + runs++; + if (rc != 0 || !same(got, want)) { + bad_red++; + snprintf(w, sizeof w, "sim order %d %s n = %zu: rc %d got %08x want %08x %s", order, op_name(op), n, rc, f2u(got), f2u(want), c.err); + check(0, w); + } + } + omega_gpu_prefix_sum_reference(x, n, ref); + int rc = sim_prefix(&c, x, n, y); + size_t first = 0, d = rc == 0 ? diff(y, ref, n, &first) : n; + runs++; + if (rc != 0 || d) { + bad_pre++; + snprintf(w, sizeof w, "sim order %d prefix n = %zu: rc %d, %zu wrong, first at %zu (%08x vs %08x) %s", order, n, rc, d, first, + f2u(y[first]), f2u(ref[first]), c.err); + check(0, w); + } + } + snprintf(w, sizeof w, "simulator order %d: %zu runs, reductions wrong %zu, prefix sums wrong %zu", order, runs, bad_red, bad_pre); + check(bad_red == 0 && bad_pre == 0, w); + printf("%s\n", w); + } + sim_free(&c); + + /* red checks: each mutant must change a result the real kernels get right */ + for (int mut = 1; mut <= 3; mut++) { + SimCtx m; memset(&m, 0, sizeof m); + int built = sim_build(&m, mut) == 0, caught = 0; + const size_t n = 1000; + fill(x, n, 1, 4242); + if (built && mut == 1) { + for (int op = 0; op < 3 && !caught; op++) { + float got = 0, want; omega_gpu_reduce_reference((OmegaGpuRedOp)op, x, n, &want); + caught = sim_reduce(&m, (OmegaGpuRedOp)op, x, n, &got) != 0 || !same(got, want); + } + } else if (built) { + omega_gpu_prefix_sum_reference(x, n, ref); + size_t first; + caught = sim_prefix(&m, x, n, y) != 0 || diff(y, ref, n, &first) != 0; + } + snprintf(w, sizeof w, "simulator red check: mutant %d is caught", mut); + check(built && caught, w); + sim_free(&m); + } + free(x); free(y); free(ref); +} + +/* ------------------------------------------------------------------ the chip */ + +static size_t mem_available(void) { + FILE *f = fopen("/proc/meminfo", "r"); + char line[256]; size_t kb = 0; + while (f && fgets(line, sizeof line, f)) if (sscanf(line, "MemAvailable: %zu kB", &kb) == 1) break; + if (f) fclose(f); + return kb * 1024u; +} + +static float *g_x, *g_y, *g_ref; + +static void chip_one(size_t n, int kind, int verbose) { + char w[600]; + fill(g_x, n, kind, n * 11 + (size_t)kind); + for (int op = 0; op < 3; op++) { + float got = 0, want; OmegaGpuScanInfo info; + omega_gpu_reduce_reference((OmegaGpuRedOp)op, g_x, n, &want); + int rc = omega_gpu_reduce((OmegaGpuRedOp)op, g_x, n, &got, &info); + int ok = rc == 0 && same(got, want); + if (!ok || verbose) { + snprintf(w, sizeof w, "chip %s n = %zu inputs %d: %s, got %08x want %08x, %u launches, kernels %.3f ms%s%s", op_name(op), n, kind, + omega_gpu_scan_rc_name(rc), f2u(got), f2u(want), info.launches, info.elapsed_ns / 1e6, rc ? ": " : "", + rc ? omega_gpu_session_last_error() : ""); + if (ok) { g_checks++; printf("ok: %s\n", w); } else check(0, w); + } else g_checks++; + } + OmegaGpuScanInfo info; + omega_gpu_prefix_sum_reference(g_x, n, g_ref); + int rc = omega_gpu_prefix_sum(g_x, n, g_y, &info); + size_t first = 0, d = rc == 0 ? diff(g_y, g_ref, n, &first) : n; + if (rc || d || verbose) { + snprintf(w, sizeof w, "chip prefix n = %zu inputs %d: %s, %zu wrong (first at %zu), %u launches, kernels %.3f ms%s%s", n, kind, + omega_gpu_scan_rc_name(rc), d, first, info.launches, info.elapsed_ns / 1e6, rc ? ": " : "", rc ? omega_gpu_session_last_error() : ""); + if (!rc && !d) { g_checks++; printf("ok: %s\n", w); } else check(0, w); + } else g_checks++; +} + +static void chip_tier(unsigned max_log2) { + char w[200]; + size_t cap = (size_t)1 << max_log2; + if (cap > OMEGA_GPU_SCAN_MAX_N) cap = OMEGA_GPU_SCAN_MAX_N; + /* host: input, output, reference (3 x 4n); device buffer: in + out + scratch (~ 8n); plus 8 GiB for everyone else */ + size_t need = cap * 4u * 5u + ((size_t)8 << 30), have = mem_available(); + if (have < need) { + printf("NOT_RUN: MemAvailable %zu MiB < %zu MiB needed for n up to %zu\n", have >> 20, need >> 20, cap); + exit(2); + } + g_x = malloc(cap * 4u); g_y = malloc(cap * 4u); g_ref = malloc(cap * 4u); + if (!g_x || !g_y || !g_ref) { printf("NOT_RUN: host allocation\n"); exit(2); } + + int before = g_failed; + for (size_t n = 1; n <= 2048; n++) chip_one(n, (int)(n % 3), 0); + printf("every n from 1 to 2048 (sum, max, min, prefix): %s\n", g_failed == before ? "PASS" : "FAIL"); + before = g_failed; + for (unsigned k = 11; k <= max_log2 && k <= 28; k++) { + size_t p = (size_t)1 << k; + chip_one(p - 1, 1, 0); + chip_one(p, 2, k >= 20); + if (p + 1 <= cap) chip_one(p + 1, 1, 0); + } + printf("2^k - 1, 2^k, 2^k + 1 up to 2^%u: %s\n", max_log2, g_failed == before ? "PASS" : "FAIL"); + before = g_failed; + g_rng = 0xc3c3c3c3ull; + for (int i = 0; i < 64; i++) { + size_t n = 2049 + (((size_t)rnd() << 16) ^ rnd()) % (cap > (1u << 24) ? (1u << 24) : cap - 2048); + chip_one(n, i % 3, 0); + } + printf("64 random lengths: %s\n", g_failed == before ? "PASS" : "FAIL"); + + for (int mut = 1; mut <= 3; mut++) { + omega_gpu_scan_test_set_mutant(mut); + const size_t n = 1000; + fill(g_x, n, 1, 4242); + int caught = 0; + if (mut == 1) { + for (int op = 0; op < 3 && !caught; op++) { + float got = 0, want; omega_gpu_reduce_reference((OmegaGpuRedOp)op, g_x, n, &want); + caught = omega_gpu_reduce((OmegaGpuRedOp)op, g_x, n, &got, NULL) != 0 || !same(got, want); + } + } else { + size_t first; + omega_gpu_prefix_sum_reference(g_x, n, g_ref); + caught = omega_gpu_prefix_sum(g_x, n, g_y, NULL) != 0 || diff(g_y, g_ref, n, &first) != 0; + } + snprintf(w, sizeof w, "chip red check: mutant %d is caught", mut); + check(caught, w); + } + omega_gpu_scan_test_set_mutant(0); + fill(g_x, 1000, 1, 4242); + omega_gpu_prefix_sum_reference(g_x, 1000, g_ref); + size_t first; + check(omega_gpu_prefix_sum(g_x, 1000, g_y, NULL) == 0 && diff(g_y, g_ref, 1000, &first) == 0, "the real kernels are right again after the red checks"); + omega_gpu_session_close(); /* the kernels go with the device (close hook); the next call builds them on a new one */ + check(omega_gpu_prefix_sum(g_x, 1000, g_y, NULL) == 0 && diff(g_y, g_ref, 1000, &first) == 0, "after the device closed and opened again the kernels are rebuilt and right"); + free(g_x); free(g_y); free(g_ref); +} + +int main(int argc, char **argv) { + const char *tier = "chip"; + unsigned max_log2 = 28; + for (int i = 1; i < argc; i++) { + if (!strcmp(argv[i], "--host-only")) tier = "host"; + else if (!strcmp(argv[i], "--sim")) tier = "sim"; + else if (!strcmp(argv[i], "--max-log2") && i + 1 < argc) max_log2 = (unsigned)atoi(argv[++i]); + else { fprintf(stderr, "usage: %s [--host-only | --sim] [--max-log2 K]\n", argv[0]); return 2; } + } + if (max_log2 < 11 || max_log2 > 28) { fprintf(stderr, "--max-log2 must be 11..28\n"); return 2; } + if (!strcmp(tier, "host")) host_only(); + else if (!strcmp(tier, "sim")) { host_only(); sim_tier(); } + else chip_tier(max_log2); + if (g_failed) printf("C3 scan %s: FAIL (%d of %d)\n", tier, g_failed, g_checks); + else printf("C3 scan %s: PASS (%d checks)\n", tier, g_checks); + return g_failed ? 1 : 0; +} ```