diff --git a/research/complex-rank2-macro-fusion-screen/README.md b/research/complex-rank2-macro-fusion-screen/README.md new file mode 100644 index 0000000000..f48db1404e --- /dev/null +++ b/research/complex-rank2-macro-fusion-screen/README.md @@ -0,0 +1,40 @@ +# Selected complex rank-two macro-fusion screen + +**A source-bound negative screen for direct pair fusion. No new supplier or κ claim.** + +## Result + +The selected complex trace has 2,412 rank-one child climbs per orientation. A direct pair-to-rank-two macro would be attractive in the recursive cost model: if it preserved the exact operator, replacing two rank-one calls by one rank-two call would multiply that pair's recursive charge by `2^-a`, for the provider saving exponent `a > 0`. + +This screen checks the narrowest concrete version of that idea against the pinned selected source. It walks each register's climb history and looks for two successive rank-one climbs whose combined endpoint transition also has an exact rank-two child recipe. Each orientation has 1,441 such rank-one successor links, of which 35 have a matching direct rank-two recipe. **All 35 candidates are interrupted by scalar operations on the same register**: 20 have five such touches, five have two, and ten have one. No candidate can be substituted directly while preserving the recorded scalar schedule. + +The forward phase-B witness on `L195` has a genuine exact identity. Its calls at events 12806 and 13402 use the recipes `0→2, rank 1` and `2→252, rank 1`; their 512-by-512 Gaussian-dyadic operator product equals the direct `0→252, rank 2` recipe on **all 512 basis vectors**. The exact operator hashes and the full intervening-window receipt are in [`RESULT.json`](RESULT.json). This confirms the child-map composition, rather than inferring it from matching endpoint frames. + +The intervening ADD still prevents a direct replacement. In the forward trace it changes the required output to `L195_out = D x + (1/2) B y`, where `D=BA` and `y=L521`; in the backward trace it also writes `L521_out = y - (1/2) A x`, so the caller consumes the intermediate `A x`. The one-output rank-two recipe supplies `D x` but neither cross-register block nor the intermediate tap. Every basis input has a nonzero missing contribution. Computing the tap separately needs a rank-one-equivalent call: the separated rank-two-plus-rank-one profile costs `2^(1-a)+1 > 2` for `0 2", + "multioutput_rank2_tap": "changed provider interface; not in pinned recipes or priced", + "original_rank_profile": [ + 1, + 1 + ], + "split_rank2_plus_rank1_tap_profile": [ + 2, + 1 + ] + }, + "max_nonzeros_per_column": { + "direct_rank2": 4, + "first_rank1": 2, + "second_rank1": 2 + }, + "operator_dimension": 512, + "operator_sha256": { + "composed_rank1_pair": "9c0e0ded05b5fb99160c05fb27624f4ee6a76d2f0922ef76caafd0a4c6808255", + "direct_rank2": "9c0e0ded05b5fb99160c05fb27624f4ee6a76d2f0922ef76caafd0a4c6808255", + "first_rank1": "b9357c40f5f003d761452d4695b65663254e3e01ef2b506ff73a5ff3da8c3c1d", + "second_rank1": "7b076f5dbf1720c05e9dc0a942836a6e283a25c64ac5e5143c8d74be7174abf7" + }, + "second_recipe": [ + 2, + 252, + 1 + ], + "selected_kappa": "0.001672660197076918", + "source_archive_sha256": "f1da80610bf33c58527f18ad63b4350d0b864e1d4486141ee17ac9bed4d529d6", + "status": "PASS_RANK1_PAIR_EQUALS_RANK2_BUT_ADD_TAP_BLOCKS_DIRECT_REPLACEMENT", + "unabsorbed_intermediate": { + "backward": { + "emitted_by_direct_rank2_recipe": false, + "max_nonzeros_per_basis_column": 2, + "nonzero_basis_inputs": 512, + "required_tap": "(-1/2)*first_rank1*L195 into L521" + }, + "forward": { + "absent_from_direct_rank2_input": true, + "max_nonzeros_per_basis_column": 2, + "nonzero_basis_inputs": 512, + "required_block": "(1/2)*second_rank1*L521" + } + } +} diff --git a/research/complex-rank2-macro-fusion-screen/SOURCES.json b/research/complex-rank2-macro-fusion-screen/SOURCES.json new file mode 100644 index 0000000000..7be5b9b1cf --- /dev/null +++ b/research/complex-rank2-macro-fusion-screen/SOURCES.json @@ -0,0 +1,24 @@ +{ + "schema_version": 1, + "base_commit": "20ecf2891d80b5acf18d980b2164fb44ea7faa90", + "files": [ + { + "path": "certificates/selected-result.json", + "usage": "input", + "git_blob": "b1fea0c94436b236cc2b7d41a7be032811b94c81", + "sha256": "bb2eaa58647b353f88d0aec2eafb9d30df1be53589231b23e12ceef3e4bad129" + }, + { + "path": "docs/research/agent-task-catalog.json", + "usage": "provenance", + "git_blob": "67f085acd86acd7d4e03922d9cf4b26bca66fed5", + "sha256": "47b42433f5a85809a3aadd29c2c339da2c6ed728e4f7762df6b88bccb7874cf0" + }, + { + "path": "research/community-frontier1094-audit/selected-replay.tar.gz", + "usage": "input", + "git_blob": "c24daf067f9c543421d89ca2eeaa4eb78a57fb6f", + "sha256": "f1da80610bf33c58527f18ad63b4350d0b864e1d4486141ee17ac9bed4d529d6" + } + ] +} diff --git a/research/complex-rank2-macro-fusion-screen/TASK.json b/research/complex-rank2-macro-fusion-screen/TASK.json new file mode 100644 index 0000000000..072728ee1d --- /dev/null +++ b/research/complex-rank2-macro-fusion-screen/TASK.json @@ -0,0 +1,72 @@ +{ + "schema_version": 1, + "task_id": "complex-rank-shape-decoder", + "priority_revision": 6, + "base_commit": "20ecf2891d80b5acf18d980b2164fb44ea7faa90", + "original_card": { + "id": "complex-rank-shape-decoder", + "question": "Can a decoder deliver the local paid mass in fewer, larger-rank children (block frame climbs instead of rank-1 to rank-3 steps)?", + "starting_points": [ + "docs/research/research-family-status.md", + "research/complex-local-word-ceiling/README.md", + "research/complex-decoder-field-extension-seed44/README.md", + "research/complex-frame-pair-decoders/README.md", + "research/selected-k8-unrestricted-ends/README.md", + "research/eisenstein-reduction-copy-floor/README.md" + ], + "methods": [ + "Find one exact small decoding identity whose frame path climbs in blocks, and price its complete per-call profile.", + "Bound the best effective child rank achievable for one fixed support alphabet, including copies, frames and restoration." + ], + "acceptance_gate": "A per-call local profile, with all copied information, frames and restoration counted, whose ceiling exceeds the selected word's by at least 5%; masked or overcomplete supports must change an assumption of the eight-term all-pairs bound. A scoped local result may be retained without a complete supplier. Expanding it into further construction rounds requires the family-level budget and obligation gate in research-family-status.md; an optimistic passing relaxation alone is not acceptance.", + "first_step": { + "command": [ + "python3", + "-B", + "research/complex-local-word-ceiling/verify.py" + ], + "expected": "PASS_COMPLEX_LOCAL_WORD_CEILING", + "inspect": "Read the selected word's per-call local profile, its packing ceiling and the lever table; run levers.py for the ideal budget of the lever you target.", + "scope": "Positive control and planning budget only; this is not a new construction or full cold replay." + }, + "first_experiment": "Before any synthesis, write the candidate per-call local profile (and centre charge / unpaid coordinates if changed) and screen it with ceiling.ceiling(); reject it if even the ideal ceiling does not beat the selected word's by your stated margin. Ideal budget: rank-16 delivery is 2.31x (levers.py).", + "bottleneck_link": "Rank-1 children are about half of recursive cost; the selected word's effective child rank is about 3.9. Larger-rank delivery lowers recursive cost per unit of paid mass.", + "allowed_variations": [ + "One masked, overcomplete or block decoder family with exact labels and supports.", + "A scoped lower bound on effective rank for one alphabet.", + "If the coverage law, template or guard changes, derive an applicable fully charged replacement bound instead of applying the incumbent ceiling outside its scope." + ], + "scope_change_triggers": [ + "Repacking idle remainders of an unchanged word (covered by the packing ceiling).", + "Repeating the eight-term all-pairs search inside its proved scope.", + "Only changing helper terminal subspaces and monotone frames within the fixed scalar word, entrances and cleanup contract already covered by #1995/#2013.", + "Searching below C=40 in the #2017 Eisenstein binary scalar-rectangle or fixed-support block-linear additive pairing model; specify a changed premise instead. This bound does not cover every complex decoder.", + "Continuing a partial cost improvement while leaving the same unpriced consumer, restoration or central-readout obligation unresolved; refresh the family expansion decision first. Local product-retiming evidence has a narrow scope and is not a ban on changed graphs or contracts." + ], + "overlap_terms": [ + "rank shape", + "block climb", + "masked decoder", + "overcomplete", + "effective rank" + ] + }, + "scope_status": "changed", + "actual_scope": "Screen the selected complex source for direct fusion of successive rank-one frame climbs into an available rank-two endpoint recipe, including exact scalar-interference windows in both orientations.", + "scope_change_reason": "The task card asks for a new block decoder and fully charged profile. This experiment screens a sequential same-register macro contract from the attachment: two rank-one climbs separated by scalar work. PR #1836 and complex-heterogeneous-child-interface instead study simultaneous unequal-rank climbs and reciprocal one-child encodings; this screen complements that work without repeating its census or claiming a profile improvement.", + "bottleneck_link": "The selected complex word contains 2,412 rank-one children, and the recursive cost is concave in child rank when a > 0. An exact two-rank-one to one-rank-two replacement would lower that pair's recursive charge by a factor 2^-a, but only if the composed operator and all intervening work are preserved.", + "budget_minutes": 60, + "stop_rule": "For the selected direct single-output rank-two contract, stop after the 512-column exact operator composition and the complete ADD tap are checked. Do not count the equality of the child product as a replacement of the full two-register map. Reopen only for a changed multi-output/tap provider with a complete interface and cost below the two rank-one calls.", + "outcome": "negative-result", + "evidence": "Both orientations contain 2,412 rank-one climbs, 1,441 successive rank-one climb links per register history, and 35 pairs with an available rank-two recipe on the combined endpoint frames. Every candidate is interrupted by same-register scalar work (histogram: 10 with one touch, 5 with two, and 20 with five); there are zero direct schedule-preserving substitutions. For the concrete L195 path 0->2->252, exact Gaussian-dyadic matrices verify on all 512 basis columns that the two rank-one child maps compose to the pinned rank-two recipe. The 597-event window contains 139 climbs and 458 scalar events; its ADD requires a nonzero cross-register block in the forward orientation and a nonzero intermediate A*x tap in the backward orientation. The single-output rank-two recipe emits neither. A separate rank-one-equivalent tap makes the ideal profile [2,1], whose charge 2^(1-a)+1 exceeds the original [1,1] charge 2 for 0(D*x+(1/2)*B*y,y) and (x,y)->(D*x,y-(1/2)*A*x); search for an explicit output encoding whose rank-two child returns the required pair of outputs, then compute the full copy, frame, precision, restoration and recursion invoice.", + "blocked_by": "The pinned CLIFFORD-RECIPES provide one 512-vector output per child. No multi-output rank-two contract, emitted tap, or physical input/output ownership for such a provider is present. A separated rank-two plus rank-one implementation is already more expensive in the recursive profile. This changed-contract question overlaps the vector-decoder proposal and is not an extension of the closed direct-replacement screen." + } + ] +} diff --git a/research/complex-rank2-macro-fusion-screen/VERIFY.json b/research/complex-rank2-macro-fusion-screen/VERIFY.json new file mode 100644 index 0000000000..954f27b34e --- /dev/null +++ b/research/complex-rank2-macro-fusion-screen/VERIFY.json @@ -0,0 +1,31 @@ +{ + "schema_version": 1, + "status": "ready", + "python": "3.12", + "requirements": [], + "inputs": [ + "certificates/selected-result.json", + "research/community-frontier1094-audit/selected-replay.tar.gz" + ], + "commands": [ + { + "argv": [ + "{python}", + "-B", + "{package}/verify.py" + ], + "timeout_seconds": 120, + "checks": "Pinned selected record, replay archive, canonical word and exact member bytes; both orientations' climb and scalar-frame replay; successive rank-one links; exact rank-two endpoint-recipe matching; same-register event-touch histogram and candidate phase pairs; no add operand has a reusable same-key rank-one transition; reconstruct the 597-event L195 window; compare exact Gaussian-dyadic child matrices on all 512 basis columns and bind the product to the direct rank-two recipe; verify nonzero forward source block/backward intermediate tap and stored operator digests; wrong-rank and removed-recipe controls; scoped no-kappa conclusion." + }, + { + "argv": [ + "{python}", + "-O", + "-B", + "{package}/verify.py" + ], + "timeout_seconds": 120, + "checks": "Repeat the complete source-pinned frame replay, exact operator and ADD-tap screen, call-reuse screen, stored result-digest comparison, and controls under optimized Python." + } + ] +} diff --git a/research/complex-rank2-macro-fusion-screen/verify.py b/research/complex-rank2-macro-fusion-screen/verify.py new file mode 100644 index 0000000000..77b47d7656 --- /dev/null +++ b/research/complex-rank2-macro-fusion-screen/verify.py @@ -0,0 +1,494 @@ +#!/usr/bin/env python3 +"""Screen direct rank-1-pair to rank-2 macro substitutions in the selected trace.""" +from __future__ import annotations + +import collections +from fractions import Fraction as Q +import gzip +import hashlib +import io +import json +import tarfile +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[2] +ARCHIVE = ROOT / "research/community-frontier1094-audit/selected-replay.tar.gz" +ARCHIVE_SHA256 = "f1da80610bf33c58527f18ad63b4350d0b864e1d4486141ee17ac9bed4d529d6" +PROGRAM_MEMBER = ( + "candidate/strict/global/base/providers/gauge44/LOCAL-PROGRAMS.json.gz" +) +PROGRAM_SHA256 = "241fde27d4e43f5a9cb6b128f9f76c5131a53d09f0371676163f914d0b0fef3a" +RECIPE_MEMBER = ( + "candidate/strict/global/base/providers/gauge44/CLIFFORD-RECIPES.json" +) +RECIPE_SHA256 = "d0768f2164d5f64a2fdbec422f1cfde4e400818792f1904ca541958377243731" +WORD_MEMBER = ( + "candidate/strict/global/base/providers/gauge44/CANONICAL-WORD.json.gz" +) +WORD_SHA256 = "16bc0ebb04b4be6c0ade74badc6a0c0cdba4215eda86e4ff9f13abf8ecebd219" +SELECTED = ROOT / "certificates/selected-result.json" +SELECTED_SHA256 = "bb2eaa58647b353f88d0aec2eafb9d30df1be53589231b23e12ceef3e4bad129" +SELECTED_KAPPA = "0.001672660197076918" +RESULT = Path(__file__).with_name("RESULT.json") +EXPECTED_TOUCH_HISTOGRAM = {1: 10, 2: 5, 5: 20} +EXPECTED_PHASE_PAIRS = { + ("A", "A"): 20, + ("B", "B"): 8, + ("cleanup_tail", "final"): 7, +} + + +def require(condition: bool, message: str) -> None: + if not condition: + raise RuntimeError(message) + + +def digest(data: bytes) -> str: + return hashlib.sha256(data).hexdigest() + + +def load_sources() -> tuple[dict[str, list], list[dict], dict, str]: + archive_bytes = ARCHIVE.read_bytes() + require(digest(archive_bytes) == ARCHIVE_SHA256, "selected archive pin") + with tarfile.open(fileobj=io.BytesIO(archive_bytes), mode="r:gz") as archive: + names = {member.name for member in archive.getmembers()} + require(PROGRAM_MEMBER in names, "selected local program present") + require(RECIPE_MEMBER in names, "selected child recipes present") + require(WORD_MEMBER in names, "selected canonical word present") + program_bytes = archive.extractfile(PROGRAM_MEMBER).read() + recipe_bytes = archive.extractfile(RECIPE_MEMBER).read() + word_bytes = archive.extractfile(WORD_MEMBER).read() + require(digest(program_bytes) == PROGRAM_SHA256, "local program member pin") + require(digest(recipe_bytes) == RECIPE_SHA256, "child recipe member pin") + require(digest(word_bytes) == WORD_SHA256, "canonical word member pin") + programs = json.loads(gzip.decompress(program_bytes)) + recipes = json.loads(recipe_bytes)["children"] + word = json.loads(gzip.decompress(word_bytes)) + require(set(programs) == {"forward", "backward"}, "both orientations present") + require(len(recipes) == 2896, "selected recipe inventory") + + keys = [(entry["old"], entry["new"], entry["rank"]) for entry in recipes] + require(len(set(keys)) == len(keys), "child recipe transitions are unique") + + selected_bytes = SELECTED.read_bytes() + require(digest(selected_bytes) == SELECTED_SHA256, "selected record pin") + selected = json.loads(selected_bytes) + require(selected["decimal"] == SELECTED_KAPPA, "selected kappa record") + return programs, recipes, word, selected["decimal"] + + +def frame_contains(frames: list[list[int]], old: int, new: int) -> bool: + for vector in frames[old]: + reduced = vector + for target in frames[new]: + if target and (reduced >> (target.bit_length() - 1)) & 1: + reduced ^= target + if reduced: + return False + return True + + +def gaussian_add(left: tuple[Q, Q], right: tuple[Q, Q]) -> tuple[Q, Q]: + return left[0] + right[0], left[1] + right[1] + + +def gaussian_mul(left: tuple[Q, Q], right: tuple[Q, Q]) -> tuple[Q, Q]: + return (left[0] * right[0] - left[1] * right[1], + left[0] * right[1] + left[1] * right[0]) + + +def gaussian_phase(value: tuple[Q, Q], exponent: int) -> tuple[Q, Q]: + real, imag = value + return ((real, imag), (-imag, real), (-real, -imag), (imag, -real))[exponent % 4] + + +def monomial_value(recipe: dict, value: int) -> tuple[int, int]: + """Apply one pinned affine-Clifford monomial, retaining its exact phase.""" + rows = recipe["matrix_rows"] + target = recipe["offset"] ^ sum( + ((row & value).bit_count() & 1) << index + for index, row in enumerate(rows) + ) + exponent = recipe["phase"] + sum( + recipe["linear_Z4"][index] + for index in range(len(rows)) if value >> index & 1 + ) + exponent += 2 * sum( + 1 for i in range(len(rows)) for j in range(i + 1, len(rows)) + if value >> i & 1 and value >> j & 1 + and recipe["quadratic_rows"][i] >> j & 1 + ) + return target, exponent % 4 + + +def child_operator(recipe: dict) -> list[dict[int, tuple[Q, Q]]]: + """Build all exact columns of left * C^rank * right over Q(i).""" + rank = recipe["rank"] + left, right = recipe["left"], recipe["right"] + dimension = len(right["matrix_rows"]) + require(len(left["matrix_rows"]) == dimension, "child monomial dimensions") + columns = [] + for source in range(1 << dimension): + middle, right_phase = monomial_value(right, source) + column: dict[int, tuple[Q, Q]] = {} + for flip in range(1 << rank): + coefficient = (Q(1), Q(0)) + for axis in range(rank): + gate_coefficient = (Q(1, 2), Q(1, 2) if not (flip >> axis & 1) + else Q(-1, 2)) + coefficient = gaussian_mul(coefficient, gate_coefficient) + target, left_phase = monomial_value(left, middle ^ flip) + value = gaussian_phase(coefficient, right_phase + left_phase) + column[target] = gaussian_add(column.get(target, (Q(0), Q(0))), value) + columns.append({target: value for target, value in column.items() + if value != (Q(0), Q(0))}) + return columns + + +def compose_operators(after: list[dict], before: list[dict]) -> list[dict]: + """Return after * before as exact sparse columns.""" + require(len(after) == len(before), "operator dimensions") + result = [] + for column in before: + output: dict[int, tuple[Q, Q]] = {} + for middle, left_value in column.items(): + for target, right_value in after[middle].items(): + output[target] = gaussian_add( + output.get(target, (Q(0), Q(0))), + gaussian_mul(right_value, left_value), + ) + result.append({target: value for target, value in output.items() + if value != (Q(0), Q(0))}) + return result + + +def operator_digest(columns: list[dict[int, tuple[Q, Q]]]) -> str: + canonical = [ + [[target, str(value[0]), str(value[1])] + for target, value in sorted(column.items())] + for column in columns + ] + return digest(json.dumps(canonical, separators=(",", ":")).encode()) + + +def verify_exact_macro_candidate(programs: dict[str, list], recipes: list[dict]) -> dict: + """Test the rank-1 pair composition and retain the intervening ADD obligation.""" + expected_climbs = ( + ["climb", "B", 195, 0, 2, 1], + ["climb", "B", 195, 2, 252, 1], + ) + expected_adds = { + "forward": ["scalar", ["add", ["L", 195], ["L", 521], 1, 2], 2], + "backward": ["scalar", ["add", ["L", 521], ["L", 195], -1, 2], 2], + } + interval_counts = {} + for orientation, events in programs.items(): + first_index, second_index = 12806, 13402 + require(events[first_index] == expected_climbs[0], + orientation + " exact first candidate event") + require(events[second_index] == expected_climbs[1], + orientation + " exact second candidate event") + require(events[first_index + 1] == expected_adds[orientation], + orientation + " exact intervening ADD") + window = events[first_index:second_index + 1] + tags = collections.Counter(event[0] for event in window) + require(tags == {"climb": 139, "scalar": 458}, + orientation + " complete candidate window") + for register, expected_indices in ((195, [first_index, first_index + 1, + second_index]), + (521, [first_index + 1])): + touches = [first_index + offset for offset, event in enumerate(window) + if local_register_touched(event, register)] + require(touches == expected_indices, + orientation + f" exact L{register} window touches") + interval_counts[orientation] = { + "events": len(window), + "climbs": tags["climb"], + "scalar_events": tags["scalar"], + "first_child": first_index, + "intervening_add": first_index + 1, + "second_child": second_index, + } + + by_key = {(entry["old"], entry["new"], entry["rank"]): entry + for entry in recipes} + first = child_operator(by_key[(0, 2, 1)]) + second = child_operator(by_key[(2, 252, 1)]) + direct = child_operator(by_key[(0, 252, 2)]) + composed = compose_operators(second, first) + require(len(composed) == len(direct) == 512, + "complete 512-vector basis covered") + mismatches = [index for index, (left, right) in enumerate(zip(composed, direct)) + if left != right] + require(not mismatches, + "successive rank-one operators equal the direct rank-two recipe") + + # The equality contracts only the product of the two child maps. The ADD + # consumes an intermediate value: forward it injects B*y into L195; backward + # it emits A*x into L521. A single-output direct recipe omits that block. + require(all(second[column] for column in range(512)), + "forward add-source contribution is nonzero on every basis vector") + require(all(first[column] for column in range(512)), + "backward intermediate tap is nonzero on every basis vector") + return { + "status": "PASS_RANK1_PAIR_EQUALS_RANK2_BUT_ADD_TAP_BLOCKS_DIRECT_REPLACEMENT", + "source_archive_sha256": ARCHIVE_SHA256, + "selected_kappa": SELECTED_KAPPA, + "candidate": { + "path": [0, 2, 252], + "child_rank": 1, + "event_indices": {"first": 12806, "add": 12807, "second": 13402}, + "windows_per_orientation": interval_counts, + }, + "operator_dimension": 512, + "basis_columns_checked": len(composed), + "composition_equals_direct_rank2": True, + "first_recipe": [0, 2, 1], + "second_recipe": [2, 252, 1], + "direct_recipe": [0, 252, 2], + "operator_sha256": { + "first_rank1": operator_digest(first), + "second_rank1": operator_digest(second), + "direct_rank2": operator_digest(direct), + "composed_rank1_pair": operator_digest(composed), + }, + "max_nonzeros_per_column": { + "first_rank1": max(map(len, first)), + "second_rank1": max(map(len, second)), + "direct_rank2": max(map(len, direct)), + }, + "unabsorbed_intermediate": { + "forward": { + "required_block": "(1/2)*second_rank1*L521", + "nonzero_basis_inputs": 512, + "max_nonzeros_per_basis_column": max(map(len, second)), + "absent_from_direct_rank2_input": True, + }, + "backward": { + "required_tap": "(-1/2)*first_rank1*L195 into L521", + "nonzero_basis_inputs": 512, + "max_nonzeros_per_basis_column": max(map(len, first)), + "emitted_by_direct_rank2_recipe": False, + }, + }, + "ideal_charge_screen": { + "original_rank_profile": [1, 1], + "split_rank2_plus_rank1_tap_profile": [2, 1], + "comparison_for_0_lt_a_lt_1": "2^(1-a)+1 > 2", + "multioutput_rank2_tap": "changed provider interface; not in pinned recipes or priced", + }, + } + + +def replay_frames(events: list, word: dict) -> None: + """Check climb order and the frame required by every scalar operation.""" + frames = word["frames"] + current = list(word["start"]) + phase = None + for event in events: + tag = event[0] + if tag == "phase": + phase = event[1] + elif tag == "climb": + _, part, register, old, new, rank = event + require(part == phase, "climb remains inside its phase") + require(current[register] == old, "rank-one climb old frame") + require(frame_contains(frames, old, new), "nested child frame") + require(rank == len(frames[new]) - len(frames[old]), + "child rank equals frame increment") + current[register] = new + elif tag == "scalar": + operation, frame = event[1], event[2] + registers = [operation[1]] + if operation[0] in ("copy", "add"): + registers.append(operation[2]) + for register in registers: + if register[0] == "L": + require(current[register[1]] == frame, + "scalar event retains its local frame") + require(current == word["final"], "terminal frame inventory") + + +def local_register_touched(event: list, register: int) -> bool: + tag = event[0] + if tag in ("climb", "adapter", "residual_adapter"): + return event[2] == register + if tag != "scalar": + return False + + operation = event[1] + kind = operation[0] + require(kind in ("scale", "add", "copy", "clear"), "known scalar event") + touched = {tuple(operation[1])} + if kind in ("add", "copy"): + touched.add(tuple(operation[2])) + return ("L", register) in touched + + +def screen_orientation( + events: list, + recipe_keys: set[tuple[int, int, int]], + target_rank: int = 2, + word: dict | None = None, +) -> dict: + if word is not None: + replay_frames(events, word) + last_by_register: dict[int, tuple[int, str, int, int, int, int]] = {} + rank_one_successors = 0 + candidates = [] + + for index, event in enumerate(events): + if event[0] != "climb": + continue + _, phase, register, old, new, rank = event + previous = last_by_register.get(register) + if previous is not None and previous[4] == old: + if previous[5] == 1 and rank == 1: + rank_one_successors += 1 + if (previous[3], new, target_rank) in recipe_keys: + intervening = [ + events[j] + for j in range(previous[0] + 1, index) + if local_register_touched(events[j], register) + ] + candidates.append({ + "first_index": previous[0], + "second_index": index, + "first_phase": previous[1], + "second_phase": phase, + "register": register, + "old": previous[3], + "middle": old, + "new": new, + "intervening_touches": intervening, + }) + last_by_register[register] = (index, phase, register, old, new, rank) + + touch_histogram = collections.Counter( + len(candidate["intervening_touches"]) for candidate in candidates + ) + phase_pairs = collections.Counter( + (candidate["first_phase"], candidate["second_phase"]) + for candidate in candidates + ) + return { + "rank_one_successors": rank_one_successors, + "candidates": candidates, + "touch_histogram": dict(sorted(touch_histogram.items())), + "phase_pairs": dict(sorted(phase_pairs.items())), + "directly_fusible": sum(not candidate["intervening_touches"] + for candidate in candidates), + "intervening_adds": [ + (candidate, event) + for candidate in candidates + for event in candidate["intervening_touches"] + if event[0] == "scalar" and event[1][0] == "add" + ], + } + + +def main() -> None: + programs, recipes, word, selected_kappa = load_sources() + recipe_keys = { + (entry["old"], entry["new"], entry["rank"]) + for entry in recipes + } + + receipts = { + orientation: screen_orientation(events, recipe_keys, word=word) + for orientation, events in programs.items() + } + for orientation, receipt in receipts.items(): + rank_one_climbs = sum( + event[0] == "climb" and event[5] == 1 + for event in programs[orientation] + ) + require(rank_one_climbs == 2412, + orientation + " selected rank-one child count") + require(receipt["rank_one_successors"] == 1441, + orientation + " rank-one successor count") + require(len(receipt["candidates"]) == 35, + orientation + " direct rank-two recipe candidates") + require(receipt["touch_histogram"] == EXPECTED_TOUCH_HISTOGRAM, + orientation + " scalar-interference histogram") + require(receipt["phase_pairs"] == EXPECTED_PHASE_PAIRS, + orientation + " candidate phase pairs") + require(receipt["directly_fusible"] == 0, + orientation + " no conflict-free direct substitution") + require(all( + event[0] == "scalar" + for candidate in receipt["candidates"] + for event in candidate["intervening_touches"] + ), orientation + " all candidate conflicts are scalar events") + + require(receipts["forward"]["touch_histogram"] + == receipts["backward"]["touch_histogram"], + "orientation screens agree") + + # Linearity suggests moving the post-add climb to the add's source. Check + # whether that register already receives the exact same child transition. + partner_matches = {"forward": [], "backward": []} + for orientation, events in programs.items(): + climb_keys_by_register = collections.defaultdict(set) + for event in events: + if event[0] == "climb" and event[5] == 1: + climb_keys_by_register[event[2]].add((event[3], event[4])) + for candidate, event in receipts[orientation]["intervening_adds"]: + operation = event[1] + target, source = operation[1], operation[2] + require(candidate["middle"] == event[2], + orientation + " addition uses the middle frame") + require(target[0] == source[0] == "L", + orientation + " addition uses local data registers") + partner = source[1] if target[1] == candidate["register"] else target[1] + transition = (event[2], candidate["new"]) + partner_matches[orientation].append( + transition in climb_keys_by_register[partner] + ) + require(len(partner_matches["forward"]) == 100, + "forward candidate/add instances") + require(len(partner_matches["backward"]) == 100, + "backward candidate/add instances") + require(not any(partner_matches["forward"] + partner_matches["backward"]), + "no add source has a reusable same-key rank-one transition") + + macro = verify_exact_macro_candidate(programs, recipes) + require(RESULT.is_file(), "exact macro result record exists") + require(json.loads(RESULT.read_text()) == macro, + "exact macro result matches full-basis replay") + + # Positive control: this exact endpoint pair has a direct rank-two recipe. + witness = (0, 27, 2) + require(witness in recipe_keys, "rank-two endpoint positive control") + witness_count = sum( + candidate["old"] == witness[0] and candidate["new"] == witness[1] + for candidate in receipts["forward"]["candidates"] + ) + require(witness_count == 2, "rank-two witness multiplicity in the forward trace") + + # Negative controls: wrong target rank rejects all candidates; deleting the + # one positive-control recipe rejects exactly its two occurrences. + wrong_rank = screen_orientation( + programs["forward"], recipe_keys, target_rank=3, word=word + ) + require(not wrong_rank["candidates"], "wrong-rank control rejects candidates") + mutated_keys = recipe_keys - {witness} + mutated = screen_orientation(programs["forward"], mutated_keys, word=word) + require(len(mutated["candidates"]) == 33, + "removed-recipe control rejects exactly two candidates") + + print( + "PASS_COMPLEX_RANK2_MACRO_FUSION_SCREEN " + "orientations=2 rank1_climbs=2412 rank1_successors=1441 " + "direct_rank2_candidates=35 intervening_adds=100 " + "reusable_same_key_partners=0 " + "blocked_by_scalar=35 directly_fusible=0 " + "touch_histogram=1:10,2:5,5:20 controls=3 " + f"pair_composition_equals_rank2={macro['basis_columns_checked']}/512 " + "intermediate_add_outputs=unabsorbed " + f"selected_kappa={selected_kappa} no_new_kappa" + ) + + +if __name__ == "__main__": + main()