Seal Phase 4AN exact continuation engineering evidence
This commit is contained in:
@@ -0,0 +1,219 @@
|
||||
"""Pure CPU comparisons of already-loaded engineering_compare run records.
|
||||
|
||||
No paths are accepted, no artifacts are opened, no GPU is imported. Structural
|
||||
or missing-required-observation errors raise ValueError. Valid but different
|
||||
records return numerical_exact=False. External seals, parent SHA authority and
|
||||
independent CSR/local-header queue reconstruction remain coordinator duties.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
|
||||
import numpy as np
|
||||
|
||||
import engineering_compare as ec
|
||||
|
||||
|
||||
SCHEMA = 'phase4an-engineering-scopes-v1'
|
||||
|
||||
|
||||
def _require(condition, message):
|
||||
if not condition:
|
||||
raise ValueError(message)
|
||||
|
||||
|
||||
def _raw_sha(value):
|
||||
return hashlib.sha256(np.ascontiguousarray(value, dtype=value.dtype.newbyteorder('<')).tobytes()).hexdigest()
|
||||
|
||||
|
||||
def _record(value, label):
|
||||
_require(type(value) is dict, label + ' must be an already-loaded run dictionary, not a path')
|
||||
try:
|
||||
ec.validate_run(value)
|
||||
summary = value['summary']
|
||||
_require(type(summary) is dict, label + ' requires summary metadata')
|
||||
steps, offset = value['steps'], value['global_tick_offset']
|
||||
_require(steps <= np.iinfo(np.int32).max, label + ' steps exceed int32 event capacity')
|
||||
for key in ('complete', 'pass_all', 'all_finite'):
|
||||
_require(summary.get(key) is True, label + ' is incomplete or failed: ' + key)
|
||||
for key in ('steps', 'completed_ticks'):
|
||||
_require(type(summary.get(key)) is int and summary[key] == steps, label + ' summary clock mismatch')
|
||||
_require(type(summary.get('nodes')) is int and summary['nodes'] == len(value['state']['v']),
|
||||
label + ' summary node count mismatch')
|
||||
_require(summary.get('precision') == 'float64' and summary.get('apl_precision') == 'float32' and
|
||||
type(summary.get('apl_delay_steps')) is int and summary['apl_delay_steps'] == 18,
|
||||
label + ' numerical metadata mismatch')
|
||||
continued = summary.get('checkpoint_continuation_used', False)
|
||||
_require(type(continued) is bool and continued == (offset > 0), label + ' continuation scope mismatch')
|
||||
declared_offset = summary.get('global_tick_offset', 0)
|
||||
_require(type(declared_offset) is int and declared_offset == offset, label + ' summary offset mismatch')
|
||||
if continued or 'global_next_tick' in summary:
|
||||
_require(type(summary.get('global_next_tick')) is int and summary['global_next_tick'] == offset + steps,
|
||||
label + ' summary global end mismatch')
|
||||
_require(ec._sha(summary.get('effective_weights_raw_sha256')), label + ' effective weight SHA is missing')
|
||||
mode = summary.get('diagnostic_mode')
|
||||
legacy = str(value.get('terminal_origin', '')).startswith('AM_')
|
||||
_require(mode in ('light', 'reference') or (mode is None and legacy), label + ' recording mode is not declared')
|
||||
observations = {'apl_traces': bool(value.get('traces')), 'state_hashes': value.get('state_hashes') is not None,
|
||||
'queues': value.get('queues') is not None}
|
||||
for name, field in (('apl_traces', 'full_APL_history_recorded'),
|
||||
('state_hashes', 'full_tick_state_hashes_recorded'), ('queues', 'full_queue_hashes_recorded')):
|
||||
if field in summary:
|
||||
_require(type(summary[field]) is bool, label + ' observation flag must be Boolean')
|
||||
if mode == 'reference' or legacy or summary.get(field) is True:
|
||||
_require(observations[name], label + ' declared reference observation missing: ' + name)
|
||||
return value, observations
|
||||
except (KeyError, TypeError, AttributeError, IndexError, OverflowError) as error:
|
||||
raise ValueError(label + ' has an invalid run-record schema') from error
|
||||
|
||||
|
||||
def _common(left, right):
|
||||
return {'same_neuron_count': len(left['state']['v']) == len(right['state']['v']),
|
||||
'same_APL_width': left['terminal']['apl_pending'].shape[1] == right['terminal']['apl_pending'].shape[1],
|
||||
'same_effective_weights': left['summary']['effective_weights_raw_sha256'] ==
|
||||
right['summary']['effective_weights_raw_sha256']}
|
||||
|
||||
|
||||
def _report(comparison, checks, observed, limitations):
|
||||
# All predicates are JSON booleans, not array scalars or success claims.
|
||||
checks = {key: bool(value) for key, value in checks.items()}
|
||||
return dict(schema=SCHEMA, comparison=comparison, numerical_exact=all(checks.values()),
|
||||
checks=checks, failed_checks=[key for key, value in checks.items() if not value],
|
||||
observed_scope=observed, limitations=list(limitations), learning_pass_claimed=False,
|
||||
complete_dynamical_equivalence_proven=False, external_authority_verified_here=False,
|
||||
independent_CSR_queue_reconstruction_performed=False)
|
||||
|
||||
|
||||
def compare_slice(candidate, monolithic):
|
||||
"""Compare candidate's absolute [start,stop) with an origin-zero full reference.
|
||||
|
||||
The final candidate V/g/ref bytes must match the reference's tick stop-1
|
||||
hashes even for light candidates. Missing light intermediate observations
|
||||
are disclosed, never replaced by fabricated zero arrays or inferred hashes.
|
||||
"""
|
||||
candidate, observed = _record(candidate, 'candidate')
|
||||
monolithic, full = _record(monolithic, 'monolithic')
|
||||
_require(monolithic['global_tick_offset'] == 0, 'Monolithic slice reference must have origin zero')
|
||||
_require(monolithic['summary'].get('diagnostic_mode') == 'reference' or
|
||||
str(monolithic.get('terminal_origin', '')).startswith('AM_'),
|
||||
'Monolithic slice source must be declared reference or loaded legacy AM')
|
||||
_require(all(full.values()), 'Monolithic slice reference needs all four APL traces, state hashes and queues')
|
||||
start, steps = candidate['global_tick_offset'], candidate['steps']
|
||||
stop = start + steps
|
||||
_require(stop <= monolithic['steps'], 'Candidate slice extends beyond monolithic reference')
|
||||
checks = _common(candidate, monolithic)
|
||||
state, whole = candidate['state'], monolithic['state']
|
||||
lo, hi = np.searchsorted(whole['step'], (start, stop))
|
||||
expected_ticks = (whole['step'][lo:hi].astype(np.int64) - start).astype(np.int32)
|
||||
expected_nodes = whole['neuron_idx'][lo:hi]
|
||||
checks['slice.event_steps'] = ec.bit_equal(state['step'], expected_ticks)
|
||||
checks['slice.event_neurons'] = ec.bit_equal(state['neuron_idx'], expected_nodes)
|
||||
counts = np.bincount(expected_nodes, minlength=len(whole['v'])).astype(np.int32)
|
||||
checks['slice.segment_counts'] = ec.bit_equal(state['counts'], counts)
|
||||
requests = monolithic['requests']
|
||||
lo, hi = np.searchsorted(requests[:, 0], (start, stop))
|
||||
expected_requests = requests[lo:hi].copy()
|
||||
expected_requests[:, 0] -= start
|
||||
checks['slice.requests_five_fields'] = ec.bit_equal(candidate['requests'], expected_requests)
|
||||
for name in ('v', 'g', 'ref'):
|
||||
checks['slice.final_' + name + '_raw_hash'] = _raw_sha(state[name]) == monolithic['state_hashes'][stop - 1][name + '_sha256']
|
||||
raw_ring = ec.reconstructed_fresh_ring(whole, stop, len(whole['v']))
|
||||
expected_ring = raw_ring[(np.arange(19) + stop % 19) % 19]
|
||||
checks['slice.normalized_spike_ring'] = ec.bit_equal(ec.normalized_ring(candidate['terminal']), expected_ring)
|
||||
traces = monolithic['traces']
|
||||
checks['slice.APL_state'] = ec.bit_equal(candidate['terminal']['apl_state'], traces['state'][stop - 1])
|
||||
pending = np.zeros((18, traces['generated'].shape[1]), dtype=np.float32)
|
||||
kept = min(stop, 18)
|
||||
pending[-kept:] = traces['generated'][stop - kept:stop]
|
||||
checks['slice.APL_pending'] = ec.bit_equal(candidate['terminal']['apl_pending'], pending)
|
||||
if observed['apl_traces']:
|
||||
for name in ec.TRACE_FILES:
|
||||
checks['slice.APL_trace.' + name] = ec.bit_equal(candidate['traces'][name], traces[name][start:stop])
|
||||
if observed['state_hashes']:
|
||||
checks['slice.per_tick_state_hashes'] = all(
|
||||
all(left[key] == right[key] for key in ec.HASH_FIELDS)
|
||||
for left, right in zip(candidate['state_hashes'], monolithic['state_hashes'][start:stop]))
|
||||
if observed['queues']:
|
||||
checks['slice.queue_order_and_acceptance'] = all(
|
||||
all(left[key] == right[key] for key in ec.QUEUE_FIELDS)
|
||||
for left, right in zip(candidate['queues'], monolithic['queues'][start:stop]))
|
||||
limitations = ['External input/seal/case/parent authority is not established by these records.',
|
||||
'Queue joint hashes contain local tick headers; their independent CSR reconstruction is not performed here.',
|
||||
'Numerical equality of this observed slice is not a learning, live-coverage or complete dynamics proof.']
|
||||
for key, label in (('apl_traces', 'APL histories'), ('state_hashes', 'intermediate V/g/ref state hashes'),
|
||||
('queues', 'per-tick queue order/acceptance')):
|
||||
if not observed[key]:
|
||||
limitations.append('Candidate ' + label + ' are unobserved; no direct comparison is claimed.')
|
||||
return _report('slice', checks, dict(start=start, stop=stop, segment_steps=steps,
|
||||
events=True, segment_counts=True, requests_five_fields=True, final_state_against_reference_tick_hash=True,
|
||||
normalized_spike_ring_from_reference_prefix_events=True, terminal_APL_state_and_pending=True,
|
||||
candidate_APL_histories=observed['apl_traces'], candidate_per_tick_state_hashes=observed['state_hashes'],
|
||||
candidate_per_tick_queue_fields=observed['queues'], queue_joint_hashes_compared=False), limitations)
|
||||
|
||||
|
||||
def compare_checkpoint(candidate, prefix):
|
||||
"""Compare equal absolute endpoints, not local phases or segment counts."""
|
||||
candidate, _ = _record(candidate, 'candidate')
|
||||
prefix, _ = _record(prefix, 'prefix')
|
||||
checks = _common(candidate, prefix)
|
||||
end = candidate['global_tick_offset'] + candidate['steps']
|
||||
reference_end = prefix['global_tick_offset'] + prefix['steps']
|
||||
checks['checkpoint.same_absolute_end'] = end == reference_end
|
||||
for name in ('v', 'g', 'ref'):
|
||||
checks['checkpoint.state.' + name] = ec.bit_equal(candidate['state'][name], prefix['state'][name])
|
||||
checks['checkpoint.normalized_spike_ring'] = ec.bit_equal(
|
||||
ec.normalized_ring(candidate['terminal']), ec.normalized_ring(prefix['terminal']))
|
||||
for name in ('apl_state', 'apl_pending'):
|
||||
checks['checkpoint.' + name] = ec.bit_equal(candidate['terminal'][name], prefix['terminal'][name])
|
||||
return _report('checkpoint', checks, dict(candidate_absolute_end=end, reference_absolute_end=reference_end,
|
||||
candidate_local_steps=candidate['steps'], reference_local_steps=prefix['steps'], terminal_v_g_ref=True,
|
||||
normalized_spike_ring=True, terminal_APL_state_and_pending=True, counts_compared=False,
|
||||
event_histories_compared=False, per_tick_state_histories_compared=False), [
|
||||
'Segment counts and cumulative prefix counts are deliberately not compared in this endpoint scope.',
|
||||
'Matching terminal records do not prove matching intermediate states or complete event histories.',
|
||||
'External input/seal/parent authority and live-carry coverage remain separate gates.'])
|
||||
|
||||
|
||||
def compare_parent_carry(candidate, parent):
|
||||
"""Check recorded short-segment carry invariants, not unobserved restoration.
|
||||
|
||||
The parent ends where the candidate starts. Slots already overwritten by
|
||||
this segment cannot be checked as initial carry from terminal data alone.
|
||||
When present, the child's applied trace directly tests consumed APL rows.
|
||||
"""
|
||||
candidate, observed = _record(candidate, 'candidate')
|
||||
parent, _ = _record(parent, 'parent')
|
||||
_require(candidate['global_tick_offset'] > 0, 'Parent-carry candidate must be a continued segment')
|
||||
checks = _common(candidate, parent)
|
||||
start, steps = candidate['global_tick_offset'], candidate['steps']
|
||||
parent_end = parent['global_tick_offset'] + parent['steps']
|
||||
checks['parent.contiguous_absolute_clock'] = parent_end == start
|
||||
restored_ring = ec.normalized_ring(parent['terminal'])
|
||||
unconsumed_spikes, unconsumed_apl = steps < 19, steps < 18
|
||||
if unconsumed_spikes:
|
||||
checks['parent.unwritten_spike_slots'] = ec.bit_equal(
|
||||
candidate['terminal']['spike_ring'][steps:], restored_ring[steps:])
|
||||
pending = parent['terminal']['apl_pending']
|
||||
if unconsumed_apl:
|
||||
checks['parent.unconsumed_APL_prefix'] = ec.bit_equal(
|
||||
candidate['terminal']['apl_pending'][:18 - steps], pending[steps:])
|
||||
consume = min(18, steps)
|
||||
if observed['apl_traces']:
|
||||
checks['parent.applied_APL_prefix'] = ec.bit_equal(candidate['traces']['applied'][:consume], pending[:consume])
|
||||
limitations = ['Initial V/g/ref/APL-state restoration is not directly observed by this terminal-only carry scope.',
|
||||
'Overwritten spike slots and consumed pending slots require the separate full-reference slice/endpoint gates.',
|
||||
'Parent manifest hashes and ancestor provenance must be independently bound by the coordinator.',
|
||||
'No queue arrival/acceptance reconstruction, live-carry or learning success is claimed here.']
|
||||
if not observed['apl_traces']:
|
||||
limitations.append('Candidate applied APL history is unobserved; consumed parent APL values are not directly compared.')
|
||||
carry_observed = unconsumed_spikes or unconsumed_apl or observed['apl_traces']
|
||||
if not carry_observed:
|
||||
limitations.append('No carry array remains directly observable in this long light segment; this scope checks clock/shape/weight identity only.')
|
||||
return _report('parent_carry', checks, dict(parent_absolute_start=parent['global_tick_offset'],
|
||||
parent_absolute_end=parent_end, candidate_absolute_start=start, candidate_absolute_end=start + steps,
|
||||
candidate_local_steps=steps, unwritten_spike_slots_checked=unconsumed_spikes,
|
||||
unwritten_spike_slot_count=max(19 - steps, 0), unconsumed_APL_prefix_checked=unconsumed_apl,
|
||||
unconsumed_APL_row_count=max(18 - steps, 0), applied_parent_APL_prefix_checked=observed['apl_traces'],
|
||||
applied_parent_APL_rows_compared=consume if observed['apl_traces'] else None,
|
||||
any_carry_values_directly_compared=carry_observed,
|
||||
initial_neural_state_restoration_directly_observed=False), limitations)
|
||||
Reference in New Issue
Block a user