diff --git a/.crumb b/.crumb index bff65f4b0..56f4bb333 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", + "digest": "38942b47f7ce98113cdf562eefff1cfd3c460a5d34ef9314ad0097cc0ded2ba8", + "last_commit": "8ec82485d", "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", + "digest": "310f0ece1ecd090c470315f89d123d10031dfdb0b932d98b5e2c1cd9ed5fe0ca", + "last_commit": "309ab461c", "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": "460466e3be802aaf74894fc2cbfe673caf81f0cebec86c10e2ab465365af3bd1", + "last_commit": "309ab461c", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "8fa4874233c906929082e779408ae009b381ade4590b930a2faf68c60fd7e48f", - "last_commit": "79b2b632", + "digest": "9945e53ba48c2e7869f72c7efbd8463b75f77bc9f7b1c94436e22d4a0be676c5", + "last_commit": "3491e05ce", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, { - "digest": "e519c1f13dc5134c5b6ab17f669745776880cb6e78c6efd2f716283760466c66", - "last_commit": "1a12d5d0", + "digest": "720264194c2cbc8fc362039b6262a720dfecb2fdef1952b34e7d80a2d4d7d63a", + "last_commit": "008096a54", "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:5813707fc120923506e9dd7d6563937f2bb12fc77742cf2cbd60f718dcfb0cd5", + "compiled_at": "2026-10-24T23:54:51Z", "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:1d30bc48076ce3a9e772e164f3f781e6da709eb411d15121a1fdd82341878243", + "evidence_files": 11633, + "evidence_root": "sha256:151d2ba27d8d4c9f8185186eb881c85a017ed7ce8ff700aad83860bde5df862f", + "files": 15040, + "generated_at_commit": "fe8bf06c6185a99c25d7658614ccbc812b6df465", + "source_tree": "sha256:56e7e0a0a574aed76651a29a4bed50c71246b75ac601d95caca86dde1edccb5d" }, "owner_lane": "", "provenance": { diff --git a/.gitignore b/.gitignore index fd0ce970e..1df9f6580 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ build/qual-runs/ /omega tests/test_omega_numeric .crumb.local +build-pr/ diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 4d86c43d6..0bc3b8196 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -13,6 +13,7 @@ Every model below did real work. A model is listed only for work it did. The orc | Gemini 3.8 Flash | Code review of the numeric changes (patch checks, qualifier script, build rules) | | Codex | Numeric uncertain-completion retention, exact completion checks, and simulated-driver lifecycle regression tests | | Codex | Private exclusive sm121 reference outputs and bounded source-bound host safety checks | +| Codex | C5 kernel-dump private exclusive creation, failure handling, and source-bound host safety checks | | Claude Opus 5.5 | Ionic-droplet reservoir (#440): pinned reference, CPU simulator and readout, `.mat` reader, Gate 2 comparison, benchmarks, timing and receipt | This table starts with the numeric work on this branch. Earlier work in this repository is not yet attributed here; add rows only for work that can be traced to a model. diff --git a/Makefile b/Makefile index aa4e7428f..c97c96f6b 100644 --- a/Makefile +++ b/Makefile @@ -44,6 +44,10 @@ 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 +# C3: matrix multiply of any shape, BF16 and FP8, both layouts, on the tensor cores (pack kernels + one multiply kernel). +SRCS += src/omega_gpu_gemm.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) @@ -2656,6 +2660,26 @@ test-gpu-cmdlist: $(GPU_CMDLIST_TEST) ./$(GPU_CMDLIST_TEST) --host-only ./$(GPU_CMDLIST_TEST) --sim +# C4 streams (docs/OMEGA.md row C4): two queues of the same half-chip work side by side, an event (cmdlist wait_for) +# between them, both with red checks. test-gpu-streams is host + warp simulator; the chip tier is "$(GPU_STREAMS_TEST)" +# with no flag, under the GB10 lock (timings need an exclusive window). +.PHONY: test-gpu-streams +GPU_STREAMS_TEST = $(OUT_DIR)/gpu_streams_test +$(GPU_STREAMS_TEST): tests/gpu_streams_test.c tests/bw_warp_sim.h src/omega_gpu_cmdlist.h src/omega_bw_em.h $(OUT_DIR)/libomega_gpu.a + $(CC) $(CFLAGS) -ffp-contract=off -Itests -o $@ tests/gpu_streams_test.c $(OUT_DIR)/libomega_gpu.a -lpthread -lm +test-gpu-streams: $(GPU_STREAMS_TEST) + ./$(GPU_STREAMS_TEST) --sim + +# C5 isolation (docs/OMEGA.md row C5): a never-ending kernel times out, omega_gpu_session_recover, a correct launch in the +# same process; two processes on one GPU; a hang beside a working process. test-gpu-isolation is host + simulator; +# the chip tier is "$(GPU_ISOLATION_TEST)" with no flag, under the GB10 lock. +.PHONY: test-gpu-isolation +GPU_ISOLATION_TEST = $(OUT_DIR)/gpu_isolation_test +$(GPU_ISOLATION_TEST): tests/gpu_isolation_test.c tests/bw_warp_sim.h src/omega_gpu_session.h src/omega_bw_em.h $(OUT_DIR)/libomega_gpu.a + $(CC) $(CFLAGS) -ffp-contract=off -Itests -o $@ tests/gpu_isolation_test.c $(OUT_DIR)/libomega_gpu.a -lpthread -lm +test-gpu-isolation: $(GPU_ISOLATION_TEST) + ./$(GPU_ISOLATION_TEST) --host-only + # OWN-30B W-L2a (2026-10-13): one decode token as one command list (omega_gpu_decode_graph). test-gpu-decode-graph is # host-only; the chip gate is tools/run_gpu_decode_graph_chip.sh under the GB10 lock. .PHONY: test-gpu-decode-graph @@ -2687,3 +2711,22 @@ 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 + +# C3 (docs/OMEGA.md): matrix multiply of any shape, BF16 and FP8 (e4m3), both layouts, on the tensor cores; bit-exact +# against the chip-measured tensor-core model. test-gpu-gemm: host refusals and codegen, then the warp simulator. +# The chip tier is "$(GPU_GEMM_TEST)" with no flag (>= 200 shapes up to 8192, speed vs the measured ceiling), under the GB10 lock. +GPU_GEMM_TEST = $(OUT_DIR)/gpu_gemm_test +$(GPU_GEMM_TEST): tests/gpu_gemm_test.c tests/bw_warp_sim_tc.h tests/bw_hmma_model.h tests/bw_qmma_model.h src/omega_gpu_gemm.h $(OUT_DIR)/libomega_gpu.a + $(CC) $(CFLAGS) -ffp-contract=off -Itests -o $@ tests/gpu_gemm_test.c $(OUT_DIR)/libomega_gpu.a -lpthread -lm +.PHONY: test-gpu-gemm +test-gpu-gemm: $(GPU_GEMM_TEST) + ./$(GPU_GEMM_TEST) --sim diff --git a/docs/.crumb b/docs/.crumb index 662fd03ce..7a1eb14dd 100644 --- a/docs/.crumb +++ b/docs/.crumb @@ -46,92 +46,92 @@ "children": [ { "digest": "640f6b7b818c769ab0dcd91437d58dd3251044084d2ae3785f000d30d2bb768a", - "last_commit": "a927bd6c", + "last_commit": "a927bd6c2", "name": "adr", "purpose": "Directory docs/adr. Purpose not yet described by a human." }, { "digest": "e7ac2460455a35b96b01dab649dcdc2cdf5bb4657bf230b91272c64fe1bf82cf", - "last_commit": "d48ab89e", + "last_commit": "d48ab89e5", "name": "autodiff", "purpose": "M21 OMEGA_AUTODIFF scope, op and error-contract table, status (not qualified)" }, { "digest": "7506b1a8659e6de84e1be31a281c8d046e4946de6ed29451155bb56cda5cb365", - "last_commit": "6637b32b", + "last_commit": "6637b32b6", "name": "brownian", "purpose": "Directory docs/brownian. Purpose not yet described by a human." }, { "digest": "1b01542d91e3b1c7c5263d6b393338a3d9d40e138a12ec833ce7852d83887ce2", - "last_commit": "5901baa5", + "last_commit": "5901baa51", "name": "crumbs", "purpose": "Directory docs/crumbs. Purpose not yet described by a human." }, { "digest": "e9baaaeec04be41a57c4a2d6a540b01a682c70d4db96cb4af4e1a9912a133aa9", - "last_commit": "55d05d6a", + "last_commit": "55d05d6a5", "name": "dual", "purpose": "DUAL (ARCH-0031) documents in omega: applicability experiment method and findings (APPLICABILITY" }, { "digest": "56a5a999ce726e90d89c1c88b9e20c81857c8e6ee82af8d651ac80ea70e2c46d", - "last_commit": "38a6a569", + "last_commit": "38a6a569b", "name": "estimation", "purpose": "Directory docs/estimation. Purpose not yet described by a human." }, { - "digest": "82617b47b6fee43e883fa67d97ecf12e15b112dfce8c5729a8ff77c1ef48bcba", - "last_commit": "95829f2a", + "digest": "8b926a045985692f116d9469af5aaa06d778bca78cf5d7e891e68c2486ed789e", + "last_commit": "8ec82485d", "name": "numeric", "purpose": "Directory docs/numeric. Purpose not yet described by a human." }, { "digest": "6fff07c654fe051c1738dd033ac3723f28df48d44efc1365982205eab21337c6", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "osc", "purpose": "Directory docs/osc. Purpose not yet described by a human." }, { "digest": "ffe2e59267bcc6597fdadad5fc7c54e32bbc0f0e409dae9af3ddd3ba49a0d8ad", - "last_commit": "61c09411", + "last_commit": "61c094113", "name": "polyglot", "purpose": "Directory docs/polyglot. Purpose not yet described by a human." }, { "digest": "46d3e2d59a468271e044ccd2398eb32345df6436c15eb6d8c703953c838023a9", - "last_commit": "dd67f6bd", + "last_commit": "dd67f6bd7", "name": "reservoir", "purpose": "Reservoir reference and contracts (omega#440): RESERVOIR_REFERENCE.md pins the authors' paper an" }, { "digest": "2ab37838f8694e5060a3789f08a16146996050af949fba329ed805429d85f695", - "last_commit": "f633e343", + "last_commit": "f633e3430", "name": "tensor", "purpose": "M20 OMEGA_TENSOR spec and qualification checklist" }, { "digest": "41ab5cf8617e9583dcda75292e1e851e1ec4187b69d5d00b5eb354f1ee55c9d5", - "last_commit": "54826a37", + "last_commit": "54826a37f", "name": "train", "purpose": "Directory docs/train. Purpose not yet described by a human." }, { "digest": "b6ed9ddbc50071409ece5b2a6510f57184defcba5ffc7103f1dfe24ae440ef67", - "last_commit": "b7a788e8", + "last_commit": "b7a788e82", "name": "turing", "purpose": "Directory docs/turing. Purpose not yet described by a human." } ], - "children_root": "sha256:714214544c32beee15166fcc8b150c5847bf3786035848011e1e564bdd72ddd4", - "compiled_at": "2026-10-24T23:30:12Z", + "children_root": "sha256:3f9c84a16227ab9b30962f44cf367f96a0d7a349b1a5105a9a423cffaeee937f", + "compiled_at": "2026-10-24T23:54:51Z", "compiler": "crumb-compile/1", - "digest": "sha256:c92de7796549ecf47bb28700d4b82e8ba3e4dd260cdb4caa61a3337c44e063d3", + "digest": "sha256:38942b47f7ce98113cdf562eefff1cfd3c460a5d34ef9314ad0097cc0ded2ba8", "evidence_files": 34, "evidence_root": "sha256:589d8b3706e6d7a202ab20b278dac5fc5bafc973f1c2a867b2668aa7210b65cc", - "files": 131, - "generated_at_commit": "60030b348fc6421d1dd035f854bd1d8b5d622813", - "source_tree": "sha256:11a0166c747b5c23cc881c80529e976729e175a9450eb300416312e04c9d03c9" + "files": 132, + "generated_at_commit": "fe8bf06c6185a99c25d7658614ccbc812b6df465", + "source_tree": "sha256:bed403af4de89d2094938ee858b4a5ced9f378bc8c0b23f43863e48bd9bd3d35" }, "owner_lane": "", "provenance": { diff --git a/docs/numeric/.crumb b/docs/numeric/.crumb index bfd1317a2..562a6b666 100644 --- a/docs/numeric/.crumb +++ b/docs/numeric/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-12T16:47:44Z", + "compiled_at": "2026-10-24T09:47:00Z", "compiler": "crumb-compile/1", - "digest": "sha256:82617b47b6fee43e883fa67d97ecf12e15b112dfce8c5729a8ff77c1ef48bcba", + "digest": "sha256:8b926a045985692f116d9469af5aaa06d778bca78cf5d7e891e68c2486ed789e", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 11, - "generated_at_commit": "a073db70e22eec13128c00554683dd8c59b6f456", - "source_tree": "sha256:556ea8188eb5b58ec322a565225ab7612de218570fe17a1d3df387a7b8be3b29" + "files": 12, + "generated_at_commit": "f792cd1d35b0e9c191647518c3be651a8d86dc21", + "source_tree": "sha256:d017116c3ccc565cf4fdc90703ba93dfb350183c517d480d809dc0944e87a0fd" }, "owner_lane": "", "provenance": { diff --git a/docs/numeric/GPU_CHANNELS.md b/docs/numeric/GPU_CHANNELS.md new file mode 100644 index 000000000..83bcecceb --- /dev/null +++ b/docs/numeric/GPU_CHANNELS.md @@ -0,0 +1,60 @@ +# GPU channels: more than one command queue (L136, roadblock R3, OSC-0 decision 12) + +Written before the measurement. The receipt goes in `evidence/q38-channels-*`. + +## What a channel is + +A channel is one command queue into the GPU: a ring of work entries plus a doorbell. Until now the engine opened one +(`src/omega_gpu_platform_linux.c`, `max_queues = 1`), so every job from every caller waited in one line. + +Cost of a second channel (physics `doc/driver-floor` DRIVER_FLOOR.md): one more BLACKWELL_CHANNEL_GPFIFO_A and +BLACKWELL_COMPUTE_B in the same KEPLER_CHANNEL_GROUP_A and FERMI_CONTEXT_SHARE_A, one UVM_REGISTER_CHANNEL, one +GET_WORK_SUBMIT_TOKEN, all before the single GPFIFO_SCHEDULE. That is setup, paid once at device open. A submission +after that is two stores (ring entry, doorbell), the same as channel 0. Physics `feat/gpu-channels` 7476de7 adds this as +`nvrm_channel_n`; the platform opens it only when `AIEN_OMEGA_GPU_CHANNELS` is 2..4. Unset, the code path is the +single channel exactly. + +## The arithmetic for one decode token of the 27B (Qwen3.8-27B, FP8 weights, bf16 head) + +- Weights read per token: 26.9 GB. At the memory ceiling of 273 GB/s that is 10.15 tok/s (calc:41e5e25b). +- The token is a chain: each layer needs the previous layer's output, so layer N+1's weights cannot be "loaded early". + On this machine the weights are not copied anywhere before use; the multiply kernel reads them straight from memory. + Reading them IS the work. The L2 cache (tens of MB) is far smaller than one layer (~380 MB), so there is nothing to + prefetch into. +- Timeline receipt `evidence/q38-decode-speed-chip-20261019T0316Z` (4 layers + head): the weight multiplies take + 18584 of 18866 us; everything else (norms, attention, GDN step, gaps between launches) is 1.49% (calc:b581cb99). +- The one independent pair inside a token (the small a|b multiply beside the big mix multiply in GDN layers) already + runs side by side on ONE channel (`omega_gpu_cmdlist_mark_independent`, `AIEN_OMEGA_Q38_OVERLAP_AB`, default on). + The brief's fallback (overlap by reordering on one channel) therefore already exists and is installed. + +So splitting one token across two channels can win at most ~1.5% for one request, minus the cost of the fences that +order the two queues. Not worth building first. + +## Where a second channel could help: two requests at once + +Each weight multiply reaches 193 to 229 GB/s of the 273 GB/s ceiling (same receipt). If a single multiply is limited by +how many memory reads one grid keeps in flight, two grids from two requests running at the same time could pull more +together. Ceiling of that gain: 273 / 220 = 1.24x aggregate tokens per second (calc:91c5deff). Requests do not share +weight reads this way; each still reads all 26.9 GB. + +Unknowns, said out loud: +1. Whether the GPU runs work from two channels of one channel group at the same time, or takes turns (time slicing). + The CUDA guide says separate queues remove false dependencies but "concurrent execution of independent GPU work is + not guaranteed". DOCS SILENT on GB10 specifics. +2. Whether a WAIT_FOR_IDLE on channel 0 (every dependent launch has one) also waits for channel 1's grids. If it does, + the two streams serialise at every barrier. + +## Prediction (before the measurement) + +Two independent 27B decode streams, 64 layers, 64 steps: +- serial (both on channel 0, one after the other): about 2 x 123 ms per step (one stream decodes at 8.13 tok/s on + branch qwen38/decode-speed), so ~8.1 tok/s aggregate, the same as one stream. +- paired (one per channel, both in flight): between 1.00x and 1.10x of serial throughput. Most likely ~1.0x: each + multiply already pulls ~80% of the bandwidth, and unknown 2 may serialise them anyway. +- Outputs bit-identical between serial and paired (the streams share only read-only weights). + +## The bigger lever, for later + +Batching: two requests multiply against the SAME weight read (a matrix times two vectors instead of one). That halves +the bytes per token for two requests, ceiling 2x aggregate, and needs a kernel change (GEMV to a thin GEMM), not a +channel change. It is the next step for throughput regardless of what the channel receipt says. diff --git a/evidence/C3-GEMM/01-host-only.log b/evidence/C3-GEMM/01-host-only.log new file mode 100644 index 000000000..c581c5126 --- /dev/null +++ b/evidence/C3-GEMM/01-host-only.log @@ -0,0 +1,39 @@ +the bf16 model is a multiply (A row B row): worst error 3.81e-07 of the magnitude sum +the bf16 model is a multiply (A col B row): worst error 5.42e-07 of the magnitude sum +the bf16 model is a multiply (A row B col): worst error 5.77e-07 of the magnitude sum +the bf16 model is a multiply (A col B col): worst error 4.26e-07 of the magnitude sum +the e4m3 model is a multiply (A row B row): worst error 3.16e-07 of the magnitude sum +the e4m3 model is a multiply (A col B row): worst error 3.17e-07 of the magnitude sum +the e4m3 model is a multiply (A row B col): worst error 2.44e-07 of the magnitude sum +the e4m3 model is a multiply (A col B col): worst error 2.58e-07 of the magnitude sum +bf16 rt 2 ct 4 wm 2 wn 2 kernel 0 mutant 0: codegen 0, encode 0, qmma patches 0, 28 gprs +bf16 rt 2 ct 4 wm 2 wn 2 kernel 1 mutant 0: codegen 0, encode 0, qmma patches 0, 28 gprs +bf16 rt 2 ct 4 wm 2 wn 2 kernel 2 mutant 0: codegen 0, encode 0, qmma patches 0, 116 gprs +bf16 rt 1 ct 1 wm 1 wn 1 kernel 0 mutant 0: codegen 0, encode 0, qmma patches 0, 28 gprs +bf16 rt 1 ct 1 wm 1 wn 1 kernel 1 mutant 0: codegen 0, encode 0, qmma patches 0, 20 gprs +bf16 rt 1 ct 1 wm 1 wn 1 kernel 2 mutant 0: codegen 0, encode 0, qmma patches 0, 48 gprs +bf16 rt 4 ct 1 wm 2 wn 2 kernel 0 mutant 0: codegen 0, encode 0, qmma patches 0, 28 gprs +bf16 rt 4 ct 1 wm 2 wn 2 kernel 1 mutant 0: codegen 0, encode 0, qmma patches 0, 20 gprs +bf16 rt 4 ct 1 wm 2 wn 2 kernel 2 mutant 0: codegen 0, encode 0, qmma patches 0, 108 gprs +bf16 rt 1 ct 4 wm 4 wn 1 kernel 0 mutant 0: codegen 0, encode 0, qmma patches 0, 28 gprs +bf16 rt 1 ct 4 wm 4 wn 1 kernel 1 mutant 0: codegen 0, encode 0, qmma patches 0, 28 gprs +bf16 rt 1 ct 4 wm 4 wn 1 kernel 2 mutant 0: codegen 0, encode 0, qmma patches 0, 84 gprs +bf16 rt 2 ct 2 wm 1 wn 4 kernel 0 mutant 0: codegen 0, encode 0, qmma patches 0, 28 gprs +bf16 rt 2 ct 2 wm 1 wn 4 kernel 1 mutant 0: codegen 0, encode 0, qmma patches 0, 28 gprs +bf16 rt 2 ct 2 wm 1 wn 4 kernel 2 mutant 0: codegen 0, encode 0, qmma patches 0, 84 gprs +e4m3 rt 2 ct 4 wm 2 wn 2 kernel 0 mutant 0: codegen 0, encode 0, qmma patches 0, 40 gprs +e4m3 rt 2 ct 4 wm 2 wn 2 kernel 1 mutant 0: codegen 0, encode 0, qmma patches 0, 40 gprs +e4m3 rt 2 ct 4 wm 2 wn 2 kernel 2 mutant 0: codegen 0, encode 0, qmma patches 32, 116 gprs +e4m3 rt 1 ct 1 wm 1 wn 1 kernel 0 mutant 0: codegen 0, encode 0, qmma patches 0, 40 gprs +e4m3 rt 1 ct 1 wm 1 wn 1 kernel 1 mutant 0: codegen 0, encode 0, qmma patches 0, 28 gprs +e4m3 rt 1 ct 1 wm 1 wn 1 kernel 2 mutant 0: codegen 0, encode 0, qmma patches 4, 48 gprs +e4m3 rt 4 ct 1 wm 2 wn 2 kernel 0 mutant 0: codegen 0, encode 0, qmma patches 0, 40 gprs +e4m3 rt 4 ct 1 wm 2 wn 2 kernel 1 mutant 0: codegen 0, encode 0, qmma patches 0, 28 gprs +e4m3 rt 4 ct 1 wm 2 wn 2 kernel 2 mutant 0: codegen 0, encode 0, qmma patches 16, 108 gprs +e4m3 rt 1 ct 4 wm 4 wn 1 kernel 0 mutant 0: codegen 0, encode 0, qmma patches 0, 40 gprs +e4m3 rt 1 ct 4 wm 4 wn 1 kernel 1 mutant 0: codegen 0, encode 0, qmma patches 0, 40 gprs +e4m3 rt 1 ct 4 wm 4 wn 1 kernel 2 mutant 0: codegen 0, encode 0, qmma patches 16, 84 gprs +e4m3 rt 2 ct 2 wm 1 wn 4 kernel 0 mutant 0: codegen 0, encode 0, qmma patches 0, 40 gprs +e4m3 rt 2 ct 2 wm 1 wn 4 kernel 1 mutant 0: codegen 0, encode 0, qmma patches 0, 40 gprs +e4m3 rt 2 ct 2 wm 1 wn 4 kernel 2 mutant 0: codegen 0, encode 0, qmma patches 16, 84 gprs +C3 gemm host: PASS (136 checks) diff --git a/evidence/C3-GEMM/02-sim.log b/evidence/C3-GEMM/02-sim.log new file mode 100644 index 000000000..fd97da9f2 --- /dev/null +++ b/evidence/C3-GEMM/02-sim.log @@ -0,0 +1,51 @@ +the bf16 model is a multiply (A row B row): worst error 3.81e-07 of the magnitude sum +the bf16 model is a multiply (A col B row): worst error 5.42e-07 of the magnitude sum +the bf16 model is a multiply (A row B col): worst error 5.77e-07 of the magnitude sum +the bf16 model is a multiply (A col B col): worst error 4.26e-07 of the magnitude sum +the e4m3 model is a multiply (A row B row): worst error 3.16e-07 of the magnitude sum +the e4m3 model is a multiply (A col B row): worst error 3.17e-07 of the magnitude sum +the e4m3 model is a multiply (A row B col): worst error 2.44e-07 of the magnitude sum +the e4m3 model is a multiply (A col B col): worst error 2.58e-07 of the magnitude sum +bf16 rt 2 ct 4 wm 2 wn 2 kernel 0 mutant 0: codegen 0, encode 0, qmma patches 0, 28 gprs +bf16 rt 2 ct 4 wm 2 wn 2 kernel 1 mutant 0: codegen 0, encode 0, qmma patches 0, 28 gprs +bf16 rt 2 ct 4 wm 2 wn 2 kernel 2 mutant 0: codegen 0, encode 0, qmma patches 0, 116 gprs +bf16 rt 1 ct 1 wm 1 wn 1 kernel 0 mutant 0: codegen 0, encode 0, qmma patches 0, 28 gprs +bf16 rt 1 ct 1 wm 1 wn 1 kernel 1 mutant 0: codegen 0, encode 0, qmma patches 0, 20 gprs +bf16 rt 1 ct 1 wm 1 wn 1 kernel 2 mutant 0: codegen 0, encode 0, qmma patches 0, 48 gprs +bf16 rt 4 ct 1 wm 2 wn 2 kernel 0 mutant 0: codegen 0, encode 0, qmma patches 0, 28 gprs +bf16 rt 4 ct 1 wm 2 wn 2 kernel 1 mutant 0: codegen 0, encode 0, qmma patches 0, 20 gprs +bf16 rt 4 ct 1 wm 2 wn 2 kernel 2 mutant 0: codegen 0, encode 0, qmma patches 0, 108 gprs +bf16 rt 1 ct 4 wm 4 wn 1 kernel 0 mutant 0: codegen 0, encode 0, qmma patches 0, 28 gprs +bf16 rt 1 ct 4 wm 4 wn 1 kernel 1 mutant 0: codegen 0, encode 0, qmma patches 0, 28 gprs +bf16 rt 1 ct 4 wm 4 wn 1 kernel 2 mutant 0: codegen 0, encode 0, qmma patches 0, 84 gprs +bf16 rt 2 ct 2 wm 1 wn 4 kernel 0 mutant 0: codegen 0, encode 0, qmma patches 0, 28 gprs +bf16 rt 2 ct 2 wm 1 wn 4 kernel 1 mutant 0: codegen 0, encode 0, qmma patches 0, 28 gprs +bf16 rt 2 ct 2 wm 1 wn 4 kernel 2 mutant 0: codegen 0, encode 0, qmma patches 0, 84 gprs +e4m3 rt 2 ct 4 wm 2 wn 2 kernel 0 mutant 0: codegen 0, encode 0, qmma patches 0, 40 gprs +e4m3 rt 2 ct 4 wm 2 wn 2 kernel 1 mutant 0: codegen 0, encode 0, qmma patches 0, 40 gprs +e4m3 rt 2 ct 4 wm 2 wn 2 kernel 2 mutant 0: codegen 0, encode 0, qmma patches 32, 116 gprs +e4m3 rt 1 ct 1 wm 1 wn 1 kernel 0 mutant 0: codegen 0, encode 0, qmma patches 0, 40 gprs +e4m3 rt 1 ct 1 wm 1 wn 1 kernel 1 mutant 0: codegen 0, encode 0, qmma patches 0, 28 gprs +e4m3 rt 1 ct 1 wm 1 wn 1 kernel 2 mutant 0: codegen 0, encode 0, qmma patches 4, 48 gprs +e4m3 rt 4 ct 1 wm 2 wn 2 kernel 0 mutant 0: codegen 0, encode 0, qmma patches 0, 40 gprs +e4m3 rt 4 ct 1 wm 2 wn 2 kernel 1 mutant 0: codegen 0, encode 0, qmma patches 0, 28 gprs +e4m3 rt 4 ct 1 wm 2 wn 2 kernel 2 mutant 0: codegen 0, encode 0, qmma patches 16, 108 gprs +e4m3 rt 1 ct 4 wm 4 wn 1 kernel 0 mutant 0: codegen 0, encode 0, qmma patches 0, 40 gprs +e4m3 rt 1 ct 4 wm 4 wn 1 kernel 1 mutant 0: codegen 0, encode 0, qmma patches 0, 40 gprs +e4m3 rt 1 ct 4 wm 4 wn 1 kernel 2 mutant 0: codegen 0, encode 0, qmma patches 16, 84 gprs +e4m3 rt 2 ct 2 wm 1 wn 4 kernel 0 mutant 0: codegen 0, encode 0, qmma patches 0, 40 gprs +e4m3 rt 2 ct 2 wm 1 wn 4 kernel 1 mutant 0: codegen 0, encode 0, qmma patches 0, 40 gprs +e4m3 rt 2 ct 2 wm 1 wn 4 kernel 2 mutant 0: codegen 0, encode 0, qmma patches 16, 84 gprs +simulator bf16 rt 2 ct 4 wm 2 wn 2: 40 multiplies, 0 wrong +simulator bf16 rt 1 ct 1 wm 1 wn 1: 10 multiplies, 0 wrong +simulator bf16 rt 4 ct 1 wm 1 wn 4: 10 multiplies, 0 wrong +simulator e4m3 rt 2 ct 4 wm 2 wn 2: 40 multiplies, 0 wrong +simulator e4m3 rt 1 ct 1 wm 1 wn 1: 10 multiplies, 0 wrong +simulator e4m3 rt 4 ct 1 wm 1 wn 4: 10 multiplies, 0 wrong +simulator red check: bf16 mutant 1 is caught (4095 wrong) +simulator red check: bf16 mutant 2 is caught (4095 wrong) +simulator red check: bf16 mutant 3 is caught (4095 wrong) +simulator red check: e4m3 mutant 1 is caught (4095 wrong) +simulator red check: e4m3 mutant 2 is caught (4095 wrong) +simulator red check: e4m3 mutant 3 is caught (4095 wrong) +C3 gemm sim: PASS (154 checks) diff --git a/evidence/C3-GEMM/README.md b/evidence/C3-GEMM/README.md new file mode 100644 index 000000000..0e730a9bf --- /dev/null +++ b/evidence/C3-GEMM/README.md @@ -0,0 +1,19 @@ +# C3 receipt, part 2: matrix multiply of any shape on the tensor cores + +Row C3 of `docs/OMEGA.md` asks for matrix multiply (GEMM) of any shape in BF16 and FP8, both layouts, at least 200 shapes +from 1 to 8192, correct, with speed as a fraction of the measured tensor-core ceiling. The kernels come from Omega's own +code generator (`src/omega_gpu_gemm.c`), no CUDA: two pack kernels put A and B (row- or column-major) into the tensor +cores' order with zeros past the edges, one multiply kernel runs the tensor cores (HMMA for BF16, QMMA for FP8). + +"Correct" here is bit for bit: the reference is a host model of the same tensor-core steps whose rounding was measured +on this chip (`tests/bw_hmma_model.h`, `tests/bw_qmma_model.h`, both exact on 262,144 of 262,144 chip outputs), and a +separate check shows that model stays within one part in a million of the exact product. + +| Log | What it checks | Result | +|---|---|---| +| 01-host-only | bad calls refused, tile shapes past the register budget refused; every kernel (both types, five tile shapes, each planted fault) through our code generator and encoder; the model against exact double-precision sums | PASS, 136 checks | +| 02-sim | the real kernels in the warp simulator (tensor-core ops computed with the chip models): ten shapes over every edge, all four layouts, three tile shapes, both types, every output compared; three planted faults (a dropped k step, no zeros past the edges, swapped columns) each caught on all 4,095 outputs | PASS, 154 checks | +| (next) chip | 208 shapes up to 8192, both types, all four layouts; speed against 115.36 TFLOPS (BF16) and 237.52 TFLOPS (FP8) | waiting for a chip window | + +Command: `make test-gpu-gemm` (host and simulator). The chip run is `build/gpu_gemm_test` with no flag, under the GB10 lock. 01 and 02 logs: 2026-10-24 23:11Z on the stacked tip (gemm layer over the scan layer over main 0d72560a; binary sha256 0a624c25 a68cef88...; `PHYSICS_DIR` must point at the checkout pinned in `physics.lock`), host 136 checks, simulator 154 checks. +This is the general kernel; the prefill-roof lane (L403) tunes it toward the ceiling. 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/evidence/C4-STREAMS/01-host-only.log b/evidence/C4-STREAMS/01-host-only.log new file mode 100644 index 000000000..b4ab2c152 --- /dev/null +++ b/evidence/C4-STREAMS/01-host-only.log @@ -0,0 +1,6 @@ +ok: codegen + encode: spin kernel (512 bytes, 12 registers) +ok: codegen + encode: spin kernel mutant 1 (512 bytes, 12 registers) +ok: codegen + encode: copy kernel (512 bytes, 12 registers) +ok: codegen + encode: copy kernel mutant 1 (512 bytes, 12 registers) +ok: wait_for refuses missing lists +C4 streams host: PASS (5 checks) diff --git a/evidence/C4-STREAMS/02-sim.log b/evidence/C4-STREAMS/02-sim.log new file mode 100644 index 000000000..bdb778d20 --- /dev/null +++ b/evidence/C4-STREAMS/02-sim.log @@ -0,0 +1,10 @@ +ok: codegen + encode: spin kernel (512 bytes, 12 registers) +ok: codegen + encode: spin kernel mutant 1 (512 bytes, 12 registers) +ok: codegen + encode: copy kernel (512 bytes, 12 registers) +ok: codegen + encode: copy kernel mutant 1 (512 bytes, 12 registers) +ok: wait_for refuses missing lists +ok: simulator order 0: spin + copy exact on all 3072 elements (0, 0 wrong) +ok: simulator order 1: spin + copy exact on all 3072 elements (0, 0 wrong) +ok: simulator red check order 0: mutant 1 caught (3072, 3072 wrong) +ok: simulator red check order 1: mutant 1 caught (3072, 3072 wrong) +C4 streams sim: PASS (9 checks) diff --git a/evidence/C4-STREAMS/03-chip-values-20261022-f792cd1d.log b/evidence/C4-STREAMS/03-chip-values-20261022-f792cd1d.log new file mode 100644 index 000000000..2ad94e09e --- /dev/null +++ b/evidence/C4-STREAMS/03-chip-values-20261022-f792cd1d.log @@ -0,0 +1,18 @@ +Thu Oct 22 07:06:34 UTC 2026 +f792cd1d35b0e9c191647518c3be651a8d86dc21 +b97749a9dc249d734a268279e0b84185b4bba28a0b22f44136fbb3b13b27007b build/gpu_streams_test +COMMAND: spark-window run timeout 180 stdbuf -oL -eL build/gpu_streams_test --values-only +ok: the second queue opens (AIEN_OMEGA_GPU_CHANNELS = 2) +calibration: 2^16 turns took 7.702 ms; using 510541 turns +C4 overlap: NOT_RUN (values-only; timing needs an exclusive window) +ok: event refusals: a list with a wait is refused alone, and cannot wait on a second list + event (wait, default barrier): X 0 wrong, Y 0 wrong of 3072, pair 56.227 ms +ok: C4 event: B's copy waited for A's spin on the other queue; every Y right (0 wrong) + event (NO wait, default barrier): X 0 wrong, Y 0 wrong of 3072, pair 17.456 ms + event (NO wait, first_barrier_none): X 0 wrong, Y 0 wrong of 3072, pair 13.879 ms +FAIL: negative control: without the wait B ran early and Y is wrong (default barrier 0 wrong, first_barrier_none 0 wrong) +ok: event refusals: a list with a wait is refused alone, and cannot wait on a second list + event (wait, first_barrier_none): X 0 wrong, Y 0 wrong of 3072, pair 17.496 ms +ok: C4 event with first_barrier_none: every Y right (0 wrong) +C4 streams values: FAIL (1 of 6) +EXIT_CODE=1 diff --git a/evidence/C4-STREAMS/04-chip-values-20261025-048e5f5.log b/evidence/C4-STREAMS/04-chip-values-20261025-048e5f5.log new file mode 100644 index 000000000..2df53f7d9 --- /dev/null +++ b/evidence/C4-STREAMS/04-chip-values-20261025-048e5f5.log @@ -0,0 +1,22 @@ +# C4 streams on the pinned driver, 2026-10-25T00:35Z +# omega c5/isolation 02db88d33 (physics.lock = 048e5f5e88, the merged extra-channel subcontext fix), PHYSICS_DIR=~/workspace/l386-physics-pr checked out at that commit, pin check ENFORCED (no PHYSICS_LOCK_CHECK=0). +# Binary: build/pinned-check/gpu_streams_test, built by make from this tree. +# command: ./build/pinned-check/gpu_streams_test (under /tmp/aien-gb10.lock) +ok: the second queue opens (AIEN_OMEGA_GPU_CHANNELS = 2) +calibration: 2^16 turns took 1.691 ms; using 2325554 turns +overlap (default barrier): solo A 53.774 ms, solo B 53.811 ms, pair 53.771 ms (medians of 5, values right): pair / (A + B) = 0.500 +overlap (first_barrier_none): solo A 53.762 ms, solo B 53.793 ms, pair 53.768 ms (medians of 5, values right): pair / (A + B) = 0.500 +serial control: solo 53.793 ms, both in one list on one queue 107.534 ms: ratio 1.000 +ok: C4 overlap: two queues of the same work finish in under 0.75 of the solo sum (default 0.500, first_barrier_none 0.500) +ok: red check: the same two spins in order on one queue do NOT pass the 0.75 bar (ratio 1.000) +ok: event refusals: a list with a wait is refused alone, and cannot wait on a second list + event (wait, default barrier): X 0 wrong, Y 0 wrong of 3072, pair 53.858 ms +ok: C4 event: B's copy waited for A's spin on the other queue; every Y right (0 wrong) + event (NO wait, default barrier): X 0 wrong, Y 3072 wrong of 3072, pair 53.869 ms +ok: negative control: without the wait B ran early and Y is wrong (default barrier 3072 wrong, first_barrier_none 3072 wrong) +ok: event refusals: a list with a wait is refused alone, and cannot wait on a second list + event (wait, first_barrier_none): X 0 wrong, Y 0 wrong of 3072, pair 53.864 ms +ok: C4 event with first_barrier_none: every Y right (0 wrong) +C4 streams chip: PASS (8 checks) + +# exit 0 diff --git a/evidence/C4-STREAMS/README.md b/evidence/C4-STREAMS/README.md new file mode 100644 index 000000000..9d1a69136 --- /dev/null +++ b/evidence/C4-STREAMS/README.md @@ -0,0 +1,33 @@ +# C4: streams and events on the GB10 (docs/OMEGA.md row C4) + +Row C4: two independent queues of the same work finish in under 0.75 of the sum of their solo times; an event wait +orders two dependent jobs, checked by data, on the chip. + +What this branch adds: +- L136's second GPFIFO queue (physics feat/gpu-channels 7476de71, rebased onto physics main as fb2ac4c = physics PR #79, the commit physics.lock now pins; omega 449be575 + 0cf570dd): `omega_gpu_session_extra_queue` + and `omega_gpu_cmdlist_submit_pair` (list A on queue 0, list B on queue 1, both submitted before either is waited). +- The event: `omega_gpu_cmdlist_wait_for(cl, other, i)`. The next launch added to `cl` starts only after launch `i` of + `other` has released its semaphore: a host semaphore ACQUIRE (the chip-proven omega-mv#580 ORDER_SEM words) before the + launch's barrier, so its cache invalidate comes after the wait. +- `first_barrier_none`: no WAIT_FOR_IDLE before the first launch of a submission (a WAIT_FOR_IDLE may also wait for the + other queue's grids; the test measures both). +- `tests/gpu_streams_test.c`: half-chip spin kernel (24 CTAs x 128 threads, FFMA chain, exact value) and copy kernel, + both from Omega's IR and our own sm_121 encoder. + +| log | what | result | +| --- | --- | --- | +| 01-host-only.log | codegen + encode of both kernels and their mutants; wait_for refusals | PASS (5 checks), 2026-10-24T23:29Z on the train tip | +| 02-sim.log | warp simulator: exact values in both scheduling orders, mutants caught | PASS (9 checks), 2026-10-24T23:29Z on the train tip | +| 03-chip-values-20261022-f792cd1d.log | chip, values only, 2026-10-22 07:06Z on `c5/isolation` f792cd1d (older head; run by the dead worker, log recovered unpushed, durable copy `~/handoffs/evidence-L386-chip-20261022/`): second queue opens; event refusals right; with the wait every Y right (0 wrong of 3072) in both barrier modes; but the NEGATIVE CONTROL IS NOT RED: without the wait Y was also right (0 wrong, both barrier modes), so the run reports `C4 streams values: FAIL (1 of 6)`. Until the control goes red the event proof is not a proof: the two channels may not have run concurrently (Cortex 27f2ae96: two channels of one group ran two decode streams in 0.9990x serial time; whether GB10 time-slices channels of one group is UNKNOWN). Overlap NOT_RUN (values-only). | FAIL (control not red) | +| 04-chip-values-20261025-048e5f5.log | the same values run after the driver cause was found and fixed: each extra channel now allocates its own asynchronous subcontext (physics 048e5f5, physics PR #80), which is what row 03's not-red control predicted. With the wait every Y right (0 wrong of 3072) in both barrier modes AND without the wait 3072 of 3072 wrong, so the control is red at last and the event proof holds | PASS (8 checks), 2026-10-25T00:35Z on `c5/isolation` 02db88d33 with the pin enforced | +| (chip, pending slot) | per-queue timestamps across 3 runs without the wait, then overlap ratio solo vs pair (both barrier modes), serial red check, event + no-wait control, on the train tip | NOT RUN (needs an exclusive window; the C4 overlap ratio is still unmeasured) | + +Chip command (GB10 lock; timings need an exclusive window): +`build/gpu_streams_test` (sets AIEN_OMEGA_GPU_CHANNELS=2 itself). PASS needs: pair / (solo A + solo B) < 0.75, the +same two spins in one list on one queue >= 0.75 (red check), every Y right with the wait, and at least one Y wrong +without it (negative control). + +Update 2026-10-22 (after an independent review): the C4 code continues on branch c5/isolation, which contains this branch. +There a launch that waits always gets the full WAIT_FOR_IDLE + invalidate after the wait, a list with waits is refused by +plain submit and runs only in submit_pair with the list it waits on, and the chip test checks both refusals. The chip +receipt for C4 will be taken on c5/isolation; merge that branch, not this one alone. diff --git a/evidence/C5-ISOLATION/01-host-only.log b/evidence/C5-ISOLATION/01-host-only.log new file mode 100644 index 000000000..dd28ea0cc --- /dev/null +++ b/evidence/C5-ISOLATION/01-host-only.log @@ -0,0 +1,6 @@ +[749500] ok: codegen + encode: work kernel (512 bytes) +[749500] ok: codegen + encode: work kernel mutant 1 (512 bytes) +[749500] ok: codegen + encode: never-ending kernel (512 bytes) +[749500] ok: simulator: work kernel exact on all 4096 elements +[749500] ok: simulator red check: work mutant caught (4096 wrong) +C5 isolation host: PASS (5 checks) diff --git a/evidence/C5-ISOLATION/02-session-platform.log b/evidence/C5-ISOLATION/02-session-platform.log new file mode 100644 index 000000000..ae5f9dda5 --- /dev/null +++ b/evidence/C5-ISOLATION/02-session-platform.log @@ -0,0 +1,19 @@ +$ build/test_omega_gpu_session_platform (fake platform, 2026-10-24T23:28Z, train tip over main 0d72560a, physics fb2ac4c52) +PASS INSTALL_REFUSALS_ABI_1_1 +PASS FREESTANDING_NO_DEFAULT_PLATFORM +PASS OPEN_LAUNCH_CLOSE_TYPED_UNCACHED_POLLING +PASS OPEN_FAILURE_KEEPS_DRIVER_TEXT +PASS CANCEL_BEFORE_SUBMIT_NOT_LATCHED +PASS CANCEL_AFTER_SUBMIT_LATCHES_AND_PARKS +PASS TIMEOUT_PARKS_NEVER_RELEASES +PASS TIMEOUT_LATCH_REFUSES_CLOSE_REOPEN_INSTALL +PASS TIMEOUT_RECOVER_SAME_PROCESS +PASS RECOVER_TEARDOWN_FAULT_STAYS_LATCHED +PASS RECOVER_MODULE_LATCH +PASS STALE_HANDLE_REFUSED_AFTER_CLOSE_REOPEN +PASS SERVING_RESERVATION_REFUSES_WITHOUT_ALLOC +PASS MATMUL_CODE_BUFFER_ENCODED_IN_PLACE +PASS WAIT_REFUSES_WITHOUT_CLOCK +PASS FMT_BOUNDED_MATCHES_SNPRINTF +test_omega_gpu_session_platform: 16 passed, 0 failed +EXIT_CODE=0 diff --git a/evidence/C5-ISOLATION/03-chip-values-20261022-f792cd1d.log b/evidence/C5-ISOLATION/03-chip-values-20261022-f792cd1d.log new file mode 100644 index 000000000..683e0fc1c --- /dev/null +++ b/evidence/C5-ISOLATION/03-chip-values-20261022-f792cd1d.log @@ -0,0 +1,4 @@ +Thu Oct 22 07:06:34 UTC 2026 +f792cd1d35b0e9c191647518c3be651a8d86dc21 +042c56ce2ffa1cec5b90fd15ef984b20ed964269765431f3f823b158b5edb99c build/gpu_isolation_test +COMMAND: spark-window run timeout 180 stdbuf -oL -eL build/gpu_isolation_test --values-only diff --git a/evidence/C5-ISOLATION/03-session-mutants.log b/evidence/C5-ISOLATION/03-session-mutants.log new file mode 100644 index 000000000..521eb3624 --- /dev/null +++ b/evidence/C5-ISOLATION/03-session-mutants.log @@ -0,0 +1,14 @@ +$ make test-gpu-session-mutants (2026-10-24T23:28Z) +freestanding GPU link (14 files): only memcpy memset strcmp strlen undefined +mutant release-when-latched killed: 2 case(s) failed +mutant ignore-cancel killed: 1 case(s) failed +mutant no-latch-on-timeout killed: 4 case(s) failed +mutant stale-not-counted killed: 1 case(s) failed +mutant marker-not-polled killed: 7 case(s) failed +mutant close-while-latched killed: 3 case(s) failed +mutant recover-keeps-latch killed: 1 case(s) failed +mutant recover-without-close killed: 3 case(s) failed +mutant recover-when-not-latched killed: 1 case(s) failed +mutant recover-ignores-teardown-fault killed: 1 case(s) failed +mutant recover-ignores-module-latch killed: 1 case(s) failed +test-gpu-session-mutants: 11 of 11 mutants killed diff --git a/evidence/C5-ISOLATION/04-never-ending-kernel.sass.txt b/evidence/C5-ISOLATION/04-never-ending-kernel.sass.txt new file mode 100644 index 000000000..4ad0ba516 --- /dev/null +++ b/evidence/C5-ISOLATION/04-never-ending-kernel.sass.txt @@ -0,0 +1,7 @@ +$ build/gpu_isolation_test --dump-hang /tmp/c5hang.bin && build/sm121dis /tmp/c5hang.bin (omega c6/disasm 19eebe0e tool) + /*0000*/ MOV R2, 0x0 ; + /*0010*/ MOV R3, 0x1 ; + /*0020*/ IADD3 R2, PT, PT, R2, R3, RZ ; + /*0030*/ BRA 0x20 ; + /*0040*/ EXIT ; + /*0050*/ BRA 0x50; diff --git a/evidence/C5-ISOLATION/05-platform-linux.log b/evidence/C5-ISOLATION/05-platform-linux.log new file mode 100644 index 000000000..e23964855 --- /dev/null +++ b/evidence/C5-ISOLATION/05-platform-linux.log @@ -0,0 +1,13 @@ +$ build/test_omega_gpu_platform_linux (simulated driver, 2026-10-24T23:28Z, train tip over main 0d72560a) +PASS LINUX_REF_CAPS_NO_MMIO_NO_IRQ +PASS HOSTED_DEFAULT_IS_LINUX_REFERENCE +PASS SAME_DRIVER_CALLS_AND_ORDER_AS_BEFORE +PASS REFERENCE_REFUSALS_CACHED_POLLED_RING_STALE +PASS OPEN_FAILURE_KEEPS_DRIVER_TEXT +PASS MARKER_NEVER_LATCHES_AND_PARKS +PASS L2_INVAL_PREFIX_ONCE_AFTER_CACHED_HOST_WRITE + DEBUG submits=4 fake_submit_calls=4 max_launches=4096 l2_invals=1 + expect failed at line 251: st.submits == 2 && fk.submit_calls - submits0 == 2 +FAIL CMDLIST_NEAR_CAP_SUBMITS_WITH_L2_INVAL_PENDING +test_omega_gpu_platform_linux (reference only, simulated driver): 7 passed, 1 failed +EXIT_CODE=1 diff --git a/evidence/C5-ISOLATION/README.md b/evidence/C5-ISOLATION/README.md new file mode 100644 index 000000000..9f658b911 --- /dev/null +++ b/evidence/C5-ISOLATION/README.md @@ -0,0 +1,48 @@ +# C5: two processes, and a never-ending kernel (docs/OMEGA.md row C5) + +Row C5: two processes on one GPU are both correct, or the second gets a named refusal and the first is unharmed. A +never-ending kernel times out, then a correct launch succeeds in the same process. + +What this branch adds (on c4/streams): +- `omega_gpu_session_recover()`: the way back from a latch without restarting the process. Runs the on-close hooks + (still latched, so nothing is released while the old grid may run), closes the device (Linux reference: the RM client + close destroys this process's channels and the grid on them, then frees and unmaps every buffer), and only then lifts + the latch and clears the parked list. The next open is a new device. `omega_gpu_session_recoveries()` counts them; + a command list latched before the latest recovery no longer blocks. +- The Linux platform close now reports a driver teardown fault (it used to drop it): recover then stays latched for good + (the old grid may still run; the process must exit) instead of claiming success. +- Fake-platform cases TIMEOUT_RECOVER_SAME_PROCESS, RECOVER_TEARDOWN_FAULT_STAYS_LATCHED and four new session mutants + (recover keeps the latch, recover + without the close, recover when not latched, recover ignores a teardown fault): each one is caught. +- `tests/gpu_isolation_test.c`: the chip test (hang + recover; two processes; a hang beside a working process). + +Driver docs: the vendored NVIDIA headers document NVA06C_CTRL_CMD_PREEMPT ("This command preempts a channel group") but +are silent on freeing a channel group whose grid never ends (RM source not vendored). The chip run is the proof. + + +Review fixes (independent Opus review, 2026-10-22): +- The never-ending launch now passes one argument word (a launch with none is refused before submit, so the hang part + would never have hung). +- The two processes are started together after both are set up and each runs at least 3 s; their time spans must + overlap by at least half of that. Beside a hang, process 1 starts at process 2's "hang launched" signal and keeps + going until process 2 is done; it must have launches inside that stretch. +- "Grid destroyed" is not claimed from timings: it rests on the driver's clean close (a teardown fault keeps the + session latched, case RECOVER_TEARDOWN_FAULT_STAYS_LATCHED). +- A command list made before a recovery is refused (its buffers are gone); the chip test checks it. +- A command-list-only latch (check launch completed) is recoverable: omega_gpu_session_note_module_latch, case + RECOVER_MODULE_LATCH, mutant recover-ignores-module-latch. Session tests 16/16, mutants 11/11. +- C4 parts fixed here too (so merge c5/isolation, which contains c4/streams): a launch that waits always gets the full + WAIT_FOR_IDLE + invalidate; a list with waits runs only in submit_pair with the list it waits on. + +| file | what | result | +| --- | --- | --- | +| 01-host-only.log | codegen + encode of the work, mutant and never-ending kernels; simulator exact, mutant caught | PASS (5 checks), 2026-10-24T23:29Z on the train tip | +| 02-session-platform.log | the session on a fake GPU, both recover cases included | PASS 16/16, 2026-10-24T23:29Z on the train tip | +| 03-session-mutants.log | 11 broken copies of the session, each caught | 11/11 killed, 2026-10-24T23:29Z on the train tip | +| 05-platform-linux.log | the Linux platform on a simulated driver | 7/8 on the train tip (2026-10-24T23:29Z): the one failure, CMDLIST_NEAR_CAP_SUBMITS_WITH_L2_INVAL_PENDING, is main's own regression from omega-mv #712 (OMEGA_GPU_CMDLIST_MAX_LAUNCHES 2048 -> 4096; the test hard-codes 2 submissions, 4096 launches take 4). Plain main 9a8a0fe2 fails it the same way; the old head 5cb252994 passed 8/8. Fixer lane L823 (ledger) fixes the test on main. Nothing in this layer changes that case. | +| 04-never-ending-kernel.sass.txt | our disassembler: `IADD3; BRA 0x20` (branch back for ever) | as designed | +| 03-chip-values-20261022-f792cd1d.log | chip, values only, 2026-10-22 07:06Z on `c5/isolation` f792cd1d: header only (date, head, binary sha256, command). The worker died at 07:06Z before the test printed a line. NO RESULT. | NO RESULT | +| (chip, pending slot) | hang + recover + right values; two processes right; hang beside a working process, on the train tip (the never-ending-kernel run touches the live server's GPU: 1-second-spin precursor first, then only with Drake's or the server owner's OK) | NOT RUN | + +Chip command (GB10 lock): `build/gpu_isolation_test`. Safety note for the slot: the never-ending kernel is one warp; +its teardown is the RM channel free, which must preempt it; this is the first chip run of that path. diff --git a/mk/.crumb b/mk/.crumb index 5f40c5976..ed50fce79 100644 --- a/mk/.crumb +++ b/mk/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-24T21:31:17Z", + "compiled_at": "2026-10-24T23:29:24Z", "compiler": "crumb-compile/1", - "digest": "sha256:28ca50efe4c6219607f80199b70e50807127d121c66550511606dede047a6200", + "digest": "sha256:310f0ece1ecd090c470315f89d123d10031dfdb0b932d98b5e2c1cd9ed5fe0ca", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 73, - "generated_at_commit": "f96020434a127d774dd54dfd665a3b89a65bea5a", - "source_tree": "sha256:7427592fdbaf30bfa73c711fc727b1fa497690af23bb04e5222fb990cff46e7a" + "generated_at_commit": "14107ee4e7bde3b3e3430601d0f56be2ed3b33bc", + "source_tree": "sha256:00ca025f4879a4f83ca26e39f5ecf583331375a6a39c1a53b98647ba2d3ec9f9" }, "owner_lane": "", "provenance": { diff --git a/mk/omega-gpu-engine.mk b/mk/omega-gpu-engine.mk index db63683e4..0a8da68f2 100644 --- a/mk/omega-gpu-engine.mk +++ b/mk/omega-gpu-engine.mk @@ -228,4 +228,9 @@ test-gpu-session-mutants: $(GPU_FS_LINK) smut stale-not-counted 's|else { BUMP(g_stats.stale_refusals, 1u); fail_plat("dma_free", rc); }|else {}|'; \ smut marker-not-polled 's|RIGHTS_MARKER, PAGE|RIGHTS_DATA, PAGE|'; \ smut close-while-latched 's|if (g_open \&\& !LOAD(g_blocked) \&\& p) {|if (g_open \&\& p) {|; s|if (!LOAD(g_blocked)) { /\* a hook|if (1) { /* a hook|'; \ - echo "test-gpu-session-mutants: $$killed of 6 mutants killed" + smut recover-keeps-latch 's|STORE(g_blocked, 0u);|;|'; \ + smut recover-without-close 's|const int prc = p->device_close(p->ctx, g_s.dev);|const int prc = OMEGA_GPU_PLAT_OK;|'; \ + smut recover-when-not-latched 's|const int latched = LOAD(g_blocked) \|\| LOAD(g_module_latched);|const int latched = 1;|'; \ + smut recover-ignores-teardown-fault 's|if (prc != OMEGA_GPU_PLAT_OK) {|if (0) {|'; \ + smut recover-ignores-module-latch 's|const int latched = LOAD(g_blocked) \|\| LOAD(g_module_latched);|const int latched = LOAD(g_blocked);|'; \ + echo "test-gpu-session-mutants: $$killed of 11 mutants killed" diff --git a/physics.lock b/physics.lock index be6c1e32a..33b0f73db 100644 --- a/physics.lock +++ b/physics.lock @@ -1 +1 @@ -6d7cf0d4d8eb2cda7b512100ff6058e25dbb3ddf +048e5f5e881b092733baaf65d0ace46abda7f908 diff --git a/src/.crumb b/src/.crumb index 492b0dd03..76c7140bb 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:51Z", "compiler": "crumb-compile/1", - "digest": "sha256:4718c14bd4052ff822a315892aebc3c2229900b338191d98c95945b109be4e91", + "digest": "sha256:460466e3be802aaf74894fc2cbfe673caf81f0cebec86c10e2ab465365af3bd1", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 558, - "generated_at_commit": "0d72560ad82c657b9b51b62119781b36484e2bdc", - "source_tree": "sha256:2c08e2cc72d20b23284775f67a56a7c759fe03ad159060f06ad765e7b76d1526" + "files": 563, + "generated_at_commit": "fe8bf06c6185a99c25d7658614ccbc812b6df465", + "source_tree": "sha256:df4d6813fd94274116b2f485aac1bd8a27400cdef04dfc25ca1e9a9ff310056c" }, "owner_lane": "", "provenance": { diff --git a/src/omega_gpu_cmdlist.c b/src/omega_gpu_cmdlist.c index e7574d57c..809243d34 100644 --- a/src/omega_gpu_cmdlist.c +++ b/src/omega_gpu_cmdlist.c @@ -62,13 +62,34 @@ struct OmegaGpuCmdList { uint32_t nop_gpr; uint8_t *indep; /* per launch: 1 = shares no data with the launch before it (mark_independent): no barrier before it */ int stamped; /* the last submit had opts.stamps (the accessor refuses stale stamps from an earlier submit) */ + uint64_t *wait_va; /* per launch: semaphore word of another list's launch to wait for before it (wait_for), 0 = none */ + uint64_t pending_wait;/* wait_for recorded, taken by the next add */ + uint32_t nwaits; /* launches with a wait (6 more words each) */ + const OmegaGpuCmdList *wait_other; /* the one list this list waits on (it must be the other list of the pair) */ + uint32_t wait_max; /* highest launch index of wait_other waited on */ + uint32_t gen; /* omega_gpu_session_recoveries() at create: a list from before a recovery is stale (C5) */ }; -static uint32_t g_latched; +static uint32_t g_latched; /* 1 + omega_gpu_session_recoveries() at the latch; 0 never latched (C5: a recovery lifts it) */ -int omega_gpu_cmdlist_is_latched(void) { return __atomic_load_n(&g_latched, __ATOMIC_ACQUIRE) ? 1 : 0; } +int omega_gpu_cmdlist_is_latched(void) { return __atomic_load_n(&g_latched, __ATOMIC_ACQUIRE) == omega_gpu_session_recoveries() + 1u ? 1 : 0; } uint32_t omega_gpu_cmdlist_count(const OmegaGpuCmdList *cl) { return cl ? cl->n : 0; } -void omega_gpu_cmdlist_reset(OmegaGpuCmdList *cl) { if (cl) cl->n = 0; } +void omega_gpu_cmdlist_reset(OmegaGpuCmdList *cl) { if (cl) { cl->n = 0; cl->pending_wait = 0; cl->nwaits = 0; cl->wait_other = NULL; cl->wait_max = 0; } } +/* C5: every handle of a list made before the latest session recovery belongs to the closed device */ +static int stale(const OmegaGpuCmdList *cl) { + if (cl->gen == omega_gpu_session_recoveries()) return 0; + omega_gpu_session_set_error("cmdlist: made before a session recovery (its buffers are gone): create a new list"); + return 1; +} +int omega_gpu_cmdlist_wait_for(OmegaGpuCmdList *cl, const OmegaGpuCmdList *other, uint32_t index) { + if (!cl || !other || cl == other || index >= other->n) { omega_gpu_session_set_error("cmdlist wait_for: another list and one of its launches"); return -1; } + if (stale(cl) || stale(other)) return -1; + if (cl->wait_other && cl->wait_other != other) { omega_gpu_session_set_error("cmdlist wait_for: a list waits on one other list only"); return -1; } + cl->wait_other = other; + if (index > cl->wait_max) cl->wait_max = index; + cl->pending_wait = other->sems.va + (uint64_t)index * 16u; + return 0; +} int omega_gpu_cmdlist_mark_independent(OmegaGpuCmdList *cl) { if (!cl || cl->n == 0) { omega_gpu_session_set_error("cmdlist mark_independent: no launch recorded"); return -1; } cl->indep[cl->n - 1] = 1; @@ -101,10 +122,12 @@ int omega_gpu_cmdlist_create(uint32_t cap, OmegaGpuCmdList **out) { OmegaGpuCmdList *cl = omega_gpu_host_calloc(1, sizeof *cl); if (!cl) return -1; cl->cap = cap; + cl->gen = omega_gpu_session_recoveries(); cl->words_cap = RING_WORDS; cl->words = omega_gpu_host_alloc((size_t)cl->words_cap * 4u); cl->indep = omega_gpu_host_calloc(cap, 1); - int rc = cl->words && cl->indep ? 0 : -1; + cl->wait_va = omega_gpu_host_calloc(cap, sizeof *cl->wait_va); + int rc = cl->words && cl->indep && cl->wait_va ? 0 : -1; omega_gpu_session_lock(); if (rc == 0 && !omega_gpu_session_open()) rc = -1; if (rc == 0 && (omega_gpu_session_alloc((size_t)cap * OMEGA_GPU_CMDLIST_BANK_BYTES, &cl->banks) != 0 || @@ -132,6 +155,7 @@ void omega_gpu_cmdlist_free(OmegaGpuCmdList *cl) { omega_gpu_session_unlock(); omega_gpu_host_free(cl->words); omega_gpu_host_free(cl->indep); + omega_gpu_host_free(cl->wait_va); omega_gpu_host_free(cl); } @@ -140,6 +164,7 @@ static uint32_t round8(uint32_t v) { return (v + 7u) / 8u * 8u; } int omega_gpu_cmdlist_add(OmegaGpuCmdList *cl, const OmegaGpuLaunch *L) { if (!cl || !L || !L->args || L->n_args == 0 || L->n_args > OMEGA_GPU_SESSION_MAX_ARGS || L->threads_x == 0 || L->grid_x == 0 || L->grid_y == 0 || L->code_va == 0) { omega_gpu_session_set_error("cmdlist add: arguments"); return -1; } + if (stale(cl)) return -1; if (cl->n >= cl->cap) { omega_gpu_session_set_error("cmdlist add: list full"); return -1; } const uint32_t i = cl->n; const uint32_t ty = L->threads_y ? L->threads_y : 1; @@ -167,6 +192,9 @@ int omega_gpu_cmdlist_add(OmegaGpuCmdList *cl, const OmegaGpuLaunch *L) { memcpy(qs, q1, sizeof q1); memcpy(qs + 0x200, q0, sizeof q0); cl->indep[cl->n] = 0; + cl->wait_va[cl->n] = cl->pending_wait; + if (cl->pending_wait) cl->nwaits++; + cl->pending_wait = 0; cl->n++; return (int)i; } @@ -203,11 +231,12 @@ static int wait_word(const OmegaGpuPlatform *p, const OmegaGpuMem *marker, uint6 /* Uncertain completion: latch this module, then queue a 1 ms session launch behind the stuck list. If the channel is * still busy it times out and the session latches the whole process (parks every buffer, refuses every API). */ static void force_latch(OmegaGpuCmdList *cl, const char *why) { - __atomic_store_n(&g_latched, 1u, __ATOMIC_RELEASE); + __atomic_store_n(&g_latched, omega_gpu_session_recoveries() + 1u, __ATOMIC_RELEASE); uint32_t arg = 0; OmegaGpuLaunch L = { .code_va = cl->nop_code.va, .gpr_count = cl->nop_gpr, .threads_x = 32, .threads_y = 1, .grid_x = 1, .grid_y = 1, .num_elements = 1, .args = &arg, .n_args = 1, .timeout_ms = 1 }; (void)omega_gpu_session_launch(&L, NULL, NULL); + omega_gpu_session_note_module_latch(); /* C5: so omega_gpu_session_recover lifts it even when the check launch completed */ omega_gpu_session_set_error(why); } @@ -244,12 +273,70 @@ static size_t emit_inline_qmd(uint32_t *pb, size_t n, uint64_t va, const uint32_ memcpy(&pb[n], words, 96 * 4); return n + 96; } +/* One submission's method stream for launches [done, end) into cl->words, ending with the completion tail at marker_va. + * Shared by submit and submit_pair (L136) so both emit the same words. Returns the word count. */ +static size_t emit_chunk(OmegaGpuCmdList *cl, const OmegaGpuCmdListOpts *op, uint32_t done, uint32_t end, uint64_t marker_va) { + const OmegaGpuCmdListOpts o = *op; + uint32_t *pb = cl->words; + size_t n = 0; + memcpy(pb, SETUP_WORDS, sizeof SETUP_WORDS); n += 18; + for (uint32_t i = done; i < end; i++) { + if (cl->wait_va[i]) { + /* wait_for: host semaphore ACQUIRE (clc96f.h SEM_ADDR_LO 0x5c .. SEM_EXECUTE 0x6c, OPERATION_ACQUIRE 0, 32-bit + * payload; the omega-mv#580 ORDER_SEM words, chip-proven): this queue stops here until the other list's launch + * released DONE. Before the barrier, so the launch's cache invalidate comes after the wait. */ + const uint64_t va = cl->wait_va[i]; + pb[n++] = bw_mthd(0, 0x005c, 5); pb[n++] = (uint32_t)va; pb[n++] = (uint32_t)(va >> 32); + pb[n++] = OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE; pb[n++] = 0; pb[n++] = 0; + } + if (o.skip_launch && i == (uint32_t)(o.skip_launch - 1)) continue; + /* the first launch of a submission gets the full barrier (CPU-written QMDs, code and banks before it), unless + * opts.first_barrier_none (C4 streams measurement: a WAIT_FOR_IDLE may also wait for another queue's grids) */ + const int indep = i != done && cl->indep[i]; /* marked independent: no barrier, overlaps the launch before it */ + /* a launch that waited (wait_for) always gets the full barrier: its invalidate must come after the wait */ + const OmegaGpuCmdListBarrier b = cl->wait_va[i] ? OMEGA_GPU_CMDLIST_BARRIER_WFI + : i == done ? (i == 0 && o.first_barrier_none ? OMEGA_GPU_CMDLIST_BARRIER_NONE : OMEGA_GPU_CMDLIST_BARRIER_WFI) + : indep ? OMEGA_GPU_CMDLIST_BARRIER_NONE : o.barrier; + if (indep && o.stamps) n = emit_stamp(pb, n, cl->stamps.va, i); /* no WFI: slot i is the front-end time, launch i - 1 may still run */ + if (b != OMEGA_GPU_CMDLIST_BARRIER_NONE) { + if (b != OMEGA_GPU_CMDLIST_BARRIER_FLUSH_DATA) { pb[n++] = bw_mthd(1, MTHD_WAIT_FOR_IDLE, 1); pb[n++] = 0; } + else { pb[n++] = bw_mthd(1, OMEGA_BW_MTHD_INVALIDATE_SHADER_CACHES, 1); pb[n++] = INVALIDATE_DATA | INVALIDATE_FLUSH_DATA; } /* implicit WFI (NVK) */ + if (o.stamps) n = emit_stamp(pb, n, cl->stamps.va, i); /* slot i: launch i - 1 done (slot `done`: submit start) */ + if (b == OMEGA_GPU_CMDLIST_BARRIER_WFI) { pb[n++] = bw_mthd(1, OMEGA_BW_MTHD_INVALIDATE_SHADER_CACHES, 1); pb[n++] = OMEGA_BW_INVALIDATE_SHADER_CACHES_ALL; } + else if (b == OMEGA_GPU_CMDLIST_BARRIER_WFI_DATA) { pb[n++] = bw_mthd(1, OMEGA_BW_MTHD_INVALIDATE_SHADER_CACHES, 1); pb[n++] = INVALIDATE_DATA; } + } + const uint64_t q1_va = cl->qmds.va + (uint64_t)i * OMEGA_GPU_CMDLIST_QMD_BYTES; + if (o.mode == OMEGA_GPU_CMDLIST_INLINE) { + const uint8_t *qs = (const uint8_t *)cl->qmds.cpu + (size_t)i * OMEGA_GPU_CMDLIST_QMD_BYTES; + uint32_t q1[OMEGA_BW_QMD_WORDS]; + memcpy(q1, qs, sizeof q1); + q1[10] = 0x00190000u; q1[12] = (uint32_t)((q1_va + 0x200) >> 8); /* the session pair: QMD1 decrements QMD0 */ + n = emit_inline_qmd(pb, n, q1_va + 0x200, (const uint32_t *)(qs + 0x200)); + n = emit_inline_qmd(pb, n, q1_va, q1); + } else { + /* standalone grid: no dependent QMD (NVK builds Blackwell QMDs without one, nak/qmd.rs Qmd5_0) */ + uint32_t *q1 = (uint32_t *)((uint8_t *)cl->qmds.cpu + (size_t)i * OMEGA_GPU_CMDLIST_QMD_BYTES); + q1[10] = 0; q1[12] = 0; + pb[n++] = bw_mthd(1, MTHD_SEND_PCAS_A, 1); pb[n++] = (uint32_t)(q1_va >> 8); + pb[n++] = bw_mthd(1, MTHD_SEND_SIGNALING_PCAS2_B, 1); pb[n++] = PCAS_ACTION_INVALIDATE_COPY_SCHEDULE; + } + } + if (o.stamps) { + pb[n++] = bw_mthd(1, MTHD_WAIT_FOR_IDLE, 1); pb[n++] = 0; + n = emit_stamp(pb, n, cl->stamps.va, end); /* slot end: the last launch done */ + } + n = emit_tail(pb, n, marker_va); + return n; +} + int omega_gpu_cmdlist_submit(OmegaGpuCmdList *cl, const OmegaGpuCmdListOpts *opts, OmegaGpuCmdListStats *st) { OmegaGpuCmdListOpts o; memset(&o, 0, sizeof o); if (opts) o = *opts; if (st) { memset(st, 0, sizeof *st); st->first_missing = -1; } const OmegaGpuPlatform *p = omega_gpu_session_platform(); if (!cl || cl->n == 0 || !p) { omega_gpu_session_set_error("cmdlist submit: empty list or no platform"); return -1; } + if (stale(cl)) return -1; + if (cl->nwaits) { omega_gpu_session_set_error("cmdlist submit: a list with waits (wait_for) runs only in a pair (submit_pair)"); return -1; } if (omega_gpu_cmdlist_is_latched() || omega_gpu_session_is_blocked()) { omega_gpu_session_set_error("cmdlist submit: latched"); return -1; } OmegaGpuSession *s = omega_gpu_session_open(); if (!s) return -1; @@ -272,7 +359,7 @@ int omega_gpu_cmdlist_submit(OmegaGpuCmdList *cl, const OmegaGpuCmdListOpts *opt omega_gpu_session_set_error("cmdlist submit: sync_cpu_to_gpu"); return -1; } - const uint32_t per_launch = (o.mode == OMEGA_GPU_CMDLIST_INLINE ? 4u + 2u * 99u : 8u) + (o.stamps ? STAMP_WORDS : 0u); + const uint32_t per_launch = (o.mode == OMEGA_GPU_CMDLIST_INLINE ? 4u + 2u * 99u : 8u) + (o.stamps ? STAMP_WORDS : 0u) + (cl->nwaits ? 6u : 0u); /* The platform may put its own words ahead of ours in the ring (the Linux reference: the omega #457 L2 invalidate * after a host write to a GPU-cached buffer). Budget what it publishes, or a near-full submission is refused. */ const uint32_t prefix = omega_gpu_session_submit_prefix_words(); @@ -291,44 +378,9 @@ int omega_gpu_cmdlist_submit(OmegaGpuCmdList *cl, const OmegaGpuCmdListOpts *opt uint32_t done = 0, submits = 0, words = 0; while (done < cl->n) { const uint64_t tb = p->time_now_ns(p->ctx); - uint32_t *pb = cl->words; - size_t n = 0; - memcpy(pb, SETUP_WORDS, sizeof SETUP_WORDS); n += 18; const uint32_t end = (cl->n - done > room) ? done + room : cl->n; - for (uint32_t i = done; i < end; i++) { - if (o.skip_launch && i == (uint32_t)(o.skip_launch - 1)) continue; - /* the first launch of a submission always gets the full barrier (CPU-written QMDs, code and banks before it) */ - const int indep = i != done && cl->indep[i]; /* marked independent: no barrier, overlaps the launch before it */ - const OmegaGpuCmdListBarrier b = i == done ? OMEGA_GPU_CMDLIST_BARRIER_WFI : indep ? OMEGA_GPU_CMDLIST_BARRIER_NONE : o.barrier; - if (indep && o.stamps) n = emit_stamp(pb, n, cl->stamps.va, i); /* no WFI: slot i is the front-end time, launch i - 1 may still run */ - if (b != OMEGA_GPU_CMDLIST_BARRIER_NONE) { - if (b != OMEGA_GPU_CMDLIST_BARRIER_FLUSH_DATA) { pb[n++] = bw_mthd(1, MTHD_WAIT_FOR_IDLE, 1); pb[n++] = 0; } - else { pb[n++] = bw_mthd(1, OMEGA_BW_MTHD_INVALIDATE_SHADER_CACHES, 1); pb[n++] = INVALIDATE_DATA | INVALIDATE_FLUSH_DATA; } /* implicit WFI (NVK) */ - if (o.stamps) n = emit_stamp(pb, n, cl->stamps.va, i); /* slot i: launch i - 1 done (slot `done`: submit start) */ - if (b == OMEGA_GPU_CMDLIST_BARRIER_WFI) { pb[n++] = bw_mthd(1, OMEGA_BW_MTHD_INVALIDATE_SHADER_CACHES, 1); pb[n++] = OMEGA_BW_INVALIDATE_SHADER_CACHES_ALL; } - else if (b == OMEGA_GPU_CMDLIST_BARRIER_WFI_DATA) { pb[n++] = bw_mthd(1, OMEGA_BW_MTHD_INVALIDATE_SHADER_CACHES, 1); pb[n++] = INVALIDATE_DATA; } - } - const uint64_t q1_va = cl->qmds.va + (uint64_t)i * OMEGA_GPU_CMDLIST_QMD_BYTES; - if (o.mode == OMEGA_GPU_CMDLIST_INLINE) { - const uint8_t *qs = (const uint8_t *)cl->qmds.cpu + (size_t)i * OMEGA_GPU_CMDLIST_QMD_BYTES; - uint32_t q1[OMEGA_BW_QMD_WORDS]; - memcpy(q1, qs, sizeof q1); - q1[10] = 0x00190000u; q1[12] = (uint32_t)((q1_va + 0x200) >> 8); /* the session pair: QMD1 decrements QMD0 */ - n = emit_inline_qmd(pb, n, q1_va + 0x200, (const uint32_t *)(qs + 0x200)); - n = emit_inline_qmd(pb, n, q1_va, q1); - } else { - /* standalone grid: no dependent QMD (NVK builds Blackwell QMDs without one, nak/qmd.rs Qmd5_0) */ - uint32_t *q1 = (uint32_t *)((uint8_t *)cl->qmds.cpu + (size_t)i * OMEGA_GPU_CMDLIST_QMD_BYTES); - q1[10] = 0; q1[12] = 0; - pb[n++] = bw_mthd(1, MTHD_SEND_PCAS_A, 1); pb[n++] = (uint32_t)(q1_va >> 8); - pb[n++] = bw_mthd(1, MTHD_SEND_SIGNALING_PCAS2_B, 1); pb[n++] = PCAS_ACTION_INVALIDATE_COPY_SCHEDULE; - } - } - if (o.stamps) { - pb[n++] = bw_mthd(1, MTHD_WAIT_FOR_IDLE, 1); pb[n++] = 0; - n = emit_stamp(pb, n, cl->stamps.va, end); /* slot end: the last launch done */ - } - n = emit_tail(pb, n, marker_va); + uint32_t *pb = cl->words; + size_t n = emit_chunk(cl, &o, done, end, marker_va); if (o.mode != OMEGA_GPU_CMDLIST_INLINE && p->sync_cpu_to_gpu(p->ctx, cl->qmds.handle, 0, (uint64_t)cl->n * OMEGA_GPU_CMDLIST_QMD_BYTES) != OMEGA_GPU_PLAT_OK) { omega_gpu_session_set_error("cmdlist submit: sync qmd"); return -1; @@ -360,6 +412,99 @@ int omega_gpu_cmdlist_submit(OmegaGpuCmdList *cl, const OmegaGpuCmdListOpts *opt return 0; } +/* L136: list a on the session queue and list b on extra queue 1, both in flight before either is waited. Each list gets + * the same prefix as submit (semaphores INIT, banks / QMDs / semaphores synced) and the same words (emit_chunk); b's + * completion markers sit at MARKER_OFF + PAIR_B_MARKER on the same uncached page. */ +#define PAIR_B_MARKER 0x80ull +static int pair_prep(const OmegaGpuPlatform *p, OmegaGpuCmdList *cl) { + uint32_t *sem = cl->sems.cpu; + for (uint32_t i = 0; i < cl->n; i++) sem[4 * i] = OMEGA_BW_SEMAPHORE_INTERMEDIATE_INIT; + cl->stamped = 0; + return p->sync_cpu_to_gpu(p->ctx, cl->banks.handle, 0, (uint64_t)cl->n * OMEGA_GPU_CMDLIST_BANK_BYTES) == OMEGA_GPU_PLAT_OK && + p->sync_cpu_to_gpu(p->ctx, cl->sems.handle, 0, (uint64_t)cl->n * 16u) == OMEGA_GPU_PLAT_OK ? 0 : -1; +} + +static int pair_check(const OmegaGpuPlatform *p, OmegaGpuCmdList *cl, uint32_t nwords, uint64_t elapsed, uint64_t host, OmegaGpuCmdListStats *st) { + (void)p->sync_gpu_to_cpu(p->ctx, cl->sems.handle, 0, (uint64_t)cl->n * 16u); + const uint32_t *sem = cl->sems.cpu; + uint32_t ok = 0; int32_t missing = -1; + for (uint32_t i = 0; i < cl->n; i++) { + if (sem[4 * i] == OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE) ok++; + else if (missing < 0) missing = (int32_t)i; + } + if (st) { st->launches = cl->n; st->submits = 1; st->words = nwords; st->elapsed_ns = elapsed; st->host_ns = host; st->sems_done = ok; st->first_missing = missing; } + if (missing >= 0) { omega_gpu_session_set_error("cmdlist pair: a launch semaphore is not DONE (launch did not run)"); return -1; } + return 0; +} + +/* Uncertain completion of a pair: queue 1 may still run, so a nop on queue 0 proves nothing; latch the session directly. */ +static void pair_latch(const char *why) { + __atomic_store_n(&g_latched, omega_gpu_session_recoveries() + 1u, __ATOMIC_RELEASE); + omega_gpu_session_latch_uncertain(why); +} + +int omega_gpu_cmdlist_submit_pair(OmegaGpuCmdList *a, OmegaGpuCmdList *b, const OmegaGpuCmdListOpts *opts, OmegaGpuCmdListStats *sa, OmegaGpuCmdListStats *sb) { + OmegaGpuCmdListOpts o; memset(&o, 0, sizeof o); + if (opts) o = *opts; + if (sa) { memset(sa, 0, sizeof *sa); sa->first_missing = -1; } + if (sb) { memset(sb, 0, sizeof *sb); sb->first_missing = -1; } + const OmegaGpuPlatform *p = omega_gpu_session_platform(); + if (!a || !b || a == b || a->n == 0 || b->n == 0 || !p) { omega_gpu_session_set_error("cmdlist pair: two distinct non-empty lists and a platform"); return -1; } + if (stale(a) || stale(b)) return -1; + for (int k = 0; k < 2; k++) { /* a wait must point into the other list of this pair, at a launch it still has */ + const OmegaGpuCmdList *me = k ? b : a, *peer = k ? a : b; + if (me->nwaits && (me->wait_other != peer || me->wait_max >= peer->n)) { + omega_gpu_session_set_error("cmdlist pair: a wait_for points outside the other list of this pair"); return -1; + } + } + if (o.mode != OMEGA_GPU_CMDLIST_PCAS || o.stamps || o.skip_launch) { omega_gpu_session_set_error("cmdlist pair: PCAS mode only, no stamps, no skip"); return -1; } + if (omega_gpu_cmdlist_is_latched() || omega_gpu_session_is_blocked()) { omega_gpu_session_set_error("cmdlist pair: latched"); return -1; } + OmegaGpuSession *s = omega_gpu_session_open(); + if (!s) return -1; + if (!s->marker.cpu || s->marker.size < MARKER_OFF + PAIR_B_MARKER + 0x20) { omega_gpu_session_set_error("cmdlist pair: session marker page"); return -1; } + OmegaGpuQueueHandle q1; + if (omega_gpu_session_extra_queue(1, &q1) != 0) return -1; /* names the refusing stage */ + const OmegaGpuMem *marker = &s->marker; + const uint64_t marker_va = marker->va + MARKER_OFF; + const uint64_t timeout = o.timeout_ms ? o.timeout_ms : DEFAULT_TIMEOUT_MS; + const uint32_t spin = o.spin_us ? o.spin_us : omega_gpu_session_spin_us(); + const uint64_t th0 = p->time_now_ns(p->ctx); + /* one submission per list: both must fit their ring with the platform prefix (a q38 token is ~650 launches x 8 words) */ + const uint32_t prefix = omega_gpu_session_submit_prefix_words(); + const OmegaGpuCmdList *L[2] = { a, b }; + for (int k = 0; k < 2; k++) { + const uint32_t avail = L[k]->words_cap - 18u - TAIL_WORDS - 2u - STAMP_WORDS; + if (prefix > avail || (avail - prefix) / (L[k]->nwaits ? 14u : 8u) < L[k]->n) { omega_gpu_session_set_error("cmdlist pair: a list does not fit one submission"); return -1; } + } + if (pair_prep(p, a) || pair_prep(p, b)) { omega_gpu_session_set_error("cmdlist pair: sync_cpu_to_gpu"); return -1; } + const size_t na = emit_chunk(a, &o, 0, a->n, marker_va), nb = emit_chunk(b, &o, 0, b->n, marker_va + PAIR_B_MARKER); + volatile uint32_t *mk = (volatile uint32_t *)((uint8_t *)marker->cpu + MARKER_OFF); + mk[0] = 0; mk[4] = 0; mk[PAIR_B_MARKER / 4] = 0; mk[PAIR_B_MARKER / 4 + 4] = 0; + if (p->sync_cpu_to_gpu(p->ctx, a->qmds.handle, 0, (uint64_t)a->n * OMEGA_GPU_CMDLIST_QMD_BYTES) != OMEGA_GPU_PLAT_OK || + p->sync_cpu_to_gpu(p->ctx, b->qmds.handle, 0, (uint64_t)b->n * OMEGA_GPU_CMDLIST_QMD_BYTES) != OMEGA_GPU_PLAT_OK || + p->sync_cpu_to_gpu(p->ctx, marker->handle, MARKER_OFF, PAIR_B_MARKER + 0x20) != OMEGA_GPU_PLAT_OK) { + omega_gpu_session_set_error("cmdlist pair: sync qmd / marker"); return -1; + } + const uint64_t t0 = p->time_now_ns(p->ctx); + const uint64_t host = t0 - th0; + if (p->queue_submit(p->ctx, s->queue, a->words, (uint32_t)na) != OMEGA_GPU_PLAT_OK) { + omega_gpu_session_set_error("cmdlist pair: queue_submit (queue 0) refused"); return -1; /* nothing in flight */ + } + const int b_ok = p->queue_submit(p->ctx, q1, b->words, (uint32_t)nb) == OMEGA_GPU_PLAT_OK; + int w = wait_word(p, marker, 0, OMEGA_BW_MARKER_COMPLETION_PAYLOAD, timeout, spin); + if (w == WAIT_OK) w = wait_word(p, marker, 0x10, OMEGA_BW_MARKER2_PAYLOAD, timeout, spin); + if (w == WAIT_OK && b_ok) w = wait_word(p, marker, PAIR_B_MARKER, OMEGA_BW_MARKER_COMPLETION_PAYLOAD, timeout, spin); + if (w == WAIT_OK && b_ok) w = wait_word(p, marker, PAIR_B_MARKER + 0x10, OMEGA_BW_MARKER2_PAYLOAD, timeout, spin); + if (w != WAIT_OK) { pair_latch(w == WAIT_TIMED_OUT ? "cmdlist pair: marker wait timed out (latched)" : "cmdlist pair: marker poll refused (latched)"); return -1; } + (void)p->sync_gpu_to_cpu(p->ctx, marker->handle, MARKER_OFF, PAIR_B_MARKER + 0x20); + const uint64_t elapsed = p->time_now_ns(p->ctx) - t0; + (void)p->queue_retire(p->ctx, s->queue); + if (!b_ok) { omega_gpu_session_set_error("cmdlist pair: queue_submit (queue 1) refused"); return -1; } /* a completed and is retired */ + (void)p->queue_retire(p->ctx, q1); + const int ra = pair_check(p, a, (uint32_t)na, elapsed, host, sa), rb = pair_check(p, b, (uint32_t)nb, elapsed, host, sb); + return ra == 0 && rb == 0 ? 0 : -1; +} + int omega_gpu_cmdlist_stamps(const OmegaGpuCmdList *cl, uint64_t *out, uint32_t cap) { if (!cl || !out || !cl->stamps.cpu) { omega_gpu_session_set_error("cmdlist stamps: arguments"); return -1; } if (!cl->stamped) { omega_gpu_session_set_error("cmdlist stamps: the last submit had stamps off"); return -1; } diff --git a/src/omega_gpu_cmdlist.h b/src/omega_gpu_cmdlist.h index c534c71d6..889bdd1e4 100644 --- a/src/omega_gpu_cmdlist.h +++ b/src/omega_gpu_cmdlist.h @@ -82,6 +82,9 @@ typedef struct { back by omega_gpu_cmdlist_stamps. Costs 6 words + the report per launch. 0 = off. */ int skip_launch; /* TEST ONLY: >= 1 drops launch (skip_launch - 1) from the method stream (its slot stays recorded), so the per-launch semaphore check must fail. 0 = off. */ + int first_barrier_none; /* C4 streams: no WAIT_FOR_IDLE + invalidate before launch 0 (the CPU-written code, banks and QMDs + must not sit in a stale cache line: fresh slots or unchanged contents). A WAIT_FOR_IDLE may also + wait for grids of another queue of the group; the streams test measures both. 0 = the barrier. */ } OmegaGpuCmdListOpts; typedef struct { @@ -96,7 +99,8 @@ typedef struct { typedef struct OmegaGpuCmdList OmegaGpuCmdList; -/* Takes the session lock itself. Allocates the slots for up to cap launches. 0 ok, -1 (session error names it). */ +/* Takes the session lock itself. Allocates the slots for up to cap launches. 0 ok, -1 (session error names it). A list + * belongs to the device it was made on: after omega_gpu_session_recover, add / submit / wait_for refuse it (C5). */ int omega_gpu_cmdlist_create(uint32_t cap, OmegaGpuCmdList **out); void omega_gpu_cmdlist_free(OmegaGpuCmdList *cl); /* Caller holds the lock. Empties the list (slots are rewritten by the next adds). */ @@ -114,7 +118,20 @@ int omega_gpu_cmdlist_add(OmegaGpuCmdList *cl, const OmegaGpuLaunch *L); /* Caller holds the lock, device open, not latched. Submits every recorded launch in order, waits for the completion, * checks every launch semaphore. 0 ok; -1 with the stage named (omega_gpu_session_last_error). */ int omega_gpu_cmdlist_submit(OmegaGpuCmdList *cl, const OmegaGpuCmdListOpts *opts, OmegaGpuCmdListStats *st); -/* 1 once a timeout latched this module. */ +/* L136 (roadblock R3): caller holds the lock. List a on the session queue and list b on extra queue 1 + * (omega_gpu_session_extra_queue; needs AIEN_OMEGA_GPU_CHANNELS >= 2 at device open), both submitted before either is + * waited, so the two may run at the same time. The lists must share no written buffer. PCAS mode, no stamps, each list in + * one submission. Both stats get the pair wall time (first submit to the last marker). 0 ok; -1 with the stage named; + * a marker timeout latches the session. */ +int omega_gpu_cmdlist_submit_pair(OmegaGpuCmdList *a, OmegaGpuCmdList *b, const OmegaGpuCmdListOpts *opts, OmegaGpuCmdListStats *sa, OmegaGpuCmdListStats *sb); +/* C4 events. Caller holds the lock. The next launch added to cl waits, on the chip, until launch `index` of list `other` + * (already added there) has completed: a host semaphore ACQUIRE on that launch's release semaphore (DONE), emitted + * before the launch's barrier, which is always the full WAIT_FOR_IDLE + invalidate (whatever the options say), so the + * launch reads what the other launch wrote. A list waits on one other list only, and a list with waits runs only in + * submit_pair with that list (submit refuses it; submit_pair refuses a wait outside the pair or past its launches). + * 0 ok, -1 (no such launch, a second other list, or a list made before a session recovery). */ +int omega_gpu_cmdlist_wait_for(OmegaGpuCmdList *cl, const OmegaGpuCmdList *other, uint32_t index); +/* 1 once a timeout latched this module, until omega_gpu_session_recover (C5). */ int omega_gpu_cmdlist_is_latched(void); /* After a submit with opts.stamps = 1: out[0] = GPU timestamp (ns, PTIMER) before launch 0, out[i + 1] = after launch i completed diff --git a/src/omega_gpu_gemm.c b/src/omega_gpu_gemm.c new file mode 100644 index 000000000..42b47a7d6 --- /dev/null +++ b/src/omega_gpu_gemm.c @@ -0,0 +1,381 @@ +/* omega_gpu_gemm.c -- matrix multiply of any shape on the GB10 tensor cores (C3). See the header. */ +#include "omega_gpu_gemm.h" +#include "omega_bw_em.h" +#include "omega_bw_em_q.h" +#include "omega_gpu_session.h" + +#include +#include + +#define POISON 0x7fa00badu /* a signalling NaN no tensor-core op writes: a word still holding it was never stored */ +#define TIMEOUT_MS 60000ull +#define FRAG 512u /* bytes of one warp fragment: 32 lanes x 16 */ + +static uint32_t esz(OmegaGpuGemmType t) { return t == OMEGA_GPU_GEMM_BF16 ? 2u : 1u; } +static uint32_t kstep(OmegaGpuGemmType t) { return t == OMEGA_GPU_GEMM_BF16 ? 16u : 32u; } +static int pow124(uint32_t v) { return v == 1u || v == 2u || v == 4u; } + +void omega_gpu_gemm_default_cfg(OmegaGpuGemmType type, OmegaGemmCfg *cfg) { + memset(cfg, 0, sizeof *cfg); + cfg->type = type; cfg->rt = 2; cfg->ct = 4; cfg->wm = 2; cfg->wn = 2; cfg->stall = type == OMEGA_GPU_GEMM_BF16 ? 4u : 2u; +} + +int omega_gpu_gemm_cfg_ok(const OmegaGemmCfg *c) { + return c && (c->type == OMEGA_GPU_GEMM_BF16 || c->type == OMEGA_GPU_GEMM_E4M3) && pow124(c->rt) && pow124(c->ct) && + pow124(c->wm) && pow124(c->wn) && c->stall >= 1u && c->stall <= 15u && c->mutant >= 0 && c->mutant <= 3 && + 4u * (4u * c->rt + 2u * c->ct + c->rt * c->ct) <= 100u; /* registers: two operand sets + accumulators, R2..R127 in all */ +} + +/* ------------------------------------------------------------------ IR helpers */ + +typedef struct { int bssy, bra; } Region; +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); } +/* d = a - b */ +static void isub(Em *e, int d, int a, int b) { imadi(e, d, b, 0, 0xffffffffu, a); } +/* d = 1 when a < b (both below 2^31), else 0 */ +static void lt_bit(Em *e, int d, int a, int b) { isub(e, d, a, b); shr(e, d, d, 0, 31); } +static void and_r(Em *e, int d, int a, int b) { lop3(e, d, a, b, NOREG, 0xc0u); } +/* tensor-core op d = a * b[bsub..bsub+1] + c */ +static void mma(Em *e, int fp8, int d, int a, int b, unsigned bsub, int c, uint32_t stall) { + BlackwellIRInsn i = I0(BW_IR_HMMA_BF16); + i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src2_subreg = (uint8_t)bsub; i.src3_vreg = c; + if (fp8) i.imm = OMEGA_QMMA_MARK; + i.control = CW(stall, SB_NONE, SB_NONE, e->pending); e->pending = 0; + if (omega_bw_ir_append(e->p, &i) < 0) e->err = -1; +} + +/* ------------------------------------------------------------------ PACK_A / PACK_B + * CTA (pair cx, row group cy), 128 threads; thread tid writes word tid of each of the F fragments of the block: + * lane L = tid / 4, register q = tid % 4, g = L / 4, t = L % 4, r = elements per register (2 bf16, 4 e4m3). + * A fragment f = u * rt + i: row 16 (rt cy + i) + g + 8 (q & 1), k = 2 step cx + step u + r t + 4 r (q >> 1) + * B fragment f = j: row 8 (ct cy + j) + g, k = 2 step cx + step (q >> 1) + r t + 4 r (q & 1) + * Element (row, k) of the source is at (row * row_stride + k * k_stride) * esz; past rows or kdim it is 0. */ +static int gen_pack(Em *e, const OmegaGemmCfg *c, int isb) { + const int fp8 = c->type == OMEGA_GPU_GEMM_E4M3; + const uint32_t ks = kstep(c->type), r = 4u / esz(c->type), F = isb ? c->ct : 2u * c->rt; + int udesc = UV64(e), psrc = V64(e), pdst = V64(e), R = V(e), Kd = V(e), sr = V(e), sk = V(e), ks2 = V(e); + int tid = V(e), cx = V(e), cy = V(e), one = V(e), g = V(e), t = V(e), qa = V(e), qb = V(e), row0 = V(e), k0 = V(e); + int tmp = V(e), pd = V64(e), row = V(e), vrow = V(e), rowoff = V(e), kx = V(e), v = V(e), off = V(e), word = V(e); + int addr[4], val[4], mask[4], odd[4]; + for (unsigned x = 0; x < 4; x++) { addr[x] = V64(e); val[x] = V(e); mask[x] = V(e); odd[x] = V(e); } + if (e->err) return -1; + ldcu64(e, udesc, 0x358); + ldc64(e, psrc, 0x380); ldc64(e, pdst, 0x388); + ldc32(e, R, 0x390); ldc32(e, Kd, 0x394); ldc32(e, sr, 0x398); ldc32(e, sk, 0x39c); ldc32(e, ks2, 0x3a0); + s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y); + movi(e, one, 1); + shr(e, g, tid, 0, 4); + shr(e, t, tid, 0, 2); andi(e, t, t, 0, 3); + andi(e, qa, tid, 0, 1); + shr(e, qb, tid, 0, 1); andi(e, qb, qb, 0, 1); + if (!isb) { + imadi(e, row0, cy, 0, 16u * c->rt, g); imadi(e, row0, qa, 0, 8u, row0); + imadi(e, k0, cx, 0, 2u * ks, NOREG); imadi(e, k0, t, 0, r, k0); imadi(e, k0, qb, 0, 4u * r, k0); + } else { + imadi(e, row0, cy, 0, 8u * c->ct, g); + imadi(e, k0, cx, 0, 2u * ks, NOREG); imadi(e, k0, t, 0, r, k0); imadi(e, k0, qb, 0, ks, k0); imadi(e, k0, qa, 0, 4u * r, k0); + } + /* this thread's word of fragment 0: ((cy * KS2 + cx) * F * 128 + tid) words */ + imadr(e, tmp, cy, ks2, cx); imadi(e, tmp, tmp, 0, F * 128u, tid); imadw(e, pd, tmp, 4u, pdst); + for (uint32_t f = 0; f < F; f++) { + const uint32_t drow = isb ? 8u * f : 16u * (f % c->rt), dk = isb ? 0u : ks * (f / c->rt); + imadi(e, row, one, 0, drow, row0); + lt_bit(e, vrow, row, R); + imadr(e, rowoff, row, sr, NOREG); + for (uint32_t x = 0; x < r; x++) { + imadi(e, kx, one, 0, dk + x, k0); + lt_bit(e, v, kx, Kd); + and_r(e, v, v, vrow); + imadr(e, off, kx, sk, rowoff); + imadr(e, off, off, v, NOREG); /* outside: element 0, always readable, then masked */ + if (c->mutant == 2) movi(e, mask[x], 0xffffffffu); /* the red check: element 0 leaks in past the edges */ + else imadi(e, mask[x], v, 0, 0xffffffffu, NOREG); + if (fp8) { andi(e, odd[x], off, 0, 1u); andi(e, off, off, 0, 0xfffffffeu); imadw(e, addr[x], off, 1u, psrc); } + else imadw(e, addr[x], off, 2u, psrc); + } + for (uint32_t x = 0; x < r; x++) ldg_wide(e, BW_IR_LDG_E_U16, val[x], addr[x], 0, udesc, x == 0); + for (uint32_t x = 0; x < r; x++) { + if (fp8) { /* the byte at off: low or high half of the u16 */ + andi(e, kx, val[x], 0, 0xffu); + shr(e, v, val[x], 0, 8); andi(e, v, v, 0, 0xffu); + isub(e, v, v, kx); + imadr(e, val[x], v, odd[x], kx); + } + and_r(e, val[x], val[x], mask[x]); + } + imadi(e, word, val[0], 0, 1u, NOREG); + for (uint32_t x = 1; x < r; x++) imadi(e, word, val[x], 0, 1u << (x * (32u / r)), word); + stg(e, pd, f * FRAG, word, udesc); + } + tail(e); + return e->err; +} + +/* ------------------------------------------------------------------ MAIN */ +static int gen_main(Em *e, const OmegaGemmCfg *c) { + const int fp8 = c->type == OMEGA_GPU_GEMM_E4M3; + const uint32_t RT = c->rt, CT = c->ct, FA = 2u * RT, FB = CT; + int udesc = UV64(e), pa = V64(e), pb = V64(e), pc = V64(e), M = V(e), N = V(e), ks2 = V(e); + int tid = V(e), cx = V(e), cy = V(e), warp = V(e), lane = V(e), g = V(e), t = V(e), one = V(e), zr = V(e); + int rga = V(e), rgb = V(e), tmp = V(e), cnt = V(e); + int acc[16], as[2][8], bs[2][4]; + for (uint32_t x = 0; x < RT * CT; x++) acc[x] = V128(e); + for (unsigned s = 0; s < 2; s++) { for (uint32_t f = 0; f < FA; f++) as[s][f] = V128(e); for (uint32_t j = 0; j < FB; j++) bs[s][j] = V128(e); } + if (e->err) return -1; + ldcu64(e, udesc, 0x358); + ldc64(e, pa, 0x380); ldc64(e, pb, 0x388); ldc64(e, pc, 0x390); + ldc32(e, M, 0x398); ldc32(e, N, 0x39c); ldc32(e, ks2, 0x3a0); + s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y); + movi(e, one, 1); movrz(e, zr); + shr(e, warp, tid, 0, 5); andi(e, lane, tid, 0, 31u); + /* warp (wm, wn) = (warp / WN, warp % WN); row groups rga = cy WM + wm, rgb = cx WN + wn */ + shr(e, tmp, warp, 0, (uint32_t)log2u(c->wn)); imadi(e, rga, cy, 0, c->wm, tmp); + andi(e, tmp, warp, 0, c->wn - 1u); imadi(e, rgb, cx, 0, c->wn, tmp); + imadr(e, tmp, rga, ks2, NOREG); imadw(e, pa, tmp, FA * FRAG, pa); + imadr(e, tmp, rgb, ks2, NOREG); imadw(e, pb, tmp, FB * FRAG, pb); + imadi(e, tmp, lane, 0, 16u, NOREG); imadw(e, pa, tmp, 1u, pa); imadw(e, pb, tmp, 1u, pb); + for (uint32_t x = 0; x < RT * CT; x++) for (unsigned q = 0; q < 4; q++) { BlackwellIRInsn mz = I0(BW_IR_MOV_RZ); mz.dst_vreg = acc[x]; mz.dst_subreg = (uint8_t)q; emit(e, mz, K_FIXED); } + shr(e, cnt, ks2, 0, 1); /* loop iterations: two k pairs each */ + + #define ISSUE(s, sb) do { \ + for (uint32_t f = 0; f < FA; f++) ldw_pf(e, BW_IR_LDG_E_128, as[s][f], pa, f * FRAG, udesc, (sb)); \ + for (uint32_t j = 0; j < FB; j++) ldw_pf(e, BW_IR_LDG_E_128, bs[s][j], pb, j * FRAG, udesc, (sb)); \ + imadw(e, pa, one, FA * FRAG, pa); imadw(e, pb, one, FB * FRAG, pb); } while (0) + #define COMPUTE(s) do { \ + for (uint32_t u = 0; u < 2; u++) { \ + if (c->mutant == 1 && u == 1) break; \ + for (uint32_t i = 0; i < RT; i++) for (uint32_t j = 0; j < CT; j++) \ + mma(e, fp8, acc[i * CT + j], as[s][u * RT + i], bs[s][j], 2u * u, acc[i * CT + j], c->stall); } } while (0) + + ISSUE(0, 3u); + const int top = here(e); + loop_begin(e); + ISSUE(1, 4u); + consume(e, 3); + COMPUTE(0); + nop(e, 15); /* the tensor ops have read set 0 before it is reloaded */ + ISSUE(0, 3u); /* the last iteration reads one pair past the end: slack */ + consume(e, 4); + COMPUTE(1); + nop(e, 15); + imadi(e, cnt, one, 0, 0xffffffffu, cnt); + isetp_ge_u32(e, cnt, one); + bra_if_p0(e, top); + loop_end(e); + #undef ISSUE + #undef COMPUTE + consume(e, 3); + nop(e, 15); nop(e, 15); nop(e, 15); nop(e, 15); /* the last results settle before the stores read them */ + + /* stores: C[m][n] for m = 16 (RT rga + i) + 8 h + g, n = 8 (CT rgb + j) + 2 t + cc, when m < M and n < N */ + int mb = V(e), nb = V(e), m = V(e), n = V(e), vm = V(e), vn = V(e), rowoff = V(e), ca = V64(e); + if (e->err) return -1; + shr(e, g, lane, 0, 2); andi(e, t, lane, 0, 3u); + imadi(e, mb, rga, 0, 16u * RT, g); + imadi(e, tmp, t, 0, 2u, NOREG); imadi(e, nb, rgb, 0, 8u * CT, tmp); + for (uint32_t i = 0; i < RT; i++) for (uint32_t h = 0; h < 2; h++) { + imadi(e, m, one, 0, 16u * i + 8u * h, mb); + lt_bit(e, vm, m, M); + imadr(e, rowoff, m, N, NOREG); + for (uint32_t j = 0; j < CT; j++) for (uint32_t cc = 0; cc < 2; cc++) { + imadi(e, n, one, 0, 8u * j + cc, nb); + lt_bit(e, vn, n, N); + and_r(e, vn, vn, vm); + isetp_ge_u32(e, zr, vn); /* P0: 0 >= ok, i.e. outside: skip */ + Region rg = skip_if_p0(e); + iadd(e, tmp, rowoff, n); imadw(e, ca, tmp, 4u, pc); + BlackwellIRInsn st = I0(BW_IR_STG_E); st.src1_vreg = ca; st.src2_vreg = acc[i * CT + j]; + st.src2_subreg = (uint8_t)(2u * h + (c->mutant == 3 ? 1u - cc : cc)); st.ureg = udesc; emit(e, st, K_STORE); + region_end(e, rg); + } + } + tail(e); + return e->err; +} + +int omega_gpu_gemm_codegen(const OmegaGemmCfg *cfg, OmegaGemmKind kind, BlackwellIRProgram *prog) { + if (!omega_gpu_gemm_cfg_ok(cfg) || !prog || (unsigned)kind >= OMEGA_GEMM_K_COUNT) return -1; + omega_bw_ir_init(prog); + Em em; memset(&em, 0, sizeof em); em.p = prog; + int rc = kind == OMEGA_GEMM_K_MAIN ? gen_main(&em, cfg) : gen_pack(&em, cfg, kind == OMEGA_GEMM_K_PACK_B); + if (rc != 0 || em.err || em.depth != 0) return -1; + return omega_bw_regalloc_solve(prog); +} + +/* ------------------------------------------------------------------ plan and driver */ + +static uint32_t roundup(uint32_t v, uint32_t m) { return (v + m - 1u) / m * m; } + +int omega_gpu_gemm_plan(const OmegaGemmCfg *c, uint32_t M, uint32_t N, uint32_t K, OmegaGemmPlan *p) { + if (!omega_gpu_gemm_cfg_ok(c) || !p || !M || !N || !K) return OMEGA_GPU_GEMM_BAD_ARGS; + if (M > OMEGA_GPU_GEMM_MAX_DIM || N > OMEGA_GPU_GEMM_MAX_DIM || K > OMEGA_GPU_GEMM_MAX_DIM) return OMEGA_GPU_GEMM_TOO_LARGE; + memset(p, 0, sizeof *p); + const uint32_t ks = kstep(c->type), ga = 16u * c->rt, gb = 8u * c->ct, FA = 2u * c->rt, FB = c->ct; + p->Mp = roundup(M, ga * c->wm); p->Np = roundup(N, gb * c->wn); p->Kp = roundup(K, 4u * ks); + p->KS2 = p->Kp / (2u * ks); + p->a_bytes = (uint64_t)M * K * esz(c->type); p->b_bytes = (uint64_t)K * N * esz(c->type); + p->ap_bytes = (uint64_t)(p->Mp / ga) * p->KS2 * FA * FRAG + 2u * FA * FRAG; + p->bp_bytes = (uint64_t)(p->Np / gb) * p->KS2 * FB * FRAG + 2u * FB * FRAG; + p->grid[OMEGA_GEMM_K_PACK_A][0] = p->KS2; p->grid[OMEGA_GEMM_K_PACK_A][1] = p->Mp / ga; + p->grid[OMEGA_GEMM_K_PACK_B][0] = p->KS2; p->grid[OMEGA_GEMM_K_PACK_B][1] = p->Np / gb; + p->grid[OMEGA_GEMM_K_MAIN][0] = p->Np / (gb * c->wn); p->grid[OMEGA_GEMM_K_MAIN][1] = p->Mp / (ga * c->wm); + p->threads[OMEGA_GEMM_K_PACK_A] = p->threads[OMEGA_GEMM_K_PACK_B] = 128u; + p->threads[OMEGA_GEMM_K_MAIN] = 32u * c->wm * c->wn; + return OMEGA_GPU_GEMM_OK; +} + +static void put64(uint32_t *a, unsigned w, uint64_t v) { a[w] = (uint32_t)v; a[w + 1] = (uint32_t)(v >> 32); } + +int omega_gpu_gemm_drive(const OmegaGemmCfg *c, unsigned layout, uint32_t M, uint32_t N, uint32_t K, uint64_t a_va, + uint64_t b_va, uint64_t ap_va, uint64_t bp_va, uint64_t c_va, OmegaGemmLaunchFn launch, void *ctx) { + if (!launch || (layout & ~3u)) return OMEGA_GPU_GEMM_BAD_ARGS; + OmegaGemmPlan p; + int rc = omega_gpu_gemm_plan(c, M, N, K, &p); + if (rc != OMEGA_GPU_GEMM_OK) return rc; + uint32_t a[OMEGA_GPU_GEMM_ARGS_WORDS]; + /* A element (m, k): row-major m K + k, column-major k M + m */ + memset(a, 0, sizeof a); + put64(a, 0, a_va); put64(a, 2, ap_va); a[4] = M; a[5] = K; + a[6] = layout & OMEGA_GPU_GEMM_A_COL ? 1u : K; a[7] = layout & OMEGA_GPU_GEMM_A_COL ? M : 1u; a[8] = p.KS2; + if (launch(ctx, OMEGA_GEMM_K_PACK_A, a, p.grid[0][0], p.grid[0][1], p.threads[0]) != 0) return OMEGA_GPU_GEMM_CHIP_FAIL; + /* B element (k, n) as packed row n: row-major k N + n, column-major n K + k */ + memset(a, 0, sizeof a); + put64(a, 0, b_va); put64(a, 2, bp_va); a[4] = N; a[5] = K; + a[6] = layout & OMEGA_GPU_GEMM_B_COL ? K : 1u; a[7] = layout & OMEGA_GPU_GEMM_B_COL ? 1u : N; a[8] = p.KS2; + if (launch(ctx, OMEGA_GEMM_K_PACK_B, a, p.grid[1][0], p.grid[1][1], p.threads[1]) != 0) return OMEGA_GPU_GEMM_CHIP_FAIL; + memset(a, 0, sizeof a); + put64(a, 0, ap_va); put64(a, 2, bp_va); put64(a, 4, c_va); a[6] = M; a[7] = N; a[8] = p.KS2; + if (launch(ctx, OMEGA_GEMM_K_MAIN, a, p.grid[2][0], p.grid[2][1], p.threads[2]) != 0) return OMEGA_GPU_GEMM_CHIP_FAIL; + return OMEGA_GPU_GEMM_OK; +} + +/* ------------------------------------------------------------------ the chip */ + +static struct { + int set[2]; + OmegaGemmCfg cfg[2]; + OmegaGpuMem code[2][OMEGA_GEMM_K_COUNT]; + uint32_t gpr[2][OMEGA_GEMM_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 t = 0; t < 2; t++) + for (unsigned k = 0; k < OMEGA_GEMM_K_COUNT; k++) if (g.code[t][k].cpu) omega_gpu_session_free(&g.code[t][k]); + memset(g.code, 0, sizeof g.code); +} + +static const OmegaGemmCfg *cfg_of(OmegaGpuGemmType t) { + if (!g.set[t]) { omega_gpu_gemm_default_cfg(t, &g.cfg[t]); g.set[t] = 1; } + return &g.cfg[t]; +} + +int omega_gpu_gemm_test_set_cfg(OmegaGpuGemmType type, const OmegaGemmCfg *cfg) { + if ((unsigned)type > OMEGA_GPU_GEMM_E4M3 || (cfg && (!omega_gpu_gemm_cfg_ok(cfg) || cfg->type != type))) return OMEGA_GPU_GEMM_BAD_ARGS; + omega_gpu_session_lock(); + for (unsigned k = 0; k < OMEGA_GEMM_K_COUNT; k++) if (g.code[type][k].cpu) omega_gpu_session_free(&g.code[type][k]); + memset(g.code[type], 0, sizeof g.code[type]); + if (cfg) g.cfg[type] = *cfg; else omega_gpu_gemm_default_cfg(type, &g.cfg[type]); + g.set[type] = 1; + omega_gpu_session_unlock(); + return OMEGA_GPU_GEMM_OK; +} + +const char *omega_gpu_gemm_rc_name(int rc) { + switch (rc) { + case OMEGA_GPU_GEMM_OK: return "OK"; + case OMEGA_GPU_GEMM_BAD_ARGS: return "BAD_ARGS"; + case OMEGA_GPU_GEMM_TOO_LARGE: return "TOO_LARGE"; + case OMEGA_GPU_GEMM_CODEGEN_FAIL: return "CODEGEN_FAIL"; + case OMEGA_GPU_GEMM_CHIP_FAIL: return "CHIP_FAIL"; + default: return "?"; + } +} + +/* lock held */ +static int kernel_locked(OmegaGpuGemmType t, OmegaGemmKind k) { + if (g.code[t][k].cpu) return OMEGA_GPU_GEMM_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_GEMM_CHIP_FAIL; + g.hooked = 1; + } + BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog); + if (!prog) return OMEGA_GPU_GEMM_CODEGEN_FAIL; + int rc = OMEGA_GPU_GEMM_OK; + size_t need = 0; + if (omega_gpu_gemm_codegen(cfg_of(t), k, prog) != 0 || (need = omega_bw_program_code_bytes(prog)) == 0) rc = OMEGA_GPU_GEMM_CODEGEN_FAIL; + else if (omega_gpu_session_alloc_code(need, &g.code[t][k]) != 0) rc = OMEGA_GPU_GEMM_CHIP_FAIL; + else { + OmegaBlackwellKernel kk; + if (omega_blackwell_kernel_encode_into(prog, g.code[t][k].cpu, need, &kk) != 0 || kk.code_size != need || + omega_qmma_patch_code(prog, g.code[t][k].cpu, need) < 0) rc = OMEGA_GPU_GEMM_CODEGEN_FAIL; + else if ((g.gpr[t][k] = omega_bw_gpr_declared(kk.gpr_count)) > 255u) rc = OMEGA_GPU_GEMM_CODEGEN_FAIL; + else if (omega_gpu_session_sync_to_gpu(&g.code[t][k]) != 0) rc = OMEGA_GPU_GEMM_CHIP_FAIL; + if (g.gpr[t][k] < 64u) g.gpr[t][k] = 64u; + if (rc != OMEGA_GPU_GEMM_OK) { omega_gpu_session_free(&g.code[t][k]); memset(&g.code[t][k], 0, sizeof g.code[t][k]); } + } + if (rc == OMEGA_GPU_GEMM_CODEGEN_FAIL) omega_gpu_session_set_error("gemm: our codegen refused a kernel"); + omega_gpu_host_free(prog); + return rc; +} + +typedef struct { OmegaGpuGemmType t; uint64_t pack_ns, main_ns; } ChipCtx; + +/* lock held */ +static int chip_launch(void *ctx, OmegaGemmKind k, const uint32_t args[OMEGA_GPU_GEMM_ARGS_WORDS], uint32_t gx, uint32_t gy, uint32_t threads) { + ChipCtx *c = ctx; + if (kernel_locked(c->t, k) != OMEGA_GPU_GEMM_OK) return -1; + OmegaGpuLaunch L = { + .code_va = g.code[c->t][k].va, .gpr_count = g.gpr[c->t][k], .threads_x = threads, .threads_y = 1, + .grid_x = gx, .grid_y = gy, .num_elements = (size_t)gx * gy * threads, + .args = args, .n_args = OMEGA_GPU_GEMM_ARGS_WORDS, .timeout_ms = TIMEOUT_MS, + }; + uint64_t ns = 0; + if (omega_gpu_session_launch(&L, &ns, NULL) != 0) return -1; + if (k == OMEGA_GEMM_K_MAIN) c->main_ns += ns; else c->pack_ns += ns; + return 0; +} + +static uint64_t up16(uint64_t v) { return (v + 255u) & ~(uint64_t)255u; } + +int omega_gpu_gemm(OmegaGpuGemmType type, unsigned layout, uint32_t M, uint32_t N, uint32_t K, const void *A, const void *B, + float *C, OmegaGpuGemmInfo *info) { + uint64_t t0 = omega_gpu_session_now_ns(); + if (info) memset(info, 0, sizeof *info); + if ((unsigned)type > OMEGA_GPU_GEMM_E4M3 || (layout & ~3u) || !A || !B || !C) return OMEGA_GPU_GEMM_BAD_ARGS; + omega_gpu_session_lock(); + const OmegaGemmCfg *cfg = cfg_of(type); + OmegaGemmPlan p; + int rc = omega_gpu_gemm_plan(cfg, M, N, K, &p); + if (rc != OMEGA_GPU_GEMM_OK) { omega_gpu_session_unlock(); return rc; } + /* one buffer: A | B | packed A | packed B | C (each 256-byte aligned; A and B carry 16 bytes of readable slack) */ + const uint64_t oa = 0, ob = up16(p.a_bytes + 16u), oap = ob + up16(p.b_bytes + 16u), obp = oap + up16(p.ap_bytes), + oc = obp + up16(p.bp_bytes), total = oc + (uint64_t)M * N * 4u; + OmegaGpuMem m; + if (!omega_gpu_session_open() || omega_gpu_session_alloc(total, &m) != 0) { omega_gpu_session_unlock(); return OMEGA_GPU_GEMM_CHIP_FAIL; } + uint8_t *base = m.cpu; + memcpy(base + oa, A, p.a_bytes); memcpy(base + ob, B, p.b_bytes); + uint32_t *cw = (uint32_t *)(base + oc); + for (uint64_t i = 0; i < (uint64_t)M * N; i++) cw[i] = POISON; + ChipCtx cc = { type, 0, 0 }; + if (omega_gpu_session_sync_to_gpu(&m) != 0) rc = OMEGA_GPU_GEMM_CHIP_FAIL; + else rc = omega_gpu_gemm_drive(cfg, layout, M, N, K, m.va + oa, m.va + ob, m.va + oap, m.va + obp, m.va + oc, chip_launch, &cc); + if (rc == OMEGA_GPU_GEMM_OK && omega_gpu_session_sync_to_cpu(&m) != 0) rc = OMEGA_GPU_GEMM_CHIP_FAIL; + if (rc == OMEGA_GPU_GEMM_OK) { + uint64_t unwritten = 0; + for (uint64_t i = 0; i < (uint64_t)M * N; i++) unwritten += cw[i] == POISON; + if (unwritten) { omega_gpu_session_set_error("gemm: output words never written"); rc = OMEGA_GPU_GEMM_CHIP_FAIL; } + else memcpy(C, cw, (size_t)M * N * 4u); + } + if (info) { + info->pack_ns = cc.pack_ns; info->main_ns = cc.main_ns; + for (unsigned k = 0; k < OMEGA_GEMM_K_COUNT; k++) info->gpr[k] = g.gpr[type][k]; + } + 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_gemm.h b/src/omega_gpu_gemm.h new file mode 100644 index 000000000..66a6b0485 --- /dev/null +++ b/src/omega_gpu_gemm.h @@ -0,0 +1,96 @@ +/* omega_gpu_gemm.h -- matrix multiply of any shape on the GB10 tensor cores (row C3 of docs/OMEGA.md). + * + * C = A * B A is M x K, B is K x N, C is M x N f32 (row-major), every M, N, K from 1 to OMEGA_GPU_GEMM_MAX_DIM + * BF16: A and B hold bf16 values, the product runs on HMMA.16816.F32.BF16 + * E4M3: A and B hold FP8 e4m3 bytes, the product runs on QMMA.16832.F32.E4M3.E4M3 + * Layouts: A row-major (M rows of K) or column-major (K rows of M); B row-major (K rows of N) or column-major (N rows of K). + * + * The arithmetic is fixed by the shape alone: every C[m][n] starts at +0 and takes one tensor-core step per 16 (BF16) or + * 32 (E4M3) values of k, in increasing k, k past K counting as zero. The rounding of one step is the chip-measured model + * (tests/bw_hmma_model.h { 16, 2, RZ, 2, 0 }, tests/bw_qmma_model.h { 32, 26, RZ, 2, 0 }), so a host model of the same + * steps reproduces the chip bit for bit (tests/gpu_gemm_test.c). + * + * Kernels, all from Omega's own code generator (no CUDA): + * PACK_A, PACK_B copy A and B, whatever their layout, into the tensor cores' fragment order (each lane's operand + * registers are 16 contiguous bytes; a warp's fragment is 512 contiguous bytes), zero past the edges. + * MAIN each warp computes a 16 rt x 8 ct block of C from the packed operands (LDG.128 straight into the + * operand registers, two register sets so the next loads fly while the tensor cores work), a CTA holds + * wm x wn warps; edge elements past M or N are not stored. + * Packed operand layout (bytes): block (row group, k pair) at ((group * KS2 + pair) * F + f) * 512, lane L at + 16 L, where + * a k pair is two tensor-core steps, KS2 = Kp / (2 * step), F = 2 rt (A: f = u * rt + i, step u of the pair, row tile i) + * or ct (B: f = column tile j; registers 0, 1 step 0 and 2, 3 step 1). */ +#ifndef OMEGA_GPU_GEMM_H +#define OMEGA_GPU_GEMM_H + +#include +#include +#include "omega_blackwell_codegen.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define OMEGA_GPU_GEMM_MAX_DIM 16384u + +enum { + OMEGA_GPU_GEMM_OK = 0, + OMEGA_GPU_GEMM_BAD_ARGS = -1, /* NULL pointer, a zero dimension, unknown type or layout bits, a bad configuration */ + OMEGA_GPU_GEMM_TOO_LARGE = -2, /* a dimension past OMEGA_GPU_GEMM_MAX_DIM */ + OMEGA_GPU_GEMM_CODEGEN_FAIL = -3, /* our codegen refused a kernel */ + OMEGA_GPU_GEMM_CHIP_FAIL = -4 /* device, allocation, launch, or an output word the chip never wrote */ +}; + +typedef enum { OMEGA_GPU_GEMM_BF16 = 0, OMEGA_GPU_GEMM_E4M3 = 1 } OmegaGpuGemmType; +#define OMEGA_GPU_GEMM_A_COL 1u /* A is column-major: K rows of M */ +#define OMEGA_GPU_GEMM_B_COL 2u /* B is column-major: N rows of K */ + +typedef enum { OMEGA_GEMM_K_PACK_A = 0, OMEGA_GEMM_K_PACK_B, OMEGA_GEMM_K_MAIN, OMEGA_GEMM_K_COUNT } OmegaGemmKind; + +/* rt row tiles of 16 and ct column tiles of 8 per warp (1, 2 or 4 each), wm x wn warps per CTA (1, 2 or 4 each), + * with 4 (4 rt + 2 ct + rt ct) <= 100 (the registers of two operand sets and the accumulators; rt 2 ct 4 is the largest), + * stall: issue cycles after each tensor-core op (1 .. 15). + * mutant (red checks): 1 MAIN drops the second step of every k pair, 2 the packs let element 0 in past the edges (instead of zero), + * 3 MAIN stores each column pair swapped. */ +typedef struct { OmegaGpuGemmType type; uint32_t rt, ct, wm, wn, stall; int mutant; } OmegaGemmCfg; +void omega_gpu_gemm_default_cfg(OmegaGpuGemmType type, OmegaGemmCfg *cfg); +int omega_gpu_gemm_cfg_ok(const OmegaGemmCfg *cfg); + +/* Argument words (constant bank 0x380). Packs: src u64, dst u64, rows, kdim, row stride, k stride (elements), KS2. + * MAIN: packed A u64, packed B u64, C u64, M, N, KS2. */ +#define OMEGA_GPU_GEMM_ARGS_WORDS 9u +int omega_gpu_gemm_codegen(const OmegaGemmCfg *cfg, OmegaGemmKind kind, BlackwellIRProgram *prog); + +typedef struct { + uint32_t Mp, Np, Kp, KS2; /* padded sizes; KS2 k pairs (even) */ + uint64_t a_bytes, b_bytes; /* the inputs as given */ + uint64_t ap_bytes, bp_bytes; /* the packed operands, slack for the last prefetch included */ + uint32_t grid[OMEGA_GEMM_K_COUNT][2]; + uint32_t threads[OMEGA_GEMM_K_COUNT]; +} OmegaGemmPlan; +int omega_gpu_gemm_plan(const OmegaGemmCfg *cfg, uint32_t M, uint32_t N, uint32_t K, OmegaGemmPlan *plan); + +/* Runs the three kernels through a launch callback (the chip, or the warp simulator in the test). Device addresses + * are plain numbers. */ +typedef int (*OmegaGemmLaunchFn)(void *ctx, OmegaGemmKind kind, const uint32_t args[OMEGA_GPU_GEMM_ARGS_WORDS], + uint32_t grid_x, uint32_t grid_y, uint32_t threads); +int omega_gpu_gemm_drive(const OmegaGemmCfg *cfg, unsigned layout, uint32_t M, uint32_t N, uint32_t K, uint64_t a_va, + uint64_t b_va, uint64_t ap_va, uint64_t bp_va, uint64_t c_va, OmegaGemmLaunchFn launch, void *ctx); + +typedef struct { + uint64_t pack_ns; /* the two pack kernels, launch to completion */ + uint64_t main_ns; /* the multiply kernel */ + uint64_t call_ns; /* the whole call, uploads and readback included */ + uint32_t gpr[OMEGA_GEMM_K_COUNT]; +} OmegaGpuGemmInfo; + +/* On the chip, with the configuration set by omega_gpu_gemm_test_set_cfg (default: omega_gpu_gemm_default_cfg). */ +int omega_gpu_gemm(OmegaGpuGemmType type, unsigned layout, uint32_t M, uint32_t N, uint32_t K, const void *A, const void *B, + float *C, OmegaGpuGemmInfo *info); +/* Tests and tuning: later calls of that type use cfg (NULL: back to the default); clears the kernel cache. */ +int omega_gpu_gemm_test_set_cfg(OmegaGpuGemmType type, const OmegaGemmCfg *cfg); +const char *omega_gpu_gemm_rc_name(int rc); + +#ifdef __cplusplus +} +#endif +#endif /* OMEGA_GPU_GEMM_H */ diff --git a/src/omega_gpu_platform_linux.c b/src/omega_gpu_platform_linux.c index e96a84d24..23f046209 100644 --- a/src/omega_gpu_platform_linux.c +++ b/src/omega_gpu_platform_linux.c @@ -52,6 +52,12 @@ typedef struct { M16NativeContext m16; OmegaGpuDeviceHandle dev; /* generation 0 = closed */ OmegaGpuQueueHandle queue; + /* L136: extra queues on extra channels of the same channel group (nvrm_channel_n), opened only when + * AIEN_OMEGA_GPU_CHANNELS > 1 at device_open; queue 0 above is unchanged. */ + uint32_t nchan; + OmegaGpuQueueHandle xq[NVRM_MAX_EXTRA_CHANNELS]; + NvrmMem xring[NVRM_MAX_EXTRA_CHANNELS]; + uint8_t x_l2_pending[NVRM_MAX_EXTRA_CHANNELS]; char diag[320]; } LinuxRef; @@ -73,7 +79,7 @@ static const OmegaGpuPlatformCaps g_caps = { .services = OMEGA_GPU_SVC_REQUIRED_SESSION, /* no MMIO (RM owns the registers), no IRQ (polling only) */ .max_devices = 1, .max_buffers = LINUX_REF_BUFFERS, - .max_queues = 1, + .max_queues = 1u + NVRM_MAX_EXTRA_CHANNELS, /* queues past the first need AIEN_OMEGA_GPU_CHANNELS (L136) */ .max_inflight = 1, .max_buffer_bytes = 1ull << 37, /* 128 GiB: the whole unified pool */ .max_total_bytes = 1ull << 37, @@ -140,6 +146,19 @@ static int ref_unlock_id(void *ctx, uint32_t id) { return pthread_mutex_unlock(&r->locks[id]) == 0 ? OMEGA_GPU_PLAT_OK : OMEGA_GPU_PLAT_REFUSED_ARGS; } +uint32_t omega_gpu_platform_linux_channels_env(void) { + const char *e = getenv("AIEN_OMEGA_GPU_CHANNELS"); + if (!e || e[1] != '\0' || e[0] < '2' || e[0] > (char)('1' + NVRM_MAX_EXTRA_CHANNELS)) return 1u; + return (uint32_t)(e[0] - '0'); +} + +/* Extra queue slot of q (0..nchan-2), or -1 when q is not an open extra queue. Caller holds r->mu. */ +static int xq_slot(const LinuxRef *r, OmegaGpuQueueHandle q) { + for (uint32_t i = 0; i + 1u < r->nchan; i++) + if (r->xq[i].generation != 0u && r->xq[i].index == q.index && r->xq[i].generation == q.generation) return (int)i; + return -1; +} + static int ref_device_open(void *ctx, uint32_t ordinal, OmegaGpuDeviceHandle *out) { LinuxRef *r = ctx; int rc; @@ -156,8 +175,9 @@ static int ref_device_open(void *ctx, uint32_t ordinal, OmegaGpuDeviceHandle *ou (void)nvrm_close(&r->m16.rm); goto out; } - if (m16_native_create_channel(&r->m16) != 0) { - rc = refuse(r, OMEGA_GPU_PLAT_REFUSED_DEVICE, "m16_native_create_channel"); + r->nchan = omega_gpu_platform_linux_channels_env(); + if (r->nchan == 1u ? m16_native_create_channel(&r->m16) != 0 : nvrm_channel_n(&r->m16.rm, r->nchan - 1u) != 0) { + rc = refuse(r, OMEGA_GPU_PLAT_REFUSED_DEVICE, r->nchan == 1u ? "m16_native_create_channel" : "nvrm_channel_n"); (void)m16_native_close(&r->m16); goto out; } @@ -188,9 +208,13 @@ static int ref_device_close(void *ctx, OmegaGpuDeviceHandle dev) { memset(&r->dev, 0, sizeof r->dev); r->l2_inval_pending = 0; /* omega #457: nothing pending survives the device */ memset(&r->queue, 0, sizeof r->queue); - (void)m16_native_close(&r->m16); + memset(r->xq, 0, sizeof r->xq); memset(r->xring, 0, sizeof r->xring); memset(r->x_l2_pending, 0, sizeof r->x_l2_pending); + r->nchan = 0; + /* C5: the driver's close result is the proof that this process's channels (and any grid still on them) are gone. + * The state above is closed either way; a teardown fault is reported so omega_gpu_session_recover stays latched. */ + rc = m16_native_close(&r->m16) == 0 ? OMEGA_GPU_PLAT_OK : refuse(r, OMEGA_GPU_PLAT_REFUSED_DEVICE, "device_close (driver teardown fault)"); ref_unlock(r); - return OMEGA_GPU_PLAT_OK; + return rc; } static const char *ref_diag_text(void *ctx) { return ((LinuxRef *)ctx)->diag; } @@ -241,6 +265,8 @@ static int ref_dma_free(void *ctx, OmegaGpuBufferHandle b) { if (rc == OMEGA_GPU_PLAT_OK && r->queue.generation != 0u && r->m16.pb_mem.handle == r->mem[b.index].handle && r->m16.pb_mem.cpu == r->mem[b.index].cpu) rc = OMEGA_GPU_PLAT_REFUSED_BUSY; /* the open queue still uses it as its ring */ + for (uint32_t i = 0; rc == OMEGA_GPU_PLAT_OK && i + 1u < r->nchan; i++) + if (r->xq[i].generation != 0u && r->xring[i].handle == r->mem[b.index].handle) rc = OMEGA_GPU_PLAT_REFUSED_BUSY; if (rc != OMEGA_GPU_PLAT_OK) { rc = refuse(r, rc, "dma_free"); ref_unlock(r); return rc; } (void)nvrm_free(&r->m16.rm, &r->mem[b.index]); (void)omega_gpu_handles_close(&r->t, b.index, b.generation, kind); @@ -268,7 +294,7 @@ static int ref_sync(void *ctx, OmegaGpuBufferHandle b, uint64_t off, uint64_t le /* omega #457 (chip, 2026-10-15): after the host rewrites a GPU-cached buffer the GPU already read, every later launch * read the old bytes from L2 (nvos.h:1117 "For system memory this will not be coherent with direct CPU mappings"). * The next submission therefore starts with L2_SYSMEM_NCOH_INVALIDATE (ref_queue_submit). */ - else if (to_gpu && r->gpu_cached[b.index]) r->l2_inval_pending = 1; + else if (to_gpu && r->gpu_cached[b.index]) { r->l2_inval_pending = 1; memset(r->x_l2_pending, 1, sizeof r->x_l2_pending); } ref_unlock(r); if (rc == OMEGA_GPU_PLAT_OK) barrier(); /* uncached system memory: a full barrier is the whole sync */ return rc; @@ -304,9 +330,15 @@ static int ref_queue_open(void *ctx, OmegaGpuDeviceHandle dev, OmegaGpuBufferHan int rc = dev_ok(r, dev); if (rc == OMEGA_GPU_PLAT_OK) rc = buf_ok(r, ring, &kind); if (rc == OMEGA_GPU_PLAT_OK && kind != OMEGA_GPU_HANDLE_BUF_QUEUE) rc = OMEGA_GPU_PLAT_REFUSED_BAD_KIND; - if (rc == OMEGA_GPU_PLAT_OK && r->queue.generation != 0u) rc = OMEGA_GPU_PLAT_REFUSED_LIMIT; - if (rc == OMEGA_GPU_PLAT_OK) rc = omega_gpu_handles_open(&r->t, OMEGA_GPU_HANDLE_QUEUE, 0, 1, 0, &out->index, &out->generation); + /* queue 0 first (channel 0); then, with AIEN_OMEGA_GPU_CHANNELS > 1, one queue per extra channel (L136) */ + int slot = -1; + if (rc == OMEGA_GPU_PLAT_OK && r->queue.generation != 0u) { + for (uint32_t i = 0; i + 1u < r->nchan && slot < 0; i++) if (r->xq[i].generation == 0u) slot = (int)i; + if (slot < 0) rc = OMEGA_GPU_PLAT_REFUSED_LIMIT; + } + if (rc == OMEGA_GPU_PLAT_OK) rc = omega_gpu_handles_open(&r->t, OMEGA_GPU_HANDLE_QUEUE, 0, r->nchan, 0, &out->index, &out->generation); /* one queue per channel (L136: nchan is 1 unless AIEN_OMEGA_GPU_CHANNELS) */ if (rc != OMEGA_GPU_PLAT_OK) { rc = refuse(r, rc, "queue_open"); ref_unlock(r); return rc; } + if (slot >= 0) { r->xring[slot] = r->mem[ring.index]; r->xq[slot] = *out; r->x_l2_pending[slot] = (uint8_t)r->l2_inval_pending; ref_unlock(r); return OMEGA_GPU_PLAT_OK; } r->m16.pb_mem = r->mem[ring.index]; /* m16_native_enqueue_methods copies the method stream here */ r->queue = *out; ref_unlock(r); @@ -319,6 +351,8 @@ static int ref_queue_close(void *ctx, OmegaGpuQueueHandle q) { int rc = queue_ok(r, q); if (rc == OMEGA_GPU_PLAT_OK) rc = omega_gpu_handles_close(&r->t, q.index, q.generation, OMEGA_GPU_HANDLE_QUEUE); if (rc != OMEGA_GPU_PLAT_OK) { rc = refuse(r, rc, "queue_close"); ref_unlock(r); return rc; } + const int slot = xq_slot(r, q); + if (slot >= 0) { memset(&r->xq[slot], 0, sizeof r->xq[slot]); memset(&r->xring[slot], 0, sizeof r->xring[slot]); ref_unlock(r); return OMEGA_GPU_PLAT_OK; } memset(&r->queue, 0, sizeof r->queue); memset(&r->m16.pb_mem, 0, sizeof r->m16.pb_mem); ref_unlock(r); @@ -330,6 +364,25 @@ static int ref_queue_submit(void *ctx, OmegaGpuQueueHandle q, const uint32_t *wo ref_lock(r); int rc = queue_ok(r, q); if (rc == OMEGA_GPU_PLAT_OK && (!words || nwords == 0u)) rc = OMEGA_GPU_PLAT_REFUSED_ARGS; + const int slot = rc == OMEGA_GPU_PLAT_OK ? xq_slot(r, q) : -1; + if (slot >= 0) { + /* an extra channel: the same #457 prefix rule (its own pending flag), the same copy-to-ring-offset-0 + one GP + * entry + doorbell as m16_native_submit_methods, on channel slot + 1 */ + NvrmMem *ring = &r->xring[slot]; + const uint32_t xpre = r->x_l2_pending[slot] ? L2_INVAL_PREFIX_WORDS : 0u; + if (((uint64_t)nwords + xpre) * 4u > ring->size) rc = OMEGA_GPU_PLAT_REFUSED_BAD_LENGTH; + else { + uint32_t *dst = ring->cpu; + if (xpre) memcpy(dst, L2_INVAL_PREFIX, sizeof L2_INVAL_PREFIX); + memcpy(dst + xpre, words, (size_t)nwords * 4u); + __asm__ volatile("dsb sy" ::: "memory"); + if (nvrm_chan_submit(&r->m16.rm, (uint32_t)slot + 1u, ring, 0, nwords + xpre) != 0) rc = OMEGA_GPU_PLAT_REFUSED_DEVICE; + else r->x_l2_pending[slot] = 0; + } + if (rc != OMEGA_GPU_PLAT_OK) rc = refuse(r, rc, "submit_methods (extra channel)"); + ref_unlock(r); + return rc; + } /* omega #457: a host sync into a GPU-cached buffer is pending, so this submission starts with L2_INVAL_PREFIX. * No dirty line is dropped: every earlier submission ended with L2_FLUSH_DIRTY and the host waited for it. * DOCS SILENT on ordering between this MEM_OP and the launch that follows (no WFI is inserted); the chip gate @@ -358,7 +411,9 @@ static int ref_queue_retire(void *ctx, OmegaGpuQueueHandle q) { LinuxRef *r = ctx; ref_lock(r); int rc = queue_ok(r, q); - if (rc == OMEGA_GPU_PLAT_OK) nvrm_retire(&r->m16.rm, r->m16.rm.put); /* both releases landed: the entry is consumed */ + const int slot = rc == OMEGA_GPU_PLAT_OK ? xq_slot(r, q) : -1; + if (slot >= 0) nvrm_chan_retire(&r->m16.rm, (uint32_t)slot + 1u, nvrm_chan_put(&r->m16.rm, (uint32_t)slot + 1u)); + else if (rc == OMEGA_GPU_PLAT_OK) nvrm_retire(&r->m16.rm, r->m16.rm.put); /* both releases landed: the entry is consumed */ else rc = refuse(r, rc, "queue_retire"); ref_unlock(r); return rc; diff --git a/src/omega_gpu_platform_linux.h b/src/omega_gpu_platform_linux.h index fc2369852..cee1b961d 100644 --- a/src/omega_gpu_platform_linux.h +++ b/src/omega_gpu_platform_linux.h @@ -50,6 +50,11 @@ extern "C" { /* The process-wide Linux reference platform and its caps (written to *caps when caps is not NULL). */ const OmegaGpuPlatform *omega_gpu_platform_linux(OmegaGpuPlatformCaps *caps); +/* L136: channels (GPFIFO queues) the next device_open creates, from env AIEN_OMEGA_GPU_CHANNELS: 1 (unset, empty, "1" or + * anything not 2..4) is today's single channel exactly; 2..4 adds that many minus one channels in the same channel group + * (nvrm_channel_n). Queue i (0-based, in queue_open order) submits on channel i. Read at every device_open. */ +uint32_t omega_gpu_platform_linux_channels_env(void); + #ifdef __cplusplus } #endif 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/src/omega_gpu_session.c b/src/omega_gpu_session.c index 0276bfd32..41a72e285 100644 --- a/src/omega_gpu_session.c +++ b/src/omega_gpu_session.c @@ -222,9 +222,16 @@ int omega_gpu_session_is_parked(OmegaGpuBufferHandle h) { return 0; } +/* L136: queues past the first (omega_gpu_session_extra_queue), each with its own 64 KB ring; opened on demand, closed + * and freed before queue 0 at close, parked at the latch like the session ring. */ +#define XQ_MAX 3u +static OmegaGpuQueueHandle g_xq[XQ_MAX]; +static OmegaGpuMem g_xpb[XQ_MAX]; + /* Uncertain completion: the device may still touch any session buffer. Keep everything, refuse everything. */ static void latch(const char *why) { STORE(g_blocked, 1u); + for (uint32_t i = 0; i < XQ_MAX; i++) if (g_xpb[i].cpu) park(&g_xpb[i]); if (g_s.pb.cpu) park(&g_s.pb); if (g_s.cbank.cpu) park(&g_s.cbank); if (g_s.marker.cpu) park(&g_s.marker); @@ -232,6 +239,8 @@ static void latch(const char *why) { omega_gpu_session_set_error(why); } +void omega_gpu_session_latch_uncertain(const char *why) { latch(why ? why : "uncertain completion (latched by a caller)"); } + /* ---- buffers ---- */ static size_t page_up(size_t b) { b = (b + PAGE - 1) & ~(PAGE - 1); return b < PAGE ? PAGE : b; } @@ -372,6 +381,25 @@ OmegaGpuSession *omega_gpu_session_open(void) { return &g_s; } +int omega_gpu_session_extra_queue(uint32_t i, OmegaGpuQueueHandle *out) { + const OmegaGpuPlatform *p = platform(); + if (!out || i == 0u || i > XQ_MAX) { omega_gpu_session_set_error("extra_queue: index outside 1..3"); return -1; } + if (LOAD(g_blocked) || !g_open || !p) { omega_gpu_session_set_error("extra_queue: device not open or latched"); return -1; } + if (g_xq[i - 1u].generation != 0u) { *out = g_xq[i - 1u]; return 0; } + for (uint32_t j = 0; j + 1u < i; j++) + if (g_xq[j].generation == 0u) { omega_gpu_session_set_error("extra_queue: open queues in order (1, then 2, ...)"); return -1; } + if (mem_alloc(OMEGA_GPU_HANDLE_BUF_QUEUE, RIGHTS_RING, 0x10000, &g_xpb[i - 1u])) return -1; + const int rc = p->queue_open(p->ctx, g_s.dev, g_xpb[i - 1u].handle, &g_xq[i - 1u]); + if (rc != OMEGA_GPU_PLAT_OK) { + fail_plat("queue_open (extra queue)", rc); + memset(&g_xq[i - 1u], 0, sizeof g_xq[i - 1u]); + omega_gpu_session_free(&g_xpb[i - 1u]); + return -1; + } + *out = g_xq[i - 1u]; + return 0; +} + int omega_gpu_session_on_close(void (*hook)(void)) { /* caller holds the lock (the APIs register from their open path) */ if (!hook) return -1; int rc = 0; @@ -387,6 +415,11 @@ void omega_gpu_session_close(void) { if (g_open && !LOAD(g_blocked) && p) { for (unsigned i = 0; i < g_nhooks; i++) g_hooks[i](); if (!LOAD(g_blocked)) { /* a hook's launch may have latched: then nothing below runs */ + for (uint32_t i = XQ_MAX; i-- > 0;) { /* L136: extra queues first, then queue 0 */ + if (g_xq[i].generation != 0u) (void)p->queue_close(p->ctx, g_xq[i]); + memset(&g_xq[i], 0, sizeof g_xq[i]); + omega_gpu_session_free(&g_xpb[i]); + } (void)p->queue_close(p->ctx, g_s.queue); memset(&g_s.queue, 0, sizeof g_s.queue); free_session_buffers(); @@ -398,6 +431,48 @@ void omega_gpu_session_close(void) { omega_gpu_session_unlock(); } + +/* C5: the way back from a latch without restarting the process. The on-close hooks run first, still latched, so the APIs + * drop their resident kernels and scratch (their frees park: nothing is released while the old device may run). Then + * the device closes: the Linux reference closes the RM client, which destroys every channel of this process (extra + * queues first) and with it every grid still running on them, then frees and unmaps every buffer, parked ones + * included. Only after the close returned OK is the latch lifted; the next open starts a new device. */ +static uint32_t g_recoveries; +static uint32_t g_module_latched; /* a module (the command lists) latched itself while the session stayed usable */ + +void omega_gpu_session_note_module_latch(void) { STORE(g_module_latched, 1u); } + +int omega_gpu_session_recover(void) { + omega_gpu_session_lock(); + const OmegaGpuPlatform *p = LOAD(g_p); + int rc = -1; + const int latched = LOAD(g_blocked) || LOAD(g_module_latched); + if (!latched || !g_open || !p) { + omega_gpu_session_set_error(latched ? "recover: latched but no device is open" : "recover: not latched (nothing to recover)"); + goto out; + } + for (unsigned i = 0; i < g_nhooks; i++) g_hooks[i](); + const int prc = p->device_close(p->ctx, g_s.dev); + if (prc != OMEGA_GPU_PLAT_OK) { fail_plat("recover: the device close reported a teardown fault (still latched: the old grid may live on, exit the process)", prc); goto out; } + memset(g_xq, 0, sizeof g_xq); + memset(g_xpb, 0, sizeof g_xpb); + const uint32_t opens = g_s.opens; + memset(&g_s, 0, sizeof g_s); + g_s.opens = opens; + g_open = false; + memset(g_parked, 0, sizeof g_parked); + memset(&g_park, 0, sizeof g_park); + BUMP(g_recoveries, 1u); + STORE(g_module_latched, 0u); + STORE(g_blocked, 0u); + omega_gpu_session_set_error(""); + rc = 0; +out: + omega_gpu_session_unlock(); + return rc; +} + +uint32_t omega_gpu_session_recoveries(void) { return LOAD(g_recoveries); } /* ---- launch ---- */ static uint32_t round_up(uint32_t v, uint32_t q) { return (v + q - 1) / q * q; } diff --git a/src/omega_gpu_session.h b/src/omega_gpu_session.h index 13a45abd7..8a4ae6727 100644 --- a/src/omega_gpu_session.h +++ b/src/omega_gpu_session.h @@ -226,6 +226,29 @@ void omega_gpu_session_cancel(void); const char *omega_gpu_session_last_error(void); void omega_gpu_session_set_error(const char *stage); +/* L136 (roadblock R3): caller holds the lock, device open. Queue i (1..3) past the session's own queue, with its own + * 64 KB ring, opened on first use (in order: 1, then 2, ...) and kept until close. The Linux reference grants it only + * when the device was opened with AIEN_OMEGA_GPU_CHANNELS > i (a second GPFIFO channel in the same channel group and + * VA space); otherwise the platform refuses and this returns -1 with the stage named. Work on different queues is not + * ordered against each other. 0 ok. */ +int omega_gpu_session_extra_queue(uint32_t i, OmegaGpuQueueHandle *out); +/* L136: latch the process now (uncertain completion seen by a caller that cannot prove it with a queue-0 launch, e.g. a + * list on an extra queue timed out). Parks every session buffer; every later launch of every API is refused. */ +void omega_gpu_session_latch_uncertain(const char *why); + +/* C5: after a latch (a timeout or a cancel after submit), throw the device away and lift the latch, in the same process. + * Takes the lock itself. Runs the on-close hooks (still latched), closes the device (Linux reference: the RM client + * close destroys this process's channels, and so any grid still running on them, then frees every buffer, parked ones + * included) and only then clears the latch and the parked list. Every handle and command list of the old device is + * stale; the next open starts a new device. 0 ok; -1 with the reason: not latched, or the close was refused or reported + * a driver teardown fault (then it stays latched for good: the old grid may still run, the process must exit). */ +int omega_gpu_session_recover(void); +/* A module that latched itself (the command lists after a timeout whose check launch completed) tells the session, so + * omega_gpu_session_recover accepts it as latched. Launches of other APIs are not refused by it. */ +void omega_gpu_session_note_module_latch(void); +/* Recoveries so far in this process (a command list latched before the latest recovery no longer blocks). */ +uint32_t omega_gpu_session_recoveries(void); + /* Caller holds the lock. Register a function the session calls (lock held, device * still open) right before it closes, so an API can drop its resident kernels and * scratch. Up to 8; the same function is registered once. (Taking the lock inside diff --git a/tests/.crumb b/tests/.crumb index c816853a1..9e9efb0e1 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:51Z", "compiler": "crumb-compile/1", - "digest": "sha256:8fa4874233c906929082e779408ae009b381ade4590b930a2faf68c60fd7e48f", + "digest": "sha256:9945e53ba48c2e7869f72c7efbd8463b75f77bc9f7b1c94436e22d4a0be676c5", "evidence_files": 16, "evidence_root": "sha256:96d08097822ad95f4e562c19e0ba5df5a035774c48632f1832912c40a27bd27a", - "files": 1287, - "generated_at_commit": "79b2b6329d552350efc62eecce2b298afa53fc10", - "source_tree": "sha256:66aa30eb539fb8432bd7571a91659a1ae9a174746c98fb2eeda085a373778a65" + "files": 1291, + "generated_at_commit": "fe8bf06c6185a99c25d7658614ccbc812b6df465", + "source_tree": "sha256:492108c435774591a4cf1a03f9c18a837cac67076e66f2cd8691eb6936456768" }, "owner_lane": "", "provenance": { diff --git a/tests/bw_warp_sim_tc.h b/tests/bw_warp_sim_tc.h index 3f779e3f7..bf662ea6b 100644 --- a/tests/bw_warp_sim_tc.h +++ b/tests/bw_warp_sim_tc.h @@ -348,8 +348,8 @@ static int ws_run_warp(WsSim *s, WsWarp *w, uint32_t warp, uint32_t cx, uint32_t int gg = L / 4, tt = L % 4; const WsLane *ln = &w->lane[L]; uint32_t a4[4], b2[2], c4[4]; - for (unsigned q = 0; q < 4; q++) { a4[q] = ws_rds(ln, in->src1_vreg, q, p); c4[q] = in->src3_vreg >= 0 ? ws_rds(ln, in->src3_vreg, q, p) : 0u; } - for (unsigned q = 0; q < 2; q++) b2[q] = ws_rds(ln, in->src2_vreg, q, p); + for (unsigned q = 0; q < 4; q++) { a4[q] = ws_rds(ln, in->src1_vreg, in->src1_subreg + q, p); c4[q] = in->src3_vreg >= 0 ? ws_rds(ln, in->src3_vreg, in->src3_subreg + q, p) : 0u; } + for (unsigned q = 0; q < 2; q++) b2[q] = ws_rds(ln, in->src2_vreg, in->src2_subreg + q, p); /* operands may start at a subregister (the encoder adds it) */ const int ar[4] = { gg, gg + 8, gg, gg + 8 }, ac[4] = { 2 * tt, 2 * tt, 2 * tt + 8, 2 * tt + 8 }; for (unsigned q = 0; q < 4; q++) { A[ar[q]][ac[q]] = ws_u2f(a4[q] << 16); A[ar[q]][ac[q] + 1] = ws_u2f(a4[q] & 0xffff0000u); } for (unsigned q = 0; q < 2; q++) { B[2 * tt + 8 * q][gg] = ws_u2f(b2[q] << 16); B[2 * tt + 8 * q + 1][gg] = ws_u2f(b2[q] & 0xffff0000u); } diff --git a/tests/fake_m16_native.c b/tests/fake_m16_native.c index 6c7984524..f9ce918fb 100644 --- a/tests/fake_m16_native.c +++ b/tests/fake_m16_native.c @@ -472,3 +472,28 @@ int m16_native_close(M16NativeContext *ctx) if (fk.close_fail) rc = inject(&ctx->rm, -1); return rc; } + +/* L136: the simulated driver has one channel. Extra channels (AIEN_OMEGA_GPU_CHANNELS > 1) are refused here, so the + * platform's device_open refuses with stage "nvrm_channel_n"; the multi-channel path is proven on the chip only. */ +int nvrm_channel_n(Nvrm *rm, uint32_t extra) +{ + (void)rm; (void)extra; + return -1; +} + +int nvrm_chan_submit(Nvrm *rm, uint32_t ch, const NvrmMem *pb, uint32_t off_bytes, uint32_t nwords) +{ + (void)rm; (void)ch; (void)pb; (void)off_bytes; (void)nwords; + return -1; +} + +void nvrm_chan_retire(Nvrm *rm, uint32_t ch, uint32_t upto) +{ + (void)rm; (void)ch; (void)upto; +} + +uint32_t nvrm_chan_put(const Nvrm *rm, uint32_t ch) +{ + (void)rm; (void)ch; + return 0; +} diff --git a/tests/gpu_gemm_test.c b/tests/gpu_gemm_test.c new file mode 100644 index 000000000..b60f70964 --- /dev/null +++ b/tests/gpu_gemm_test.c @@ -0,0 +1,337 @@ +/* gpu_gemm_test.c -- row C3 of docs/OMEGA.md: matrix multiply of any shape, BF16 and FP8 (e4m3), both layouts of A and B, + * on the GB10 tensor cores, checked bit for bit against a host model of the same tensor-core steps (src/omega_gpu_gemm.h). + * + * --host-only no device: refusals, every kernel (both types, several shapes of tile, the red-check mutants) through our + * code generator and encoder, register counts + * --sim the real kernels in the warp simulator (tensor-core ops computed with the chip-exact models), small shapes + * over every edge case, all four layouts, three tile configurations; the three mutants must be caught + * (no flag) the GB10: at least 200 shapes with M, N, K from 1 to 8192, both types, all four layouts. Every output word + * must be written; shapes up to 2^27 multiply-adds are compared in full, larger ones on 4096 sampled + * elements plus the last row and column. Speed: 2 M N K / kernel time, as a fraction of the measured + * tensor-core ceiling (BF16 HMMA 115.36 TFLOPS, evidence OWN30B-T3-TC-PEAK-BW-5faa0f70-20261013T2236Z; + * FP8 QMMA 237.52 TFLOPS, L151 as quoted in evidence/L403-roof-20261022). The mutants must be caught. + * --quick: 24 shapes, for a first look. + * Last line: "C3 gemm : PASS ( checks)" or "... FAIL ( of )". */ +#include "omega_gpu_gemm.h" +#include "omega_gpu_session.h" +#include "bw_warp_sim_tc.h" + +#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 const HmmaModel HM = { 16, 2, 0, 2, 0 }; /* chip-exact, evidence OWN30B-T3-HMMA-MODEL */ +static const QmmaModel QM = { 32, 26, 0, 2, 0 }; /* chip-exact, evidence OWN30B-L1G-QMMA-PFGEMM-c742aa13 */ +static const char *tname(OmegaGpuGemmType t) { return t == OMEGA_GPU_GEMM_BF16 ? "bf16" : "e4m3"; } +static const char *lname(unsigned l) { static const char *n[4] = { "A row B row", "A col B row", "A row B col", "A col B col" }; return n[l & 3u]; } + +static uint64_t g_rng = 1; +static uint32_t rnd(void) { g_rng = g_rng * 6364136223846793005ull + 1442695040888963407ull; return (uint32_t)(g_rng >> 33); } + +/* inputs: bf16 values 0 or +-(1 + m/128) 2^e, e in -8..8; e4m3 any code except the two NaNs */ +static void fill(OmegaGpuGemmType t, void *p, uint64_t n, uint64_t seed) { + g_rng = seed * 0x2545f4914f6cdd1dull + 7; + if (t == OMEGA_GPU_GEMM_BF16) { + uint16_t *h = p; + for (uint64_t i = 0; i < n; i++) { + uint32_t r = rnd(); + h[i] = r % 61u == 0 ? 0 : (uint16_t)(((r >> 8) & 1u) << 15 | (uint32_t)(127 + (int)((r >> 9) % 17u) - 8) << 7 | (r >> 16 & 0x7fu)); + } + } else { + uint8_t *b = p; + for (uint64_t i = 0; i < n; i++) { uint8_t c = (uint8_t)rnd(); b[i] = (c & 0x7fu) == 0x7fu ? (uint8_t)(c ^ 1u) : c; } + } +} + +static float val(OmegaGpuGemmType t, const void *p, uint64_t i) { + if (t == OMEGA_GPU_GEMM_BF16) { uint32_t u = (uint32_t)((const uint16_t *)p)[i] << 16; float f; memcpy(&f, &u, 4); return f; } + return qm_e4m3(((const uint8_t *)p)[i]); +} + +/* the host model: one tensor-core step per 16 (bf16) or 32 (e4m3) values of k, in increasing k, from +0 */ +static float ref_elem(OmegaGpuGemmType t, unsigned lay, uint32_t M, uint32_t N, uint32_t K, const void *A, const void *B, uint32_t m, uint32_t n) { + const uint32_t ks = t == OMEGA_GPU_GEMM_BF16 ? 16u : 32u; + float acc = 0.0f; + for (uint32_t k0 = 0; k0 < K; k0 += ks) { + float av[32], bv[32]; + for (uint32_t k = 0; k < ks; k++) { + uint32_t kk = k0 + k; + if (kk >= K) { av[k] = bv[k] = 0.0f; continue; } + av[k] = val(t, A, lay & OMEGA_GPU_GEMM_A_COL ? (uint64_t)kk * M + m : (uint64_t)m * K + kk); + bv[k] = val(t, B, lay & OMEGA_GPU_GEMM_B_COL ? (uint64_t)n * K + kk : (uint64_t)kk * N + n); + } + acc = t == OMEGA_GPU_GEMM_BF16 ? hm_dot16(&HM, av, bv, acc) : qm_dot32(&QM, av, bv, acc); + } + return acc; +} + +static uint32_t f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } + +/* compare: every element when M N K <= 2^27, else 4096 samples + the last row and column; returns the mismatches */ +static uint64_t compare(OmegaGpuGemmType t, unsigned lay, uint32_t M, uint32_t N, uint32_t K, const void *A, const void *B, const float *C, + uint64_t *checked, char *first, size_t firstn) { + uint64_t bad = 0; *checked = 0; first[0] = 0; + #define ONE(mm, nn) do { uint32_t m_ = (mm), n_ = (nn); float w_ = ref_elem(t, lay, M, N, K, A, B, m_, n_), g_ = C[(uint64_t)m_ * N + n_]; (*checked)++; \ + if (f2u(w_) != f2u(g_)) { if (!bad) snprintf(first, firstn, "C[%u][%u] = %08x, model %08x", m_, n_, f2u(g_), f2u(w_)); bad++; } } while (0) + if ((uint64_t)M * N * K <= (1ull << 27)) { + for (uint32_t m = 0; m < M; m++) for (uint32_t n = 0; n < N; n++) ONE(m, n); + } else { + g_rng = (uint64_t)M * 31 + N * 7 + K; + for (int i = 0; i < 4096; i++) ONE(rnd() % M, rnd() % N); + for (uint32_t n = 0; n < N; n += N > 512 ? N / 512 : 1) ONE(M - 1, n); + for (uint32_t m = 0; m < M; m += M > 512 ? M / 512 : 1) ONE(m, N - 1); + ONE(M - 1, N - 1); + } + #undef ONE + return bad; +} + +/* ------------------------------------------------------------------ host only */ + +static void host_only(void) { + char w[200]; + uint16_t a[4] = { 0 }, b[4] = { 0 }; float c[4]; + check(omega_gpu_gemm(OMEGA_GPU_GEMM_BF16, 0, 0, 1, 1, a, b, c, NULL) == OMEGA_GPU_GEMM_BAD_ARGS, "M = 0 refused"); + check(omega_gpu_gemm(OMEGA_GPU_GEMM_BF16, 0, 1, 1, 0, a, b, c, NULL) == OMEGA_GPU_GEMM_BAD_ARGS, "K = 0 refused"); + check(omega_gpu_gemm(OMEGA_GPU_GEMM_BF16, 0, OMEGA_GPU_GEMM_MAX_DIM + 1, 1, 1, a, b, c, NULL) == OMEGA_GPU_GEMM_TOO_LARGE, "M past the maximum refused"); + check(omega_gpu_gemm(OMEGA_GPU_GEMM_BF16, 4, 1, 1, 1, a, b, c, NULL) == OMEGA_GPU_GEMM_BAD_ARGS, "unknown layout bits refused"); + check(omega_gpu_gemm((OmegaGpuGemmType)2, 0, 1, 1, 1, a, b, c, NULL) == OMEGA_GPU_GEMM_BAD_ARGS, "unknown type refused"); + check(omega_gpu_gemm(OMEGA_GPU_GEMM_E4M3, 0, 1, 1, 1, NULL, b, c, NULL) == OMEGA_GPU_GEMM_BAD_ARGS, "NULL A refused"); + OmegaGemmCfg bad; omega_gpu_gemm_default_cfg(OMEGA_GPU_GEMM_BF16, &bad); bad.rt = 3; + check(!omega_gpu_gemm_cfg_ok(&bad), "a tile of 3 row tiles refused"); + + /* the model itself is a multiply: within 2^-20 of the exact sum, relative to the sum of the magnitudes */ + for (int t = 0; t < 2; t++) for (unsigned lay = 0; lay < 4; lay++) { + enum { MM = 23, NN = 19, KK = 300 }; + static uint16_t A[MM * KK], B[KK * NN]; + fill((OmegaGpuGemmType)t, A, MM * KK, 5 + lay); fill((OmegaGpuGemmType)t, B, KK * NN, 9 + lay); + double worst = 0; + for (uint32_t m = 0; m < MM; m++) for (uint32_t n = 0; n < NN; n++) { + double ex = 0, mag = 0; + for (uint32_t k = 0; k < KK; k++) { + double p = (double)val((OmegaGpuGemmType)t, A, lay & OMEGA_GPU_GEMM_A_COL ? (uint64_t)k * MM + m : (uint64_t)m * KK + k) * + (double)val((OmegaGpuGemmType)t, B, lay & OMEGA_GPU_GEMM_B_COL ? (uint64_t)n * KK + k : (uint64_t)k * NN + n); + ex += p; mag += fabs(p); + } + double d = fabs((double)ref_elem((OmegaGpuGemmType)t, lay, MM, NN, KK, A, B, m, n) - ex) / (mag > 0 ? mag : 1); + if (d > worst) worst = d; + } + snprintf(w, sizeof w, "the %s model is a multiply (%s): worst error %.3g of the magnitude sum", tname((OmegaGpuGemmType)t), lname(lay), worst); + check(worst < ldexp(1.0, -20), w); + printf("%s\n", w); + } + + const OmegaGemmCfg shapes[] = { { 0, 2, 4, 2, 2, 4, 0 }, { 0, 1, 1, 1, 1, 4, 0 }, { 0, 4, 1, 2, 2, 2, 0 }, { 0, 1, 4, 4, 1, 2, 0 }, { 0, 2, 2, 1, 4, 8, 0 } }; + OmegaGemmCfg big; omega_gpu_gemm_default_cfg(OMEGA_GPU_GEMM_BF16, &big); big.rt = 4; big.ct = 2; + check(!omega_gpu_gemm_cfg_ok(&big), "a tile past the register budget (rt 4 ct 2) refused"); + for (int t = 0; t < 2; t++) + for (size_t s = 0; s < sizeof shapes / sizeof shapes[0]; s++) + for (int mut = 0; mut <= 3; mut++) + for (int k = 0; k < OMEGA_GEMM_K_COUNT; k++) { + OmegaGemmCfg c = shapes[s]; c.type = (OmegaGpuGemmType)t; c.mutant = mut; + BlackwellIRProgram *p = calloc(1, sizeof *p); + int rc = omega_gpu_gemm_codegen(&c, (OmegaGemmKind)k, 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; + int np = enc == 0 ? omega_qmma_patch_code(p, code, bytes) : -1; + snprintf(w, sizeof w, "%s rt %u ct %u wm %u wn %u kernel %d mutant %d: codegen %d, encode %d, qmma patches %d, %u gprs", tname(c.type), + c.rt, c.ct, c.wm, c.wn, k, mut, rc, enc, np, (unsigned)kk.gpr_count); + check(rc == 0 && enc == 0 && kk.code_size == bytes && np >= 0 && (t == 1 || np == 0) && omega_bw_gpr_declared(kk.gpr_count) <= 255u, w); + if (mut == 0) printf("%s\n", w); + free(code); free(p); + } +} + +/* ------------------------------------------------------------------ the simulator */ + +typedef struct { + BlackwellIRProgram *prog[OMEGA_GEMM_K_COUNT]; + WsRange ranges[5]; + int order; + char err[300]; +} SimCtx; + +static int sim_launch(void *ctx, OmegaGemmKind k, const uint32_t a[OMEGA_GPU_GEMM_ARGS_WORDS], uint32_t gx, uint32_t gy, uint32_t threads) { + SimCtx *c = ctx; + static WsSim s; + memset(&s, 0, sizeof s); + s.p = c->prog[k]; s.threads = threads; s.order = c->order; + for (unsigned i = 0; i < OMEGA_GPU_GEMM_ARGS_WORDS; i++) s.cbank[0x380 / 4 + i] = a[i]; + memcpy(s.ranges, c->ranges, sizeof c->ranges); s.nranges = 5; + if (ws_run(&s, gx, gy) != 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, const OmegaGemmCfg *cfg) { + for (int k = 0; k < OMEGA_GEMM_K_COUNT; k++) { + c->prog[k] = calloc(1, sizeof *c->prog[k]); + if (!c->prog[k] || omega_gpu_gemm_codegen(cfg, (OmegaGemmKind)k, c->prog[k]) != 0) return -1; + } + return 0; +} +static void sim_free(SimCtx *c) { for (int k = 0; k < OMEGA_GEMM_K_COUNT; k++) { free(c->prog[k]); c->prog[k] = NULL; } } + +/* one multiply through the simulator; 0 when it ran and every output word was written */ +static int sim_gemm(SimCtx *c, const OmegaGemmCfg *cfg, unsigned lay, uint32_t M, uint32_t N, uint32_t K, const void *A, const void *B, float *C) { + OmegaGemmPlan p; + if (omega_gpu_gemm_plan(cfg, M, N, K, &p) != 0) return -2; + uint8_t *ap = malloc(p.ap_bytes), *bp = malloc(p.bp_bytes); + memset(ap, 0xa5, p.ap_bytes); memset(bp, 0x5a, p.bp_bytes); + for (uint64_t i = 0; i < (uint64_t)M * N; i++) { uint32_t u = 0x7fa00badu; memcpy(&C[i], &u, 4); } + c->ranges[0] = (WsRange){ (uintptr_t)A, (uintptr_t)A + p.a_bytes + 1 }; /* the e4m3 pack reads a u16 around the last byte */ + c->ranges[1] = (WsRange){ (uintptr_t)B, (uintptr_t)B + p.b_bytes + 1 }; + c->ranges[2] = (WsRange){ (uintptr_t)ap, (uintptr_t)ap + p.ap_bytes }; + c->ranges[3] = (WsRange){ (uintptr_t)bp, (uintptr_t)bp + p.bp_bytes }; + c->ranges[4] = (WsRange){ (uintptr_t)C, (uintptr_t)(C + (uint64_t)M * N) }; + int rc = omega_gpu_gemm_drive(cfg, lay, M, N, K, (uintptr_t)A, (uintptr_t)B, (uintptr_t)ap, (uintptr_t)bp, (uintptr_t)C, sim_launch, c); + for (uint64_t i = 0; rc == 0 && i < (uint64_t)M * N; i++) if (f2u(C[i]) == 0x7fa00badu) rc = -9; + free(ap); free(bp); + return rc; +} + +static void sim_tier(void) { + char w[600], first[200]; + static const uint32_t sh[][3] = { { 1, 1, 1 }, { 1, 1, 17 }, { 16, 8, 16 }, { 17, 9, 33 }, { 64, 64, 64 }, { 65, 63, 100 }, + { 3, 130, 70 }, { 100, 3, 257 }, { 129, 65, 31 }, { 31, 200, 129 } }; + const size_t nsh = sizeof sh / sizeof sh[0]; + const OmegaGemmCfg cfgs[] = { { 0, 2, 4, 2, 2, 4, 0 }, { 0, 1, 1, 1, 1, 4, 0 }, { 0, 4, 1, 1, 4, 2, 0 } }; + void *A = malloc(300 * 300 * 2), *B = malloc(300 * 300 * 2); + float *C = malloc(300 * 300 * 4); + for (int t = 0; t < 2; t++) + for (size_t ci = 0; ci < sizeof cfgs / sizeof cfgs[0]; ci++) { + OmegaGemmCfg cfg = cfgs[ci]; cfg.type = (OmegaGpuGemmType)t; + SimCtx c; memset(&c, 0, sizeof c); + check(sim_build(&c, &cfg) == 0, "simulator: kernels built"); + uint64_t runs = 0, wrong = 0; + for (size_t s = 0; s < nsh; s++) + for (unsigned lay = 0; lay < 4; lay++) { + if (ci > 0 && lay != (unsigned)(s % 4)) continue; /* the other tile shapes: one layout per shape */ + const uint32_t M = sh[s][0], N = sh[s][1], K = sh[s][2]; + fill(cfg.type, A, (uint64_t)M * K, s * 4 + lay); fill(cfg.type, B, (uint64_t)K * N, s * 4 + lay + 1000); + c.order = (int)(s & 1u); + int rc = sim_gemm(&c, &cfg, lay, M, N, K, A, B, C); + uint64_t checked = 0, bad = rc == 0 ? compare(cfg.type, lay, M, N, K, A, B, C, &checked, first, sizeof first) : 1; + runs++; + if (rc != 0 || bad) { + wrong++; + snprintf(w, sizeof w, "sim %s rt %u ct %u wm %u wn %u, %ux%ux%u %s: rc %d, %" PRIu64 " wrong of %" PRIu64 " %s %s", tname(cfg.type), cfg.rt, + cfg.ct, cfg.wm, cfg.wn, M, N, K, lname(lay), rc, bad, checked, first, c.err); + check(0, w); + } + } + snprintf(w, sizeof w, "simulator %s rt %u ct %u wm %u wn %u: %" PRIu64 " multiplies, %" PRIu64 " wrong", tname(cfg.type), cfg.rt, cfg.ct, cfg.wm, cfg.wn, runs, wrong); + check(wrong == 0, w); + printf("%s\n", w); + sim_free(&c); + } + /* red checks: each mutant must change a result the real kernels get right */ + for (int t = 0; t < 2; t++) + for (int mut = 1; mut <= 3; mut++) { + OmegaGemmCfg cfg; omega_gpu_gemm_default_cfg((OmegaGpuGemmType)t, &cfg); cfg.mutant = mut; + SimCtx c; memset(&c, 0, sizeof c); + const uint32_t M = 65, N = 63, K = 100; + fill(cfg.type, A, M * K, 77); fill(cfg.type, B, K * N, 78); + int built = sim_build(&c, &cfg) == 0, rc = built ? sim_gemm(&c, &cfg, 0, M, N, K, A, B, C) : -1; + uint64_t checked = 0, bad = rc == 0 ? compare(cfg.type, 0, M, N, K, A, B, C, &checked, first, sizeof first) : 1; + snprintf(w, sizeof w, "simulator red check: %s mutant %d is caught (%" PRIu64 " wrong)", tname(cfg.type), mut, bad); + check(built && bad > 0, w); + printf("%s\n", w); + sim_free(&c); + } + free(A); free(B); free(C); +} + +/* ------------------------------------------------------------------ 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 const double CEIL_TFLOPS[2] = { 115.36, 237.52 }; + +static void chip_one(OmegaGpuGemmType t, unsigned lay, uint32_t M, uint32_t N, uint32_t K, void *A, void *B, float *C) { + char w[600], first[200]; + fill(t, A, (uint64_t)M * K, (uint64_t)M * 3 + N * 5 + K * 7 + lay); + fill(t, B, (uint64_t)K * N, (uint64_t)M * 11 + N * 13 + K * 17 + lay); + OmegaGpuGemmInfo info; + int rc = omega_gpu_gemm(t, lay, M, N, K, A, B, C, &info); + uint64_t checked = 0, bad = rc == 0 ? compare(t, lay, M, N, K, A, B, C, &checked, first, sizeof first) : 1; + double flop = 2.0 * M * N * K, tf = info.main_ns ? flop / (double)info.main_ns / 1e3 : 0, tfp = info.main_ns ? flop / (double)(info.main_ns + info.pack_ns) / 1e3 : 0; + snprintf(w, sizeof w, "%s %ux%ux%u %s: %s, %" PRIu64 " checked, %" PRIu64 " wrong%s%s; kernel %.3f ms %.2f TFLOPS = %.4f of ceiling, with packs %.2f TFLOPS = %.4f%s%s", + tname(t), M, N, K, lname(lay), omega_gpu_gemm_rc_name(rc), checked, bad, bad && rc == 0 ? ", first " : "", bad && rc == 0 ? first : "", + info.main_ns / 1e6, tf, tf / CEIL_TFLOPS[t], tfp, tfp / CEIL_TFLOPS[t], rc ? ": " : "", rc ? omega_gpu_session_last_error() : ""); + check(rc == 0 && bad == 0, w); + if (rc == 0 && bad == 0) printf("ok: %s\n", w); +} + +static void chip_tier(int quick) { + char w[200]; + const size_t need = (size_t)8192 * 8192 * 2 * 2 + (size_t)8192 * 8192 * 4 + ((size_t)8 << 30); + if (!quick && mem_available() < need * 2) { printf("NOT_RUN: MemAvailable %zu MiB < %zu MiB\n", mem_available() >> 20, need * 2 >> 20); exit(2); } + const uint32_t big = quick ? 1024u : 8192u; + void *A = malloc((size_t)big * big * 2), *B = malloc((size_t)big * big * 2); + float *C = malloc((size_t)big * big * 4); + if (!A || !B || !C) { printf("NOT_RUN: host allocation\n"); exit(2); } + /* the shapes: per type and layout, the same list (26 each, 208 in all): edges, odd sizes, skinny, square up to 8192 */ + static const uint32_t sh[][3] = { + { 1, 1, 1 }, { 1, 8192, 1 }, { 8192, 1, 1 }, { 1, 1, 8192 }, { 7, 13, 29 }, { 16, 8, 16 }, { 33, 65, 127 }, { 100, 100, 100 }, + { 128, 64, 64 }, { 255, 257, 511 }, { 1000, 999, 1001 }, { 1, 4096, 4096 }, { 4096, 1, 4096 }, { 4096, 4096, 1 }, + { 17, 8192, 3000 }, { 8192, 17, 3000 }, { 1024, 1024, 1024 }, { 2048, 2048, 2048 }, { 4096, 4096, 4096 }, { 3000, 5000, 700 }, + { 8192, 8192, 64 }, { 64, 8192, 8192 }, { 8192, 64, 8192 }, { 6000, 7000, 8000 }, { 8191, 8191, 8191 }, { 8192, 8192, 8192 } }; + const size_t nsh = quick ? 12 : sizeof sh / sizeof sh[0]; + int before = g_failed, shapes = 0; + for (int t = 0; t < 2; t++) + for (unsigned lay = 0; lay < 4; lay++) + for (size_t s = 0; s < nsh; s++) { + if (quick && lay != (unsigned)(s % 4)) continue; + uint32_t M = sh[s][0] > big ? big : sh[s][0], N = sh[s][1] > big ? big : sh[s][1], K = sh[s][2] > big ? big : sh[s][2]; + chip_one((OmegaGpuGemmType)t, lay, M, N, K, A, B, C); + shapes++; + } + printf("%d shapes, both types, all four layouts: %s\n", shapes, g_failed == before ? "PASS" : "FAIL"); + for (int t = 0; t < 2; t++) + for (int mut = 1; mut <= 3; mut++) { + OmegaGemmCfg cfg; omega_gpu_gemm_default_cfg((OmegaGpuGemmType)t, &cfg); cfg.mutant = mut; + omega_gpu_gemm_test_set_cfg((OmegaGpuGemmType)t, &cfg); + const uint32_t M = 65, N = 63, K = 100; + fill((OmegaGpuGemmType)t, A, M * K, 77); fill((OmegaGpuGemmType)t, B, K * N, 78); + int rc = omega_gpu_gemm((OmegaGpuGemmType)t, 0, M, N, K, A, B, C, NULL); + char first[200]; uint64_t checked = 0, bad = rc == 0 ? compare((OmegaGpuGemmType)t, 0, M, N, K, A, B, C, &checked, first, sizeof first) : 1; + snprintf(w, sizeof w, "chip red check: %s mutant %d is caught (%s, %" PRIu64 " wrong)", tname((OmegaGpuGemmType)t), mut, omega_gpu_gemm_rc_name(rc), bad); + check(bad > 0, w); + printf("%s\n", w); + omega_gpu_gemm_test_set_cfg((OmegaGpuGemmType)t, NULL); + } + for (int t = 0; t < 2; t++) chip_one((OmegaGpuGemmType)t, 3, 65, 63, 100, A, B, C); /* 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 */ + printf("after the device closed and opened again:\n"); + for (int t = 0; t < 2; t++) chip_one((OmegaGpuGemmType)t, 0, 70, 33, 129, A, B, C); + free(A); free(B); free(C); +} + +int main(int argc, char **argv) { + const char *tier = "chip"; + int quick = 0; + 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], "--quick")) quick = 1; + else { fprintf(stderr, "usage: %s [--host-only | --sim | --quick]\n", argv[0]); return 2; } + } + if (!strcmp(tier, "host")) host_only(); + else if (!strcmp(tier, "sim")) { host_only(); sim_tier(); } + else chip_tier(quick); + if (g_failed) printf("C3 gemm %s: FAIL (%d of %d)\n", tier, g_failed, g_checks); + else printf("C3 gemm %s: PASS (%d checks)\n", tier, g_checks); + return g_failed ? 1 : 0; +} diff --git a/tests/gpu_isolation_test.c b/tests/gpu_isolation_test.c new file mode 100644 index 000000000..786629e31 --- /dev/null +++ b/tests/gpu_isolation_test.c @@ -0,0 +1,371 @@ +/* gpu_isolation_test.c -- row C5 of docs/OMEGA.md: two processes on one GPU, and a never-ending kernel, on the GB10. + * + * --host-only codegen + encode of the work kernel, its mutant and the never-ending kernel; the simulator runs the work + * kernel exactly and catches the mutant; the never-ending kernel never reaches its exit in the simulator + * (no flag) the chip, under the GB10 lock, every part in its own child process: + * hang a work launch (values checked), then a never-ending kernel (one warp: count, branch back) with a + * HANG_MS timeout: the launch fails "timed out" and the process latches; red check: a work launch is + * refused "process latched"; omega_gpu_session_recover() closes the device (the RM client close destroys + * the channel and the grid on it); the work kernel uploaded again on the new device runs, values right. + * two two processes, each its own device, ROUNDS work launches with their own inputs: both all right. + * two-hang process 1 runs ROUNDS work launches while process 2 runs the hang part: process 1 all right (unharmed), + * process 2 recovers and is right too. + * Kernels from Omega's IR (omega_bw_em.h), our own sm_121 encoder; no CUDA. */ +#include "omega_gpu_cmdlist.h" +#include "omega_gpu_session.h" +#include "bw_warp_sim.h" /* brings math.h, whose fadd() / ffma() would hide the emitter's */ +#define fadd em_fadd +#define ffma em_ffma +#include "omega_bw_em.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#define THREADS 128u +#define CTAS 32u +#define NEL (THREADS * CTAS) +#define ROUNDS 300 +#define HANG_MS 2000ull +#define POISON 0x7fa00badu +#define ARGS 5u /* in u64, out u64, tag f32 */ + +static int g_failed, g_checks; +static void check(int ok, const char *what) { + g_checks++; + if (!ok) g_failed++; + printf("[%d] %s: %s\n", (int)getpid(), ok ? "ok" : "FAIL", what); + fflush(stdout); +} +static uint32_t f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } +static uint64_t now_ns(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return (uint64_t)t.tv_sec * 1000000000ull + (uint64_t)t.tv_nsec; } + +/* ------------------------------------------------------------------ the kernels */ + +typedef struct { int bssy, bra; } Region; +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); } + +/* work: out[gid] = in[gid] * 2 + tag (mutant 1: * 2 + tag + 1) */ +static int gen_work(Em *e, int mutant) { + int udesc = UV64(e), pin = V64(e), pout = V64(e), a64 = V64(e), tag = V(e), n = V(e), tid = V(e), cta = V(e), gid = V(e), two = V(e), v = V(e), one = V(e); + if (e->err) return -1; + ldcu64(e, udesc, 0x358); + ldc64(e, pin, 0x380); ldc64(e, pout, 0x388); ldc32(e, tag, 0x390); + s2r(e, tid, BW_SR_TID_X); s2r(e, cta, BW_SR_CTAID_X); + imadi(e, gid, cta, 0, THREADS, tid); + movi(e, n, NEL); movi(e, two, 0x40000000u); movi(e, one, 0x3f800000u); + isetp_ge_u32(e, gid, n); + { + Region r = skip_if_p0(e); + imadw(e, a64, gid, 4u, pin); ldg32(e, v, a64, udesc, 1); + ffma(e, v, v, two, 0, tag); + if (mutant == 1) fadd(e, v, v, one); + imadw(e, a64, gid, 4u, pout); stg(e, a64, 0, v, udesc); + region_end(e, r); + } + tail(e); + return e->err; +} + +/* never-ending: x = x + 1, branch back, for ever; the exit after the loop is never reached */ +static int gen_hang(Em *e) { + int x = V(e), one = V(e); + if (e->err) return -1; + movi(e, x, 0); movi(e, one, 1); + const int top = here(e); + loop_begin(e); + iadd(e, x, x, one); + bra(e, top); + loop_end(e); + tail(e); + return e->err; +} + +static int codegen(int hang, int mutant, BlackwellIRProgram *prog) { + omega_bw_ir_init(prog); + Em em; memset(&em, 0, sizeof em); em.p = prog; + if ((hang ? gen_hang(&em) : gen_work(&em, mutant)) != 0 || em.err || em.depth != 0) return -1; + return omega_bw_regalloc_solve(prog); +} + +static float input(unsigned i, unsigned seed) { return (float)((i * 2654435761u + seed * 40503u) % 100000u); } +static float expect(unsigned i, unsigned seed, float tag) { return input(i, seed) * 2.0f + tag; } + +/* ------------------------------------------------------------------ host */ + +static const char *g_dump; /* --dump-hang PATH: code bytes for the disassembler; PATH must not already exist */ + +static void host_only(void) { + char w[200]; + for (int k = 0; k < 3; k++) { + BlackwellIRProgram *p = calloc(1, sizeof *p); + size_t need = 0; + int ok = p && codegen(k == 2, k == 1, p) == 0 && (need = omega_bw_program_code_bytes(p)) > 0; + void *code = ok ? calloc(1, need) : NULL; + OmegaBlackwellKernel kk; + ok = ok && code && omega_blackwell_kernel_encode_into(p, code, need, &kk) == 0 && kk.code_size == need; + if (ok && k == 2 && g_dump) { + /* Never truncate an existing file or follow a final-component symlink. + * A new dump is private even with a permissive caller umask. */ + int fd = open(g_dump, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, 0600); + FILE *f = fd >= 0 ? fdopen(fd, "wb") : NULL; + ok = f && fwrite(code, 1, need, f) == need; + if (f) { if (fclose(f) != 0) ok = 0; } + else if (fd >= 0) close(fd); + if (!ok) fprintf(stderr, "cannot write private kernel dump: %s (requires a new path)\n", g_dump); + } + snprintf(w, sizeof w, "codegen + encode: %s (%zu bytes)", k == 0 ? "work kernel" : k == 1 ? "work kernel mutant 1" : "never-ending kernel", need); + check(ok, w); + free(code); free(p); + } + static float in[NEL], out[NEL]; + WsRange rg[2] = { { (uintptr_t)in, (uintptr_t)(in + NEL) }, { (uintptr_t)out, (uintptr_t)(out + NEL) } }; + for (int mut = 0; mut < 2; mut++) { + BlackwellIRProgram *p = calloc(1, sizeof *p); + static WsSim s; + memset(&s, 0, sizeof s); + for (unsigned i = 0; i < NEL; i++) { in[i] = input(i, 7); uint32_t pz = POISON; memcpy(&out[i], &pz, 4); } + int rc = !p || codegen(0, mut, p) != 0; + if (!rc) { + const uint64_t a = (uintptr_t)in, b = (uintptr_t)out; + s.p = p; s.threads = THREADS; + s.cbank[0x380 / 4] = (uint32_t)a; s.cbank[0x384 / 4] = (uint32_t)(a >> 32); + s.cbank[0x388 / 4] = (uint32_t)b; s.cbank[0x38c / 4] = (uint32_t)(b >> 32); s.cbank[0x390 / 4] = f2u(3.0f); + memcpy(s.ranges, rg, sizeof rg); s.nranges = 2; + rc = ws_run(&s, CTAS, 1); + } + unsigned bad = 0; + for (unsigned i = 0; i < NEL; i++) bad += f2u(out[i]) != f2u(expect(i, 7, 3.0f)); + snprintf(w, sizeof w, mut ? "simulator red check: work mutant caught (%u wrong)" : "simulator: work kernel exact on all %u elements", mut ? bad : NEL); + check(mut ? rc != 0 || bad > 0 : rc == 0 && bad == 0, w); + free(p); + } +} + +/* ------------------------------------------------------------------ the chip */ + +typedef struct { BlackwellIRProgram prog; OmegaGpuMem code; uint32_t gpr; } Kern; + +/* lock held */ +static int upload(Kern *k, int hang) { + memset(&k->code, 0, sizeof k->code); + if (codegen(hang, 0, &k->prog) != 0) return -1; + size_t need = omega_bw_program_code_bytes(&k->prog); + if (need == 0 || omega_gpu_session_alloc_code(need, &k->code) != 0) return -1; + OmegaBlackwellKernel kk; + if (omega_blackwell_kernel_encode_into(&k->prog, k->code.cpu, need, &kk) != 0 || omega_gpu_session_sync_to_gpu(&k->code) != 0) return -1; + k->gpr = omega_bw_gpr_declared(kk.gpr_count); + if (k->gpr < 64u) k->gpr = 64u; + return 0; +} + +static Kern g_work, g_hang; +static OmegaGpuMem g_buf; /* [in | out] */ + +/* lock held, device open: device buffer + work kernel */ +static int setup(void) { + if (!omega_gpu_session_open()) return -1; + if (omega_gpu_session_alloc(2u * NEL * 4u, &g_buf) != 0) return -1; + return upload(&g_work, 0); +} + +/* lock held: one work launch with inputs from seed; returns the count of wrong outputs, or -1 if the launch failed */ +static long work_once(unsigned seed, float tag, uint64_t *ns) { + float *in = g_buf.cpu, *out = in + NEL; + for (unsigned i = 0; i < NEL; i++) { in[i] = input(i, seed); uint32_t pz = POISON; memcpy(&out[i], &pz, 4); } + if (omega_gpu_session_sync_to_gpu(&g_buf) != 0) return -1; + uint32_t a[ARGS]; + const uint64_t vi = g_buf.va, vo = g_buf.va + NEL * 4u; + a[0] = (uint32_t)vi; a[1] = (uint32_t)(vi >> 32); a[2] = (uint32_t)vo; a[3] = (uint32_t)(vo >> 32); a[4] = f2u(tag); + OmegaGpuLaunch L = { .code_va = g_work.code.va, .gpr_count = g_work.gpr, .threads_x = THREADS, .threads_y = 1, .grid_x = CTAS, .grid_y = 1, + .num_elements = NEL, .args = a, .n_args = ARGS, .timeout_ms = 20000 }; + uint64_t t = 0; + if (omega_gpu_session_launch(&L, &t, NULL) != 0) return -1; + if (ns) *ns = t; + if (omega_gpu_session_sync_to_cpu(&g_buf) != 0) return -1; + long bad = 0; + for (unsigned i = 0; i < NEL; i++) bad += f2u(out[i]) != f2u(expect(i, seed, tag)); + return bad; +} + +/* Child processes talk to the parent and to each other over pipes (one byte per event, then raw timestamps). */ +static void say(int fd, char c) { if (fd >= 0 && write(fd, &c, 1) != 1) printf("[%d] pipe write failed\n", (int)getpid()); } +static int hear(int fd) { char c = 0; return read(fd, &c, 1) == 1 ? c : -1; } +static int heard_now(int fd) { /* non-blocking: 1 when a byte is waiting */ + fd_set s; FD_ZERO(&s); FD_SET(fd, &s); + struct timeval tv = { 0, 0 }; + return select(fd + 1, &s, NULL, NULL, &tv) == 1; +} + +/* the hang part; sig_fd (or -1) gets 'H' right before the never-ending launch and 'D' when the part is over */ +static int part_hang(int sig_fd) { + char w[300]; + omega_gpu_session_lock(); + if (setup() != 0) { check(0, "hang part: device, buffer, work kernel"); printf(" %s\n", omega_gpu_session_last_error()); omega_gpu_session_unlock(); say(sig_fd, 'D'); return 1; } + uint64_t ns0 = 0; + long bad = work_once(11, 1.0f, &ns0); + snprintf(w, sizeof w, "hang part: a work launch before the hang is right (%ld wrong, %.3f ms)", bad, ns0 / 1e6); + check(bad == 0, w); + check(upload(&g_hang, 1) == 0, "hang part: never-ending kernel uploaded"); + OmegaGpuCmdList *old_list = NULL; /* made on the device the recovery throws away */ + omega_gpu_session_unlock(); + check(omega_gpu_cmdlist_create(2, &old_list) == 0, "hang part: a command list made before the hang"); + omega_gpu_session_lock(); + uint32_t dummy = 0; /* the kernel reads no argument; a launch needs at least one word */ + OmegaGpuLaunch H = { .code_va = g_hang.code.va, .gpr_count = g_hang.gpr, .threads_x = 32, .threads_y = 1, .grid_x = 1, .grid_y = 1, + .num_elements = 32, .args = &dummy, .n_args = 1, .timeout_ms = HANG_MS }; + say(sig_fd, 'H'); + const uint64_t t0 = now_ns(); + int rc = omega_gpu_session_launch(&H, NULL, NULL); + const double waited = (now_ns() - t0) / 1e6; + snprintf(w, sizeof w, "the never-ending kernel times out after %.0f ms (rc %d: %s), the process latches", waited, rc, omega_gpu_session_last_error()); + check(rc != 0 && strstr(omega_gpu_session_last_error(), "timed out") && omega_gpu_session_is_blocked() && waited >= (double)HANG_MS * 0.9, w); + bad = work_once(12, 1.0f, NULL); + snprintf(w, sizeof w, "red check: while latched a work launch is refused (%s)", omega_gpu_session_last_error()); + check(bad == -1 && strstr(omega_gpu_session_last_error(), "process latched") != NULL, w); + omega_gpu_session_unlock(); + + const uint64_t r0 = now_ns(); + rc = omega_gpu_session_recover(); + const double rec_ms = (now_ns() - r0) / 1e6; + /* the driver's clean close of the RM client is what says the channel, and the grid on it, are gone: a teardown fault + * would keep the session latched (tests/test_omega_gpu_session_platform.c RECOVER_TEARDOWN_FAULT_STAYS_LATCHED) */ + snprintf(w, sizeof w, "recover: the device closed with no driver teardown fault in %.1f ms (rc %d %s)", rec_ms, rc, rc ? omega_gpu_session_last_error() : ""); + check(rc == 0 && !omega_gpu_session_is_blocked() && omega_gpu_session_recoveries() == 1, w); + memset(&g_buf, 0, sizeof g_buf); /* handles of the old device are gone */ + omega_gpu_session_lock(); + if (setup() != 0) { check(0, "after recover: new device, buffer, work kernel"); printf(" %s\n", omega_gpu_session_last_error()); omega_gpu_session_unlock(); say(sig_fd, 'D'); return 1; } + uint64_t ns1 = 0, ns2 = 0; + bad = work_once(13, 4.0f, &ns1); + long bad2 = work_once(14, 5.0f, &ns2); + snprintf(w, sizeof w, "after recover, same process: work launches right (%ld, %ld wrong; %.3f, %.3f ms vs %.3f ms before the hang), device opens %u", + bad, bad2, ns1 / 1e6, ns2 / 1e6, ns0 / 1e6, omega_gpu_session_open_count()); + check(bad == 0 && bad2 == 0, w); + { + uint32_t a1 = 0; + OmegaGpuLaunch L = { .code_va = g_work.code.va, .gpr_count = g_work.gpr, .threads_x = 32, .threads_y = 1, .grid_x = 1, .grid_y = 1, + .num_elements = 32, .args = &a1, .n_args = 1 }; + int refused = old_list && omega_gpu_cmdlist_add(old_list, &L) == -1 && strstr(omega_gpu_session_last_error(), "before a session recovery") != NULL; + snprintf(w, sizeof w, "a command list from before the recovery is refused (%s)", omega_gpu_session_last_error()); + check(refused, w); + } + omega_gpu_session_unlock(); + omega_gpu_cmdlist_free(old_list); + omega_gpu_session_close(); + say(sig_fd, 'D'); + return g_failed ? 1 : 0; +} + +typedef struct { uint64_t start, end; uint32_t launches, during; } Span; + +/* work launches with own inputs. ready_fd gets 'R' once set up; then it waits for one byte on go_fd. It runs for at + * least RUN_MS and ROUNDS launches; with a hang_fd it waits for 'H' first and keeps going until 'D', counting the + * launches made in between. Writes its Span to span_fd. */ +#define RUN_MS 3000ull +static int part_rounds(unsigned seed_base, const char *who, int ready_fd, int go_fd, int hang_fd, int span_fd) { + char w[300]; + omega_gpu_session_lock(); + if (setup() != 0) { check(0, "rounds: device, buffer, work kernel"); printf(" %s\n", omega_gpu_session_last_error()); omega_gpu_session_unlock(); say(ready_fd, 'X'); return 1; } + say(ready_fd, 'R'); + if (go_fd >= 0) (void)hear(go_fd); + if (hang_fd >= 0 && hear(hang_fd) != 'H') { check(0, "rounds: the hang process never started its hang"); omega_gpu_session_unlock(); return 1; } + Span sp = { now_ns(), 0, 0, 0 }; + long wrong_total = 0; int failed = 0, hang_live = hang_fd >= 0; + uint64_t ns, worst = 0; + for (unsigned r = 0;; r++) { + if (hang_live && heard_now(hang_fd)) { (void)hear(hang_fd); hang_live = 0; } + if (!hang_live && r >= ROUNDS && now_ns() - sp.start >= RUN_MS * 1000000ull) break; + long bad = work_once(seed_base + r, (float)(r % 7), &ns); + if (bad < 0) { failed++; printf(" [%d] launch %u: %s\n", (int)getpid(), r, omega_gpu_session_last_error()); break; } + wrong_total += bad; + sp.launches++; + if (hang_live) sp.during++; + if (ns > worst) worst = ns; + } + sp.end = now_ns(); + snprintf(w, sizeof w, "%s: %u work launches over %.0f ms, own inputs: %ld wrong values, %d failed launches, slowest %.3f ms%s", + who, sp.launches, (sp.end - sp.start) / 1e6, wrong_total, failed, worst / 1e6, ""); + check(wrong_total == 0 && failed == 0, w); + if (hang_fd >= 0) { + snprintf(w, sizeof w, "%s: %u of its launches ran while the other process's kernel was hung or being torn down", who, sp.during); + check(sp.during > 0, w); + } + if (span_fd >= 0 && write(span_fd, &sp, sizeof sp) != (ssize_t)sizeof sp) printf("[%d] span write failed\n", (int)getpid()); + omega_gpu_session_unlock(); + omega_gpu_session_close(); + return g_failed ? 1 : 0; +} + +static int reap(pid_t p) { int st = 0; if (p < 0 || waitpid(p, &st, 0) != p) return -1; return WIFEXITED(st) ? WEXITSTATUS(st) : 128 + (WIFSIGNALED(st) ? WTERMSIG(st) : 0); } +static int pipe_or_die(int fd[2]) { if (pipe(fd) != 0) { printf("NOT_RUN: pipe\n"); exit(2); } return 0; } + +static void chip_tier(int values_only) { + char w[300]; + fflush(stdout); + if (!values_only) { + pid_t h = fork(); + if (h == 0) _exit(part_hang(-1)); + int rc = reap(h); + snprintf(w, sizeof w, "C5 hang: never-ending kernel timed out, recovered, correct launch in the same process (child exit %d)", rc); + check(rc == 0, w); + } + + /* two processes, started together after both are set up, each running RUN_MS: their spans must overlap */ + int ready[2], go[2], span[2]; + pipe_or_die(ready); pipe_or_die(go); pipe_or_die(span); + pid_t a = fork(); + if (a == 0) _exit(part_rounds(1000, "process 1", ready[1], go[0], -1, span[1])); + pid_t b = fork(); + if (b == 0) _exit(part_rounds(5000, "process 2", ready[1], go[0], -1, span[1])); + int r1 = hear(ready[0]), r2 = hear(ready[0]); + say(go[1], 'G'); say(go[1], 'G'); + int ra = reap(a), rb = reap(b); + Span s1 = {0}, s2 = {0}; + int got = read(span[0], &s1, sizeof s1) == (ssize_t)sizeof s1 && read(span[0], &s2, sizeof s2) == (ssize_t)sizeof s2; + const uint64_t lo = s1.start > s2.start ? s1.start : s2.start, hi = s1.end < s2.end ? s1.end : s2.end; + const double overlap_ms = got && hi > lo ? (hi - lo) / 1e6 : 0.0; + snprintf(w, sizeof w, "C5 two processes on one GPU: both right (ready %c %c, exits %d, %d), running side by side for %.0f ms", + r1 > 0 ? r1 : '?', r2 > 0 ? r2 : '?', ra, rb, overlap_ms); + check(ra == 0 && rb == 0 && overlap_ms >= (double)RUN_MS * 0.5, w); + close(ready[0]); close(ready[1]); close(go[0]); close(go[1]); close(span[0]); close(span[1]); + + if (values_only) { + puts("C5 recovery: NOT_RUN (values-only; hang needs a separate reservation)"); + return; + } + + /* a hang beside a working process: process 1 starts when process 2's hang begins and runs until process 2 is done */ + int sig[2], rdy[2]; + pipe_or_die(sig); pipe_or_die(rdy); + a = fork(); + if (a == 0) { close(sig[1]); _exit(part_rounds(9000, "process 1 beside a hang", rdy[1], -1, sig[0], -1)); } + b = fork(); + if (b == 0) { close(sig[0]); _exit(part_hang(sig[1])); } + close(sig[0]); close(sig[1]); + ra = reap(a); rb = reap(b); + snprintf(w, sizeof w, "C5 a hang in process 2 leaves process 1 unharmed, and process 2 recovers (exits %d, %d)", ra, rb); + check(ra == 0 && rb == 0, w); + close(rdy[0]); close(rdy[1]); +} + +int main(int argc, char **argv) { + const char *tier = "chip"; + for (int i = 1; i < argc; i++) { + if (!strcmp(argv[i], "--host-only")) tier = "host"; + else if (!strcmp(argv[i], "--values-only")) tier = "values"; + else if (!strcmp(argv[i], "--dump-hang") && i + 1 < argc) { tier = "host"; g_dump = argv[++i]; } + else { fprintf(stderr, "usage: %s [--host-only | --values-only] [--dump-hang PATH]\n", argv[0]); return 2; } + } + setvbuf(stdout, NULL, _IOLBF, 0); + if (!strcmp(tier, "host")) host_only(); + else chip_tier(!strcmp(tier, "values")); + if (g_failed) printf("C5 isolation %s: FAIL (%d of %d)\n", tier, g_failed, g_checks); + else printf("C5 isolation %s: PASS (%d checks)\n", tier, g_checks); + return g_failed ? 1 : 0; +} 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; +} diff --git a/tests/gpu_streams_test.c b/tests/gpu_streams_test.c new file mode 100644 index 000000000..621049e14 --- /dev/null +++ b/tests/gpu_streams_test.c @@ -0,0 +1,392 @@ +/* gpu_streams_test.c -- row C4 of docs/OMEGA.md: two independent queues and an event between them, on the GB10. + * + * --host-only codegen + encode of the two kernels (and their mutants), refusals of omega_gpu_cmdlist_wait_for + * --sim the kernels through the warp simulator (tests/bw_warp_sim.h): exact values, mutants caught + * (no flag) the chip, under the GB10 lock, AIEN_OMEGA_GPU_CHANNELS = 2 (L136 extra queue): + * overlap list A and list B each hold the same half-chip spin kernel (24 CTAs x 128 threads); solo A, solo B + * (session queue, one submit each) and the pair (A on queue 0, B on queue 1, omega_gpu_cmdlist_submit_pair), + * medians of REPS: PASS when pair < 0.75 (solo A + solo B), for the default barrier or first_barrier_none + * (both are printed). Red check: both spins in ONE list on one queue (WAIT_FOR_IDLE between them) must + * NOT come in under 0.75, or the measurement cannot tell overlap from order. + * event A: a long spin writes X. B: omega_gpu_cmdlist_wait_for(A, 0), then copy Y = X + 1. X and Y start as a + * signalling-NaN poison; every Y must be the spin's value + 1. Negative control: the same pair without the + * wait must leave at least one Y wrong (B ran while A spun), else the event is "not demonstrated" (FAIL). + * Every kernel comes from Omega's IR (omega_bw_em.h), encoded by our own sm_121 encoder; no CUDA. */ +#include "omega_gpu_cmdlist.h" +#include "omega_gpu_session.h" +#include "bw_warp_sim.h" /* brings math.h, whose fadd() would hide the emitter's */ +#define fadd em_fadd +#define ffma em_ffma +#include "omega_bw_em.h" + +#include +#include +#include + +#define THREADS 128u +#define CTAS 24u /* half of the GB10's 48 SMs */ +#define NEL (THREADS * CTAS) +#define UNROLL 4u /* FFMAs per loop turn: x = x * 1 + 1 */ +#define MAX_ITERS ((1u << 22) - 1024u) /* UNROLL * iters + tag stays below 2^24: every value exact in f32 */ +#define POISON 0x7fa00badu /* a signalling NaN no FFMA / FADD writes */ +#define REPS 5 + +/* argument words (constant bank 0x380) */ +#define A_OUT 0x380 /* spin: out u64 | copy: in u64 */ +#define A_ITERS 0x388 /* spin: loop turns | copy: out u64 (0x388..0x38f) */ +#define A_TAG 0x38c /* spin: f32 added at the end */ +#define A_N 0x390 /* elements */ +#define ARGS 5u + +static int g_failed, g_checks; +static void check(int ok, const char *what) { + g_checks++; + if (!ok) g_failed++; + printf("%s: %s\n", ok ? "ok" : "FAIL", what); +} +static uint32_t f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } + +/* ------------------------------------------------------------------ the kernels */ + +typedef struct { int bssy, bra; } Region; +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); } + +/* spin: out[gid] = UNROLL * iters + tag (mutant 1: one FFMA short per turn) */ +static int gen_spin(Em *e, int mutant) { + int udesc = UV64(e), pout = V64(e), a64 = V64(e), iters = V(e), tag = V(e), n = V(e), tid = V(e), cta = V(e), gid = V(e); + int i = V(e), one = V(e), fone = V(e), x = V(e); + if (e->err) return -1; + ldcu64(e, udesc, 0x358); + ldc64(e, pout, A_OUT); ldc32(e, iters, A_ITERS); ldc32(e, tag, A_TAG); ldc32(e, n, A_N); + s2r(e, tid, BW_SR_TID_X); s2r(e, cta, BW_SR_CTAID_X); + imadi(e, gid, cta, 0, THREADS, tid); + movi(e, one, 1); movi(e, fone, 0x3f800000u); movi(e, x, 0); movi(e, i, 0); + const int top = here(e); + loop_begin(e); + isetp_ge_u32(e, i, iters); + const int done = bra_if_p0_fwd(e); + for (unsigned u = 0; u < UNROLL - (mutant == 1 ? 1u : 0u); u++) ffma(e, x, x, fone, 0, fone); + iadd(e, i, i, one); + bra(e, top); + loop_end(e); + patch_bra(e, done, here(e)); + fadd(e, x, x, tag); + isetp_ge_u32(e, gid, n); + { Region r = skip_if_p0(e); imadw(e, a64, gid, 4u, pout); stg(e, a64, 0, x, udesc); region_end(e, r); } + tail(e); + return e->err; +} + +/* copy: out[gid] = in[gid] + 1 (mutant 1: + 2) */ +static int gen_copy(Em *e, int mutant) { + int udesc = UV64(e), pin = V64(e), pout = V64(e), a64 = V64(e), n = V(e), tid = V(e), cta = V(e), gid = V(e), add = V(e), v = V(e); + if (e->err) return -1; + ldcu64(e, udesc, 0x358); + ldc64(e, pin, A_OUT); ldc64(e, pout, A_ITERS); ldc32(e, n, A_N); + s2r(e, tid, BW_SR_TID_X); s2r(e, cta, BW_SR_CTAID_X); + imadi(e, gid, cta, 0, THREADS, tid); + movi(e, add, mutant == 1 ? 0x40000000u : 0x3f800000u); + isetp_ge_u32(e, gid, n); + { + Region r = skip_if_p0(e); + imadw(e, a64, gid, 4u, pin); ldg32(e, v, a64, udesc, 1); + fadd(e, v, v, add); + imadw(e, a64, gid, 4u, pout); stg(e, a64, 0, v, udesc); + region_end(e, r); + } + tail(e); + return e->err; +} + +static int codegen(int copy, int mutant, BlackwellIRProgram *prog) { + omega_bw_ir_init(prog); + Em em; memset(&em, 0, sizeof em); em.p = prog; + if ((copy ? gen_copy(&em, mutant) : gen_spin(&em, mutant)) != 0 || em.err || em.depth != 0) return -1; + return omega_bw_regalloc_solve(prog); +} + +static void spin_args(uint32_t a[ARGS], uint64_t out, uint32_t iters, float tag) { + memset(a, 0, ARGS * 4u); + a[0] = (uint32_t)out; a[1] = (uint32_t)(out >> 32); a[2] = iters; a[3] = f2u(tag); a[4] = NEL; +} +static void copy_args(uint32_t a[ARGS], uint64_t in, uint64_t out) { + memset(a, 0, ARGS * 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] = NEL; +} +static float spin_value(uint32_t iters, float tag) { return (float)(UNROLL * iters) + tag; } + +/* ------------------------------------------------------------------ host + simulator */ + +static void host_only(void) { + char w[200]; + for (int copy = 0; copy < 2; copy++) + for (int mut = 0; mut < 2; mut++) { + BlackwellIRProgram *p = calloc(1, sizeof *p); + size_t need = 0; + int ok = p && codegen(copy, mut, p) == 0 && (need = omega_bw_program_code_bytes(p)) > 0; + void *code = ok ? calloc(1, need) : NULL; + OmegaBlackwellKernel kk; + ok = ok && code && omega_blackwell_kernel_encode_into(p, code, need, &kk) == 0 && kk.code_size == need; + snprintf(w, sizeof w, "codegen + encode: %s kernel%s (%zu bytes, %u registers)", copy ? "copy" : "spin", mut ? " mutant 1" : "", + need, ok ? kk.gpr_count : 0u); + check(ok, w); + free(code); free(p); + } + check(omega_gpu_cmdlist_wait_for(NULL, NULL, 0) == -1, "wait_for refuses missing lists"); +} + +static int sim_run(BlackwellIRProgram *p, const uint32_t a[ARGS], WsRange *rg, int nrg, int order) { + static WsSim s; + memset(&s, 0, sizeof s); + s.p = p; s.threads = THREADS; s.order = order; + for (unsigned i = 0; i < ARGS; i++) s.cbank[0x380 / 4 + i] = a[i]; + memcpy(s.ranges, rg, (size_t)nrg * sizeof *rg); s.nranges = nrg; + if (ws_run(&s, CTAS, 1) != 0) { printf(" simulator: %s at pc %d\n", s.msg, s.err_pc); return -1; } + return 0; +} + +static void sim_tier(void) { + char w[200]; + static float X[NEL], Y[NEL]; + WsRange rg[2] = { { (uintptr_t)X, (uintptr_t)(X + NEL) }, { (uintptr_t)Y, (uintptr_t)(Y + NEL) } }; + for (int mut = 0; mut < 2; mut++) { + BlackwellIRProgram *ps = calloc(1, sizeof *ps), *pc = calloc(1, sizeof *pc); + if (!ps || !pc || codegen(0, mut, ps) || codegen(1, mut, pc)) { check(0, "simulator: kernels built"); free(ps); free(pc); return; } + for (int order = 0; order < 2; order++) { + const uint32_t iters = 37u + (uint32_t)order; + const float tag = 5.0f + (float)order; + uint32_t a[ARGS]; + for (unsigned i = 0; i < NEL; i++) { uint32_t pz = POISON; memcpy(&X[i], &pz, 4); memcpy(&Y[i], &pz, 4); } + spin_args(a, (uintptr_t)X, iters, tag); + int rc = sim_run(ps, a, rg, 2, order); + copy_args(a, (uintptr_t)X, (uintptr_t)Y); + rc = rc || sim_run(pc, a, rg, 2, order); + unsigned wx = 0, wy = 0; + for (unsigned i = 0; i < NEL; i++) { + wx += f2u(X[i]) != f2u(spin_value(iters, tag)); + wy += f2u(Y[i]) != f2u(spin_value(iters, tag) + 1.0f); + } + if (mut == 0) snprintf(w, sizeof w, "simulator order %d: spin + copy exact on all %u elements (%u, %u wrong)", order, NEL, wx, wy); + else snprintf(w, sizeof w, "simulator red check order %d: mutant 1 caught (%u, %u wrong)", order, wx, wy); + check(mut == 0 ? rc == 0 && wx == 0 && wy == 0 : rc != 0 || (wx > 0 && wy > 0), w); + } + free(ps); free(pc); + } +} + +/* ------------------------------------------------------------------ the chip */ + +typedef struct { BlackwellIRProgram prog; OmegaGpuMem code; uint32_t gpr; } Kern; +static Kern K_spin, K_copy; + +static int kern_upload(Kern *k, int copy) { + if (codegen(copy, 0, &k->prog) != 0) return -1; + size_t need = omega_bw_program_code_bytes(&k->prog); + if (need == 0 || omega_gpu_session_alloc_code(need, &k->code) != 0) return -1; + OmegaBlackwellKernel kk; + if (omega_blackwell_kernel_encode_into(&k->prog, k->code.cpu, need, &kk) != 0 || omega_gpu_session_sync_to_gpu(&k->code) != 0) return -1; + k->gpr = omega_bw_gpr_declared(kk.gpr_count); + if (k->gpr < 64u) k->gpr = 64u; + return 0; +} + +static OmegaGpuMem g_buf; /* [OA | OB | X | Y], NEL floats each */ +static float *buf_at(int i) { return (float *)g_buf.cpu + (size_t)i * NEL; } +static uint64_t va_at(int i) { return g_buf.va + (uint64_t)i * NEL * 4u; } +enum { B_OA = 0, B_OB = 1, B_X = 2, B_Y = 3 }; + +static void poison_all(void) { + uint32_t *u = g_buf.cpu; + for (size_t i = 0; i < 4u * NEL; i++) u[i] = POISON; + (void)omega_gpu_session_sync_to_gpu(&g_buf); +} +static unsigned wrong(int b, float want) { + (void)omega_gpu_session_sync_to_cpu(&g_buf); + const float *p = buf_at(b); + unsigned n = 0; + for (unsigned i = 0; i < NEL; i++) n += f2u(p[i]) != f2u(want); + return n; +} + +static uint32_t g_args[8][ARGS]; /* the launches' argument words stay alive until submit */ +static int add_spin(OmegaGpuCmdList *cl, int slot, int out_buf, uint32_t iters, float tag) { + spin_args(g_args[slot], va_at(out_buf), iters, tag); + OmegaGpuLaunch L = { .code_va = K_spin.code.va, .gpr_count = K_spin.gpr, .threads_x = THREADS, .threads_y = 1, .grid_x = CTAS, .grid_y = 1, + .num_elements = NEL, .args = g_args[slot], .n_args = ARGS }; + return omega_gpu_cmdlist_add(cl, &L) < 0 ? -1 : 0; +} +static int add_copy(OmegaGpuCmdList *cl, int slot) { + copy_args(g_args[slot], va_at(B_X), va_at(B_Y)); + OmegaGpuLaunch L = { .code_va = K_copy.code.va, .gpr_count = K_copy.gpr, .threads_x = THREADS, .threads_y = 1, .grid_x = CTAS, .grid_y = 1, + .num_elements = NEL, .args = g_args[slot], .n_args = ARGS }; + return omega_gpu_cmdlist_add(cl, &L) < 0 ? -1 : 0; +} + +static int cmp_u64(const void *a, const void *b) { uint64_t x = *(const uint64_t *)a, y = *(const uint64_t *)b; return x < y ? -1 : x > y; } +static uint64_t median(uint64_t *v, int n) { qsort(v, (size_t)n, sizeof *v, cmp_u64); return v[n / 2]; } + +static OmegaGpuCmdList *g_a, *g_b, *g_ab; + +/* one solo submit of a one-spin list; values checked; elapsed ns or 0 on failure */ +static uint64_t solo(OmegaGpuCmdList *cl, int out_buf, uint32_t iters, float tag, const OmegaGpuCmdListOpts *o) { + OmegaGpuCmdListStats st; + poison_all(); + if (omega_gpu_cmdlist_submit(cl, o, &st) != 0) { printf(" solo submit: %s\n", omega_gpu_session_last_error()); return 0; } + unsigned bad = wrong(out_buf, spin_value(iters, tag)); + if (bad) { printf(" solo: %u wrong values\n", bad); return 0; } + return st.elapsed_ns; +} + +static int overlap(uint32_t iters, int fbn, double *ratio_out) { + char w[300]; + OmegaGpuCmdListOpts o; memset(&o, 0, sizeof o); + o.first_barrier_none = fbn; + uint64_t sa[REPS], sb[REPS], sp[REPS]; + for (int r = 0; r < REPS; r++) { + sa[r] = solo(g_a, B_OA, iters, 1.0f, &o); + sb[r] = solo(g_b, B_OB, iters, 2.0f, &o); + OmegaGpuCmdListStats s1, s2; + poison_all(); + int rc = omega_gpu_cmdlist_submit_pair(g_a, g_b, &o, &s1, &s2); + unsigned bad = rc ? NEL : wrong(B_OA, spin_value(iters, 1.0f)) + wrong(B_OB, spin_value(iters, 2.0f)); + if (rc || bad) { printf(" pair: %s, %u wrong values\n", rc ? omega_gpu_session_last_error() : "ok", bad); sp[r] = 0; } + else sp[r] = s1.elapsed_ns; + if (!sa[r] || !sb[r] || !sp[r]) { check(0, "overlap: every submit ran and every value is right"); return -1; } + } + const uint64_t ma = median(sa, REPS), mb = median(sb, REPS), mp = median(sp, REPS); + const double ratio = (double)mp / (double)(ma + mb); + *ratio_out = ratio; + snprintf(w, sizeof w, "overlap (%s): solo A %.3f ms, solo B %.3f ms, pair %.3f ms (medians of %d, values right): pair / (A + B) = %.3f", + fbn ? "first_barrier_none" : "default barrier", ma / 1e6, mb / 1e6, mp / 1e6, REPS, ratio); + printf("%s\n", w); + return 0; +} + +static int serial_control(uint32_t iters, double *ratio_out) { + OmegaGpuCmdListOpts o; memset(&o, 0, sizeof o); + uint64_t sa[REPS], sab[REPS]; + for (int r = 0; r < REPS; r++) { + sa[r] = solo(g_a, B_OA, iters, 1.0f, &o); + OmegaGpuCmdListStats st; + poison_all(); + int rc = omega_gpu_cmdlist_submit(g_ab, &o, &st); + unsigned bad = rc ? NEL : wrong(B_OA, spin_value(iters, 1.0f)) + wrong(B_OB, spin_value(iters, 2.0f)); + if (!sa[r] || rc || bad) { check(0, "serial control: every submit ran and every value is right"); return -1; } + sab[r] = st.elapsed_ns; + } + const uint64_t ma = median(sa, REPS), mab = median(sab, REPS); + *ratio_out = (double)mab / (double)(2 * ma); + printf("serial control: solo %.3f ms, both in one list on one queue %.3f ms: ratio %.3f\n", ma / 1e6, mab / 1e6, *ratio_out); + return 0; +} + +/* list A: spin into X; list B: [wait for A launch 0] copy X -> Y. Returns the count of wrong Y, or -1 on a submit failure. */ +static long event_run(uint32_t iters, int with_wait, int fbn) { + OmegaGpuCmdList *a = NULL, *b = NULL; + long res = -1; + if (omega_gpu_cmdlist_create(1, &a) || omega_gpu_cmdlist_create(1, &b)) { printf(" event: create: %s\n", omega_gpu_session_last_error()); goto out; } + omega_gpu_session_lock(); + if (add_spin(a, 4, B_X, iters, 3.0f) || (with_wait && omega_gpu_cmdlist_wait_for(b, a, 0)) || add_copy(b, 5)) { + omega_gpu_session_unlock(); printf(" event: add: %s\n", omega_gpu_session_last_error()); goto out; + } + OmegaGpuCmdListOpts o; memset(&o, 0, sizeof o); + o.first_barrier_none = fbn; + OmegaGpuCmdListStats s1, s2; + if (with_wait) { /* the refusals: a list with a wait never runs alone, nor waits on a second list */ + OmegaGpuCmdList *c = NULL; + int alone = omega_gpu_cmdlist_submit(b, &o, &s2) == -1 && strstr(omega_gpu_session_last_error(), "only in a pair") != NULL; + omega_gpu_session_unlock(); + int third = omega_gpu_cmdlist_create(1, &c) == 0; + omega_gpu_session_lock(); + third = third && add_spin(c, 6, B_OA, 1, 0.0f) == 0 && omega_gpu_cmdlist_wait_for(b, c, 0) == -1; + omega_gpu_session_unlock(); + omega_gpu_cmdlist_free(c); + omega_gpu_session_lock(); + check(alone && third, "event refusals: a list with a wait is refused alone, and cannot wait on a second list"); + } + poison_all(); + int rc = omega_gpu_cmdlist_submit_pair(a, b, &o, &s1, &s2); + if (rc) printf(" event: pair: %s\n", omega_gpu_session_last_error()); + else { + unsigned bx = wrong(B_X, spin_value(iters, 3.0f)), by = wrong(B_Y, spin_value(iters, 3.0f) + 1.0f); + printf(" event (%s, %s): X %u wrong, Y %u wrong of %u, pair %.3f ms\n", with_wait ? "wait" : "NO wait", fbn ? "first_barrier_none" : "default barrier", + bx, by, NEL, s1.elapsed_ns / 1e6); + res = bx ? -1 : (long)by; + } + omega_gpu_session_unlock(); +out: + omega_gpu_cmdlist_free(a); omega_gpu_cmdlist_free(b); + return res; +} + +static void chip_tier(int values_only) { + char w[300]; + setenv("AIEN_OMEGA_GPU_CHANNELS", "2", 1); + if (omega_gpu_cmdlist_create(1, &g_a) || omega_gpu_cmdlist_create(1, &g_b) || omega_gpu_cmdlist_create(2, &g_ab)) { + printf("NOT_RUN: command lists: %s\n", omega_gpu_session_last_error()); exit(2); + } + omega_gpu_session_lock(); + if (!omega_gpu_session_open() || omega_gpu_session_alloc(4u * NEL * 4u, &g_buf) || kern_upload(&K_spin, 0) || kern_upload(&K_copy, 1)) { + printf("NOT_RUN: device, buffer or kernels: %s\n", omega_gpu_session_last_error()); omega_gpu_session_unlock(); exit(2); + } + OmegaGpuQueueHandle q1; + check(omega_gpu_session_extra_queue(1, &q1) == 0, "the second queue opens (AIEN_OMEGA_GPU_CHANNELS = 2)"); + + /* calibrate: one spin of about 60 ms */ + uint32_t iters = 1u << 16; + OmegaGpuCmdListOpts o0; memset(&o0, 0, sizeof o0); + omega_gpu_cmdlist_reset(g_a); + if (add_spin(g_a, 0, B_OA, iters, 1.0f)) { check(0, "calibration list"); omega_gpu_session_unlock(); return; } + uint64_t t = solo(g_a, B_OA, iters, 1.0f, &o0); + if (!t) { check(0, "calibration spin ran with right values"); omega_gpu_session_unlock(); return; } + double scaled = (double)iters * 60e6 / (double)t; + iters = scaled > MAX_ITERS ? MAX_ITERS : (uint32_t)scaled; + printf("calibration: 2^16 turns took %.3f ms; using %u turns\n", t / 1e6, iters); + + omega_gpu_cmdlist_reset(g_a); omega_gpu_cmdlist_reset(g_b); omega_gpu_cmdlist_reset(g_ab); + if (add_spin(g_a, 0, B_OA, iters, 1.0f) || add_spin(g_b, 1, B_OB, iters, 2.0f) || + add_spin(g_ab, 2, B_OA, iters, 1.0f) || add_spin(g_ab, 3, B_OB, iters, 2.0f)) { check(0, "timing lists"); omega_gpu_session_unlock(); return; } + + if (!values_only) { + double r_def = 9, r_fbn = 9, r_ser = 0; + int ok_def = overlap(iters, 0, &r_def) == 0, ok_fbn = overlap(iters, 1, &r_fbn) == 0, ok_ser = serial_control(iters, &r_ser) == 0; + snprintf(w, sizeof w, "C4 overlap: two queues of the same work finish in under 0.75 of the solo sum (default %.3f, first_barrier_none %.3f)", r_def, r_fbn); + check((ok_def && r_def < 0.75) || (ok_fbn && r_fbn < 0.75), w); + snprintf(w, sizeof w, "red check: the same two spins in order on one queue do NOT pass the 0.75 bar (ratio %.3f)", r_ser); + check(ok_ser && r_ser >= 0.75, w); + } else puts("C4 overlap: NOT_RUN (values-only; timing needs an exclusive window)"); + omega_gpu_session_unlock(); + + /* the event: positive, then the control without the wait (both barrier modes) */ + long pos = event_run(iters, 1, 0); + snprintf(w, sizeof w, "C4 event: B's copy waited for A's spin on the other queue; every Y right (%ld wrong)", pos); + check(pos == 0, w); + long neg0 = event_run(iters, 0, 0), neg1 = neg0 > 0 ? neg0 : event_run(iters, 0, 1); + snprintf(w, sizeof w, "negative control: without the wait B ran early and Y is wrong (default barrier %ld wrong, first_barrier_none %ld wrong)", neg0, neg1); + check(neg0 > 0 || neg1 > 0, w); + long again = event_run(iters, 1, 1); + snprintf(w, sizeof w, "C4 event with first_barrier_none: every Y right (%ld wrong)", again); + check(again == 0, w); + + omega_gpu_session_lock(); + omega_gpu_session_free(&g_buf); omega_gpu_session_free(&K_spin.code); omega_gpu_session_free(&K_copy.code); + omega_gpu_session_unlock(); + omega_gpu_cmdlist_free(g_a); omega_gpu_cmdlist_free(g_b); omega_gpu_cmdlist_free(g_ab); +} + +int main(int argc, char **argv) { + const char *tier = "chip"; + for (int i = 1; i < argc; i++) { + if (!strcmp(argv[i], "--host-only")) tier = "host"; + else if (!strcmp(argv[i], "--values-only")) tier = "values"; + else if (!strcmp(argv[i], "--sim")) tier = "sim"; + else { fprintf(stderr, "usage: %s [--host-only | --sim | --values-only]\n", argv[0]); return 2; } + } + if (!strcmp(tier, "host")) host_only(); + else if (!strcmp(tier, "sim")) { host_only(); sim_tier(); } + else chip_tier(!strcmp(tier, "values")); + if (g_failed) printf("C4 streams %s: FAIL (%d of %d)\n", tier, g_failed, g_checks); + else printf("C4 streams %s: PASS (%d checks)\n", tier, g_checks); + return g_failed ? 1 : 0; +} diff --git a/tests/test_omega_gpu_session_platform.c b/tests/test_omega_gpu_session_platform.c index b3f16e88c..b02defe6f 100644 --- a/tests/test_omega_gpu_session_platform.c +++ b/tests/test_omega_gpu_session_platform.c @@ -66,6 +66,7 @@ static struct { unsigned cached_requests; /* buffers asked GPU-cached (must stay 0) */ unsigned allocs_unlocked; /* allocations without the session lock (must stay 0) */ int fail_open; + int fail_close; /* device_close closes, then reports a driver teardown fault (C5) */ } fk; static uint64_t f_now_ns(void *c) { (void)c; fk.ns += 1000u; return fk.ns; } @@ -92,7 +93,7 @@ static int f_dev_close(void *c, OmegaGpuDeviceHandle d) { (void)omega_gpu_handles_close(&fk.t, d.index, d.generation, OMEGA_GPU_HANDLE_DEVICE); memset(&fk.dev, 0, sizeof fk.dev); memset(&fk.q, 0, sizeof fk.q); fk.dev_closes++; - return 0; + return fk.fail_close ? OMEGA_GPU_PLAT_REFUSED_DEVICE : 0; } static OmegaGpuPlatformCaps g_caps = { @@ -382,6 +383,78 @@ static void c_timeout_latch_rules(void) { check_latched(0); } +/* C5: recover lifts the latch in the same process. Refused when nothing is latched; after a timeout it closes the device + * once (the platform releases every buffer, parked ones included; the session itself frees nothing), clears the parked + * list, and the next open is a new device on which a launch completes. */ +static void c_timeout_recover(void) { + fk_reset(); + EXPECT(install() == 0); + EXPECT(omega_gpu_session_recover() == -1 && strstr(omega_gpu_session_last_error(), "not latched") != NULL); + omega_gpu_session_lock(); + OmegaGpuSession *s = omega_gpu_session_open(); + EXPECT(s != NULL); + const OmegaGpuDeviceHandle old_dev = s ? s->dev : (OmegaGpuDeviceHandle){0}; + OmegaGpuLaunch L = launch_desc(5); + EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == 0); /* a working launch first */ + omega_gpu_session_unlock(); + EXPECT(omega_gpu_session_recover() == -1 && fk.dev_closes == 0); /* open but not latched: refused, device kept */ + omega_gpu_session_lock(); + OmegaGpuMem user; + EXPECT(omega_gpu_session_alloc(4096, &user) == 0); + fk.mode = MODE_HANG; + EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == -1 && omega_gpu_session_is_blocked()); + omega_gpu_session_free(&user); /* parked */ + omega_gpu_session_unlock(); + EXPECT(omega_gpu_session_recoveries() == 0); + EXPECT(omega_gpu_session_recover() == 0); + EXPECT(!omega_gpu_session_is_blocked() && omega_gpu_session_recoveries() == 1); + EXPECT(fk.dev_closes == 1 && fk.frees == 0 && fk.t.nlive == 0); /* the platform close released all of it */ + OmegaGpuParkStats ps; omega_gpu_session_parked(&ps); + EXPECT(ps.buffers == 0 && ps.bytes == 0); + EXPECT(omega_gpu_session_recover() == -1); /* nothing latched any more */ + fk.mode = MODE_COMPLETE; + omega_gpu_session_lock(); + s = omega_gpu_session_open(); + EXPECT(s != NULL && s->dev.generation != old_dev.generation && fk.dev_opens == 2); + EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == 0); + omega_gpu_session_unlock(); + omega_gpu_session_close(); + EXPECT(fk.dev_closes == 2 && fk.t.nlive == 0); +} + +/* C5: a close that reports a driver teardown fault leaves the process latched for good (the old grid may still run). */ +static void c_recover_teardown_fault(void) { + fk_reset(); + EXPECT(install() == 0); + omega_gpu_session_lock(); + EXPECT(omega_gpu_session_open() != NULL); + fk.mode = MODE_HANG; + OmegaGpuLaunch L = launch_desc(5); + EXPECT(omega_gpu_session_launch(&L, NULL, NULL) == -1 && omega_gpu_session_is_blocked()); + omega_gpu_session_unlock(); + fk.fail_close = 1; + EXPECT(omega_gpu_session_recover() == -1 && strstr(omega_gpu_session_last_error(), "teardown fault") != NULL); + EXPECT(omega_gpu_session_is_blocked() && omega_gpu_session_recoveries() == 0 && fk.dev_closes == 1); + fk.fail_close = 0; + EXPECT(omega_gpu_session_recover() == -1 && omega_gpu_session_is_blocked()); /* the handle is gone: no second try */ + omega_gpu_session_lock(); + EXPECT(omega_gpu_session_open() == NULL); + omega_gpu_session_unlock(); +} + +/* C5: a module latch (the command lists after a timeout whose check launch completed) is recoverable too */ +static void c_recover_module_latch(void) { + fk_reset(); + EXPECT(install() == 0); + omega_gpu_session_lock(); + EXPECT(omega_gpu_session_open() != NULL); + omega_gpu_session_unlock(); + omega_gpu_session_note_module_latch(); + EXPECT(!omega_gpu_session_is_blocked()); /* other APIs keep working */ + EXPECT(omega_gpu_session_recover() == 0 && fk.dev_closes == 1 && omega_gpu_session_recoveries() == 1); + EXPECT(omega_gpu_session_recover() == -1); /* lifted */ +} + static void c_stale_after_reopen(void) { fk_reset(); EXPECT(install() == 0); @@ -509,6 +582,9 @@ static const Case CASES[] = { {"CANCEL_AFTER_SUBMIT_LATCHES_AND_PARKS", c_cancel_after_submit}, {"TIMEOUT_PARKS_NEVER_RELEASES", c_timeout_parks}, {"TIMEOUT_LATCH_REFUSES_CLOSE_REOPEN_INSTALL", c_timeout_latch_rules}, + {"TIMEOUT_RECOVER_SAME_PROCESS", c_timeout_recover}, + {"RECOVER_TEARDOWN_FAULT_STAYS_LATCHED", c_recover_teardown_fault}, + {"RECOVER_MODULE_LATCH", c_recover_module_latch}, {"STALE_HANDLE_REFUSED_AFTER_CLOSE_REOPEN", c_stale_after_reopen}, {"SERVING_RESERVATION_REFUSES_WITHOUT_ALLOC", c_reservation_refusal}, {"MATMUL_CODE_BUFFER_ENCODED_IN_PLACE", c_matmul_code_buffer}, diff --git a/tools/.crumb b/tools/.crumb index c7c3b01dd..319a85af1 100644 --- a/tools/.crumb +++ b/tools/.crumb @@ -26,50 +26,50 @@ "children": [ { "digest": "9f82d33852a7cdcedb210ee1525f42c4d963c18940cae1df7f69d1210de71985", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "argus", "purpose": "Directory tools/argus. Purpose not yet described by a human." }, { "digest": "4b3a030252eaa6f1c693105a76092e8a5fccfd340658a982f5bb1130ce9aba33", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "brownian", "purpose": "Directory tools/brownian. Purpose not yet described by a human." }, { "digest": "1d21e7e466d0fd96abf30d11b2d7e45718bc42ada1784bc8bc2a749f541b3ab7", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "dual_experiment", "purpose": "Runner for the DUAL applicability experiment: runs the pre-registered campaign and writes result" }, { "digest": "2a1bd46e9c9502d02144e3f76c784863001323c564a22bdae2905c0660a5f7a4", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "estimation", "purpose": "Directory tools/estimation. Purpose not yet described by a human." }, { "digest": "b3241d8cdc884bbfcca5cf9d5f4ed0aac8f7e2df43d9ab47ca2789432dd49a75", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "reservoir", "purpose": "Gate 2 tooling for the reservoir (omega#440): in-house zlib inflate and MAT v5 reader, the archi" }, { "digest": "f1a1a7f24d51a52f01171d01edbd59e8b15c53b15eeffb97e3c0ffc553e639af", - "last_commit": "d863780e", + "last_commit": "d863780eb", "name": "turing_verify_indep", "purpose": "Directory tools/turing_verify_indep. Purpose not yet described by a human." } ], "children_root": "sha256:8d3d3b952374b3b72e667521f07bf028c4c02bcefff772ad1a71cfff3e5266f2", - "compiled_at": "2026-10-24T17:45:20Z", + "compiled_at": "2026-10-24T23:29:24Z", "compiler": "crumb-compile/1", - "digest": "sha256:e519c1f13dc5134c5b6ab17f669745776880cb6e78c6efd2f716283760466c66", + "digest": "sha256:720264194c2cbc8fc362039b6262a720dfecb2fdef1952b34e7d80a2d4d7d63a", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 287, - "generated_at_commit": "e10bee30af65673a7d8f077ff2d6ef1bfcf0f46e", - "source_tree": "sha256:0a246536d1a23d218d0db2bdab696067d83d76514d0de462b79a21876fcb3b69" + "files": 288, + "generated_at_commit": "14107ee4e7bde3b3e3430601d0f56be2ed3b33bc", + "source_tree": "sha256:787f2b712460f8baf532d15b6f889bb1455e5a2b4b47e1a5585d03eb57b74e55" }, "owner_lane": "", "provenance": { diff --git a/tools/l136_channel_gates.sh b/tools/l136_channel_gates.sh new file mode 100755 index 000000000..3d465cfa5 --- /dev/null +++ b/tools/l136_channel_gates.sh @@ -0,0 +1,23 @@ +#!/bin/sh +# L136 correctness gates on the chip (GB10 lock only). $1 = evidence dir (absolute). +E=$1; N=$HOME/workspace/omega-gpu-channels/build; B=$HOME/workspace/omega-gpu-channels-base/build +cd $HOME/workspace/omega-gpu-channels || exit 1 +{ date -u; git rev-parse HEAD; uname -r; grep MemAvailable /proc/meminfo; cat /proc/loadavg; } > $E/device.txt +run() { name=$1; shift; "$@" > $E/$name.log 2>&1; echo "$name rc=$? $(tail -1 $E/$name.log)" >> $E/summary.txt; } +unset AIEN_OMEGA_GPU_CHANNELS +# G1: one channel, the new build against the base build (0010f7d3 on physics 6d7cf0d4), same arguments +run g1-base-q38-l4 $B/gpu_q38_graph_test --layers 4 --mask 0x8 --reps 3 +run g1-new-q38-l4 $N/gpu_q38_graph_test --layers 4 --mask 0x8 --reps 3 +run g1-base-q38-l2 $B/gpu_q38_graph_test --reps 3 +run g1-new-q38-l2 $N/gpu_q38_graph_test --reps 3 +run g1-base-cmdlist $B/gpu_cmdlist_test +run g1-new-cmdlist $N/gpu_cmdlist_test +run g1-base-dgraph $B/gpu_decode_graph_test +run g1-new-dgraph $N/gpu_decode_graph_test +# refusal: no AIEN_OMEGA_GPU_CHANNELS, the pair is refused by name, the serial path decodes +run refusal $N/gpu_q38_channels_test --layers 4 --steps 2 --block 0 --refusal +# G2 small: two channels, 4 layers, 16 steps + 16-step blocks +AIEN_OMEGA_GPU_CHANNELS=2 run g2-l4 $N/gpu_q38_channels_test --layers 4 --steps 16 --block 16 +# two-channel device: the one-channel tests still pass on queue 0 +AIEN_OMEGA_GPU_CHANNELS=2 run g1-ch2-q38-l4 $N/gpu_q38_graph_test --layers 4 --mask 0x8 --reps 3 +echo done >> $E/summary.txt