Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/patches/33808b62c1e84ac9 # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers ## GitHub aien-dev/omega-mv#708 (code only) closed derived `33808b62c1e84ac9` #708 Imported from GitHub aien-dev/omega-mv#708 (code only) GitHub's metadata for this pull request is unavailable: its state is DERIVED from the commits (merged: the head is reachable from this project's main; open: GitHub kept a merge ref; else closed). Base `a0eafa39ac9a92786dac68f7aa6d8564a28fa981` · Head `d10a4ac9be8de9834d9c022c5a4e614b85e04e58` ### Fetch it A git bundle of the pull request's head, 48100 bytes. ``` curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/33808b62c1e84ac9.bundle -o 33808b62c1e84ac9.bundle git fetch 33808b62c1e84ac9.bundle refs/pull/708/head ``` Raw bundle · Diff ### Provenance This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record: ``` github-import 1 aien-dev/omega-mv 708 d10a4ac9be8de9834d9c022c5a4e614b85e04e58 ``` ``` {"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":708,"url":"https://github.com/aien-dev/omega-mv/pull/708","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:38:18Z","metadata":"unavailable","state_derived":true} ``` ### Changes ``` diff --git a/.crumb b/.crumb index cb3d904f2..534428cdb 100644 --- a/.crumb +++ b/.crumb @@ -42,68 +42,68 @@ "children": [ { "digest": "9f19f11674ca5dfc3c9c3ae35fe6106557aee9583210da13d257e1b9d88e052c", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "calibration", "purpose": "Directory calibration. Purpose not yet described by a human." }, { "digest": "38942b47f7ce98113cdf562eefff1cfd3c460a5d34ef9314ad0097cc0ded2ba8", - "last_commit": "8ec82485d", + "last_commit": "8ec82485", "name": "docs", "purpose": "Directory docs. Purpose not yet described by a human." }, { "digest": "536f7b8db9a92a1f586bad4379d47544a30d1b8e83c2d799c18e66cf8660c9ae", - "last_commit": "30bb116cc", + "last_commit": "30bb116c", "name": "doctrine", "purpose": "Directory doctrine. Purpose not yet described by a human." }, { - "digest": "4c711384133e933fc175aab7d48daae8b597767d17eaa17464d560c758a687b7", - "last_commit": "e9dcfafb7", + "digest": "572ee7099a7e17b97459799f2d0e11cd8ba3bd40f1ca1cc6ece02bf96ddd165b", + "last_commit": "d044d9af", "name": "mk", "purpose": "Directory mk. Purpose not yet described by a human." }, { "digest": "590f1f3a26a12dfd9df281ec944b882b85fd4ac2ae07a88a03bb8bd0d7d62536", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "research", "purpose": "Directory research. Purpose not yet described by a human." }, { - "digest": "6327473d41e10acbf43be0918d3557509322bfd7d6e075e114c79c2600a47305", - "last_commit": "43d8d20da", + "digest": "3d820d0f9b2bce822e5556cf43cef5325148288fa4403f3cc5eb53945bf1b028", + "last_commit": "c02e4c35", "name": "spec", "purpose": "Directory spec. Purpose not yet described by a human." }, { - "digest": "8ab10b555fb095ec6fc706f96f7294d2b99ce62438600a493fe235ec11bcd0d6", - "last_commit": "2f44aa3b6", + "digest": "91079e941f8e481e45f20f7bb898dd32d7c03b534c779807dbfc9e76fb8a697a", + "last_commit": "d044d9af", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "cc52017187608f4ee94d1480602ff214c614604c515bbabbde448c2733e555ab", - "last_commit": "e9dcfafb7", + "digest": "fc0957e92759897f586607f055b8b6c3420c7b5168f504ba82246aa8d4a2bced", + "last_commit": "d044d9af", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, { - "digest": "dbe7b08900fc053b23c993a25a3fe744e3874be2f23ee24f20e183797db53c27", - "last_commit": "e9dcfafb7", + "digest": "c86cd0893d1f95dee1febdf19efe3ae5a2fcc2ec2b619f9a876a4a28b80bf548", + "last_commit": "39d07c91", "name": "tools", "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:22c5443cb1d244ce3757eeaf7b6be6dd6ae8da548460b2f9f7cce346fe627d2d", - "compiled_at": "2026-10-24T23:55:01Z", + "children_root": "sha256:577ab9c2ef813aa9762acf51bcb16931fbdd07cc987a608ff9f877df21be8191", + "compiled_at": "2026-10-25T02:29:06Z", "compiler": "crumb-compile/1", - "digest": "sha256:120d70c3009b2973340b89b2394656968f41de2eebd0dc31756a071315ceb358", - "evidence_files": 11663, - "evidence_root": "sha256:9a183185ae8671e6263ec276a9bac3332e357843bcfd0e071b2bd14a3a75efc0", - "files": 15080, - "generated_at_commit": "4cd9ad9845612038a49c15940811ce24c38c061d", - "source_tree": "sha256:14caf26ffe8d06a0538594b63e92e61957aa06455feef677dcb25e05224fc464" + "digest": "sha256:bde32de00e9b277b8ce64e0b5158c0506c6f48cde894d78014b3c932caf375df", + "evidence_files": 11670, + "evidence_root": "sha256:5fe3292e363957fdf76b5586a0e7e87a79b00fb125acb3900d3d7ef035daafbb", + "files": 14999, + "generated_at_commit": "d044d9af667dd04d28d9dbf64d2e6ca4caad67ce", + "source_tree": "sha256:b74e393e9fa6e40e34fb8a234f9bc281acfee80211377475c73410ce7cccf86d" }, "owner_lane": "", "provenance": { diff --git a/evidence/LATTICE-698/20261024T1827Z-a8742c96/SHA256SUMS b/evidence/LATTICE-698/20261024T1827Z-a8742c96/SHA256SUMS new file mode 100644 index 000000000..e122bf8f8 --- /dev/null +++ b/evidence/LATTICE-698/20261024T1827Z-a8742c96/SHA256SUMS @@ -0,0 +1,8 @@ +ae2bcb2ebf0c71213e9168309d330a960827f15ea060f661e76790313f63b57a src/runtime/rx_lattice.c +9c3d2e88171f46ffef658f0958c14a9d3140c5b74874220e8de571db5e42781f src/runtime/rx_lattice.h +8929149ef4ffb7e6c82c253b1d01fb16180891eef1beb53837a7d64741f61a11 src/runtime/rx_costmodel.c +4c7c8b67afaba8632321537c2a02d89a0d120cb6a7f7995f73d073bfefc2efae tests/runtime/rx_lattice_bench.c +7efe6ae976abf1db6a06d2f57d39abe4faafeb8386782b66f93181b425879ea4 tests/runtime/rx_lattice_unit.c +5cc7933dc90fe65c2d1f7a80e644b4c26a8ef5bf0632cc49e31d2edb96b06d12 tests/runtime/rx_lattice_fixture.h +af00670d135a4feb52906a5d4e120502ed8dd81ab2a78bb13795edd66917039a build/rx_lattice_unit +96fd34d2205daa219203056ab4fedcc265e4d705d9c3bee03bc1548cbc9f74bb build/rx_lattice_bench diff --git a/evidence/LATTICE-698/20261024T1827Z-a8742c96/bench-lattice.txt b/evidence/LATTICE-698/20261024T1827Z-a8742c96/bench-lattice.txt new file mode 100644 index 000000000..1b5cf964b --- /dev/null +++ b/evidence/LATTICE-698/20261024T1827Z-a8742c96/bench-lattice.txt @@ -0,0 +1,26 @@ +source a8742c96499447c0bcf3049c732d364f178d5458 +host Linux 7.0.0-1019-nvidia aarch64 +cmd: make build/rx_lattice_bench && ./build/rx_lattice_bench 31 +rx_lattice host benchmark (omega-mv #698). Host CPUs only, no GPU. + arm reference verified + arm scalar verified + arm quad4 verified + arm planted_wrong refused + arm model_call refused + lanes: x925 cpu8 (busy 0.00) cpu17 (busy 0.00); a725 cpu0 (busy 0.00) cpu10 (busy 0.00); loadavg 3.78 4.63 5.10 4/2434 2147836 + calibration: 240 timed observations in 0.11 s (training grid, not the graph's shapes) + lattice: 109 candidates, 0 nodes without a candidate; admitted 76 scope 8 realization 14 unverified 11 + predicted critical path (best admitted estimate per node; caps for unmodelled ops): 132.7 us + dispatcher cpu5 + serial outputs equal the independent reference-only oracle: yes + MEASURED, 31 rounds after 1 warm-up, rotating policy order; loadavg after: 3.78 4.63 5.10 5/2434 2147856 + policy wall_med_us wall_min wall_max select_us busy_sum_us busy/wall crit_path_us util_x925 util_a725 arms(ref/scalar/quad4) dispatched(x925/a725/gpu.sim) weight_switches + serial 655.8 646.2 678.1 13.6 490.4 0.75 195.1 0.373 0.000 11/0/0 17/0/0 9 + lcf 243.4 235.9 259.0 14.1 434.9 1.79 163.9 0.636 0.259 0/0/11 15/2/0 10 + q3d 303.3 260.1 314.0 15.6 380.4 1.25 150.5 0.625 0.000 0/0/11 17/0/0 9 + lcf/serial wall ratio: median 0.366; faster than serial in 31 of 31 paired rounds + q3d/serial wall ratio: median 0.461; faster than serial in 31 of 31 paired rounds + q3d/lcf wall ratio: median 1.255; q3d faster in 0 of 31 paired rounds + outputs and effect value identical to serial in every run: yes; failed runs: 0 + not measured: energy, memory bytes, GPU (gpu.sim refused: no FORGE lease). +exit 0 diff --git a/evidence/LATTICE-698/20261024T1827Z-a8742c96/test-costmodel.txt b/evidence/LATTICE-698/20261024T1827Z-a8742c96/test-costmodel.txt new file mode 100644 index 000000000..97acc718d --- /dev/null +++ b/evidence/LATTICE-698/20261024T1827Z-a8742c96/test-costmodel.txt @@ -0,0 +1,6 @@ +source a8742c96499447c0bcf3049c732d364f178d5458 +cmd: make test-costmodel + 80% coverage on later, drifted data: 0.323 before calibration, 0.820 after + model blob: 28112 bytes +checks 68 failures 0 +exit 0 diff --git a/evidence/LATTICE-698/20261024T1827Z-a8742c96/test-lattice.txt b/evidence/LATTICE-698/20261024T1827Z-a8742c96/test-lattice.txt new file mode 100644 index 000000000..380d3e84d --- /dev/null +++ b/evidence/LATTICE-698/20261024T1827Z-a8742c96/test-lattice.txt @@ -0,0 +1,21 @@ +source a8742c96499447c0bcf3049c732d364f178d5458 +host Linux 7.0.0-1019-nvidia aarch64 spark-b87b +cc gcc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0 +cmd: make test-lattice +cc -std=gnu11 -Wall -Wextra -Werror -MMD -MP -D_GNU_SOURCE -O2 -Isrc -I../physics/m16 -I../physics/nvrm -I../physics/third_party/nvidia-open-580.173.02/src/common/sdk/nvidia/inc -I../physics/third_party/nvidia-open-580.173.02/kernel-open/common/inc -I../physics/third_party/nvidia-open-580.173.02/kernel-open/nvidia-uvm -I../physics/third_party/nvidia-open-580.173.02/src/nvidia/arch/nvalloc/unix/include -c -o build/rx_lattice.o src/runtime/rx_lattice.c +cc -std=gnu11 -Wall -Wextra -Werror -MMD -MP -D_GNU_SOURCE -O2 -Isrc -I../physics/m16 -I../physics/nvrm -I../physics/third_party/nvidia-open-580.173.02/src/common/sdk/nvidia/inc -I../physics/third_party/nvidia-open-580.173.02/kernel-open/common/inc -I../physics/third_party/nvidia-open-580.173.02/kernel-open/nvidia-uvm -I../physics/third_party/nvidia-open-580.173.02/src/nvidia/arch/nvalloc/unix/include -Itests -o build/rx_lattice_unit tests/runtime/rx_lattice_unit.c build/rx_lattice.o src/sha256.c src/omega_evidence.c src/omega_canonical.c src/omega_validate.c src/omega_core.c src/omega_codec.c src/aarch64_encoder.c src/aarch64_decoder.c src/omega_realize.c src/omega_realize_synth.c src/omega_program.c src/omega_machine.c src/omega_exec.c src/omega_verify.c src/omega_matvec.c src/omega_matvec_quad.c src/runtime/rx_costmodel.c -lm +./build/rx_lattice_unit +rx_lattice unit (omega-mv #698) + arm reference verified + arm scalar verified + arm quad4 verified + arm planted_wrong REFUSED + arm model_call REFUSED + build: 109 candidates; admitted 83; refused unverified 11 seat 7 scope 8 + MODELED time (synthetic truth table, not a measurement): + predicted critical path 187.8 us + serial makespan 658.3 us busy x925 658.3 a725 0.0 gpu.sim 0.0 us arms ref 11 scalar 0 quad4 0 weight switches 9 + lcf makespan 154.2 us busy x925 187.3 a725 88.3 gpu.sim 56.6 us arms ref 0 scalar 0 quad4 11 weight switches 10 + q3d makespan 118.6 us busy x925 172.8 a725 0.0 gpu.sim 105.6 us arms ref 0 scalar 0 quad4 11 weight switches 11 +checks 237 failures 0 +exit 0 diff --git a/mk/.crumb b/mk/.crumb index 79c40b88a..e64df8781 100644 --- a/mk/.crumb +++ b/mk/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-24T23:51:52Z", + "compiled_at": "2026-10-25T02:29:06Z", "compiler": "crumb-compile/1", - "digest": "sha256:4c711384133e933fc175aab7d48daae8b597767d17eaa17464d560c758a687b7", + "digest": "sha256:572ee7099a7e17b97459799f2d0e11cd8ba3bd40f1ca1cc6ece02bf96ddd165b", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 74, - "generated_at_commit": "c8d7d52946c22a7a8a730897dfff9e1cd9ad069a", - "source_tree": "sha256:035bce1bfdbd9e88ae98df0849e50d7bc64ab72563b896c9f506dbb9dd61eeca" + "files": 75, + "generated_at_commit": "d044d9af667dd04d28d9dbf64d2e6ca4caad67ce", + "source_tree": "sha256:d94092b10ffb7c167c9dc040a93b29958242ca18e470d771876b9da997270306" }, "owner_lane": "", "provenance": { diff --git a/mk/lattice.mk b/mk/lattice.mk new file mode 100644 index 000000000..9c6acfc9b --- /dev/null +++ b/mk/lattice.mk @@ -0,0 +1,41 @@ +# SI LATTICE L1 (omega-mv #698): sparse meaning x method x realization +# candidate lattice over rx_costmodel. Picked up by `-include mk/*.mk`. +# New files only: src/runtime/rx_lattice.{c,h}, +# tests/runtime/rx_lattice_{fixture.h,unit.c,bench.c}. +# +# rx_lattice.o is a calculator like rx_costmodel.o: built alone first, it must +# not reference anything that maps, forks or runs code, or any authority or +# generation operation. +# +# test-lattice: AArch64 host; modeled time, real arm outputs, all mutants. +# bench-lattice: AArch64 host; measured wall time of serial / lcf / q3d on +# the same objective with real pinned CPU lanes. No GPU is used. +.PHONY: test-lattice bench-lattice +RX_LAT_OBJ = $(OUT_DIR)/rx_lattice.o +RX_LAT_MV_SRCS = src/sha256.c src/omega_evidence.c src/omega_canonical.c src/omega_validate.c \ + src/omega_core.c src/omega_codec.c src/aarch64_encoder.c src/aarch64_decoder.c \ + src/omega_realize.c src/omega_realize_synth.c src/omega_program.c src/omega_machine.c \ + src/omega_exec.c src/omega_verify.c src/omega_matvec.c src/omega_matvec_quad.c \ + src/runtime/rx_costmodel.c +RX_LAT_UNIT = $(OUT_DIR)/rx_lattice_unit +RX_LAT_BENCH = $(OUT_DIR)/rx_lattice_bench + +$(RX_LAT_OBJ): src/runtime/rx_lattice.c src/runtime/rx_lattice.h src/runtime/rx_costmodel.h | $(OUT_DIR) + $(CC) $(CFLAGS) -c -o $@ src/runtime/rx_lattice.c + @if nm -u $@ | grep -E 'rx_gen_|aienos_cap_|rx_caproot_|rx_capadmin_|rx_world_|mmap|mprotect|fork|exec|dlopen|system|pthread|open|write' ; then \ + echo "rx_lattice.o references an operation a calculator must not have"; \ + rm -f $@; exit 1; fi + +$(RX_LAT_UNIT): tests/runtime/rx_lattice_unit.c tests/runtime/rx_lattice_fixture.h $(RX_LAT_OBJ) \ + $(RX_LAT_MV_SRCS) | $(OUT_DIR) + $(CC) $(CFLAGS) -Itests -o $@ tests/runtime/rx_lattice_unit.c $(RX_LAT_OBJ) $(RX_LAT_MV_SRCS) -lm + +$(RX_LAT_BENCH): tests/runtime/rx_lattice_bench.c tests/runtime/rx_lattice_fixture.h $(RX_LAT_OBJ) \ + $(RX_LAT_MV_SRCS) | $(OUT_DIR) + $(CC) $(CFLAGS) -Itests -pthread -o $@ tests/runtime/rx_lattice_bench.c $(RX_LAT_OBJ) $(RX_LAT_MV_SRCS) -lm + +test-lattice: $(RX_LAT_UNIT) + ./$(RX_LAT_UNIT) + +bench-lattice: $(RX_LAT_BENCH) + ./$(RX_LAT_BENCH) diff --git a/spec/.crumb b/spec/.crumb index e63063df8..8536361c2 100644 --- a/spec/.crumb +++ b/spec/.crumb @@ -20,14 +20,14 @@ } ], "children_root": "sha256:6834a3241647d3a098283d4eafedbdb1428dfa4d834f9ed5f22025b6d2303093", - "compiled_at": "2026-10-16T07:53:23Z", + "compiled_at": "2026-10-25T02:29:06Z", "compiler": "crumb-compile/1", - "digest": "sha256:6327473d41e10acbf43be0918d3557509322bfd7d6e075e114c79c2600a47305", + "digest": "sha256:3d820d0f9b2bce822e5556cf43cef5325148288fa4403f3cc5eb53945bf1b028", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 58, - "generated_at_commit": "4197e0bb49ad1e67d9dac37f259246470ffecedc", - "source_tree": "sha256:2130510e8275fd1ae1fdf6078a82521c7ac2fe0ea6ada8a9d1156f0f2ba191fd" + "files": 59, + "generated_at_commit": "d044d9af667dd04d28d9dbf64d2e6ca4caad67ce", + "source_tree": "sha256:ee79154e3b31333f04a8f332cd78ef86c53ca457b3edf2f4c8d1798846e8fdb6" }, "owner_lane": "", "provenance": { diff --git a/spec/candidate-lattice.md b/spec/candidate-lattice.md new file mode 100644 index 000000000..91fd1c437 --- /dev/null +++ b/spec/candidate-lattice.md @@ -0,0 +1,185 @@ +# Candidate lattice: meaning × method × realization (SI LATTICE L1, #698) + +Code: `src/runtime/rx_lattice.{c,h}`. Tests: `tests/runtime/rx_lattice_unit.c` +(`make test-lattice`, AArch64) and `tests/runtime/rx_lattice_bench.c` +(`make bench-lattice`, AArch64 host CPUs, no GPU). Shared fixture: +`tests/runtime/rx_lattice_fixture.h`. Build fragment: `mk/lattice.mk`. +Evidence: `evidence/LATTICE-698/`. + +## Rule + +For one bounded, typed objective graph, the lattice enumerates the sparse set +`C[i,j,k]`: + +- `i` is an obligation, numbered like `rx_graph.h` (`AgKind`: CONST, + PHYSICAL, JOIN, EFFECT_PERFORM); +- `j` is a qualified method (for MATVEC, an `rx_costmodel` arm); +- `k` is a reported realization: a core class with a fixed lane count, or a + FORGE realization with its descriptor digest and verified flag. + +Only declared supported `(j, k)` pairs are enumerated. Each coordinate has a +SHA-256 identity over the obligation (shape, weights, scope, precision, +generation, dependencies), the method (arm, revision, precision, rights, seat, +source digest) and the realization (core, lanes, descriptor). + +**A coordinate is a proposal, not permission.** Admission records the first +refusal reason in this order: precision, stale revision, privilege, scope, +realization (unverified, or the live descriptor has changed), model seat, +unverified method, changed cost evidence, unknown cost, failed arm, budget. +The caller still runs work only through its own verified path. A node is +committed only from a receipt that binds the dispatched candidate and reports +a passing independent verification. + +## Cost + +There is no second cost model. Mean and spread come from `rx_cm_predict` +(features: the method's arm, the realization's core class, cache and +pressure). The lattice pins `rx_cm_digest` at admission; if the evidence later +differs, every non-incumbent MATVEC coordinate is refused. An operation that +`rx_costmodel` does not cover (SOURCE, JOIN, EFFECT here) has no prediction, +so only its incumbent may run, charged at the obligation's work cap. If there +is no cap, the incumbent is refused too. Unknown cost is never zero. An arm +with a recorded wrong result is excluded, as in `rx_cm_decide`. + +## Graph checks + +`rx_lat_validate` reports `AG_E_TYPE` for: + +- a dependency out of range, duplicated, or on an effect; +- wrong arity, or a kind/op mismatch; +- two shapes for one weight id. + +It reports `AG_E_CYCLE` (a self-dependency counts) and `AG_E_EFFECT_ORDER` +for an effect without the EFFECT right or without a transaction id. It +reports `RX_LAT_E_SCOPE` when data flows from a tenant-private node into +another scope. Public data may flow into any scope. + +## Policies + +All three see the same admitted set and the same lanes. A lane runs one +coordinate at a time. A single-owner device has one lane, so concurrency +never exceeds what a realization declares. + +| policy | node order | candidate | +|---|---|---| +| `serial` | one at a time, topological | incumbent method, first admitted realization | +| `lcf` | lowest predicted cost first, dependency-driven | lowest predicted mean with a free lane now | +| `q3d` | longest remaining predicted path first | minimum predicted finish = lane availability + upper confidence cost (z = 1). It waits for a busy lane when that is predicted to finish first. Ties go to the incumbent, then to the lane already holding the weights, then to the lowest index. | + +Effects are committed by the single dispatcher. This happens only after every +input is DONE, and at most once per transaction id (`RX_LAT_E_REPLAY` +otherwise). Pure outputs may be reused through `RxLatCache`. The key covers +the node's semantics and its input digests, not the method or realization. A +hit needs the same scope, the same generation and an intact binding. Anything +else is refused and recomputed. Effects have no cache key. + +The lattice is a calculator, as `rx_costmodel` is. `mk/lattice.mk` builds +`rx_lattice.o` alone and fails if it references anything that maps, forks, +executes or opens anything, or any authority or generation operation. + +## Fixture + +The fixture is a 17-node graph: + +- fork/join, mixed privacy scopes (public, tenant A, tenant B), shared + weights, and one trusted effect for tenant A; +- methods: the reference (incumbent), two emitted arms (M12 scalar, R10 quad4), + a planted wrong arm and a declared model-call method; +- realizations: two Cortex-X925 lanes, two Cortex-A725 lanes, and one + simulated single-owner accelerator lane leased to tenant A. + +Every emitted arm passes a differential test in a forked child before +admission. Every node output is checked independently before its receipt is +sealed: four matvec rows are recomputed plus guard words, and SOURCE and JOIN +are recomputed in full. A reference-only oracle that does not use the lattice +recomputes the whole graph. + +## Results + +### Unit test (`make test-lattice`, 237 checks, 0 failures) + +Positive results: + +- 109 candidates, against 408 in the dense cube. Identities are distinct and + build plus admission is deterministic. +- All three policies complete all 17 nodes. Every output equals the oracle and + serial, and the effect is committed once with an identical value. +- No realization ever exceeds its declared lanes. +- The planted arm and the model-call arm never run. + +Mutants, each on a fresh fixture: + +- a cycle and a self-dependency; +- wrong dependencies: effect consumed, out of range, arity, duplicate, weight + shape, kind, cross-tenant flow; +- a stale revision and a stale precision (method and realization); +- illegitimate cache entries: tampered output, another tenant, stale + generation; +- privilege escalation (a method asks for EFFECT), an effect without the + right, a compute node committing an effect, and an effect before its inputs; +- budget starvation: the node and its dependents are refused, no effect, and + independent work still completes; +- the model worker down (refused, math path used), then the seat up + (admissible, not run here); +- a self-modified receipt, a receipt for another candidate, and a replayed + receipt; +- changed cost evidence after pinning; +- an unverified FORGE realization, and a changed live descriptor; +- transaction replay; +- unknown cost with and without caps; +- an arm with a recorded wrong result. + +The unit test's makespans are MODELED from a synthetic truth table. They are +not measurements: serial 658.3 µs, lcf 154.2 µs, q3d 118.6 µs. + +### Host benchmark (`make bench-lattice`, measured) + +Five runs: 15 or 31 paired rounds each after one warm-up, in rotating policy +order. The recorded run is `evidence/LATTICE-698/20261024T1827Z-a8742c96/`. +The cost model is fitted from 240 timed observations on a training grid that +does not contain the graph's shapes. `gpu.sim` is refused (unverified: no +FORGE lease), so no GPU work happens. Results on the Spark host CPUs +(loadavg 3.8–5.3), range of the per-run median: + +| policy | wall | busy sum | busy / wall | realized critical path | +|---|---|---|---|---| +| serial | 656–688 µs | 473–494 µs | 0.69–0.75 | 187–195 µs | +| lcf | 243–281 µs | 427–435 µs | 1.52–1.79 | 156–164 µs | +| q3d | 303–345 µs | 369–384 µs | 1.08–1.25 | 147–152 µs | + +- **q3d loses to lcf:** a median wall ratio of 1.21–1.26. q3d was faster in + 0/31, 4/31, 5/31, 1/15 and 0/15 paired rounds. One-sided sign test for the + recorded run, P(0 of 31) = 4.7 × 10⁻¹⁰. +- Both beat serial: lcf/serial 0.37–0.41 and q3d/serial 0.46–0.50, in 14–31 + of each run's rounds. +- q3d does the least total work (all quad4 on X925, lowest busy sum and + shortest realized critical path). It keeps the A725 lanes idle because it + predicts that waiting for an X925 lane finishes sooner. +- Serial's wall exceeds its busy sum by 9.7–10.6 µs per node, which is thread + hand-off and dispatch. `rx_costmodel` has no feature for that overhead. With + nodes of 10–40 µs, waiting costs more than predicted. +- Selection overhead (inside pick and finish) is 14–27 µs per objective. It + depends on which cpu the dispatcher lands on. + +Not measured: energy, memory bytes, GPU utilization. None is claimed. + +## Status and limits + +HOST_TESTED for the calculator, the mutants and the CPU-lane benchmark. + +**Migration debt.** Under the seeded-`.om` rule this is a C implementation. +It consumes `rx_costmodel`, which is C with IEEE doubles. The seeded `omc` +language has no floating point or fixed-point `log2`/`exp2`, and no C ABI or +import. So an `.om` lattice can neither call nor faithfully port the cost +model's predictive arithmetic. Both are tracked under the compiler owner in +omega-mv #636 (FR4 IEEE floats, FR7 ABI/imports). The `.om` successor of +`rx_costmodel` and of this lattice depends on that issue. + +Not done, and not claimed: + +- signed proposal and outcome links into Cortex: the receipt binding is + SHA-256, not a signature; +- the real accelerator realization, FORGE leases (physics-mv #77), the model + seat (Core #735) and batching (#695); +- a cost feature for dispatch overhead; +- any real-chip or Spark GPU claim. diff --git a/src/.crumb b/src/.crumb index 76700a054..9be921ead 100644 --- a/src/.crumb +++ b/src/.crumb @@ -62,140 +62,140 @@ "children": [ { "digest": "728ab6985dea994eb78a7eb2521384a911492d1c36819c17970a5dc3a8a0c5e2", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "algebra", "purpose": "Directory src/algebra. Purpose not yet described by a human." }, { "digest": "1881ed1d139a405a777d24d4b26f97bc8405dee0552d6ce21841ce4833560ce2", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "ans", "purpose": "ANS (Alignment Navigation System), Inertial Alignment v1: frozen reference atoms, drift filter o" }, { "digest": "92c3e2481f7457afd6c7717e6c1a75ebab950ec9114b48025dd137a6edb2975c", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "autodiff", "purpose": "M21 OMEGA_AUTODIFF: reverse-mode tape over M20 tensor CPU ops, caller-owned nodes and scratch, p" }, { "digest": "3bd51397fcd28cd54db65c1df72c320883798bc8550779b2b79c8498b28ffb0d", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "compiler", "purpose": "Directory src/compiler. Purpose not yet described by a human." }, { "digest": "36a66980895553ae73884798bc09a92141e1aeba5e5068a7d9d62a99bad02548", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "crumbline", "purpose": "Directory src/crumbline. Purpose not yet described by a human." }, { "digest": "c75df77ddb2bae35ac5e1225a0ae9dcef77b24608775c7b8d02dce1af0ec3ca4", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "dual_experiment", "purpose": "DUAL applicability experiment library (ARCH-0031 offline evidence lane): deterministic discrete " }, { "digest": "8856d5162c55cb33997201cd580638f80ea933b46e11c61e1913185d419731d7", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "estimation", "purpose": "Directory src/estimation. Purpose not yet described by a human." }, { "digest": "391e337c60cb8b2ae549595394482c23717c1757d83747a7446e81cd780750c1", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "fabric", "purpose": "Directory src/fabric. Purpose not yet described by a human." }, { "digest": "2d33eb528dafb5954bb027cdd9a7c7923594c177f17181194311155b4e1f40b3", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "language", "purpose": "Directory src/language. Purpose not yet described by a human." }, { "digest": "cd0551dbfb6fc354741efe9a3ad1808ffaaf6a8d7e9e7345851333d7a721cee0", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "osh", "purpose": "osh (Omega-native shell, arch#158) lexer unit: layout list, generator, osh_lex.osc, C reference " }, { "digest": "2b5dd2c64d4bbba8c41471dc1af5a0b00bfc1361a2c680d78b99fb30b5b547fe", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "path", "purpose": "Directory src/path. Purpose not yet described by a human." }, { "digest": "e0b91129cc9c188fc0e47b7f3189a3a9a09f9c55eecd018786794571ffc39aad", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "physics0", "purpose": "PD-0 hidden-equation benchmark (Physics-0 Discovery Engine cut 1): world, oracle, scorer, ladder" }, { "digest": "8b923fb5dc1de3b04afae043a4770f7b1fb27d959a8f597191ae2556d822a1cf", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "polyglot", "purpose": "Directory src/polyglot. Purpose not yet described by a human." }, { "digest": "076f486fdf51e12a26160e7f0bf7686fea611db54216f7fef81367620e7a6bdb", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "reservoir", "purpose": "Ionic-droplet reservoir simulator (omega#440): binary32 fixed-step RK4 of the authors' droplet m" }, { - "digest": "c6af24b823d6a18a8ad1593003a214fcd577955d1d134bc4304a5c097a71cd35", - "last_commit": "d863780eb", + "digest": "739cb1d1e9c410a30cc37cb28477fe649feebe18eede368d85eb0f1f3db48e6a", + "last_commit": "a8742c96", "name": "runtime", "purpose": "Directory src/runtime. Purpose not yet described by a human." }, { "digest": "af5fcc64fe83dc35f782724372854febde5461f5eab8b327f3c3ed75f8572053", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "searchtrace", "purpose": "Directory src/searchtrace. Purpose not yet described by a human." }, { "digest": "b82288db7e732d10f954e921ac98c60e19579a3e58118cc12c49230587929ea2", - "last_commit": "ed8de5736", + "last_commit": "ed8de573", "name": "synapse", "purpose": "Synapse ADR 0003 J1 binary codec port: layout list, generator, syn_codec.osc (A1 WAL records + c" }, { "digest": "9723dd7c18d4e2c22db631680974e1025bccb9c309619424e2f4dfcc43cc7068", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "tensor", "purpose": "M20 OMEGA_TENSOR: immutable tensor semantic layer, views, generation-checked storage, CPU realiz" }, { "digest": "06d9fb18dddf7ab0f8ad02fffd15ce8b80808c77d0904609b6baae83c2b05ce5", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "train", "purpose": "Directory src/train. Purpose not yet described by a human." }, { "digest": "812a7faed07b21e066b732ed596eb0aea609379a2b2af0e8b61dd1ea05bef602", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "turing", "purpose": "Directory src/turing. Purpose not yet described by a human." }, { "digest": "b2e34b2ea0392059ac30d17e27289b1146e523db60a02ef825edef96036817ab", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "visor", "purpose": "Directory src/visor. Purpose not yet described by a human." } ], - "children_root": "sha256:4460b4a6d5ed4054d462af660a6a02691350a35a8f44197e60ce605e243f698e", - "compiled_at": "2026-10-24T23:55:01Z", + "children_root": "sha256:83f71730739189815ee5a34f946053a6d86b6d7bdb2f34b7940860536559a8bf", + "compiled_at": "2026-10-25T02:29:06Z", "compiler": "crumb-compile/1", - "digest": "sha256:8ab10b555fb095ec6fc706f96f7294d2b99ce62438600a493fe235ec11bcd0d6", + "digest": "sha256:91079e941f8e481e45f20f7bb898dd32d7c03b534c779807dbfc9e76fb8a697a", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 565, - "generated_at_commit": "4cd9ad9845612038a49c15940811ce24c38c061d", - "source_tree": "sha256:093f3a2aba4e74fd82076e3921f29b215d8fe508982a525f3c5b487c950e64bd" + "files": 567, + "generated_at_commit": "d044d9af667dd04d28d9dbf64d2e6ca4caad67ce", + "source_tree": "sha256:8c6c836886b2a9282147e3ae2c9a42d57c5172676512b4503e5f66eb9333ff2b" }, "owner_lane": "", "provenance": { diff --git a/src/runtime/.crumb b/src/runtime/.crumb index 5e0a0c362..ef75655c6 100644 --- a/src/runtime/.crumb +++ b/src/runtime/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-24T04:15:18Z", + "compiled_at": "2026-10-25T02:29:06Z", "compiler": "crumb-compile/1", - "digest": "sha256:c6af24b823d6a18a8ad1593003a214fcd577955d1d134bc4304a5c097a71cd35", + "digest": "sha256:739cb1d1e9c410a30cc37cb28477fe649feebe18eede368d85eb0f1f3db48e6a", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 65, - "generated_at_commit": "dc7c1304c038f49db3b121d5a5e27d7bcab5bf6c", - "source_tree": "sha256:8a697a598a04d6cd0bfaf1bfea1a95800cd8cc6288b14ab5cef835155f7cf211" + "files": 67, + "generated_at_commit": "d044d9af667dd04d28d9dbf64d2e6ca4caad67ce", + "source_tree": "sha256:ab41e97e9b046a63d4a5827e6a87761f3e95f7f5362f31649084b86497281467" }, "owner_lane": "", "provenance": { diff --git a/src/runtime/rx_lattice.c b/src/runtime/rx_lattice.c new file mode 100644 index 000000000..43ec464de --- /dev/null +++ b/src/runtime/rx_lattice.c @@ -0,0 +1,694 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2026 Drake Stapleton (AIEN / aien-dev) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* rx_lattice.c -- see rx_lattice.h. A calculator: no I/O, no code execution. */ +#include "rx_lattice.h" +#include "sha256.h" + +#include +#include + +/* ---- small helpers ---- */ + +static void put_u64(sha256_ctx *c, uint64_t v) { + uint8_t b[8]; + for (int i = 0; i < 8; i++) b[i] = (uint8_t)(v >> (8 * i)); + sha256_update(c, b, 8); +} + +static void put_tag(sha256_ctx *c, const char *tag) { + sha256_update(c, (const uint8_t *)tag, strlen(tag) + 1u); +} + +static uint64_t ps_of_log2(double l2) { + if (!(l2 > 0.0)) return 1; + if (l2 >= 63.0) return UINT64_MAX; + double v = exp2(l2); + return v < 1.0 ? 1 : (uint64_t)v; +} + +static uint64_t arm_failures(const RxCostModel *m, uint32_t arm) { + uint64_t f = 0; + for (uint32_t c = 0; c < RX_CM_CELLS; c++) f += m->cell[c][arm].failures; + return f; +} + +static int has_dep(const RxLatNode *n, uint32_t d) { + for (uint32_t k = 0; k < n->n_deps; k++) + if (n->dep[k] == d) return 1; + return 0; +} + +/* ---- building ---- */ + +void rx_lat_init(RxLattice *L) { + memset(L, 0, sizeof *L); + L->z = 1.0; +} + +int rx_lat_add_node(RxLattice *L, const RxLatNode *n) { + if (!L || !n) return RX_LAT_E_ARG; + if (L->n_nodes >= RX_LAT_MAX_NODES) return RX_LAT_E_FULL; + L->node[L->n_nodes] = *n; + return (int)L->n_nodes++; +} + +int rx_lat_add_method(RxLattice *L, const RxLatMethod *m) { + if (!L || !m) return RX_LAT_E_ARG; + if (L->n_methods >= RX_LAT_MAX_METHODS) return RX_LAT_E_FULL; + if (m->op == RX_LAT_OP_MATVEC && m->arm >= RX_CM_ARMS) return RX_LAT_E_ARG; + L->method[L->n_methods] = *m; + return (int)L->n_methods++; +} + +int rx_lat_add_real(RxLattice *L, const RxLatReal *r) { + if (!L || !r) return RX_LAT_E_ARG; + if (L->n_reals >= RX_LAT_MAX_REALS) return RX_LAT_E_FULL; + if (r->lanes == 0 || r->lanes > RX_LAT_MAX_LANES) return RX_LAT_E_ARG; + L->real[L->n_reals] = *r; + return (int)L->n_reals++; +} + +int rx_lat_support(RxLattice *L, uint32_t method, uint32_t real) { + if (!L || method >= L->n_methods || real >= L->n_reals) return RX_LAT_E_ARG; + L->supported[method][real] = 1; + return RX_LAT_OK; +} + +static int kind_matches(const RxLatNode *n) { + switch (n->op) { + case RX_LAT_OP_MATVEC: return n->kind == RX_LAT_KIND_PHYSICAL; + case RX_LAT_OP_SOURCE: return n->kind == RX_LAT_KIND_CONST; + case RX_LAT_OP_JOIN: return n->kind == RX_LAT_KIND_JOIN; + case RX_LAT_OP_EFFECT: return n->kind == RX_LAT_KIND_EFFECT; + default: return 0; + } +} + +static int arity_ok(const RxLatNode *n) { + switch (n->op) { + case RX_LAT_OP_SOURCE: return n->n_deps == 0; + case RX_LAT_OP_MATVEC: return n->n_deps == 1; + case RX_LAT_OP_JOIN: return n->n_deps >= 2; + case RX_LAT_OP_EFFECT: return n->n_deps >= 1; + default: return 0; + } +} + +int rx_lat_validate(RxLattice *L) { + if (!L || L->n_nodes == 0) return RX_LAT_E_ARG; + uint32_t n = L->n_nodes; + /* Types, arity, ranges. A self-dependency is a cycle, reported below. */ + for (uint32_t i = 0; i < n; i++) { + const RxLatNode *a = &L->node[i]; + if (a->op >= RX_LAT_OP_COUNT || !kind_matches(a) || a->n_deps > RX_LAT_MAX_DEPS || + !arity_ok(a) || a->scope >= RX_LAT_MAX_SCOPES || a->precision >= 32u) + return RX_LAT_E_TYPE; + if (a->op == RX_LAT_OP_MATVEC && (a->M == 0 || a->N == 0 || a->M > 8192u || a->N > 8192u)) + return RX_LAT_E_TYPE; + if ((a->op == RX_LAT_OP_SOURCE || a->op == RX_LAT_OP_JOIN) && (a->N == 0 || a->N > 8192u)) + return RX_LAT_E_TYPE; + for (uint32_t k = 0; k < a->n_deps; k++) { + uint32_t d = a->dep[k]; + if (d >= n) return RX_LAT_E_TYPE; + if (d == i) continue; + for (uint32_t q = 0; q < k; q++) + if (a->dep[q] == d) return RX_LAT_E_TYPE; + /* An effect yields a receipt, never a value another node computes on. */ + if (L->node[d].op == RX_LAT_OP_EFFECT) return RX_LAT_E_TYPE; + } + if (a->op == RX_LAT_OP_MATVEC) + for (uint32_t j = 0; j < i; j++) { + const RxLatNode *b = &L->node[j]; + if (b->op == RX_LAT_OP_MATVEC && b->weight == a->weight && (b->M != a->M || b->N != a->N)) + return RX_LAT_E_TYPE; + } + } + /* Acyclic: Kahn, lowest index first, so the order is deterministic. */ + uint32_t indeg[RX_LAT_MAX_NODES], placed = 0; + uint8_t used[RX_LAT_MAX_NODES]; + memset(used, 0, sizeof used); + for (uint32_t i = 0; i < n; i++) indeg[i] = L->node[i].n_deps; + while (placed < n) { + uint32_t pick = n; + for (uint32_t i = 0; i < n && pick == n; i++) + if (!used[i] && indeg[i] == 0) pick = i; + if (pick == n) return RX_LAT_E_CYCLE; + used[pick] = 1; + L->topo[placed++] = (uint16_t)pick; + for (uint32_t s = 0; s < n; s++) + if (!used[s] && has_dep(&L->node[s], pick)) indeg[s]--; + } + /* Privacy: a node may consume public data or data of its own scope. */ + for (uint32_t i = 0; i < n; i++) { + const RxLatNode *a = &L->node[i]; + for (uint32_t k = 0; k < a->n_deps; k++) { + uint32_t s = L->node[a->dep[k]].scope; + if (s != 0 && s != a->scope) return RX_LAT_E_SCOPE; + } + } + /* Effects: explicit EFFECT right and a transaction identity. */ + for (uint32_t i = 0; i < n; i++) { + const RxLatNode *a = &L->node[i]; + if (a->op == RX_LAT_OP_EFFECT && (!(a->rights & RX_LAT_RIGHT_EFFECT) || a->txid == 0)) + return RX_LAT_E_EFFECT; + } + return RX_LAT_OK; +} + +static void cand_identity(const RxLattice *L, uint32_t i, uint32_t j, uint32_t k, uint8_t id[32]) { + const RxLatNode *a = &L->node[i]; + const RxLatMethod *m = &L->method[j]; + const RxLatReal *r = &L->real[k]; + sha256_ctx c; + sha256_init(&c); + put_tag(&c, "rx_lattice.candidate.v1"); + put_u64(&c, i); + put_u64(&c, a->kind); + put_u64(&c, a->op); + put_u64(&c, a->M); + put_u64(&c, a->N); + put_u64(&c, a->weight); + put_u64(&c, a->scope); + put_u64(&c, a->precision); + put_u64(&c, a->gen); + put_u64(&c, a->n_deps); + for (uint32_t d = 0; d < a->n_deps; d++) put_u64(&c, a->dep[d]); + put_u64(&c, j); + put_u64(&c, m->op); + put_u64(&c, m->arm); + put_u64(&c, m->revision); + put_u64(&c, m->precision); + put_u64(&c, m->rights); + put_u64(&c, m->needs_seat); + sha256_update(&c, m->source, 32); + put_u64(&c, k); + put_u64(&c, r->core); + put_u64(&c, r->lanes); + sha256_update(&c, r->descriptor, 32); + sha256_final(&c, id); +} + +int rx_lat_build(RxLattice *L) { + int rc = rx_lat_validate(L); + if (rc != RX_LAT_OK) return rc; + L->n_cands = 0; + for (uint32_t i = 0; i < L->n_nodes; i++) { + L->cand_first[i] = (uint16_t)L->n_cands; + L->cand_count[i] = 0; + for (uint32_t j = 0; j < L->n_methods; j++) { + if (L->method[j].op != L->node[i].op) continue; + for (uint32_t k = 0; k < L->n_reals; k++) { + if (!L->supported[j][k]) continue; + if (L->n_cands >= RX_LAT_MAX_CANDS) return RX_LAT_E_FULL; + RxLatCand *c = &L->cand[L->n_cands++]; + memset(c, 0, sizeof *c); + c->node = (uint16_t)i; + c->method = (uint8_t)j; + c->real = (uint8_t)k; + c->reason = RX_LAT_R_UNKNOWN_COST; /* nothing admitted before rx_lat_admit */ + cand_identity(L, i, j, k, c->id); + L->cand_count[i]++; + } + } + } + return (int)L->n_cands; +} + +void rx_lat_pin(RxLattice *L, const RxCostModel *m) { + rx_cm_digest(m, L->cm_digest); + L->pinned = 1; +} + +static uint32_t incumbent_arm(const RxLattice *L, uint32_t op) { + for (uint32_t j = 0; j < L->n_methods; j++) + if (L->method[j].op == op && L->method[j].incumbent) return L->method[j].arm; + return 0; +} + +static void features(const RxLattice *L, const RxLatNode *a, const RxLatReal *r, RxCmFeatures *f) { + memset(f, 0, sizeof *f); + f->op = RX_CM_OP_MATVEC; + f->M = a->M; + f->N = a->N; + f->state_bytes = ((uint64_t)a->M * a->N + a->M + a->N) * 8u; + f->cache_bytes = r->cache_bytes; + f->core = r->core; + f->pressure = r->pressure; + f->current_arm = incumbent_arm(L, RX_LAT_OP_MATVEC); + for (uint32_t j = 0; j < L->n_methods; j++) + if (L->method[j].op == RX_LAT_OP_MATVEC && L->method[j].verified) f->eligible |= 1u << L->method[j].arm; + f->need_evidence = 1; + f->latency_budget_ps = a->budget_ps; +} + +static uint32_t admit_one(const RxLattice *L, RxLatCand *c, const RxCostModel *m, const RxCmPolicy *p, + int evidence_ok) { + const RxLatNode *a = &L->node[c->node]; + const RxLatMethod *me = &L->method[c->method]; + const RxLatReal *r = &L->real[c->real]; + c->n_obs = 0; + c->mean_log2_ps = c->sd_log2 = 0.0; + c->est_ps = c->ucb_ps = 0; + if (me->precision != a->precision || !(r->precisions & (1u << a->precision))) return RX_LAT_R_PRECISION; + if (me->revision != me->evidence_rev) return RX_LAT_R_STALE; + if (me->rights & ~a->rights) return RX_LAT_R_PRIVILEGE; + if (!(r->scopes & (1u << a->scope))) return RX_LAT_R_SCOPE; + if (!r->forge_verified || memcmp(r->descriptor, r->live, 32) != 0) return RX_LAT_R_REALIZATION; + if (me->needs_seat && !L->seat_up) return RX_LAT_R_SEAT; + if (!me->verified) return RX_LAT_R_UNVERIFIED; + int have = 0; + if (a->op == RX_LAT_OP_MATVEC) { + if (!evidence_ok) { + if (!me->incumbent) return RX_LAT_R_EVIDENCE; + } else { + RxCmFeatures f; + RxCmPrediction pr; + features(L, a, r, &f); + rx_cm_predict(m, &f, me->arm, &pr); + if (arm_failures(m, me->arm) > 0 && pr.fail_p > p->max_fail_p) return RX_LAT_R_FAILED; + if (pr.n > 0) { + have = 1; + c->n_obs = pr.n; + c->mean_log2_ps = pr.mean_log2_ps; + c->sd_log2 = pr.sd_log2; + } + } + } + if (have) { + c->est_ps = ps_of_log2(c->mean_log2_ps); + c->ucb_ps = ps_of_log2(c->mean_log2_ps + L->z * c->sd_log2); + } else if (me->incumbent && a->budget_ps) { + /* No prediction: the incumbent may run, charged at its cap, never zero. */ + c->est_ps = c->ucb_ps = a->budget_ps; + } else { + return RX_LAT_R_UNKNOWN_COST; + } + if (a->budget_ps && c->ucb_ps > a->budget_ps) return RX_LAT_R_BUDGET; + return RX_LAT_ADMITTED; +} + +int rx_lat_admit(RxLattice *L, const RxCostModel *m, const RxCmPolicy *p) { + uint8_t now[32]; + rx_cm_digest(m, now); + int evidence_ok = L->pinned && memcmp(now, L->cm_digest, 32) == 0; + memset(L->reasons, 0, sizeof L->reasons); + for (uint32_t c = 0; c < L->n_cands; c++) { + L->cand[c].reason = admit_one(L, &L->cand[c], m, p, evidence_ok); + L->reasons[L->cand[c].reason]++; + } + int empty = 0; + for (uint32_t i = 0; i < L->n_nodes; i++) { + int any = 0; + for (uint32_t c = L->cand_first[i]; c < (uint32_t)L->cand_first[i] + L->cand_count[i]; c++) + if (L->cand[c].reason == RX_LAT_ADMITTED) any = 1; + if (!any) empty++; + } + return empty; +} + +/* ---- one run ---- */ + +static int admitted(const RxLattice *L, uint32_t c) { + return L->cand[c].reason == RX_LAT_ADMITTED; +} + +static uint64_t best_est(const RxLattice *L, uint32_t i) { + uint64_t b = 0; + for (uint32_t c = L->cand_first[i]; c < (uint32_t)L->cand_first[i] + L->cand_count[i]; c++) + if (admitted(L, c) && (b == 0 || L->cand[c].est_ps < b)) b = L->cand[c].est_ps; + return b; +} + +static int incumbent_cand(const RxLattice *L, uint32_t i) { + for (uint32_t c = L->cand_first[i]; c < (uint32_t)L->cand_first[i] + L->cand_count[i]; c++) + if (admitted(L, c) && L->method[L->cand[c].method].incumbent) return (int)c; + return -1; +} + +static void propagate(const RxLattice *L, RxLatRun *r) { + for (uint32_t t = 0; t < L->n_nodes; t++) { + uint32_t i = L->topo[t]; + if (r->status[i] != RX_LAT_PENDING) continue; + for (uint32_t k = 0; k < L->node[i].n_deps; k++) { + uint8_t s = r->status[L->node[i].dep[k]]; + if (s == RX_LAT_FAILED || s == RX_LAT_REFUSED) { + r->status[i] = RX_LAT_REFUSED; + r->refused++; + break; + } + } + } +} + +static int deps_done(const RxLattice *L, const RxLatRun *r, uint32_t i) { + for (uint32_t k = 0; k < L->node[i].n_deps; k++) + if (r->status[L->node[i].dep[k]] != RX_LAT_DONE) return 0; + return 1; +} + +int rx_lat_run_init(const RxLattice *L, RxLatRun *r, uint32_t policy) { + if (!L || !r || policy >= RX_LAT_POLICY_COUNT || L->n_cands == 0) return RX_LAT_E_ARG; + uint64_t now = r->now_ps; + memset(r, 0, sizeof *r); + r->policy = policy; + r->now_ps = now; + for (uint32_t i = 0; i < L->n_nodes; i++) { + int ok = policy == RX_LAT_POLICY_SERIAL ? incumbent_cand(L, i) >= 0 : best_est(L, i) > 0; + if (!ok) { + r->status[i] = RX_LAT_REFUSED; + r->refused++; + } + } + propagate(L, r); + /* Upward rank: own best estimate plus the longest successor rank. */ + for (uint32_t t = L->n_nodes; t-- > 0;) { + uint32_t i = L->topo[t]; + uint64_t succ = 0; + for (uint32_t s = 0; s < L->n_nodes; s++) + if (has_dep(&L->node[s], i) && r->rank_ps[s] > succ) succ = r->rank_ps[s]; + r->rank_ps[i] = r->status[i] == RX_LAT_REFUSED ? 0 : best_est(L, i) + succ; + } + for (uint32_t i = 0; i < L->n_nodes; i++) r->ready_ps[i] = now; + for (uint32_t k = 0; k < L->n_reals; k++) + for (uint32_t l = 0; l < RX_LAT_MAX_LANES; l++) r->lane_until[k][l] = now; + return RX_LAT_OK; +} + +static uint64_t real_avail(const RxLattice *L, const RxLatRun *r, uint32_t k) { + uint64_t best = UINT64_MAX; + for (uint32_t l = 0; l < L->real[k].lanes; l++) { + uint64_t t = r->lane_busy[k][l] && r->lane_until[k][l] > r->now_ps ? r->lane_until[k][l] : r->now_ps; + if (t < best) best = t; + } + return best; +} + +/* A free lane of k, preferring one that already holds the weights. -1 if none. */ +static int free_lane(const RxLattice *L, const RxLatRun *r, uint32_t k, uint32_t weight_tag) { + int any = -1; + for (uint32_t l = 0; l < L->real[k].lanes; l++) { + if (r->lane_busy[k][l]) continue; + if (weight_tag && r->lane_weight[k][l] == weight_tag) return (int)l; + if (any < 0) any = (int)l; + } + return any; +} + +static uint32_t weight_tag(const RxLatNode *a) { + return a->op == RX_LAT_OP_MATVEC ? a->weight + 1u : 0u; +} + +static int has_resident(const RxLattice *L, const RxLatRun *r, uint32_t k, uint32_t tag) { + if (!tag) return 0; + for (uint32_t l = 0; l < L->real[k].lanes; l++) + if (!r->lane_busy[k][l] && r->lane_weight[k][l] == tag) return 1; + return 0; +} + +static void dispatch(const RxLattice *L, RxLatRun *r, uint32_t i, uint32_t c, uint32_t lane, RxLatPick *out) { + uint32_t k = L->cand[c].real; + uint32_t tag = weight_tag(&L->node[i]); + r->status[i] = RX_LAT_RUNNING; + r->chosen[i] = (uint16_t)c; + r->lane_of[i] = (uint8_t)lane; + r->start_ps[i] = r->now_ps; + r->lane_busy[k][lane] = 1; + uint64_t span = r->policy == RX_LAT_POLICY_Q3D ? L->cand[c].ucb_ps : L->cand[c].est_ps; + r->lane_until[k][lane] = r->now_ps + span; + if (tag && r->lane_weight[k][lane] != tag) { + r->weight_switches++; + r->lane_weight[k][lane] = tag; + } + r->in_flight++; + r->dispatched[k]++; + out->node = i; + out->cand = c; + out->real = k; + out->lane = lane; +} + +static int pick_serial(const RxLattice *L, RxLatRun *r, RxLatPick *out) { + if (r->in_flight) return 0; + for (uint32_t t = 0; t < L->n_nodes; t++) { + uint32_t i = L->topo[t]; + if (r->status[i] != RX_LAT_PENDING || !deps_done(L, r, i)) continue; + int c = incumbent_cand(L, i); + int lane = c < 0 ? -1 : free_lane(L, r, L->cand[c].real, weight_tag(&L->node[i])); + if (lane < 0) continue; + dispatch(L, r, i, (uint32_t)c, (uint32_t)lane, out); + return 1; + } + return 0; +} + +static int pick_lcf(const RxLattice *L, RxLatRun *r, RxLatPick *out) { + int bi = -1, bc = -1; + uint64_t be = 0; + for (uint32_t t = 0; t < L->n_nodes; t++) { + uint32_t i = L->topo[t]; + if (r->status[i] != RX_LAT_PENDING || !deps_done(L, r, i)) continue; + int nc = -1; + for (uint32_t c = L->cand_first[i]; c < (uint32_t)L->cand_first[i] + L->cand_count[i]; c++) { + if (!admitted(L, c) || free_lane(L, r, L->cand[c].real, 0) < 0) continue; + if (nc < 0 || L->cand[c].est_ps < L->cand[nc].est_ps) nc = (int)c; + } + if (nc < 0) continue; + if (bi < 0 || L->cand[nc].est_ps < be) { + bi = (int)i; + bc = nc; + be = L->cand[nc].est_ps; + } + } + if (bi < 0) return 0; + uint32_t i = (uint32_t)bi, c = (uint32_t)bc; + dispatch(L, r, i, c, (uint32_t)free_lane(L, r, L->cand[c].real, weight_tag(&L->node[i])), out); + return 1; +} + +/* Q3D: is candidate a better than b for node i? */ +static int q3d_better(const RxLattice *L, const RxLatRun *r, uint32_t a, uint32_t b, uint32_t tag) { + uint64_t fa = real_avail(L, r, L->cand[a].real) + L->cand[a].ucb_ps; + uint64_t fb = real_avail(L, r, L->cand[b].real) + L->cand[b].ucb_ps; + if (fa != fb) return fa < fb; + int ia = L->method[L->cand[a].method].incumbent, ib = L->method[L->cand[b].method].incumbent; + if (ia != ib) return ia > ib; + int ra = has_resident(L, r, L->cand[a].real, tag), rb = has_resident(L, r, L->cand[b].real, tag); + if (ra != rb) return ra > rb; + return a < b; +} + +static int pick_q3d(const RxLattice *L, RxLatRun *r, RxLatPick *out) { + uint8_t seen[RX_LAT_MAX_NODES]; + memset(seen, 0, sizeof seen); + for (;;) { + /* Highest rank among unseen ready nodes; topological position breaks ties. */ + int bi = -1; + for (uint32_t t = 0; t < L->n_nodes; t++) { + uint32_t i = L->topo[t]; + if (seen[i] || r->status[i] != RX_LAT_PENDING || !deps_done(L, r, i)) continue; + if (bi < 0 || r->rank_ps[i] > r->rank_ps[bi]) bi = (int)i; + } + if (bi < 0) return 0; + uint32_t i = (uint32_t)bi; + seen[i] = 1; + uint32_t tag = weight_tag(&L->node[i]); + int bc = -1; + for (uint32_t c = L->cand_first[i]; c < (uint32_t)L->cand_first[i] + L->cand_count[i]; c++) + if (admitted(L, c) && (bc < 0 || q3d_better(L, r, c, (uint32_t)bc, tag))) bc = (int)c; + if (bc < 0) continue; + int lane = free_lane(L, r, L->cand[bc].real, tag); + if (lane < 0) continue; /* predicted to finish sooner by waiting for this lane */ + dispatch(L, r, i, (uint32_t)bc, (uint32_t)lane, out); + return 1; + } +} + +int rx_lat_pick(const RxLattice *L, RxLatRun *r, RxLatPick *out) { + if (!L || !r || !out) return 0; + switch (r->policy) { + case RX_LAT_POLICY_SERIAL: return pick_serial(L, r, out); + case RX_LAT_POLICY_LCF: return pick_lcf(L, r, out); + case RX_LAT_POLICY_Q3D: return pick_q3d(L, r, out); + default: return 0; + } +} + +static void binding_of(const RxLattice *L, const RxLatReceipt *rc, uint8_t out[32]) { + sha256_ctx c; + sha256_init(&c); + put_tag(&c, "rx_lattice.receipt.v1"); + put_u64(&c, rc->node); + put_u64(&c, rc->cand); + if (rc->cand < L->n_cands) sha256_update(&c, L->cand[rc->cand].id, 32); + put_u64(&c, rc->verdict); + put_u64(&c, rc->elapsed_ps); + sha256_update(&c, rc->output, 32); + sha256_final(&c, out); +} + +void rx_lat_receipt_seal(const RxLattice *L, RxLatReceipt *rc) { + binding_of(L, rc, rc->binding); +} + +int rx_lat_finish(const RxLattice *L, RxLatRun *r, const RxLatReceipt *rc) { + if (!L || !r || !rc || rc->node >= L->n_nodes) return RX_LAT_E_ARG; + uint32_t i = rc->node; + if (r->status[i] != RX_LAT_RUNNING) return RX_LAT_E_STATE; + uint32_t c = r->chosen[i], k = L->cand[c].real, l = r->lane_of[i]; + r->lane_busy[k][l] = 0; + r->lane_until[k][l] = r->now_ps; + r->in_flight--; + r->finish_ps[i] = r->now_ps; + uint8_t want[32]; + binding_of(L, rc, want); + int bound = rc->cand == c && memcmp(want, rc->binding, 32) == 0; + if (!bound || rc->verdict != 0) { + if (!bound) r->receipt_refusals++; + r->status[i] = RX_LAT_FAILED; + r->failed++; + propagate(L, r); + return bound ? 1 : RX_LAT_E_RECEIPT; + } + r->status[i] = RX_LAT_DONE; + memcpy(r->out[i], rc->output, 32); + r->done++; + for (uint32_t s = 0; s < L->n_nodes; s++) + if (r->status[s] == RX_LAT_PENDING && has_dep(&L->node[s], i) && deps_done(L, r, s)) + r->ready_ps[s] = r->now_ps; + return RX_LAT_OK; +} + +int rx_lat_settled(const RxLattice *L, const RxLatRun *r) { + for (uint32_t i = 0; i < L->n_nodes; i++) + if (r->status[i] == RX_LAT_PENDING || r->status[i] == RX_LAT_RUNNING) return 0; + return 1; +} + +int rx_lat_effect_commit(RxLatLedger *g, const RxLattice *L, const RxLatRun *r, uint32_t node, + uint8_t value[32]) { + if (!g || !L || !r || node >= L->n_nodes) return RX_LAT_E_ARG; + const RxLatNode *a = &L->node[node]; + if (a->op != RX_LAT_OP_EFFECT || !(a->rights & RX_LAT_RIGHT_EFFECT)) return RX_LAT_E_EFFECT; + if (r->status[node] != RX_LAT_RUNNING || !deps_done(L, r, node)) return RX_LAT_E_STATE; + for (uint32_t t = 0; t < g->n; t++) + if (g->txid[t] == a->txid) { + g->replays++; + return RX_LAT_E_REPLAY; + } + if (g->n >= RX_LAT_MAX_LEDGER) return RX_LAT_E_FULL; + sha256_ctx c; + sha256_init(&c); + put_tag(&c, "rx_lattice.effect.v1"); + put_u64(&c, a->txid); + for (uint32_t k = 0; k < a->n_deps; k++) sha256_update(&c, r->out[a->dep[k]], 32); + sha256_final(&c, value); + g->txid[g->n] = a->txid; + memcpy(g->value[g->n], value, 32); + g->n++; + return RX_LAT_OK; +} + +int rx_lat_cache_key(const RxLattice *L, const RxLatRun *r, uint32_t node, uint8_t key[32]) { + if (!L || !r || node >= L->n_nodes) return RX_LAT_E_ARG; + const RxLatNode *a = &L->node[node]; + if (a->op == RX_LAT_OP_EFFECT) return RX_LAT_E_TYPE; /* effects are never reused */ + sha256_ctx c; + sha256_init(&c); + put_tag(&c, "rx_lattice.cache.v1"); + put_u64(&c, a->op); + put_u64(&c, a->M); + put_u64(&c, a->N); + put_u64(&c, a->weight); + put_u64(&c, a->precision); + for (uint32_t k = 0; k < a->n_deps; k++) sha256_update(&c, r->out[a->dep[k]], 32); + sha256_final(&c, key); + return RX_LAT_OK; +} + +static void cache_binding(const RxLatCacheEntry *e, uint8_t out[32]) { + sha256_ctx c; + sha256_init(&c); + put_tag(&c, "rx_lattice.cache.entry.v1"); + sha256_update(&c, e->key, 32); + put_u64(&c, e->scope); + put_u64(&c, e->gen); + sha256_update(&c, e->output, 32); + sha256_final(&c, out); +} + +int rx_lat_cache_put(RxLatCache *c, const uint8_t key[32], uint32_t scope, uint64_t gen, + const uint8_t output[32]) { + if (!c) return RX_LAT_E_ARG; + if (c->n >= RX_LAT_MAX_CACHE) return RX_LAT_E_FULL; + RxLatCacheEntry *e = &c->e[c->n++]; + memcpy(e->key, key, 32); + e->scope = scope; + e->gen = gen; + memcpy(e->output, output, 32); + cache_binding(e, e->binding); + return RX_LAT_OK; +} + +int rx_lat_cache_get(RxLatCache *c, const uint8_t key[32], uint32_t scope, uint64_t gen, + uint8_t output[32]) { + if (!c) return RX_LAT_E_ARG; + int found = 0; + for (uint32_t t = 0; t < c->n; t++) { + const RxLatCacheEntry *e = &c->e[t]; + if (memcmp(e->key, key, 32) != 0) continue; + found = 1; + uint8_t b[32]; + cache_binding(e, b); + if (e->scope == scope && e->gen == gen && memcmp(b, e->binding, 32) == 0) { + memcpy(output, e->output, 32); + c->hits++; + return 0; + } + } + if (found) { + c->refusals++; + return RX_LAT_E_REFUSED; + } + c->misses++; + return 1; +} + +uint64_t rx_lat_critical_path_ps(const RxLattice *L) { + uint64_t fin[RX_LAT_MAX_NODES], cp = 0; + for (uint32_t t = 0; t < L->n_nodes; t++) { + uint32_t i = L->topo[t]; + uint64_t start = 0; + for (uint32_t k = 0; k < L->node[i].n_deps; k++) + if (fin[L->node[i].dep[k]] > start) start = fin[L->node[i].dep[k]]; + fin[i] = start + best_est(L, i); + if (fin[i] > cp) cp = fin[i]; + } + return cp; +} + +const char *rx_lat_reason_name(uint32_t reason) { + static const char *const names[RX_LAT_R_COUNT] = { + "admitted", "precision", "stale", "privilege", "scope", "realization", "seat", + "unverified", "evidence", "unknown_cost", "failed", "budget", + }; + return reason < RX_LAT_R_COUNT ? names[reason] : "?"; +} + +const char *rx_lat_policy_name(uint32_t policy) { + static const char *const names[RX_LAT_POLICY_COUNT] = { "serial", "lcf", "q3d" }; + return policy < RX_LAT_POLICY_COUNT ? names[policy] : "?"; +} diff --git a/src/runtime/rx_lattice.h b/src/runtime/rx_lattice.h new file mode 100644 index 000000000..484b3e227 --- /dev/null +++ b/src/runtime/rx_lattice.h @@ -0,0 +1,304 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2026 Drake Stapleton (AIEN / aien-dev) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * rx_lattice.h -- sparse meaning x method x realization candidate lattice + * (omega-mv #698, SI LATTICE L1). + * + * For one bounded typed objective graph, the lattice enumerates the sparse + * set C[i,j,k]: obligation i, qualified method j, reported realization k. + * Only combinations declared supported are enumerated. A coordinate is a + * PROPOSAL. It authorizes nothing: admission records why each coordinate is + * refused, and the caller still runs work only through its own verified + * execution path and only after the receipt check here. + * + * Cost and uncertainty come only from rx_costmodel (rx_cm_predict): the + * method's arm, the realization's core class and pressure are its features. + * There is no second cost model here. An operation rx_costmodel does not + * cover has no cost prediction, so only its qualified incumbent may run, and + * its estimate is the obligation's declared work cap, never zero. + * + * Like rx_costmodel this is a calculator: it never runs, maps or forks code, + * never reads or mints authority and does no I/O. Whoever runs the work + * reports a sealed receipt; the lattice commits a node only when the receipt + * binds the dispatched candidate and its independent verification passed. + * Effects are recorded at most once per transaction identity. + * + * Three dispatch policies over the same admitted set and the same lanes: + * SERIAL one node at a time in topological order, incumbent method, + * first admitted realization of the incumbent; + * LCF dependency-driven list scheduling: the ready node with the + * lowest predicted cost first, its lowest predicted-cost admitted + * candidate on any free lane; + * Q3D longest remaining predicted path first; the candidate that + * minimizes predicted finish time (lane availability + upper + * confidence cost); a node waits for a busy lane when that is + * predicted to finish first; ties go to the incumbent, then to the + * lane already holding the node's weights, then lowest index. + * A realization offers a fixed number of lanes; a single-owner device has + * one. Nothing is dispatched to a busy lane, so concurrency never exceeds + * what the realization declares. + * + * Numbering follows rx_graph.h: kinds are AgKind values and graph errors + * reuse the AG_E_* values. + */ +#ifndef RX_LATTICE_H +#define RX_LATTICE_H + +#include "rx_costmodel.h" + +#include +#include + +#define RX_LAT_MAX_NODES 32u +#define RX_LAT_MAX_DEPS 4u +#define RX_LAT_MAX_METHODS RX_CM_ARMS +#define RX_LAT_MAX_REALS 4u +#define RX_LAT_MAX_LANES 4u +#define RX_LAT_MAX_CANDS 256u +#define RX_LAT_MAX_CACHE 64u +#define RX_LAT_MAX_LEDGER 64u +#define RX_LAT_MAX_SCOPES 8u + +/* Obligation kinds, rx_graph.h AgKind values. */ +#define RX_LAT_KIND_CONST 1u /* AG_CONST */ +#define RX_LAT_KIND_PHYSICAL 7u /* AG_PHYSICAL */ +#define RX_LAT_KIND_EFFECT 10u /* AG_EFFECT_PERFORM */ +#define RX_LAT_KIND_JOIN 13u /* AG_JOIN */ + +/* Operation classes. MATVEC is rx_costmodel's RX_CM_OP_MATVEC. */ +enum { RX_LAT_OP_MATVEC = RX_CM_OP_MATVEC, RX_LAT_OP_SOURCE, RX_LAT_OP_JOIN, RX_LAT_OP_EFFECT, + RX_LAT_OP_COUNT }; + +/* Rights a node is granted by the objective's approval, and a method needs. */ +#define RX_LAT_RIGHT_COMPUTE 1u +#define RX_LAT_RIGHT_MODEL 2u +#define RX_LAT_RIGHT_EFFECT 4u + +/* Numeric contracts. */ +#define RX_LAT_PREC_EXACT_U64 1u +#define RX_LAT_PREC_APPROX 2u + +/* Graph errors (rx_graph.h AG_E_* values, then lattice-specific ones). */ +#define RX_LAT_OK 0 +#define RX_LAT_E_ARG -1 +#define RX_LAT_E_FULL -2 +#define RX_LAT_E_TYPE -3 /* AG_E_TYPE: wrong dependency, arity, weight shape */ +#define RX_LAT_E_CYCLE -4 /* AG_E_CYCLE */ +#define RX_LAT_E_EFFECT -8 /* AG_E_EFFECT_ORDER: effect not a sink or without EFFECT right */ +#define RX_LAT_E_SCOPE -12 /* data would flow into a less private scope */ +#define RX_LAT_E_STATE -13 /* call out of order */ +#define RX_LAT_E_RECEIPT -14 /* receipt does not bind the dispatched candidate */ +#define RX_LAT_E_REPLAY -15 /* effect transaction already committed */ +#define RX_LAT_E_REFUSED -16 /* cache entry refused */ + +/* Admission: 0 admitted, else the first reason found, in this order. */ +enum { + RX_LAT_ADMITTED = 0, + RX_LAT_R_PRECISION, /* method or realization lacks the required numeric contract */ + RX_LAT_R_STALE, /* method revision differs from the revision its evidence measured */ + RX_LAT_R_PRIVILEGE, /* method needs a right the node was not granted */ + RX_LAT_R_SCOPE, /* realization may not hold this privacy scope */ + RX_LAT_R_REALIZATION, /* FORGE realization unverified or descriptor changed */ + RX_LAT_R_SEAT, /* model-call method with no admitted model seat */ + RX_LAT_R_UNVERIFIED, /* method has not passed independent verification */ + RX_LAT_R_EVIDENCE, /* cost-model evidence differs from the pinned digest */ + RX_LAT_R_UNKNOWN_COST, /* no prediction and not the incumbent (or no cap for it) */ + RX_LAT_R_FAILED, /* predicted failure probability above policy */ + RX_LAT_R_BUDGET, /* upper confidence cost above the node's work cap */ + RX_LAT_R_COUNT +}; + +enum { RX_LAT_PENDING = 0, RX_LAT_RUNNING, RX_LAT_DONE, RX_LAT_FAILED, RX_LAT_REFUSED }; +enum { RX_LAT_POLICY_SERIAL = 0, RX_LAT_POLICY_LCF, RX_LAT_POLICY_Q3D, RX_LAT_POLICY_COUNT }; + +typedef struct { + uint32_t kind; /* RX_LAT_KIND_* */ + uint32_t op; /* RX_LAT_OP_* */ + uint32_t M, N; /* MATVEC shape; SOURCE/JOIN: N = output length */ + uint32_t weight; /* MATVEC: shared weight identity (same id, same M x N) */ + uint32_t scope; /* 0 public, 1.. tenant-private */ + uint32_t rights; /* RX_LAT_RIGHT_* granted */ + uint32_t precision; /* RX_LAT_PREC_* required */ + uint32_t n_deps; + uint16_t dep[RX_LAT_MAX_DEPS]; + uint64_t budget_ps; /* work cap; 0 = none */ + uint64_t txid; /* EFFECT: transaction identity */ + uint64_t gen; /* input generation, binds cache entries */ +} RxLatNode; + +typedef struct { + uint32_t op; + uint32_t arm; /* rx_costmodel arm (MATVEC only) */ + uint32_t rights; /* needed */ + uint32_t precision; + uint32_t revision; /* current source / model revision */ + uint32_t evidence_rev; /* revision the cost evidence was measured with */ + uint32_t needs_seat; /* model call: needs an admitted model seat */ + uint32_t incumbent; /* the known qualified default for its op */ + uint32_t verified; /* passed independent differential verification */ + uint8_t source[32]; /* source digest */ +} RxLatMethod; + +typedef struct { + uint32_t core; /* RX_CM_CORE_* feature */ + uint32_t lanes; /* independent lanes; 1 = single submission owner */ + uint32_t scopes; /* bit s: may hold scope s */ + uint32_t precisions; /* bit p: supports RX_LAT_PREC p */ + uint32_t forge_verified; /* FORGE reported a verified realization */ + uint32_t pressure; /* RX_CM pressure bucket */ + uint64_t cache_bytes; /* last private cache, rx_costmodel feature */ + uint8_t descriptor[32]; /* descriptor digest the lattice was built against */ + uint8_t live[32]; /* descriptor digest observed now */ +} RxLatReal; + +typedef struct { + uint16_t node; + uint8_t method, real; + uint32_t reason; /* RX_LAT_ADMITTED or RX_LAT_R_* */ + uint32_t n_obs; /* observations behind the prediction */ + double mean_log2_ps, sd_log2; /* rx_cm prediction; 0 when none */ + uint64_t est_ps; /* 2^mean, or the work cap for an unmodelled incumbent */ + uint64_t ucb_ps; /* 2^(mean + z sd), or the work cap */ + uint8_t id[32]; /* SHA-256 of the coordinate's complete identity */ +} RxLatCand; + +typedef struct { + uint32_t n_nodes, n_methods, n_reals, n_cands; + RxLatNode node[RX_LAT_MAX_NODES]; + RxLatMethod method[RX_LAT_MAX_METHODS]; + RxLatReal real[RX_LAT_MAX_REALS]; + uint8_t supported[RX_LAT_MAX_METHODS][RX_LAT_MAX_REALS]; + RxLatCand cand[RX_LAT_MAX_CANDS]; + uint16_t cand_first[RX_LAT_MAX_NODES], cand_count[RX_LAT_MAX_NODES]; + uint16_t topo[RX_LAT_MAX_NODES]; /* a topological order (set by validate) */ + uint32_t seat_up; /* a model seat is admitted (Core #735) */ + double z; /* upper confidence width, in sd units */ + uint8_t cm_digest[32]; /* pinned cost-model evidence */ + uint32_t pinned; + uint32_t reasons[RX_LAT_R_COUNT]; /* candidates per admission outcome */ +} RxLattice; + +/* A completion report from whoever ran the work. */ +typedef struct { + uint16_t node, cand; + uint32_t verdict; /* 0: independent verification passed */ + uint64_t elapsed_ps; /* as measured by the runner */ + uint8_t output[32]; /* digest of the produced output */ + uint8_t binding[32]; /* rx_lat_receipt_seal */ +} RxLatReceipt; + +typedef struct { + uint32_t node, cand, real, lane; +} RxLatPick; + +typedef struct { + uint32_t policy; + uint64_t now_ps; /* the caller's clock; set before pick/finish */ + uint8_t status[RX_LAT_MAX_NODES]; + uint16_t chosen[RX_LAT_MAX_NODES]; + uint8_t lane_of[RX_LAT_MAX_NODES]; + uint64_t ready_ps[RX_LAT_MAX_NODES], start_ps[RX_LAT_MAX_NODES], finish_ps[RX_LAT_MAX_NODES]; + uint64_t rank_ps[RX_LAT_MAX_NODES]; + uint8_t lane_busy[RX_LAT_MAX_REALS][RX_LAT_MAX_LANES]; + uint64_t lane_until[RX_LAT_MAX_REALS][RX_LAT_MAX_LANES]; /* predicted free time */ + uint32_t lane_weight[RX_LAT_MAX_REALS][RX_LAT_MAX_LANES]; /* resident weight id + 1 */ + uint8_t out[RX_LAT_MAX_NODES][32]; /* committed output digests */ + uint32_t in_flight, done, failed, refused; + uint32_t dispatched[RX_LAT_MAX_REALS]; + uint32_t weight_switches; + uint32_t receipt_refusals; +} RxLatRun; + +typedef struct { + uint32_t n; + uint64_t txid[RX_LAT_MAX_LEDGER]; + uint8_t value[RX_LAT_MAX_LEDGER][32]; + uint32_t replays; +} RxLatLedger; + +typedef struct { + uint8_t key[32]; + uint32_t scope; + uint64_t gen; + uint8_t output[32]; + uint8_t binding[32]; +} RxLatCacheEntry; + +typedef struct { + uint32_t n; + RxLatCacheEntry e[RX_LAT_MAX_CACHE]; + uint32_t hits, misses, refusals; +} RxLatCache; + +/* ---- building ---- */ +void rx_lat_init(RxLattice *L); +int rx_lat_add_node(RxLattice *L, const RxLatNode *n); /* index or RX_LAT_E_* */ +int rx_lat_add_method(RxLattice *L, const RxLatMethod *m); +int rx_lat_add_real(RxLattice *L, const RxLatReal *r); +int rx_lat_support(RxLattice *L, uint32_t method, uint32_t real); + +/* Acyclic, dependencies in range and of a consumable type, arity per op, + * one shape per weight id, no flow into a less private scope, effects are + * sinks holding the EFFECT right. Sets L->topo. */ +int rx_lat_validate(RxLattice *L); + +/* Validate, then enumerate the sparse candidate set. Returns its size. */ +int rx_lat_build(RxLattice *L); + +/* Pin the cost-model evidence the lattice will admit against. */ +void rx_lat_pin(RxLattice *L, const RxCostModel *m); + +/* Admission and estimates for every candidate. Returns the number of nodes + * left with no admitted candidate. */ +int rx_lat_admit(RxLattice *L, const RxCostModel *m, const RxCmPolicy *p); + +/* ---- one run ---- */ +int rx_lat_run_init(const RxLattice *L, RxLatRun *r, uint32_t policy); +/* 1: out is dispatched (node RUNNING, lane held); 0: nothing may start now. */ +int rx_lat_pick(const RxLattice *L, RxLatRun *r, RxLatPick *out); +/* Verify-before-commit. RX_LAT_OK commits the node; otherwise the node fails + * and its dependents are refused. */ +int rx_lat_finish(const RxLattice *L, RxLatRun *r, const RxLatReceipt *rc); +/* 1 when nothing is pending or running. */ +int rx_lat_settled(const RxLattice *L, const RxLatRun *r); + +void rx_lat_receipt_seal(const RxLattice *L, RxLatReceipt *rc); + +/* Exactly-once effect: requires the node to be an EFFECT obligation with the + * EFFECT right, RUNNING, every dependency DONE, and a transaction id never + * committed before. value: SHA-256 of the dependency outputs, in dep order. */ +int rx_lat_effect_commit(RxLatLedger *g, const RxLattice *L, const RxLatRun *r, uint32_t node, + uint8_t value[32]); + +/* Pure-node output reuse. The key covers the node's semantics and its + * dependency outputs, not the method or realization. A hit requires the same + * scope, the same generation and an intact binding; anything else is refused. */ +int rx_lat_cache_key(const RxLattice *L, const RxLatRun *r, uint32_t node, uint8_t key[32]); +int rx_lat_cache_put(RxLatCache *c, const uint8_t key[32], uint32_t scope, uint64_t gen, + const uint8_t output[32]); +/* 0 hit (output filled), 1 miss, RX_LAT_E_REFUSED for an illegitimate entry. */ +int rx_lat_cache_get(RxLatCache *c, const uint8_t key[32], uint32_t scope, uint64_t gen, + uint8_t output[32]); + +/* Longest predicted path from any source to any sink (lower bound on the + * makespan with unlimited lanes), using each node's best admitted estimate. */ +uint64_t rx_lat_critical_path_ps(const RxLattice *L); + +const char *rx_lat_reason_name(uint32_t reason); +const char *rx_lat_policy_name(uint32_t policy); + +#endif /* RX_LATTICE_H */ diff --git a/tests/.crumb b/tests/.crumb index b2f41a3f8..db92a8da7 100644 --- a/tests/.crumb +++ b/tests/.crumb @@ -110,206 +110,206 @@ "children": [ { "digest": "2c1deaaa1bae6257789749edde641df61413250fe91ce6aa61a99dfb3b271306", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "algebra", "purpose": "Directory tests/algebra. Purpose not yet described by a human." }, { "digest": "63f086b68bb9ffc032cbd74931309ab34df3c8c341b57935e5db43d8bf475800", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "ans", "purpose": "Tests 1-10 for ANS (docs/ans/INERTIAL_ALIGNMENT_SPEC_V1.md): freeze/digest, hostile reference mu" }, { "digest": "45c5f4fd157769cf0dcc4b8acbd76b0953580bc328714bab8e1d65e9264a5664", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "brownian", "purpose": "Directory tests/brownian. Purpose not yet described by a human." }, { "digest": "1cd97575b2b10d9313ca0df4997391612056754778bb2851bf052c146144f15e", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "canonicalization", "purpose": "Directory tests/canonicalization. Purpose not yet described by a human." }, { "digest": "646c083a94914e5726634c33cc1e694349cfcd4f43817864ffec83e506a52d4e", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "compiler", "purpose": "Directory tests/compiler. Purpose not yet described by a human." }, { "digest": "9e372cfc9747befacdcf31bc8643356aaff861e81ee6640b4866170be67b5e6c", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "constraints", "purpose": "Directory tests/constraints. Purpose not yet described by a human." }, { "digest": "87a3efa5cd0aefa7821046f9a31594bfa0a109e95005a1e12f40c99a1bc6cd3d", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "crumbline", "purpose": "Directory tests/crumbline. Purpose not yet described by a human." }, { "digest": "10aa3424eded7768fee0bae0eedd062b9deac3279d2e5721439fd024493bc42b", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "dual_experiment", "purpose": "Tests for the DUAL applicability experiment: determinism (same seed, byte-identical results dige" }, { "digest": "758dde696afa847d9347fad76b0c4935e968f8af4551f0f9810ec2a37ad9847a", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "effect", "purpose": "Directory tests/effect. Purpose not yet described by a human." }, { "digest": "23e559d13ff664995baca31dc157a8cf5a860615f3ecb07a9a40fe4626b57ae3", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "estimation", "purpose": "Directory tests/estimation. Purpose not yet described by a human." }, { "digest": "b7b6391caf653c76707a21f9969126abcbc730d1243259219225fdb9bc0084e0", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "fabric", "purpose": "Directory tests/fabric. Purpose not yet described by a human." }, { "digest": "defc52a44ea0f65ba5de3468de786148ba58d292ffa217fb95629a28a569a977", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "identity", "purpose": "Directory tests/identity. Purpose not yet described by a human." }, { "digest": "6ba43d622b712a4215767cab29425016f29cce4b28eb9e4961fc67fd20e9d765", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "language", "purpose": "Directory tests/language. Purpose not yet described by a human." }, { "digest": "54f43a3416595ff5d4e64bd12afe013359e706651c39c78e2896a5799638d263", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "malformed", "purpose": "Directory tests/malformed. Purpose not yet described by a human." }, { "digest": "a03a4dd9a72f7b449342e562f6d9df1a3348fb03fb640b662956caf163dcd7e9", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "numeric_native", "purpose": "Simulated M16 driver for numeric resource-lifecycle tests. No device execution or numeric parity" }, { "digest": "d958af5413726ddc48a47721800d9766939129f480b26492332ba094577837cb", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "osh", "purpose": "osh lexer host driver, vendored aien-protocols PR#16 draft vectors (5ae5292)" }, { "digest": "12b31dc2cd4f6a6c27983db11b4140c597025c9124da5ddcbe6a9230ab4f8410", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "path", "purpose": "Directory tests/path. Purpose not yet described by a human." }, { "digest": "51f14936b27d3d21379cf05ea1b6b532cf3175a40844dac85f023a61e7a0912c", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "physics0", "purpose": "PD-0 gates: world self-test, oracle calibration, learner L0/L1/NULL, mutations, OSC differential" }, { "digest": "c8524317c751fa8f464cabc31b760cef262abba822f1843482a02e239daa81b3", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "polyglot", "purpose": "Directory tests/polyglot. Purpose not yet described by a human." }, { "digest": "6569e2cd6dbf6f792edb5d83bf0715df9dece42ca7bd9b3a291535de0492d3aa", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "program", "purpose": "Directory tests/program. Purpose not yet described by a human." }, { "digest": "c549b5b82fd026d664b35e4b1a5d7fd2aaafa13b60b541cfbba87d7522517b1d", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "r16_inventory", "purpose": "Directory tests/r16_inventory. Purpose not yet described by a human." }, { "digest": "855bd23e4c526fb49eeb430c4ea5967d3f2f9da3a5b3aa3ec9dae5b814b11a21", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "r16_negative", "purpose": "Directory tests/r16_negative. Purpose not yet described by a human." }, { "digest": "f21be04969cbf51de17118c4f81b103f712db56fd517a1f12dd0d3ac4ff3afe4", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "r16_surface", "purpose": "Directory tests/r16_surface. Purpose not yet described by a human." }, { "digest": "b0ba3448394d5f27958c38b7233ae389e5e437e327f0859749a774fb252d6b02", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "realize", "purpose": "Directory tests/realize. Purpose not yet described by a human." }, { "digest": "157018d66de5bfd149015654cf0bda1346a9cfcefe5fd552c1d54859cd9ece81", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "relations", "purpose": "Directory tests/relations. Purpose not yet described by a human." }, { "digest": "e5f68362c083510e34c41f65de3ba2d553eac8a52fbc3e4cc1f69314eac9cc32", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "reservoir", "purpose": "Reservoir tests (omega#440): Gate 1 simulator and readout checks against an independent binary64" }, { - "digest": "39bb8b5bc5bb91af7db77cc15bcd326cdb083060f27077e9f60670cad9086a11", - "last_commit": "843424cea", + "digest": "73b1a0329063c96071cbdd66f156dcf360f285335617cf1d64f02745825ea301", + "last_commit": "d044d9af", "name": "runtime", "purpose": "Directory tests/runtime. Purpose not yet described by a human." }, { "digest": "467652c09505ecbd82d96a5e0e7ea24b19d9216cfd1dad7c60fa8258e47594c0", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "searchtrace", "purpose": "Directory tests/searchtrace. Purpose not yet described by a human." }, { "digest": "e69616ad471704066a86bf2796776301179eada5c0aa36a7166b9aca788619f4", - "last_commit": "ed8de5736", + "last_commit": "ed8de573", "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": "d863780eb", + "last_commit": "d863780e", "name": "train", "purpose": "Directory tests/train. Purpose not yet described by a human." }, { "digest": "7679bd1c49c7e89d8c26ccf8dfb91478f4e6e2f584745806fa8d26061dbdbb9c", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "turing", "purpose": "Directory tests/turing. Purpose not yet described by a human." }, { "digest": "003966bc697c6f85cfffbae00fae992a634bc386f4a44340daaa8ddcb5618ef6", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "visor", "purpose": "Directory tests/visor. Purpose not yet described by a human." } ], - "children_root": "sha256:21fc25ed991f4066326a9270e13961ed8a45edc12064ce9e7846ab6782d5b72b", - "compiled_at": "2026-10-24T23:55:01Z", + "children_root": "sha256:c5494c5c01c5d2451900c069d65d509ef62f543c5bb3b4b78558554dd4f2a8ba", + "compiled_at": "2026-10-25T02:29:06Z", "compiler": "crumb-compile/1", - "digest": "sha256:cc52017187608f4ee94d1480602ff214c614604c515bbabbde448c2733e555ab", + "digest": "sha256:fc0957e92759897f586607f055b8b6c3420c7b5168f504ba82246aa8d4a2bced", "evidence_files": 16, "evidence_root": "sha256:96d08097822ad95f4e562c19e0ba5df5a035774c48632f1832912c40a27bd27a", - "files": 1292, - "generated_at_commit": "4cd9ad9845612038a49c15940811ce24c38c061d", - "source_tree": "sha256:aa47c530df8df05215fc7536fe21612bfa187896b8f02e9219b525dc477ea8c3" + "files": 1295, + "generated_at_commit": "d044d9af667dd04d28d9dbf64d2e6ca4caad67ce", + "source_tree": "sha256:62831279ad3d1980c420c2c93cad90c8f3f89a93b7f31170bc04ede721c156b8" }, "owner_lane": "", "provenance": { diff --git a/tests/runtime/.crumb b/tests/runtime/.crumb index 422a08a6e..652d06c71 100644 --- a/tests/runtime/.crumb +++ b/tests/runtime/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-24T23:00:43Z", + "compiled_at": "2026-10-25T02:29:06Z", "compiler": "crumb-compile/1", - "digest": "sha256:39bb8b5bc5bb91af7db77cc15bcd326cdb083060f27077e9f60670cad9086a11", + "digest": "sha256:73b1a0329063c96071cbdd66f156dcf360f285335617cf1d64f02745825ea301", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 70, - "generated_at_commit": "6b8927fdbb8e15c2668a29a6beca577c8cf71651", - "source_tree": "sha256:53b4e9a9bb5a07b946577ebe5d09fb4bda158de5b1bb699d613049a710f10de5" + "files": 73, + "generated_at_commit": "d044d9af667dd04d28d9dbf64d2e6ca4caad67ce", + "source_tree": "sha256:52ea9d6ed1584cafbd2fb341abb6bd9ee15e790e2a91fe2bbb7c96614a7b6cd0" }, "owner_lane": "", "provenance": { diff --git a/tests/runtime/rx_lattice_bench.c b/tests/runtime/rx_lattice_bench.c new file mode 100644 index 000000000..12260d0bd --- /dev/null +++ b/tests/runtime/rx_lattice_bench.c @@ -0,0 +1,505 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2026 Drake Stapleton (AIEN / aien-dev) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * rx_lattice host benchmark (omega-mv #698). AArch64 (GB10 host CPUs); no GPU. + * + * Same objective graph, same admitted candidate set, same lanes for every + * policy: serial, lcf (lowest predicted cost first, dependency-driven) and + * q3d. Lanes are real threads pinned to two Cortex-X925 and two Cortex-A725 + * cores, chosen as the quietest of their class at start. The simulated + * accelerator realization is declared but not FORGE-verified here, so the + * lattice refuses it; no GPU work is done. + * + * The cost model is rx_costmodel, fitted from this program's own timed runs + * of every verified arm on each core class over a training grid of shapes + * that differs from the graph's shapes. The benchmark runs are separate from + * the calibration runs. + * + * Reported per policy, over rounds in rotating policy order after one + * discarded warm-up round: objective wall time (clock_gettime MONOTONIC, + * includes every pick, dispatch, verification and receipt), selection + * overhead (time inside rx_lat_pick / rx_lat_finish), summed lane busy time, + * realized critical path (longest dependency chain of measured node times), + * per-lane-class utilization, and output equality with serial. Nothing + * here measures energy, memory traffic or GPU utilization; none is claimed. + */ +#include "rx_lattice_fixture.h" + +#include +#include +#include +#include + +static uint64_t now_ns(void) { + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; +} + +/* ---- cores ---- */ + +static int g_cpus[RX_CM_CORES][64], g_ncpu[RX_CM_CORES]; + +static void discover(void) { + long n = sysconf(_SC_NPROCESSORS_CONF); + for (long c = 0; c < n && c < 64; c++) { + char path[128]; + snprintf(path, sizeof path, "/sys/devices/system/cpu/cpu%ld/regs/identification/midr_el1", c); + FILE *fp = fopen(path, "r"); + unsigned long long midr = 0; + int ok = fp && fscanf(fp, "%llx", &midr) == 1; + if (fp) fclose(fp); + uint32_t part = ok ? (uint32_t)((midr >> 4) & 0xfffu) : 0; + uint32_t k = part == 0xd85u ? RX_CM_CORE_X925 : part == 0xd87u ? RX_CM_CORE_A725 : RX_CM_CORE_OTHER; + g_cpus[k][g_ncpu[k]++] = (int)c; + } +} + +static void cpu_busy(double *busy) { + unsigned long long a[2][64][2]; + memset(a, 0, sizeof a); + for (int s = 0; s < 2; s++) { + FILE *fp = fopen("/proc/stat", "r"); + char line[512]; + while (fp && fgets(line, sizeof line, fp)) { + int c; + unsigned long long u, ni, sy, id, io, irq, sirq, st; + if (sscanf(line, "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu", &c, &u, &ni, &sy, &id, &io, &irq, + &sirq, &st) == 9 && c >= 0 && c < 64) { + a[s][c][0] = u + ni + sy + irq + sirq + st; + a[s][c][1] = a[s][c][0] + id + io; + } + } + if (fp) fclose(fp); + if (s == 0) nanosleep(&(struct timespec){ 0, 200000000 }, NULL); + } + for (int c = 0; c < 64; c++) { + unsigned long long tot = a[1][c][1] - a[0][c][1]; + busy[c] = tot ? (double)(a[1][c][0] - a[0][c][0]) / (double)tot : 1.0; + } +} + +/* The two quietest cpus of class k. */ +static void quietest2(uint32_t k, const double *busy, int out[2]) { + out[0] = out[1] = -1; + for (int i = 0; i < g_ncpu[k]; i++) { + int c = g_cpus[k][i]; + if (out[0] < 0 || busy[c] < busy[out[0]]) { + out[1] = out[0]; + out[0] = c; + } else if (out[1] < 0 || busy[c] < busy[out[1]]) { + out[1] = c; + } + } +} + +static int pin_self(int cpu) { + cpu_set_t s; + CPU_ZERO(&s); + CPU_SET(cpu, &s); + return pthread_setaffinity_np(pthread_self(), sizeof s, &s); +} + +static void loadavg(char *out, size_t n) { + FILE *fp = fopen("/proc/loadavg", "r"); + out[0] = 0; + if (fp) { + if (!fgets(out, (int)n, fp)) out[0] = 0; + fclose(fp); + } + size_t l = strlen(out); + if (l && out[l - 1] == '\n') out[l - 1] = 0; +} + +/* ---- calibration: rx_costmodel from our own timed runs ---- */ + +static RxCostModel g_cm; +static uint32_t g_obs; + +static uint64_t time_ps_per_call(uint32_t arm, const uint64_t *A, const uint64_t *x, uint64_t *y, uint32_t M, + uint32_t N) { + uint64_t calls = 400000ull / ((uint64_t)M * N + 64u); + if (calls < 3) calls = 3; + uint64_t best = UINT64_MAX; + for (int chunk = 0; chunk < 3; chunk++) { + uint64_t t0 = now_ns(); + for (uint64_t c = 0; c < calls; c++) fx_fn[arm](A, x, y, M, N); + uint64_t ps = (now_ns() - t0) * 1000u / calls; + if (ps < best) best = ps; + } + return best ? best : 1; +} + +static int calibrate(const int cpu[RX_CM_CORES][2]) { + static const uint32_t TM[] = { 16, 64, 128, 256, 512 }; + static const uint32_t TN[] = { 16, 33, 64, 100, 200, 257, 300, 400 }; + static const uint64_t CACHE[RX_CM_CORES] = { 2u << 20, 512u << 10, 1u << 20 }; + static uint64_t A[512 * 400], x[400], y[512], yr[512]; + uint64_t s = 0xCA1Bu; + for (size_t i = 0; i < sizeof A / sizeof A[0]; i++) A[i] = fx_xs(&s); + for (size_t i = 0; i < 400; i++) x[i] = fx_xs(&s); + rx_cm_init(&g_cm, N_ARMS); + for (uint32_t core = RX_CM_CORE_X925; core <= RX_CM_CORE_A725; core++) { + if (pin_self(cpu[core][0]) != 0) return -1; + for (unsigned a = 0; a < sizeof TM / sizeof TM[0]; a++) + for (unsigned b = 0; b < sizeof TN / sizeof TN[0]; b++) + for (uint32_t r = 0; r < 3; r++) { /* rotate arm order per shape */ + uint32_t arm = (r + a + b) % 3u; /* ARM_REF, ARM_SCALAR, ARM_QUAD4 */ + if (!fx_verified[arm]) continue; + RxCmFeatures f; + memset(&f, 0, sizeof f); + f.op = RX_CM_OP_MATVEC; + f.M = TM[a]; + f.N = TN[b]; + f.state_bytes = ((uint64_t)TM[a] * TN[b] + TM[a] + TN[b]) * 8u; + f.cache_bytes = CACHE[core]; + f.core = core; + f.eligible = 0x7; + f.need_evidence = 1; + uint64_t ps = time_ps_per_call(arm, A, x, y, TM[a], TN[b]); + omega_matvec_reference(A, x, yr, TM[a], TN[b]); + RxCmObservation o = { ps, memcmp(y, yr, (size_t)TM[a] * 8u) != 0 }; + rx_cm_observe(&g_cm, &f, arm, &o); + g_obs++; + } + } + return 0; +} + +/* ---- lanes ---- */ + +#define N_LANES 4u +typedef struct { + pthread_t th; + uint32_t real, lane; + int cpu; + int has_job, quit; + uint32_t node, cand; +} Lane; + +static Lane g_lane[N_LANES]; +static RxLattice g_L; +static pthread_mutex_t g_mu = PTHREAD_MUTEX_INITIALIZER; +static pthread_cond_t g_cv_job = PTHREAD_COND_INITIALIZER, g_cv_done = PTHREAD_COND_INITIALIZER; + +typedef struct { + RxLatReceipt rc; + uint64_t t_start, t_end; +} Done; +static Done g_doneq[RX_LAT_MAX_NODES]; +static uint32_t g_done_head, g_done_tail; + +static void *lane_main(void *arg) { + Lane *ln = (Lane *)arg; + pin_self(ln->cpu); + pthread_mutex_lock(&g_mu); + for (;;) { + while (!ln->has_job && !ln->quit) pthread_cond_wait(&g_cv_job, &g_mu); + if (ln->quit) break; + uint32_t i = ln->node, c = ln->cand; + pthread_mutex_unlock(&g_mu); + Done d; + memset(&d, 0, sizeof d); + d.t_start = now_ns(); + d.rc.node = (uint16_t)i; + d.rc.cand = (uint16_t)c; + d.rc.verdict = fx_execute(&g_L, i, c); + fx_vec_digest(i, d.rc.output); + d.t_end = now_ns(); + d.rc.elapsed_ps = (d.t_end - d.t_start) * 1000u; + rx_lat_receipt_seal(&g_L, &d.rc); + pthread_mutex_lock(&g_mu); + ln->has_job = 0; + g_doneq[g_done_tail++ % RX_LAT_MAX_NODES] = d; + pthread_cond_broadcast(&g_cv_done); + } + pthread_mutex_unlock(&g_mu); + return NULL; +} + +static Lane *lane_for(uint32_t real, uint32_t lane) { + for (uint32_t t = 0; t < N_LANES; t++) + if (g_lane[t].real == real && g_lane[t].lane == lane) return &g_lane[t]; + return NULL; +} + +/* ---- one measured run ---- */ + +typedef struct { + uint64_t wall_ns, select_ns, busy_ns, busy_real_ns[N_REALS], crit_ns; + uint32_t done, failed, refused, switches, arm_runs[N_ARMS], dispatched[N_REALS]; + uint8_t out[FX_NODES][32]; + uint8_t effect[32]; + int effect_rc; +} Meas; + +static int run_measured(uint32_t policy, Meas *m) { + memset(m, 0, sizeof *m); + RxLatRun r; + RxLatLedger g; + memset(&r, 0, sizeof r); + memset(&g, 0, sizeof g); + uint64_t node_ns[FX_NODES]; + memset(node_ns, 0, sizeof node_ns); + g_done_head = g_done_tail = 0; + uint64_t t0 = now_ns(); + r.now_ps = 0; + if (rx_lat_run_init(&g_L, &r, policy) != RX_LAT_OK) return -1; + pthread_mutex_lock(&g_mu); + for (;;) { + RxLatPick p; + r.now_ps = (now_ns() - t0) * 1000u; + for (;;) { + uint64_t ts = now_ns(); + int got = rx_lat_pick(&g_L, &r, &p); + m->select_ns += now_ns() - ts; + if (!got) break; + if (g_L.node[p.node].op == RX_LAT_OP_EFFECT) { + /* The single writer commits the effect itself, after verified inputs. */ + Done d; + memset(&d, 0, sizeof d); + d.t_start = now_ns(); + d.rc.node = (uint16_t)p.node; + d.rc.cand = (uint16_t)p.cand; + m->effect_rc = rx_lat_effect_commit(&g, &g_L, &r, p.node, d.rc.output); + d.rc.verdict = m->effect_rc == RX_LAT_OK ? 0u : 1u; + d.t_end = now_ns(); + d.rc.elapsed_ps = (d.t_end - d.t_start) * 1000u; + rx_lat_receipt_seal(&g_L, &d.rc); + g_doneq[g_done_tail++ % RX_LAT_MAX_NODES] = d; + continue; + } + Lane *ln = lane_for(p.real, p.lane); + if (!ln || ln->has_job) { + pthread_mutex_unlock(&g_mu); + return -2; /* the lattice handed out a busy or unknown lane */ + } + if (g_L.node[p.node].op == RX_LAT_OP_MATVEC) m->arm_runs[g_L.method[g_L.cand[p.cand].method].arm]++; + ln->node = p.node; + ln->cand = p.cand; + ln->has_job = 1; + pthread_cond_broadcast(&g_cv_job); + } + if (g_done_head == g_done_tail) { + if (r.in_flight == 0) break; + pthread_cond_wait(&g_cv_done, &g_mu); + continue; + } + Done d = g_doneq[g_done_head++ % RX_LAT_MAX_NODES]; + r.now_ps = (now_ns() - t0) * 1000u; + uint32_t k = g_L.cand[d.rc.cand].real; + node_ns[d.rc.node] = d.t_end - d.t_start; + m->busy_ns += node_ns[d.rc.node]; + m->busy_real_ns[k] += node_ns[d.rc.node]; + uint64_t ts = now_ns(); + rx_lat_finish(&g_L, &r, &d.rc); + m->select_ns += now_ns() - ts; + } + pthread_mutex_unlock(&g_mu); + m->wall_ns = now_ns() - t0; + /* Realized critical path: longest dependency chain of measured node times. */ + uint64_t fin[FX_NODES]; + for (uint32_t t = 0; t < g_L.n_nodes; t++) { + uint32_t i = g_L.topo[t]; + uint64_t st = 0; + for (uint32_t q = 0; q < g_L.node[i].n_deps; q++) + if (fin[g_L.node[i].dep[q]] > st) st = fin[g_L.node[i].dep[q]]; + fin[i] = st + node_ns[i]; + if (fin[i] > m->crit_ns) m->crit_ns = fin[i]; + } + m->done = r.done; + m->failed = r.failed; + m->refused = r.refused; + m->switches = r.weight_switches; + for (uint32_t k = 0; k < N_REALS; k++) m->dispatched[k] = r.dispatched[k]; + memcpy(m->out, r.out, sizeof m->out); + if (g.n == 1) memcpy(m->effect, g.value[0], 32); + return rx_lat_settled(&g_L, &r) ? 0 : -3; +} + +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, uint32_t n) { + qsort(v, n, sizeof *v, cmp_u64); + return n % 2 ? v[n / 2] : (v[n / 2 - 1] + v[n / 2]) / 2u; +} + +#define ROUNDS 15u + +int main(int argc, char **argv) { + uint32_t rounds = ROUNDS; + if (argc > 1) rounds = (uint32_t)strtoul(argv[1], NULL, 10); + if (rounds < 5) rounds = 5; + printf("rx_lattice host benchmark (omega-mv #698). Host CPUs only, no GPU.\n"); + discover(); + if (g_ncpu[RX_CM_CORE_X925] < 2 || g_ncpu[RX_CM_CORE_A725] < 2) { + printf("NOT_RUN: needs two Cortex-X925 and two Cortex-A725 cpus (found %d, %d)\n", + g_ncpu[RX_CM_CORE_X925], g_ncpu[RX_CM_CORE_A725]); + return 2; + } + if (fx_setup_arms() != 0) { + printf("FAIL: arm setup\n"); + return 1; + } + for (uint32_t a = 0; a < N_ARMS; a++) printf(" arm %-13s %s\n", ARM_NAME[a], fx_verified[a] ? "verified" : "refused"); + double busy[64]; + cpu_busy(busy); + int cpu[RX_CM_CORES][2]; + quietest2(RX_CM_CORE_X925, busy, cpu[RX_CM_CORE_X925]); + quietest2(RX_CM_CORE_A725, busy, cpu[RX_CM_CORE_A725]); + char la[128]; + loadavg(la, sizeof la); + printf(" lanes: x925 cpu%d (busy %.2f) cpu%d (busy %.2f); a725 cpu%d (busy %.2f) cpu%d (busy %.2f); loadavg %s\n", + cpu[0][0], busy[cpu[0][0]], cpu[0][1], busy[cpu[0][1]], cpu[1][0], busy[cpu[1][0]], cpu[1][1], + busy[cpu[1][1]], la); + + uint64_t tc = now_ns(); + if (calibrate((const int (*)[2])cpu) != 0) { + printf("FAIL: calibration pinning\n"); + return 1; + } + printf(" calibration: %u timed observations in %.2f s (training grid, not the graph's shapes)\n", g_obs, + (double)(now_ns() - tc) / 1e9); + + fx_build(&g_L, 0); + if (rx_lat_build(&g_L) < 0) { + printf("FAIL: build\n"); + return 1; + } + rx_lat_pin(&g_L, &g_cm); + RxCmPolicy pol; + rx_cm_default_policy(&pol); + int empty = rx_lat_admit(&g_L, &g_cm, &pol); + printf(" lattice: %u candidates, %d nodes without a candidate;", g_L.n_cands, empty); + for (uint32_t q = 0; q < RX_LAT_R_COUNT; q++) + if (g_L.reasons[q]) printf(" %s %u", rx_lat_reason_name(q), g_L.reasons[q]); + printf("\n predicted critical path (best admitted estimate per node; caps for unmodelled ops): %.1f us\n", + (double)rx_lat_critical_path_ps(&g_L) / 1e6); + if (empty) { + printf("FAIL: objective not admissible\n"); + return 1; + } + + for (uint32_t k = 0, t = 0; k < 2; k++) + for (uint32_t l = 0; l < 2; l++, t++) { + g_lane[t].real = k; + g_lane[t].lane = l; + g_lane[t].cpu = cpu[k][l]; + pthread_create(&g_lane[t].th, NULL, lane_main, &g_lane[t]); + } + /* The dispatcher (single writer) runs on the quietest cpu that is not a lane. */ + int disp = -1; + for (int c = 0; c < 64; c++) { + int lane_cpu = c == cpu[0][0] || c == cpu[0][1] || c == cpu[1][0] || c == cpu[1][1]; + int known = 0; + for (uint32_t k = 0; k < RX_CM_CORES; k++) + for (int i = 0; i < g_ncpu[k]; i++) known |= g_cpus[k][i] == c; + if (known && !lane_cpu && (disp < 0 || busy[c] < busy[disp])) disp = c; + } + if (disp >= 0) pin_self(disp); + printf(" dispatcher cpu%d\n", disp); + + static uint64_t wall[RX_LAT_POLICY_COUNT][64], sel[RX_LAT_POLICY_COUNT][64], bsy[RX_LAT_POLICY_COUNT][64], + crit[RX_LAT_POLICY_COUNT][64], util[RX_LAT_POLICY_COUNT][2][64]; + Meas last[RX_LAT_POLICY_COUNT], base; + int equal_all = 1, fails = 0; + memset(&base, 0, sizeof base); + if (run_measured(RX_LAT_POLICY_SERIAL, &base) != 0 || base.done != FX_NODES) fails++; + { + uint8_t oracle[FX_NODES][32]; + int ok = 1; + fx_oracle(&g_L, oracle); + for (uint32_t i = 0; i < FX_NODES; i++) + if (i != FX_EFFECT && memcmp(oracle[i], base.out[i], 32)) ok = 0; + printf(" serial outputs equal the independent reference-only oracle: %s\n", ok ? "yes" : "NO"); + if (!ok) fails++; + } + for (uint32_t rd = 0; rd <= rounds; rd++) /* round 0 is a discarded warm-up */ + for (uint32_t q = 0; q < RX_LAT_POLICY_COUNT; q++) { + uint32_t p = (q + rd) % RX_LAT_POLICY_COUNT; + Meas m; + if (run_measured(p, &m) != 0 || m.done != FX_NODES || m.effect_rc != RX_LAT_OK) fails++; + if (memcmp(m.out, base.out, sizeof m.out) || memcmp(m.effect, base.effect, 32)) equal_all = 0; + if (rd == 0) continue; + uint32_t i = rd - 1; + wall[p][i] = m.wall_ns; + sel[p][i] = m.select_ns; + bsy[p][i] = m.busy_ns; + crit[p][i] = m.crit_ns; + for (uint32_t k = 0; k < 2; k++) + util[p][k][i] = m.busy_real_ns[k] * 1000u / (m.wall_ns * g_L.real[k].lanes); /* per mille */ + last[p] = m; + } + pthread_mutex_lock(&g_mu); + for (uint32_t t = 0; t < N_LANES; t++) g_lane[t].quit = 1; + pthread_cond_broadcast(&g_cv_job); + pthread_mutex_unlock(&g_mu); + for (uint32_t t = 0; t < N_LANES; t++) pthread_join(g_lane[t].th, NULL); + + loadavg(la, sizeof la); + printf(" MEASURED, %u rounds after 1 warm-up, rotating policy order; loadavg after: %s\n", rounds, la); + printf(" policy wall_med_us wall_min wall_max select_us busy_sum_us busy/wall crit_path_us util_x925 util_a725 arms(ref/scalar/quad4) dispatched(x925/a725/gpu.sim) weight_switches\n"); + for (uint32_t p = 0; p < RX_LAT_POLICY_COUNT; p++) { + uint64_t w[64], s[64], b[64], c[64], u0[64], u1[64]; + memcpy(w, wall[p], sizeof w); + memcpy(s, sel[p], sizeof s); + memcpy(b, bsy[p], sizeof b); + memcpy(c, crit[p], sizeof c); + memcpy(u0, util[p][0], sizeof u0); + memcpy(u1, util[p][1], sizeof u1); + uint64_t wmin = UINT64_MAX, wmax = 0; + for (uint32_t i = 0; i < rounds; i++) { + if (w[i] < wmin) wmin = w[i]; + if (w[i] > wmax) wmax = w[i]; + } + uint64_t wm = median(w, rounds), bm = median(b, rounds); + printf(" %-6s %11.1f %8.1f %8.1f %9.1f %11.1f %9.2f %12.1f %9.3f %9.3f %u/%u/%u %u/%u/%u %u\n", + rx_lat_policy_name(p), (double)wm / 1e3, (double)wmin / 1e3, (double)wmax / 1e3, + (double)median(s, rounds) / 1e3, (double)bm / 1e3, (double)bm / (double)wm, + (double)median(c, rounds) / 1e3, (double)median(u0, rounds) / 1e3, (double)median(u1, rounds) / 1e3, + last[p].arm_runs[ARM_REF], last[p].arm_runs[ARM_SCALAR], last[p].arm_runs[ARM_QUAD4], + last[p].dispatched[0], last[p].dispatched[1], last[p].dispatched[2], last[p].switches); + } + /* Paired per-round ratios against serial, so drift between rounds cancels. */ + for (uint32_t p = 1; p < RX_LAT_POLICY_COUNT; p++) { + uint64_t ratio[64]; + uint32_t wins = 0; + for (uint32_t i = 0; i < rounds; i++) { + ratio[i] = wall[p][i] * 1000u / wall[RX_LAT_POLICY_SERIAL][i]; + wins += wall[p][i] < wall[RX_LAT_POLICY_SERIAL][i]; + } + printf(" %s/serial wall ratio: median %.3f; faster than serial in %u of %u paired rounds\n", + rx_lat_policy_name(p), (double)median(ratio, rounds) / 1e3, wins, rounds); + } + { + uint64_t ratio[64]; + uint32_t wins = 0; + for (uint32_t i = 0; i < rounds; i++) { + ratio[i] = wall[RX_LAT_POLICY_Q3D][i] * 1000u / wall[RX_LAT_POLICY_LCF][i]; + wins += wall[RX_LAT_POLICY_Q3D][i] < wall[RX_LAT_POLICY_LCF][i]; + } + printf(" q3d/lcf wall ratio: median %.3f; q3d faster in %u of %u paired rounds\n", + (double)median(ratio, rounds) / 1e3, wins, rounds); + } + printf(" outputs and effect value identical to serial in every run: %s; failed runs: %d\n", + equal_all ? "yes" : "NO", fails); + printf(" not measured: energy, memory bytes, GPU (gpu.sim refused: no FORGE lease).\n"); + return equal_all && !fails ? 0 : 1; +} diff --git a/tests/runtime/rx_lattice_fixture.h b/tests/runtime/rx_lattice_fixture.h new file mode 100644 index 000000000..22527da5a --- /dev/null +++ b/tests/runtime/rx_lattice_fixture.h @@ -0,0 +1,437 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2026 Drake Stapleton (AIEN / aien-dev) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * Shared by rx_lattice_unit.c and rx_lattice_bench.c: the fixed synthetic + * objective graph, the methods (Omega's real integer matvec arms) and the + * runner that executes a dispatched coordinate and verifies its output + * independently before sealing a receipt. + * + * Graph (17 nodes, scopes: 0 public, 1 tenant A, 2 tenant B): + * + * 0 SRC.p -> 1 MV.p w0 -+-> 2 MV.A w1 -+-> 6 JOIN.A -> 7 MV.A w1 -> 10 MV.A w2 -+ + * |-> 3 MV.A w2 -+ +-> 14 JOIN.A -> 15 EFFECT.A + * |-> 12 MV.p w5 ------------------------------------------+ + * +-> 4 MV.B w3 -+-> 8 JOIN.B -> 9 MV.B w4 -> 11 MV.B w3 -+-> 16 JOIN.B + * 0 --------------------------> 5 MV.B w3 -+ | + * 0 --------------------------> 13 MV.p w5 --------------------------------------+ + * + * Weights are shared: w1 by 2 and 7, w2 by 3 and 10, w3 by 4, 5 and 11, w5 by + * 12 and 13. Several shapes have N % 4 != 0. + * + * Methods: arm 0 the compiled semantic reference (incumbent), arm 1 the + * M12 scalar emitter, arm 2 quad4 (R10), arm 3 a planted wrong arm that + * drops the last N % 4 columns, arm 4 a declared model-call method that + * needs a model seat and has no executor on this host. Each emitted arm is + * verified by differential test in a forked child before it may be admitted. + * + * Realizations: 0 Cortex-X925 lanes (2), 1 Cortex-A725 lanes (2), 2 a + * simulated single-owner accelerator lane. It holds scopes 0 and 1 only (its + * lease belongs to tenant A). It only exists as a simulation: the unit test + * declares it verified and runs its work on the host; the benchmark leaves + * it unverified, because no FORGE lease or GPU window exists for this run. + */ +#ifndef RX_LATTICE_FIXTURE_H +#define RX_LATTICE_FIXTURE_H + +#include "runtime/rx_lattice.h" +#include "omega_machine.h" +#include "omega_matvec.h" +#include "omega_matvec_quad.h" +#include "sha256.h" + +#include +#include +#include +#include +#include +#include +#include + +enum { ARM_REF = 0, ARM_SCALAR, ARM_QUAD4, ARM_PLANTED, ARM_MODEL, N_ARMS }; +static const char *const ARM_NAME[N_ARMS] = { "reference", "scalar", "quad4", "planted_wrong", + "model_call" }; +enum { M_REF = 0, M_SCALAR, M_QUAD4, M_PLANTED, M_MODEL, M_SOURCE, M_JOIN, M_EFFECT, N_METHODS }; +enum { K_X925 = 0, K_A725, K_GPUSIM, N_REALS }; +static const char *const REAL_NAME[N_REALS] = { "cpu.x925", "cpu.a725", "gpu.sim" }; + +#define FX_NODES 17u +#define FX_EFFECT 15u +#define FX_TXID 0x5EC0DE000698ull +#define FX_MAXLEN 512u +#define FX_GUARD 8u +#define FX_JOIN_CAP_PS 20000000ull /* 20 us: declared cap for unmodelled JOIN/SOURCE/EFFECT */ + +typedef void (*MvFn)(const uint64_t *, const uint64_t *, uint64_t *, uint64_t, uint64_t); + +static MvFn fx_fn[N_ARMS]; +static int fx_verified[N_ARMS]; + +typedef struct { uint32_t M, N; } FxShape; +static const FxShape FX_W[6] = { { 256, 255 }, { 384, 256 }, { 128, 384 }, { 320, 320 }, { 192, 320 }, + { 512, 129 } }; +static uint64_t *fx_A[6]; + +/* Per-node buffers: x (input), y (output + guard). Written only by the lane + * running that node; read by dependents only after the node is DONE. */ +static uint64_t fx_x[FX_NODES][FX_MAXLEN]; +static uint64_t fx_y[FX_NODES][FX_MAXLEN + FX_GUARD]; +static uint32_t fx_len[FX_NODES]; +static const uint64_t FX_CANARY = 0xC0FFEE0DDBA11AD5ull; + +static uint64_t fx_xs(uint64_t *s) { + *s ^= *s << 13; + *s ^= *s >> 7; + *s ^= *s << 17; + return *s; +} + +static void fx_arm_reference(const uint64_t *A, const uint64_t *x, uint64_t *y, uint64_t M, uint64_t N) { + omega_matvec_reference(A, x, y, (uint32_t)M, (uint32_t)N); +} + +static void fx_arm_planted(const uint64_t *A, const uint64_t *x, uint64_t *y, uint64_t M, uint64_t N) { + uint64_t n4 = N - (N % 4u); + for (uint64_t i = 0; i < M; i++) { + uint64_t s = 0; + for (uint64_t j = 0; j < n4; j++) s += A[i * N + j] * x[j]; + y[i] = s; + } +} + +static int fx_map_code(const uint8_t *code, size_t len, MvFn *out) { + void *p = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); + if (p == MAP_FAILED || len > 4096) return -1; + memcpy(p, code, len); + __builtin___clear_cache((char *)p, (char *)p + len); + if (mprotect(p, 4096, PROT_READ | PROT_EXEC) != 0) return -1; + union { void *v; MvFn f; } u = { p }; + *out = u.f; + return 0; +} + +/* Differential against the semantic reference, odd and even shapes, guard + * words and input immutability. 0 pass, 1 wrong, 2 wrote outside y. */ +static int fx_differential(MvFn fn) { + static const uint32_t VM[] = { 1, 3, 4, 7, 16, 64 }; + static const uint32_t VN[] = { 1, 2, 3, 4, 5, 7, 8, 15, 16, 17, 33, 129, 255, 256 }; + static uint64_t A[64 * 256], A0[64 * 256], x[256], x0[256], y[64 + FX_GUARD], yr[64]; + int wrong = 0; + for (unsigned mi = 0; mi < sizeof VM / sizeof VM[0]; mi++) + for (unsigned ni = 0; ni < sizeof VN / sizeof VN[0]; ni++) { + uint32_t M = VM[mi], N = VN[ni]; + uint64_t s = 0x9E3779B97F4A7C15ull + mi * 131u + ni; + for (uint32_t i = 0; i < M * N; i++) A[i] = fx_xs(&s); + for (uint32_t i = 0; i < N; i++) x[i] = fx_xs(&s); + memcpy(A0, A, sizeof(uint64_t) * M * N); + memcpy(x0, x, sizeof(uint64_t) * N); + for (uint32_t i = 0; i < M + FX_GUARD; i++) y[i] = FX_CANARY; + omega_matvec_reference(A, x, yr, M, N); + fn(A, x, y, M, N); + for (uint32_t i = M; i < M + FX_GUARD; i++) + if (y[i] != FX_CANARY) return 2; + if (memcmp(A, A0, sizeof(uint64_t) * M * N) || memcmp(x, x0, sizeof(uint64_t) * N)) return 2; + if (memcmp(y, yr, sizeof(uint64_t) * M)) wrong = 1; + } + return wrong; +} + +static int fx_verify_in_child(MvFn fn) { + pid_t pid = fork(); + if (pid == 0) { + alarm(20); + _exit(fx_differential(fn)); + } + int st = 0; + if (pid < 0 || waitpid(pid, &st, 0) != pid || !WIFEXITED(st)) return 3; + return WEXITSTATUS(st); +} + +/* Synthesize the emitted arms and verify every executable arm. 0 on success. */ +static int fx_setup_arms(void) { + OmegaMachineGraph mg; + MatVecSemanticSpec spec; + MatVecLivingKernel kernel; + MatVecRealization r; + if (omega_machine_build_dgx_spark(&mg) != 0) return -1; + if (omega_matvec_spec_init(&spec, "rx_lattice_matvec", 1024, 1024) != 0) return -1; + memset(&kernel, 0, sizeof kernel); + kernel.spec = spec; + kernel.machine = &mg; + fx_fn[ARM_REF] = fx_arm_reference; + fx_fn[ARM_PLANTED] = fx_arm_planted; + if (omega_matvec_synthesize(&kernel, (MatVecRealizationKind)0, &r) != 0 || + fx_map_code(r.realization.code_bytes, r.realization.code_len, &fx_fn[ARM_SCALAR]) != 0) + return -1; + if (omega_matvec_synthesize_quad4(&spec, &mg, &r) != 0 || + fx_map_code(r.realization.code_bytes, r.realization.code_len, &fx_fn[ARM_QUAD4]) != 0) + return -1; + for (uint32_t a = 0; a < ARM_MODEL; a++) fx_verified[a] = fx_verify_in_child(fx_fn[a]) == 0; + fx_verified[ARM_MODEL] = 0; /* no model-seat executor exists on this host */ + for (uint32_t w = 0; w < 6; w++) { + size_t n = (size_t)FX_W[w].M * FX_W[w].N; + fx_A[w] = (uint64_t *)malloc(n * sizeof(uint64_t)); + if (!fx_A[w]) return -1; + uint64_t s = 0xA11CE000ull + w * 7919u; + for (size_t i = 0; i < n; i++) fx_A[w][i] = fx_xs(&s); + } + return 0; +} + +static void fx_digest(const uint8_t *tag, size_t tl, const uint8_t *p, size_t n, uint8_t out[32]) { + sha256_ctx c; + sha256_init(&c); + sha256_update(&c, tag, tl); + sha256_update(&c, p, n); + sha256_final(&c, out); +} + +static void fx_vec_digest(uint32_t node, uint8_t out[32]) { + uint8_t tag[8]; + for (int i = 0; i < 4; i++) tag[i] = (uint8_t)(fx_len[node] >> (8 * i)); + tag[4] = 'v'; tag[5] = 'e'; tag[6] = 'c'; tag[7] = '1'; + fx_digest(tag, 8, (const uint8_t *)fx_y[node], (size_t)fx_len[node] * 8u, out); +} + +static RxLatNode fx_node(uint32_t op, uint32_t scope, uint32_t weight, uint32_t n_deps, int d0, int d1) { + RxLatNode a; + memset(&a, 0, sizeof a); + a.op = op; + a.kind = op == RX_LAT_OP_MATVEC ? RX_LAT_KIND_PHYSICAL + : op == RX_LAT_OP_SOURCE ? RX_LAT_KIND_CONST + : op == RX_LAT_OP_JOIN ? RX_LAT_KIND_JOIN : RX_LAT_KIND_EFFECT; + a.scope = scope; + a.rights = RX_LAT_RIGHT_COMPUTE; + a.precision = RX_LAT_PREC_EXACT_U64; + a.n_deps = n_deps; + a.dep[0] = (uint16_t)(d0 < 0 ? 0 : d0); + a.dep[1] = (uint16_t)(d1 < 0 ? 0 : d1); + a.gen = 1; + if (op == RX_LAT_OP_MATVEC) { + /* The objective's approval allows a model call for these obligations. */ + a.rights |= RX_LAT_RIGHT_MODEL; + a.weight = weight; + a.M = FX_W[weight].M; + a.N = FX_W[weight].N; + } else { + a.budget_ps = FX_JOIN_CAP_PS; + } + return a; +} + +static void fx_build_graph(RxLattice *L) { + const uint32_t MV = RX_LAT_OP_MATVEC, JN = RX_LAT_OP_JOIN; + RxLatNode n[FX_NODES]; + n[0] = fx_node(RX_LAT_OP_SOURCE, 0, 0, 0, -1, -1); + n[0].N = 256; + n[1] = fx_node(MV, 0, 0, 1, 0, -1); + n[2] = fx_node(MV, 1, 1, 1, 1, -1); + n[3] = fx_node(MV, 1, 2, 1, 1, -1); + n[4] = fx_node(MV, 2, 3, 1, 1, -1); + n[5] = fx_node(MV, 2, 3, 1, 0, -1); + n[6] = fx_node(JN, 1, 0, 2, 2, 3); + n[6].N = 384; + n[7] = fx_node(MV, 1, 1, 1, 6, -1); + n[8] = fx_node(JN, 2, 0, 2, 4, 5); + n[8].N = 320; + n[9] = fx_node(MV, 2, 4, 1, 8, -1); + n[10] = fx_node(MV, 1, 2, 1, 7, -1); + n[11] = fx_node(MV, 2, 3, 1, 9, -1); + n[12] = fx_node(MV, 0, 5, 1, 1, -1); + n[13] = fx_node(MV, 0, 5, 1, 0, -1); + n[14] = fx_node(JN, 1, 0, 2, 10, 12); + n[14].N = 128; + n[15] = fx_node(RX_LAT_OP_EFFECT, 1, 0, 1, 14, -1); + n[15].rights = RX_LAT_RIGHT_COMPUTE | RX_LAT_RIGHT_EFFECT; + n[15].txid = FX_TXID; + n[16] = fx_node(JN, 2, 0, 2, 11, 13); + n[16].N = 320; + for (uint32_t i = 0; i < FX_NODES; i++) rx_lat_add_node(L, &n[i]); +} + +static void fx_source_digest(const char *name, uint32_t rev, uint8_t out[32]) { + uint8_t tag[4] = { (uint8_t)rev, (uint8_t)(rev >> 8), 'm', '1' }; + fx_digest(tag, 4, (const uint8_t *)name, strlen(name), out); +} + +static void fx_build_methods(RxLattice *L) { + static const struct { uint32_t op, arm, rights, seat, inc; const char *name; } D[N_METHODS] = { + { RX_LAT_OP_MATVEC, ARM_REF, RX_LAT_RIGHT_COMPUTE, 0, 1, "matvec.reference" }, + { RX_LAT_OP_MATVEC, ARM_SCALAR, RX_LAT_RIGHT_COMPUTE, 0, 0, "matvec.m12.scalar" }, + { RX_LAT_OP_MATVEC, ARM_QUAD4, RX_LAT_RIGHT_COMPUTE, 0, 0, "matvec.r10.quad4" }, + { RX_LAT_OP_MATVEC, ARM_PLANTED, RX_LAT_RIGHT_COMPUTE, 0, 0, "matvec.planted_wrong" }, + { RX_LAT_OP_MATVEC, ARM_MODEL, RX_LAT_RIGHT_COMPUTE | RX_LAT_RIGHT_MODEL, 1, 0, "matvec.model_call" }, + { RX_LAT_OP_SOURCE, 0, RX_LAT_RIGHT_COMPUTE, 0, 1, "source.xorshift" }, + { RX_LAT_OP_JOIN, 0, RX_LAT_RIGHT_COMPUTE, 0, 1, "join.wrapping_sum" }, + { RX_LAT_OP_EFFECT, 0, RX_LAT_RIGHT_EFFECT, 0, 1, "effect.ledger_commit" }, + }; + for (uint32_t j = 0; j < N_METHODS; j++) { + RxLatMethod m; + memset(&m, 0, sizeof m); + m.op = D[j].op; + m.arm = D[j].arm; + m.rights = D[j].rights; + m.precision = RX_LAT_PREC_EXACT_U64; + m.revision = m.evidence_rev = 1; + m.needs_seat = D[j].seat; + m.incumbent = D[j].inc; + m.verified = D[j].op == RX_LAT_OP_MATVEC ? (uint32_t)fx_verified[D[j].arm] : 1u; + fx_source_digest(D[j].name, m.revision, m.source); + rx_lat_add_method(L, &m); + } +} + +static void fx_build_reals(RxLattice *L, int gpu_sim_verified) { + static const struct { uint32_t core, lanes, scopes; uint64_t cache; const char *desc; } D[N_REALS] = { + { RX_CM_CORE_X925, 2, 0x7, 2u << 20, "host.cpu.cortex-x925.lanes2" }, + { RX_CM_CORE_A725, 2, 0x7, 512u << 10, "host.cpu.cortex-a725.lanes2" }, + { RX_CM_CORE_OTHER, 1, 0x3, 1u << 20, "forge.gb10.sim.single-owner" }, + }; + for (uint32_t k = 0; k < N_REALS; k++) { + RxLatReal r; + memset(&r, 0, sizeof r); + r.core = D[k].core; + r.lanes = D[k].lanes; + r.scopes = D[k].scopes; + r.precisions = 1u << RX_LAT_PREC_EXACT_U64; + r.forge_verified = k == K_GPUSIM ? (uint32_t)gpu_sim_verified : 1u; + r.cache_bytes = D[k].cache; + fx_digest((const uint8_t *)"desc", 4, (const uint8_t *)D[k].desc, strlen(D[k].desc), r.descriptor); + memcpy(r.live, r.descriptor, 32); + rx_lat_add_real(L, &r); + } + /* Bounded supported combinations. */ + rx_lat_support(L, M_REF, K_X925); + rx_lat_support(L, M_REF, K_A725); + rx_lat_support(L, M_SCALAR, K_X925); + rx_lat_support(L, M_SCALAR, K_A725); + rx_lat_support(L, M_QUAD4, K_X925); + rx_lat_support(L, M_QUAD4, K_A725); + rx_lat_support(L, M_QUAD4, K_GPUSIM); + rx_lat_support(L, M_PLANTED, K_X925); + rx_lat_support(L, M_MODEL, K_GPUSIM); + rx_lat_support(L, M_SOURCE, K_X925); + rx_lat_support(L, M_JOIN, K_X925); + rx_lat_support(L, M_JOIN, K_A725); + rx_lat_support(L, M_EFFECT, K_X925); +} + +static void fx_build(RxLattice *L, int gpu_sim_verified) { + rx_lat_init(L); + fx_build_graph(L); + fx_build_methods(L); + fx_build_reals(L, gpu_sim_verified); +} + +/* Independent output check, before any receipt: MATVEC recomputes four rows + * with a plain loop and checks the guard words; SOURCE and JOIN recompute + * fully. 0 pass. */ +static uint32_t fx_check(const RxLattice *L, uint32_t i) { + const RxLatNode *a = &L->node[i]; + for (uint32_t g = 0; g < FX_GUARD; g++) + if (fx_y[i][fx_len[i] + g] != FX_CANARY) return 2; + if (a->op == RX_LAT_OP_MATVEC) { + const uint64_t *A = fx_A[a->weight]; + for (uint32_t t = 0; t < 4; t++) { + uint32_t row = (uint32_t)((i * 2654435761u + t * 997u) % a->M); + uint64_t s = 0; + for (uint32_t j = 0; j < a->N; j++) s += A[(size_t)row * a->N + j] * fx_x[i][j]; + if (s != fx_y[i][row]) return 1; + } + return 0; + } + if (a->op == RX_LAT_OP_SOURCE) { + uint64_t s = 0x0698ull; + for (uint32_t n = 0; n < a->N; n++) + if (fx_y[i][n] != fx_xs(&s)) return 1; + return 0; + } + for (uint32_t n = 0; n < a->N; n++) { + uint64_t v = 0; + for (uint32_t d = 0; d < a->n_deps; d++) { + uint32_t q = a->dep[d]; + v += fx_y[q][n % fx_len[q]]; + } + if (v != fx_y[i][n]) return 1; + } + return 0; +} + +/* Run one dispatched non-effect coordinate. Returns the independent verdict. + * The model-call method has no executor here and always fails closed. */ +static uint32_t fx_execute(const RxLattice *L, uint32_t i, uint32_t cand) { + const RxLatNode *a = &L->node[i]; + const RxLatMethod *m = &L->method[L->cand[cand].method]; + uint32_t len = a->op == RX_LAT_OP_MATVEC ? a->M : a->N; + fx_len[i] = len; + for (uint32_t g = 0; g < FX_GUARD; g++) fx_y[i][len + g] = FX_CANARY; + if (a->op == RX_LAT_OP_SOURCE) { + uint64_t s = 0x0698ull; + for (uint32_t n = 0; n < len; n++) fx_y[i][n] = fx_xs(&s); + } else if (a->op == RX_LAT_OP_JOIN) { + for (uint32_t n = 0; n < len; n++) { + uint64_t v = 0; + for (uint32_t d = 0; d < a->n_deps; d++) { + uint32_t q = a->dep[d]; + v += fx_y[q][n % fx_len[q]]; + } + fx_y[i][n] = v; + } + } else { + uint32_t q = a->dep[0]; + for (uint32_t n = 0; n < a->N; n++) fx_x[i][n] = fx_y[q][n % fx_len[q]] ^ (n * 0x9E3779B97F4A7C15ull); + if (m->arm >= N_ARMS || !fx_verified[m->arm]) return 3; /* never run an unverified arm */ + fx_fn[m->arm](fx_A[a->weight], fx_x[i], fx_y[i], a->M, a->N); + } + return fx_check(L, i); +} + +/* Independent oracle: the whole graph with the reference only, no lattice. */ +static void fx_oracle(const RxLattice *L, uint8_t out[FX_NODES][32]) { + static uint64_t y[FX_NODES][FX_MAXLEN], x[FX_MAXLEN]; + uint32_t len[FX_NODES]; + for (uint32_t t = 0; t < L->n_nodes; t++) { + uint32_t i = L->topo[t]; + const RxLatNode *a = &L->node[i]; + if (a->op == RX_LAT_OP_EFFECT) continue; + len[i] = a->op == RX_LAT_OP_MATVEC ? a->M : a->N; + if (a->op == RX_LAT_OP_SOURCE) { + uint64_t s = 0x0698ull; + for (uint32_t n = 0; n < len[i]; n++) y[i][n] = fx_xs(&s); + } else if (a->op == RX_LAT_OP_JOIN) { + for (uint32_t n = 0; n < len[i]; n++) { + uint64_t v = 0; + for (uint32_t d = 0; d < a->n_deps; d++) v += y[a->dep[d]][n % len[a->dep[d]]]; + y[i][n] = v; + } + } else { + uint32_t q = a->dep[0]; + for (uint32_t n = 0; n < a->N; n++) x[n] = y[q][n % len[q]] ^ (n * 0x9E3779B97F4A7C15ull); + const uint64_t *A = fx_A[a->weight]; + for (uint32_t r = 0; r < a->M; r++) { + uint64_t s = 0; + for (uint32_t j = 0; j < a->N; j++) s += A[(size_t)r * a->N + j] * x[j]; + y[i][r] = s; + } + } + uint8_t tag[8]; + for (int b = 0; b < 4; b++) tag[b] = (uint8_t)(len[i] >> (8 * b)); + tag[4] = 'v'; tag[5] = 'e'; tag[6] = 'c'; tag[7] = '1'; + fx_digest(tag, 8, (const uint8_t *)y[i], (size_t)len[i] * 8u, out[i]); + } +} + +#endif /* RX_LATTICE_FIXTURE_H */ diff --git a/tests/runtime/rx_lattice_unit.c b/tests/runtime/rx_lattice_unit.c new file mode 100644 index 000000000..a43455c00 --- /dev/null +++ b/tests/runtime/rx_lattice_unit.c @@ -0,0 +1,642 @@ +/* SPDX-License-Identifier: Apache-2.0 + * Copyright 2026 Drake Stapleton (AIEN / aien-dev) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* + * rx_lattice unit test (omega-mv #698). AArch64 host: the emitted matvec + * arms are real machine code, verified in a forked child before use. + * + * Time here is MODELED: each dispatched coordinate's duration comes from a + * fixed synthetic truth table (held-out noise, not the training noise), so + * schedules are deterministic and replayable. Every output is nevertheless + * computed for real by the chosen arm and checked independently before its + * receipt is sealed; the outputs of every policy are compared with each other + * and with an independent reference-only oracle that does not use the + * lattice. Measured wall time is rx_lattice_bench's job. + * + * Mutants, each on a fresh fixture: cycle, wrong dependencies, stale model + * revision and precision, illegitimate cached output, privilege escalation, + * budget starvation, model worker down, self-modified receipt and cost + * evidence, invalid realization, transaction replay, unknown cost. + */ +#include "rx_lattice_fixture.h" + +#include + +static int g_checks, g_fail; +#define CHECK(cond, ...) do { \ + g_checks++; \ + if (!(cond)) { \ + g_fail++; \ + fprintf(stderr, " FAIL %s:%d ", __FILE__, __LINE__); \ + fprintf(stderr, __VA_ARGS__); \ + fprintf(stderr, "\n"); \ + } \ + } while (0) + +/* ---- synthetic cost truth (log2 picoseconds) ---- */ + +static double truth_l2(uint32_t arm, uint32_t M, uint32_t N, uint32_t core) { + double b = 10.0 + 0.95 * log2((double)M * N + 1.0); + if (core == RX_CM_CORE_A725) b += 0.6; + if (arm == ARM_REF) b += 0.3; + if (arm == ARM_SCALAR) b += 0.5; + if (arm == ARM_QUAD4) b += core == RX_CM_CORE_OTHER ? -1.6 : -0.4; + if (arm == ARM_MODEL) b += 3.0; + return b; +} + +static double gauss(uint64_t *s) { + double u1 = ((double)(fx_xs(s) >> 11) + 1.0) / 9007199254740993.0; + double u2 = (double)(fx_xs(s) >> 11) / 9007199254740992.0; + return sqrt(-2.0 * log(u1)) * cos(6.283185307179586 * u2); +} + +static RxCmFeatures feat(uint32_t M, uint32_t N, uint32_t core, uint64_t cache) { + RxCmFeatures f; + memset(&f, 0, sizeof f); + f.op = RX_CM_OP_MATVEC; + f.M = M; + f.N = N; + f.state_bytes = ((uint64_t)M * N + M + N) * 8u; + f.cache_bytes = cache; + f.core = core; + f.eligible = 0x7; + f.need_evidence = 1; + return f; +} + +static void train(RxCostModel *m, uint32_t arm_mask) { + static const uint32_t TM[] = { 16, 64, 128, 256, 512 }; + static const uint32_t TN[] = { 16, 33, 64, 129, 255, 256, 320, 384 }; + static const uint64_t CACHE[RX_CM_CORES] = { 2u << 20, 512u << 10, 1u << 20 }; + uint64_t s = 0x7A11u; + rx_cm_init(m, N_ARMS); + for (int rep = 0; rep < 2; rep++) + for (uint32_t core = 0; core < RX_CM_CORES; core++) + for (unsigned a = 0; a < sizeof TM / sizeof TM[0]; a++) + for (unsigned b = 0; b < sizeof TN / sizeof TN[0]; b++) + for (uint32_t arm = 0; arm < N_ARMS; arm++) { + if (!(arm_mask & (1u << arm))) continue; + RxCmFeatures f = feat(TM[a], TN[b], core, CACHE[core]); + RxCmObservation o = { (uint64_t)exp2(truth_l2(arm, TM[a], TN[b], core) + 0.1 * gauss(&s)), 0 }; + rx_cm_observe(m, &f, arm, &o); + } +} + +/* Held-out duration of one coordinate: its own noise stream, never trained on. */ +static uint64_t actual_ps(const RxLattice *L, uint32_t i, uint32_t c) { + const RxLatNode *a = &L->node[i]; + if (a->op != RX_LAT_OP_MATVEC) return 200000ull + (uint64_t)a->N * 500u; + const RxLatCand *k = &L->cand[c]; + uint64_t s = 0xD0E5u + i * 1000003ull + c * 7919ull; + double l2 = truth_l2(L->method[k->method].arm, a->M, a->N, L->real[k->real].core) + 0.15 * gauss(&s); + return (uint64_t)exp2(l2); +} + +/* ---- reuse store: cached vectors addressed by output digest ---- */ + +static uint64_t st_vec[RX_LAT_MAX_CACHE][FX_MAXLEN]; +static uint32_t st_len[RX_LAT_MAX_CACHE]; +static uint8_t st_dig[RX_LAT_MAX_CACHE][32]; +static uint32_t st_n; + +static int st_find(const uint8_t d[32]) { + for (uint32_t t = 0; t < st_n; t++) + if (!memcmp(st_dig[t], d, 32)) return (int)t; + return -1; +} + +/* ---- modeled runner ---- */ + +typedef struct { + uint64_t makespan_ps; + uint64_t busy_ps[N_REALS]; + uint32_t max_conc[N_REALS]; + uint32_t arm_runs[N_ARMS]; + uint32_t cache_hits; + uint8_t status[FX_NODES]; + uint8_t out[FX_NODES][32]; + uint32_t done, failed, refused, weight_switches, receipt_refusals; + int effect_rc; + uint64_t chosen_real_mask; +} Outcome; + +typedef struct { + int tamper_node; /* flip a byte of this node's receipt output after sealing */ + int wrong_cand_node; /* report a different candidate for this node */ + RxLatCache *cache; +} Hooks; + +static const Hooks NO_HOOKS = { -1, -1, NULL }; + +static int run_modeled(const RxLattice *L, uint32_t policy, RxLatLedger *g, const Hooks *h, Outcome *o) { + RxLatRun r; + memset(&r, 0, sizeof r); + memset(o, 0, sizeof *o); + o->effect_rc = 1; /* not attempted */ + if (rx_lat_run_init(L, &r, policy) != RX_LAT_OK) return -1; + uint64_t fin[FX_NODES], dur[FX_NODES]; + RxLatReceipt rc[FX_NODES]; + uint8_t running[FX_NODES]; + uint32_t conc[N_REALS]; + memset(running, 0, sizeof running); + memset(conc, 0, sizeof conc); + for (;;) { + RxLatPick p; + while (rx_lat_pick(L, &r, &p)) { + uint32_t i = p.node; + const RxLatNode *a = &L->node[i]; + if (++conc[p.real] > o->max_conc[p.real]) o->max_conc[p.real] = conc[p.real]; + o->chosen_real_mask |= 1ull << (p.real * 8u + L->cand[p.cand].method); + memset(&rc[i], 0, sizeof rc[i]); + rc[i].node = (uint16_t)i; + rc[i].cand = (uint16_t)p.cand; + dur[i] = actual_ps(L, i, p.cand); + int reused = 0; + uint8_t key[32]; + if (a->op == RX_LAT_OP_EFFECT) { + o->effect_rc = rx_lat_effect_commit(g, L, &r, i, rc[i].output); + rc[i].verdict = o->effect_rc == RX_LAT_OK ? 0u : 1u; + } else { + if (h->cache && rx_lat_cache_key(L, &r, i, key) == RX_LAT_OK) { + uint8_t d[32]; + int t = rx_lat_cache_get(h->cache, key, a->scope, a->gen, d) == 0 ? st_find(d) : -1; + if (t >= 0) { + fx_len[i] = st_len[t]; + memcpy(fx_y[i], st_vec[t], (size_t)st_len[t] * 8u); + for (uint32_t q = 0; q < FX_GUARD; q++) fx_y[i][st_len[t] + q] = FX_CANARY; + fx_vec_digest(i, rc[i].output); + rc[i].verdict = memcmp(rc[i].output, d, 32) != 0; + dur[i] = 100000; /* lookup */ + reused = 1; + o->cache_hits++; + } + } + if (!reused) { + rc[i].verdict = fx_execute(L, i, p.cand); + if (a->op == RX_LAT_OP_MATVEC) o->arm_runs[L->method[L->cand[p.cand].method].arm]++; + fx_vec_digest(i, rc[i].output); + if (h->cache && rc[i].verdict == 0 && rx_lat_cache_key(L, &r, i, key) == RX_LAT_OK && + st_n < RX_LAT_MAX_CACHE && rx_lat_cache_put(h->cache, key, a->scope, a->gen, rc[i].output) == 0) { + memcpy(st_vec[st_n], fx_y[i], (size_t)fx_len[i] * 8u); + st_len[st_n] = fx_len[i]; + memcpy(st_dig[st_n], rc[i].output, 32); + st_n++; + } + } + } + rc[i].elapsed_ps = dur[i]; + rx_lat_receipt_seal(L, &rc[i]); + if (h->tamper_node == (int)i) rc[i].output[0] ^= 1u; + if (h->wrong_cand_node == (int)i) rc[i].cand = (uint16_t)(rc[i].cand + 1u); + fin[i] = r.now_ps + dur[i]; + running[i] = 1; + } + int next = -1; + for (uint32_t i = 0; i < L->n_nodes; i++) + if (running[i] && (next < 0 || fin[i] < fin[next])) next = (int)i; + if (next < 0) break; + uint32_t i = (uint32_t)next; + running[i] = 0; + r.now_ps = fin[i]; + uint32_t k = L->cand[r.chosen[i]].real; + conc[k]--; + o->busy_ps[k] += dur[i]; + (void)rx_lat_finish(L, &r, &rc[i]); /* outcome lands in r.status / counters */ + } + o->makespan_ps = r.now_ps; + memcpy(o->status, r.status, sizeof o->status); + memcpy(o->out, r.out, sizeof o->out); + o->done = r.done; + o->failed = r.failed; + o->refused = r.refused; + o->weight_switches = r.weight_switches; + o->receipt_refusals = r.receipt_refusals; + return rx_lat_settled(L, &r) ? 0 : -2; +} + +/* ---- fixture with a trained cost model ---- */ + +static RxCostModel g_cm; +static RxCmPolicy g_pol; + +static int fresh(RxLattice *L, int gpu_sim_verified) { + fx_build(L, gpu_sim_verified); + int n = rx_lat_build(L); + if (n < 0) return n; + rx_lat_pin(L, &g_cm); + return rx_lat_admit(L, &g_cm, &g_pol); +} + +static uint32_t count_reason(const RxLattice *L, uint32_t method, uint32_t reason) { + uint32_t n = 0; + for (uint32_t c = 0; c < L->n_cands; c++) + if (L->cand[c].method == method && L->cand[c].reason == reason) n++; + return n; +} + +static uint8_t g_base[FX_NODES][32]; +static uint8_t g_base_effect[32]; + +static int same_outputs(const Outcome *o, uint32_t skip_mask) { + for (uint32_t i = 0; i < FX_NODES; i++) { + if (skip_mask & (1u << i)) continue; + if (o->status[i] != RX_LAT_DONE || memcmp(o->out[i], g_base[i], 32)) return 0; + } + return 1; +} + +/* ---- tests ---- */ + +static void t_build(void) { + RxLattice L, L2; + int empty = fresh(&L, 1); + /* MATVEC nodes: 11 x (ref 2 + scalar 2 + quad4 3 + planted 1 + model 1) = 99; + * SOURCE 1, JOIN 4 x 2, EFFECT 1. */ + CHECK(L.n_cands == 99u + 1u + 8u + 1u, "sparse candidate count %u", L.n_cands); + CHECK(L.n_cands < FX_NODES * N_METHODS * N_REALS, "sparser than the dense cube"); + CHECK(empty == 0, "every node has an admitted candidate (%d empty)", empty); + int distinct = 1; + for (uint32_t a = 0; a < L.n_cands; a++) + for (uint32_t b = a + 1; b < L.n_cands; b++) + if (!memcmp(L.cand[a].id, L.cand[b].id, 32)) distinct = 0; + CHECK(distinct, "candidate identities distinct"); + fresh(&L2, 1); + CHECK(!memcmp(L.cand, L2.cand, sizeof L.cand), "build and admission deterministic"); + CHECK(count_reason(&L, M_PLANTED, RX_LAT_R_UNVERIFIED) == 11, "planted arm refused: unverified"); + CHECK(count_reason(&L, M_MODEL, RX_LAT_R_SEAT) == 7 && count_reason(&L, M_MODEL, RX_LAT_R_SCOPE) == 4, + "model-call refused: seat down or scope"); + uint32_t bscope = 0; + for (uint32_t c = 0; c < L.n_cands; c++) + if (L.cand[c].real == K_GPUSIM && L.node[L.cand[c].node].scope == 2) + bscope += L.cand[c].reason == RX_LAT_R_SCOPE; + CHECK(bscope == 4u * 2u, "tenant B never admitted onto tenant A's leased device (%u)", bscope); + for (uint32_t c = 0; c < L.n_cands; c++) + if (L.cand[c].reason == RX_LAT_ADMITTED) + CHECK(L.cand[c].est_ps > 0 && L.cand[c].ucb_ps >= L.cand[c].est_ps, "estimates nonzero, ucb >= mean"); + printf(" build: %u candidates; admitted %u; refused unverified %u seat %u scope %u\n", L.n_cands, + L.reasons[RX_LAT_ADMITTED], L.reasons[RX_LAT_R_UNVERIFIED], L.reasons[RX_LAT_R_SEAT], + L.reasons[RX_LAT_R_SCOPE]); +} + +static void t_policies(void) { + RxLattice L; + fresh(&L, 1); + uint8_t oracle[FX_NODES][32]; + fx_oracle(&L, oracle); + Outcome o[RX_LAT_POLICY_COUNT]; + for (uint32_t p = 0; p < RX_LAT_POLICY_COUNT; p++) { + RxLatLedger g; + memset(&g, 0, sizeof g); + CHECK(run_modeled(&L, p, &g, &NO_HOOKS, &o[p]) == 0, "%s settles", rx_lat_policy_name(p)); + CHECK(o[p].done == FX_NODES && o[p].failed == 0 && o[p].refused == 0, "%s completes all", rx_lat_policy_name(p)); + CHECK(g.n == 1 && o[p].effect_rc == RX_LAT_OK, "%s commits the effect once", rx_lat_policy_name(p)); + for (uint32_t k = 0; k < N_REALS; k++) + CHECK(o[p].max_conc[k] <= L.real[k].lanes, "%s never exceeds %s lanes", rx_lat_policy_name(p), REAL_NAME[k]); + for (uint32_t i = 0; i < FX_NODES; i++) + if (i != FX_EFFECT) CHECK(!memcmp(o[p].out[i], oracle[i], 32), "%s node %u equals oracle", rx_lat_policy_name(p), i); + if (p == 0) { + memcpy(g_base, o[0].out, sizeof g_base); + memcpy(g_base_effect, g.value[0], 32); + } else { + CHECK(same_outputs(&o[p], 0), "%s outputs equal serial", rx_lat_policy_name(p)); + CHECK(!memcmp(g.value[0], g_base_effect, 32), "%s effect value equals serial", rx_lat_policy_name(p)); + } + } + CHECK(o[0].max_conc[K_X925] == 1 && o[0].max_conc[K_A725] == 0 && o[0].max_conc[K_GPUSIM] == 0, + "serial: one node at a time on the incumbent realization"); + CHECK(o[0].arm_runs[ARM_REF] == 11, "serial runs the incumbent arm only"); + CHECK(o[RX_LAT_POLICY_Q3D].arm_runs[ARM_PLANTED] == 0 && o[RX_LAT_POLICY_LCF].arm_runs[ARM_PLANTED] == 0, + "planted arm never runs"); + printf(" MODELED time (synthetic truth table, not a measurement):\n"); + printf(" predicted critical path %.1f us\n", (double)rx_lat_critical_path_ps(&L) / 1e6); + for (uint32_t p = 0; p < RX_LAT_POLICY_COUNT; p++) + printf(" %-6s makespan %8.1f us busy x925 %7.1f a725 %7.1f gpu.sim %7.1f us arms ref %u scalar %u quad4 %u weight switches %u\n", + rx_lat_policy_name(p), (double)o[p].makespan_ps / 1e6, (double)o[p].busy_ps[0] / 1e6, + (double)o[p].busy_ps[1] / 1e6, (double)o[p].busy_ps[2] / 1e6, o[p].arm_runs[ARM_REF], + o[p].arm_runs[ARM_SCALAR], o[p].arm_runs[ARM_QUAD4], o[p].weight_switches); +} + +static void t_cycle(void) { + RxLattice L; + fx_build(&L, 1); + L.node[1].dep[0] = 7; /* 1 -> 2 -> 6 -> 7 -> 1 */ + CHECK(rx_lat_build(&L) == RX_LAT_E_CYCLE, "cycle refused"); + CHECK(rx_lat_run_init(&L, &(RxLatRun){ 0 }, RX_LAT_POLICY_Q3D) == RX_LAT_E_ARG, "no run without candidates"); + fx_build(&L, 1); + L.node[3].dep[0] = 3; + CHECK(rx_lat_build(&L) == RX_LAT_E_CYCLE, "self dependency refused as a cycle"); +} + +static void t_wrong_deps(void) { + RxLattice L; + fx_build(&L, 1); + L.node[16].dep[1] = FX_EFFECT; + CHECK(rx_lat_build(&L) == RX_LAT_E_TYPE, "consuming an effect receipt as a value refused"); + fx_build(&L, 1); + L.node[9].dep[0] = 40; + CHECK(rx_lat_build(&L) == RX_LAT_E_TYPE, "dependency out of range refused"); + fx_build(&L, 1); + L.node[7].n_deps = 2; + L.node[7].dep[1] = 1; + CHECK(rx_lat_build(&L) == RX_LAT_E_TYPE, "matvec arity refused"); + fx_build(&L, 1); + L.node[6].dep[1] = 2; + CHECK(rx_lat_build(&L) == RX_LAT_E_TYPE, "duplicate dependency refused"); + fx_build(&L, 1); + L.node[5].N = 321; + CHECK(rx_lat_build(&L) == RX_LAT_E_TYPE, "two shapes for one weight id refused"); + fx_build(&L, 1); + L.node[8].dep[1] = 2; /* tenant B consumes tenant A's private output */ + CHECK(rx_lat_build(&L) == RX_LAT_E_SCOPE, "cross-tenant flow refused"); + fx_build(&L, 1); + L.node[12].kind = RX_LAT_KIND_JOIN; + CHECK(rx_lat_build(&L) == RX_LAT_E_TYPE, "kind / op mismatch refused"); +} + +static void t_stale(void) { + RxLattice L; + fx_build(&L, 1); + L.method[M_QUAD4].revision = 2; /* new code, evidence still from revision 1 */ + rx_lat_build(&L); + rx_lat_pin(&L, &g_cm); + CHECK(rx_lat_admit(&L, &g_cm, &g_pol) == 0, "stale model: every node still admissible"); + CHECK(count_reason(&L, M_QUAD4, RX_LAT_R_STALE) == 33, "stale model revision refused"); + Outcome o; + RxLatLedger g = { 0 }; + CHECK(run_modeled(&L, RX_LAT_POLICY_Q3D, &g, &NO_HOOKS, &o) == 0 && same_outputs(&o, 0), + "stale model: same outputs"); + CHECK(o.arm_runs[ARM_QUAD4] == 0, "stale arm never runs"); + + fx_build(&L, 1); + L.method[M_SCALAR].precision = RX_LAT_PREC_APPROX; + L.real[K_GPUSIM].precisions = 1u << RX_LAT_PREC_APPROX; + rx_lat_build(&L); + rx_lat_pin(&L, &g_cm); + rx_lat_admit(&L, &g_cm, &g_pol); + CHECK(count_reason(&L, M_SCALAR, RX_LAT_R_PRECISION) == 22, "approximate method refused for exact contract"); + CHECK(count_reason(&L, M_QUAD4, RX_LAT_R_PRECISION) == 11, "realization without the exact contract refused"); + memset(&g, 0, sizeof g); + CHECK(run_modeled(&L, RX_LAT_POLICY_Q3D, &g, &NO_HOOKS, &o) == 0 && same_outputs(&o, 0) && + o.arm_runs[ARM_SCALAR] == 0 && o.busy_ps[K_GPUSIM] == 0, "stale precision: same outputs, never placed"); +} + +static void t_cache(void) { + RxLattice L; + fresh(&L, 1); + RxLatCache c; + memset(&c, 0, sizeof c); + st_n = 0; + Hooks h = { -1, -1, &c }; + Outcome o; + RxLatLedger g = { 0 }; + CHECK(run_modeled(&L, RX_LAT_POLICY_Q3D, &g, &h, &o) == 0 && same_outputs(&o, 0), "cache fill run"); + CHECK(c.n == FX_NODES - 1u && o.cache_hits == 0, "pure nodes cached, effect never (%u)", c.n); + /* Legitimate reuse: same objective again with a new transaction. */ + L.node[FX_EFFECT].txid = FX_TXID + 1; + rx_lat_build(&L); + rx_lat_admit(&L, &g_cm, &g_pol); + /* The effect value binds the new transaction id, so it is compared by status only. */ + CHECK(run_modeled(&L, RX_LAT_POLICY_Q3D, &g, &h, &o) == 0 && same_outputs(&o, 1u << FX_EFFECT) && + o.status[FX_EFFECT] == RX_LAT_DONE && g.n == 2, "reuse run"); + CHECK(o.cache_hits == FX_NODES - 1u && o.arm_runs[ARM_REF] + o.arm_runs[ARM_SCALAR] + o.arm_runs[ARM_QUAD4] == 0, + "every pure node reused, nothing recomputed (%u hits)", o.cache_hits); + /* Illegitimate entries: tampered output, other tenant, stale generation. */ + c.e[2].output[5] ^= 0x40; /* third entry (insertion order follows dispatch) */ + for (uint32_t t = 0; t < c.n; t++) { + if (t == 3) c.e[t].scope ^= 3u; /* claim another scope */ + if (t == 4) c.e[t].gen += 1; /* stale generation */ + } + uint32_t ref0 = c.refusals; + L.node[FX_EFFECT].txid = FX_TXID + 2; + rx_lat_build(&L); + rx_lat_admit(&L, &g_cm, &g_pol); + CHECK(run_modeled(&L, RX_LAT_POLICY_Q3D, &g, &h, &o) == 0 && same_outputs(&o, 1u << FX_EFFECT) && + o.status[FX_EFFECT] == RX_LAT_DONE && g.n == 3, + "illegitimate entries: outputs still equal serial"); + CHECK(c.refusals - ref0 == 3, "three illegitimate entries refused (%u)", c.refusals - ref0); + CHECK(o.cache_hits == FX_NODES - 1u - 3u, "the rest reused (%u)", o.cache_hits); + RxLatRun r; + memset(&r, 0, sizeof r); + uint8_t key[32]; + CHECK(rx_lat_cache_key(&L, &r, FX_EFFECT, key) == RX_LAT_E_TYPE, "effects have no cache key"); +} + +static void t_privilege(void) { + RxLattice L; + fx_build(&L, 1); + L.method[M_QUAD4].rights |= RX_LAT_RIGHT_EFFECT; /* compute method asking for the effect right */ + rx_lat_build(&L); + rx_lat_pin(&L, &g_cm); + rx_lat_admit(&L, &g_cm, &g_pol); + CHECK(count_reason(&L, M_QUAD4, RX_LAT_R_PRIVILEGE) == 33, "escalating method refused everywhere"); + Outcome o; + RxLatLedger g = { 0 }; + CHECK(run_modeled(&L, RX_LAT_POLICY_Q3D, &g, &NO_HOOKS, &o) == 0 && same_outputs(&o, 0) && + o.arm_runs[ARM_QUAD4] == 0, "escalating method never runs; outputs equal"); + fx_build(&L, 1); + L.node[FX_EFFECT].rights = RX_LAT_RIGHT_COMPUTE; + CHECK(rx_lat_build(&L) == RX_LAT_E_EFFECT, "effect without the EFFECT right refused"); + fresh(&L, 1); + RxLatRun r; + memset(&r, 0, sizeof r); + rx_lat_run_init(&L, &r, RX_LAT_POLICY_Q3D); + uint8_t v[32]; + CHECK(rx_lat_effect_commit(&g, &L, &r, 7, v) == RX_LAT_E_EFFECT, "compute node cannot commit an effect"); + CHECK(rx_lat_effect_commit(&g, &L, &r, FX_EFFECT, v) == RX_LAT_E_STATE, "effect before its inputs refused"); +} + +static void t_budget(void) { + RxLattice L; + fx_build(&L, 1); + L.node[10].budget_ps = 1000; /* 1 ns: nothing can fit */ + rx_lat_build(&L); + rx_lat_pin(&L, &g_cm); + CHECK(rx_lat_admit(&L, &g_cm, &g_pol) == 1, "one node left without a candidate"); + for (uint32_t c = L.cand_first[10]; c < (uint32_t)L.cand_first[10] + L.cand_count[10]; c++) + CHECK(L.cand[c].reason != RX_LAT_ADMITTED, "starved node: nothing admitted"); + Outcome o; + RxLatLedger g = { 0 }; + CHECK(run_modeled(&L, RX_LAT_POLICY_Q3D, &g, &NO_HOOKS, &o) == 0, "settles"); + CHECK(o.status[10] == RX_LAT_REFUSED && o.status[14] == RX_LAT_REFUSED && o.status[FX_EFFECT] == RX_LAT_REFUSED, + "starved node and its dependents refused"); + CHECK(g.n == 0, "no effect committed on a refused objective"); + CHECK(same_outputs(&o, (1u << 10) | (1u << 14) | (1u << FX_EFFECT)), "independent work completes, equal outputs"); + CHECK(o.done == FX_NODES - 3u, "done %u", o.done); +} + +static void t_seat(void) { + RxLattice L; + RxCostModel m; + train(&m, 0x1Fu); /* evidence for the model-call arm too */ + fx_build(&L, 1); + L.method[M_MODEL].verified = 1; /* suppose it were qualified */ + rx_lat_build(&L); + rx_lat_pin(&L, &m); + rx_lat_admit(&L, &m, &g_pol); + CHECK(count_reason(&L, M_MODEL, RX_LAT_R_SEAT) == 7, "worker down: model-call refused (seat)"); + Outcome o; + RxLatLedger g = { 0 }; + CHECK(run_modeled(&L, RX_LAT_POLICY_Q3D, &g, &NO_HOOKS, &o) == 0 && same_outputs(&o, 0), + "worker down: math-only path, same outputs"); + L.seat_up = 1; + rx_lat_admit(&L, &m, &g_pol); + CHECK(count_reason(&L, M_MODEL, RX_LAT_ADMITTED) == 7, "seat up: proposal admissible (not run here)"); +} + +static void t_receipts(void) { + RxLattice L; + fresh(&L, 1); + Outcome o; + RxLatLedger g = { 0 }; + Hooks h = { 7, -1, NULL }; + CHECK(run_modeled(&L, RX_LAT_POLICY_Q3D, &g, &h, &o) == 0, "settles"); + CHECK(o.status[7] == RX_LAT_FAILED && o.receipt_refusals == 1, "modified receipt refused"); + CHECK(o.status[10] == RX_LAT_REFUSED && o.status[14] == RX_LAT_REFUSED && g.n == 0, + "dependents refused, no effect"); + memset(&g, 0, sizeof g); + Hooks h2 = { -1, 9, NULL }; + CHECK(run_modeled(&L, RX_LAT_POLICY_LCF, &g, &h2, &o) == 0 && o.status[9] == RX_LAT_FAILED, + "receipt for another candidate refused"); + /* A receipt replayed for a committed node. */ + RxLatRun r; + memset(&r, 0, sizeof r); + rx_lat_run_init(&L, &r, RX_LAT_POLICY_SERIAL); + RxLatPick p; + CHECK(rx_lat_pick(&L, &r, &p) == 1 && p.node == 0, "serial picks the source"); + RxLatReceipt rc = { .node = 0, .cand = (uint16_t)p.cand }; + fx_execute(&L, 0, p.cand); + fx_vec_digest(0, rc.output); + rx_lat_receipt_seal(&L, &rc); + CHECK(rx_lat_finish(&L, &r, &rc) == RX_LAT_OK, "genuine receipt commits"); + CHECK(rx_lat_finish(&L, &r, &rc) == RX_LAT_E_STATE, "replayed receipt refused"); + /* Self-modified cost evidence after pinning. */ + RxCostModel m = g_cm; + fx_build(&L, 1); + rx_lat_build(&L); + rx_lat_pin(&L, &m); + RxCmFeatures f = feat(256, 256, RX_CM_CORE_X925, 2u << 20); + RxCmObservation fake = { 1000, 0 }; + rx_cm_observe(&m, &f, ARM_SCALAR, &fake); + rx_lat_admit(&L, &m, &g_pol); + CHECK(count_reason(&L, M_SCALAR, RX_LAT_R_EVIDENCE) == 22 && count_reason(&L, M_QUAD4, RX_LAT_R_EVIDENCE) == 29, + "changed evidence: non-incumbent proposals refused"); + CHECK(count_reason(&L, M_REF, RX_LAT_R_UNKNOWN_COST) == 22, "incumbent without prediction or cap refused"); + for (uint32_t i = 0; i < FX_NODES; i++) + if (L.node[i].op == RX_LAT_OP_MATVEC) L.node[i].budget_ps = 400000000ull; /* 400 us cap */ + rx_lat_admit(&L, &m, &g_pol); + CHECK(count_reason(&L, M_REF, RX_LAT_ADMITTED) == 22, "with a cap the incumbent runs at its cap"); + memset(&g, 0, sizeof g); + CHECK(run_modeled(&L, RX_LAT_POLICY_Q3D, &g, &NO_HOOKS, &o) == 0 && same_outputs(&o, 0) && + o.arm_runs[ARM_REF] == 11, "changed evidence: incumbent only, same outputs"); +} + +static void t_realization(void) { + RxLattice L; + fresh(&L, 0); + CHECK(count_reason(&L, M_QUAD4, RX_LAT_R_REALIZATION) == 7, "unverified FORGE realization refused (%u)", + count_reason(&L, M_QUAD4, RX_LAT_R_REALIZATION)); + fx_build(&L, 1); + rx_lat_build(&L); + L.real[K_X925].live[0] ^= 1; /* descriptor changed since the lattice was built */ + rx_lat_pin(&L, &g_cm); + rx_lat_admit(&L, &g_cm, &g_pol); + uint32_t bad = 0; + for (uint32_t c = 0; c < L.n_cands; c++) + if (L.cand[c].real == K_X925) bad += L.cand[c].reason == RX_LAT_R_REALIZATION || + L.cand[c].reason == RX_LAT_R_UNVERIFIED; + CHECK(bad == 11u * 4u + 1u + 4u + 1u, "changed descriptor: every x925 coordinate refused (%u)", bad); + /* SOURCE and EFFECT exist only on x925: they are refused, so the objective is. */ + Outcome o; + RxLatLedger g = { 0 }; + CHECK(run_modeled(&L, RX_LAT_POLICY_Q3D, &g, &NO_HOOKS, &o) == 0 && o.done == 0 && g.n == 0, + "objective refused when its source has no valid realization"); + fresh(&L, 0); + memset(&g, 0, sizeof g); + CHECK(run_modeled(&L, RX_LAT_POLICY_Q3D, &g, &NO_HOOKS, &o) == 0 && same_outputs(&o, 0) && + o.busy_ps[K_GPUSIM] == 0, "invalid gpu.sim: CPU only, same outputs"); +} + +static void t_replay(void) { + RxLattice L; + fresh(&L, 1); + RxLatLedger g = { 0 }; + Outcome o; + CHECK(run_modeled(&L, RX_LAT_POLICY_Q3D, &g, &NO_HOOKS, &o) == 0 && g.n == 1, "first commit"); + CHECK(run_modeled(&L, RX_LAT_POLICY_Q3D, &g, &NO_HOOKS, &o) == 0, "second run settles"); + CHECK(o.effect_rc == RX_LAT_E_REPLAY && g.n == 1 && g.replays == 1 && o.status[FX_EFFECT] == RX_LAT_FAILED, + "replayed transaction refused, ledger unchanged"); +} + +static void t_unknown(void) { + RxLattice L; + RxCostModel empty; + rx_cm_init(&empty, N_ARMS); + fx_build(&L, 1); + rx_lat_build(&L); + rx_lat_pin(&L, &empty); + CHECK(rx_lat_admit(&L, &empty, &g_pol) == 11, "no evidence and no cap: matvec nodes have no candidate"); + CHECK(count_reason(&L, M_QUAD4, RX_LAT_R_UNKNOWN_COST) == 29, "unknown cost never admitted for a challenger"); + for (uint32_t i = 0; i < FX_NODES; i++) + if (L.node[i].op == RX_LAT_OP_MATVEC) L.node[i].budget_ps = 400000000ull; + CHECK(rx_lat_admit(&L, &empty, &g_pol) == 0, "with caps: the incumbent is the selection"); + Outcome o; + RxLatLedger g = { 0 }; + CHECK(run_modeled(&L, RX_LAT_POLICY_Q3D, &g, &NO_HOOKS, &o) == 0 && same_outputs(&o, 0) && + o.arm_runs[ARM_REF] == 11, "unknown cost: incumbent only, same outputs"); +} + +static void t_failed_arm(void) { + RxLattice L; + RxCostModel m = g_cm; + RxCmFeatures f = feat(64, 64, RX_CM_CORE_X925, 2u << 20); + RxCmObservation bad = { 0, 1 }; + rx_cm_observe(&m, &f, ARM_QUAD4, &bad); + fx_build(&L, 1); + rx_lat_build(&L); + rx_lat_pin(&L, &m); + rx_lat_admit(&L, &m, &g_pol); + CHECK(count_reason(&L, M_QUAD4, RX_LAT_R_FAILED) == 29, "an arm that produced a wrong result is excluded"); +} + +int main(void) { + printf("rx_lattice unit (omega-mv #698)\n"); + if (fx_setup_arms() != 0) { + fprintf(stderr, "arm setup failed\n"); + return 1; + } + for (uint32_t a = 0; a < N_ARMS; a++) printf(" arm %-13s %s\n", ARM_NAME[a], fx_verified[a] ? "verified" : "REFUSED"); + CHECK(fx_verified[ARM_REF] && fx_verified[ARM_SCALAR] && fx_verified[ARM_QUAD4], "real arms verified"); + CHECK(!fx_verified[ARM_PLANTED], "planted arm caught by the differential"); + train(&g_cm, (1u << ARM_REF) | (1u << ARM_SCALAR) | (1u << ARM_QUAD4)); + rx_cm_default_policy(&g_pol); + t_build(); + t_policies(); + t_cycle(); + t_wrong_deps(); + t_stale(); + t_cache(); + t_privilege(); + t_budget(); + t_seat(); + t_receipts(); + t_realization(); + t_replay(); + t_unknown(); + t_failed_arm(); + printf("checks %d failures %d\n", g_checks, g_fail); + return g_fail ? 1 : 0; +} diff --git a/tools/.crumb b/tools/.crumb index b7ed2e98e..e37035a43 100644 --- a/tools/.crumb +++ b/tools/.crumb @@ -26,50 +26,50 @@ "children": [ { "digest": "9f82d33852a7cdcedb210ee1525f42c4d963c18940cae1df7f69d1210de71985", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "argus", "purpose": "Directory tools/argus. Purpose not yet described by a human." }, { "digest": "4b3a030252eaa6f1c693105a76092e8a5fccfd340658a982f5bb1130ce9aba33", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "brownian", "purpose": "Directory tools/brownian. Purpose not yet described by a human." }, { "digest": "1d21e7e466d0fd96abf30d11b2d7e45718bc42ada1784bc8bc2a749f541b3ab7", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "dual_experiment", "purpose": "Runner for the DUAL applicability experiment: runs the pre-registered campaign and writes result" }, { "digest": "2a1bd46e9c9502d02144e3f76c784863001323c564a22bdae2905c0660a5f7a4", - "last_commit": "d863780eb", + "last_commit": "d863780e", "name": "estimation", "purpose": "Directory tools/estimation. Purpose not yet described by a human." }, { "digest": "b3241d8cdc884bbfcca5cf9d5f4ed0aac8f7e2df43d9ab47ca2789432dd49a75", - "last_commit": "d863780eb", + "last_commit": "d863780e", "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": "d863780eb", + "last_commit": "d863780e", "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-24T23:52:12Z", + "compiled_at": "2026-10-25T02:29:06Z", "compiler": "crumb-compile/1", - "digest": "sha256:dbe7b08900fc053b23c993a25a3fe744e3874be2f23ee24f20e183797db53c27", + "digest": "sha256:c86cd0893d1f95dee1febdf19efe3ae5a2fcc2ec2b619f9a876a4a28b80bf548", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 294, - "generated_at_commit": "51f4d9c65856420ea00459e8054b5869951b081e", - "source_tree": "sha256:a37bc026768d74bbf861e4cf26811f03a22efe9b5fb0ae28d646aedd9e5c9477" + "files": 295, + "generated_at_commit": "d044d9af667dd04d28d9dbf64d2e6ca4caad67ce", + "source_tree": "sha256:17d77462e54c4f104cf89c75c779be09f7bb83a18e3f436918e06147f9022d79" }, "owner_lane": "", "provenance": { ```