Lyotherin_Tia / verification /check_constructive_system.py
PureOne's picture
Publish Lyotherin_Tia theoretical research release 1.0
5be082c verified
Raw History Blame Contribute Delete
16.5 kB
#!/usr/bin/env python3
"""Exact finite checks of the manuscript's complete restricted realization.
Twenty-four seeded streams interleave legal clamped-affine actions, positive
invertible affine evolution, transported-prefix rebases, presentation-only
evaluations, and separately sampled native-history commits. The independent
reference directly executes every semantic event and the full affine image
recipe. The candidate uses a prepared base, ordered transported residual,
periodic source decoder, and compact effect context.
Only fractions.Fraction and Python's standard library are used. These finite
mathematical checks are neither a proof for all programs nor measurements of
hardware, floating-point engines, latency, or NVIDIA software.
"""
from __future__ import annotations
import argparse
import hashlib
import json
import random
from dataclasses import dataclass
from fractions import Fraction as F
from pathlib import Path
from typing import Optional
SEED = 20261030
STREAMS = 24
EVENTS_PER_STREAM = 40
PIXELS = 7
RGB = tuple[F, F, F]
Image = tuple[RGB, ...]
def require(condition: bool, message: str) -> None:
if not condition:
raise AssertionError(message)
def clip(value: F, lo: F, hi: F) -> F:
return min(hi, max(lo, value))
@dataclass(frozen=True)
class ClampAffine:
a: F
b: F
lo: F
hi: F
def __post_init__(self) -> None:
require(self.a >= 0 and self.lo <= self.hi, "invalid action")
def __call__(self, value: F) -> F:
return clip(self.a * value + self.b, self.lo, self.hi)
# None is the expressly adjoined empty-word identity, not a finite-bound clip.
Residual = Optional[ClampAffine]
def apply(residual: Residual, value: F) -> F:
return value if residual is None else residual(value)
def compose(outer: Residual, inner: Residual) -> Residual:
if outer is None:
return inner
if inner is None:
return outer
return ClampAffine(
outer.a * inner.a,
outer.a * inner.b + outer.b,
outer(inner.lo),
outer(inner.hi),
)
def transport(residual: Residual, alpha: F, beta: F) -> Residual:
require(alpha > 0, "slow affine evolution must be invertible and positive")
if residual is None:
return None
return ClampAffine(
residual.a,
alpha * residual.b + (1 - residual.a) * beta,
alpha * residual.lo + beta,
alpha * residual.hi + beta,
)
def aggregate(word: list[tuple[int, ClampAffine]]) -> Residual:
residual: Residual = None
for _, action in word:
residual = compose(action, residual)
return residual
def source_reference(source: Image, coordinate: F) -> RGB:
"""Floor the unwrapped coordinate, then wrap the two endpoint indices."""
lower = coordinate.numerator // coordinate.denominator
weight = coordinate - lower
left, right = source[lower % len(source)], source[(lower + 1) % len(source)]
return tuple((1 - weight) * left[c] + weight * right[c] for c in range(3))
def source_candidate(source: Image, coordinate: F) -> RGB:
"""Wrap first; this also checks negative-coordinate periodic agreement."""
position = coordinate % len(source)
lower = position.numerator // position.denominator
weight = position - lower
left, right = source[lower], source[(lower + 1) % len(source)]
return tuple(left[c] + weight * (right[c] - left[c]) for c in range(3))
def matvec(matrix: tuple[RGB, RGB, RGB], value: RGB) -> RGB:
return tuple(sum((row[c] * value[c] for c in range(3)), F(0)) for row in matrix)
@dataclass(frozen=True)
class Recipe:
source: Image
scale: F
weights: tuple[F, ...]
matrix: tuple[RGB, RGB, RGB]
bias: RGB
hud: Image
hud_alpha: tuple[F, ...]
@dataclass(frozen=True)
class Context:
coefficient: tuple[F, ...]
bias: Image
history_tail: Image
def prepare(recipe: Recipe, history: Image) -> Context:
tails = tuple(
tuple((1 - recipe.weights[p]) * history[p][c] for c in range(3))
for p in range(PIXELS)
)
coefficients = tuple(
(1 - recipe.hud_alpha[p]) * recipe.weights[p] for p in range(PIXELS)
)
biases = []
for p in range(PIXELS):
graded_tail = matvec(recipe.matrix, tails[p])
biases.append(tuple(
recipe.hud[p][c]
+ (1 - recipe.hud_alpha[p]) * (graded_tail[c] + recipe.bias[c])
for c in range(3)
))
return Context(coefficients, tuple(biases), tails)
def render_reference(recipe: Recipe, history: Image, state: F) -> tuple[Image, Image]:
visible, next_history = [], []
for p in range(PIXELS):
z = source_reference(recipe.source, F(p) + recipe.scale * state)
mixed = tuple(
recipe.weights[p] * z[c] + (1 - recipe.weights[p]) * history[p][c]
for c in range(3)
)
next_history.append(mixed)
graded = tuple(
sum((recipe.matrix[c][j] * mixed[j] for j in range(3)), F(0))
+ recipe.bias[c]
for c in range(3)
)
visible.append(tuple(
recipe.hud[p][c] + (1 - recipe.hud_alpha[p]) * graded[c]
for c in range(3)
))
return tuple(visible), tuple(next_history)
def render_candidate(recipe: Recipe, context: Context, state: F) -> tuple[Image, Image]:
visible, next_history = [], []
for p in range(PIXELS):
z = source_candidate(recipe.source, F(p) + recipe.scale * state)
graded = matvec(recipe.matrix, z)
visible.append(tuple(
context.coefficient[p] * graded[c] + context.bias[p][c]
for c in range(3)
))
next_history.append(tuple(
recipe.weights[p] * z[c] + context.history_tail[p][c]
for c in range(3)
))
return tuple(visible), tuple(next_history)
class NativeHistoryOwner:
"""A mathematical single-owner/version gate, not an engine adapter."""
def __init__(self, history: Image):
self.history = history
self.version = 0
self.committed_ids: set[int] = set()
def commit(self, commit_id: int, expected_version: int, proposed: Image) -> bool:
if commit_id in self.committed_ids or expected_version != self.version:
return False
self.history = proposed
self.version += 1
self.committed_ids.add(commit_id)
return True
def random_image(rng: random.Random) -> Image:
return tuple(tuple(F(rng.randint(0, 12), 12) for _ in range(3)) for _ in range(PIXELS))
def random_recipe(rng: random.Random) -> Recipe:
source = random_image(rng)
alpha = tuple(F(rng.randint(0, 6), 6) for _ in range(PIXELS))
hud_color = random_image(rng)
hud = tuple(tuple(alpha[p] * hud_color[p][c] for c in range(3)) for p in range(PIXELS))
matrix = tuple(
tuple(F(int(i == j)) + F(rng.randint(-2, 2), 12) for j in range(3))
for i in range(3)
)
return Recipe(
source=source,
scale=F(rng.choice((-3, -2, -1, 1, 2, 3)), rng.choice((2, 3))),
weights=tuple(F(rng.randint(0, 6), 6) for _ in range(PIXELS)),
matrix=matrix,
bias=tuple(F(rng.randint(-2, 2), 24) for _ in range(3)),
hud=hud,
hud_alpha=alpha,
)
def random_action(rng: random.Random) -> ClampAffine:
lo = F(rng.randint(-12, 0), 6)
hi = F(rng.randint(0, 12), 6)
return ClampAffine(
rng.choice((F(0), F(1, 2), F(1), F(3, 2), F(2))),
F(rng.randint(-12, 12), 6), lo, hi,
)
def verify() -> dict:
rng = random.Random(SEED)
counts = {name: 0 for name in (
"actions", "slow_affine_transitions", "identity_transport_events",
"state_equalities", "native_rebases", "consumed_input_ids",
"native_captures", "native_history_commits", "duplicate_commit_rejections",
"explicit_presentations", "presentation_history_unchanged_checks",
"visible_channel_equalities", "candidate_history_channel_equalities",
"capture_vs_later_state_distinctions",
)}
stream_summaries = []
for stream in range(STREAMS):
recipe = random_recipe(rng)
reference_history = random_image(rng)
owner = NativeHistoryOwner(reference_history)
context = prepare(recipe, owner.history)
reference_state = F(rng.randint(-6, 6), 3)
base = reference_state
residual: Residual = None
pending: list[tuple[int, ClampAffine]] = []
next_input_id = 1
consumed_prefix = 0
consumed_ids: set[int] = set()
captured = None
def rebase(number: int) -> None:
nonlocal base, residual, pending, consumed_prefix
require(0 <= number <= len(pending), "invalid prefix length")
old_state = apply(residual, base)
prefix, suffix = pending[:number], pending[number:]
# Execute the transported prefix directly, rather than by its summary.
for input_id, action in prefix:
require(input_id == consumed_prefix + 1, "noncontiguous consumed prefix")
require(input_id not in consumed_ids, "duplicate input consumption")
base = action(base)
consumed_ids.add(input_id)
consumed_prefix = input_id
counts["consumed_input_ids"] += 1
pending = suffix
residual = aggregate(suffix)
require(apply(residual, base) == old_state == reference_state, "rebase changed state")
counts["native_rebases"] += 1
# One cycle samples a native candidate before newer input arrives, and
# commits that sampled candidate only at the cycle's final event.
for event in range(EVENTS_PER_STREAM):
phase, cycle = event % 10, event // 10
if phase in (0, 3, 8):
action = random_action(rng)
reference_state = action(reference_state)
residual = compose(action, residual)
pending.append((next_input_id, action))
next_input_id += 1
counts["actions"] += 1
elif phase in (1, 6):
alpha = rng.choice((F(1, 2), F(2, 3), F(1), F(3, 2), F(2)))
beta = F(rng.randint(-6, 6), 6)
reference_state = alpha * reference_state + beta
base = alpha * base + beta
if residual is None:
counts["identity_transport_events"] += 1
residual = transport(residual, alpha, beta)
pending = [
(input_id, transport(action, alpha, beta))
for input_id, action in pending
]
counts["slow_affine_transitions"] += 1
elif phase == 2:
require(captured is None, "native capture overwritten before commit")
_, reference_next = render_reference(recipe, reference_history, reference_state)
_, candidate_next = render_candidate(recipe, context, apply(residual, base))
require(reference_next == candidate_next, "sampled native histories differ")
captured = (cycle, owner.version, reference_next, candidate_next, reference_state)
counts["native_captures"] += 1
elif phase in (4, 7):
counts["explicit_presentations"] += 1
elif phase == 5:
number = len(pending) if (stream + cycle) % 2 == 0 else max(1, len(pending) // 2)
rebase(number)
elif phase == 9:
require(captured is not None, "native commit has no sampled candidate")
commit_id, expected, reference_next, candidate_next, capture_state = captured
if capture_state != reference_state:
counts["capture_vs_later_state_distinctions"] += 1
reference_history = reference_next
require(owner.commit(commit_id, expected, candidate_next), "valid native commit rejected")
require(owner.history == reference_history, "native history continuation differs")
counts["native_history_commits"] += 1
committed_state = (owner.history, owner.version)
require(not owner.commit(commit_id, expected, candidate_next), "duplicate history commit accepted")
require((owner.history, owner.version) == committed_state, "rejected commit changed history")
counts["duplicate_commit_rejections"] += 1
context = prepare(recipe, owner.history)
captured = None
candidate_state = apply(residual, base)
require(candidate_state == reference_state, "transported state differs from direct execution")
counts["state_equalities"] += 1
before = (owner.history, owner.version)
reference_image, reference_next = render_reference(recipe, reference_history, reference_state)
candidate_image, candidate_next = render_candidate(recipe, context, candidate_state)
require(reference_image == candidate_image, "compact image differs from complete recipe")
require(reference_next == candidate_next, "candidate next history differs")
require((owner.history, owner.version) == before, "presentation advanced native history")
counts["presentation_history_unchanged_checks"] += 1
counts["visible_channel_equalities"] += PIXELS * 3
counts["candidate_history_channel_equalities"] += PIXELS * 3
rebase(len(pending))
require(residual is None and base == reference_state, "final catch-up did not reach direct state")
require(consumed_prefix == next_input_id - 1, "final catch-up lost an input")
require(owner.version == 4 and len(owner.committed_ids) == 4, "wrong native-history cadence")
require(captured is None, "uncommitted native candidate")
digest = hashlib.sha256(json.dumps(
[[str(value) for value in pixel] for pixel in owner.history],
separators=(",", ":"),
).encode()).hexdigest()
stream_summaries.append({
"stream": stream,
"final_state": str(reference_state),
"consumed_input_prefix": consumed_prefix,
"native_history_version": owner.version,
"native_history_sha256": digest,
})
require(counts["identity_transport_events"] > 0, "identity transport not exercised")
require(counts["capture_vs_later_state_distinctions"] > 0, "native sampling boundary not distinguished")
return {
"status": "passed",
"scope": "Finite integrated mathematical witness for the declared periodic-strip model",
"arithmetic": "fractions.Fraction; exact rational arithmetic",
"seed": SEED,
"streams": STREAMS,
"events_per_stream": EVENTS_PER_STREAM,
"total_stream_events": STREAMS * EVENTS_PER_STREAM,
"pixels_per_image": PIXELS,
"counts": counts,
"reference": "Direct ordered state updates and full temporal/grade/HUD recipe",
"candidate": "Transported clamped-affine residual, prefix rebase, periodic decoder, compact context",
"native_history_rule": "One commit per sampled native boundary; intervening presentations cannot commit",
"limitations": [
"Finite examples supplement the analytical proofs; they are not exhaustive for all states.",
"Exact rational equality does not imply equivalence to IEEE engine arithmetic.",
"Periodic static texture and frozen affine effects do not cover general 3D visibility.",
"No game process, GPU, physical input, optical detector, or NVIDIA method was tested.",
"No latency or hardware-performance reduction is measured or asserted.",
],
"stream_summaries": stream_summaries,
}
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--output", type=Path, default=Path(__file__).with_name("constructive_system_validation.json"))
args = parser.parse_args()
result = verify()
args.output.write_text(json.dumps(result, indent=2, sort_keys=True) + "\n", encoding="utf-8")
print(json.dumps({key: result[key] for key in ("status", "streams", "total_stream_events", "counts")}, indent=2))
if __name__ == "__main__":
main()