"""Resynthesis RBO (Recursive Bidirectional Orchestrator). The reasoning brain: forward embed → retrieve → route → experts → blend; backward feedback → mutation → reinforce → stop. UNCAPPED: all traversal and generation termination is confidence-based and trained. There are NO step/hop/token caps anywhere — not in defaults, not in env vars, not in safety valves. The trained additive NoNE/RBO/Fabric reasoning-readiness surface owns text completion. The exact frozen parent is only vocabulary projection and feature plumbing; its historical logits and decode-confidence surface are diagnostic and cannot select, veto, retain, or stop an answer. The model learns when to stop via: 1. NeuralStopGate (LSTM trajectory encoder — domain-specific stopping) 2. Learnable utility/contradiction sigmoid thresholds (trained from outcomes) 3. Additive NoNE/RBO/Fabric confidence over the generated trajectory. EOS is observed as a token, never used as stop authority. Pillar 17: forward_thinking NEVER receives the token currently being predicted. Training replays the same growing autoregressive prefix used by generation; targets enter loss-boundary methods on the head logits only. """ from __future__ import annotations import contextlib import copy import hashlib import json import math import os import re import threading from concurrent.futures import ThreadPoolExecutor from dataclasses import dataclass, replace from pathlib import Path from typing import TYPE_CHECKING, Any, Callable, Final, Mapping, cast import torch import torch.nn as nn import torch.nn.functional as F from torch.utils.checkpoint import checkpoint if TYPE_CHECKING: from resynthesis.base_loader import NativeSharedPrefixTrainingPacket from resynthesis.none_migration import NoNETrainingAuthority from resynthesis.none_paging import ( NoNECandidateVJPCheckpointPacket, NoNEGenerationBinding, NoNEGraphAuthorityBinding, NoNEImmutablePageStore, NoNEPageBundle, NoNEPagedExpertRuntime, NoNETrainingBranchScopePacket, ) from resynthesis.release_runtime import ReleaseInferenceAuthority from resynthesis.base_forward_cache import ( BaseForwardCache, BaseForwardCacheEntry, FrozenBackbonePrefillPacket, base_forward_cache_allowed_boundary, base_forward_cache_capacity_boundary, base_forward_cache_enabled_boundary, base_forward_cache_key_boundary, ) from resynthesis.causal_algebra import ( CausalTheoryProofPacket, CausalWorldState, ) from resynthesis.causal_integration_tensor import ( CAUSAL_INTEGRATION_TENSOR_ASSURANCE_V3_LOCAL_STATE_NAMES, CAUSAL_INTEGRATION_TENSOR_CAUSAL_V1_LOCAL_STATE_NAMES, CAUSAL_INTEGRATION_TENSOR_EXPLORATION_META_V2_LOCAL_STATE_NAMES, CAUSAL_INTEGRATION_TENSOR_KNOWLEDGE_TRANSFER_V4_LOCAL_STATE_NAMES, CAUSAL_INTEGRATION_TENSOR_LOCAL_STATE_NAMES, CAUSAL_INTEGRATION_TENSOR_MHC_V5_LOCAL_STATE_NAMES, CAUSAL_INTEGRATION_TENSOR_OUTCOME_V1_LOCAL_STATE_NAMES, CAUSAL_INTEGRATION_TENSOR_V1_LOCAL_STATE_NAMES, CausalIntegrationOutput, ) from resynthesis.config import ( NATIVE_ATTENTION_POSITION_APERTURE, RESYNTHESIS_NATIVE_PREFILL_TILE_TOKENS, RESYNTHESIS_NATIVE_CONTEXT_FLOOR, RESYNTHESIS_ONLINE_SOFTMAX_TILE_TOKENS, ResynthesisConfig, ) from resynthesis.language_catalog import ( BROAD_LANGUAGE_PACK_IDS_SHA256, LANGUAGE_ABILITY_AXIS_IDS, LANGUAGE_ABILITY_AXIS_IDS_SHA256, LANGUAGE_ABILITY_SET_SCHEMA, LINGUIST_LANGUAGE_SOURCE_COMMIT, LINGUIST_LANGUAGE_SOURCE_FILE_SHA256, LINGUIST_LANGUAGE_SOURCE_NAMES_SHA256, LINGUIST_LANGUAGE_SOURCE_PACK_IDS_SHA256, LINGUIST_LANGUAGE_SOURCE_PATH, LINGUIST_LANGUAGE_SOURCE_RECORD_COUNT, LINGUIST_LANGUAGE_SOURCE_REPOSITORY, LINGUIST_LANGUAGE_SOURCE_SCHEMA, NATIVE_LANGUAGE_PACK_PREFIX_IDS, NATIVE_LANGUAGE_PACK_PREFIX_IDS_SHA256, NATIVE_LANGUAGE_PACK_PREFIX_SCHEMA, ) from resynthesis.packed_token_rows import ( AllKnowledgeTargetFreeCalibrationPromptPacket, validated_target_free_calibration_prompt_packet_boundary, ) from resynthesis.language_experts import ( NONE_LANGUAGE_EXPERT_ABILITY_ASSIGNMENTS_SHA256, NONE_LANGUAGE_EXPERT_CATALOG_SCHEMA, NONE_LANGUAGE_EXPERT_FAMILIES, NONE_LANGUAGE_EXPERT_IDS_SHA256, NONE_LANGUAGE_EXPERT_INHERITED_CATALOG_SCHEMA, NONE_LANGUAGE_EXPERT_INHERITED_PREFIX_COUNT, NONE_LANGUAGE_EXPERT_INHERITED_PREFIX_IDS_SHA256, ) from resynthesis.molecular_geometry import ( MOLECULAR_INITIALIZATION_SCHEME, MolecularInputPacket, ) from resynthesis.parent_bridge import ( DRAFTING_LIFECYCLE_NAMES, NoNEDraftingLifecyclePacket, ResynthesisNoNEFabric, ) from resynthesis.quantile_balancing import ( ANTI_THOMPSON_FAIL_COUNTS_BUFFER_SUFFIX, ANTI_THOMPSON_FAIL_COUNTS_INITIALIZATION_SCHEME, QuantileBalancingRouter, QuantileBalancingStepSnapshot, ) from resynthesis.science_layers import ( FUNCTIONAL_CAPABILITY_INITIALIZATION_SCHEME, LANGUAGE_ABILITY_ROUTING_INITIALIZATION_SCHEME, ResynthesisScienceLayerConfig, ResynthesisScienceLayerStack, ScienceStackResult, ScienceTraversalState, _deterministic_xavier_tensor, adapt_attention_state_to_context_relation, adapt_native_attention_expert_state, build_resynthesis_science_stack, online_softmax_last_token_pool, ) from resynthesis.scientific_experts import ( NONE_SCIENCE_SPECIALIST_CATALOG_SCHEMA, NONE_V2_PLUS_SCIENCE_SPECIALIST_DEFINITIONS, NONE_V2_PLUS_SCIENCE_SPECIALIST_IDS_SHA256, ) # Stop reasons (tensor-native: encoded as int, no string keys on hot path). STOP_REASON_NONE = 0 STOP_REASON_REASONING_READY = 1 STOP_REASON_CONTRADICTION_RESOLVED = 2 STOP_REASON_PROOF_VERIFIED = 3 STOP_REASON_SELF_CORRECTION_PLATEAU = 4 STOP_REASON_CONFIDENCE_GATE = 5 # Authority value zero is retained for checkpoint/receipt compatibility, but it # now means the active Resynthesis additive completion graph. Values 1 and 2 # are historical diagnostic identities only; no current execution path may # select them as an answer or stopping authority. STOP_AUTHORITY_ADDITIVE_TELEMETRY = 0 STOP_AUTHORITY_PARENT_NATIVE = 1 STOP_AUTHORITY_TRAINED_FUSION = 2 STOP_AUTHORITY_SUCCESSOR_CANDIDATE = 3 STOP_AUTHORITY_SUCCESSOR_RETAINED = 4 # The completion objective is shared by scalar emission training and the # packed-bulk calibration arm. Keeping one coefficient prevents the packed # path from silently learning a different stop policy than native generation. COMPLETION_STOP_TRAINING_WEIGHT = 0.15 COMPOSED_ADDITIVE_LINEAGE_SCHEMA = "nnf.resynthesis.composed_additive_lineage.v29" NATIVE_ATTENTION_CHECKPOINT_GEOMETRY_FIELD = ( "nativeAttentionCheckpointGeometryChanged" ) LEGACY_NATIVE_ATTENTION_CHECKPOINT_GEOMETRY_FIELD = ( "k" + "3NativeCheckpointGeometryChanged" ) ADDITIVE_CHECKPOINT_SCHEMA = "nnf.resynthesis.additive_state.v1" ADDITIVE_BRANCH_DELTA_SCHEMA = "nnf.resynthesis.additive_branch_delta.v1" ADDITIVE_BRANCH_CAUSAL_DELTA_SCHEMA = ( "nnf.resynthesis.additive_branch_delta.v2" ) RECONCILED_ADDITIVE_KNOWLEDGE_PROOF_SCHEMA = ( "nnf.resynthesis.reconciled_additive_knowledge.v1" ) RECONCILED_ADDITIVE_CALIBRATION_PENDING_SCHEMA = ( "nnf.resynthesis.reconciled_additive_calibration_pending.v1" ) RECONCILED_ADDITIVE_CALIBRATION_PROOF_SCHEMA = ( "nnf.resynthesis.reconciled_additive_calibration.v2" ) VOCABULARY_TRANSFER_INITIALIZATION_SCHEME = ( "deterministic_gap_down_zero_lexical_up_identity_v1" ) HISTORICAL_ADDITIVE_KNOWLEDGE_PROOF_SCHEMA = ( "nnf.resynthesis.historical_additive_knowledge.v1" ) HISTORICAL_ADDITIVE_OPTIMIZER_PROOF_SCHEMA = ( "nnf.resynthesis.historical_additive_optimizer.v1" ) _RECONCILED_DIRECT_PAGE_PREFIX_END = 1_020 _RECONCILED_DIRECT_PAGE_SCIENCE_START = 30_000 _RECONCILED_DIRECT_PAGE_SCIENCE_END = 110_582 _RECONCILED_DIRECT_PAGE_COUNT = 81_602 _RECONCILED_DIRECT_GRAPH_LAYER_COUNT = 18 _RECONCILED_PAGED_RUNTIME_BUFFER_PATTERN = re.compile( r"^(science_stack\.science_layer_(\d+)\.paged_expert_runtime\.)" r"(accepted_route_count_t|accepted_gradient_update_count_t|" r"accepted_gradient_norm_t|accepted_parameter_delta_norm_t|" r"accepted_gradient_signature_t)$" ) _RECONCILED_GRADIENT_SIGNATURE_WIDTH = 8 _RECONCILED_DENSE_ROUTER_BIAS_PATTERN = re.compile( r"^science_stack\.science_layer_(\d+)\." r"quantile_router\.expert_bias_t$" ) _RECONCILED_PAGED_ROUTER_BIAS_PATTERN = re.compile( r"^science_stack\.science_layer_(\d+)\." r"paged_expert_runtime\.router\.quantile_router\.expert_bias_t$" ) _RECONCILED_MONOTONIC_PAGED_BUFFER_SUFFIXES = frozenset( { "accepted_route_count_t", "accepted_gradient_update_count_t", "accepted_gradient_norm_t", "accepted_parameter_delta_norm_t", } ) _RECONCILED_SCOPED_PAGED_BUFFER_SUFFIXES = frozenset( { "accepted_gradient_update_count_t", "accepted_gradient_norm_t", "accepted_parameter_delta_norm_t", "accepted_gradient_signature_t", } ) _HISTORICAL_RECONCILIATION_TRANSIENT_BUFFER_NAMES: Final[ frozenset[str] ] = frozenset( { "completion_successor_retention_passed", "fabric._last_parent_route_conditioning", "fabric._parent_route_uses", "fabric._session_step", } ) FUNCTIONAL_GRAPH_PARENT_GENESIS_SCHEMA = ( "nnf.resynthesis.functional_graph_parent_genesis.v1" ) CAUSAL_CAPABILITY_INTEGRATION_INITIALIZATION_SCHEME = ( "default_intent_zero_value_causal_exploration_calibration_v1" ) EXPLORATION_META_INTEGRATION_INITIALIZATION_SCHEME = ( "default_value_confidence_strategy_meta_controller_v1" ) EXPLORATION_OUTCOME_INTEGRATION_INITIALIZATION_SCHEME = ( "default_anti_thompson_outcome_registry_v1" ) ASSURANCE_INTEGRATION_INITIALIZATION_SCHEME = ( "default_ccl_kla_rla_consensus_context_trauma_milt_v1" ) KNOWLEDGE_TRANSFER_INITIALIZATION_SCHEME = ( "deterministic_tensor_native_anchor_distill_transfer_residual_v1" ) CAUSAL_CONTRASTIVE_MHC_INITIALIZATION_SCHEME = ( "proof_outcome_posterior_sinkhorn_mhc_v1" ) LOGIT_RESIDUAL_RANK_GROWTH_INITIALIZATION_SCHEME = ( "deterministic_prefix_preserving_logit_residual_rank_growth_v1" ) _CAUSAL_CAPABILITY_INTEGRATION_STATE_PREFIX = ( "science_stack.capability_integration." ) _CAUSAL_CAPABILITY_CORE_V24_STATE_NAMES = frozenset( f"{_CAUSAL_CAPABILITY_INTEGRATION_STATE_PREFIX}{name}" for name in CAUSAL_INTEGRATION_TENSOR_CAUSAL_V1_LOCAL_STATE_NAMES ) _EXPLORATION_OUTCOME_V24_STATE_NAMES = frozenset( f"{_CAUSAL_CAPABILITY_INTEGRATION_STATE_PREFIX}{name}" for name in CAUSAL_INTEGRATION_TENSOR_OUTCOME_V1_LOCAL_STATE_NAMES ) _CAUSAL_CAPABILITY_INTEGRATION_V24_STATE_NAMES = frozenset( f"{_CAUSAL_CAPABILITY_INTEGRATION_STATE_PREFIX}{name}" for name in CAUSAL_INTEGRATION_TENSOR_V1_LOCAL_STATE_NAMES ) _EXPLORATION_META_INTEGRATION_V25_STATE_NAMES = frozenset( f"{_CAUSAL_CAPABILITY_INTEGRATION_STATE_PREFIX}{name}" for name in ( CAUSAL_INTEGRATION_TENSOR_EXPLORATION_META_V2_LOCAL_STATE_NAMES ) ) _ASSURANCE_INTEGRATION_V26_STATE_NAMES = frozenset( f"{_CAUSAL_CAPABILITY_INTEGRATION_STATE_PREFIX}{name}" for name in ( CAUSAL_INTEGRATION_TENSOR_ASSURANCE_V3_LOCAL_STATE_NAMES ) ) _KNOWLEDGE_TRANSFER_INTEGRATION_V27_STATE_NAMES = frozenset( f"{_CAUSAL_CAPABILITY_INTEGRATION_STATE_PREFIX}{name}" for name in ( CAUSAL_INTEGRATION_TENSOR_KNOWLEDGE_TRANSFER_V4_LOCAL_STATE_NAMES ) ) _CAUSAL_CONTRASTIVE_MHC_V28_STATE_NAMES = frozenset( f"{_CAUSAL_CAPABILITY_INTEGRATION_STATE_PREFIX}{name}" for name in CAUSAL_INTEGRATION_TENSOR_MHC_V5_LOCAL_STATE_NAMES ) _KNOWLEDGE_TRANSFER_DOWN_WEIGHT_NAME = ( f"{_CAUSAL_CAPABILITY_INTEGRATION_STATE_PREFIX}" "knowledge_transfer.transfer_bank.transfer_proj_down.weight" ) _KNOWLEDGE_TRANSFER_UP_WEIGHT_NAME = ( f"{_CAUSAL_CAPABILITY_INTEGRATION_STATE_PREFIX}" "knowledge_transfer.transfer_bank.transfer_proj_up.weight" ) _LOGIT_RESIDUAL_DOWN_WEIGHT_NAME = "logit_residual_down.weight" _LOGIT_RESIDUAL_UP_WEIGHT_NAME = "logit_residual_up.weight" _CAUSAL_CAPABILITY_INTEGRATION_STATE_NAMES = frozenset( f"{_CAUSAL_CAPABILITY_INTEGRATION_STATE_PREFIX}{name}" for name in CAUSAL_INTEGRATION_TENSOR_LOCAL_STATE_NAMES ) BRANCH_CAUSAL_MOVING_GRAPH_BUFFER_GROWTH_SCHEMA = ( "nnf.resynthesis.branch_causal_moving_graph_buffer_growth.v1" ) CAUSAL_PARENT_INITIALIZATION_SCHEMA = ( "nnf.resynthesis.causal_parent_initialization.role_seeded.v1" ) CAUSAL_PARENT_GENESIS_LINEAGE_SCHEMA = ( "nnf.resynthesis.causal_parent_genesis_lineage.v1" ) BRANCH_CAUSAL_PARENT_AUTHORITY_RESIDENT = "resident_exact_v1" BRANCH_CAUSAL_PARENT_AUTHORITY_GENESIS = "all_absent_genesis_v1" _CAUSAL_WORLD_GRAPH_STATE_PREFIX = ( "science_stack.causal_algebra_world_graph." ) BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES = frozenset( { "science_stack.causal_algebra_world_graph.domain_basis_t", "science_stack.causal_algebra_world_graph.domain_transfer_logits_t", "science_stack.causal_algebra_world_graph.domain_transfer_scale", "science_stack.causal_algebra_world_graph.prior_carry_scale", "science_stack.causal_algebra_world_graph.likelihood_scale", "science_stack.causal_algebra_world_graph.action_read_t", "science_stack.causal_algebra_world_graph.action_write_t", "science_stack.causal_algebra_world_graph.action_bias_t", "science_stack.causal_algebra_world_graph.action_cost_logit_t", "science_stack.causal_algebra_world_graph.exploration_influence_scale", "science_stack.causal_algebra_world_graph.world_residual_scale", "science_stack.causal_algebra_world_graph.rbo_stop_influence_scale", "science_stack.causal_algebra_world_graph.evidence_norm.weight", "science_stack.causal_algebra_world_graph.evidence_norm.bias", "science_stack.causal_algebra_world_graph.ontology_seed.weight", "science_stack.causal_algebra_world_graph.rule_seed.weight", "science_stack.causal_algebra_world_graph.pathway_seed.weight", "science_stack.causal_algebra_world_graph.hypothesis_prior.weight", "science_stack.causal_algebra_world_graph.hypothesis_prior.bias", "science_stack.causal_algebra_world_graph.domain_source.weight", "science_stack.causal_algebra_world_graph.domain_source.bias", ( "science_stack.causal_algebra_world_graph." "compiler.primitive_read_t" ), ( "science_stack.causal_algebra_world_graph." "compiler.primitive_write_t" ), ( "science_stack.causal_algebra_world_graph." "compiler.primitive_bias_t" ), ( "science_stack.causal_algebra_world_graph." "compiler.program_step_gate_t" ), ( "science_stack.causal_algebra_world_graph." "compiler.program_query.weight" ), ( "science_stack.causal_algebra_world_graph." "compiler.program_query.bias" ), "science_stack.causal_algebra_world_graph.action_policy.weight", "science_stack.causal_algebra_world_graph.action_policy.bias", ( "science_stack.causal_algebra_world_graph." "observation_hidden.weight" ), ( "science_stack.causal_algebra_world_graph." "observation_action.weight" ), ( "science_stack.causal_algebra_world_graph." "contradiction_head.weight" ), ( "science_stack.causal_algebra_world_graph." "contradiction_head.bias" ), "science_stack.causal_algebra_world_graph.commitment_head.weight", "science_stack.causal_algebra_world_graph.commitment_head.bias", "science_stack.causal_algebra_world_graph.reopen_head.weight", "science_stack.causal_algebra_world_graph.reopen_head.bias", ( "science_stack.causal_algebra_world_graph." "experiment_policy.weight" ), ( "science_stack.causal_algebra_world_graph." "experiment_policy.bias" ), "science_stack.causal_algebra_world_graph.working_memory_input.weight", "science_stack.causal_algebra_world_graph.working_memory_input.bias", "science_stack.causal_algebra_world_graph.working_memory_gate.weight", "science_stack.causal_algebra_world_graph.working_memory_gate.bias", "science_stack.causal_algebra_world_graph.counter_thompson_head.weight", "science_stack.causal_algebra_world_graph.counter_thompson_head.bias", "science_stack.causal_algebra_world_graph.hill_climb_head.weight", "science_stack.causal_algebra_world_graph.hill_climb_head.bias", "science_stack.causal_algebra_world_graph.token_world.weight", "science_stack.causal_algebra_world_graph.world_hidden.weight", "science_stack.causal_algebra_world_graph.proof_verifier.weight", "science_stack.causal_algebra_world_graph.proof_verifier.bias", "science_stack.causal_algebra_world_graph.continuation_head.weight", "science_stack.causal_algebra_world_graph.continuation_head.bias", } ) _CAUSAL_WORLD_GRAPH_BUFFER_NAMES: Final[frozenset[str]] = frozenset( { f"{_CAUSAL_WORLD_GRAPH_STATE_PREFIX}accepted_gradient_norm_t", ( f"{_CAUSAL_WORLD_GRAPH_STATE_PREFIX}" "accepted_gradient_update_count_t" ), ( f"{_CAUSAL_WORLD_GRAPH_STATE_PREFIX}" "accepted_parameter_delta_norm_t" ), f"{_CAUSAL_WORLD_GRAPH_STATE_PREFIX}cold_reload_verified_t", ( f"{_CAUSAL_WORLD_GRAPH_STATE_PREFIX}" "cross_domain_transfer_verified_t" ), f"{_CAUSAL_WORLD_GRAPH_STATE_PREFIX}falsification_verified_t", ( f"{_CAUSAL_WORLD_GRAPH_STATE_PREFIX}" "promotion_candidate_lineage_t" ), ( f"{_CAUSAL_WORLD_GRAPH_STATE_PREFIX}" "promotion_cohort_identity_t" ), f"{_CAUSAL_WORLD_GRAPH_STATE_PREFIX}promotion_generation_t", ( f"{_CAUSAL_WORLD_GRAPH_STATE_PREFIX}" "promotion_gradient_floor_t" ), ( f"{_CAUSAL_WORLD_GRAPH_STATE_PREFIX}" "promotion_heldout_identity_t" ), ( f"{_CAUSAL_WORLD_GRAPH_STATE_PREFIX}" "promotion_qualification_identity_t" ), ( f"{_CAUSAL_WORLD_GRAPH_STATE_PREFIX}" "promotion_transaction_identity_t" ), ( f"{_CAUSAL_WORLD_GRAPH_STATE_PREFIX}" "required_domain_action_coverage_t" ), f"{_CAUSAL_WORLD_GRAPH_STATE_PREFIX}retained_heldout_t", ( f"{_CAUSAL_WORLD_GRAPH_STATE_PREFIX}" "verified_abstraction_cohort_identity_t" ), ( f"{_CAUSAL_WORLD_GRAPH_STATE_PREFIX}" "verified_abstraction_count_t" ), ( f"{_CAUSAL_WORLD_GRAPH_STATE_PREFIX}" "verified_abstraction_gradient_update_count_t" ), ( f"{_CAUSAL_WORLD_GRAPH_STATE_PREFIX}" "verified_abstraction_transfer_t" ), ( f"{_CAUSAL_WORLD_GRAPH_STATE_PREFIX}" "verified_coupled_gain_delta_t" ), ( f"{_CAUSAL_WORLD_GRAPH_STATE_PREFIX}" "verified_domain_action_coverage_t" ), ( f"{_CAUSAL_WORLD_GRAPH_STATE_PREFIX}" "verified_native_knowledge_ownership_gain_t" ), f"{_CAUSAL_WORLD_GRAPH_STATE_PREFIX}verified_operator_count_t", ( f"{_CAUSAL_WORLD_GRAPH_STATE_PREFIX}" "verified_operator_signature_t" ), ( f"{_CAUSAL_WORLD_GRAPH_STATE_PREFIX}" "verified_self_correction_gain_t" ), } ) BRANCH_FUNCTIONAL_GRAPH_OWNERSHIP_SCHEMA = ( "nnf.resynthesis.branch_functional_graph_parameter_ownership.v1" ) BRANCH_FUNCTIONAL_GRAPH_OWNER_ROLES: Final[tuple[str, ...]] = ( "causal_working_memory_hill_climb", "fabric_intent_stop_acquisition", "feedback_correction_nla_adapters", "shared_science_routing_knowledge_transfer", ) def _normalize_historical_parent_public_identity_boundary( value: object, reference: dict[str, Any], ) -> object: """Internalize one exact pre-rename parent receipt. Accepted checkpoints may contain inherited artifact identities in the old generic parent fields. Migration is permitted only when all three values exactly match the explicitly retained ``historicalInherited*`` audit fields in the current reference. Partial audit metadata and every conflicting identity remain strict failures. """ if not isinstance(value, dict): return value record = cast(dict[str, Any], value) source_parent = record.get("parent") target_parent = reference.get("parent") if not isinstance(source_parent, dict) or not isinstance( target_parent, dict, ): return value field_pairs = ( ("checkpointId", "historicalInheritedCheckpointId"), ("composition", "historicalInheritedComposition"), ("modelType", "historicalInheritedModelType"), ) if not all( isinstance(target_parent.get(public_field), str) and bool(target_parent[public_field]) and isinstance(target_parent.get(historical_field), str) and bool(target_parent[historical_field]) for public_field, historical_field in field_pairs ): return value if any( historical_field in source_parent for _public_field, historical_field in field_pairs ): return value if any( source_parent.get(public_field) != target_parent.get(historical_field) for public_field, historical_field in field_pairs ): return value migrated_parent = dict(cast(dict[str, Any], source_parent)) for public_field, historical_field in field_pairs: inherited_value = migrated_parent[public_field] migrated_parent[historical_field] = inherited_value migrated_parent[public_field] = target_parent[public_field] migrated = dict(record) migrated["parent"] = migrated_parent return migrated _BRANCH_FUNCTIONAL_GRAPH_OWNER_ONE_PREFIXES: Final[tuple[str, ...]] = ( "fabric.", "stop_gate.", "acquisition_encoder.", "acquisition_policy.", "science_stack.capability_integration.intent_scorer.", ) _BRANCH_FUNCTIONAL_GRAPH_OWNER_TWO_PREFIXES: Final[tuple[str, ...]] = ( "feedback_head.", "prior_hidden_proj.", "outcome_encoder.", "parent_outcome_encoder.", "correction_context_norm.", "correction_hidden_up.", "correction_trigger_head.", "task_confidence_head.", "delegation_head.", "correction_expert_head.", "correction_layer_head.", "logit_residual_down.", "logit_residual_up.", ) _BRANCH_FUNCTIONAL_GRAPH_OWNER_TWO_EXACT: Final[frozenset[str]] = frozenset( {"nla_confidence_scale"} ) def _branch_functional_graph_anchor_owner_boundary( name: str, ) -> int | None: """Return the semantic anchor owner, leaving dense components for packing.""" if not isinstance(name, str) or not name or name.startswith("base."): return None if name in BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES: return 0 if name.startswith(_BRANCH_FUNCTIONAL_GRAPH_OWNER_ONE_PREFIXES): return 1 if ( name in _BRANCH_FUNCTIONAL_GRAPH_OWNER_TWO_EXACT or name.startswith(_BRANCH_FUNCTIONAL_GRAPH_OWNER_TWO_PREFIXES) ): return 2 return None def branch_functional_graph_parameter_owner_boundary( name: str, ) -> int | None: """Return a total semantic owner for every additive parameter name. The authoritative geometry-aware map below redistributes unanchored dense components across all four owners. This name-only boundary remains total for audits and diagnostics: only the hollow parent ``base.*`` namespace is excluded, while an unanchored additive tensor belongs to the shared science/knowledge role until its real geometry is available for packing. """ if not isinstance(name, str) or not name or name.startswith("base."): return None anchor_owner = _branch_functional_graph_anchor_owner_boundary(name) return 3 if anchor_owner is None else anchor_owner def _branch_functional_graph_component_name_boundary(name: str) -> str: """Return the indivisible functional component for geometry packing.""" parts = name.split(".") if ( len(parts) >= 3 and parts[0] == "science_stack" and parts[1].startswith("science_layer_") ): # A science layer is a complete traversal/expert gain. Its parameters # move together so an owner never publishes a half-layer checkpoint. return ".".join(parts[:2]) if ( len(parts) >= 4 and parts[0] == "science_stack" and parts[1] in { "molecular_science", "delta_attn_res", "capability_integration", } ): # Preserve each domain/projection subsystem as a functional unit while # allowing independent subsystems to balance across branch devices. return ".".join(parts[:3]) module_name, separator, _parameter_name = name.rpartition(".") return module_name if separator else name def _balanced_branch_functional_graph_owner_map_boundary( named_parameters: Mapping[str, torch.Tensor], *, owner_count: int, ) -> dict[str, int]: """Pack every non-parent functional component across the four owners.""" if ( type(owner_count) is not int or owner_count != len(BRANCH_FUNCTIONAL_GRAPH_OWNER_ROLES) or not named_parameters ): raise RuntimeError("functional graph owner packing geometry differs") additive_parameters = { name: value for name, value in named_parameters.items() if not name.startswith("base.") } if ( not additive_parameters or not all( isinstance(value, torch.Tensor) for value in additive_parameters.values() ) ): raise RuntimeError("functional graph additive parameter set is empty") owner_map: dict[str, int] = {} owner_elements = [0 for _owner_index in range(owner_count)] unanchored_components: dict[str, list[str]] = {} for name, value in additive_parameters.items(): anchor_owner = _branch_functional_graph_anchor_owner_boundary(name) if anchor_owner is not None: owner_map[name] = anchor_owner owner_elements[anchor_owner] += value.numel() continue component_name = _branch_functional_graph_component_name_boundary( name ) unanchored_components.setdefault(component_name, []).append(name) component_rows = sorted( ( ( sum( additive_parameters[name].numel() for name in component_names ), component_name, tuple(sorted(component_names)), ) for component_name, component_names in ( unanchored_components.items() ) ), key=lambda row: (-row[0], row[1]), ) for component_elements, _component_name, component_names in component_rows: owner_index = min( range(owner_count), key=lambda candidate: ( owner_elements[candidate], candidate, ), ) for name in component_names: owner_map[name] = owner_index owner_elements[owner_index] += component_elements if ( set(owner_map) != set(additive_parameters) or any( owner_index < 0 or owner_index >= owner_count for owner_index in owner_map.values() ) ): raise RuntimeError( "functional graph owner packing left additive parameters unowned" ) return owner_map def branch_functional_graph_parameter_names_boundary( named_parameters: Mapping[str, torch.Tensor], *, owner_index: int, owner_count: int, ) -> frozenset[str]: """Resolve the exact non-overlapping additive tensor set for one owner.""" expected_owner_count = len(BRANCH_FUNCTIONAL_GRAPH_OWNER_ROLES) if ( type(owner_index) is not int or type(owner_count) is not int or owner_count != expected_owner_count or owner_index < 0 or owner_index >= owner_count or not named_parameters ): raise RuntimeError("functional graph branch ownership geometry differs") owner_map = _balanced_branch_functional_graph_owner_map_boundary( named_parameters, owner_count=owner_count, ) names = frozenset( name for name, assigned_owner in owner_map.items() if assigned_owner == owner_index ) if ( not names or any(name.startswith("base.") for name in names) ): raise RuntimeError("functional graph branch parameter ownership differs") return names REASONING_LAYER_GROWTH_INITIALIZATION = ( "graph_transfer_seeded_exact_zero_execution_gate_v1" ) _SCIENCE_LAYER_STATE_PATTERN = re.compile( r"^science_stack\.science_layer_(\d+)\.(.+)$" ) _REASONING_LAYER_ROW_STATE_NAMES = frozenset( { "correction_layer_head.weight", "fabric.layer_bias_table", "fabric.layer_hidden_table", "science_stack.layer_identity_glyphs", "science_stack.layer_execution_scale", "science_stack.traversal_gate", "science_stack.delta_attn_res.block_keys", } ) _REASONING_LAYER_SQUARE_STATE_NAMES = frozenset( { "science_stack.layer_transfer_graph", "science_stack.delta_attn_res.depth_connection_logits", } ) # Retain a bounded proposal-local CPU journal between model-owned route # cohorts. Live r152 transactions update about 106 page rows per indexed # update; the former 1/256 ceiling spilled that complete working set after # every update, costing 24-56 seconds before the next CUDA wave could reopen # the same exact-delta rows. One thirty-second of physical memory keeps all # four updates in an 8,192-row rollback unit resident. Across four disjoint # branch owners this consumes at most one eighth of physical memory, while a # crossed ceiling still spills every completed row to proposal-local scratch. # This affects storage placement only, never routing, gradient eligibility, # acceptance, or durable cursor authority. _CANDIDATE_PAGE_JOURNAL_PHYSICAL_MEMORY_DIVISOR = 32 def _candidate_page_journal_budget_bytes_boundary() -> int: """Return one process's bounded proposal-local host journal budget.""" page_size = os.sysconf("SC_PAGE_SIZE") physical_memory_bytes = os.sysconf("SC_PHYS_PAGES") * page_size return max( page_size, physical_memory_bytes // _CANDIDATE_PAGE_JOURNAL_PHYSICAL_MEMORY_DIVISOR, ) MOLECULAR_MODAL_EXTENSION_INITIALIZATION_SCHEME = ( "sha256_role_seeded_diffusion_time_vibrational_identity_residual_v1" ) MOLECULAR_MODAL_EXTENSION_STATE_NAMES = frozenset( { "science_stack.molecular_science.diffusion.time_in.weight", "science_stack.molecular_science.vibrational.atom_context.weight", "science_stack.molecular_science.vibrational.mode_out.weight", "science_stack.molecular_science.vibrational.pair_context.weight", "science_stack.molecular_science.vibrational.residual_scale", } ) MOLECULAR_V21_EXTENSION_INITIALIZATION_SCHEME = ( "diffusion_time_zero_identity_vibrational_role_seeded_zero_gate_v1" ) MOLECULAR_V21_EXTENSION_STATE_NAMES: tuple[str, ...] = ( "science_stack.molecular_science.diffusion.time_in.weight", "science_stack.molecular_science.vibrational.atom_context.weight", "science_stack.molecular_science.vibrational.mode_out.weight", "science_stack.molecular_science.vibrational.pair_context.weight", "science_stack.molecular_science.vibrational.residual_scale", ) def _file_sha256_boundary(path: Path) -> str: """Hash one immutable artifact at an explicit checkpoint boundary.""" digest = hashlib.sha256() with path.open("rb") as handle: for chunk in iter(lambda: handle.read(8 * 1024 * 1024), b""): digest.update(chunk) return digest.hexdigest() def _canonical_json_sha256_boundary(payload: object) -> str: return hashlib.sha256( json.dumps( payload, sort_keys=True, separators=(",", ":"), ).encode("utf-8") ).hexdigest() _TRAINING_BRANCH_DESCENDANT_CHAIN_CACHE_SCHEMA = ( "nnf.resynthesis.training_branch_descendant_chain_identity_cache.v1" ) def _training_branch_descendant_chain_cache_path_boundary( *, store_root: Path, branch_scope_record: dict[str, Any], live_generation_record: dict[str, Any], ) -> Path: """Resolve one reconstructable cache from exact scope/live identities.""" cache_key = _canonical_json_sha256_boundary( { "branchScope": branch_scope_record, "liveGeneration": live_generation_record, } ) return ( store_root.expanduser().resolve() / ".training_branch_descendant_chain_cache" / f"{cache_key}.json" ) def _load_training_branch_descendant_chain_cache_boundary( *, cache_path: Path, store_root: Path, session_root: Path, branch_scope_record: dict[str, Any], live_generation_record: dict[str, Any], scope_page_ids: set[int], ) -> bool: """Validate a durable immutable-manifest lineage cache by exact identity.""" if not cache_path.is_file(): return False from resynthesis.none_paging import ( _cache_generation_lineage_summary_boundary, _file_identity, _file_identity_from_record, ) payload = json.loads(cache_path.read_text(encoding="utf-8")) if not isinstance(payload, dict): raise RuntimeError("NoNE descendant chain cache is malformed") payload_sha256 = payload.get("cachePayloadSha256") unsigned = dict(payload) unsigned.pop("cachePayloadSha256", None) rows = payload.get("generations") if ( payload.get("schema") != _TRAINING_BRANCH_DESCENDANT_CHAIN_CACHE_SCHEMA or payload.get("storeRoot") != str(store_root) or payload.get("branchScope") != branch_scope_record or payload.get("liveGeneration") != live_generation_record or not isinstance(payload_sha256, str) or len(payload_sha256) != 64 or _canonical_json_sha256_boundary(unsigned) != payload_sha256 or not isinstance(rows, list) or len(rows) < 2 ): raise RuntimeError("NoNE descendant chain cache authority differs") previous_generation: int | None = None previous_parent_generation: int | None = None previous_parent_payload_sha256: str | None = None observed: set[tuple[int, str]] = set() for row_index, row in enumerate(rows): binding = row.get("binding") if isinstance(row, dict) else None identity_record = ( row.get("manifestIdentity") if isinstance(row, dict) else None ) parent_payload_sha256 = ( row.get("parentManifestPayloadSha256") if isinstance(row, dict) else None ) if not isinstance(binding, dict): raise RuntimeError("NoNE descendant chain cache binding differs") generation = binding.get("generation") parent_generation = binding.get("parentGeneration") payload_sha256 = binding.get("manifestPayloadSha256") manifest_sha256 = binding.get("manifestSha256") manifest_relative = binding.get("manifest") updated_page_ids = binding.get("updatedPageIds") identity = _file_identity_from_record(identity_record) if ( binding.get("schema") != "nnf.resynthesis.none_generation_binding.v1" or binding.get("sessionId") != branch_scope_record.get("sessionId") or not isinstance(generation, int) or isinstance(generation, bool) or generation < 1 or not isinstance(parent_generation, int) or isinstance(parent_generation, bool) or parent_generation < 0 or not isinstance(payload_sha256, str) or len(payload_sha256) != 64 or not isinstance(manifest_sha256, str) or len(manifest_sha256) != 64 or not isinstance(manifest_relative, str) or not isinstance(updated_page_ids, list) or not all( isinstance(page_id, int) and not isinstance(page_id, bool) for page_id in updated_page_ids ) or identity is None or (generation, payload_sha256) in observed ): raise RuntimeError("NoNE descendant chain cache generation differs") observed.add((generation, payload_sha256)) manifest_path = (session_root / manifest_relative).resolve() if ( not manifest_path.is_relative_to(session_root) or not manifest_path.is_file() or _file_identity(manifest_path) != identity ): raise RuntimeError( "NoNE descendant chain cached manifest identity changed" ) if row_index == 0 and binding != live_generation_record: raise RuntimeError("NoNE descendant chain cache live binding differs") if previous_generation is not None and ( previous_parent_generation != generation or previous_parent_payload_sha256 != payload_sha256 or generation >= previous_generation ): raise RuntimeError("NoNE descendant chain cache ancestry differs") if row_index < len(rows) - 1: if ( not updated_page_ids or len(set(updated_page_ids)) != len(updated_page_ids) or not set(updated_page_ids).issubset(scope_page_ids) or not isinstance(parent_payload_sha256, str) or len(parent_payload_sha256) != 64 ): raise RuntimeError("NoNE descendant chain cache scope differs") _cache_generation_lineage_summary_boundary( session_root=session_root, binding_record=binding, parent_manifest_payload_sha256=( parent_payload_sha256 if isinstance(parent_payload_sha256, str) else None ), expected_identity=identity, ) previous_generation = generation previous_parent_generation = parent_generation previous_parent_payload_sha256 = parent_payload_sha256 final_binding = rows[-1]["binding"] if ( final_binding.get("generation") != branch_scope_record.get("parentGeneration") or final_binding.get("manifestPayloadSha256") != branch_scope_record.get("parentManifestPayloadSha256") ): raise RuntimeError("NoNE descendant chain cache parent differs") return True def _load_training_branch_descendant_chain_incremental_cache_boundary( *, cache_path: Path, store_root: Path, session_root: Path, branch_scope_record: dict[str, Any], live_generation_record: dict[str, Any], scope_page_ids: set[int], load_generation: Callable[..., Any], ) -> bool: """Extend the prior verified chain by one immutable generation. A branch resume normally creates a new generation, which changes the cache key and used to force a cold walk through every descendant manifest. The previous cache is already a complete proof of the parent chain. Bind the current manifest to that exact prior live binding, validate the prior cache once, and publish a new cache containing the one added row. No page values or routing decisions are inferred here; the manifest identities and parent payload digest remain the authority. """ from resynthesis.none_paging import _file_identity, _file_identity_record current_generation = live_generation_record.get("generation") current_parent_generation = live_generation_record.get("parentGeneration") current_manifest_relative = live_generation_record.get("manifest") current_manifest_sha256 = live_generation_record.get("manifestSha256") current_payload_sha256 = live_generation_record.get( "manifestPayloadSha256" ) current_page_ids = live_generation_record.get("updatedPageIds") if ( not isinstance(current_generation, int) or isinstance(current_generation, bool) or not isinstance(current_parent_generation, int) or isinstance(current_parent_generation, bool) or not isinstance(current_manifest_relative, str) or not isinstance(current_manifest_sha256, str) or not isinstance(current_payload_sha256, str) or not isinstance(current_page_ids, list) or not current_page_ids or len(set(current_page_ids)) != len(current_page_ids) or not set(current_page_ids).issubset(scope_page_ids) ): return False current_path = (session_root / current_manifest_relative).resolve() if ( not current_path.is_relative_to(session_root) or not current_path.is_file() ): return False try: loaded, manifest = load_generation( current_manifest_relative, expected_manifest_sha256=current_manifest_sha256, expected_payload_sha256=current_payload_sha256, ) except (OSError, RuntimeError, ValueError): return False if ( not isinstance(manifest, dict) or not hasattr(loaded, "external_record_boundary") or loaded.external_record_boundary() != live_generation_record ): return False manifest_parent_generation = manifest.get("parentGeneration") manifest_parent_payload = manifest.get("parentManifestPayloadSha256") if ( manifest_parent_generation != current_parent_generation or not isinstance(manifest_parent_payload, str) or len(manifest_parent_payload) != 64 ): return False for candidate in sorted(cache_path.parent.glob("*.json"), reverse=True): if candidate == cache_path: continue try: payload = json.loads(candidate.read_text(encoding="utf-8")) except (OSError, ValueError): continue if not isinstance(payload, dict) or payload.get("branchScope") != ( branch_scope_record ): continue prior_live = payload.get("liveGeneration") prior_rows = payload.get("generations") if ( not isinstance(prior_live, dict) or not isinstance(prior_rows, list) or not prior_rows or prior_live.get("generation") != current_parent_generation or prior_live.get("manifestPayloadSha256") != manifest_parent_payload ): continue try: valid_prior = _load_training_branch_descendant_chain_cache_boundary( cache_path=candidate, store_root=store_root, session_root=session_root, branch_scope_record=branch_scope_record, live_generation_record=prior_live, scope_page_ids=scope_page_ids, ) except (OSError, RuntimeError, ValueError): continue if not valid_prior: continue current_row = { "binding": live_generation_record, "parentManifestPayloadSha256": manifest_parent_payload, "manifestIdentity": _file_identity_record( _file_identity(current_path) ), } _write_training_branch_descendant_chain_cache_boundary( cache_path=cache_path, store_root=store_root, branch_scope_record=branch_scope_record, live_generation_record=live_generation_record, generation_rows=[current_row, *prior_rows], ) return True return False def _write_training_branch_descendant_chain_cache_boundary( *, cache_path: Path, store_root: Path, branch_scope_record: dict[str, Any], live_generation_record: dict[str, Any], generation_rows: list[dict[str, Any]], ) -> None: """Persist a reconstructable, self-hashed immutable lineage observation.""" from resynthesis.none_paging import _atomic_json unsigned: dict[str, Any] = { "schema": _TRAINING_BRANCH_DESCENDANT_CHAIN_CACHE_SCHEMA, "storeRoot": str(store_root), "branchScope": branch_scope_record, "liveGeneration": live_generation_record, "generations": generation_rows, "cacheAuthorizesTraining": False, "immutableManifestIdentityRequired": True, } payload = { **unsigned, "cachePayloadSha256": _canonical_json_sha256_boundary(unsigned), } _atomic_json(cache_path, payload) cache_path.parent.chmod(0o700) cache_path.chmod(0o600) def _checkpoint_state_identity_boundary( state: dict[str, torch.Tensor], ) -> tuple[str, str]: geometry = { name: (tuple(value.shape), value.dtype) for name, value in state.items() } return _checkpoint_state_geometry_identity_boundary(geometry) def _checkpoint_state_geometry_identity_boundary( geometry: Mapping[str, tuple[tuple[int, ...], torch.dtype]], ) -> tuple[str, str]: """Hash an already validated tensor geometry without materializing tensors.""" names = sorted(geometry) key_sha256 = hashlib.sha256("\n".join(names).encode("utf-8")).hexdigest() geometry_rows = [ (name, geometry[name][0], str(geometry[name][1])) for name in names ] geometry_sha256 = hashlib.sha256( json.dumps(geometry_rows, separators=(",", ":")).encode("utf-8") ).hexdigest() return key_sha256, geometry_sha256 def _checkpoint_state_value_sha256_boundary( state: dict[str, torch.Tensor], ) -> str: digest = hashlib.sha256() for name in sorted(state): value = state[name].detach().to(device="cpu").contiguous() digest.update(name.encode("utf-8")) digest.update(b"\x00") digest.update(str(value.dtype).encode("ascii")) digest.update(b"\x00") digest.update( json.dumps( tuple(value.shape), separators=(",", ":"), ).encode("ascii") ) digest.update(b"\x00") digest.update(value.reshape(-1).view(torch.uint8).numpy().tobytes()) return digest.hexdigest() def _functional_graph_parent_genesis_authority_boundary( *, parameters: Mapping[str, torch.Tensor], buffers: Mapping[str, torch.Tensor], parameter_universe: Mapping[str, torch.Tensor], source_lineage: Mapping[str, Any], target_lineage: Mapping[str, Any], inherited_graph_growth: Mapping[str, Any], parent_parameters: Mapping[str, torch.Tensor] | None = None, parent_buffers: Mapping[str, torch.Tensor] | None = None, ) -> dict[str, Any]: """Seal moving-graph genesis without granting mutation authority. A historical thin branch can predate complete additive parameter families. The versioned migration is the sole authority allowed to create those bytes. This record makes that migration durable in the next thin checkpoint while branch ownership remains a separate, disjoint overlay. """ genesis_parameters = dict(parameters) genesis_buffers = dict(buffers) additive_universe = { name: value for name, value in parameter_universe.items() if not name.startswith("base.") } growth = dict(inherited_graph_growth) seeded_tensor_names = growth.get("seededTensorNames") seeded_parameter_names = ( { name.partition("[")[0] for name in seeded_tensor_names } if isinstance(seeded_tensor_names, list) and all(isinstance(name, str) for name in seeded_tensor_names) else set() ) if ( (not genesis_parameters and not genesis_buffers) or not additive_universe or not all( isinstance(name, str) and bool(name) and not name.startswith("base.") and isinstance(value, torch.Tensor) for name, value in genesis_parameters.items() ) or not all( isinstance(name, str) and bool(name) and not name.startswith("base.") and isinstance(value, torch.Tensor) for name, value in genesis_buffers.items() ) or set(genesis_parameters).intersection(genesis_buffers) or not all( isinstance(name, str) and bool(name) and isinstance(value, torch.Tensor) for name, value in additive_universe.items() ) or not set(genesis_parameters).issubset(additive_universe) or growth.get("schema") != "nnf.resynthesis.inherited_graph_growth.v1" or growth.get("strictInheritedGeometryVerified") is not True or growth.get("partialGrowthStateAccepted") is not False or growth.get("unexpectedTensorCount") != 0 or not isinstance(seeded_tensor_names, list) or not all( isinstance(name, str) and bool(name) for name in seeded_tensor_names ) or growth.get("seededTensorCount") != len(seeded_tensor_names) or growth.get("seededKeySetSha256") != hashlib.sha256( "\n".join(seeded_tensor_names).encode("utf-8") ).hexdigest() or not set(genesis_parameters).union(genesis_buffers).issubset( seeded_parameter_names ) ): raise RuntimeError( "functional graph parent genesis growth authority differs" ) if parent_parameters is not None: physical_parent = { name: value for name, value in parent_parameters.items() if not name.startswith("base.") } expected_genesis_names = { name for name, target in additive_universe.items() if ( name not in physical_parent or tuple(physical_parent[name].shape) != tuple(target.shape) ) } if set(genesis_parameters) != expected_genesis_names: raise RuntimeError( "functional graph parent genesis parameter set differs" ) overlap = set(genesis_parameters).intersection(physical_parent) if overlap: _validate_functional_graph_parent_genesis_overlap_boundary( source_parameters={ name: physical_parent[name] for name in overlap }, genesis_parameters={ name: genesis_parameters[name] for name in overlap }, ) if parent_buffers is not None: physical_buffers = dict(parent_buffers) expected_genesis_buffer_names = ( set(genesis_buffers) if not physical_buffers else { name for name in genesis_buffers if name not in physical_buffers } ) if ( set(genesis_buffers) != expected_genesis_buffer_names or set(genesis_buffers).intersection(physical_buffers) ): raise RuntimeError( "functional graph parent genesis buffer set differs" ) source_lineage_record = dict(source_lineage) target_lineage_record = dict(target_lineage) source_lineage_sha256 = _canonical_json_sha256_boundary( source_lineage_record ) target_lineage_sha256 = _canonical_json_sha256_boundary( target_lineage_record ) if ( growth.get("sourceLineageSchema") != source_lineage_record.get("schema") or growth.get("sourceLineageSha256") != source_lineage_sha256 or growth.get("targetLineageSchema") != target_lineage_record.get("schema") ): raise RuntimeError( "functional graph parent genesis lineage authority differs" ) parameter_key_sha256, parameter_geometry_sha256 = ( _checkpoint_state_identity_boundary(genesis_parameters) ) parameter_value_sha256 = _checkpoint_state_value_sha256_boundary( genesis_parameters ) buffer_key_sha256, buffer_geometry_sha256 = ( _checkpoint_state_identity_boundary(genesis_buffers) ) buffer_value_sha256 = _checkpoint_state_value_sha256_boundary( genesis_buffers ) universe_names = sorted(additive_universe) universe_key_sha256 = hashlib.sha256( "\n".join(universe_names).encode("utf-8") ).hexdigest() universe_ownership_geometry_sha256 = hashlib.sha256( json.dumps( [ ( name, tuple(additive_universe[name].shape), additive_universe[name].numel(), ) for name in universe_names ], separators=(",", ":"), ).encode("utf-8") ).hexdigest() return { "schema": FUNCTIONAL_GRAPH_PARENT_GENESIS_SCHEMA, "sourceLineageSha256": source_lineage_sha256, "targetLineageSha256": target_lineage_sha256, "inheritedGraphGrowthSha256": _canonical_json_sha256_boundary(growth), "parameterCount": len(genesis_parameters), "parameterKeySetSha256": parameter_key_sha256, "parameterGeometrySha256": parameter_geometry_sha256, "parameterValueSha256": parameter_value_sha256, "bufferCount": len(genesis_buffers), "bufferKeySetSha256": buffer_key_sha256, "bufferGeometrySha256": buffer_geometry_sha256, "bufferValueSha256": buffer_value_sha256, "parameterUniverseKeySetSha256": universe_key_sha256, "parameterUniverseOwnershipGeometrySha256": ( universe_ownership_geometry_sha256 ), "baseParameterOwnership": False, "overlayMutationAuthority": False, } def _validated_functional_graph_parent_genesis_boundary( genesis: object, authority: object, *, parameter_universe: Mapping[str, torch.Tensor], target_lineage: Mapping[str, Any], expected_source_lineage: Mapping[str, Any] | None = None, parent_parameters: Mapping[str, torch.Tensor] | None = None, parent_buffers: Mapping[str, torch.Tensor] | None = None, ) -> tuple[ dict[str, torch.Tensor], dict[str, torch.Tensor], dict[str, Any], ]: """Validate one immutable moving-graph parent extension exactly.""" if not isinstance(genesis, dict) or not isinstance(authority, dict): raise RuntimeError("functional graph parent genesis is incomplete") source_lineage = genesis.get("sourceLineage") inherited_graph_growth = genesis.get("inheritedGraphGrowth") parameters = genesis.get("parameters") buffers = genesis.get("buffers") if ( set(genesis) != { "schema", "sourceLineage", "targetLineage", "inheritedGraphGrowth", "parameters", "buffers", } or genesis.get("schema") != FUNCTIONAL_GRAPH_PARENT_GENESIS_SCHEMA or not isinstance(source_lineage, dict) or genesis.get("targetLineage") != dict(target_lineage) or not isinstance(inherited_graph_growth, dict) or not isinstance(parameters, dict) or (not parameters and not buffers) or not all( isinstance(name, str) and isinstance(value, torch.Tensor) for name, value in parameters.items() ) or not isinstance(buffers, dict) or not all( isinstance(name, str) and isinstance(value, torch.Tensor) for name, value in buffers.items() ) or set(parameters).intersection(buffers) or ( expected_source_lineage is not None and source_lineage != dict(expected_source_lineage) ) ): raise RuntimeError("functional graph parent genesis is malformed") expected_authority = ( _functional_graph_parent_genesis_authority_boundary( parameters=parameters, buffers=buffers, parameter_universe=parameter_universe, source_lineage=source_lineage, target_lineage=target_lineage, inherited_graph_growth=inherited_graph_growth, parent_parameters=parent_parameters, parent_buffers=parent_buffers, ) ) if authority != expected_authority: raise RuntimeError( "functional graph parent genesis identity differs" ) return ( dict(parameters), dict(buffers), copy.deepcopy(expected_authority), ) def _validate_functional_graph_parent_genesis_overlap_boundary( *, source_parameters: Mapping[str, torch.Tensor], genesis_parameters: Mapping[str, torch.Tensor], ) -> None: """Validate the only supported in-place functional-parameter growth. Most genesis tensors are wholly absent from an older parent. The logit residual bank is the one versioned exception: its rank can grow while the inherited down/up prefixes remain exact in the successor precision. A retained lower-precision parent may be widened when the successor rank is materialized. The historical r26 logit residual is the one production exception in the other direction: its FP32 values may enter the active BF16 graph only when every inherited value survives an exact BF16->FP32 round trip. Equal-width reinterpretation and lossy narrowing are never migration authority. Treating those tensors as ordinary parent overlays would compare the successor geometry to the old rank and reject a valid durable resume; accepting arbitrary overlaps would let a thin branch replace parent weights. Keep the exception exact-name, exact-axis, growth-only, lossless-precision-only, and prefix-preserving. """ overlap = set(source_parameters).intersection(genesis_parameters) logit_residual_pair = { _LOGIT_RESIDUAL_DOWN_WEIGHT_NAME, _LOGIT_RESIDUAL_UP_WEIGHT_NAME, } if overlap.intersection(logit_residual_pair) and not ( logit_residual_pair.issubset(overlap) ): raise RuntimeError( "functional graph parent genesis logit residual rank expansion " "is incomplete" ) if logit_residual_pair.issubset(overlap): source_down = source_parameters[ _LOGIT_RESIDUAL_DOWN_WEIGHT_NAME ].detach() source_up = source_parameters[ _LOGIT_RESIDUAL_UP_WEIGHT_NAME ].detach() successor_down = genesis_parameters[ _LOGIT_RESIDUAL_DOWN_WEIGHT_NAME ].detach() successor_up = genesis_parameters[ _LOGIT_RESIDUAL_UP_WEIGHT_NAME ].detach() paired_geometry_valid = ( source_down.ndim == 2 and source_up.ndim == 2 and successor_down.ndim == 2 and successor_up.ndim == 2 and source_down.shape[0] == source_up.shape[1] and successor_down.shape[0] == successor_up.shape[1] and successor_down.shape[0] > source_down.shape[0] and source_down.shape[1] == source_up.shape[0] == successor_down.shape[1] == successor_up.shape[0] and source_down.dtype == source_up.dtype and successor_down.dtype == successor_up.dtype ) if not paired_geometry_valid: raise RuntimeError( "functional graph parent genesis logit residual paired " "geometry differs" ) for name in sorted(overlap): source = source_parameters[name].detach().to(device="cpu") successor = genesis_parameters[name].detach().to(device="cpu") if source.dtype == successor.dtype: inherited_source = source elif ( source.is_floating_point() and successor.is_floating_point() and successor.element_size() > source.element_size() ): # Widening preserves the accepted parent's values while allowing # the successor graph to use a higher-precision representation. # Every other narrowing or equal-width dtype reinterpretation would # instead weaken that authority. inherited_source = source.to(dtype=successor.dtype) elif ( name in logit_residual_pair and source.dtype == torch.float32 and successor.dtype == torch.bfloat16 and torch.equal( source.to(dtype=torch.bfloat16).to(dtype=torch.float32), source, ) ): # The shipped r26 residual pair is FP32 but exactly BF16 # representable. Permit only that lossless production migration; # the exact inherited-prefix check below still prevents a thin # branch from substituting any accepted value. inherited_source = source.to(dtype=torch.bfloat16) else: raise RuntimeError( "functional graph parent genesis overlap precision differs: " f"{name}" ) if name == _LOGIT_RESIDUAL_DOWN_WEIGHT_NAME: geometry_valid = ( source.ndim == 2 and successor.ndim == 2 and successor.shape[0] > source.shape[0] and successor.shape[1] == source.shape[1] ) prefix = successor[: source.shape[0]] elif name == _LOGIT_RESIDUAL_UP_WEIGHT_NAME: geometry_valid = ( source.ndim == 2 and successor.ndim == 2 and successor.shape[0] == source.shape[0] and successor.shape[1] > source.shape[1] ) prefix = successor[:, : source.shape[1]] else: raise RuntimeError( "functional graph parent genesis overlaps an unsupported " f"parameter: {name}" ) if not geometry_valid or not torch.equal(prefix, inherited_source): raise RuntimeError( "functional graph parent genesis inherited prefix differs: " f"{name}" ) def build_branch_functional_graph_parameter_ownership_boundary( parameters: Mapping[str, torch.Tensor], *, owner_index: int, owner_count: int, parameter_universe: Mapping[str, torch.Tensor] | None = None, ) -> dict[str, Any]: """Bind one exact optimizer subset to its disjoint functional graph owner.""" parameter_state = dict(parameters) expected_names = branch_functional_graph_parameter_names_boundary( ( parameter_state if parameter_universe is None else parameter_universe ), owner_index=owner_index, owner_count=owner_count, ) if set(parameter_state) != expected_names or not all( isinstance(value, torch.Tensor) for value in parameter_state.values() ): raise RuntimeError("functional graph optimizer parameter set differs") parameter_key_sha256, parameter_geometry_sha256 = ( _checkpoint_state_identity_boundary(parameter_state) ) return { "schema": BRANCH_FUNCTIONAL_GRAPH_OWNERSHIP_SCHEMA, "ownerIndex": owner_index, "ownerCount": owner_count, "ownerRole": BRANCH_FUNCTIONAL_GRAPH_OWNER_ROLES[owner_index], "parameterNames": sorted(expected_names), "parameterCount": len(expected_names), "parameterElements": sum( value.numel() for value in parameter_state.values() ), "parameterKeySetSha256": parameter_key_sha256, "parameterGeometrySha256": parameter_geometry_sha256, "parentVocabularyFeatureProjectionOnly": True, "baseParameterOwnership": False, "sharedDenseGraphMutationAuthority": False, "overlapAllowed": False, "expertSpecializationGainRequired": True, "layerTraversalCapacityGainRequired": True, "retainedDomainKnowledgeGainRequired": True, "intentEndStateReasoningGainRequired": True, } def validate_branch_functional_graph_parameter_ownership_boundary( record: object, parameters: Mapping[str, torch.Tensor], *, parameter_universe: Mapping[str, torch.Tensor] | None = None, ) -> dict[str, Any]: """Validate exact disjoint ownership without granting route authority.""" if not isinstance(record, dict): raise RuntimeError("functional graph parameter ownership is missing") owner_index = record.get("ownerIndex") owner_count = record.get("ownerCount") if type(owner_index) is not int or type(owner_count) is not int: raise RuntimeError("functional graph parameter ownership is malformed") expected = build_branch_functional_graph_parameter_ownership_boundary( parameters, owner_index=owner_index, owner_count=owner_count, parameter_universe=parameter_universe, ) if record != expected: raise RuntimeError("functional graph parameter ownership differs") return expected def _branch_causal_moving_graph_buffer_growth_boundary( parent_geometry: Mapping[ str, tuple[tuple[int, ...], torch.dtype], ], buffers: Mapping[str, torch.Tensor], *, runtime_precision_compatible: bool = False, ) -> dict[str, Any] | None: """Describe exact branch-local history growth over an immutable parent.""" if set(parent_geometry) != set(buffers): raise RuntimeError( "NoNE branch causal moving-graph buffer set differs" ) rows: list[dict[str, Any]] = [] for name in sorted(parent_geometry): parent_shape, parent_dtype = parent_geometry[name] value = buffers[name] active_shape = tuple(value.shape) precision_compatible = ( value.dtype == parent_dtype or ( runtime_precision_compatible and value.is_floating_point() and torch.empty((), dtype=parent_dtype).is_floating_point() ) ) if active_shape == parent_shape and precision_compatible: continue if ( not name.endswith("._expert_history_states") or len(parent_shape) != 2 or len(active_shape) != 2 or active_shape[0] <= parent_shape[0] or active_shape[1:] != parent_shape[1:] or not precision_compatible ): raise RuntimeError( "NoNE branch causal moving-graph buffer geometry differs: " f"{name}" ) rows.append( { "name": name, "growthAxis": 0, "parentShape": list(parent_shape), "overlayShape": list(active_shape), "dtype": str(parent_dtype), "inheritedPrefixRows": parent_shape[0], "appendedRowsRetainBranchTraining": True, } ) if not rows: return None return { "schema": BRANCH_CAUSAL_MOVING_GRAPH_BUFFER_GROWTH_SCHEMA, "sharedDenseGraphMutationAuthority": False, "exactInheritedPrefixRequired": True, "buffers": rows, } def _validated_branch_causal_moving_graph_buffer_growth_boundary( record: object, *, parent_geometry: Mapping[ str, tuple[tuple[int, ...], torch.dtype], ], buffers: Mapping[str, torch.Tensor], ) -> dict[str, Any] | None: """Validate the sole versioned causal-buffer growth transformation.""" expected = _branch_causal_moving_graph_buffer_growth_boundary( parent_geometry, buffers, ) if expected is None: if record is not None: raise RuntimeError( "NoNE branch causal moving-graph authority is unexpected" ) return None if record != expected: raise RuntimeError( "NoNE branch causal moving-graph authority differs" ) return expected def _deterministic_causal_parent_state_boundary( parameters: object, ) -> tuple[dict[str, torch.Tensor], dict[str, torch.Tensor]]: """Reconstruct the versioned causal genesis from its exact geometry. A legacy full parent predates this additive head. Its v2 child may reference the deterministic migration values, but it may not invent arbitrary parent weights. Geometry is inferred from the complete exact-name tensor packet, then a fresh graph reconstructs the role-seeded values independently. """ if ( not isinstance(parameters, dict) or set(parameters) != BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES or not all(isinstance(value, torch.Tensor) for value in parameters.values()) ): raise RuntimeError( "Resynthesis causal parent initialization state is incomplete" ) prefix = _CAUSAL_WORLD_GRAPH_STATE_PREFIX def value(name: str) -> torch.Tensor: tensor = parameters.get(f"{prefix}{name}") if not isinstance(tensor, torch.Tensor): raise RuntimeError( "Resynthesis causal parent initialization geometry differs" ) return tensor evidence_norm_t = value("evidence_norm.weight") action_policy_t = value("action_policy.weight") pathway_seed_t = value("pathway_seed.weight") domain_basis_t = value("domain_basis_t") ontology_seed_t = value("ontology_seed.weight") primitive_read_t = value("compiler.primitive_read_t") program_step_gate_t = value("compiler.program_step_gate_t") action_read_t = value("action_read_t") if ( evidence_norm_t.ndim != 1 or action_policy_t.ndim != 2 or action_policy_t.shape[0] != action_policy_t.shape[1] or pathway_seed_t.ndim != 2 or domain_basis_t.ndim != 2 or ontology_seed_t.ndim != 2 or primitive_read_t.ndim != 3 or program_step_gate_t.ndim != 1 or action_read_t.ndim != 3 ): raise RuntimeError( "Resynthesis causal parent initialization geometry differs" ) hidden_size = int(evidence_norm_t.shape[0]) action_size = int(action_policy_t.shape[0]) world_size = int(domain_basis_t.shape[1]) domain_count = int(domain_basis_t.shape[0]) pathway_size = int(pathway_seed_t.shape[1]) primitive_count = int(primitive_read_t.shape[0]) program_steps = int(program_step_gate_t.shape[0]) operator_rank = int(action_read_t.shape[2]) if ( world_size < 1 or ontology_seed_t.shape[1] != hidden_size or ontology_seed_t.shape[0] % world_size != 0 ): raise RuntimeError( "Resynthesis causal parent initialization geometry differs" ) hypothesis_count = int(ontology_seed_t.shape[0] // world_size) from resynthesis.causal_algebra import ( CausalAlgebraConfig, CausalAlgebraWorldGraph, ) graph = CausalAlgebraWorldGraph( CausalAlgebraConfig( hidden_size=hidden_size, action_size=action_size, pathway_size=pathway_size, world_size=world_size, hypothesis_count=hypothesis_count, primitive_count=primitive_count, program_steps=program_steps, domain_count=domain_count, operator_rank=operator_rank, ) ) expected_parameters = { f"{prefix}{name}": parameter.detach().to( device="cpu", dtype=value(name).dtype, ) for name, parameter in graph.named_parameters() } if set(expected_parameters) != BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES: raise RuntimeError( "Resynthesis causal parent initialization parameter set differs" ) expected_buffers = { f"{prefix}{name}": buffer.detach().to(device="cpu") for name, buffer in graph.named_buffers() } if not expected_buffers or any( not name.startswith(prefix) for name in expected_buffers ): raise RuntimeError( "Resynthesis causal parent initialization buffer set differs" ) return expected_parameters, expected_buffers def _validated_deterministic_causal_parent_parameters_boundary( parameters: object, ) -> dict[str, torch.Tensor]: """Validate the exact role-seeded causal parameters used by migration.""" expected, _ = _deterministic_causal_parent_state_boundary(parameters) assert isinstance(parameters, dict) validated: dict[str, torch.Tensor] = {} for name, candidate in parameters.items(): assert isinstance(candidate, torch.Tensor) expected_value = expected[name] candidate_value = candidate.detach().to(device="cpu") if ( candidate_value.shape != expected_value.shape or candidate_value.dtype != expected_value.dtype or not torch.equal(candidate_value, expected_value) ): raise RuntimeError( "Resynthesis causal parent initialization value differs: " f"{name}" ) validated[name] = candidate_value.clone() return validated def _validated_deterministic_causal_parent_buffers_boundary( parameters: object, buffers: object, ) -> dict[str, torch.Tensor]: """Validate the exact proof/history buffer genesis for an absent parent.""" _, expected = _deterministic_causal_parent_state_boundary(parameters) if ( not isinstance(buffers, dict) or set(buffers) != set(expected) or not all(isinstance(value, torch.Tensor) for value in buffers.values()) ): raise RuntimeError( "Resynthesis causal parent initialization buffer state is incomplete" ) validated: dict[str, torch.Tensor] = {} for name, candidate in buffers.items(): assert isinstance(candidate, torch.Tensor) expected_value = expected[name].to(dtype=candidate.dtype) candidate_value = candidate.detach().to(device="cpu") if ( candidate_value.shape != expected_value.shape or candidate_value.dtype != expected_value.dtype or not torch.equal(candidate_value, expected_value) ): raise RuntimeError( "Resynthesis causal parent initialization buffer value differs: " f"{name}" ) validated[name] = candidate_value.clone() return validated def _causal_parent_genesis_source_lineage_sha256_boundary( *, base_checkpoint_sha256: str, lineage_sha256: str, parameter_key_sha256: str, parameter_geometry_sha256: str, parameter_value_sha256: str, buffer_key_sha256: str, buffer_geometry_sha256: str, buffer_value_sha256: str, ) -> str: """Bind deterministic schema growth to one immutable parent lineage.""" return _canonical_json_sha256_boundary( { "schema": CAUSAL_PARENT_GENESIS_LINEAGE_SCHEMA, "baseCheckpointSha256": base_checkpoint_sha256, "lineageSha256": lineage_sha256, "causalParameterInitialization": ( CAUSAL_PARENT_INITIALIZATION_SCHEMA ), "causalParameterKeySetSha256": parameter_key_sha256, "causalParameterGeometrySha256": parameter_geometry_sha256, "causalParameterValueSha256": parameter_value_sha256, "causalBufferKeySetSha256": buffer_key_sha256, "causalBufferGeometrySha256": buffer_geometry_sha256, "causalBufferValueSha256": buffer_value_sha256, } ) def _verified_checkpoint_file_sha256_boundary( path: Path, expected_sha256: str | None, *, identity_cache_root: Path | None = None, ) -> str: if expected_sha256 is None: return _file_sha256_boundary(path) if ( len(expected_sha256) != 64 or any(character not in "0123456789abcdef" for character in expected_sha256) ): raise RuntimeError("Resynthesis checkpoint SHA-256 is malformed") from resynthesis.none_paging import file_sha256_authority_boundary resolved_identity_cache_root = ( identity_cache_root.expanduser().resolve() if identity_cache_root is not None else path.parent.parent / ".artifact_sha256_cache" ) return file_sha256_authority_boundary( path, expected_sha256=expected_sha256, identity_cache_root=resolved_identity_cache_root, ) def _validated_full_additive_checkpoint_payload_boundary( payload: object, ) -> dict[str, Any]: if ( not isinstance(payload, dict) or payload.get("schema") != ADDITIVE_CHECKPOINT_SCHEMA ): raise RuntimeError("Resynthesis additive checkpoint schema differs") lineage = payload.get("lineage") parameters = payload.get("parameters") buffers = payload.get("buffers") if ( not isinstance(lineage, dict) or not isinstance(parameters, dict) or not isinstance(buffers, dict) or not all(isinstance(value, torch.Tensor) for value in parameters.values()) or not all(isinstance(value, torch.Tensor) for value in buffers.values()) or set(parameters).intersection(buffers) ): raise RuntimeError("Resynthesis additive checkpoint payload is incomplete") state = {**parameters, **buffers} key_sha256, geometry_sha256 = _checkpoint_state_identity_boundary(state) if payload.get("stateKeySetSha256") != key_sha256: raise RuntimeError("Resynthesis additive checkpoint key identity differs") if payload.get("stateGeometrySha256") != geometry_sha256: raise RuntimeError("Resynthesis additive checkpoint geometry identity differs") return payload _CausalParentFileIdentity = tuple[int, int, int, int, int] @dataclass(frozen=True) class _ValidatedImmutableCausalParent: """Compact exact proof for one unchanged full parent checkpoint.""" file_identity: _CausalParentFileIdentity checkpoint_sha256: str lineage: dict[str, Any] state_key_sha256: str state_geometry_sha256: str buffer_key_sha256: str buffer_geometry_sha256: str parameter_geometry: dict[str, tuple[tuple[int, ...], torch.dtype]] buffer_geometry: dict[str, tuple[tuple[int, ...], torch.dtype]] physical_causal_parameter_names: frozenset[str] physical_causal_buffer_names: frozenset[str] causal_parameters: dict[str, torch.Tensor] causal_buffers: dict[str, torch.Tensor] causal_parameter_key_sha256: str | None causal_parameter_geometry_sha256: str | None causal_parameter_value_sha256: str | None causal_buffer_key_sha256: str | None causal_buffer_geometry_sha256: str | None causal_buffer_value_sha256: str | None deterministic_causal_parameter_geometry: dict[ str, tuple[tuple[int, ...], torch.dtype], ] deterministic_causal_buffer_geometry: dict[ str, tuple[tuple[int, ...], torch.dtype], ] # A training process owns one branch and normally observes one immutable # checkpoint parent. The small bound also covers nested validation and tests # without retaining mmap-backed payloads or growing with snapshot count. _IMMUTABLE_CAUSAL_PARENT_CACHE_MAX_ENTRIES = 8 _IMMUTABLE_CAUSAL_PARENT_CACHE_LOCK = threading.Lock() _IMMUTABLE_CAUSAL_PARENT_CACHE: dict[ tuple[Path, str], _ValidatedImmutableCausalParent, ] = {} def _causal_parent_file_identity_boundary( path: Path, ) -> _CausalParentFileIdentity: stat = path.stat() return ( int(stat.st_dev), int(stat.st_ino), int(stat.st_size), int(stat.st_mtime_ns), int(stat.st_ctime_ns), ) def _validated_immutable_causal_parent_boundary( *, base_path: Path, base_sha256: str, identity_cache_root: Path | None, ) -> _ValidatedImmutableCausalParent: """Validate an immutable parent once per exact process-local identity. Cryptographic file authority and stat identity are checked before every reuse. A changed parent therefore misses or fails before cached tensor geometry can participate, while each new causal overlay remains outside this cache and is validated independently. """ cache_key = (base_path, base_sha256) with _IMMUTABLE_CAUSAL_PARENT_CACHE_LOCK: parent_sha256 = _verified_checkpoint_file_sha256_boundary( base_path, base_sha256, identity_cache_root=identity_cache_root, ) identity_before = _causal_parent_file_identity_boundary(base_path) cached = _IMMUTABLE_CAUSAL_PARENT_CACHE.get(cache_key) if ( parent_sha256 == base_sha256 and cached is not None and cached.file_identity == identity_before ): parent_sha256_after = _verified_checkpoint_file_sha256_boundary( base_path, base_sha256, identity_cache_root=identity_cache_root, ) if ( parent_sha256_after != base_sha256 or _causal_parent_file_identity_boundary(base_path) != identity_before ): raise RuntimeError( "Resynthesis branch causal delta parent changed during " "cache validation" ) return cached if parent_sha256 != base_sha256: raise RuntimeError( "Resynthesis branch causal delta parent identity differs" ) parent_payload = torch.load( base_path, map_location="cpu", mmap=True, weights_only=True, ) parent_sha256_after = _verified_checkpoint_file_sha256_boundary( base_path, base_sha256, identity_cache_root=identity_cache_root, ) identity_after = _causal_parent_file_identity_boundary(base_path) if ( parent_sha256_after != base_sha256 or identity_after != identity_before ): raise RuntimeError( "Resynthesis branch causal delta parent changed during load" ) validated_parent = _validated_full_additive_checkpoint_payload_boundary( parent_payload ) parent_parameters = validated_parent["parameters"] parent_buffers = validated_parent["buffers"] parent_lineage = validated_parent["lineage"] assert isinstance(parent_parameters, dict) assert isinstance(parent_buffers, dict) assert isinstance(parent_lineage, dict) physical_causal_parameter_names = ( BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES.intersection( parent_parameters ) ) physical_causal_buffer_names = frozenset( name for name in parent_buffers if name.startswith(_CAUSAL_WORLD_GRAPH_STATE_PREFIX) ) causal_parameters = { name: parent_parameters[name].detach().to(device="cpu").clone() for name in physical_causal_parameter_names } causal_buffers = { name: parent_buffers[name].detach().to(device="cpu").clone() for name in physical_causal_buffer_names } deterministic_parameter_geometry: dict[ str, tuple[tuple[int, ...], torch.dtype], ] = {} deterministic_buffer_geometry: dict[ str, tuple[tuple[int, ...], torch.dtype], ] = {} if ( physical_causal_parameter_names == BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES ): expected_parameters, expected_buffers = ( _deterministic_causal_parent_state_boundary( causal_parameters ) ) deterministic_parameter_geometry = { name: (tuple(value.shape), value.dtype) for name, value in expected_parameters.items() } deterministic_buffer_geometry = { name: (tuple(value.shape), value.dtype) for name, value in expected_buffers.items() } buffer_geometry = { name: (tuple(value.shape), value.dtype) for name, value in parent_buffers.items() } parameter_geometry = { name: (tuple(value.shape), value.dtype) for name, value in parent_parameters.items() } buffer_key_sha256, buffer_geometry_sha256 = ( _checkpoint_state_geometry_identity_boundary(buffer_geometry) ) causal_parameter_identity = ( _checkpoint_state_identity_boundary(causal_parameters) if causal_parameters else None ) causal_buffer_identity = ( _checkpoint_state_identity_boundary(causal_buffers) if causal_buffers else None ) validated = _ValidatedImmutableCausalParent( file_identity=identity_after, checkpoint_sha256=base_sha256, lineage=copy.deepcopy(parent_lineage), state_key_sha256=str(validated_parent["stateKeySetSha256"]), state_geometry_sha256=str( validated_parent["stateGeometrySha256"] ), buffer_key_sha256=buffer_key_sha256, buffer_geometry_sha256=buffer_geometry_sha256, parameter_geometry=parameter_geometry, buffer_geometry=buffer_geometry, physical_causal_parameter_names=frozenset( physical_causal_parameter_names ), physical_causal_buffer_names=physical_causal_buffer_names, causal_parameters=causal_parameters, causal_buffers=causal_buffers, causal_parameter_key_sha256=( causal_parameter_identity[0] if causal_parameter_identity is not None else None ), causal_parameter_geometry_sha256=( causal_parameter_identity[1] if causal_parameter_identity is not None else None ), causal_parameter_value_sha256=( _checkpoint_state_value_sha256_boundary(causal_parameters) if causal_parameters else None ), causal_buffer_key_sha256=( causal_buffer_identity[0] if causal_buffer_identity is not None else None ), causal_buffer_geometry_sha256=( causal_buffer_identity[1] if causal_buffer_identity is not None else None ), causal_buffer_value_sha256=( _checkpoint_state_value_sha256_boundary(causal_buffers) if causal_buffers else None ), deterministic_causal_parameter_geometry=( deterministic_parameter_geometry ), deterministic_causal_buffer_geometry=( deterministic_buffer_geometry ), ) _IMMUTABLE_CAUSAL_PARENT_CACHE.pop(cache_key, None) _IMMUTABLE_CAUSAL_PARENT_CACHE[cache_key] = validated while ( len(_IMMUTABLE_CAUSAL_PARENT_CACHE) > _IMMUTABLE_CAUSAL_PARENT_CACHE_MAX_ENTRIES ): oldest_key = next(iter(_IMMUTABLE_CAUSAL_PARENT_CACHE)) _IMMUTABLE_CAUSAL_PARENT_CACHE.pop(oldest_key) return validated @dataclass(frozen=True) class AdditiveBranchDeltaResolution: artifact_path: Path artifact_sha256: str base_checkpoint_path: Path base_checkpoint_sha256: str base_state_key_sha256: str base_state_geometry_sha256: str base_buffer_key_sha256: str base_buffer_geometry_sha256: str lineage: dict[str, Any] branch_scope: dict[str, Any] parameters: dict[str, torch.Tensor] buffers: dict[str, torch.Tensor] functional_graph_parameter_ownership: dict[str, Any] | None functional_graph_parent_genesis_authority: dict[str, Any] | None functional_graph_parent_genesis_parameters: dict[str, torch.Tensor] functional_graph_parent_genesis_buffers: dict[str, torch.Tensor] composed_state_sha256: str @dataclass(frozen=True) class AdditiveBranchCausalStatePacket: """Typed checkpoint-boundary state for one isolated causal/page branch.""" parameters: dict[str, torch.Tensor] buffers: dict[str, torch.Tensor] @dataclass(frozen=True) class AdditiveBranchCausalDeltaResolution: """Validated v2 overlay; it never grants shared graph mutation authority.""" artifact_path: Path artifact_sha256: str base_checkpoint_path: Path base_checkpoint_sha256: str base_state_key_sha256: str base_state_geometry_sha256: str base_buffer_key_sha256: str base_buffer_geometry_sha256: str base_causal_parameter_key_sha256: str base_causal_parameter_geometry_sha256: str base_causal_parameter_value_sha256: str base_causal_parameters: dict[str, torch.Tensor] base_causal_buffer_key_sha256: str base_causal_buffer_geometry_sha256: str base_causal_buffer_value_sha256: str base_causal_buffers: dict[str, torch.Tensor] causal_parameter_authority_mode: str parent_causal_parameter_presence_count: int causal_parameter_genesis_source_lineage_sha256: str | None lineage: dict[str, Any] branch_scope: dict[str, Any] parameters: dict[str, torch.Tensor] buffers: dict[str, torch.Tensor] functional_graph_parameter_ownership: dict[str, Any] | None functional_graph_parent_genesis_authority: dict[str, Any] | None functional_graph_parent_genesis_parameters: dict[str, torch.Tensor] functional_graph_parent_genesis_buffers: dict[str, torch.Tensor] moving_graph_buffer_growth: dict[str, Any] | None composed_state_sha256: str @dataclass(frozen=True) class AdditiveCheckpointResolution: artifact_path: Path artifact_sha256: str base_checkpoint_path: Path base_checkpoint_sha256: str base_payload: dict[str, Any] payload: dict[str, Any] branch_delta: ( AdditiveBranchDeltaResolution | AdditiveBranchCausalDeltaResolution | None ) @dataclass(frozen=True) class AdditivePageBranchReconciliationInput: """One fully validated page-branch overlay at the checkpoint boundary.""" artifact_path: Path artifact_sha256: str base_checkpoint_path: Path base_checkpoint_sha256: str lineage: dict[str, Any] scope: NoNETrainingBranchScopePacket buffers: dict[str, torch.Tensor] @dataclass(frozen=True) class AdditiveKnowledgeCheckpointFiles: """Exact checkpoint family consumed by all-knowledge reconciliation.""" checkpoint_path: Path optimizer_path: Path external_state_path: Path @dataclass(frozen=True) class AdditiveKnowledgeReconciliationAuthority: """Exact page-union and checkpoint identities admitted to dense merge.""" common_parent_checkpoint_path: Path common_parent_checkpoint_sha256: str common_parent_generation_t: torch.Tensor common_parent_manifest_payload_sha256_t: torch.Tensor target_checkpoint_path: Path target_checkpoint_sha256: str target_optimizer_path: Path target_optimizer_sha256: str target_external_state_path: Path target_external_state_sha256: str target_generation_t: torch.Tensor target_manifest_sha256_t: torch.Tensor target_manifest_payload_sha256_t: torch.Tensor branch_checkpoint_paths: tuple[Path, ...] branch_checkpoint_sha256s: tuple[str, ...] branch_optimizer_paths: tuple[Path, ...] branch_optimizer_sha256s: tuple[str, ...] branch_external_state_paths: tuple[Path, ...] branch_external_state_sha256s: tuple[str, ...] branch_generations_t: torch.Tensor branch_manifest_sha256s_t: torch.Tensor branch_manifest_payload_sha256s_t: torch.Tensor branch_scope_sha256s_t: torch.Tensor branch_scope_page_ids_t: torch.Tensor branch_scope_page_layer_ids_t: torch.Tensor branch_union_sha256_t: torch.Tensor lineage_rebase_sha256_t: torch.Tensor @dataclass(frozen=True) class AdditiveKnowledgeCalibrationPacket: """Direct full state with only model-owned router calibration outstanding. ``payload`` has no branch-delta dependency and retains the target checkpoint storage layout. Its pending-only schema and deliberately zero router biases make it an in-memory calibration input rather than an acceptable checkpoint. ``ResynthesisRBO.recalibrate_reconciled_router_biases_boundary`` must run once before the ordinary unscoped snapshot boundary publishes the result. """ payload: dict[str, Any] router_bias_names: tuple[str, ...] physical_page_ids_t: torch.Tensor branch_scope_page_ids_t: torch.Tensor proof: dict[str, Any] @dataclass(frozen=True) class HistoricalAdditiveCheckpointSegmentPacket: """One plan-ordered historical checkpoint/page evidence segment. This is an offline checkpoint I/O boundary, not a model hot path. A checkpoint-bearing row contains a fully validated terminal resolution. A page-only row deliberately contains no checkpoint resolution and cannot contribute dense tensors or optimizer state. """ ordinal_t: torch.Tensor source_generation_t: torch.Tensor terminal_generation_t: torch.Tensor terminal: AdditiveCheckpointResolution | None checkpoint_path: Path checkpoint_sha256: str optimizer_path: Path optimizer_sha256: str external_state_path: Path external_state_sha256: str changed_page_count_t: torch.Tensor changed_page_ids_sha256_t: torch.Tensor provenance_sha256_t: torch.Tensor checkpoint_tensor_evidence_t: torch.Tensor shared_dense_graph_mutation_authority_t: torch.Tensor branch_local_causal_control_overlay_t: torch.Tensor scope: NoNETrainingBranchScopePacket | None causal_genesis: AdditiveBranchCausalStatePacket | None @dataclass(frozen=True) class HistoricalAdditiveKnowledgeReconciliationAuthority: """Immutable plan and output policy admitted to historical tensor surgery.""" plan_sha256_t: torch.Tensor common_parent_generation_t: torch.Tensor target_generation_t: torch.Tensor output_generation_t: torch.Tensor target_checkpoint_path: Path target_checkpoint_sha256: str target_optimizer_path: Path target_optimizer_sha256: str target_external_state_path: Path target_external_state_sha256: str target_manifest_sha256_t: torch.Tensor target_manifest_payload_sha256_t: torch.Tensor physical_page_ids_sha256_t: torch.Tensor semantic_page_ids_sha256_t: torch.Tensor page_change_event_count_t: torch.Tensor revision6_accepted_t: torch.Tensor cross_store_overlay_accepted_t: torch.Tensor direct_page_map_required_t: torch.Tensor optimizer_moments_reinitialized_t: torch.Tensor @dataclass(frozen=True) class HistoricalAdditiveOptimizerPacket: """Fresh optimizer topology bound to one reconciled parameter universe.""" payload: dict[str, Any] proof: dict[str, Any] @dataclass(frozen=True) class CalibratedAdditiveKnowledgePacket: """One publishable direct checkpoint and its target-free calibration proof.""" payload: dict[str, Any] reconciliation_proof: dict[str, Any] calibration_proof: dict[str, Any] router_bias_names: tuple[str, ...] physical_page_ids_t: torch.Tensor branch_scope_page_ids_t: torch.Tensor calibration_prompt_authority_sha256_t: torch.Tensor def _immutable_file_sha256_boundary( path: Path, *, label: str, ) -> tuple[Path, str, tuple[int, int, int, int, int]]: """Hash one unchanged regular file at the reconciliation I/O boundary.""" if not isinstance(path, Path): raise RuntimeError( f"Resynthesis all-knowledge {label} path is malformed" ) resolved = path.expanduser().resolve() if not resolved.is_file(): raise RuntimeError( f"Resynthesis all-knowledge {label} is missing" ) before = resolved.stat() sha256 = _file_sha256_boundary(resolved) after = resolved.stat() before_identity = ( before.st_dev, before.st_ino, before.st_size, before.st_mtime_ns, before.st_ctime_ns, ) after_identity = ( after.st_dev, after.st_ino, after.st_size, after.st_mtime_ns, after.st_ctime_ns, ) if before_identity != after_identity: raise RuntimeError( f"Resynthesis all-knowledge {label} changed while hashing" ) return resolved, sha256, after_identity def _require_immutable_file_identity_boundary( path: Path, identity: tuple[int, int, int, int, int], *, label: str, ) -> None: """Reject replacement or mutation after an artifact was hashed.""" current = path.stat() if ( current.st_dev, current.st_ino, current.st_size, current.st_mtime_ns, current.st_ctime_ns, ) != identity: raise RuntimeError( f"Resynthesis all-knowledge {label} changed after hashing" ) def _validated_additive_knowledge_checkpoint_files_boundary( files: AdditiveKnowledgeCheckpointFiles, *, label: str, expected_session_id_t: torch.Tensor, expected_scope_sha256_t: torch.Tensor | None, expected_scope_parent_generation_t: torch.Tensor | None, expected_scope_parent_manifest_payload_sha256_t: torch.Tensor | None, ) -> tuple[ AdditiveKnowledgeCheckpointFiles, str, str, str, torch.Tensor, torch.Tensor, torch.Tensor, ]: """Hash and parse one immutable checkpoint/optimizer/sidecar family.""" from resynthesis.none_paging import ( generation_binding_from_record_boundary, training_branch_scope_from_record_boundary, ) if not isinstance(files, AdditiveKnowledgeCheckpointFiles): raise RuntimeError( f"Resynthesis all-knowledge {label} checkpoint family is malformed" ) ( checkpoint_path, checkpoint_sha256, checkpoint_identity, ) = _immutable_file_sha256_boundary( files.checkpoint_path, label=f"{label} checkpoint", ) ( optimizer_path, optimizer_sha256, optimizer_identity, ) = _immutable_file_sha256_boundary( files.optimizer_path, label=f"{label} optimizer", ) ( external_state_path, external_state_sha256, external_state_identity, ) = _immutable_file_sha256_boundary( files.external_state_path, label=f"{label} external sidecar", ) expected_optimizer_path = checkpoint_path.with_name( f"{checkpoint_path.stem}.optimizer.pt" ) expected_external_path = checkpoint_path.with_name( f"{checkpoint_path.stem}.none.json" ) if ( checkpoint_path.suffix != ".pt" or optimizer_path != expected_optimizer_path or external_state_path != expected_external_path ): raise RuntimeError( f"Resynthesis all-knowledge {label} checkpoint family differs" ) try: sidecar = json.loads(external_state_path.read_text(encoding="utf-8")) except (OSError, UnicodeDecodeError, json.JSONDecodeError) as error: raise RuntimeError( f"Resynthesis all-knowledge {label} external sidecar is malformed" ) from error if _file_sha256_boundary(external_state_path) != external_state_sha256: raise RuntimeError( f"Resynthesis all-knowledge {label} external sidecar changed" ) external_state = ( sidecar.get("externalState") if isinstance(sidecar, dict) else None ) generation_record = ( external_state.get("generationBinding") if isinstance(external_state, dict) else None ) training_proof = ( external_state.get("trainingProof") if isinstance(external_state, dict) else None ) sidecar_schema = ( external_state.get("schema") if isinstance(external_state, dict) else None ) replica_fields = ( "replicaReceiptPath", "replicaReceiptSha256", "replicaStoreRoots", "replicaDurabilityComplete", "canonicalPointerAdvancesLast", ) valid_sha256 = re.compile(r"[0-9a-f]{64}").fullmatch if ( not isinstance(sidecar, dict) or set(sidecar) != { "schema", "checkpointSha256", "optimizerSha256", "externalState", } or sidecar.get("schema") != "nnf.resynthesis.additive_external_checkpoint_binding.v1" or sidecar.get("checkpointSha256") != checkpoint_sha256 or sidecar.get("optimizerSha256") != optimizer_sha256 or not isinstance(external_state, dict) or sidecar_schema not in { "nnf.resynthesis.none_checkpoint_external_state.v1", "nnf.resynthesis.none_checkpoint_external_state.v2", } or not isinstance(external_state.get("compositionPath"), str) or not external_state["compositionPath"] or not Path(external_state["compositionPath"]).expanduser().is_absolute() or valid_sha256(str(external_state.get("compositionSha256", ""))) is None or valid_sha256( str(external_state.get("sourceCheckpointSha256", "")) ) is None or not isinstance(external_state.get("storeRoot"), str) or not external_state["storeRoot"] or not Path(external_state["storeRoot"]).expanduser().is_absolute() or external_state.get("candidatePageUpdate") is not False or not isinstance(generation_record, Mapping) or not isinstance(training_proof, Mapping) or training_proof.get("schema") != "nnf.resynthesis.none_family_training_proof.v1" ): raise RuntimeError( f"Resynthesis all-knowledge {label} external sidecar differs" ) if sidecar_schema == "nnf.resynthesis.none_checkpoint_external_state.v1": if any(field in external_state for field in replica_fields): raise RuntimeError( f"Resynthesis all-knowledge {label} external sidecar differs" ) else: replica_roots = external_state.get("replicaStoreRoots") if ( not isinstance(external_state.get("replicaReceiptPath"), str) or not external_state["replicaReceiptPath"] or not Path( external_state["replicaReceiptPath"] ).expanduser().is_absolute() or valid_sha256( str(external_state.get("replicaReceiptSha256", "")) ) is None or not isinstance(replica_roots, list) or not replica_roots or len(set(replica_roots)) != len(replica_roots) or not all( isinstance(root, str) and bool(root) and Path(root).expanduser().is_absolute() for root in replica_roots ) or type(external_state.get("replicaDurabilityComplete")) is not bool or external_state.get("canonicalPointerAdvancesLast") is not True ): raise RuntimeError( f"Resynthesis all-knowledge {label} external sidecar differs" ) generation = generation_binding_from_record_boundary(generation_record) generation_value = int(generation.generation_t) manifest_payload_sha256 = bytes( generation.manifest_payload_sha256_t.detach().cpu().tolist() ).hex() expected_manifest_path = ( f"generations/generation_{generation_value:08d}_" f"{manifest_payload_sha256}/generation.json" ) if ( not torch.equal( generation.session_id_t.detach().cpu().long().reshape(-1), expected_session_id_t.detach().cpu().long().reshape(-1), ) or int(generation.parent_generation_t) >= generation_value or generation.manifest_relative_path != expected_manifest_path ): raise RuntimeError( f"Resynthesis all-knowledge {label} generation session differs" ) branch_scope_record = external_state.get("branchScope") if expected_scope_sha256_t is None: if ( expected_scope_parent_generation_t is not None or expected_scope_parent_manifest_payload_sha256_t is not None or training_proof.get("branchScopeActive") is not False or branch_scope_record is not None ): raise RuntimeError( f"Resynthesis all-knowledge {label} unexpectedly owns a branch" ) else: proof_scope_record = training_proof.get("branchScope") if ( expected_scope_parent_generation_t is None or expected_scope_parent_manifest_payload_sha256_t is None or training_proof.get("branchScopeActive") is not True or not isinstance(branch_scope_record, Mapping) or branch_scope_record != proof_scope_record ): raise RuntimeError( f"Resynthesis all-knowledge {label} branch scope differs" ) branch_scope = training_branch_scope_from_record_boundary( branch_scope_record ) if not torch.equal( branch_scope.scope_sha256_t.detach() .cpu() .to(dtype=torch.uint8), expected_scope_sha256_t.detach() .cpu() .to(dtype=torch.uint8), ) or not torch.equal( branch_scope.session_id_t.detach().cpu().long().reshape(-1), expected_session_id_t.detach().cpu().long().reshape(-1), ) or not torch.equal( branch_scope.parent_generation_t.detach() .cpu() .long() .reshape(()), expected_scope_parent_generation_t.detach() .cpu() .long() .reshape(()), ) or not torch.equal( branch_scope.parent_manifest_payload_sha256_t.detach() .cpu() .to(dtype=torch.uint8), expected_scope_parent_manifest_payload_sha256_t.detach() .cpu() .to(dtype=torch.uint8), ): raise RuntimeError( f"Resynthesis all-knowledge {label} branch scope differs" ) _require_immutable_file_identity_boundary( checkpoint_path, checkpoint_identity, label=f"{label} checkpoint", ) _require_immutable_file_identity_boundary( optimizer_path, optimizer_identity, label=f"{label} optimizer", ) _require_immutable_file_identity_boundary( external_state_path, external_state_identity, label=f"{label} external sidecar", ) return ( AdditiveKnowledgeCheckpointFiles( checkpoint_path=checkpoint_path, optimizer_path=optimizer_path, external_state_path=external_state_path, ), checkpoint_sha256, optimizer_sha256, external_state_sha256, generation.generation_t.detach().cpu().long().reshape(()).clone(), generation.manifest_sha256_t.detach() .cpu() .to(dtype=torch.uint8) .clone(), generation.manifest_payload_sha256_t.detach() .cpu() .to(dtype=torch.uint8) .clone(), ) def additive_knowledge_reconciliation_authority_from_union_boundary( *, union: object, common_parent_checkpoint_path: Path, target_checkpoint_files: AdditiveKnowledgeCheckpointFiles, branch_checkpoint_files: tuple[AdditiveKnowledgeCheckpointFiles, ...], ) -> AdditiveKnowledgeReconciliationAuthority: """Bind a validated historical union to immutable checkpoint families.""" from resynthesis.none_paging import ( NoNETrainingBranchUnionPacket, _validate_training_branch_union_packet_boundary, ) if ( not isinstance(union, NoNETrainingBranchUnionPacket) or not isinstance(common_parent_checkpoint_path, Path) or not branch_checkpoint_files ): raise RuntimeError( "Resynthesis all-knowledge branch union authority is incomplete" ) _validate_training_branch_union_packet_boundary(union) rebase = union.lineage_rebase if ( rebase is None or union.branch_scope_sha256s_t.shape != (len(branch_checkpoint_files), 32) or rebase.source_parent_generation_t.numel() != 1 or rebase.target_parent_generation_t.numel() != 1 or int(rebase.source_parent_generation_t.detach().cpu().long()) >= int(rebase.target_parent_generation_t.detach().cpu().long()) or not torch.equal( union.parent_generation_t.detach().cpu().long().reshape(()), rebase.target_parent_generation_t.detach() .cpu() .long() .reshape(()), ) or not torch.equal( union.parent_manifest_payload_sha256_t.detach() .cpu() .to(dtype=torch.uint8), rebase.target_parent_manifest_payload_sha256_t.detach() .cpu() .to(dtype=torch.uint8), ) or not torch.equal( union.union_page_ids_t.detach().cpu().long(), rebase.training_page_ids_t.detach().cpu().long(), ) ): raise RuntimeError( "Resynthesis all-knowledge branch union rebase authority differs" ) ( common_parent_checkpoint_path, common_parent_checkpoint_sha256, common_parent_checkpoint_identity, ) = _immutable_file_sha256_boundary( common_parent_checkpoint_path, label="common parent checkpoint", ) ( validated_target_files, target_checkpoint_sha256, target_optimizer_sha256, target_external_state_sha256, target_generation_t, target_manifest_sha256_t, target_manifest_payload_sha256_t, ) = _validated_additive_knowledge_checkpoint_files_boundary( target_checkpoint_files, label="target", expected_session_id_t=union.parent_session_id_t, expected_scope_sha256_t=None, expected_scope_parent_generation_t=None, expected_scope_parent_manifest_payload_sha256_t=None, ) branch_bindings = tuple( _validated_additive_knowledge_checkpoint_files_boundary( files, label=f"branch {branch_index}", expected_session_id_t=union.parent_session_id_t, expected_scope_sha256_t=union.branch_scope_sha256s_t[ branch_index ], expected_scope_parent_generation_t=( rebase.source_parent_generation_t ), expected_scope_parent_manifest_payload_sha256_t=( rebase.source_parent_manifest_payload_sha256_t ), ) for branch_index, files in enumerate(branch_checkpoint_files) ) branch_checkpoint_sha256s = tuple( binding[1] for binding in branch_bindings ) branch_optimizer_sha256s = tuple( binding[2] for binding in branch_bindings ) branch_external_state_sha256s = tuple( binding[3] for binding in branch_bindings ) branch_generations_t = torch.stack( tuple(binding[4] for binding in branch_bindings) ) branch_manifest_sha256s_t = torch.stack( tuple(binding[5] for binding in branch_bindings) ) branch_manifest_payload_sha256s_t = torch.stack( tuple(binding[6] for binding in branch_bindings) ) checkpoint_sha256s = ( common_parent_checkpoint_sha256, target_checkpoint_sha256, *branch_checkpoint_sha256s, ) checkpoint_paths = ( common_parent_checkpoint_path, validated_target_files.checkpoint_path, *(binding[0].checkpoint_path for binding in branch_bindings), ) optimizer_paths = ( validated_target_files.optimizer_path, *(binding[0].optimizer_path for binding in branch_bindings), ) external_state_paths = ( validated_target_files.external_state_path, *(binding[0].external_state_path for binding in branch_bindings), ) if ( len(set(checkpoint_sha256s)) != len(checkpoint_sha256s) or len(set(checkpoint_paths)) != len(checkpoint_paths) or len(set(optimizer_paths)) != len(optimizer_paths) or len(set(external_state_paths)) != len(external_state_paths) or not torch.equal( target_generation_t, rebase.target_parent_generation_t.detach() .cpu() .long() .reshape(()), ) or not torch.equal( target_manifest_payload_sha256_t, rebase.target_parent_manifest_payload_sha256_t.detach() .cpu() .to(dtype=torch.uint8), ) or not torch.equal( branch_generations_t, rebase.branch_source_generations_t.detach().cpu().long(), ) or not torch.equal( branch_manifest_sha256s_t, rebase.branch_source_manifest_sha256s_t.detach() .cpu() .to(dtype=torch.uint8), ) or not torch.equal( branch_manifest_payload_sha256s_t, rebase.branch_source_manifest_payload_sha256s_t.detach() .cpu() .to(dtype=torch.uint8), ) or branch_external_state_sha256s != tuple( bytes(row.tolist()).hex() for row in ( rebase.branch_source_external_state_sha256s_t.detach() .cpu() .to(dtype=torch.uint8) ) ) or branch_optimizer_sha256s != tuple( bytes(row.tolist()).hex() for row in ( rebase.branch_source_optimizer_sha256s_t.detach() .cpu() .to(dtype=torch.uint8) ) ) ): raise RuntimeError( "Resynthesis all-knowledge checkpoint lineage authority differs" ) _require_immutable_file_identity_boundary( common_parent_checkpoint_path, common_parent_checkpoint_identity, label="common parent checkpoint", ) return AdditiveKnowledgeReconciliationAuthority( common_parent_checkpoint_path=common_parent_checkpoint_path, common_parent_checkpoint_sha256=common_parent_checkpoint_sha256, common_parent_generation_t=( rebase.source_parent_generation_t.detach() .cpu() .long() .reshape(()) .clone() ), common_parent_manifest_payload_sha256_t=( rebase.source_parent_manifest_payload_sha256_t.detach() .cpu() .to(dtype=torch.uint8) .clone() ), target_checkpoint_path=validated_target_files.checkpoint_path, target_checkpoint_sha256=target_checkpoint_sha256, target_optimizer_path=validated_target_files.optimizer_path, target_optimizer_sha256=target_optimizer_sha256, target_external_state_path=validated_target_files.external_state_path, target_external_state_sha256=target_external_state_sha256, target_generation_t=( rebase.target_parent_generation_t.detach() .cpu() .long() .reshape(()) .clone() ), target_manifest_sha256_t=target_manifest_sha256_t, target_manifest_payload_sha256_t=( rebase.target_parent_manifest_payload_sha256_t.detach() .cpu() .to(dtype=torch.uint8) .clone() ), branch_checkpoint_paths=tuple( binding[0].checkpoint_path for binding in branch_bindings ), branch_checkpoint_sha256s=branch_checkpoint_sha256s, branch_optimizer_paths=tuple( binding[0].optimizer_path for binding in branch_bindings ), branch_optimizer_sha256s=branch_optimizer_sha256s, branch_external_state_paths=tuple( binding[0].external_state_path for binding in branch_bindings ), branch_external_state_sha256s=branch_external_state_sha256s, branch_generations_t=branch_generations_t, branch_manifest_sha256s_t=branch_manifest_sha256s_t, branch_manifest_payload_sha256s_t=( branch_manifest_payload_sha256s_t ), branch_scope_sha256s_t=( union.branch_scope_sha256s_t.detach() .cpu() .to(dtype=torch.uint8) .clone() ), branch_scope_page_ids_t=( union.union_page_ids_t.detach().cpu().long().clone() ), branch_scope_page_layer_ids_t=( union.union_page_layer_ids_t.detach().cpu().long().clone() ), branch_union_sha256_t=( union.union_sha256_t.detach().cpu().to(dtype=torch.uint8).clone() ), lineage_rebase_sha256_t=( rebase.rebase_sha256_t.detach() .cpu() .to(dtype=torch.uint8) .clone() ), ) def _validated_reconciled_additive_calibration_packet_boundary( packet: AdditiveKnowledgeCalibrationPacket, ) -> tuple[dict[str, torch.Tensor], dict[str, torch.Tensor]]: """Validate one non-publishable direct state before model calibration.""" if not isinstance(packet, AdditiveKnowledgeCalibrationPacket): raise RuntimeError( "Resynthesis reconciled calibration packet is malformed" ) payload = packet.payload proof = packet.proof parameters = payload.get("parameters") if isinstance(payload, dict) else None buffers = payload.get("buffers") if isinstance(payload, dict) else None lineage = payload.get("lineage") if isinstance(payload, dict) else None router_bias_names = packet.router_bias_names if ( not isinstance(payload, dict) or payload.get("schema") != RECONCILED_ADDITIVE_CALIBRATION_PENDING_SCHEMA or payload.get("finalCheckpointSchema") != ADDITIVE_CHECKPOINT_SCHEMA or not isinstance(lineage, dict) or not isinstance(parameters, dict) or not isinstance(buffers, dict) or not all( isinstance(name, str) and isinstance(value, torch.Tensor) for name, value in (*parameters.items(), *buffers.items()) ) or set(parameters).intersection(buffers) or not isinstance(proof, dict) or proof.get("schema") != RECONCILED_ADDITIVE_KNOWLEDGE_PROOF_SCHEMA or not isinstance(router_bias_names, tuple) or not router_bias_names or router_bias_names != tuple(sorted(router_bias_names)) or len(set(router_bias_names)) != len(router_bias_names) or set(router_bias_names) - set(buffers) ): raise RuntimeError( "Resynthesis reconciled calibration packet is incomplete" ) unsigned_proof = { name: value for name, value in proof.items() if name != "proofSha256" } if ( proof.get("proofSha256") != _canonical_json_sha256_boundary(unsigned_proof) or proof.get("routerBiasCount") != len(router_bias_names) or proof.get("routerBiasCalibrationPending") is not True or proof.get("targetFreeCalibrationRequired") is not True or proof.get("directCheckpointPublicationAllowed") is not False ): raise RuntimeError( "Resynthesis reconciled calibration proof differs" ) state = {**parameters, **buffers} state_key_sha256, state_geometry_sha256 = ( _checkpoint_state_identity_boundary(state) ) pending_state_value_sha256 = _checkpoint_state_value_sha256_boundary(state) parameter_key_set_sha256 = hashlib.sha256( "\n".join(sorted(parameters)).encode("utf-8") ).hexdigest() buffer_key_set_sha256 = hashlib.sha256( "\n".join(sorted(buffers)).encode("utf-8") ).hexdigest() physical_page_ids_t = ( packet.physical_page_ids_t.detach().cpu().long().reshape(-1).clone() ) branch_page_ids_t = ( packet.branch_scope_page_ids_t.detach() .cpu() .long() .reshape(-1) .clone() ) physical_page_ids_sha256 = hashlib.sha256( physical_page_ids_t.contiguous().numpy().tobytes() ).hexdigest() branch_page_ids_sha256 = hashlib.sha256( branch_page_ids_t.contiguous().numpy().tobytes() ).hexdigest() if ( packet.physical_page_ids_t.dtype != torch.long or packet.physical_page_ids_t.ndim != 1 or physical_page_ids_t.numel() < 1 or not torch.equal( physical_page_ids_t, torch.sort(physical_page_ids_t).values, ) or torch.unique(physical_page_ids_t).numel() != physical_page_ids_t.numel() or packet.branch_scope_page_ids_t.dtype != torch.long or packet.branch_scope_page_ids_t.ndim != 1 or branch_page_ids_t.numel() < 1 or not torch.equal( branch_page_ids_t, torch.sort(branch_page_ids_t).values, ) or torch.unique(branch_page_ids_t).numel() != branch_page_ids_t.numel() or not bool(torch.isin(branch_page_ids_t, physical_page_ids_t).all()) or proof.get("physicalPageCount") != int(physical_page_ids_t.numel()) or proof.get("minimumPhysicalPageId") != int(physical_page_ids_t[0]) or proof.get("maximumPhysicalPageId") != int(physical_page_ids_t[-1]) or proof.get("physicalPageIdsSha256") != physical_page_ids_sha256 or proof.get("branchOwnedPageCount") != int(branch_page_ids_t.numel()) or proof.get("branchScopePageIdsSha256") != branch_page_ids_sha256 or payload.get("stateKeySetSha256") != state_key_sha256 or payload.get("stateGeometrySha256") != state_geometry_sha256 or proof.get("stateKeySetSha256") != state_key_sha256 or proof.get("stateGeometrySha256") != state_geometry_sha256 or proof.get("parameterKeySetSha256") != parameter_key_set_sha256 or proof.get("bufferKeySetSha256") != buffer_key_set_sha256 ): raise RuntimeError( "Resynthesis reconciled calibration state authority differs" ) router_bias_state = { name: buffers[name] for name in router_bias_names } if any( value.ndim != 1 or value.numel() < 1 or not value.is_floating_point() or not torch.isfinite(value).all() or bool(torch.count_nonzero(value)) for value in router_bias_state.values() ): raise RuntimeError( "Resynthesis reconciled calibration bias seed differs" ) cumulative_state = { name: value for name, value in buffers.items() if name == "science_stack._step_count" or _RECONCILED_PAGED_RUNTIME_BUFFER_PATTERN.match(name) is not None } merged_cumulative_value_sha256 = ( _checkpoint_state_value_sha256_boundary(cumulative_state) ) router_bias_value_sha256 = _checkpoint_state_value_sha256_boundary( router_bias_state ) pending_state_composite = { "targetCheckpointSha256": proof.get("targetCheckpointSha256"), "stateKeySetSha256": state_key_sha256, "stateGeometrySha256": state_geometry_sha256, "parameterKeySetSha256": parameter_key_set_sha256, "bufferKeySetSha256": buffer_key_set_sha256, "pendingStateValueSha256": pending_state_value_sha256, "mergedCumulativeValueSha256": merged_cumulative_value_sha256, "routerBiasValueSha256": router_bias_value_sha256, } if ( proof.get("mergedCumulativeValueSha256") != merged_cumulative_value_sha256 or proof.get("pendingStateValueSha256") != pending_state_value_sha256 or proof.get("routerBiasValueSha256") != router_bias_value_sha256 or proof.get("pendingStateCompositeSha256") != _canonical_json_sha256_boundary(pending_state_composite) ): raise RuntimeError( "Resynthesis reconciled calibration pending-state seal differs" ) return ( { name: value.detach().cpu().clone() for name, value in parameters.items() }, { name: value.detach().cpu().clone() for name, value in buffers.items() }, ) def _validated_calibrated_additive_knowledge_packet_boundary( packet: CalibratedAdditiveKnowledgePacket, ) -> dict[str, Any]: """Recompute every publication seal on one calibrated direct checkpoint.""" if not isinstance(packet, CalibratedAdditiveKnowledgePacket): raise RuntimeError( "Resynthesis calibrated all-knowledge packet is malformed" ) payload = _validated_full_additive_checkpoint_payload_boundary( packet.payload ) reconciliation_proof = packet.reconciliation_proof calibration_proof = packet.calibration_proof calibration_prompt_authority_sha256_t = ( packet.calibration_prompt_authority_sha256_t.detach() .cpu() .to(dtype=torch.uint8) .clone() ) calibration_prompt_authority_sha256 = bytes( calibration_prompt_authority_sha256_t.tolist() ).hex() if ( not isinstance(reconciliation_proof, dict) or reconciliation_proof.get("schema") != RECONCILED_ADDITIVE_KNOWLEDGE_PROOF_SCHEMA or reconciliation_proof.get("proofSha256") != _canonical_json_sha256_boundary( { name: value for name, value in reconciliation_proof.items() if name != "proofSha256" } ) or not isinstance(calibration_proof, dict) or calibration_proof.get("schema") != RECONCILED_ADDITIVE_CALIBRATION_PROOF_SCHEMA or calibration_proof.get("proofSha256") != _canonical_json_sha256_boundary( { name: value for name, value in calibration_proof.items() if name != "proofSha256" } ) or payload.get("reconciliationProofSha256") != reconciliation_proof.get("proofSha256") or payload.get("calibrationProofSha256") != calibration_proof.get("proofSha256") or calibration_proof.get("reconciliationProofSha256") != reconciliation_proof.get("proofSha256") or packet.calibration_prompt_authority_sha256_t.dtype != torch.uint8 or packet.calibration_prompt_authority_sha256_t.device.type != "cpu" or packet.calibration_prompt_authority_sha256_t.shape != (32,) or calibration_proof.get("calibrationPromptAuthoritySha256") != calibration_prompt_authority_sha256 ): raise RuntimeError( "Resynthesis calibrated all-knowledge proof seal differs" ) parameters = cast(dict[str, torch.Tensor], payload["parameters"]) buffers = cast(dict[str, torch.Tensor], payload["buffers"]) state = {**parameters, **buffers} state_key_sha256, state_geometry_sha256 = ( _checkpoint_state_identity_boundary(state) ) state_value_sha256 = _checkpoint_state_value_sha256_boundary(state) lineage_sha256 = _canonical_json_sha256_boundary(payload["lineage"]) router_bias_names = packet.router_bias_names expected_router_bias_names = { name for layer_id in range(18) for name in ( ( f"science_stack.science_layer_{layer_id}." "quantile_router.expert_bias_t" ), ( f"science_stack.science_layer_{layer_id}." "paged_expert_runtime.router.quantile_router.expert_bias_t" ), ) } if ( not isinstance(router_bias_names, tuple) or router_bias_names != tuple(sorted(expected_router_bias_names)) or set(router_bias_names) - set(buffers) or calibration_proof.get("routerBiasCount") != 36 or calibration_proof.get("routerTraversalCount") != 36 or calibration_proof.get("routerBiasNamesSha256") != hashlib.sha256( "\n".join(router_bias_names).encode("utf-8") ).hexdigest() ): raise RuntimeError( "Resynthesis calibrated all-knowledge router authority differs" ) router_bias_state = { name: buffers[name] for name in router_bias_names } for bias_t in router_bias_state.values(): if not bias_t.is_floating_point(): raise RuntimeError( "Resynthesis calibrated all-knowledge router dtype differs" ) centered_tolerance_t = ( torch.finfo(bias_t.dtype).eps * bias_t.detach().abs().amax().clamp_min(1) * 8 ) if ( bias_t.ndim != 1 or bias_t.numel() < 1 or not torch.isfinite(bias_t).all() or bool(bias_t.float().mean().abs().gt(centered_tolerance_t)) ): raise RuntimeError( "Resynthesis calibrated all-knowledge router value differs" ) router_bias_value_sha256 = _checkpoint_state_value_sha256_boundary( router_bias_state ) physical_page_ids_t = ( packet.physical_page_ids_t.detach().cpu().long().reshape(-1).clone() ) branch_page_ids_t = ( packet.branch_scope_page_ids_t.detach() .cpu() .long() .reshape(-1) .clone() ) physical_page_ids_sha256 = hashlib.sha256( physical_page_ids_t.contiguous().numpy().tobytes() ).hexdigest() branch_page_ids_sha256 = hashlib.sha256( branch_page_ids_t.contiguous().numpy().tobytes() ).hexdigest() if ( packet.physical_page_ids_t.dtype != torch.long or packet.physical_page_ids_t.ndim != 1 or physical_page_ids_t.numel() < 1 or not torch.equal( physical_page_ids_t, torch.sort(physical_page_ids_t).values, ) or torch.unique(physical_page_ids_t).numel() != physical_page_ids_t.numel() or packet.branch_scope_page_ids_t.dtype != torch.long or packet.branch_scope_page_ids_t.ndim != 1 or branch_page_ids_t.numel() < 1 or not torch.equal( branch_page_ids_t, torch.sort(branch_page_ids_t).values, ) or torch.unique(branch_page_ids_t).numel() != branch_page_ids_t.numel() or not bool(torch.isin(branch_page_ids_t, physical_page_ids_t).all()) or reconciliation_proof.get("physicalPageCount") != int(physical_page_ids_t.numel()) or reconciliation_proof.get("physicalPageIdsSha256") != physical_page_ids_sha256 or reconciliation_proof.get("branchOwnedPageCount") != int(branch_page_ids_t.numel()) or reconciliation_proof.get("branchScopePageIdsSha256") != branch_page_ids_sha256 or calibration_proof.get("physicalPageCount") != int(physical_page_ids_t.numel()) or calibration_proof.get("physicalPageIdsSha256") != physical_page_ids_sha256 ): raise RuntimeError( "Resynthesis calibrated all-knowledge page authority differs" ) if ( payload.get("stateKeySetSha256") != state_key_sha256 or payload.get("stateGeometrySha256") != state_geometry_sha256 or payload.get("stateValueSha256") != state_value_sha256 or calibration_proof.get("stateKeySetSha256") != state_key_sha256 or calibration_proof.get("stateGeometrySha256") != state_geometry_sha256 or calibration_proof.get("finalStateValueSha256") != state_value_sha256 or calibration_proof.get("routerBiasValueSha256") != router_bias_value_sha256 or calibration_proof.get("currentLineageSha256") != lineage_sha256 or reconciliation_proof.get("stateKeySetSha256") != state_key_sha256 or reconciliation_proof.get("stateGeometrySha256") != state_geometry_sha256 or reconciliation_proof.get("routerBiasCalibrationPending") is not True or reconciliation_proof.get("directCheckpointPublicationAllowed") is not False or calibration_proof.get("calibrationTargetsPresent") is not False or calibration_proof.get("calibrationTargetEnteredForward") is not False or calibration_proof.get("calibrationLossComputed") is not False or calibration_proof.get("optimizerStepExecuted") is not False or calibration_proof.get("targetFreeCalibrationCompleted") is not True or calibration_proof.get("routerBiasCalibrationPending") is not False or calibration_proof.get("directCheckpointPublicationAllowed") is not True or calibration_proof.get("nonRouterStatePreserved") is not True or calibration_proof.get("routerBiasesFinite") is not True or calibration_proof.get("routerBiasesMeanCentered") is not True or calibration_proof.get("coldReloadRequired") is not True or not isinstance( calibration_proof.get("routerTopologySha256"), str, ) or len(calibration_proof["routerTopologySha256"]) != 64 or not isinstance( calibration_proof.get("calibrationInputIdsSha256"), str, ) or len(calibration_proof["calibrationInputIdsSha256"]) != 64 or not isinstance( calibration_proof.get("calibrationInputMaskSha256"), str, ) or len(calibration_proof["calibrationInputMaskSha256"]) != 64 or any( not isinstance(calibration_proof.get(name), str) or re.fullmatch( r"[0-9a-f]{64}", cast(str, calibration_proof.get(name)), ) is None for name in ( "calibrationPromptAuthoritySha256", "calibrationPromptSha256", "calibrationWindowSha256", "calibrationComponentSha256", "calibrationPayloadWorkId", "calibrationSnapshotFileSha256", "calibrationCollectionAuthoritySha256", "calibrationFederationAuthoritySha256", "calibrationExclusionCollectionFileSha256", "calibrationExcludedPriorWorkIdsSha256", "calibrationPhysicalSourceRangesSha256", ) ) or type(calibration_proof.get("calibrationGlobalCursor")) is not int or calibration_proof["calibrationGlobalCursor"] < 0 or not isinstance(calibration_proof.get("fabricPhaseCount"), int) or calibration_proof["fabricPhaseCount"] < 1 or not isinstance( calibration_proof.get("scienceActivePositions"), int, ) or calibration_proof["scienceActivePositions"] < 1 ): raise RuntimeError( "Resynthesis calibrated all-knowledge checkpoint authority differs" ) return payload def _branch_delta_composed_identity_boundary( *, base: Mapping[str, Any], lineage_sha256: str, branch_scope_sha256: str, buffer_key_sha256: str, buffer_geometry_sha256: str, buffer_value_sha256: str, parameter_key_sha256: str | None = None, parameter_geometry_sha256: str | None = None, parameter_value_sha256: str | None = None, functional_graph_parameter_ownership_sha256: str | None = None, functional_graph_parent_genesis_authority_sha256: str | None = None, ) -> dict[str, Any]: """Build the hash-covered identity for a v1 page/functional overlay. Legacy page-only deltas deliberately retain their historical identity. Functional graph owners add an exact parameter/value/ownership fence while preserving the same immutable-parent and one-hop branch-scope contract. """ identity: dict[str, Any] = { "schema": ADDITIVE_BRANCH_DELTA_SCHEMA, "baseCheckpointSha256": base["sha256"], "baseStateKeySetSha256": base["stateKeySetSha256"], "baseStateGeometrySha256": base["stateGeometrySha256"], "overlayBufferKeySetSha256": buffer_key_sha256, "overlayBufferGeometrySha256": buffer_geometry_sha256, "overlayBufferValueSha256": buffer_value_sha256, "lineageSha256": lineage_sha256, "branchScopeSha256": branch_scope_sha256, } parameter_identity = ( parameter_key_sha256, parameter_geometry_sha256, parameter_value_sha256, functional_graph_parameter_ownership_sha256, ) if any(value is not None for value in parameter_identity): if any(value is None for value in parameter_identity): raise RuntimeError( "functional graph branch parameter identity is partial" ) identity.update( { "overlayParameterKeySetSha256": parameter_key_sha256, "overlayParameterGeometrySha256": ( parameter_geometry_sha256 ), "overlayParameterValueSha256": parameter_value_sha256, "functionalGraphParameterOwnershipSha256": ( functional_graph_parameter_ownership_sha256 ), } ) if functional_graph_parent_genesis_authority_sha256 is not None: identity["functionalGraphParentGenesisAuthoritySha256"] = ( functional_graph_parent_genesis_authority_sha256 ) return identity def build_additive_branch_delta_payload_boundary( *, lineage: dict[str, Any], authority: dict[str, Any], parameters: Mapping[str, torch.Tensor], buffers: Mapping[str, torch.Tensor], parameter_universe: Mapping[str, torch.Tensor] | None = None, functional_graph_parent_genesis: dict[str, Any] | None = None, ) -> dict[str, Any]: """Build one strict buffer-only or functional-graph v1 branch overlay.""" base = authority.get("baseCheckpoint") branch_scope = authority.get("branchScope") ownership = authority.get("functionalGraphParameterOwnership") functional_genesis_authority = base.get( "functionalGraphParentGenesisAuthority" ) if isinstance(base, dict) else None parameter_state = dict(parameters) buffer_state = dict(buffers) if ( not isinstance(lineage, dict) or not isinstance(base, dict) or base.get("schema") != ADDITIVE_CHECKPOINT_SCHEMA or not isinstance(branch_scope, dict) or not isinstance(parameter_state, dict) or not all( isinstance(value, torch.Tensor) for value in parameter_state.values() ) or not buffer_state or not all( isinstance(value, torch.Tensor) for value in buffer_state.values() ) or set(parameter_state).intersection(buffer_state) or any(name.startswith("base.") for name in parameter_state) or ((not parameter_state) != (ownership is None)) ): raise RuntimeError( "Resynthesis branch delta source state is incomplete" ) from resynthesis.none_paging import ( training_branch_scope_from_record_boundary, ) training_branch_scope_from_record_boundary(branch_scope) base_identity_fields = ( base.get("sha256"), base.get("stateKeySetSha256"), base.get("stateGeometrySha256"), base.get("bufferKeySetSha256"), base.get("bufferGeometrySha256"), ) if any( not isinstance(value, str) or len(value) != 64 for value in base_identity_fields ): raise RuntimeError( "Resynthesis branch delta parent identity is malformed" ) validated_ownership: dict[str, Any] | None = None validated_functional_genesis: dict[str, torch.Tensor] = {} validated_functional_genesis_buffers: dict[str, torch.Tensor] = {} validated_functional_genesis_authority: dict[str, Any] | None = None functional_genesis_authority_sha256: str | None = None parameter_key_sha256: str | None = None parameter_geometry_sha256: str | None = None parameter_value_sha256: str | None = None ownership_sha256: str | None = None if parameter_state: if parameter_universe is None: raise RuntimeError( "Resynthesis functional branch delta parameter universe is " "absent" ) validated_ownership = ( validate_branch_functional_graph_parameter_ownership_boundary( ownership, parameter_state, parameter_universe=parameter_universe, ) ) if validated_ownership["ownerIndex"] == 0: raise RuntimeError( "causal functional owner requires the v2 checkpoint schema" ) parameter_key_sha256, parameter_geometry_sha256 = ( _checkpoint_state_identity_boundary(parameter_state) ) parameter_value_sha256 = ( _checkpoint_state_value_sha256_boundary(parameter_state) ) ownership_sha256 = _canonical_json_sha256_boundary( validated_ownership ) if ( (functional_graph_parent_genesis is None) != (functional_genesis_authority is None) ): raise RuntimeError( "Resynthesis functional parent genesis authority is partial" ) if functional_graph_parent_genesis is not None: if parameter_universe is None: raise RuntimeError( "Resynthesis functional parent genesis universe is absent" ) ( validated_functional_genesis, validated_functional_genesis_buffers, validated_functional_genesis_authority, ) = _validated_functional_graph_parent_genesis_boundary( functional_graph_parent_genesis, functional_genesis_authority, parameter_universe=parameter_universe, target_lineage=lineage, ) functional_genesis_authority_sha256 = ( _canonical_json_sha256_boundary( validated_functional_genesis_authority ) ) buffer_key_sha256, buffer_geometry_sha256 = ( _checkpoint_state_identity_boundary(buffer_state) ) buffer_value_sha256 = _checkpoint_state_value_sha256_boundary( buffer_state ) lineage_sha256 = _canonical_json_sha256_boundary(lineage) branch_scope_record_sha256 = _canonical_json_sha256_boundary( branch_scope ) composed_identity = _branch_delta_composed_identity_boundary( base=base, lineage_sha256=lineage_sha256, branch_scope_sha256=branch_scope["scopeSha256"], buffer_key_sha256=buffer_key_sha256, buffer_geometry_sha256=buffer_geometry_sha256, buffer_value_sha256=buffer_value_sha256, parameter_key_sha256=parameter_key_sha256, parameter_geometry_sha256=parameter_geometry_sha256, parameter_value_sha256=parameter_value_sha256, functional_graph_parameter_ownership_sha256=ownership_sha256, functional_graph_parent_genesis_authority_sha256=( functional_genesis_authority_sha256 ), ) payload: dict[str, Any] = { "schema": ADDITIVE_BRANCH_DELTA_SCHEMA, "lineage": copy.deepcopy(lineage), "lineageSha256": lineage_sha256, "baseCheckpoint": copy.deepcopy(base), "branchScope": copy.deepcopy(branch_scope), "branchScopeSha256": branch_scope["scopeSha256"], "branchScopeRecordSha256": branch_scope_record_sha256, "sharedDenseGraphMutationAuthority": False, "deltaChainAllowed": False, "overlayBufferKeySetSha256": buffer_key_sha256, "overlayBufferGeometrySha256": buffer_geometry_sha256, "overlayBufferValueSha256": buffer_value_sha256, "composedStateSha256": _canonical_json_sha256_boundary( composed_identity ), "parameters": { name: value.detach().to(device="cpu").clone() for name, value in parameter_state.items() }, "buffers": { name: value.detach().to(device="cpu").clone() for name, value in buffer_state.items() }, } if validated_ownership is not None: assert ( parameter_key_sha256 is not None and parameter_geometry_sha256 is not None and parameter_value_sha256 is not None and ownership_sha256 is not None ) payload.update( { "functionalGraphParameterOwnership": ( validated_ownership ), "functionalGraphParameterOwnershipSha256": ( ownership_sha256 ), "overlayParameterKeySetSha256": parameter_key_sha256, "overlayParameterGeometrySha256": ( parameter_geometry_sha256 ), "overlayParameterValueSha256": parameter_value_sha256, } ) if ( validated_functional_genesis_authority is not None and functional_genesis_authority_sha256 is not None ): assert functional_graph_parent_genesis is not None source_lineage = functional_graph_parent_genesis["sourceLineage"] target_lineage = functional_graph_parent_genesis["targetLineage"] inherited_growth = functional_graph_parent_genesis[ "inheritedGraphGrowth" ] payload["functionalGraphParentGenesis"] = { "schema": FUNCTIONAL_GRAPH_PARENT_GENESIS_SCHEMA, "sourceLineage": copy.deepcopy(source_lineage), "targetLineage": copy.deepcopy(target_lineage), "inheritedGraphGrowth": copy.deepcopy(inherited_growth), "parameters": { name: value.detach().to(device="cpu").clone() for name, value in validated_functional_genesis.items() }, "buffers": { name: value.detach().to(device="cpu").clone() for name, value in ( validated_functional_genesis_buffers.items() ) }, } payload["functionalGraphParentGenesisAuthoritySha256"] = ( functional_genesis_authority_sha256 ) return payload def _branch_causal_delta_composed_identity_boundary( *, base: dict[str, Any], lineage_sha256: str, branch_scope_sha256: str, parameter_key_sha256: str, parameter_geometry_sha256: str, parameter_value_sha256: str, buffer_key_sha256: str, buffer_geometry_sha256: str, buffer_value_sha256: str, functional_graph_parameter_ownership_sha256: str | None = None, functional_graph_parent_genesis_authority_sha256: str | None = None, moving_graph_buffer_growth_sha256: str | None = None, ) -> dict[str, Any]: """Build the exact identity covered by one v2 branch-local overlay.""" identity = { "schema": ADDITIVE_BRANCH_CAUSAL_DELTA_SCHEMA, "baseCheckpointSha256": base["sha256"], "baseStateKeySetSha256": base["stateKeySetSha256"], "baseStateGeometrySha256": base["stateGeometrySha256"], "baseCausalParameterKeySetSha256": ( base["causalParameterKeySetSha256"] ), "baseCausalParameterGeometrySha256": ( base["causalParameterGeometrySha256"] ), "baseCausalParameterValueSha256": ( base["causalParameterValueSha256"] ), "baseCausalBufferKeySetSha256": base["causalBufferKeySetSha256"], "baseCausalBufferGeometrySha256": ( base["causalBufferGeometrySha256"] ), "baseCausalBufferValueSha256": base["causalBufferValueSha256"], "causalParameterAuthorityMode": base["causalParameterAuthorityMode"], "parentCausalParameterPresenceCount": ( base["parentCausalParameterPresenceCount"] ), "expectedCausalParameterCount": base["expectedCausalParameterCount"], "causalParameterInitialization": base.get( "causalParameterInitialization" ), "causalParameterGenesisSourceLineageSha256": base.get( "causalParameterGenesisSourceLineageSha256" ), "overlayParameterKeySetSha256": parameter_key_sha256, "overlayParameterGeometrySha256": parameter_geometry_sha256, "overlayParameterValueSha256": parameter_value_sha256, "overlayBufferKeySetSha256": buffer_key_sha256, "overlayBufferGeometrySha256": buffer_geometry_sha256, "overlayBufferValueSha256": buffer_value_sha256, "lineageSha256": lineage_sha256, "branchScopeSha256": branch_scope_sha256, } if moving_graph_buffer_growth_sha256 is not None: identity["movingGraphBufferGrowthSha256"] = ( moving_graph_buffer_growth_sha256 ) if functional_graph_parameter_ownership_sha256 is not None: identity["functionalGraphParameterOwnershipSha256"] = ( functional_graph_parameter_ownership_sha256 ) if functional_graph_parent_genesis_authority_sha256 is not None: identity["functionalGraphParentGenesisAuthoritySha256"] = ( functional_graph_parent_genesis_authority_sha256 ) return identity def build_additive_branch_causal_delta_payload_boundary( *, lineage: dict[str, Any], authority: dict[str, Any], parameters: dict[str, torch.Tensor], buffers: dict[str, torch.Tensor], base_causal_parameters: dict[str, torch.Tensor] | None = None, base_causal_buffers: dict[str, torch.Tensor] | None = None, parent_buffer_geometry: Mapping[ str, tuple[tuple[int, ...], torch.dtype], ] | None = None, parameter_universe: Mapping[str, torch.Tensor] | None = None, moving_graph_buffer_growth: dict[str, Any] | None = None, functional_graph_parent_genesis: dict[str, Any] | None = None, ) -> dict[str, Any]: """Build a strict v2 payload at the explicit checkpoint I/O boundary.""" base = authority.get("baseCheckpoint") branch_scope = authority.get("branchScope") functional_ownership = authority.get( "functionalGraphParameterOwnership" ) functional_genesis_authority = base.get( "functionalGraphParentGenesisAuthority" ) if isinstance(base, dict) else None if ( not isinstance(lineage, dict) or not isinstance(base, dict) or base.get("schema") != ADDITIVE_CHECKPOINT_SCHEMA or not isinstance(branch_scope, dict) or not all( isinstance(value, torch.Tensor) for value in parameters.values() ) or not buffers or not all(isinstance(value, torch.Tensor) for value in buffers.values()) or set(parameters).intersection(buffers) ): raise RuntimeError( "Resynthesis branch causal delta source state is incomplete" ) validated_ownership: dict[str, Any] | None = None validated_functional_genesis: dict[str, torch.Tensor] = {} validated_functional_genesis_buffers: dict[str, torch.Tensor] = {} validated_functional_genesis_authority: dict[str, Any] | None = None functional_genesis_authority_sha256: str | None = None if functional_ownership is None: if set(parameters) != BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES: raise RuntimeError( "Resynthesis branch causal delta payload is incomplete" ) else: if parameter_universe is None: raise RuntimeError( "Resynthesis functional causal delta parameter universe is " "absent" ) validated_ownership = ( validate_branch_functional_graph_parameter_ownership_boundary( functional_ownership, parameters, parameter_universe=parameter_universe, ) ) if ( validated_ownership["ownerIndex"] != 0 or not BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES.issubset(parameters) or any(name.startswith("base.") for name in parameters) ): raise RuntimeError( "Resynthesis functional causal delta ownership differs" ) if ( (functional_graph_parent_genesis is None) != (functional_genesis_authority is None) ): raise RuntimeError( "Resynthesis functional parent genesis authority is partial" ) if functional_graph_parent_genesis is not None: if parameter_universe is None: raise RuntimeError( "Resynthesis functional parent genesis universe is absent" ) ( validated_functional_genesis, validated_functional_genesis_buffers, validated_functional_genesis_authority, ) = _validated_functional_graph_parent_genesis_boundary( functional_graph_parent_genesis, functional_genesis_authority, parameter_universe=parameter_universe, target_lineage=lineage, ) functional_genesis_authority_sha256 = ( _canonical_json_sha256_boundary( validated_functional_genesis_authority ) ) from resynthesis.none_paging import ( training_branch_scope_from_record_boundary, ) training_branch_scope_from_record_boundary(branch_scope) base_identity_fields = ( base.get("sha256"), base.get("stateKeySetSha256"), base.get("stateGeometrySha256"), base.get("bufferKeySetSha256"), base.get("bufferGeometrySha256"), base.get("causalParameterKeySetSha256"), base.get("causalParameterGeometrySha256"), base.get("causalParameterValueSha256"), base.get("causalBufferKeySetSha256"), base.get("causalBufferGeometrySha256"), base.get("causalBufferValueSha256"), ) if any( not isinstance(value, str) or len(value) != 64 for value in base_identity_fields ): raise RuntimeError( "Resynthesis branch causal delta parent identity is malformed" ) authority_mode = base.get("causalParameterAuthorityMode") parent_presence_count = base.get("parentCausalParameterPresenceCount") expected_causal_count = base.get("expectedCausalParameterCount") genesis_parameters: dict[str, torch.Tensor] | None = None genesis_buffers: dict[str, torch.Tensor] | None = None if authority_mode == BRANCH_CAUSAL_PARENT_AUTHORITY_RESIDENT: if ( parent_presence_count != len(BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES) or expected_causal_count != len(BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES) or base.get("causalParameterInitialization") is not None or base.get("causalParameterGenesisSourceLineageSha256") is not None or base_causal_parameters is not None or base_causal_buffers is not None ): raise RuntimeError( "Resynthesis branch causal delta resident authority differs" ) elif authority_mode == BRANCH_CAUSAL_PARENT_AUTHORITY_GENESIS: genesis_source_lineage_sha256 = base.get( "causalParameterGenesisSourceLineageSha256" ) if ( parent_presence_count != 0 or expected_causal_count != len(BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES) or base.get("causalParameterInitialization") != CAUSAL_PARENT_INITIALIZATION_SCHEMA or not isinstance(genesis_source_lineage_sha256, str) or len(genesis_source_lineage_sha256) != 64 ): raise RuntimeError( "Resynthesis branch causal delta genesis authority differs" ) genesis_parameters = ( _validated_deterministic_causal_parent_parameters_boundary( base_causal_parameters ) ) genesis_buffers = ( _validated_deterministic_causal_parent_buffers_boundary( genesis_parameters, base_causal_buffers, ) ) else: raise RuntimeError( "Resynthesis branch causal delta parent authority mode differs" ) causal_parameters = { name: parameters[name] for name in BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES } causal_parameter_key_sha256, causal_parameter_geometry_sha256 = ( _checkpoint_state_identity_boundary(causal_parameters) ) parameter_key_sha256, parameter_geometry_sha256 = ( _checkpoint_state_identity_boundary(parameters) ) parameter_value_sha256 = _checkpoint_state_value_sha256_boundary( parameters ) buffer_key_sha256, buffer_geometry_sha256 = ( _checkpoint_state_identity_boundary(buffers) ) buffer_value_sha256 = _checkpoint_state_value_sha256_boundary(buffers) lineage_sha256 = _canonical_json_sha256_boundary(lineage) validated_moving_graph_growth: dict[str, Any] | None = None if moving_graph_buffer_growth is not None: if parent_buffer_geometry is None: raise RuntimeError( "Resynthesis branch causal delta moving-graph parent " "geometry is absent" ) validated_moving_graph_growth = ( _validated_branch_causal_moving_graph_buffer_growth_boundary( moving_graph_buffer_growth, parent_geometry=parent_buffer_geometry, buffers=buffers, ) ) moving_graph_growth_sha256 = ( _canonical_json_sha256_boundary(validated_moving_graph_growth) if validated_moving_graph_growth is not None else None ) if ( causal_parameter_key_sha256 != base["causalParameterKeySetSha256"] or causal_parameter_geometry_sha256 != base["causalParameterGeometrySha256"] or buffer_key_sha256 != base["bufferKeySetSha256"] or ( buffer_geometry_sha256 != base["bufferGeometrySha256"] and validated_moving_graph_growth is None ) ): raise RuntimeError( "Resynthesis branch causal delta overlay geometry differs" ) if genesis_parameters is not None and genesis_buffers is not None: genesis_parameter_key_sha256, genesis_parameter_geometry_sha256 = ( _checkpoint_state_identity_boundary(genesis_parameters) ) genesis_parameter_value_sha256 = ( _checkpoint_state_value_sha256_boundary(genesis_parameters) ) genesis_buffer_key_sha256, genesis_buffer_geometry_sha256 = ( _checkpoint_state_identity_boundary(genesis_buffers) ) genesis_buffer_value_sha256 = ( _checkpoint_state_value_sha256_boundary(genesis_buffers) ) expected_genesis_lineage_sha256 = ( _causal_parent_genesis_source_lineage_sha256_boundary( base_checkpoint_sha256=str(base["sha256"]), lineage_sha256=( str( validated_functional_genesis_authority[ "sourceLineageSha256" ] ) if validated_functional_genesis_authority is not None else lineage_sha256 ), parameter_key_sha256=genesis_parameter_key_sha256, parameter_geometry_sha256=( genesis_parameter_geometry_sha256 ), parameter_value_sha256=genesis_parameter_value_sha256, buffer_key_sha256=genesis_buffer_key_sha256, buffer_geometry_sha256=genesis_buffer_geometry_sha256, buffer_value_sha256=genesis_buffer_value_sha256, ) ) if ( genesis_parameter_key_sha256 != base["causalParameterKeySetSha256"] or genesis_parameter_geometry_sha256 != base["causalParameterGeometrySha256"] or genesis_parameter_value_sha256 != base["causalParameterValueSha256"] or genesis_buffer_key_sha256 != base["causalBufferKeySetSha256"] or genesis_buffer_geometry_sha256 != base["causalBufferGeometrySha256"] or genesis_buffer_value_sha256 != base["causalBufferValueSha256"] or expected_genesis_lineage_sha256 != base["causalParameterGenesisSourceLineageSha256"] ): raise RuntimeError( "Resynthesis branch causal delta genesis identity differs" ) branch_scope_record_sha256 = _canonical_json_sha256_boundary(branch_scope) branch_scope_sha256 = branch_scope.get("scopeSha256") if not isinstance(branch_scope_sha256, str) or len(branch_scope_sha256) != 64: raise RuntimeError( "Resynthesis branch causal delta scope identity is malformed" ) composed_identity = _branch_causal_delta_composed_identity_boundary( base=base, lineage_sha256=lineage_sha256, branch_scope_sha256=branch_scope_sha256, parameter_key_sha256=parameter_key_sha256, parameter_geometry_sha256=parameter_geometry_sha256, parameter_value_sha256=parameter_value_sha256, buffer_key_sha256=buffer_key_sha256, buffer_geometry_sha256=buffer_geometry_sha256, buffer_value_sha256=buffer_value_sha256, functional_graph_parameter_ownership_sha256=( _canonical_json_sha256_boundary(validated_ownership) if validated_ownership is not None else None ), functional_graph_parent_genesis_authority_sha256=( functional_genesis_authority_sha256 ), moving_graph_buffer_growth_sha256=moving_graph_growth_sha256, ) payload: dict[str, Any] = { "schema": ADDITIVE_BRANCH_CAUSAL_DELTA_SCHEMA, "lineage": copy.deepcopy(lineage), "lineageSha256": lineage_sha256, "baseCheckpoint": copy.deepcopy(base), "branchScope": copy.deepcopy(branch_scope), "branchScopeSha256": branch_scope_sha256, "branchScopeRecordSha256": branch_scope_record_sha256, "sharedDenseGraphMutationAuthority": False, "branchLocalCausalControlOverlay": True, "deltaChainAllowed": False, "overlayParameterKeySetSha256": parameter_key_sha256, "overlayParameterGeometrySha256": parameter_geometry_sha256, "overlayParameterValueSha256": parameter_value_sha256, "overlayBufferKeySetSha256": buffer_key_sha256, "overlayBufferGeometrySha256": buffer_geometry_sha256, "overlayBufferValueSha256": buffer_value_sha256, "composedStateSha256": _canonical_json_sha256_boundary( composed_identity ), "parameters": { name: value.detach().to(device="cpu").clone() for name, value in parameters.items() }, "buffers": { name: value.detach().to(device="cpu").clone() for name, value in buffers.items() }, } if validated_ownership is not None: payload["functionalGraphParameterOwnership"] = copy.deepcopy( validated_ownership ) payload["functionalGraphParameterOwnershipSha256"] = ( _canonical_json_sha256_boundary(validated_ownership) ) if genesis_parameters is not None and genesis_buffers is not None: payload["causalParentGenesis"] = { "schema": CAUSAL_PARENT_INITIALIZATION_SCHEMA, "parameters": { name: value.detach().to(device="cpu").clone() for name, value in genesis_parameters.items() }, "buffers": { name: value.detach().to(device="cpu").clone() for name, value in genesis_buffers.items() }, } if ( validated_functional_genesis_authority is not None and functional_genesis_authority_sha256 is not None ): assert functional_graph_parent_genesis is not None source_lineage = functional_graph_parent_genesis["sourceLineage"] target_lineage = functional_graph_parent_genesis["targetLineage"] inherited_growth = functional_graph_parent_genesis[ "inheritedGraphGrowth" ] payload["functionalGraphParentGenesis"] = { "schema": FUNCTIONAL_GRAPH_PARENT_GENESIS_SCHEMA, "sourceLineage": copy.deepcopy(source_lineage), "targetLineage": copy.deepcopy(target_lineage), "inheritedGraphGrowth": copy.deepcopy(inherited_growth), "parameters": { name: value.detach().to(device="cpu").clone() for name, value in validated_functional_genesis.items() }, "buffers": { name: value.detach().to(device="cpu").clone() for name, value in ( validated_functional_genesis_buffers.items() ) }, } payload["functionalGraphParentGenesisAuthoritySha256"] = ( functional_genesis_authority_sha256 ) if ( validated_moving_graph_growth is not None and moving_graph_growth_sha256 is not None ): payload["movingGraphBufferGrowth"] = copy.deepcopy( validated_moving_graph_growth ) payload["movingGraphBufferGrowthSha256"] = ( moving_graph_growth_sha256 ) return payload def validate_additive_branch_delta_boundary( checkpoint_path: str | Path, *, expected_artifact_sha256: str | None = None, identity_cache_root: Path | None = None, ) -> AdditiveBranchDeltaResolution: """Validate one one-hop page-branch overlay without loading its full parent.""" artifact_path = Path(checkpoint_path).expanduser().resolve() if not artifact_path.is_file(): raise RuntimeError("Resynthesis branch delta checkpoint is missing") artifact_sha256 = _verified_checkpoint_file_sha256_boundary( artifact_path, expected_artifact_sha256, identity_cache_root=identity_cache_root, ) payload = torch.load( artifact_path, map_location="cpu", mmap=True, weights_only=True, ) base = payload.get("baseCheckpoint") if isinstance(payload, dict) else None lineage = payload.get("lineage") if isinstance(payload, dict) else None branch_scope = payload.get("branchScope") if isinstance(payload, dict) else None parameters = payload.get("parameters") if isinstance(payload, dict) else None buffers = payload.get("buffers") if isinstance(payload, dict) else None functional_ownership = ( payload.get("functionalGraphParameterOwnership") if isinstance(payload, dict) else None ) functional_parent_genesis = ( payload.get("functionalGraphParentGenesis") if isinstance(payload, dict) else None ) functional_genesis_authority = ( base.get("functionalGraphParentGenesisAuthority") if isinstance(base, dict) else None ) if ( not isinstance(payload, dict) or payload.get("schema") != ADDITIVE_BRANCH_DELTA_SCHEMA or payload.get("deltaChainAllowed") is not False or payload.get("sharedDenseGraphMutationAuthority") is not False or not isinstance(base, dict) or base.get("schema") != ADDITIVE_CHECKPOINT_SCHEMA or not isinstance(lineage, dict) or not isinstance(branch_scope, dict) or not isinstance(parameters, dict) or not all( isinstance(value, torch.Tensor) for value in parameters.values() ) or not isinstance(buffers, dict) or not buffers or not all(isinstance(value, torch.Tensor) for value in buffers.values()) or set(parameters).intersection(buffers) or any(name.startswith("base.") for name in parameters) or ((not parameters) != (functional_ownership is None)) or ( (functional_parent_genesis is None) != (functional_genesis_authority is None) ) ): raise RuntimeError("Resynthesis branch delta payload is incomplete") from resynthesis.none_paging import ( training_branch_scope_from_record_boundary, ) training_branch_scope_from_record_boundary(branch_scope) base_path_value = base.get("path") base_sha256 = base.get("sha256") identity_fields: tuple[object, ...] = ( base.get("stateKeySetSha256"), base.get("stateGeometrySha256"), base.get("bufferKeySetSha256"), base.get("bufferGeometrySha256"), payload.get("overlayBufferKeySetSha256"), payload.get("overlayBufferGeometrySha256"), payload.get("overlayBufferValueSha256"), payload.get("lineageSha256"), payload.get("branchScopeRecordSha256"), payload.get("composedStateSha256"), ) if parameters: identity_fields = ( *identity_fields, payload.get("overlayParameterKeySetSha256"), payload.get("overlayParameterGeometrySha256"), payload.get("overlayParameterValueSha256"), payload.get("functionalGraphParameterOwnershipSha256"), ) if functional_parent_genesis is not None: identity_fields = ( *identity_fields, payload.get( "functionalGraphParentGenesisAuthoritySha256" ), ) if ( not isinstance(base_path_value, str) or not base_path_value or not isinstance(base_sha256, str) or len(base_sha256) != 64 or any( not isinstance(value, str) or len(value) != 64 for value in identity_fields ) or branch_scope.get("scopeSha256") != payload.get("branchScopeSha256") or payload.get("branchScopeRecordSha256") != _canonical_json_sha256_boundary(branch_scope) or payload.get("lineageSha256") != _canonical_json_sha256_boundary(lineage) ): raise RuntimeError("Resynthesis branch delta identity is malformed") base_path = Path(base_path_value).expanduser().resolve() try: parent_sha256 = ( _verified_checkpoint_file_sha256_boundary( base_path, base_sha256, identity_cache_root=identity_cache_root, ) if base_path.is_file() and base_path != artifact_path else "" ) except RuntimeError as error: raise RuntimeError( "Resynthesis branch delta parent identity differs" ) from error if parent_sha256 != base_sha256: raise RuntimeError("Resynthesis branch delta parent identity differs") try: parent_payload = torch.load( base_path, map_location="meta", mmap=True, weights_only=True, ) validated_parent = _validated_full_additive_checkpoint_payload_boundary( parent_payload ) except (OSError, RuntimeError, ValueError) as error: raise RuntimeError( "Resynthesis branch delta parent identity differs" ) from error parent_parameters = validated_parent["parameters"] parent_buffers = validated_parent["buffers"] assert isinstance(parent_parameters, dict) assert isinstance(parent_buffers, dict) parent_state = {**parent_parameters, **parent_buffers} parent_key_sha256, parent_geometry_sha256 = ( _checkpoint_state_identity_boundary(parent_state) ) functional_genesis_parameters: dict[str, torch.Tensor] = {} functional_genesis_buffers: dict[str, torch.Tensor] = {} validated_functional_genesis_authority: dict[str, Any] | None = None functional_genesis_authority_sha256: str | None = None if functional_parent_genesis is not None: raw_genesis_parameters = ( functional_parent_genesis.get("parameters") if isinstance(functional_parent_genesis, dict) else None ) raw_genesis_buffers = ( functional_parent_genesis.get("buffers") if isinstance(functional_parent_genesis, dict) else None ) if ( not isinstance(raw_genesis_parameters, dict) or not isinstance(raw_genesis_buffers, dict) or set(raw_genesis_buffers).intersection(parent_buffers) ): raise RuntimeError( "Resynthesis functional parent genesis escapes its parent" ) overlapping_genesis_names = set(raw_genesis_parameters).intersection( parent_parameters ) if overlapping_genesis_names: parent_payload_cpu = torch.load( base_path, map_location="cpu", mmap=True, weights_only=True, ) validated_parent_cpu = ( _validated_full_additive_checkpoint_payload_boundary( parent_payload_cpu ) ) parent_parameters_cpu = validated_parent_cpu["parameters"] assert isinstance(parent_parameters_cpu, dict) _validate_functional_graph_parent_genesis_overlap_boundary( source_parameters={ name: parent_parameters_cpu[name] for name in overlapping_genesis_names }, genesis_parameters={ name: raw_genesis_parameters[name] for name in overlapping_genesis_names }, ) genesis_parameter_universe = { **parent_parameters, **raw_genesis_parameters, } ( functional_genesis_parameters, functional_genesis_buffers, validated_functional_genesis_authority, ) = _validated_functional_graph_parent_genesis_boundary( functional_parent_genesis, functional_genesis_authority, parameter_universe=genesis_parameter_universe, target_lineage=lineage, expected_source_lineage=validated_parent["lineage"], parent_buffers=parent_buffers, ) functional_genesis_authority_sha256 = ( _canonical_json_sha256_boundary( validated_functional_genesis_authority ) ) if ( payload.get("functionalGraphParentGenesisAuthoritySha256") != functional_genesis_authority_sha256 ): raise RuntimeError( "Resynthesis functional parent genesis identity differs" ) parameter_universe = { **parent_parameters, **functional_genesis_parameters, } if not set(parameters).issubset(parameter_universe): raise RuntimeError( "Resynthesis branch delta parameter set escapes its parent" ) if not set(buffers).issubset(parent_buffers): raise RuntimeError("Resynthesis branch delta buffer set escapes its parent") parent_parameter_overlay = { name: parameter_universe[name] for name in parameters } parent_overlay = {name: parent_buffers[name] for name in buffers} parent_buffer_key_sha256, parent_buffer_geometry_sha256 = ( _checkpoint_state_identity_boundary(parent_overlay) ) if ( parent_key_sha256 != base.get("stateKeySetSha256") or parent_geometry_sha256 != base.get("stateGeometrySha256") or parent_buffer_key_sha256 != base.get("bufferKeySetSha256") or parent_buffer_geometry_sha256 != base.get("bufferGeometrySha256") or ( functional_parent_genesis is None and validated_parent.get("lineage") != lineage ) ): raise RuntimeError("Resynthesis branch delta parent contract differs") parameter_key_sha256: str | None = None parameter_geometry_sha256: str | None = None parameter_value_sha256: str | None = None ownership_sha256: str | None = None validated_ownership: dict[str, Any] | None = None if parameters: for name, parent_value in parent_parameter_overlay.items(): overlay_value = parameters[name] if ( overlay_value.shape != parent_value.shape or overlay_value.dtype != parent_value.dtype ): raise RuntimeError( "Resynthesis branch delta parameter geometry differs: " f"{name}" ) validated_ownership = ( validate_branch_functional_graph_parameter_ownership_boundary( functional_ownership, parameters, parameter_universe=parameter_universe, ) ) if validated_ownership["ownerIndex"] == 0: raise RuntimeError( "Resynthesis causal functional owner requires v2 checkpoint" ) parameter_key_sha256, parameter_geometry_sha256 = ( _checkpoint_state_identity_boundary(parameters) ) parameter_value_sha256 = ( _checkpoint_state_value_sha256_boundary(parameters) ) ownership_sha256 = _canonical_json_sha256_boundary( validated_ownership ) if ( payload.get("overlayParameterKeySetSha256") != parameter_key_sha256 or payload.get("overlayParameterGeometrySha256") != parameter_geometry_sha256 or payload.get("overlayParameterValueSha256") != parameter_value_sha256 or payload.get("functionalGraphParameterOwnershipSha256") != ownership_sha256 ): raise RuntimeError( "Resynthesis branch delta functional parameter identity differs" ) overlay_key_sha256, overlay_geometry_sha256 = ( _checkpoint_state_identity_boundary(buffers) ) overlay_value_sha256 = _checkpoint_state_value_sha256_boundary(buffers) if ( payload.get("overlayBufferKeySetSha256") != overlay_key_sha256 or payload.get("overlayBufferGeometrySha256") != overlay_geometry_sha256 or payload.get("overlayBufferValueSha256") != overlay_value_sha256 ): raise RuntimeError("Resynthesis branch delta overlay identity differs") composed_identity = _branch_delta_composed_identity_boundary( base=base, lineage_sha256=payload["lineageSha256"], branch_scope_sha256=payload["branchScopeSha256"], buffer_key_sha256=overlay_key_sha256, buffer_geometry_sha256=overlay_geometry_sha256, buffer_value_sha256=overlay_value_sha256, parameter_key_sha256=parameter_key_sha256, parameter_geometry_sha256=parameter_geometry_sha256, parameter_value_sha256=parameter_value_sha256, functional_graph_parameter_ownership_sha256=ownership_sha256, functional_graph_parent_genesis_authority_sha256=( functional_genesis_authority_sha256 ), ) composed_state_sha256 = _canonical_json_sha256_boundary(composed_identity) if payload.get("composedStateSha256") != composed_state_sha256: raise RuntimeError("Resynthesis branch delta composed identity differs") return AdditiveBranchDeltaResolution( artifact_path=artifact_path, artifact_sha256=artifact_sha256, base_checkpoint_path=base_path, base_checkpoint_sha256=base_sha256, base_state_key_sha256=str(base["stateKeySetSha256"]), base_state_geometry_sha256=str(base["stateGeometrySha256"]), base_buffer_key_sha256=str(base["bufferKeySetSha256"]), base_buffer_geometry_sha256=str(base["bufferGeometrySha256"]), lineage=dict(lineage), branch_scope=dict(branch_scope), parameters=dict(parameters), buffers=dict(buffers), functional_graph_parameter_ownership=( copy.deepcopy(validated_ownership) if validated_ownership is not None else None ), functional_graph_parent_genesis_authority=( copy.deepcopy(validated_functional_genesis_authority) if validated_functional_genesis_authority is not None else None ), functional_graph_parent_genesis_parameters={ name: value.detach().to(device="cpu").clone() for name, value in functional_genesis_parameters.items() }, functional_graph_parent_genesis_buffers={ name: value.detach().to(device="cpu").clone() for name, value in functional_genesis_buffers.items() }, composed_state_sha256=composed_state_sha256, ) def validate_additive_branch_causal_delta_boundary( checkpoint_path: str | Path, *, expected_artifact_sha256: str | None = None, identity_cache_root: Path | None = None, ) -> AdditiveBranchCausalDeltaResolution: """Validate one v2 branch-local causal overlay and its immutable parent.""" artifact_path = Path(checkpoint_path).expanduser().resolve() if not artifact_path.is_file(): raise RuntimeError( "Resynthesis branch causal delta checkpoint is missing" ) artifact_sha256 = _verified_checkpoint_file_sha256_boundary( artifact_path, expected_artifact_sha256, identity_cache_root=identity_cache_root, ) payload = torch.load( artifact_path, map_location="cpu", mmap=True, weights_only=True, ) base = payload.get("baseCheckpoint") if isinstance(payload, dict) else None lineage = payload.get("lineage") if isinstance(payload, dict) else None branch_scope = ( payload.get("branchScope") if isinstance(payload, dict) else None ) parameters = payload.get("parameters") if isinstance(payload, dict) else None buffers = payload.get("buffers") if isinstance(payload, dict) else None functional_ownership = ( payload.get("functionalGraphParameterOwnership") if isinstance(payload, dict) else None ) functional_parent_genesis = ( payload.get("functionalGraphParentGenesis") if isinstance(payload, dict) else None ) functional_genesis_authority = ( base.get("functionalGraphParentGenesisAuthority") if isinstance(base, dict) else None ) causal_parent_genesis = ( payload.get("causalParentGenesis") if isinstance(payload, dict) else None ) moving_graph_buffer_growth = ( payload.get("movingGraphBufferGrowth") if isinstance(payload, dict) else None ) moving_graph_buffer_growth_sha256 = ( payload.get("movingGraphBufferGrowthSha256") if isinstance(payload, dict) else None ) if ( not isinstance(payload, dict) or payload.get("schema") != ADDITIVE_BRANCH_CAUSAL_DELTA_SCHEMA or payload.get("deltaChainAllowed") is not False or payload.get("sharedDenseGraphMutationAuthority") is not False or payload.get("branchLocalCausalControlOverlay") is not True or not isinstance(base, dict) or base.get("schema") != ADDITIVE_CHECKPOINT_SCHEMA or not isinstance(lineage, dict) or not isinstance(branch_scope, dict) or not isinstance(parameters, dict) or not all( isinstance(value, torch.Tensor) for value in parameters.values() ) or not isinstance(buffers, dict) or not buffers or not all(isinstance(value, torch.Tensor) for value in buffers.values()) or set(parameters).intersection(buffers) or ( (functional_parent_genesis is None) != (functional_genesis_authority is None) ) ): raise RuntimeError( "Resynthesis branch causal delta payload is incomplete" ) if functional_ownership is None: if set(parameters) != BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES: raise RuntimeError( "Resynthesis branch causal delta payload is incomplete" ) elif ( not BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES.issubset(parameters) or any(name.startswith("base.") for name in parameters) ): raise RuntimeError( "Resynthesis functional causal delta parameter set differs" ) from resynthesis.none_paging import ( training_branch_scope_from_record_boundary, ) training_branch_scope_from_record_boundary(branch_scope) base_path_value = base.get("path") base_sha256 = base.get("sha256") identity_fields: tuple[object, ...] = ( base_sha256, base.get("stateKeySetSha256"), base.get("stateGeometrySha256"), base.get("bufferKeySetSha256"), base.get("bufferGeometrySha256"), base.get("causalParameterKeySetSha256"), base.get("causalParameterGeometrySha256"), base.get("causalParameterValueSha256"), base.get("causalBufferKeySetSha256"), base.get("causalBufferGeometrySha256"), base.get("causalBufferValueSha256"), payload.get("overlayParameterKeySetSha256"), payload.get("overlayParameterGeometrySha256"), payload.get("overlayParameterValueSha256"), payload.get("overlayBufferKeySetSha256"), payload.get("overlayBufferGeometrySha256"), payload.get("overlayBufferValueSha256"), payload.get("lineageSha256"), payload.get("branchScopeRecordSha256"), payload.get("composedStateSha256"), ) if functional_ownership is not None: identity_fields = ( *identity_fields, payload.get("functionalGraphParameterOwnershipSha256"), ) if functional_parent_genesis is not None: identity_fields = ( *identity_fields, payload.get( "functionalGraphParentGenesisAuthoritySha256" ), ) if ( not isinstance(base_path_value, str) or not base_path_value or any( not isinstance(value, str) or len(value) != 64 for value in identity_fields ) or branch_scope.get("scopeSha256") != payload.get("branchScopeSha256") or payload.get("branchScopeRecordSha256") != _canonical_json_sha256_boundary(branch_scope) or payload.get("lineageSha256") != _canonical_json_sha256_boundary(lineage) ): raise RuntimeError( "Resynthesis branch causal delta identity is malformed" ) if ( (moving_graph_buffer_growth is None) != (moving_graph_buffer_growth_sha256 is None) or ( moving_graph_buffer_growth is not None and ( not isinstance(moving_graph_buffer_growth_sha256, str) or len(moving_graph_buffer_growth_sha256) != 64 or moving_graph_buffer_growth_sha256 != _canonical_json_sha256_boundary( moving_graph_buffer_growth ) ) ) ): raise RuntimeError( "Resynthesis branch causal delta moving-graph identity differs" ) authority_mode = base.get("causalParameterAuthorityMode") parent_presence_count = base.get("parentCausalParameterPresenceCount") expected_causal_count = base.get("expectedCausalParameterCount") if authority_mode == BRANCH_CAUSAL_PARENT_AUTHORITY_RESIDENT: if ( parent_presence_count != len(BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES) or expected_causal_count != len(BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES) or base.get("causalParameterInitialization") is not None or base.get("causalParameterGenesisSourceLineageSha256") is not None or causal_parent_genesis is not None ): raise RuntimeError( "Resynthesis branch causal delta resident authority differs" ) elif authority_mode == BRANCH_CAUSAL_PARENT_AUTHORITY_GENESIS: genesis_lineage_sha256 = base.get( "causalParameterGenesisSourceLineageSha256" ) if ( parent_presence_count != 0 or expected_causal_count != len(BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES) or base.get("causalParameterInitialization") != CAUSAL_PARENT_INITIALIZATION_SCHEMA or not isinstance(genesis_lineage_sha256, str) or len(genesis_lineage_sha256) != 64 or not isinstance(causal_parent_genesis, dict) or causal_parent_genesis.get("schema") != CAUSAL_PARENT_INITIALIZATION_SCHEMA ): raise RuntimeError( "Resynthesis branch causal delta genesis authority differs" ) else: raise RuntimeError( "Resynthesis branch causal delta parent authority mode differs" ) assert isinstance(base_sha256, str) base_path = Path(base_path_value).expanduser().resolve() if not base_path.is_file() or base_path == artifact_path: raise RuntimeError( "Resynthesis branch causal delta parent identity differs" ) try: validated_parent = _validated_immutable_causal_parent_boundary( base_path=base_path, base_sha256=base_sha256, identity_cache_root=identity_cache_root, ) except (OSError, RuntimeError, ValueError) as error: raise RuntimeError( "Resynthesis branch causal delta parent identity differs" ) from error physical_causal_parameter_names = ( validated_parent.physical_causal_parameter_names ) physical_causal_buffer_names = ( validated_parent.physical_causal_buffer_names ) science_layers = lineage.get("scienceLayers") science_experts = lineage.get("scienceExperts") if ( type(science_layers) is not int or science_layers < 1 or type(science_experts) is not int or science_experts < 1 ): raise RuntimeError( "Resynthesis branch causal delta anti-thompson lineage differs" ) expected_anti_thompson_outcome_names = frozenset( ( f"science_stack.science_layer_{layer_idx}" f"{ANTI_THOMPSON_FAIL_COUNTS_BUFFER_SUFFIX}" ) for layer_idx in range(science_layers) ) parent_buffer_names = set(validated_parent.buffer_geometry) physical_anti_thompson_outcome_names = frozenset( name for name in parent_buffer_names if name.endswith(ANTI_THOMPSON_FAIL_COUNTS_BUFFER_SUFFIX) ) supplied_anti_thompson_outcome_names = frozenset( name for name in buffers if name.endswith(ANTI_THOMPSON_FAIL_COUNTS_BUFFER_SUFFIX) ) if ( physical_anti_thompson_outcome_names not in (frozenset(), expected_anti_thompson_outcome_names) or supplied_anti_thompson_outcome_names not in (frozenset(), expected_anti_thompson_outcome_names) or ( physical_anti_thompson_outcome_names and supplied_anti_thompson_outcome_names != expected_anti_thompson_outcome_names ) ): raise RuntimeError( "Resynthesis branch causal delta anti-thompson outcome family " "is partial" ) for name in supplied_anti_thompson_outcome_names: value = buffers[name] if ( tuple(value.shape) != (science_experts,) or not value.is_floating_point() ): raise RuntimeError( "Resynthesis branch causal delta anti-thompson outcome " f"geometry differs: {name}" ) anti_thompson_outcome_growth_geometry = { name: (tuple(buffers[name].shape), buffers[name].dtype) for name in ( supplied_anti_thompson_outcome_names - physical_anti_thompson_outcome_names ) } if physical_causal_parameter_names not in ( frozenset(), BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES, ): raise RuntimeError( "Resynthesis branch causal delta escapes its parent state" ) parent_key_sha256 = validated_parent.state_key_sha256 parent_geometry_sha256 = validated_parent.state_geometry_sha256 genesis_source_lineage_sha256: str | None = None if authority_mode == BRANCH_CAUSAL_PARENT_AUTHORITY_RESIDENT: if ( physical_causal_parameter_names != BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES ): raise RuntimeError( "Resynthesis branch causal delta resident parent state differs" ) parent_causal_parameters = validated_parent.causal_parameters parent_causal_buffers = validated_parent.causal_buffers expected_causal_parameter_geometry = ( validated_parent.deterministic_causal_parameter_geometry ) expected_causal_buffer_geometry = ( validated_parent.deterministic_causal_buffer_geometry ) if ( physical_causal_parameter_names != BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES or physical_causal_buffer_names != frozenset(expected_causal_buffer_geometry) or set(buffers) != parent_buffer_names.union( anti_thompson_outcome_growth_geometry ) or any( parent_causal_parameters[name].shape != expected_causal_parameter_geometry[name][0] for name in BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES ) or any( validated_parent.buffer_geometry[name][0] != expected_causal_buffer_geometry[name][0] for name in physical_causal_buffer_names ) ): raise RuntimeError( "Resynthesis branch causal delta resident parent state differs" ) parent_overlay_geometry = { **validated_parent.buffer_geometry, **anti_thompson_outcome_growth_geometry, } ( parent_buffer_key_sha256, parent_buffer_geometry_sha256, ) = _checkpoint_state_geometry_identity_boundary( parent_overlay_geometry ) parent_causal_key_sha256 = ( validated_parent.causal_parameter_key_sha256 ) parent_causal_geometry_sha256 = ( validated_parent.causal_parameter_geometry_sha256 ) parent_causal_value_sha256 = ( validated_parent.causal_parameter_value_sha256 ) parent_causal_buffer_key_sha256 = ( validated_parent.causal_buffer_key_sha256 ) parent_causal_buffer_geometry_sha256 = ( validated_parent.causal_buffer_geometry_sha256 ) parent_causal_buffer_value_sha256 = ( validated_parent.causal_buffer_value_sha256 ) if any( value is None for value in ( parent_causal_key_sha256, parent_causal_geometry_sha256, parent_causal_value_sha256, parent_causal_buffer_key_sha256, parent_causal_buffer_geometry_sha256, parent_causal_buffer_value_sha256, ) ): raise RuntimeError( "Resynthesis branch causal delta resident parent state differs" ) else: if ( physical_causal_parameter_names or physical_causal_buffer_names or not isinstance(causal_parent_genesis, dict) ): raise RuntimeError( "Resynthesis branch causal delta genesis parent is not all-absent" ) parent_causal_parameters = ( _validated_deterministic_causal_parent_parameters_boundary( causal_parent_genesis.get("parameters") ) ) parent_causal_buffers = ( _validated_deterministic_causal_parent_buffers_boundary( parent_causal_parameters, causal_parent_genesis.get("buffers"), ) ) parent_causal_geometry = { name: (tuple(value.shape), value.dtype) for name, value in parent_causal_buffers.items() } parent_overlay_geometry = { **validated_parent.buffer_geometry, **parent_causal_geometry, **anti_thompson_outcome_growth_geometry, } if set(buffers) != set(parent_overlay_geometry): raise RuntimeError( "Resynthesis branch causal delta genesis buffer set differs" ) parent_buffer_key_sha256, parent_buffer_geometry_sha256 = ( _checkpoint_state_geometry_identity_boundary( parent_overlay_geometry ) ) parent_causal_key_sha256, parent_causal_geometry_sha256 = ( _checkpoint_state_identity_boundary(parent_causal_parameters) ) parent_causal_value_sha256 = ( _checkpoint_state_value_sha256_boundary( parent_causal_parameters ) ) ( parent_causal_buffer_key_sha256, parent_causal_buffer_geometry_sha256, ) = _checkpoint_state_identity_boundary(parent_causal_buffers) parent_causal_buffer_value_sha256 = ( _checkpoint_state_value_sha256_boundary(parent_causal_buffers) ) assert isinstance(parent_causal_key_sha256, str) assert isinstance(parent_causal_geometry_sha256, str) assert isinstance(parent_causal_value_sha256, str) assert isinstance(parent_causal_buffer_key_sha256, str) assert isinstance(parent_causal_buffer_geometry_sha256, str) assert isinstance(parent_causal_buffer_value_sha256, str) if authority_mode == BRANCH_CAUSAL_PARENT_AUTHORITY_GENESIS: genesis_source_lineage_sha256 = ( _causal_parent_genesis_source_lineage_sha256_boundary( base_checkpoint_sha256=base_sha256, lineage_sha256=_canonical_json_sha256_boundary( validated_parent.lineage ), parameter_key_sha256=parent_causal_key_sha256, parameter_geometry_sha256=parent_causal_geometry_sha256, parameter_value_sha256=parent_causal_value_sha256, buffer_key_sha256=parent_causal_buffer_key_sha256, buffer_geometry_sha256=parent_causal_buffer_geometry_sha256, buffer_value_sha256=parent_causal_buffer_value_sha256, ) ) if ( parent_key_sha256 != base.get("stateKeySetSha256") or parent_geometry_sha256 != base.get("stateGeometrySha256") or parent_buffer_key_sha256 != base.get("bufferKeySetSha256") or parent_buffer_geometry_sha256 != base.get("bufferGeometrySha256") or parent_causal_key_sha256 != base.get("causalParameterKeySetSha256") or parent_causal_geometry_sha256 != base.get("causalParameterGeometrySha256") or parent_causal_value_sha256 != base.get("causalParameterValueSha256") or parent_causal_buffer_key_sha256 != base.get("causalBufferKeySetSha256") or parent_causal_buffer_geometry_sha256 != base.get("causalBufferGeometrySha256") or parent_causal_buffer_value_sha256 != base.get("causalBufferValueSha256") or genesis_source_lineage_sha256 != base.get("causalParameterGenesisSourceLineageSha256") or ( functional_parent_genesis is None and validated_parent.lineage != lineage ) ): raise RuntimeError( "Resynthesis branch causal delta parent contract differs" ) parameter_universe_geometry = dict( validated_parent.parameter_geometry ) parameter_universe_geometry.update( { name: (tuple(value.shape), value.dtype) for name, value in parent_causal_parameters.items() } ) functional_genesis_parameters: dict[str, torch.Tensor] = {} functional_genesis_buffers: dict[str, torch.Tensor] = {} validated_functional_genesis_authority: dict[str, Any] | None = None functional_genesis_authority_sha256: str | None = None if functional_parent_genesis is not None: raw_genesis_parameters = ( functional_parent_genesis.get("parameters") if isinstance(functional_parent_genesis, dict) else None ) if not isinstance(raw_genesis_parameters, dict): raise RuntimeError( "Resynthesis functional parent genesis escapes its parent" ) overlapping_genesis_names = set(raw_genesis_parameters).intersection( parameter_universe_geometry ) if overlapping_genesis_names: parent_payload_cpu = torch.load( base_path, map_location="cpu", mmap=True, weights_only=True, ) validated_parent_cpu = ( _validated_full_additive_checkpoint_payload_boundary( parent_payload_cpu ) ) parent_parameters_cpu = validated_parent_cpu["parameters"] assert isinstance(parent_parameters_cpu, dict) _validate_functional_graph_parent_genesis_overlap_boundary( source_parameters={ name: parent_parameters_cpu[name] for name in overlapping_genesis_names }, genesis_parameters={ name: raw_genesis_parameters[name] for name in overlapping_genesis_names }, ) provisional_universe = { name: torch.empty(shape, dtype=dtype, device="meta") for name, (shape, dtype) in parameter_universe_geometry.items() } provisional_universe.update(raw_genesis_parameters) ( functional_genesis_parameters, functional_genesis_buffers, validated_functional_genesis_authority, ) = _validated_functional_graph_parent_genesis_boundary( functional_parent_genesis, functional_genesis_authority, parameter_universe=provisional_universe, target_lineage=lineage, expected_source_lineage=validated_parent.lineage, parent_buffers={ name: torch.empty(shape, dtype=dtype, device="meta") for name, (shape, dtype) in ( validated_parent.buffer_geometry.items() ) }, ) functional_genesis_authority_sha256 = ( _canonical_json_sha256_boundary( validated_functional_genesis_authority ) ) if ( payload.get("functionalGraphParentGenesisAuthoritySha256") != functional_genesis_authority_sha256 ): raise RuntimeError( "Resynthesis functional parent genesis identity differs" ) parameter_universe_geometry.update( { name: (tuple(value.shape), value.dtype) for name, value in functional_genesis_parameters.items() } ) parameter_universe = { name: torch.empty(shape, dtype=dtype, device="meta") for name, (shape, dtype) in parameter_universe_geometry.items() } validated_ownership: dict[str, Any] | None = None ownership_sha256: str | None = None if functional_ownership is not None: validated_ownership = ( validate_branch_functional_graph_parameter_ownership_boundary( functional_ownership, parameters, parameter_universe=parameter_universe, ) ) ownership_sha256 = _canonical_json_sha256_boundary( validated_ownership ) if ( validated_ownership["ownerIndex"] != 0 or payload.get("functionalGraphParameterOwnershipSha256") != ownership_sha256 ): raise RuntimeError( "Resynthesis branch causal delta functional ownership differs" ) if not set(parameters).issubset(parameter_universe_geometry): raise RuntimeError( "Resynthesis branch causal delta parameter set escapes its parent" ) for name, overlay_value in parameters.items(): parent_shape, parent_dtype = parameter_universe_geometry[name] if ( tuple(overlay_value.shape) != parent_shape or overlay_value.dtype != parent_dtype ): raise RuntimeError( "Resynthesis branch causal delta parameter geometry differs: " f"{name}" ) _validated_branch_causal_moving_graph_buffer_growth_boundary( moving_graph_buffer_growth, parent_geometry=parent_overlay_geometry, buffers=buffers, ) parameter_key_sha256, parameter_geometry_sha256 = ( _checkpoint_state_identity_boundary(parameters) ) parameter_value_sha256 = _checkpoint_state_value_sha256_boundary( parameters ) buffer_key_sha256, buffer_geometry_sha256 = ( _checkpoint_state_identity_boundary(buffers) ) buffer_value_sha256 = _checkpoint_state_value_sha256_boundary(buffers) if ( payload.get("overlayParameterKeySetSha256") != parameter_key_sha256 or payload.get("overlayParameterGeometrySha256") != parameter_geometry_sha256 or payload.get("overlayParameterValueSha256") != parameter_value_sha256 or payload.get("overlayBufferKeySetSha256") != buffer_key_sha256 or payload.get("overlayBufferGeometrySha256") != buffer_geometry_sha256 or payload.get("overlayBufferValueSha256") != buffer_value_sha256 ): raise RuntimeError( "Resynthesis branch causal delta overlay identity differs" ) composed_identity = _branch_causal_delta_composed_identity_boundary( base=base, lineage_sha256=str(payload["lineageSha256"]), branch_scope_sha256=str(payload["branchScopeSha256"]), parameter_key_sha256=parameter_key_sha256, parameter_geometry_sha256=parameter_geometry_sha256, parameter_value_sha256=parameter_value_sha256, buffer_key_sha256=buffer_key_sha256, buffer_geometry_sha256=buffer_geometry_sha256, buffer_value_sha256=buffer_value_sha256, functional_graph_parameter_ownership_sha256=ownership_sha256, functional_graph_parent_genesis_authority_sha256=( functional_genesis_authority_sha256 ), moving_graph_buffer_growth_sha256=( moving_graph_buffer_growth_sha256 if isinstance(moving_graph_buffer_growth_sha256, str) else None ), ) composed_state_sha256 = _canonical_json_sha256_boundary( composed_identity ) if payload.get("composedStateSha256") != composed_state_sha256: raise RuntimeError( "Resynthesis branch causal delta composed identity differs" ) return AdditiveBranchCausalDeltaResolution( artifact_path=artifact_path, artifact_sha256=artifact_sha256, base_checkpoint_path=base_path, base_checkpoint_sha256=base_sha256, base_state_key_sha256=str(base["stateKeySetSha256"]), base_state_geometry_sha256=str(base["stateGeometrySha256"]), base_buffer_key_sha256=str(base["bufferKeySetSha256"]), base_buffer_geometry_sha256=str(base["bufferGeometrySha256"]), base_causal_parameter_key_sha256=parent_causal_key_sha256, base_causal_parameter_geometry_sha256=( parent_causal_geometry_sha256 ), base_causal_parameter_value_sha256=parent_causal_value_sha256, base_causal_parameters={ name: value.detach().to(device="cpu").clone() for name, value in parent_causal_parameters.items() }, base_causal_buffer_key_sha256=( parent_causal_buffer_key_sha256 ), base_causal_buffer_geometry_sha256=( parent_causal_buffer_geometry_sha256 ), base_causal_buffer_value_sha256=( parent_causal_buffer_value_sha256 ), base_causal_buffers={ name: value.detach().to(device="cpu").clone() for name, value in parent_causal_buffers.items() }, causal_parameter_authority_mode=str(authority_mode), parent_causal_parameter_presence_count=int(parent_presence_count), causal_parameter_genesis_source_lineage_sha256=( genesis_source_lineage_sha256 ), lineage=dict(lineage), branch_scope=dict(branch_scope), parameters=dict(parameters), buffers=dict(buffers), functional_graph_parameter_ownership=( copy.deepcopy(validated_ownership) if validated_ownership is not None else None ), functional_graph_parent_genesis_authority=( copy.deepcopy(validated_functional_genesis_authority) if validated_functional_genesis_authority is not None else None ), functional_graph_parent_genesis_parameters={ name: value.detach().to(device="cpu").clone() for name, value in functional_genesis_parameters.items() }, functional_graph_parent_genesis_buffers={ name: value.detach().to(device="cpu").clone() for name, value in functional_genesis_buffers.items() }, moving_graph_buffer_growth=( dict(moving_graph_buffer_growth) if isinstance(moving_graph_buffer_growth, dict) else None ), composed_state_sha256=composed_state_sha256, ) def _branch_checkpoint_scope_supports_requested_ownership_boundary( checkpoint_scope: dict[str, Any], requested_scope: dict[str, Any], ) -> bool: """Accept exact ownership or one strictly validated descendant fork. A retained branch checkpoint is the immutable parent state for a later disjoint fanout. Its embedded scope therefore describes the pages that produced the parent delta, while the requested scope describes the subset this new child may mutate. Requiring byte-identical scopes here makes a sealed fanout impossible. The relationship remains strict: both records must be canonical training scopes in the same session, the child must name a later accepted generation, and every child page/layer pair must already be owned by the checkpoint scope. Artifact and accepted-generation hashes are validated independently by the training authority before this boundary. """ if checkpoint_scope == requested_scope: return True from resynthesis.none_paging import ( training_branch_scope_from_record_boundary, ) try: training_branch_scope_from_record_boundary(checkpoint_scope) training_branch_scope_from_record_boundary(requested_scope) except (RuntimeError, TypeError, ValueError): return False checkpoint_generation = checkpoint_scope.get("parentGeneration") requested_generation = requested_scope.get("parentGeneration") checkpoint_session = checkpoint_scope.get("sessionId") requested_session = requested_scope.get("sessionId") checkpoint_page_ids = checkpoint_scope.get("pageIds") checkpoint_layer_ids = checkpoint_scope.get("pageLayerIds") requested_page_ids = requested_scope.get("pageIds") requested_layer_ids = requested_scope.get("pageLayerIds") if ( isinstance(checkpoint_generation, bool) or not isinstance(checkpoint_generation, int) or isinstance(requested_generation, bool) or not isinstance(requested_generation, int) or requested_generation <= checkpoint_generation or checkpoint_session != requested_session or not isinstance(checkpoint_page_ids, list) or not isinstance(checkpoint_layer_ids, list) or len(checkpoint_page_ids) != len(checkpoint_layer_ids) or not isinstance(requested_page_ids, list) or not isinstance(requested_layer_ids, list) or len(requested_page_ids) != len(requested_layer_ids) ): return False checkpoint_layers_by_page = dict( zip(checkpoint_page_ids, checkpoint_layer_ids, strict=True) ) return all( checkpoint_layers_by_page.get(page_id) == layer_id for page_id, layer_id in zip( requested_page_ids, requested_layer_ids, strict=True, ) ) def _resolve_additive_branch_causal_checkpoint_boundary( artifact_path: Path, *, expected_artifact_sha256: str | None, expected_branch_scope: dict[str, Any] | None, identity_cache_root: Path | None, ) -> AdditiveCheckpointResolution: """Compose one validated v2 causal/page overlay over its immutable parent.""" delta = validate_additive_branch_causal_delta_boundary( artifact_path, expected_artifact_sha256=expected_artifact_sha256, identity_cache_root=identity_cache_root, ) if expected_branch_scope is not None and not ( _branch_checkpoint_scope_supports_requested_ownership_boundary( delta.branch_scope, expected_branch_scope, ) ): raise RuntimeError( "Resynthesis branch causal delta scope differs from requested " "ownership" ) base_payload = torch.load( delta.base_checkpoint_path, map_location="cpu", mmap=True, weights_only=True, ) if ( _verified_checkpoint_file_sha256_boundary( delta.base_checkpoint_path, delta.base_checkpoint_sha256, identity_cache_root=identity_cache_root, ) != delta.base_checkpoint_sha256 ): raise RuntimeError( "Resynthesis branch causal delta parent changed during composition" ) validated_base = _validated_full_additive_checkpoint_payload_boundary( base_payload ) base_parameters = validated_base["parameters"] base_buffers = validated_base["buffers"] assert isinstance(base_parameters, dict) assert isinstance(base_buffers, dict) physical_causal_names = ( BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES.intersection(base_parameters) ) physical_causal_buffer_names = { name for name in base_buffers if name.startswith(_CAUSAL_WORLD_GRAPH_STATE_PREFIX) } if ( delta.causal_parameter_authority_mode == BRANCH_CAUSAL_PARENT_AUTHORITY_RESIDENT and ( physical_causal_names != BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES or not physical_causal_buffer_names or delta.parent_causal_parameter_presence_count != len(BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES) ) ) or ( delta.causal_parameter_authority_mode == BRANCH_CAUSAL_PARENT_AUTHORITY_GENESIS and ( physical_causal_names or physical_causal_buffer_names or delta.parent_causal_parameter_presence_count != 0 ) ): raise RuntimeError( "Resynthesis branch causal delta escapes its parent state" ) base_state = {**base_parameters, **base_buffers} base_key_sha256, base_geometry_sha256 = ( _checkpoint_state_identity_boundary(base_state) ) base_causal_buffers = delta.base_causal_buffers base_overlay_buffers = {**base_buffers, **base_causal_buffers} if set(delta.buffers) != set(base_overlay_buffers): raise RuntimeError( "Resynthesis branch causal delta buffer set differs" ) buffer_key_sha256, buffer_geometry_sha256 = ( _checkpoint_state_identity_boundary(base_overlay_buffers) ) base_causal_parameters = delta.base_causal_parameters causal_key_sha256, causal_geometry_sha256 = ( _checkpoint_state_identity_boundary(base_causal_parameters) ) causal_value_sha256 = _checkpoint_state_value_sha256_boundary( base_causal_parameters ) causal_buffer_key_sha256, causal_buffer_geometry_sha256 = ( _checkpoint_state_identity_boundary(base_causal_buffers) ) causal_buffer_value_sha256 = _checkpoint_state_value_sha256_boundary( base_causal_buffers ) if ( base_key_sha256 != delta.base_state_key_sha256 or base_geometry_sha256 != delta.base_state_geometry_sha256 or buffer_key_sha256 != delta.base_buffer_key_sha256 or buffer_geometry_sha256 != delta.base_buffer_geometry_sha256 or causal_key_sha256 != delta.base_causal_parameter_key_sha256 or causal_geometry_sha256 != delta.base_causal_parameter_geometry_sha256 or causal_value_sha256 != delta.base_causal_parameter_value_sha256 or causal_buffer_key_sha256 != delta.base_causal_buffer_key_sha256 or causal_buffer_geometry_sha256 != delta.base_causal_buffer_geometry_sha256 or causal_buffer_value_sha256 != delta.base_causal_buffer_value_sha256 or ( delta.functional_graph_parent_genesis_authority is None and validated_base.get("lineage") != delta.lineage ) ): raise RuntimeError( "Resynthesis branch causal delta parent contract differs" ) parent_parameters = { **base_parameters, **base_causal_parameters, **delta.functional_graph_parent_genesis_parameters, } parent_buffers = { **base_buffers, **base_causal_buffers, **delta.functional_graph_parent_genesis_buffers, } parent_composed_state = {**parent_parameters, **parent_buffers} parent_composed_key_sha256, parent_composed_geometry_sha256 = ( _checkpoint_state_identity_boundary(parent_composed_state) ) composed_parameters = {**parent_parameters, **delta.parameters} composed_buffers = {**parent_buffers, **delta.buffers} composed_state = {**composed_parameters, **composed_buffers} composed_key_sha256, composed_geometry_sha256 = ( _checkpoint_state_identity_boundary(composed_state) ) if ( composed_key_sha256 != parent_composed_key_sha256 or ( composed_geometry_sha256 != parent_composed_geometry_sha256 and delta.moving_graph_buffer_growth is None ) ): raise RuntimeError( "Resynthesis branch causal delta composed state differs" ) composed_payload = { "schema": ADDITIVE_CHECKPOINT_SCHEMA, "lineage": delta.lineage, "stateKeySetSha256": composed_key_sha256, "stateGeometrySha256": composed_geometry_sha256, "parameters": composed_parameters, "buffers": composed_buffers, } return AdditiveCheckpointResolution( artifact_path=artifact_path, artifact_sha256=delta.artifact_sha256, base_checkpoint_path=delta.base_checkpoint_path, base_checkpoint_sha256=delta.base_checkpoint_sha256, base_payload=validated_base, payload=composed_payload, branch_delta=delta, ) def resolve_additive_checkpoint_boundary( checkpoint_path: str | Path, *, expected_artifact_sha256: str | None = None, expected_branch_scope: dict[str, Any] | None = None, identity_cache_root: Path | None = None, ) -> AdditiveCheckpointResolution: """Resolve a legacy full checkpoint or one verified one-hop branch overlay.""" artifact_path = Path(checkpoint_path).expanduser().resolve() payload = torch.load( artifact_path, map_location="cpu", mmap=True, weights_only=True, ) if isinstance(payload, dict) and payload.get("schema") == ADDITIVE_CHECKPOINT_SCHEMA: validated = _validated_full_additive_checkpoint_payload_boundary(payload) artifact_sha256 = _verified_checkpoint_file_sha256_boundary( artifact_path, expected_artifact_sha256, identity_cache_root=identity_cache_root, ) return AdditiveCheckpointResolution( artifact_path=artifact_path, artifact_sha256=artifact_sha256, base_checkpoint_path=artifact_path, base_checkpoint_sha256=artifact_sha256, base_payload=validated, payload=validated, branch_delta=None, ) if ( isinstance(payload, dict) and payload.get("schema") == ADDITIVE_BRANCH_CAUSAL_DELTA_SCHEMA ): return _resolve_additive_branch_causal_checkpoint_boundary( artifact_path, expected_artifact_sha256=expected_artifact_sha256, expected_branch_scope=expected_branch_scope, identity_cache_root=identity_cache_root, ) delta = validate_additive_branch_delta_boundary( artifact_path, expected_artifact_sha256=expected_artifact_sha256, identity_cache_root=identity_cache_root, ) if ( expected_branch_scope is not None and delta.branch_scope != expected_branch_scope ): raise RuntimeError( "Resynthesis branch delta scope differs from requested ownership" ) base_payload = torch.load( delta.base_checkpoint_path, map_location="cpu", mmap=True, weights_only=True, ) validated_base = _validated_full_additive_checkpoint_payload_boundary( base_payload ) base_parameters = validated_base["parameters"] base_buffers = validated_base["buffers"] assert isinstance(base_parameters, dict) assert isinstance(base_buffers, dict) base_state = {**base_parameters, **base_buffers} base_key_sha256, base_geometry_sha256 = ( _checkpoint_state_identity_boundary(base_state) ) if not set(delta.buffers).issubset(base_buffers): raise RuntimeError("Resynthesis branch delta buffer set escapes its parent") base_overlay_buffers = { name: base_buffers[name] for name in delta.buffers } buffer_key_sha256, buffer_geometry_sha256 = ( _checkpoint_state_identity_boundary(base_overlay_buffers) ) if ( base_key_sha256 != delta.base_state_key_sha256 or base_geometry_sha256 != delta.base_state_geometry_sha256 or buffer_key_sha256 != delta.base_buffer_key_sha256 or buffer_geometry_sha256 != delta.base_buffer_geometry_sha256 or ( delta.functional_graph_parent_genesis_authority is None and validated_base.get("lineage") != delta.lineage ) ): raise RuntimeError("Resynthesis branch delta parent contract differs") parent_parameters = { **base_parameters, **delta.functional_graph_parent_genesis_parameters, } if not set(delta.parameters).issubset(parent_parameters): raise RuntimeError( "Resynthesis branch delta parameter set escapes its parent" ) for name, overlay_value in delta.parameters.items(): base_value = parent_parameters[name] if ( overlay_value.shape != base_value.shape or overlay_value.dtype != base_value.dtype ): raise RuntimeError( f"Resynthesis branch delta parameter geometry differs: {name}" ) for name, base_value in base_overlay_buffers.items(): overlay_value = delta.buffers[name] if ( overlay_value.shape != base_value.shape or overlay_value.dtype != base_value.dtype ): raise RuntimeError( f"Resynthesis branch delta buffer geometry differs: {name}" ) parent_buffers = { **base_buffers, **delta.functional_graph_parent_genesis_buffers, } parent_composed_state = {**parent_parameters, **parent_buffers} parent_composed_key_sha256, parent_composed_geometry_sha256 = ( _checkpoint_state_identity_boundary(parent_composed_state) ) composed_parameters = {**parent_parameters, **delta.parameters} composed_buffers = {**parent_buffers, **delta.buffers} composed_state = {**composed_parameters, **composed_buffers} composed_key_sha256, composed_geometry_sha256 = ( _checkpoint_state_identity_boundary(composed_state) ) if ( composed_key_sha256 != parent_composed_key_sha256 or composed_geometry_sha256 != parent_composed_geometry_sha256 ): raise RuntimeError("Resynthesis branch delta composed state differs") composed_payload = { "schema": ADDITIVE_CHECKPOINT_SCHEMA, "lineage": delta.lineage, "stateKeySetSha256": composed_key_sha256, "stateGeometrySha256": composed_geometry_sha256, "parameters": composed_parameters, "buffers": composed_buffers, } return AdditiveCheckpointResolution( artifact_path=artifact_path, artifact_sha256=delta.artifact_sha256, base_checkpoint_path=delta.base_checkpoint_path, base_checkpoint_sha256=delta.base_checkpoint_sha256, base_payload=validated_base, payload=composed_payload, branch_delta=delta, ) def page_branch_reconciliation_input_boundary( resolution: AdditiveCheckpointResolution, ) -> AdditivePageBranchReconciliationInput: """Project one verified v1 page delta into the typed union boundary.""" from resynthesis.none_paging import ( training_branch_scope_from_record_boundary, validate_training_branch_scope_boundary, ) delta = resolution.branch_delta if ( not isinstance(delta, AdditiveBranchDeltaResolution) or isinstance(delta, AdditiveBranchCausalDeltaResolution) or delta.parameters or delta.functional_graph_parameter_ownership is not None or delta.functional_graph_parent_genesis_authority is not None or delta.functional_graph_parent_genesis_parameters or delta.functional_graph_parent_genesis_buffers or delta.base_checkpoint_sha256 != resolution.base_checkpoint_sha256 ): raise RuntimeError( "Resynthesis all-knowledge reconciliation requires a buffer-only " "page branch" ) scope = training_branch_scope_from_record_boundary(delta.branch_scope) validate_training_branch_scope_boundary(scope) return AdditivePageBranchReconciliationInput( artifact_path=resolution.artifact_path.expanduser().resolve(), artifact_sha256=resolution.artifact_sha256, base_checkpoint_path=( resolution.base_checkpoint_path.expanduser().resolve() ), base_checkpoint_sha256=resolution.base_checkpoint_sha256, lineage=copy.deepcopy(delta.lineage), scope=scope, buffers={ name: value.detach().to(device="cpu").clone() for name, value in delta.buffers.items() }, ) def historical_additive_checkpoint_segment_boundary( *, ordinal: int, source_generation: int, terminal_generation: int, terminal: AdditiveCheckpointResolution | None, checkpoint_path: Path, checkpoint_sha256: str, optimizer_path: Path, optimizer_sha256: str, external_state_path: Path, external_state_sha256: str, changed_page_count: int, changed_page_ids_sha256: str, provenance_sha256: str, ) -> HistoricalAdditiveCheckpointSegmentPacket: """Bind one immutable historical row to its validated tensor evidence. A missing terminal is admitted only as page-only evidence. In particular, a deleted checkpoint never grants permission to synthesize dense tensors or optimizer moments from its filename, receipt, or neighboring branches. """ from resynthesis.none_paging import ( digest_tensor, training_branch_scope_from_record_boundary, validate_training_branch_scope_boundary, ) scalar_values = (ordinal, source_generation, terminal_generation) sha256_values = ( checkpoint_sha256, optimizer_sha256, external_state_sha256, changed_page_ids_sha256, provenance_sha256, ) if ( any( not isinstance(value, int) or isinstance(value, bool) or value < 0 for value in scalar_values ) or source_generation < 1 or terminal_generation <= source_generation or not isinstance(changed_page_count, int) or isinstance(changed_page_count, bool) or changed_page_count < 1 or not all( isinstance(value, str) and len(value) == 64 and all(character in "0123456789abcdef" for character in value) for value in sha256_values ) or not all( isinstance(value, Path) for value in ( checkpoint_path, optimizer_path, external_state_path, ) ) ): raise RuntimeError( "Resynthesis historical checkpoint segment identity is malformed" ) resolved_checkpoint_path = checkpoint_path.expanduser().resolve() resolved_optimizer_path = optimizer_path.expanduser().resolve() resolved_external_state_path = external_state_path.expanduser().resolve() scope: NoNETrainingBranchScopePacket | None = None causal_genesis: AdditiveBranchCausalStatePacket | None = None branch_local_causal_control_overlay = False checkpoint_tensor_evidence = terminal is not None if terminal is None: if ( resolved_checkpoint_path.exists() or resolved_optimizer_path.exists() or resolved_external_state_path.exists() ): raise RuntimeError( "Resynthesis page-only historical segment has recoverable " "checkpoint tensors" ) else: if ( terminal.artifact_path.expanduser().resolve() != resolved_checkpoint_path or terminal.artifact_sha256 != checkpoint_sha256 ): raise RuntimeError( "Resynthesis historical checkpoint segment artifact differs" ) _, observed_optimizer_sha256, _ = _immutable_file_sha256_boundary( resolved_optimizer_path, label="historical optimizer", ) _, observed_external_state_sha256, _ = ( _immutable_file_sha256_boundary( resolved_external_state_path, label="historical external state", ) ) if ( observed_optimizer_sha256 != optimizer_sha256 or observed_external_state_sha256 != external_state_sha256 ): raise RuntimeError( "Resynthesis historical checkpoint family identity differs" ) delta = terminal.branch_delta if not isinstance( delta, (AdditiveBranchDeltaResolution, AdditiveBranchCausalDeltaResolution), ): raise RuntimeError( "Resynthesis historical terminal is not a branch checkpoint" ) scope = training_branch_scope_from_record_boundary(delta.branch_scope) validate_training_branch_scope_boundary(scope) branch_local_causal_control_overlay = isinstance( delta, AdditiveBranchCausalDeltaResolution, ) if isinstance(delta, AdditiveBranchCausalDeltaResolution): causal_genesis = AdditiveBranchCausalStatePacket( parameters={ name: value.detach().to(device="cpu").clone() for name, value in delta.base_causal_parameters.items() }, buffers={ name: value.detach().to(device="cpu").clone() for name, value in delta.base_causal_buffers.items() }, ) return HistoricalAdditiveCheckpointSegmentPacket( ordinal_t=torch.tensor(ordinal, dtype=torch.long), source_generation_t=torch.tensor(source_generation, dtype=torch.long), terminal_generation_t=torch.tensor( terminal_generation, dtype=torch.long, ), terminal=terminal, checkpoint_path=resolved_checkpoint_path, checkpoint_sha256=checkpoint_sha256, optimizer_path=resolved_optimizer_path, optimizer_sha256=optimizer_sha256, external_state_path=resolved_external_state_path, external_state_sha256=external_state_sha256, changed_page_count_t=torch.tensor(changed_page_count, dtype=torch.long), changed_page_ids_sha256_t=digest_tensor(changed_page_ids_sha256), provenance_sha256_t=digest_tensor(provenance_sha256), checkpoint_tensor_evidence_t=torch.tensor( checkpoint_tensor_evidence, dtype=torch.bool, ), shared_dense_graph_mutation_authority_t=torch.tensor( False, dtype=torch.bool, ), branch_local_causal_control_overlay_t=torch.tensor( branch_local_causal_control_overlay, dtype=torch.bool, ), scope=scope, causal_genesis=causal_genesis, ) def _historical_reconciliation_lineage_boundary( lineage: Mapping[str, Any], ) -> dict[str, Any]: """Normalize the one historical compact-object transport annotation.""" normalized = copy.deepcopy(dict(lineage)) paged = normalized.get("pagedNoNE") if ( isinstance(paged, dict) and paged.get("sharedCompactObjectOverlay") is True and paged.get("compactBankCapacityClaimedTrained") is False and paged.get("replicatedGenerationTransaction") is True and paged.get("storageBoundaryMayReroute") is False ): paged.pop("sharedCompactObjectOverlay") return normalized def _historical_reconciliation_compute_dtype_boundary( value_t: torch.Tensor, ) -> torch.dtype: if value_t.dtype == torch.float64: return torch.float64 if value_t.is_floating_point(): return torch.float32 return torch.int64 def reconcile_historical_additive_knowledge_boundary( *, target: AdditiveCheckpointResolution, common_parent: AdditiveCheckpointResolution, segments: tuple[HistoricalAdditiveCheckpointSegmentPacket, ...], physical_page_ids_t: torch.Tensor, semantic_page_ids_t: torch.Tensor, authority: HistoricalAdditiveKnowledgeReconciliationAuthority, ) -> AdditiveKnowledgeCalibrationPacket: """Cold-compose all historical checkpoint evidence into one direct state. Common page-training tensors use the same ordered ``target + Σ(terminal - source)`` algebra as the independently materialized page bundles. The causal graph is genuine expandable model state: its recorded genesis is added once, then every prefix/residual parameter delta is accumulated in float32. No branch overlay, base-bound delta object, optimizer moment, accepted pointer, or source-store lookup is copied into the result. """ from resynthesis.none_paging import ( validate_training_branch_scope_boundary, ) def valid_sha256(value: str) -> bool: return ( isinstance(value, str) and len(value) == 64 and all(character in "0123456789abcdef" for character in value) ) authority_tensors = ( authority.plan_sha256_t, authority.common_parent_generation_t, authority.target_generation_t, authority.output_generation_t, authority.target_manifest_sha256_t, authority.target_manifest_payload_sha256_t, authority.physical_page_ids_sha256_t, authority.semantic_page_ids_sha256_t, authority.page_change_event_count_t, authority.revision6_accepted_t, authority.cross_store_overlay_accepted_t, authority.direct_page_map_required_t, authority.optimizer_moments_reinitialized_t, ) scalar_long_tensors = ( authority.common_parent_generation_t, authority.target_generation_t, authority.output_generation_t, authority.page_change_event_count_t, ) digest_tensors = ( authority.plan_sha256_t, authority.target_manifest_sha256_t, authority.target_manifest_payload_sha256_t, authority.physical_page_ids_sha256_t, authority.semantic_page_ids_sha256_t, ) bool_tensors = ( authority.revision6_accepted_t, authority.cross_store_overlay_accepted_t, authority.direct_page_map_required_t, authority.optimizer_moments_reinitialized_t, ) if ( not isinstance( authority, HistoricalAdditiveKnowledgeReconciliationAuthority, ) or not segments or any(not isinstance(value, torch.Tensor) for value in authority_tensors) or any( value.dtype != torch.long or value.numel() != 1 for value in scalar_long_tensors ) or any( value.dtype != torch.uint8 or value.shape != (32,) for value in digest_tensors ) or any( value.dtype != torch.bool or value.numel() != 1 for value in bool_tensors ) or int(authority.common_parent_generation_t) < 1 or int(authority.target_generation_t) <= int(authority.common_parent_generation_t) or int(authority.output_generation_t) != int(authority.target_generation_t) + 1 or int(authority.page_change_event_count_t) < 1 or bool(authority.revision6_accepted_t) or bool(authority.cross_store_overlay_accepted_t) or not bool(authority.direct_page_map_required_t) or not bool(authority.optimizer_moments_reinitialized_t) or not all( isinstance(value, Path) for value in ( authority.target_checkpoint_path, authority.target_optimizer_path, authority.target_external_state_path, ) ) or not all( valid_sha256(value) for value in ( authority.target_checkpoint_sha256, authority.target_optimizer_sha256, authority.target_external_state_sha256, ) ) or target.artifact_path.expanduser().resolve() != authority.target_checkpoint_path.expanduser().resolve() or target.artifact_sha256 != authority.target_checkpoint_sha256 or common_parent.branch_delta is not None or target.branch_delta is not None ): raise RuntimeError( "Resynthesis historical reconciliation authority is incomplete" ) _, target_optimizer_sha256, target_optimizer_identity = ( _immutable_file_sha256_boundary( authority.target_optimizer_path, label="historical target optimizer", ) ) _, target_external_sha256, target_external_identity = ( _immutable_file_sha256_boundary( authority.target_external_state_path, label="historical target external state", ) ) if ( target_optimizer_sha256 != authority.target_optimizer_sha256 or target_external_sha256 != authority.target_external_state_sha256 ): raise RuntimeError( "Resynthesis historical target checkpoint family differs" ) physical_page_ids = ( physical_page_ids_t.detach().to(device="cpu", dtype=torch.long).clone() ) semantic_page_ids = ( semantic_page_ids_t.detach().to(device="cpu", dtype=torch.long).clone() ) if ( physical_page_ids_t.dtype != torch.long or physical_page_ids_t.ndim != 1 or semantic_page_ids_t.dtype != torch.long or semantic_page_ids_t.ndim != 1 or physical_page_ids.numel() < 1 or semantic_page_ids.numel() < 1 or not torch.equal( physical_page_ids, torch.sort(physical_page_ids).values, ) or torch.unique(physical_page_ids).numel() != physical_page_ids.numel() or not torch.equal( semantic_page_ids, torch.sort(semantic_page_ids).values, ) or torch.unique(semantic_page_ids).numel() != semantic_page_ids.numel() or not bool(torch.isin(semantic_page_ids, physical_page_ids).all()) ): raise RuntimeError( "Resynthesis historical reconciliation page universe differs" ) physical_page_ids_sha256 = hashlib.sha256( physical_page_ids.contiguous() .numpy() .astype(" HistoricalAdditiveOptimizerPacket: """Reseal the exact expanded parameter set without invented moments.""" parameters, _ = ( _validated_reconciled_additive_calibration_packet_boundary( calibration_packet ) ) def load_empty_optimizer( path: Path, expected_sha256: str, *, label: str, ) -> tuple[dict[str, Any], tuple[str, ...]]: resolved, observed_sha256, identity = ( _immutable_file_sha256_boundary(path, label=label) ) if observed_sha256 != expected_sha256: raise RuntimeError( "Resynthesis historical optimizer identity differs" ) payload = torch.load( resolved, map_location="cpu", mmap=True, weights_only=True, ) state = payload.get("state") if isinstance(payload, dict) else None groups = ( payload.get("param_groups") if isinstance(payload, dict) else None ) if ( not isinstance(state, dict) or state or not isinstance(groups, list) or len(groups) != 1 or not isinstance(groups[0], dict) ): raise RuntimeError( "Resynthesis historical optimizer contains unmergeable moments" ) group = groups[0] param_ids = group.get("params") param_names = group.get("param_names") if ( not isinstance(param_ids, list) or not isinstance(param_names, (list, tuple)) or not all( isinstance(value, int) and not isinstance(value, bool) for value in param_ids ) or not all(isinstance(value, str) for value in param_names) or len(param_ids) != len(param_names) or param_ids != list(range(len(param_ids))) or len(set(param_names)) != len(param_names) ): raise RuntimeError( "Resynthesis historical optimizer parameter topology differs" ) _require_immutable_file_identity_boundary( resolved, identity, label=label, ) return dict(group), tuple(cast(tuple[str, ...], tuple(param_names))) target_group, target_names = load_empty_optimizer( authority.target_optimizer_path, authority.target_optimizer_sha256, label="historical target optimizer", ) parameter_names = set(parameters) causal_names = set(BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES) if ( set(target_names) != parameter_names - causal_names or causal_names - parameter_names ): raise RuntimeError( "Resynthesis historical target optimizer parameter set differs" ) target_hyperparameters = { name: value for name, value in target_group.items() if name not in {"params", "param_names"} } canonical_causal_names: tuple[str, ...] | None = None source_rows: list[dict[str, Any]] = [] for segment in sorted(segments, key=lambda row: int(row.ordinal_t)): if not bool(segment.checkpoint_tensor_evidence_t): source_rows.append( { "ordinal": int(segment.ordinal_t), "optimizerPresent": False, "optimizerMomentCount": 0, "optimizerContribution": "page_tensors_only", } ) continue group, names = load_empty_optimizer( segment.optimizer_path, segment.optimizer_sha256, label=f"historical segment {int(segment.ordinal_t)} optimizer", ) hyperparameters = { name: value for name, value in group.items() if name not in {"params", "param_names"} } causal = bool( segment.branch_local_causal_control_overlay_t ) if causal: if ( set(names) != causal_names or hyperparameters != target_hyperparameters ): raise RuntimeError( "Resynthesis historical causal optimizer topology differs" ) if canonical_causal_names is None: canonical_causal_names = names elif canonical_causal_names != names: raise RuntimeError( "Resynthesis historical causal optimizer order drifted" ) contribution = "expanded_parameters_without_moments" else: if names != ("__external_page_optimizer_policy__",): raise RuntimeError( "Resynthesis historical page optimizer topology differs" ) contribution = "page_tensors_only" source_rows.append( { "ordinal": int(segment.ordinal_t), "optimizerPresent": True, "optimizerPath": str(segment.optimizer_path), "optimizerSha256": segment.optimizer_sha256, "optimizerMomentCount": 0, "optimizerContribution": contribution, } ) if canonical_causal_names is None: raise RuntimeError( "Resynthesis historical causal optimizer evidence is missing" ) merged_names = (*target_names, *canonical_causal_names) if ( len(set(merged_names)) != len(merged_names) or set(merged_names) != parameter_names ): raise RuntimeError( "Resynthesis historical merged optimizer parameters differ" ) merged_group = { **target_hyperparameters, "params": list(range(len(merged_names))), "param_names": merged_names, } payload: dict[str, Any] = { "state": {}, "param_groups": [merged_group], } optimizer_structure = { "stateEntryCount": 0, "paramGroups": [ { **target_hyperparameters, "params": list(range(len(merged_names))), "param_names": list(merged_names), } ], } unsigned_proof: dict[str, Any] = { "schema": HISTORICAL_ADDITIVE_OPTIMIZER_PROOF_SCHEMA, "historicalPlanSha256": bytes( authority.plan_sha256_t.detach().cpu().tolist() ).hex(), "reconciliationProofSha256": calibration_packet.proof[ "proofSha256" ], "targetOptimizerPath": str(authority.target_optimizer_path), "targetOptimizerSha256": authority.target_optimizer_sha256, "targetParameterCount": len(target_names), "expandedCausalParameterCount": len(canonical_causal_names), "finalParameterCount": len(merged_names), "sourceOptimizerMomentCount": 0, "finalOptimizerMomentCount": 0, "optimizerMomentsReinitialized": True, "missingOptimizerMomentKnowledgeInvented": False, "parameterNamesSha256": hashlib.sha256( "\n".join(merged_names).encode("utf-8") ).hexdigest(), "optimizerStructureSha256": _canonical_json_sha256_boundary( optimizer_structure ), "sources": source_rows, } proof = { **unsigned_proof, "proofSha256": _canonical_json_sha256_boundary(unsigned_proof), } return HistoricalAdditiveOptimizerPacket( payload=payload, proof=proof, ) def reconcile_additive_page_branch_knowledge_boundary( *, target: AdditiveCheckpointResolution, common_parent: AdditiveCheckpointResolution, branches: tuple[AdditivePageBranchReconciliationInput, ...], physical_page_ids_t: torch.Tensor, authority: AdditiveKnowledgeReconciliationAuthority, ) -> AdditiveKnowledgeCalibrationPacket: """Compose all independent page-branch graph evidence over one live target. The target and every branch are independent descendants of ``common_parent``. Cumulative tensors therefore use the same explicit three-way algebra as page objects: ``target + Σ(branch - parent)``. Stable buffers must agree byte-for-byte across every terminal branch. Router quantile biases are lossy route-distribution summaries, so they are never arithmetically merged; they are zeroed and must be regenerated by one target-free model forward before a direct checkpoint can be published. """ from resynthesis.none_paging import ( validate_common_parent_training_branch_scopes_boundary, validate_training_branch_scope_boundary, ) if not isinstance(authority, AdditiveKnowledgeReconciliationAuthority): raise RuntimeError( "Resynthesis all-knowledge reconciliation authority is incomplete" ) authority_tensors = ( authority.common_parent_generation_t, authority.common_parent_manifest_payload_sha256_t, authority.target_generation_t, authority.target_manifest_sha256_t, authority.target_manifest_payload_sha256_t, authority.branch_generations_t, authority.branch_manifest_sha256s_t, authority.branch_manifest_payload_sha256s_t, authority.branch_scope_sha256s_t, authority.branch_scope_page_ids_t, authority.branch_scope_page_layer_ids_t, authority.branch_union_sha256_t, authority.lineage_rebase_sha256_t, ) valid_authority_sha256s = all( isinstance(value, str) and len(value) == 64 and all(character in "0123456789abcdef" for character in value) for value in ( authority.common_parent_checkpoint_sha256, authority.target_checkpoint_sha256, authority.target_optimizer_sha256, authority.target_external_state_sha256, *authority.branch_checkpoint_sha256s, *authority.branch_optimizer_sha256s, *authority.branch_external_state_sha256s, ) ) if ( target.branch_delta is not None or common_parent.branch_delta is not None or not branches or not valid_authority_sha256s or len(authority.branch_checkpoint_sha256s) != len(branches) or len(authority.branch_checkpoint_paths) != len(branches) or len(authority.branch_optimizer_paths) != len(branches) or len(authority.branch_optimizer_sha256s) != len(branches) or len(authority.branch_external_state_paths) != len(branches) or len(authority.branch_external_state_sha256s) != len(branches) or any(not isinstance(value, torch.Tensor) for value in authority_tensors) or not isinstance(authority.common_parent_checkpoint_path, Path) or not isinstance(authority.target_checkpoint_path, Path) or not isinstance(authority.target_optimizer_path, Path) or not isinstance(authority.target_external_state_path, Path) or authority.common_parent_generation_t.dtype != torch.long or authority.common_parent_generation_t.numel() != 1 or authority.target_generation_t.dtype != torch.long or authority.target_generation_t.numel() != 1 or int(authority.common_parent_generation_t) < 1 or int(authority.target_generation_t) <= int(authority.common_parent_generation_t) or authority.common_parent_manifest_payload_sha256_t.shape != (32,) or authority.common_parent_manifest_payload_sha256_t.dtype != torch.uint8 or authority.target_manifest_sha256_t.shape != (32,) or authority.target_manifest_sha256_t.dtype != torch.uint8 or authority.target_manifest_payload_sha256_t.shape != (32,) or authority.target_manifest_payload_sha256_t.dtype != torch.uint8 or authority.branch_generations_t.shape != (len(branches),) or authority.branch_generations_t.dtype != torch.long or authority.branch_manifest_sha256s_t.shape != (len(branches), 32) or authority.branch_manifest_sha256s_t.dtype != torch.uint8 or authority.branch_manifest_payload_sha256s_t.shape != (len(branches), 32) or authority.branch_manifest_payload_sha256s_t.dtype != torch.uint8 or authority.branch_scope_sha256s_t.shape != (len(branches), 32) or authority.branch_scope_sha256s_t.dtype != torch.uint8 or authority.branch_scope_page_ids_t.dtype != torch.long or authority.branch_scope_page_ids_t.ndim != 1 or authority.branch_scope_page_ids_t.numel() < 1 or authority.branch_scope_page_layer_ids_t.dtype != torch.long or authority.branch_scope_page_layer_ids_t.shape != authority.branch_scope_page_ids_t.shape or authority.branch_union_sha256_t.shape != (32,) or authority.branch_union_sha256_t.dtype != torch.uint8 or authority.lineage_rebase_sha256_t.shape != (32,) or authority.lineage_rebase_sha256_t.dtype != torch.uint8 or target.artifact_sha256 != authority.target_checkpoint_sha256 or target.artifact_path.expanduser().resolve() != authority.target_checkpoint_path or common_parent.artifact_sha256 != authority.common_parent_checkpoint_sha256 or common_parent.artifact_path.expanduser().resolve() != authority.common_parent_checkpoint_path or physical_page_ids_t.dtype != torch.long or physical_page_ids_t.ndim != 1 or physical_page_ids_t.numel() < 1 ): raise RuntimeError( "Resynthesis all-knowledge reconciliation authority is incomplete" ) physical_page_ids = ( physical_page_ids_t.detach().to(device="cpu", dtype=torch.long).clone() ) if ( bool(physical_page_ids.lt(0).any()) or not torch.equal(physical_page_ids, torch.sort(physical_page_ids).values) or torch.unique(physical_page_ids).numel() != physical_page_ids.numel() ): raise RuntimeError( "Resynthesis all-knowledge physical page catalog differs" ) expected_scope_page_ids_t = ( authority.branch_scope_page_ids_t.detach().cpu().long().clone() ) expected_scope_page_layer_ids_t = ( authority.branch_scope_page_layer_ids_t.detach().cpu().long().clone() ) if ( bool(expected_scope_page_ids_t.lt(0).any()) or bool(expected_scope_page_layer_ids_t.lt(0).any()) or not torch.equal( expected_scope_page_ids_t, torch.sort(expected_scope_page_ids_t).values, ) or torch.unique(expected_scope_page_ids_t).numel() != expected_scope_page_ids_t.numel() or not bool( torch.isin(expected_scope_page_ids_t, physical_page_ids).all() ) ): raise RuntimeError( "Resynthesis all-knowledge expected branch scope differs" ) target_payload = _validated_full_additive_checkpoint_payload_boundary( target.payload ) parent_payload = _validated_full_additive_checkpoint_payload_boundary( common_parent.payload ) target_parameters = cast( dict[str, torch.Tensor], target_payload["parameters"], ) target_buffers = cast( dict[str, torch.Tensor], target_payload["buffers"], ) parent_parameters = cast( dict[str, torch.Tensor], parent_payload["parameters"], ) parent_buffers = cast( dict[str, torch.Tensor], parent_payload["buffers"], ) target_state = {**target_parameters, **target_buffers} parent_state = {**parent_parameters, **parent_buffers} target_lineage = cast(dict[str, Any], target_payload["lineage"]) parent_lineage = cast(dict[str, Any], parent_payload["lineage"]) normalized_parent_lineage = copy.deepcopy(parent_lineage) normalized_target_lineage = copy.deepcopy(target_lineage) parent_paged_lineage = normalized_parent_lineage.get("pagedNoNE") target_paged_lineage = normalized_target_lineage.get("pagedNoNE") if ( isinstance(parent_paged_lineage, dict) and isinstance(target_paged_lineage, dict) and parent_paged_lineage.get("sharedCompactObjectOverlay") is True and "sharedCompactObjectOverlay" not in target_paged_lineage and parent_paged_lineage.get("compactBankCapacityClaimedTrained") is False and parent_paged_lineage.get("replicatedGenerationTransaction") is True and parent_paged_lineage.get("storageBoundaryMayReroute") is False ): parent_paged_lineage.pop("sharedCompactObjectOverlay") if ( set(target_state) != set(parent_state) or normalized_target_lineage != normalized_parent_lineage ): raise RuntimeError( "Resynthesis all-knowledge target and common parent graph differ" ) for name, parent_value in parent_state.items(): target_value = target_state[name] precision_compatible = ( target_value.dtype == parent_value.dtype or ( target_value.is_floating_point() and parent_value.is_floating_point() ) ) if ( target_value.shape != parent_value.shape or not precision_compatible ): raise RuntimeError( "Resynthesis all-knowledge target tensor geometry differs: " f"{name}" ) ordered_branches = tuple( sorted( branches, key=lambda branch: str(branch.scope.external_record_boundary()[ "scopeSha256" ]), ) ) checkpoint_binding_by_scope_sha256 = { bytes(scope_sha256_t.tolist()).hex(): ( authority.branch_checkpoint_paths[branch_index], authority.branch_checkpoint_sha256s[branch_index], ) for branch_index, scope_sha256_t in enumerate( authority.branch_scope_sha256s_t.detach() .cpu() .to(dtype=torch.uint8) ) } if ( len(checkpoint_binding_by_scope_sha256) != len(ordered_branches) or any( ( checkpoint_binding_by_scope_sha256.get( str(branch.scope.external_record_boundary()["scopeSha256"]) ) != ( branch.artifact_path.expanduser().resolve(), branch.artifact_sha256, ) ) for branch in ordered_branches ) ): raise RuntimeError( "Resynthesis all-knowledge branch checkpoint authority differs" ) expected_scope_sha256s = sorted( bytes(row.tolist()).hex() for row in ( authority.branch_scope_sha256s_t.detach() .cpu() .to(dtype=torch.uint8) ) ) observed_scope_sha256s = sorted( bytes(branch.scope.scope_sha256_t.detach().cpu().tolist()).hex() for branch in ordered_branches ) if ( expected_scope_sha256s != observed_scope_sha256s or len(set(expected_scope_sha256s)) != len(expected_scope_sha256s) ): raise RuntimeError( "Resynthesis all-knowledge branch scope authority differs" ) for branch in ordered_branches: validate_training_branch_scope_boundary(branch.scope) if ( branch.base_checkpoint_path.expanduser().resolve() != authority.common_parent_checkpoint_path or branch.base_checkpoint_sha256 != common_parent.artifact_sha256 or branch.lineage != parent_payload["lineage"] or set(branch.buffers) != set(parent_buffers) or not torch.equal( branch.scope.parent_generation_t.detach() .cpu() .long() .reshape(()), authority.common_parent_generation_t.detach() .cpu() .long() .reshape(()), ) or not torch.equal( branch.scope.parent_manifest_payload_sha256_t.detach() .cpu() .to(dtype=torch.uint8), authority.common_parent_manifest_payload_sha256_t.detach() .cpu() .to(dtype=torch.uint8), ) ): raise RuntimeError( "Resynthesis all-knowledge branch parent contract differs" ) for name, parent_value in parent_buffers.items(): branch_value = branch.buffers[name] if ( branch_value.shape != parent_value.shape or branch_value.dtype != parent_value.dtype ): raise RuntimeError( "Resynthesis all-knowledge branch buffer geometry differs: " f"{name}" ) scope_page_ids = validate_common_parent_training_branch_scopes_boundary( tuple(branch.scope for branch in ordered_branches) ) observed_scope_page_ids_t = torch.cat( tuple( branch.scope.page_ids_t.detach().cpu().long() for branch in ordered_branches ) ) observed_scope_layer_ids_t = torch.cat( tuple( branch.scope.page_layer_ids_t.detach().cpu().long() for branch in ordered_branches ) ) observed_scope_order_t = torch.argsort(observed_scope_page_ids_t) if ( not torch.equal( observed_scope_page_ids_t.index_select( 0, observed_scope_order_t, ), expected_scope_page_ids_t, ) or not torch.equal( observed_scope_layer_ids_t.index_select( 0, observed_scope_order_t, ), expected_scope_page_layer_ids_t, ) ): raise RuntimeError( "Resynthesis all-knowledge branch scope coverage differs" ) if ( not torch.equal(scope_page_ids, expected_scope_page_ids_t) or not bool(torch.isin(scope_page_ids, physical_page_ids).all()) ): raise RuntimeError( "Resynthesis all-knowledge branch scope escapes the physical catalog" ) catalog_names = tuple( sorted( name for name in parent_buffers if name.endswith( ".paged_expert_runtime.router.page_catalog_ids_t" ) ) ) catalog_page_ids: list[torch.Tensor] = [] layer_catalog_by_id: dict[int, torch.Tensor] = {} for catalog_name in catalog_names: match = re.match( r"^science_stack\.science_layer_(\d+)\." r"paged_expert_runtime\.router\.page_catalog_ids_t$", catalog_name, ) if match is None: raise RuntimeError( "Resynthesis all-knowledge page catalog name differs" ) layer_id = int(match.group(1)) parent_catalog_t = parent_buffers[catalog_name].detach().cpu().long() target_catalog_t = target_state[catalog_name].detach().cpu().long() if ( parent_catalog_t.ndim != 1 or parent_catalog_t.numel() < 1 or torch.unique(parent_catalog_t).numel() != parent_catalog_t.numel() or not torch.equal(parent_catalog_t, target_catalog_t) or any( not torch.equal( branch.buffers[catalog_name].detach().cpu().long(), parent_catalog_t, ) for branch in ordered_branches ) ): raise RuntimeError( "Resynthesis all-knowledge page catalog tensor differs" ) layer_catalog_by_id[layer_id] = parent_catalog_t catalog_page_ids.append(parent_catalog_t) if ( not catalog_page_ids or not torch.equal( torch.sort(torch.cat(catalog_page_ids)).values, physical_page_ids, ) ): raise RuntimeError( "Resynthesis all-knowledge tensor catalogs do not cover every page" ) page_layer_by_id = { int(page_id): layer_id for layer_id, catalog_t in layer_catalog_by_id.items() for page_id in catalog_t.tolist() } if any( page_layer_by_id.get(int(page_id)) != int(page_layer_id) for page_id, page_layer_id in zip( expected_scope_page_ids_t.tolist(), expected_scope_page_layer_ids_t.tolist(), strict=True, ) ): raise RuntimeError( "Resynthesis all-knowledge branch page/layer authority differs" ) branch_scope_by_layer: dict[str, dict[int, set[int]]] = {} for branch in ordered_branches: scope_record = branch.scope.external_record_boundary() scope_key = str(scope_record["scopeSha256"]) page_ids = branch.scope.page_ids_t.detach().cpu().long() layer_ids = branch.scope.page_layer_ids_t.detach().cpu().long() branch_scope_by_layer[scope_key] = { layer_id: { int(page_ids[row_index]) for row_index in torch.nonzero( layer_ids.eq(layer_id), as_tuple=False, ).reshape(-1) } for layer_id in layer_catalog_by_id } cumulative_names = { name for name in parent_buffers if name == "science_stack._step_count" or _RECONCILED_PAGED_RUNTIME_BUFFER_PATTERN.match(name) is not None } router_bias_names = tuple( sorted( name for name in parent_buffers if name.endswith(".quantile_router.expert_bias_t") ) ) expected_router_bias_names = { name for layer_id in layer_catalog_by_id for name in ( ( f"science_stack.science_layer_{layer_id}." "quantile_router.expert_bias_t" ), ( f"science_stack.science_layer_{layer_id}." "paged_expert_runtime.router.quantile_router.expert_bias_t" ), ) } expected_cumulative_count = 1 + 5 * len(catalog_names) if ( len(cumulative_names) != expected_cumulative_count or len(router_bias_names) != 2 * len(catalog_names) or set(router_bias_names) != expected_router_bias_names or set(cumulative_names).intersection(router_bias_names) ): raise RuntimeError( "Resynthesis all-knowledge router/cumulative topology differs" ) stable_names = ( set(parent_buffers) - cumulative_names - set(router_bias_names) ) reconciled_buffers = dict(target_buffers) for name in stable_names: reference = ordered_branches[0].buffers[name] if any( not torch.equal(reference, branch.buffers[name]) for branch in ordered_branches[1:] ): raise RuntimeError( "Resynthesis all-knowledge stable branch buffer differs: " f"{name}" ) target_value = target_state[name] if not torch.equal( target_value.detach().cpu().to(dtype=reference.dtype), reference, ): raise RuntimeError( "Resynthesis all-knowledge target stable buffer differs: " f"{name}" ) for name in sorted(cumulative_names): parent_value = parent_buffers[name].detach().cpu() target_value = target_state[name].detach().cpu() match = _RECONCILED_PAGED_RUNTIME_BUFFER_PATTERN.match(name) suffix = match.group(3) if match is not None else None runtime_layer_id: int | None = ( int(match.group(2)) if match is not None else None ) if runtime_layer_id is not None: catalog_count = int( layer_catalog_by_id[runtime_layer_id].numel() ) expected_shape = ( (catalog_count, _RECONCILED_GRADIENT_SIGNATURE_WIDTH) if suffix == "accepted_gradient_signature_t" else (catalog_count,) ) if parent_value.shape != expected_shape: raise RuntimeError( "Resynthesis all-knowledge cumulative catalog geometry " f"differs: {name}" ) values = (parent_value, target_value) + tuple( branch.buffers[name].detach().cpu() for branch in ordered_branches ) if not all(torch.isfinite(value).all() for value in values): raise RuntimeError( "Resynthesis all-knowledge cumulative buffer is non-finite: " f"{name}" ) monotonic = ( name == "science_stack._step_count" or suffix in _RECONCILED_MONOTONIC_PAGED_BUFFER_SUFFIXES ) if monotonic and ( bool(target_value.lt(parent_value).any()) or any( bool(branch.buffers[name].detach().cpu().lt(parent_value).any()) for branch in ordered_branches ) ): raise RuntimeError( "Resynthesis all-knowledge cumulative buffer regressed: " f"{name}" ) if ( match is not None and suffix in _RECONCILED_SCOPED_PAGED_BUFFER_SUFFIXES ): assert layer_id is not None catalog_t = layer_catalog_by_id[layer_id] for branch in ordered_branches: scope_key = str( branch.scope.external_record_boundary()["scopeSha256"] ) owned_ids = branch_scope_by_layer[scope_key][layer_id] owned_mask_t = torch.tensor( [int(page_id_t) in owned_ids for page_id_t in catalog_t], dtype=torch.bool, ) delta_t = branch.buffers[name].detach().cpu() - parent_value changed_t = ( delta_t.ne(0) if delta_t.ndim == 1 else delta_t.ne(0).flatten(1).any(dim=1) ) if bool((changed_t & ~owned_mask_t).any()): raise RuntimeError( "Resynthesis all-knowledge gradient evidence escaped " f"branch ownership: {name}" ) compute_dtype = ( torch.float64 if target_value.dtype == torch.float64 else ( torch.float32 if target_value.is_floating_point() else torch.int64 ) ) reconciled = target_value.to(dtype=compute_dtype) parent_compute = parent_value.to(dtype=compute_dtype) for branch in ordered_branches: branch_compute = ( branch.buffers[name].detach().cpu().to(dtype=compute_dtype) ) branch_delta = branch_compute - parent_compute if compute_dtype == torch.int64 and ( target_value.dtype != torch.long or parent_value.dtype != torch.long or branch.buffers[name].dtype != torch.long or bool(branch_delta.lt(0).any()) or bool( branch_delta.gt( torch.full_like( reconciled, torch.iinfo(torch.long).max, ) - reconciled ).any() ) ): raise RuntimeError( "Resynthesis all-knowledge cumulative integer overflow: " f"{name}" ) reconciled = reconciled + branch_delta reconciled = reconciled.to(dtype=target_value.dtype) if ( not torch.isfinite(reconciled).all() or ( monotonic and ( bool(reconciled.lt(target_value).any()) or bool(reconciled.lt(0).any()) ) ) ): raise RuntimeError( "Resynthesis all-knowledge cumulative merge differs: " f"{name}" ) reconciled_buffers[name] = reconciled dense_bias_widths: set[int] = set() for name in router_bias_names: target_value = target_state[name] if ( target_value.ndim != 1 or target_value.numel() < 1 or not target_value.is_floating_point() ): raise RuntimeError( "Resynthesis all-knowledge router bias geometry differs" ) paged_match = re.match( r"^science_stack\.science_layer_(\d+)\." r"paged_expert_runtime\.router\.quantile_router\.expert_bias_t$", name, ) dense_match = re.match( r"^science_stack\.science_layer_(\d+)\." r"quantile_router\.expert_bias_t$", name, ) if paged_match is not None: layer_id = int(paged_match.group(1)) if target_value.shape != layer_catalog_by_id[layer_id].shape: raise RuntimeError( "Resynthesis all-knowledge paged router bias geometry differs" ) elif dense_match is not None: dense_bias_widths.add(int(target_value.numel())) else: raise RuntimeError( "Resynthesis all-knowledge router bias identity differs" ) reconciled_buffers[name] = torch.zeros_like( target_value, device="cpu", ) if len(dense_bias_widths) != 1: raise RuntimeError( "Resynthesis all-knowledge dense router bias geometry differs" ) reconciled_state = {**target_parameters, **reconciled_buffers} state_key_sha256, state_geometry_sha256 = ( _checkpoint_state_identity_boundary(reconciled_state) ) pending_state_value_sha256 = _checkpoint_state_value_sha256_boundary( reconciled_state ) payload = { "schema": RECONCILED_ADDITIVE_CALIBRATION_PENDING_SCHEMA, "finalCheckpointSchema": ADDITIVE_CHECKPOINT_SCHEMA, "lineage": copy.deepcopy(target_payload["lineage"]), "stateKeySetSha256": state_key_sha256, "stateGeometrySha256": state_geometry_sha256, "parameters": dict(target_parameters), "buffers": reconciled_buffers, } physical_page_ids_sha256 = hashlib.sha256( physical_page_ids.contiguous().numpy().tobytes() ).hexdigest() branch_scope_page_pairs_sha256 = hashlib.sha256( torch.stack( ( expected_scope_page_ids_t, expected_scope_page_layer_ids_t, ), dim=1, ) .contiguous() .numpy() .tobytes() ).hexdigest() branch_scope_page_ids_sha256 = hashlib.sha256( expected_scope_page_ids_t.contiguous().numpy().tobytes() ).hexdigest() merged_cumulative_value_sha256 = _checkpoint_state_value_sha256_boundary( { name: reconciled_buffers[name] for name in cumulative_names } ) router_bias_value_sha256 = _checkpoint_state_value_sha256_boundary( { name: reconciled_buffers[name] for name in router_bias_names } ) parameter_key_set_sha256 = hashlib.sha256( "\n".join(sorted(target_parameters)).encode("utf-8") ).hexdigest() buffer_key_set_sha256 = hashlib.sha256( "\n".join(sorted(reconciled_buffers)).encode("utf-8") ).hexdigest() pending_state_composite = { "targetCheckpointSha256": target.artifact_sha256, "stateKeySetSha256": state_key_sha256, "stateGeometrySha256": state_geometry_sha256, "parameterKeySetSha256": parameter_key_set_sha256, "bufferKeySetSha256": buffer_key_set_sha256, "pendingStateValueSha256": pending_state_value_sha256, "mergedCumulativeValueSha256": merged_cumulative_value_sha256, "routerBiasValueSha256": router_bias_value_sha256, } authority_scope_sha256s = tuple( bytes(row.tolist()).hex() for row in ( authority.branch_scope_sha256s_t.detach() .cpu() .to(dtype=torch.uint8) ) ) authority_row_by_scope_sha256 = { scope_sha256: row_index for row_index, scope_sha256 in enumerate(authority_scope_sha256s) } ordered_authority_row_indices = tuple( authority_row_by_scope_sha256[ str(branch.scope.external_record_boundary()["scopeSha256"]) ] for branch in ordered_branches ) unsigned_proof: dict[str, Any] = { "schema": RECONCILED_ADDITIVE_KNOWLEDGE_PROOF_SCHEMA, "targetCheckpointPath": str(authority.target_checkpoint_path), "targetCheckpointSha256": target.artifact_sha256, "targetOptimizerPath": str(authority.target_optimizer_path), "targetOptimizerSha256": authority.target_optimizer_sha256, "targetExternalStatePath": str( authority.target_external_state_path ), "targetExternalStateSha256": ( authority.target_external_state_sha256 ), "commonParentCheckpointPath": str( authority.common_parent_checkpoint_path ), "commonParentCheckpointSha256": common_parent.artifact_sha256, "commonParentGeneration": int( authority.common_parent_generation_t.detach().cpu().long() ), "commonParentManifestPayloadSha256": bytes( authority.common_parent_manifest_payload_sha256_t.detach() .cpu() .tolist() ).hex(), "targetGeneration": int( authority.target_generation_t.detach().cpu().long() ), "targetManifestSha256": bytes( authority.target_manifest_sha256_t.detach().cpu().tolist() ).hex(), "targetManifestPayloadSha256": bytes( authority.target_manifest_payload_sha256_t.detach() .cpu() .tolist() ).hex(), "branchCheckpointSha256s": [ branch.artifact_sha256 for branch in ordered_branches ], "branchCheckpointPaths": [ str(authority.branch_checkpoint_paths[row_index]) for row_index in ordered_authority_row_indices ], "branchOptimizerSha256s": [ authority.branch_optimizer_sha256s[row_index] for row_index in ordered_authority_row_indices ], "branchOptimizerPaths": [ str(authority.branch_optimizer_paths[row_index]) for row_index in ordered_authority_row_indices ], "branchExternalStateSha256s": [ authority.branch_external_state_sha256s[row_index] for row_index in ordered_authority_row_indices ], "branchExternalStatePaths": [ str(authority.branch_external_state_paths[row_index]) for row_index in ordered_authority_row_indices ], "branchGenerations": [ int(authority.branch_generations_t[row_index]) for row_index in ordered_authority_row_indices ], "branchManifestSha256s": [ bytes( authority.branch_manifest_sha256s_t[row_index] .detach() .cpu() .tolist() ).hex() for row_index in ordered_authority_row_indices ], "branchManifestPayloadSha256s": [ bytes( authority.branch_manifest_payload_sha256s_t[row_index] .detach() .cpu() .tolist() ).hex() for row_index in ordered_authority_row_indices ], "branchScopeSha256s": [ branch.scope.external_record_boundary()["scopeSha256"] for branch in ordered_branches ], "physicalPageCount": int(physical_page_ids.numel()), "minimumPhysicalPageId": int(physical_page_ids[0]), "maximumPhysicalPageId": int(physical_page_ids[-1]), "physicalPageIdsSha256": physical_page_ids_sha256, "branchOwnedPageCount": int(scope_page_ids.numel()), "branchScopePageIdsSha256": branch_scope_page_ids_sha256, "branchScopePagePairsSha256": branch_scope_page_pairs_sha256, "branchUnionSha256": bytes( authority.branch_union_sha256_t.detach().cpu().tolist() ).hex(), "lineageRebaseSha256": bytes( authority.lineage_rebase_sha256_t.detach().cpu().tolist() ).hex(), "cumulativeBufferCount": len(cumulative_names), "stableConsensusBufferCount": len(stable_names), "routerBiasCount": len(router_bias_names), "stateKeySetSha256": state_key_sha256, "stateGeometrySha256": state_geometry_sha256, "parameterKeySetSha256": parameter_key_set_sha256, "bufferKeySetSha256": buffer_key_set_sha256, "pendingStateValueSha256": pending_state_value_sha256, "cumulativeMerge": "target_plus_each_branch_minus_common_parent", "mergedCumulativeValueSha256": merged_cumulative_value_sha256, "routerBiasValueSha256": router_bias_value_sha256, "pendingStateCompositeSha256": ( _canonical_json_sha256_boundary(pending_state_composite) ), "routerBiasArithmeticMergeAllowed": False, "routerBiasCalibrationPending": True, "targetFreeCalibrationRequired": True, "directCheckpointPublicationAllowed": False, } proof = { **unsigned_proof, "proofSha256": _canonical_json_sha256_boundary(unsigned_proof), } return AdditiveKnowledgeCalibrationPacket( payload=payload, router_bias_names=router_bias_names, physical_page_ids_t=physical_page_ids, branch_scope_page_ids_t=scope_page_ids.detach().cpu().long().clone(), proof=proof, ) def _checkpoint_tensor_state_boundary( payload: dict[str, Any], ) -> dict[str, torch.Tensor]: """Extract one exact tensor state from a validated checkpoint payload.""" parameters = payload.get("parameters") buffers = payload.get("buffers") model = payload.get("model") if isinstance(parameters, dict) and isinstance(buffers, dict): candidate: object = {**parameters, **buffers} elif isinstance(model, dict): candidate = model else: candidate = payload if not isinstance(candidate, dict) or not all( isinstance(name, str) and isinstance(value, torch.Tensor) for name, value in candidate.items() ): raise RuntimeError( "Resynthesis additive checkpoint is not a tensor state mapping" ) return dict(candidate) def _resolved_additive_checkpoint_geometry_boundary( checkpoint_path: Path, ) -> tuple[int, int, bool, int, int, int, int]: """Read structural and lexical geometry before model allocation.""" from resynthesis.geometry_migration import ( STRUCTURAL_EXPERTS, checkpoint_geometry, ) probe = torch.load( checkpoint_path, map_location="meta", mmap=True, weights_only=True, ) if not isinstance(probe, dict): raise RuntimeError( "Resynthesis additive checkpoint envelope is invalid" ) if probe.get("schema") not in { ADDITIVE_BRANCH_DELTA_SCHEMA, ADDITIVE_BRANCH_CAUSAL_DELTA_SCHEMA, }: layers, experts, migrated = checkpoint_geometry(checkpoint_path) lineage = probe.get("lineage") if not isinstance(lineage, dict): raise RuntimeError( "Resynthesis additive checkpoint lineage is absent" ) else: lineage = ( validate_additive_branch_causal_delta_boundary( checkpoint_path ).lineage if probe.get("schema") == ADDITIVE_BRANCH_CAUSAL_DELTA_SCHEMA else validate_additive_branch_delta_boundary( checkpoint_path ).lineage ) layers = int(lineage.get("scienceLayers", 0)) experts = int(lineage.get("scienceExperts", 0)) migrated = bool( lineage.get("draftingCheckpointGeometryChanged", False) ) if ( layers < 1 or experts < STRUCTURAL_EXPERTS + 1 or int(lineage.get("parallelDraftWorkers", 0)) != experts ): raise RuntimeError( "Resynthesis branch delta checkpoint geometry is invalid" ) lexical_vocabulary_size = lineage.get("lexicalVocabularySize", 0) projection_vocabulary_size = lineage.get("vocabSize", 0) projection_only_vocabulary_rows = lineage.get( "projectionOnlyVocabularyRows", ) tokenizer_prefix_vocabulary_size = lineage.get( "tokenizerPrefixVocabularySize", ) vocabulary_transfer_rank = lineage.get("vocabularyTransferRank", 0) current_vocabulary_schema = ( lineage.get("schema") == COMPOSED_ADDITIVE_LINEAGE_SCHEMA ) if ( tokenizer_prefix_vocabulary_size is None and current_vocabulary_schema and type(projection_vocabulary_size) is int and type(projection_only_vocabulary_rows) is int and 0 <= projection_only_vocabulary_rows < projection_vocabulary_size ): # Exact v29 checkpoints written before the prefix field was named # already bind the same immutable boundary through P - source rows. tokenizer_prefix_vocabulary_size = ( projection_vocabulary_size - projection_only_vocabulary_rows ) if tokenizer_prefix_vocabulary_size is None: tokenizer_prefix_vocabulary_size = 0 if ( type(projection_vocabulary_size) is not int or type(lexical_vocabulary_size) is not int or type(tokenizer_prefix_vocabulary_size) is not int or type(vocabulary_transfer_rank) is not int or projection_vocabulary_size < 1 or lexical_vocabulary_size < 0 or tokenizer_prefix_vocabulary_size < 0 or vocabulary_transfer_rank < 0 ): raise RuntimeError( "Resynthesis checkpoint lexical geometry is invalid" ) if current_vocabulary_schema and ( lexical_vocabulary_size < 1 or tokenizer_prefix_vocabulary_size < 1 or tokenizer_prefix_vocabulary_size > lexical_vocabulary_size or type(projection_only_vocabulary_rows) is not int or projection_only_vocabulary_rows != ( projection_vocabulary_size - min( projection_vocabulary_size, tokenizer_prefix_vocabulary_size, ) ) or ( projection_only_vocabulary_rows > 0 and vocabulary_transfer_rank < 1 ) or ( projection_only_vocabulary_rows == 0 and ( vocabulary_transfer_rank != 0 or lexical_vocabulary_size > projection_vocabulary_size ) ) ): raise RuntimeError( "Resynthesis checkpoint vocabulary-transfer geometry is invalid" ) return ( layers, experts, migrated, projection_vocabulary_size, lexical_vocabulary_size, tokenizer_prefix_vocabulary_size, vocabulary_transfer_rank, ) def _checkpoint_adopted_vocabulary_configuration_boundary( cfg: ResynthesisConfig, *, projection_vocabulary_size: int, lexical_vocabulary_size: int, tokenizer_prefix_vocabulary_size: int, vocabulary_transfer_rank: int, ) -> ResynthesisConfig: """Adopt retained lexical growth without changing immutable coordinates.""" if ( type(projection_vocabulary_size) is not int or type(lexical_vocabulary_size) is not int or type(tokenizer_prefix_vocabulary_size) is not int or type(vocabulary_transfer_rank) is not int or lexical_vocabulary_size < 0 or tokenizer_prefix_vocabulary_size < 0 or vocabulary_transfer_rank < 0 ): raise RuntimeError( "Resynthesis checkpoint vocabulary configuration is invalid" ) if projection_vocabulary_size != cfg.vocab_size: raise RuntimeError( "Resynthesis checkpoint projection vocabulary differs" ) if ( tokenizer_prefix_vocabulary_size > 0 and tokenizer_prefix_vocabulary_size != cfg.tokenizer_prefix_vocab_size ): raise RuntimeError( "Resynthesis checkpoint tokenizer prefix differs" ) return replace( cfg, tokenizer_vocab_size=max( cfg.tokenizer_vocab_size, lexical_vocabulary_size, ), vocabulary_transfer_rank=max( cfg.vocabulary_transfer_rank, vocabulary_transfer_rank, ), ) def _admitted_capacity_subplan_boundary( *, migration_receipt_path: Path | None, migration_plan_path: Path, migration_plan_sha256: str, migration_alignment: dict[str, Any], target_alignment: dict[str, Any], target_physical: dict[str, Any], ) -> bool: """Prove that a smaller capacity plan is the exactly admitted bank.""" from resynthesis.none_paging import ( page_generation_schema_supported_boundary, ) if migration_receipt_path is None: return False receipt_path = migration_receipt_path.expanduser().resolve() if not receipt_path.is_file(): return False receipt_sha256 = _file_sha256_boundary(receipt_path) receipt = json.loads(receipt_path.read_text(encoding="utf-8")) if _file_sha256_boundary(receipt_path) != receipt_sha256: raise RuntimeError("paged NoNE admission receipt changed during validation") if not isinstance(receipt, dict): return False growth = receipt.get("growthPlan") expansion = receipt.get("expansion") generation = receipt.get("generationBinding") accepted_pointer = receipt.get("acceptedPointer") artifacts = receipt.get("artifacts") checks = receipt.get("checks") graph_authority = ( accepted_pointer.get("graphAuthority") if isinstance(accepted_pointer, dict) else None ) topology = ( graph_authority.get("topology") if isinstance(graph_authority, dict) else None ) if not all( isinstance(record, dict) for record in ( growth, expansion, generation, accepted_pointer, artifacts, checks, graph_authority, topology, ) ): return False assert isinstance(growth, dict) assert isinstance(expansion, dict) assert isinstance(generation, dict) assert isinstance(accepted_pointer, dict) assert isinstance(artifacts, dict) assert isinstance(checks, dict) assert isinstance(graph_authority, dict) assert isinstance(topology, dict) migration_additive = migration_alignment.get("additiveBankPageCount") target_inherited = target_alignment.get("inheritedPhysicalNoNELayerCount") target_additive = target_alignment.get("additiveBankPageCount") target_count = target_alignment.get("alignedPhysicalNoNELayerCount") target_page_elements = target_alignment.get("pageModelParameterElements") target_added_elements = target_alignment.get( "additiveBankPhysicalParameterElements" ) target_total_elements = target_alignment.get("alignedPhysicalParameterElements") target_session = target_alignment.get("bankSessionId") updated_page_ids = generation.get("updatedPageIds") generation_number = generation.get("generation") parent_generation = generation.get("parentGeneration") if ( type(migration_additive) is not int or type(target_inherited) is not int or type(target_additive) is not int or type(target_count) is not int or type(target_page_elements) is not int or type(target_added_elements) is not int or type(target_total_elements) is not int or not isinstance(target_session, list) or not target_session or not all(type(value) is int for value in target_session) or not isinstance(updated_page_ids, list) or not all(type(page_id) is int for page_id in updated_page_ids) or len(set(updated_page_ids)) != len(updated_page_ids) or type(generation_number) is not int or type(parent_generation) is not int or not 0 < target_additive < migration_additive or target_count != target_inherited + target_additive or target_added_elements != target_additive * target_page_elements or target_total_elements != target_physical.get("physicalGraphParameterElements") ): return False page_catalog_record = artifacts.get("pageCatalog") if not isinstance(page_catalog_record, dict): return False catalog_path_value = page_catalog_record.get("path") catalog_sha256 = page_catalog_record.get("sha256") if ( not isinstance(catalog_path_value, str) or not isinstance(catalog_sha256, str) or len(catalog_sha256) != 64 ): return False catalog_path = Path(catalog_path_value).expanduser().resolve() if ( not catalog_path.is_file() or _file_sha256_boundary(catalog_path) != catalog_sha256 ): return False catalog = json.loads(catalog_path.read_text(encoding="utf-8")) compact_banks = catalog.get("compactPageBanks") if isinstance(catalog, dict) else None if not isinstance(catalog, dict) or not isinstance(compact_banks, list): return False summary_value = target_alignment.get("bankSummaryPath") summary_sha256 = target_alignment.get("bankSummarySha256") journal_value = target_alignment.get("bankJournalPath") journal_sha256 = target_alignment.get("bankJournalSha256") if not all( isinstance(value, str) for value in ( summary_value, summary_sha256, journal_value, journal_sha256, ) ): return False assert isinstance(summary_value, str) assert isinstance(summary_sha256, str) assert isinstance(journal_value, str) assert isinstance(journal_sha256, str) summary_path = Path(summary_value).expanduser().resolve() journal_path = Path(journal_value).expanduser().resolve() if ( len(summary_sha256) != 64 or len(journal_sha256) != 64 or not summary_path.is_file() or _file_sha256_boundary(summary_path) != summary_sha256 or not journal_path.is_file() or _file_sha256_boundary(journal_path) != journal_sha256 ): return False summary = json.loads(summary_path.read_text(encoding="utf-8")) if not isinstance(summary, dict): return False start_page_id = summary.get("startPageId") matching_banks = [ bank for bank in compact_banks if isinstance(bank, dict) and Path(str(bank.get("summaryPath", ""))).expanduser().resolve() == summary_path and bank.get("summarySha256") == summary_sha256 and Path(str(bank.get("journalPath", ""))).expanduser().resolve() == journal_path and bank.get("journalSha256") == journal_sha256 and bank.get("sessionId") == target_session and bank.get("bankPageCount") == target_additive and bank.get("physicalParameterElementsInitialized") == target_added_elements ] if len(matching_banks) != 1: return False admitted_bank = matching_banks[0] admitted_ids = admitted_bank.get("admittedPageIds") latest_ids = admitted_bank.get("latestCohortPageIds") if ( not isinstance(admitted_ids, list) or admitted_ids != updated_page_ids or latest_ids != updated_page_ids or type(start_page_id) is not int or updated_page_ids != list(range(start_page_id, start_page_id + target_additive)) ): return False manifest_relative = accepted_pointer.get("manifest") session_key = accepted_pointer.get("sessionKey") page_store_root = artifacts.get("pageStoreRoot") manifest_sha256 = accepted_pointer.get("manifestSha256") manifest_payload_sha256 = accepted_pointer.get("manifestPayloadSha256") if not all( isinstance(value, str) for value in ( manifest_relative, session_key, page_store_root, manifest_sha256, manifest_payload_sha256, ) ): return False assert isinstance(manifest_relative, str) assert isinstance(session_key, str) assert isinstance(page_store_root, str) assert isinstance(manifest_sha256, str) assert isinstance(manifest_payload_sha256, str) session_root = Path(page_store_root).expanduser().resolve() / "sessions" / session_key manifest_path = (session_root / manifest_relative).resolve() pointer_path = session_root / "accepted.json" if ( not manifest_path.is_relative_to(session_root) or not manifest_path.is_file() or _file_sha256_boundary(manifest_path) != manifest_sha256 or not pointer_path.is_file() or json.loads(pointer_path.read_text(encoding="utf-8")) != accepted_pointer ): return False manifest = json.loads(manifest_path.read_text(encoding="utf-8")) manifest_payload = ( {key: value for key, value in manifest.items() if key != "manifestPayloadSha256"} if isinstance(manifest, dict) else None ) components = manifest.get("components") if isinstance(manifest, dict) else None corpus_component = ( components.get("corpus") if isinstance(components, dict) else None ) checkpoint_component = ( components.get("model") if isinstance(components, dict) else None ) optimizer_component = ( components.get("optimizer") if isinstance(components, dict) else None ) checkpoint_record = artifacts.get("checkpoint") optimizer_record = artifacts.get("optimizer") return bool( receipt.get("schema") == "nnf.resynthesis.none_v2_plus_catalog_expansion.v1" and receipt.get("passed") is True and receipt.get("status") == "PHYSICAL_ADMISSION_ACCEPTED_TRAINING_PENDING" and receipt.get("trainingClaimed") is False and receipt.get("promotionEligible") is False and Path(str(growth.get("path", ""))).expanduser().resolve() == migration_plan_path and growth.get("sha256") == migration_plan_sha256 and growth.get("plannedObjectivePages") == migration_additive and growth.get("addedObjectivePages") == target_additive and growth.get("targetObjectivePages") == target_additive and growth.get("pendingObjectivePages") == migration_additive - target_additive and expansion.get("sourcePageObjects") == target_inherited and expansion.get("addedPageObjects") == target_additive and expansion.get("targetPageObjects") == target_count and expansion.get("pageModelParameterElements") == target_page_elements and expansion.get("addedPageModelParameterElements") == target_added_elements and expansion.get("targetPageModelParameterElements") == target_total_elements and expansion.get("sessionId") == target_session and expansion.get("trainingStartedForNewRoots") is False and expansion.get("compactBankCapacityClaimedTrained") is False and expansion.get("trainedValidatedNewFamilyExperts") == 0 and generation.get("schema") == "nnf.resynthesis.none_generation_binding.v1" and generation_number == accepted_pointer.get("generation") and parent_generation + 1 == generation_number and len(updated_page_ids) == target_additive and generation.get("manifest") == manifest_relative and generation.get("manifestSha256") == manifest_sha256 and generation.get("manifestPayloadSha256") == manifest_payload_sha256 and graph_authority.get("schema") == "nnf.resynthesis.none_graph_authority.v1" and Path(str(graph_authority.get("migrationReceiptPath", ""))) .expanduser() .resolve() == receipt_path and topology.get("pages") == target_count and checks.get("acceptedPageObjectsPrefixExact") is True and checks.get("compactBankAdmissionUntrained") is True and checks.get("allNewRootsExplicitlyUntrained") is True and catalog.get("schema") == "nnf.resynthesis.none_v2_plus_page_catalog.v1" and catalog.get("growthPlanSha256") == migration_plan_sha256 and catalog.get("plannedObjectivePageCount") == migration_additive and catalog.get("addedObjectivePageCount") == target_additive and catalog.get("objectivePageCount") == target_additive and catalog.get("pendingObjectivePageCount") == migration_additive - target_additive and catalog.get("pageCount") == target_count and catalog.get("physicalPageModelParameterElements") == target_total_elements and catalog.get("sessionId") == target_session and admitted_bank.get("schema") == "nnf.resynthesis.compact_page_bank_authority.v1" and admitted_bank.get("admissionTrainingClaimed") is False and admitted_bank.get("sourceBankTrainedPageCountAtDiscovery") == 0 and summary.get("schema") == "nnf.resynthesis.compact_transfer_page_summary.v1" and summary.get("passed") is True and summary.get("requestedPageCount") == target_additive and summary.get("journaledPageCount") == target_additive and summary.get("transferInitializedPageCount") == target_additive and summary.get("trainedPageCount") == 0 and summary.get("journalPath") == str(journal_path) and summary.get("journalSha256") == journal_sha256 and summary.get("sessionId") == target_session and summary.get("physicalParameterElementsInitialized") == target_added_elements and isinstance(manifest, dict) and page_generation_schema_supported_boundary( manifest.get("schema") ) and manifest.get("sessionKey") == session_key and manifest.get("generation") == generation.get("generation") and manifest.get("parentGeneration") == generation.get("parentGeneration") and manifest.get("pageCount") == target_count and manifest.get("updatedPageIds") == updated_page_ids and manifest.get("manifestPayloadSha256") == manifest_payload_sha256 and isinstance(manifest_payload, dict) and hashlib.sha256( json.dumps( manifest_payload, sort_keys=True, separators=(",", ":"), ).encode("utf-8") ).hexdigest() == manifest_payload_sha256 and isinstance(corpus_component, dict) and corpus_component.get("sha256") == migration_plan_sha256 and isinstance(checkpoint_component, dict) and isinstance(checkpoint_record, dict) and checkpoint_component.get("sha256") == checkpoint_record.get("sha256") and isinstance(optimizer_component, dict) and isinstance(optimizer_record, dict) and optimizer_component.get("sha256") == optimizer_record.get("sha256") ) def _validated_offline_growth_plan_boundary( *, migration_plan_path: Path, migration_plan_sha256: str, target_plan_path: Path, expected_target_plan_sha256: str, migration_receipt_path: Path | None = None, ) -> tuple[Path, dict[str, Any]]: """Validate one exact migration plan or its bank-bound descendant.""" migration_path = migration_plan_path.expanduser().resolve() target_path = target_plan_path.expanduser().resolve() if ( not migration_path.is_file() or _file_sha256_boundary(migration_path) != migration_plan_sha256 or len(migration_plan_sha256) != 64 or not target_path.is_file() or _file_sha256_boundary(target_path) != expected_target_plan_sha256 ): raise RuntimeError("paged NoNE offline target growth authority differs") value = json.loads(target_path.read_text(encoding="utf-8")) if not isinstance(value, dict): raise RuntimeError("paged NoNE offline target growth plan is malformed") migration_value = json.loads( migration_path.read_text(encoding="utf-8") ) lineage = value.get("lineage") migration_lineage = ( migration_value.get("lineage") if isinstance(migration_value, dict) else None ) alignment = value.get("capacityAlignment") migration_alignment = ( migration_value.get("capacityAlignment") if isinstance(migration_value, dict) else None ) physical = value.get("physicalGraphPlan") target = value.get("proposedMinimumTargetGeometry") checks = value.get("checks") if not all( isinstance(record, dict) for record in ( migration_value, lineage, migration_lineage, alignment, migration_alignment, physical, target, checks, ) ): raise RuntimeError("paged NoNE offline target has no capacity lineage") assert isinstance(lineage, dict) assert isinstance(migration_lineage, dict) assert isinstance(alignment, dict) assert isinstance(migration_alignment, dict) assert isinstance(physical, dict) assert isinstance(target, dict) assert isinstance(checks, dict) summary_value = alignment.get("bankSummaryPath") summary_sha256 = alignment.get("bankSummarySha256") journal_value = alignment.get("bankJournalPath") journal_sha256 = alignment.get("bankJournalSha256") if ( not isinstance(summary_value, str) or not isinstance(summary_sha256, str) or len(summary_sha256) != 64 or not isinstance(journal_value, str) or not isinstance(journal_sha256, str) or len(journal_sha256) != 64 ): raise RuntimeError("paged NoNE compact-bank authority is malformed") summary_path = Path(summary_value).expanduser().resolve() journal_path = Path(journal_value).expanduser().resolve() inherited = alignment.get("inheritedPhysicalNoNELayerCount") additive = alignment.get("additiveBankPageCount") physical_count = alignment.get("alignedPhysicalNoNELayerCount") dense_layers = alignment.get( "residentTraversalLayerCount", alignment.get("alignedTargetScienceLayers"), ) lineage_source_value = lineage.get("sourcePlan") lineage_source_sha256 = lineage.get("sourcePlanSha256") migration_source_value = migration_lineage.get("sourcePlan") migration_source_sha256 = migration_lineage.get("sourcePlanSha256") source_path = Path(str(lineage_source_value)).expanduser().resolve() bank_identity_fields = ( "bankSummaryPath", "bankSummarySha256", "bankJournalPath", "bankJournalSha256", "bankSessionId", "bankPageCount", "bankPageParameterElements", "bankPhysicalParameterElements", ) exact_bank_authority = all( alignment.get(field) == migration_alignment.get(field) for field in bank_identity_fields ) admitted_bank_authority = ( False if exact_bank_authority else _admitted_capacity_subplan_boundary( migration_receipt_path=migration_receipt_path, migration_plan_path=migration_path, migration_plan_sha256=migration_plan_sha256, migration_alignment=migration_alignment, target_alignment=alignment, target_physical=physical, ) ) if ( value.get("schema") != "nnf.resynthesis.none_growth_plan.v2" or not isinstance(migration_value, dict) or migration_value.get("functionalExpertFamilyRoadmap") != value.get("functionalExpertFamilyRoadmap") or not isinstance(lineage_source_value, str) or not isinstance(lineage_source_sha256, str) or len(lineage_source_sha256) != 64 or lineage_source_value != migration_source_value or lineage_source_sha256 != migration_source_sha256 or not source_path.is_file() or _file_sha256_boundary(source_path) != lineage_source_sha256 or lineage.get("sourcePlanPreserved") is not True or lineage.get("modelTrainingClaimed") is not False or lineage.get("promotionEligibilityClaimed") is not False or alignment.get("schema") != "nnf.resynthesis.none_growth_plan_capacity_alignment.v1" or alignment.get("modelTrainingClaimed") is not False or alignment.get("promotionEligibilityClaimed") is not False or not (exact_bank_authority or admitted_bank_authority) or not summary_path.is_file() or _file_sha256_boundary(summary_path) != summary_sha256 or not journal_path.is_file() or _file_sha256_boundary(journal_path) != journal_sha256 or type(inherited) is not int or type(additive) is not int or type(physical_count) is not int or type(dense_layers) is not int or inherited < 1 or additive < 1 or physical_count != inherited + additive or value.get("initialLogicalExpertPageObjectives") != additive or target.get("scienceLayers") != dense_layers or target.get("residentScienceLayers") != dense_layers or target.get("physicalNoNELayers") != physical_count or target.get("physicalGraphLayerCount") != physical_count or target.get("physicalGraphNodeCount") != physical_count or physical.get("schema") != "nnf.resynthesis.physical_graph_plan.v1" or physical.get("inheritedPhysicalGraphLayerCount") != inherited or physical.get("additiveBankPageCount") != additive or physical.get("physicalGraphLayerCount") != physical_count or physical.get("physicalGraphNodeCount") != physical_count or physical.get("pageToPhysicalGraphLayerCardinality") != "one_to_one" or physical.get("pageToPhysicalGraphNodeCardinality") != "one_to_one" or physical.get("denseResidentScienceLayerCount") != dense_layers or physical.get("pageBackedLayersReportedAsDenseLayers") is not False or physical.get("physicalGraphLayerCountMaximum") is not None or physical.get("successorCompactBanksRemainAdditive") is not True or physical.get("trainingClaimed") is not False or physical.get("promotionEligible") is not False or checks.get("capacityLayerGeometryHasNoFixedMaximum") is not True ): raise RuntimeError("paged NoNE offline target capacity authority differs") return target_path, value def _paged_reasoning_layer_target_boundary( migration_receipt_path: Path, *, source_layers: int, source_experts: int, target_growth_plan_path: Path | None = None, expected_target_growth_plan_sha256: str | None = None, ) -> tuple[int, str]: """Resolve layer growth from the signed paged expansion receipt. The plan's large expert target describes out-of-core page families, not a request to duplicate every page into the resident dense seed. Only the signed reasoning-layer target changes resident graph geometry here. """ receipt = json.loads(migration_receipt_path.read_text(encoding="utf-8")) growth = receipt.get("growthPlan") if isinstance(receipt, dict) else None if not isinstance(growth, dict): return source_layers, "" plan_path_value = growth.get("path") plan_sha256 = growth.get("sha256") if ( not isinstance(plan_path_value, str) or not plan_path_value or not isinstance(plan_sha256, str) or len(plan_sha256) != 64 ): raise RuntimeError("paged NoNE reasoning growth plan binding is malformed") receipt_plan_path = Path(plan_path_value).expanduser().resolve() if ( not receipt_plan_path.is_file() or _file_sha256_boundary(receipt_plan_path) != plan_sha256 ): raise RuntimeError("paged NoNE reasoning growth plan identity differs") explicit_target_authority = target_growth_plan_path is not None if explicit_target_authority != ( expected_target_growth_plan_sha256 is not None ): raise RuntimeError( "paged NoNE target growth plan path and digest must be paired" ) if explicit_target_authority: assert target_growth_plan_path is not None assert expected_target_growth_plan_sha256 is not None plan_path, plan = _validated_offline_growth_plan_boundary( migration_plan_path=receipt_plan_path, migration_plan_sha256=plan_sha256, target_plan_path=target_growth_plan_path, expected_target_plan_sha256=( expected_target_growth_plan_sha256 ), migration_receipt_path=migration_receipt_path, ) plan_sha256 = expected_target_growth_plan_sha256 else: plan_path = receipt_plan_path plan = json.loads(plan_path.read_text(encoding="utf-8")) current = plan.get("currentSeedGeometry") if isinstance(plan, dict) else None target = ( plan.get("proposedMinimumTargetGeometry") if isinstance(plan, dict) else None ) paging = plan.get("pagingPolicy") if isinstance(plan, dict) else None graph_endstate_routing = ( paging.get("modelOwnedGraphEndstateRouting") if isinstance(paging, dict) else None ) if ( plan.get("schema") != "nnf.resynthesis.none_growth_plan.v2" or not isinstance(current, dict) or (target is not None and not isinstance(target, dict)) or not isinstance(paging, dict) or paging.get("acceptedGenerationPointer") is not True or paging.get("storageBoundaryMayReroute") is not False or ( graph_endstate_routing is not None and graph_endstate_routing != ( "family_to_cluster_to_page_to_expert — graph endstate-backward, " "never hierarchical intent ladders" ) ) ): raise RuntimeError("paged NoNE reasoning growth contract differs") current_layers = current.get("scienceLayers") seed_layers = current_layers current_experts = current.get("scienceExperts") target_layers = ( target.get("residentScienceLayers", target.get("scienceLayers")) if isinstance(target, dict) else current_layers ) target_page_experts = ( target.get("scienceExperts") if isinstance(target, dict) else current_experts ) capacity_alignment = plan.get("capacityAlignment") if isinstance(capacity_alignment, dict): baseline_target_layers = capacity_alignment.get( "baselineResidentTraversalLayerCount", capacity_alignment.get("baselineTargetScienceLayers"), ) aligned_target_layers = capacity_alignment.get( "residentTraversalLayerCount", capacity_alignment.get("alignedTargetScienceLayers"), ) if ( type(baseline_target_layers) is not int or type(aligned_target_layers) is not int or type(current_layers) is not int or current_layers > baseline_target_layers or baseline_target_layers > aligned_target_layers or aligned_target_layers != target_layers ): raise RuntimeError( "paged NoNE capacity-aligned target layer authority differs" ) # ``currentSeedGeometry`` is the historical dense seed. A retained # paged checkpoint may already have grown beyond it; the signed # capacity alignment records that exact accepted dense baseline. # Validate continuation against that baseline without counting sparse # page-backed graph layers as resident dense layers. current_layers = baseline_target_layers accepted_pointer = receipt.get("acceptedPointer") graph_authority = ( accepted_pointer.get("graphAuthority") if isinstance(accepted_pointer, dict) else None ) accepted_topology = ( graph_authority.get("topology") if isinstance(graph_authority, dict) else None ) admitted_layers = ( accepted_topology.get("layers") if isinstance(accepted_topology, dict) else None ) admitted_pages = ( accepted_topology.get("pages") if isinstance(accepted_topology, dict) else None ) source_layer_ids = growth.get("sourcePagedRuntimeLayerIds") added_layer_ids = growth.get("addedPagedRuntimeLayerIds") admitted_layer_ids = growth.get("targetPagedRuntimeLayerIds") accepted_runtime_authority = ( receipt.get("passed") is True and receipt.get("status") == "PHYSICAL_ADMISSION_ACCEPTED_TRAINING_PENDING" and isinstance(accepted_pointer, dict) and accepted_pointer.get("schema") == "nnf.resynthesis.none_page_accepted_pointer.v1" and isinstance(graph_authority, dict) and graph_authority.get("schema") == "nnf.resynthesis.none_graph_authority.v1" and Path(str(graph_authority.get("migrationReceiptPath", ""))) .expanduser() .resolve() == migration_receipt_path.expanduser().resolve() ) if accepted_pointer is not None and not accepted_runtime_authority: raise RuntimeError("paged NoNE accepted reasoning authority differs") if explicit_target_authority and isinstance(capacity_alignment, dict): inherited_physical_layers = capacity_alignment.get( "inheritedPhysicalNoNELayerCount" ) aligned_physical_layers = capacity_alignment.get( "alignedPhysicalPageExpertCount" ) expansion = receipt.get("expansion") if accepted_runtime_authority: if ( not isinstance(expansion, dict) or type(inherited_physical_layers) is not int or type(aligned_physical_layers) is not int or type(admitted_pages) is not int or expansion.get("sourcePageObjects") != inherited_physical_layers or expansion.get("targetPageObjects") != admitted_pages or type(expansion.get("addedPageObjects")) is not int or expansion["sourcePageObjects"] + expansion["addedPageObjects"] != admitted_pages or not inherited_physical_layers <= admitted_pages <= aligned_physical_layers ): raise RuntimeError( "paged NoNE accepted cumulative physical topology differs" ) elif ( type(admitted_pages) is not int or inherited_physical_layers != admitted_pages ): raise RuntimeError( "paged NoNE target inherited physical topology differs" ) if admitted_layers is not None: accepted_source_layers = ( len(source_layer_ids) if isinstance(source_layer_ids, list) else -1 ) accepted_target_layers = ( len(admitted_layer_ids) if isinstance(admitted_layer_ids, list) else -1 ) checkpoint_already_has_dense_target = ( accepted_runtime_authority and type(source_layers) is int and type(target_layers) is int and type(admitted_layers) is int and source_layers >= admitted_layers and ( source_layers == admitted_layers or ( explicit_target_authority and source_layers == target_layers ) ) ) if ( not accepted_runtime_authority or type(seed_layers) is not int or type(current_layers) is not int or type(target_layers) is not int or type(admitted_layers) is not int or accepted_source_layers < 1 or admitted_layers != accepted_target_layers or admitted_layers < accepted_source_layers or admitted_layers > target_layers or ( source_layers not in {seed_layers, accepted_source_layers} and not checkpoint_already_has_dense_target ) or source_layer_ids != list(range(accepted_source_layers)) or added_layer_ids != list(range(accepted_source_layers, admitted_layers)) or not isinstance(admitted_layer_ids, list) or admitted_layer_ids != source_layer_ids + added_layer_ids or ( isinstance(capacity_alignment, dict) and current_layers not in {accepted_source_layers, admitted_layers} ) ): raise RuntimeError( "paged NoNE admitted reasoning geometry differs: " + json.dumps( { "acceptedRuntimeAuthority": accepted_runtime_authority, "seedLayers": seed_layers, "currentLayers": current_layers, "targetLayers": target_layers, "sourceLayers": source_layers, "admittedLayers": admitted_layers, "acceptedSourceLayers": accepted_source_layers, "acceptedTargetLayers": accepted_target_layers, "sourceLayerIds": source_layer_ids, "addedLayerIds": added_layer_ids, "admittedLayerIds": admitted_layer_ids, "capacityAlignmentPresent": isinstance( capacity_alignment, dict, ), }, sort_keys=True, ) ) # The accepted graph is the active resident traversal authority. A # normal training load must not instantiate a pending dense target. # An explicitly signed offline adaptation is the boundary that grows # that accepted traversal engine, so it retains its validated target. current_layers = admitted_layers if not explicit_target_authority: target_layers = admitted_layers elif ( type(source_layers) is int and type(target_layers) is int and source_layers > target_layers ): # A minimum-growth plan without an accepted runtime authority cannot # shrink a checkpoint that has already traversed beyond that minimum. # Accepted paged authorities take the exact branch above and remain # bound to their graph topology. target_layers = source_layers validated_source_layers = ( current_layers if accepted_runtime_authority else source_layers ) if ( type(current_layers) is not int or type(target_layers) is not int or type(validated_source_layers) is not int or not current_layers <= validated_source_layers <= target_layers or type(current_experts) is not int or target_layers < source_layers or type(target_page_experts) is not int or source_experts not in {current_experts, target_page_experts} or target_page_experts < current_experts ): raise RuntimeError("paged NoNE reasoning growth geometry differs") return target_layers, plan_sha256 def _adapt_reasoning_layer_growth_state( state: dict[str, torch.Tensor], target_state: dict[str, torch.Tensor], *, source_layers: int, target_layers: int, ) -> tuple[ dict[str, torch.Tensor], bool, tuple[tuple[str, torch.Tensor], ...], tuple[tuple[int, int], ...], ]: """Transfer-seed added reasoning layers without copying paged runtimes.""" if source_layers < 1 or target_layers < source_layers: raise RuntimeError("reasoning-layer growth cannot shrink the source graph") adapted = dict(state) seeded: list[tuple[str, torch.Tensor]] = [] layer_sources: list[tuple[int, int]] = [] def seed(name: str, value: torch.Tensor) -> None: cloned = value.detach().to(device="cpu").clone() adapted[name] = cloned seeded.append((name, cloned)) execution_name = "science_stack.layer_execution_scale" execution_target = target_state.get(execution_name) if not isinstance(execution_target, torch.Tensor): raise RuntimeError("active graph has no reasoning-layer execution gate") source_execution = adapted.get(execution_name) if source_execution is None: execution = execution_target.detach().to(device="cpu").clone() execution[:source_layers].fill_(1.0) execution[source_layers:].zero_() seed(execution_name, execution) elif source_execution.shape != (source_layers,): raise RuntimeError("source reasoning-layer execution gate geometry differs") elif target_layers > source_layers: execution = execution_target.detach().to(device="cpu").clone() execution[:source_layers].copy_(source_execution) execution[source_layers:].zero_() seed(execution_name, execution) for name in sorted(_REASONING_LAYER_ROW_STATE_NAMES - {execution_name}): source = adapted.get(name) target = target_state.get(name) if source is None or target is None: continue if source.ndim < 1 or target.ndim != source.ndim: raise RuntimeError(f"reasoning-layer row geometry differs: {name}") source_stride = source.shape[0] // source_layers target_stride = target.shape[0] // target_layers if ( source.shape[0] % source_layers or target.shape[0] % target_layers or source_stride != target_stride or source.shape[1:] != target.shape[1:] ): raise RuntimeError(f"reasoning-layer row geometry differs: {name}") if source.shape == target.shape: continue expanded = target.detach().to(device="cpu").clone() expanded[: source.shape[0]].copy_(source) seed(name, expanded) for name in sorted(_REASONING_LAYER_SQUARE_STATE_NAMES): source = adapted.get(name) target = target_state.get(name) if source is None or target is None: continue if source.ndim != 2 or target.ndim != 2: raise RuntimeError(f"reasoning-layer square geometry differs: {name}") source_stride_0 = source.shape[0] // source_layers source_stride_1 = source.shape[1] // source_layers target_stride_0 = target.shape[0] // target_layers target_stride_1 = target.shape[1] // target_layers if ( source.shape[0] % source_layers or source.shape[1] % source_layers or target.shape[0] % target_layers or target.shape[1] % target_layers or source_stride_0 != target_stride_0 or source_stride_1 != target_stride_1 ): raise RuntimeError(f"reasoning-layer square geometry differs: {name}") if source.shape == target.shape: continue expanded = target.detach().to(device="cpu").clone() expanded[: source.shape[0], : source.shape[1]].copy_(source) seed(name, expanded) pathway_seed_name = ( "science_stack.causal_algebra_world_graph.pathway_seed.weight" ) source_pathway_seed = adapted.get(pathway_seed_name) target_pathway_seed = target_state.get(pathway_seed_name) if isinstance(source_pathway_seed, torch.Tensor) and isinstance( target_pathway_seed, torch.Tensor, ) and source_pathway_seed.shape != target_pathway_seed.shape: # Causal pathway width grows with the accepted layer/expert topology. # Preserve the learned source columns exactly and retain only the # target generation's initialized appended pathways. The target width # is an accepted geometry, never a fixed model-capacity maximum. if ( source_pathway_seed.ndim != 2 or target_pathway_seed.ndim != 2 or source_pathway_seed.shape[0] != target_pathway_seed.shape[0] or not 0 < source_pathway_seed.shape[1] < target_pathway_seed.shape[1] ): raise RuntimeError( "causal pathway seed cannot preserve inherited geometry" ) expanded_pathway_seed = ( target_pathway_seed.detach().to(device="cpu").clone() ) expanded_pathway_seed[:, : source_pathway_seed.shape[1]].copy_( source_pathway_seed.detach().to(device="cpu") ) seed(pathway_seed_name, expanded_pathway_seed) transfer_graph = target_state.get("science_stack.layer_transfer_graph") if not isinstance(transfer_graph, torch.Tensor): raise RuntimeError("active graph has no reasoning-layer transfer matrix") for target_layer in range(source_layers, target_layers): source_layer = int( transfer_graph[:source_layers, target_layer] .detach() .abs() .argmax() .to(device="cpu", dtype=torch.long) ) layer_sources.append((target_layer, source_layer)) target_prefix = f"science_stack.science_layer_{target_layer}." source_prefix = f"science_stack.science_layer_{source_layer}." for target_name, target_value in sorted(target_state.items()): if not target_name.startswith(target_prefix): continue suffix = target_name[len(target_prefix) :] if suffix.startswith("paged_expert_runtime."): raise RuntimeError( "added reasoning layer unexpectedly owns a copied page runtime" ) if target_name.endswith(ANTI_THOMPSON_FAIL_COUNTS_BUFFER_SUFFIX): # Outcome memory belongs to the layer that observed it. A new # layer inherits trainable structure, but starts with no # failures of its own; the versioned adapter seeds this buffer # at zero before layer transfer. continue source_name = source_prefix + suffix source_value = adapted.get(source_name) if source_value is None: continue if source_value.shape != target_value.shape: if suffix in { "expert_capability_proj.weight", "expert_capability_proj.bias", }: # Preserve the exact source-family rows on newly added # reasoning layers. The capability-growth pass below then # expands every layer through the same validated prefix # transaction, rather than initializing new layers from a # different family geometry. seed(target_name, source_value) continue raise RuntimeError( f"reasoning-layer transfer tensor geometry differs: {target_name}" ) seed(target_name, source_value) return adapted, bool(seeded), tuple(seeded), tuple(layer_sources) STOP_REASON_NAMES: tuple[str, ...] = ( "NONE", "REASONING_READY", "CONTRADICTION_RESOLVED", "PROOF_VERIFIED", "SELF_CORRECTION_PLATEAU", "CONFIDENCE_GATE", "", "", ) # RBO phase names (tensor-native: encoded by index). RBO_PHASE_NAMES: tuple[str, ...] = ( "retrieve", "route", "experts", "blend", "feedback", "feedback_update", "traversal_mutation", "reinforce", "stop_gate", "response_shape", ) PARENT_ACQUISITION_ACTION_NAMES: tuple[str, ...] = ( "retry_observation", "build_tool", "build_environment", "verified_complete", ) def _additive_completion_probability(stop_scores: torch.Tensor) -> torch.Tensor: """Conjoin trained readiness and submission-verified task confidence.""" if stop_scores.ndim < 1 or stop_scores.shape[-1] != 3: raise ValueError("additive stop-score geometry must end in [3]") completion_readiness = stop_scores[..., :2].max(dim=-1).values task_correctness_confidence = stop_scores[..., 2:].mean(dim=-1) return torch.minimum(completion_readiness, task_correctness_confidence) def _additive_completion_decision(stop_scores: torch.Tensor) -> torch.Tensor: """Return the tensor-owned conjunction decision for additive completion.""" return _additive_completion_probability(stop_scores).gt(0.5) LONG_CONTEXT_HIDDEN_CHUNK_TOKENS = RESYNTHESIS_ONLINE_SOFTMAX_TILE_TOKENS NATIVE_CONTEXT_POSITION_APERTURE = NATIVE_ATTENTION_POSITION_APERTURE def _hidden_sequence_context( hidden: torch.Tensor, context_intent: torch.Tensor | None = None, *, context_action: torch.Tensor | None = None, intent_additive_gate: torch.Tensor | float = 1.0, action_additive_gate: torch.Tensor | float = 1.0, intent_multiplicative_gate: torch.Tensor | float = 0.0, action_multiplicative_gate: torch.Tensor | float = 0.0, ) -> torch.Tensor: """Exact last-token attention over every sequence position. STACK+COMPOSE additive long pool: tiled online-softmax composition keeps multi-million-token science-stack trajectories fully addressable. Parent decode already folded Dual Chunk → parent RoPE; this pool composes over absolute hidden order without replacing parent positional authority. Chunking is execution only and never omits keys. ``RESYNTHESIS_ONLINE_SOFTMAX_TILE_TOKENS`` aligns with ``DUAL_CHUNK_LOCAL_SIZE`` and native prefill tiles so successive seam windows, parent RoPE localization, and this pool stay coherent. Optional context-intent and context-action channels (``[batch, sequence]`` or ``[batch, sequence, hidden]``) compose scores as ``Q·K + gate_i*(Q·C_i) + gate_a*(Q·A)`` and optionally open multiplicative pivots on intent and action. When both channels are absent the pool is identical to the baseline ``Q·K`` path (identity). """ return online_softmax_last_token_pool( hidden, context_intent=context_intent, context_action=context_action, chunk_tokens=LONG_CONTEXT_HIDDEN_CHUNK_TOKENS, intent_additive_gate=intent_additive_gate, action_additive_gate=action_additive_gate, intent_multiplicative_gate=intent_multiplicative_gate, action_multiplicative_gate=action_multiplicative_gate, ) @dataclass class ResynthesisRBOConfig: """Configuration for the Resynthesis RBO outer loop — UNCAPPED. NO CAPS. All traversal and generation termination is confidence-based and trained. There are no max/min step fields. The gates start NEUTRAL (sigmoid(0)=0.5) and TRAIN purely from execution-grounded outcomes. """ reinforce_lr: float = 0.01 feedback_hidden_size: int = 256 enable_outcome_feedback: bool = True enable_outcome_learning: bool = True contractive_loss_weight: float = 0.5 contractive_contradiction_floor: float = 0.3 nla_round_trip_loss_weight: float = 0.01 feedback_head_lr: float = 1e-3 mhc_stability_floor: float = 0.95 outcome_ledger_path: str = ".nnf-resynthesis/rbo_outcomes.jsonl" rbo_state_path: str = ".nnf-resynthesis/rbo_state.pt" candidate_state_path: str | None = None candidate_migration_receipt_path: str | None = None paged_none_composition_path: str | None = None paged_none_migration_receipt_path: str | None = None paged_none_replica_receipt_path: str | None = None paged_none_target_growth_plan_path: str | None = None paged_none_target_growth_plan_sha256: str | None = None paged_none_adaptation_source_checkpoint_sha256: str | None = None paged_none_training_branch_scope_path: str | None = None paged_none_training_branch_store_root: str | None = None training_branch_functional_owner_index: int | None = None training_branch_functional_owner_count: int | None = None outcome_memory_path: str = ".nnf-resynthesis/rbo_outcome_memory.json" # NO relationship_plan_beam_width / training_frontier config fields. # Once the relationship DAG has signal, the full trained graph frontier is kept. @dataclass class RBOStopDecision: """Tensor-native stop decision from the confidence gates.""" stop: torch.Tensor reason: torch.Tensor @dataclass(frozen=True) class NeuralStopGateOutput: """Tensor-only confidence surfaces for one reasoning trajectory.""" utility: torch.Tensor contradiction: torch.Tensor @dataclass(frozen=True) class NLAActivationPacket: """Tensor-native, target-free activation round-trip diagnostics. The stop trajectory projection is reused as the activation verbalizer and its transpose as the reconstructor. The packet never selects, replaces, scores, or vetoes the visible native answer surface. """ latent: torch.Tensor latent_code_ids: torch.Tensor reconstructed: torch.Tensor round_trip_mse: torch.Tensor sequence_context_positions: torch.Tensor intent_context: torch.Tensor action_context: torch.Tensor completion_context: torch.Tensor stop_context_scale: torch.Tensor confidence_context_scale: torch.Tensor @dataclass(frozen=True) class TaskIntentPacket: """Target-free five-axis task interpretation owned by science/NLA state.""" logits: torch.Tensor probabilities: torch.Tensor dominant_index: torch.Tensor @dataclass(frozen=True) class PrefillParticipationPacket: """Tensor proof that a new prompt traversed attention, Fabric, and NoNE.""" active: torch.Tensor input_positions: torch.Tensor summary_positions: torch.Tensor fabric_phase_count: torch.Tensor expert_routes: torch.Tensor layer_routes: torch.Tensor def _select_prefill_route_batch_row_boundary( routes: torch.Tensor, *, batch_size: int, batch_index: int, ) -> torch.Tensor: """Select one external batch row from prefill route evidence. Inactive prefill packets historically carried a rank-one empty tensor while active packets carry ``[route, batch, ...]`` evidence. Normalize that legacy empty representation at this observation boundary and otherwise preserve every trailing route axis while selecting the exact batch row. """ if routes.ndim == 1: if routes.shape[0] != 0: raise ValueError("rank-one prefill route evidence must be empty") return routes.reshape(0, 1) if routes.ndim < 2 or routes.shape[1] != batch_size: raise ValueError("prefill route evidence must be [route, batch, ...]") return routes[:, batch_index : batch_index + 1] def _select_prefill_positions_batch_row_boundary( positions: torch.Tensor, *, batch_size: int, batch_index: int, ) -> torch.Tensor: """Select row-wise prefill positions while retaining scalar compatibility.""" if positions.ndim == 0: return positions if positions.shape != (batch_size,): raise ValueError("prefill input positions must be scalar or [batch]") return positions[batch_index : batch_index + 1] def _select_capability_output_batch_row_boundary( capability_output: CausalIntegrationOutput | None, *, batch_index: int, ) -> CausalIntegrationOutput | None: """Row-slice the capability-integration packet at an external I/O boundary. Mirrors ``CausalTheoryProofPacket.select_batch_row_boundary``: every tensor field carries a leading batch axis and is sliced to ``[batch_index]`` while preserving the packet's geometry. ``None`` passes through unchanged so the pre-capability code path is untouched. """ if capability_output is None: return None row = slice(batch_index, batch_index + 1) intent_composite = capability_output.intent_composite return replace( capability_output, shaped_action_logits=capability_output.shaped_action_logits[row], value=capability_output.value[row], intent_composite=( None if intent_composite is None else intent_composite[row] ), calibrated_confidence=capability_output.calibrated_confidence[row], exploration_bonus=capability_output.exploration_bonus[row], surprise=capability_output.surprise[row], ) @dataclass(frozen=True) class RBOCorrectionState: """Caller-owned tensor state connecting real outcomes to the next attempt.""" prior_hidden: torch.Tensor outcome_features: torch.Tensor parent_outcome_features: torch.Tensor acquisition_action_probs: torch.Tensor acquisition_action_index: torch.Tensor acquisition_authority: torch.Tensor outcome_present: torch.Tensor attempt_index: torch.Tensor traversal_state: ScienceTraversalState causal_world_state: CausalWorldState | None = None def select_batch_row_boundary(self, batch_index: int) -> RBOCorrectionState: """Return one caller session at an explicit external batch boundary.""" batch_size = self.prior_hidden.shape[0] if batch_index < 0 or batch_index >= batch_size: raise IndexError("correction-state batch index is out of range") row = slice(batch_index, batch_index + 1) traversal = self.traversal_state return RBOCorrectionState( prior_hidden=self.prior_hidden[row], outcome_features=self.outcome_features[row], parent_outcome_features=self.parent_outcome_features[row], acquisition_action_probs=self.acquisition_action_probs[row], acquisition_action_index=self.acquisition_action_index[row], acquisition_authority=self.acquisition_authority[row], outcome_present=self.outcome_present[row], attempt_index=self.attempt_index[row], traversal_state=ScienceTraversalState( expert_visits=traversal.expert_visits[row], expert_selections=traversal.expert_selections[row], layer_visits=traversal.layer_visits[row], traversal_index=traversal.traversal_index[row], ), causal_world_state=( None if self.causal_world_state is None else self.causal_world_state.select_batch_row_boundary(batch_index) ), ) @dataclass(frozen=True) class RBOCorrectionPacket: """Model-owned correction/delegation decision produced from attempt one.""" hidden_residual: torch.Tensor trigger: torch.Tensor task_confidence: torch.Tensor delegation_pressure: torch.Tensor expert_bias: torch.Tensor layer_bias: torch.Tensor parent_outcome_features: torch.Tensor acquisition_action_probs: torch.Tensor acquisition_action_index: torch.Tensor acquisition_authority: torch.Tensor @dataclass(frozen=True) class RBOBilevelIntervention: """Tensor-only outer-observation binding into trained pathway heads.""" observation: torch.Tensor active: torch.Tensor trigger: torch.Tensor expert_bias: torch.Tensor layer_bias: torch.Tensor def boundary_receipt(self) -> dict[str, Any]: """Serialize proof only at the explicit external receipt boundary.""" return { "active": bool(self.active.detach().to(device="cpu", dtype=torch.bool)), "trigger": float(self.trigger.detach().float().cpu()), "expertBiasL2": float(self.expert_bias.detach().float().norm().cpu()), "layerBiasL2": float(self.layer_bias.detach().float().norm().cpu()), "observation": self.observation.detach().float().cpu().tolist(), "observationTargetFree": True, "pathwayOwner": "trained_resynthesis_rbo_correction_heads", "checkpointGeometryChanged": False, } @dataclass(frozen=True) class RBOAcquisitionRequest: """Model-owned request consumed only by an external evidence tool boundary.""" execute: torch.Tensor action_probs: torch.Tensor action_index: torch.Tensor confidence: torch.Tensor authority: torch.Tensor def boundary_receipt(self) -> dict[str, Any]: """Serialize trained action evidence at the explicit I/O boundary.""" index = int(self.action_index.detach().to(device="cpu", dtype=torch.long)) return { "execute": bool(self.execute.detach().to(device="cpu", dtype=torch.bool)), "actionIndex": index, "actionName": ( PARENT_ACQUISITION_ACTION_NAMES[index] if 0 <= index < len(PARENT_ACQUISITION_ACTION_NAMES) else "unknown" ), "confidence": float(self.confidence.detach().float().cpu()), "authority": bool( self.authority.detach().to(device="cpu", dtype=torch.bool) ), "actionProbabilities": self.action_probs.detach().float().cpu().tolist(), } @dataclass(frozen=True) class RBOCorrectionArmDecision: """Tensor-owned decision to continue or close the outer correction arm. This is deliberately separate from answer correctness. A persisted verifier outcome activates the learned acquisition policy, while the model's correction trigger decides whether another arm is useful. The host may observe this packet but cannot invent a retry or impose a numeric attempt limit. """ continue_arm: torch.Tensor terminal_arm: torch.Tensor correction_trigger: torch.Tensor action_index: torch.Tensor authority: torch.Tensor environment_required: torch.Tensor def boundary_receipt(self) -> dict[str, Any]: """Serialize the trained transition at the external grading boundary.""" action_index = int( self.action_index.detach().to(device="cpu", dtype=torch.long) ) return { "schema": "nnf.resynthesis.correction_arm_decision.v1", "modelOwned": True, "targetFree": True, "hostAttemptCap": None, "continueCorrectionArm": bool( self.continue_arm.detach().to(device="cpu", dtype=torch.bool) ), "terminalCorrectionArm": bool( self.terminal_arm.detach().to(device="cpu", dtype=torch.bool) ), "correctionTrigger": float( self.correction_trigger.detach().float().cpu() ), "actionIndex": action_index, "actionName": ( PARENT_ACQUISITION_ACTION_NAMES[action_index] if 0 <= action_index < len(PARENT_ACQUISITION_ACTION_NAMES) else "unknown" ), "authority": bool( self.authority.detach().to(device="cpu", dtype=torch.bool) ), "environmentRequired": bool( self.environment_required.detach().to( device="cpu", dtype=torch.bool, ) ), } @dataclass(frozen=True) class LearnedAcquisitionPolicyOutput: """Tensor-only action packet owned by the trainable Resynthesis graph.""" action_probs: torch.Tensor action_index: torch.Tensor confidence: torch.Tensor authority: torch.Tensor class LearnedEvidenceAcquisitionPolicy(nn.Module): """Choose the next evidence/tool action from persisted outcome state. The historical parent has trained execution memory but predates its later four-way acquisition head. Resynthesis therefore owns and trains this missing policy additively. It consumes observations only—never targets or gold evidence—and remains inactive in evaluation until an actual gradient update and held-out retention candidate have both been recorded. """ authority_trained: torch.Tensor retention_passed: torch.Tensor candidate_evaluation_active: torch.Tensor candidate_update_count: torch.Tensor def __init__(self, hidden_size: int = 64) -> None: super().__init__() self.input_norm = nn.LayerNorm(13) self.context = nn.Linear(13, hidden_size) self.action_head = nn.Linear(hidden_size, 4) nn.init.zeros_(self.action_head.weight) nn.init.zeros_(self.action_head.bias) self.register_buffer("authority_trained", torch.zeros((), dtype=torch.bool)) self.register_buffer("retention_passed", torch.zeros((), dtype=torch.bool)) self.register_buffer( "candidate_evaluation_active", torch.zeros((), dtype=torch.bool), persistent=False, ) self.register_buffer( "candidate_update_count", torch.zeros((), dtype=torch.long), persistent=True, ) def forward( self, outcome_features: torch.Tensor, parent_outcome_features: torch.Tensor, ) -> LearnedAcquisitionPolicyOutput: if outcome_features.ndim != 2 or outcome_features.shape[-1] != 8: raise ValueError("acquisition outcome geometry must be [batch, 8]") if parent_outcome_features.ndim != 2 or ( parent_outcome_features.shape[-1] != 5 ): raise ValueError("acquisition parent-outcome geometry must be [batch, 5]") context = torch.cat((outcome_features, parent_outcome_features), dim=-1) logits = self.action_head(F.silu(self.context(self.input_norm(context)))) probs = logits.softmax(dim=-1) action_index = probs.argmax(dim=-1, keepdim=True) authority = ( self.authority_trained & (self.retention_passed | self.candidate_evaluation_active) ).reshape(1, 1) authority = authority.expand(outcome_features.shape[0], 1) return LearnedAcquisitionPolicyOutput( action_probs=probs, action_index=action_index, confidence=probs.max(dim=-1, keepdim=True).values, authority=authority, ) def activate_updated_candidate_from_boundary(self) -> torch.Tensor: """Activate only after the optimizer produced finite policy gradients.""" gradient_evidence = self.authority_trained.new_zeros(()) for parameter in self.parameters(): gradient = parameter.grad if gradient is not None: finite_nonzero = torch.isfinite(gradient).all() & gradient.ne(0).any() gradient_evidence = gradient_evidence | finite_nonzero.to( device=gradient_evidence.device, dtype=torch.bool, ) with torch.no_grad(): self.authority_trained.logical_or_(gradient_evidence) self.retention_passed.copy_( torch.where( gradient_evidence, self.retention_passed.new_zeros(()), self.retention_passed, ) ) self.candidate_evaluation_active.logical_or_(gradient_evidence) self.candidate_update_count.add_(gradient_evidence.to(dtype=torch.long)) return gradient_evidence def begin_candidate_update_window_from_boundary(self) -> torch.Tensor: """Start one validation-bound proposal without discarding retained authority.""" with torch.no_grad(): self.candidate_evaluation_active.zero_() return self.candidate_evaluation_active.clone() def retain_candidate_from_boundary(self, accepted: torch.Tensor) -> torch.Tensor: """Commit authority only after the candidate produced no validation regression.""" if accepted.numel() != 1: raise RuntimeError("acquisition retention receipt must be scalar") retained = self.authority_trained & accepted.to( device=self.authority_trained.device, dtype=torch.bool, ).reshape(()) with torch.no_grad(): self.retention_passed.copy_(retained) self.candidate_evaluation_active.zero_() return retained @dataclass(frozen=True) class NoNEModelScalePacket: """Tensor-only physical, trainable, resident, route, and lineage scale.""" physical_total_parameter_elements_t: torch.Tensor currently_trainable_parameter_elements_t: torch.Tensor resident_parameter_elements_t: torch.Tensor parent_parameter_elements_t: torch.Tensor additive_parameter_elements_t: torch.Tensor additive_trainable_parameter_elements_t: torch.Tensor page_parameter_elements_t: torch.Tensor external_page_parameter_elements_t: torch.Tensor resident_page_parameter_elements_t: torch.Tensor trainable_page_parameter_elements_t: torch.Tensor physical_page_expert_count_t: torch.Tensor validated_trained_page_expert_count_t: torch.Tensor unvalidated_page_expert_count_t: torch.Tensor expert_family_count_t: torch.Tensor active_routed_page_ids_t: torch.Tensor resident_page_ids_t: torch.Tensor active_trainable_page_ids_t: torch.Tensor parent_layer_count_t: torch.Tensor science_layer_count_t: torch.Tensor physical_layer_count_t: torch.Tensor accepted_generation_t: torch.Tensor parent_artifact_sha256_t: torch.Tensor page_composition_sha256_t: torch.Tensor @dataclass(frozen=True) class RBOResult: """Result of an RBO forward pass — tensor-native boundary contract.""" shaped_hidden: torch.Tensor shaped_logits: torch.Tensor # Legacy field name retained for checkpoint/API compatibility. This is the # frozen vocabulary projection of additive shaped hidden, not parent logits. completion_baseline_logits: torch.Tensor # Parent tensors below are detached feature/provenance diagnostics only. # They have no answer, loss, stop, retention, or veto authority. baseline_hidden: torch.Tensor baseline_logits: torch.Tensor parent_context_hidden: torch.Tensor steps: torch.Tensor stop_reason: torch.Tensor glyph_packet: torch.Tensor native_token_ids: torch.Tensor native_bit_ids: torch.Tensor expert_routes: torch.Tensor layer_routes: torch.Tensor parent_expert_routes: torch.Tensor parent_layer_routes: torch.Tensor parent_kv_prefix_positions: torch.Tensor parent_kv_new_positions: torch.Tensor science_active_positions: torch.Tensor fabric_phase_count: torch.Tensor parent_fabric_connected: torch.Tensor parent_route_conditioning: torch.Tensor attempt_weights: torch.Tensor stop_scores: torch.Tensor stop_probability: torch.Tensor stop_decision: torch.Tensor decode_stop_authority: torch.Tensor parent_native_stop_probability: torch.Tensor parent_native_stop_decision: torch.Tensor generation_trajectory_initialized: torch.Tensor generation_native_progress: torch.Tensor generation_task_progress: torch.Tensor generation_arm_exhausted: torch.Tensor generation_delegation_exit: torch.Tensor task_intent: TaskIntentPacket prefill: PrefillParticipationPacket nla: NLAActivationPacket drafting: NoNEDraftingLifecyclePacket bilevel: RBOBilevelIntervention correction: RBOCorrectionPacket next_state: RBOCorrectionState causal_proof: CausalTheoryProofPacket | None = None # Capability-integration signals from the science stack's # ``CausalIntegrationTensor`` (shaped action logits, value, calibrated # confidence, exploration bonus). Populated by ``forward_thinking`` when # the science stack produced one this step; ``None`` preserves the prior # (pre-capability) behaviour exactly. Consumed for stop-score shaping and # correction-routing bias — never serialized into the boundary receipt. capability_output: CausalIntegrationOutput | None = None def select_batch_row_boundary(self, batch_index: int) -> RBOResult: """Return one model result for external grading and receipt emission.""" batch_size = self.shaped_hidden.shape[0] if batch_index < 0 or batch_index >= batch_size: raise IndexError("RBO result batch index is out of range") row = slice(batch_index, batch_index + 1) return replace( self, shaped_hidden=self.shaped_hidden[row], shaped_logits=self.shaped_logits[row], completion_baseline_logits=self.completion_baseline_logits[row], baseline_hidden=self.baseline_hidden[row], baseline_logits=self.baseline_logits[row], parent_context_hidden=self.parent_context_hidden[row], stop_reason=self.stop_reason[row], glyph_packet=self.glyph_packet[row], native_token_ids=self.native_token_ids[row], native_bit_ids=self.native_bit_ids[row], expert_routes=self.expert_routes[:, :, row], layer_routes=self.layer_routes[:, :, row], parent_expert_routes=self.parent_expert_routes[:, row], parent_layer_routes=self.parent_layer_routes[:, row], attempt_weights=self.attempt_weights[row], stop_scores=self.stop_scores[row], stop_probability=self.stop_probability[row], stop_decision=self.stop_decision[row], decode_stop_authority=self.decode_stop_authority[row], parent_native_stop_probability=self.parent_native_stop_probability[row], parent_native_stop_decision=self.parent_native_stop_decision[row], generation_trajectory_initialized=( self.generation_trajectory_initialized[row] ), generation_native_progress=self.generation_native_progress[row], generation_task_progress=self.generation_task_progress[row], generation_arm_exhausted=self.generation_arm_exhausted[row], generation_delegation_exit=self.generation_delegation_exit[row], task_intent=replace( self.task_intent, logits=self.task_intent.logits[row], probabilities=self.task_intent.probabilities[row], dominant_index=self.task_intent.dominant_index[row], ), prefill=replace( self.prefill, input_positions=( _select_prefill_positions_batch_row_boundary( self.prefill.input_positions, batch_size=batch_size, batch_index=batch_index, ) ), expert_routes=_select_prefill_route_batch_row_boundary( self.prefill.expert_routes, batch_size=batch_size, batch_index=batch_index, ), layer_routes=_select_prefill_route_batch_row_boundary( self.prefill.layer_routes, batch_size=batch_size, batch_index=batch_index, ), ), nla=replace( self.nla, latent=self.nla.latent[row], latent_code_ids=self.nla.latent_code_ids[row], reconstructed=self.nla.reconstructed[row], intent_context=self.nla.intent_context[row], action_context=self.nla.action_context[row], completion_context=self.nla.completion_context[row], ), drafting=replace( self.drafting, lifecycle_scores=self.drafting.lifecycle_scores[row], worker_assignments=self.drafting.worker_assignments[row], reproduction_states=self.drafting.reproduction_states[row], triage_weights=self.drafting.triage_weights[row], worker_draft_states=self.drafting.worker_draft_states[row], experiment_plan_state=self.drafting.experiment_plan_state[row], peer_review_routes=self.drafting.peer_review_routes[row], critique_state=self.drafting.critique_state[row], revised_draft_state=self.drafting.revised_draft_state[row], disclosure_state=self.drafting.disclosure_state[row], patch_state=self.drafting.patch_state[row], verification_state=self.drafting.verification_state[row], revision_delta=self.drafting.revision_delta[row], draft_proposal_gate=self.drafting.draft_proposal_gate[row], draft_proposal_delta=self.drafting.draft_proposal_delta[row], experiment_requirement=self.drafting.experiment_requirement[row], experiment_observed=self.drafting.experiment_observed[row], experiment_passed=self.drafting.experiment_passed[row], revision_gate=self.drafting.revision_gate[row], verified_revision_delta=self.drafting.verified_revision_delta[row], submission_proposal=self.drafting.submission_proposal[row], submission_readiness=self.drafting.submission_readiness[row], ), correction=replace( self.correction, hidden_residual=self.correction.hidden_residual[row], trigger=self.correction.trigger[row], task_confidence=self.correction.task_confidence[row], delegation_pressure=self.correction.delegation_pressure[row], expert_bias=self.correction.expert_bias[row], layer_bias=self.correction.layer_bias[row], parent_outcome_features=self.correction.parent_outcome_features[row], acquisition_action_probs=self.correction.acquisition_action_probs[row], acquisition_action_index=self.correction.acquisition_action_index[row], acquisition_authority=self.correction.acquisition_authority[row], ), next_state=self.next_state.select_batch_row_boundary(batch_index), causal_proof=( None if self.causal_proof is None else self.causal_proof.select_batch_row_boundary(batch_index) ), capability_output=_select_capability_output_batch_row_boundary( self.capability_output, batch_index=batch_index, ), ) def boundary_receipt(self) -> dict[str, Any]: """Serialize proof metadata only at an explicit external I/O boundary.""" reason = int(self.stop_reason.detach().to(device="cpu", dtype=torch.long)) answer_completion_verified = bool( self.stop_decision.detach().to(device="cpu", dtype=torch.bool) ) generation_arm_exhausted = bool( self.generation_arm_exhausted.detach().to( device="cpu", dtype=torch.bool, ) ) generation_arm_exhaustion_exit = bool( reason == STOP_REASON_SELF_CORRECTION_PLATEAU and generation_arm_exhausted and not answer_completion_verified ) corrected_attempt = bool( self.next_state.attempt_index.detach() .ge(1) .all() .to(device="cpu", dtype=torch.bool) ) stop_authority = int( self.decode_stop_authority.detach().to(device="cpu", dtype=torch.long) ) expert_selection_coverage = ( self.next_state.traversal_state.expert_selections.detach().to( device="cpu", dtype=torch.bool ) ) expert_selection_count = int( expert_selection_coverage.sum().to(dtype=torch.long) ) expert_selection_total = expert_selection_coverage.numel() expert_selection_events = int( self.next_state.traversal_state.expert_selections.detach() .to(device="cpu", dtype=torch.long) .sum() ) context_action_l1 = float( self.correction.acquisition_action_probs.detach().float().abs().sum().cpu() ) context_action_applied = bool( bool( self.next_state.outcome_present.detach() .to(device="cpu", dtype=torch.bool) .all() ) and context_action_l1 > 0.0 ) causal_proof = self.causal_proof causal_receipt: dict[str, Any] | None = None if causal_proof is not None: causal_receipt = { "schema": "nnf.resynthesis.causal_algebra.proof_packet.v1", "modelOwned": True, "targetFree": True, "proofValidity": float( causal_proof.proof_validity_t.detach().float().mean().cpu() ), "continuationProbability": float( causal_proof.continuation_probability_t.detach() .float() .mean() .cpu() ), "proofObligationMean": float( causal_proof.proof_obligation_t.detach().float().mean().cpu() ), "falsifyingExperimentIndex": int( causal_proof.falsifying_experiment.experiment_index_t.detach() .to(device="cpu", dtype=torch.long) .reshape(-1)[0] ), # Receipt-safe diagnostics expose only scalar decisions. Raw # workspace/world tensors and hidden reasoning remain inside # the model-owned session state. "counterThompsonProbability": float( causal_proof.exploration.counter_thompson_probability_t.detach() .float() .mean() .cpu() ), "explorationCounterweight": float( causal_proof.exploration.exploration_counterweight_t.detach() .float() .mean() .cpu() ), "hillClimbGain": float( causal_proof.exploration.hill_climb_gain_t.detach() .float() .mean() .cpu() ), "hillClimbAccepted": bool( causal_proof.exploration.hill_climb_accept_t.detach() .to(device="cpu", dtype=torch.bool) .all() ), "informationValue": float( causal_proof.exploration.information_value_t.detach() .float() .mean() .cpu() ), "successorReplayError": float( causal_proof.exploration.successor_replay_error_t.detach() .float() .mean() .cpu() ), "operatorCommutatorError": float( causal_proof.exploration.operator_commutator_error_t.detach() .float() .mean() .cpu() ), "domainCycleError": float( causal_proof.exploration.domain_cycle_error_t.detach() .float() .mean() .cpu() ), "workingMemoryL2": float( causal_proof.exploration.working_memory_t.detach() .float() .square() .sum(dim=-1) .sqrt() .mean() .cpu() ), "workingMemoryDeltaL2": float( causal_proof.exploration.working_memory_delta_l2_t.detach() .float() .mean() .cpu() ), "workingMemoryStateTracked": bool( torch.isfinite( causal_proof.exploration.working_memory_t.detach() ) .all() .to(device="cpu", dtype=torch.bool) ), "sourceDomainIndex": int( causal_proof.source_domain_probability_t.detach() .float() .mean(dim=0) .argmax() .to(device="cpu", dtype=torch.long) ), "targetDomainIndex": int( causal_proof.target_domain_probability_t.detach() .float() .mean(dim=0) .argmax() .to(device="cpu", dtype=torch.long) ), "actionIndex": int( causal_proof.exploration.action_index_t.detach() .reshape(-1)[0] .to(device="cpu", dtype=torch.long) ), "knowledgeOwnership": float( causal_proof.knowledge_ownership_t.detach() .float() .mean() .cpu() ), "knowledgeDisagreement": float( causal_proof.knowledge_disagreement_t.detach() .float() .mean() .cpu() ), "domainActionRouteModelOwned": True, "domainActionRouteTargetFree": True, "domainActionRouteHasRoutingAuthority": False, "promotionAuthority": bool( causal_proof.promotion_authority_t.detach() .to(device="cpu", dtype=torch.bool) .all() ), } return { "steps": int(self.steps.detach().to(device="cpu", dtype=torch.long)), "stopReason": reason, "stopReasonName": STOP_REASON_NAMES[reason] if reason < len(STOP_REASON_NAMES) else "UNKNOWN", "uncappedPolicy": "intentional", "correctionPolicy": "model_owned_uncapped_recurrent_v1", "hostCorrectionAttemptCap": None, "causalAlgebra": causal_receipt, "draftingLifecycle": { "phaseNames": DRAFTING_LIFECYCLE_NAMES, "phaseScores": ( self.drafting.lifecycle_scores.detach().float().cpu().tolist() ), "parallelWorkerCount": self.drafting.worker_draft_states.shape[1], "workerAssignments": ( self.drafting.worker_assignments.detach().float().cpu().tolist() ), "triageWeights": ( self.drafting.triage_weights.detach().float().cpu().tolist() ), "peerReviewRoutes": ( self.drafting.peer_review_routes.detach().float().cpu().tolist() ), "reproductionStateL2": float( self.drafting.reproduction_states.detach().float().norm().cpu() ), "experimentPlanStateL2": float( self.drafting.experiment_plan_state.detach().float().norm().cpu() ), "critiqueStateL2": float( self.drafting.critique_state.detach().float().norm().cpu() ), "disclosureStateL2": float( self.drafting.disclosure_state.detach().float().norm().cpu() ), "patchStateL2": float( self.drafting.patch_state.detach().float().norm().cpu() ), "verificationStateL2": float( self.drafting.verification_state.detach().float().norm().cpu() ), "revisionDeltaL2": float( self.drafting.revision_delta.detach().float().norm().cpu() ), "draftResidualScale": float( self.drafting.draft_residual_scale.detach().float().cpu() ), "draftProposalGate": float( self.drafting.draft_proposal_gate.detach().float().mean().cpu() ), "draftProposalDeltaL2": float( self.drafting.draft_proposal_delta.detach().float().norm().cpu() ), "revisionGate": float( self.drafting.revision_gate.detach().float().mean().cpu() ), "verifiedRevisionDeltaL2": float( self.drafting.verified_revision_delta.detach().float().norm().cpu() ), "experimentRequirement": float( self.drafting.experiment_requirement.detach().float().mean().cpu() ), "experimentObserved": bool( self.drafting.experiment_observed.detach() .to(device="cpu", dtype=torch.bool) .all() ), "experimentPassed": bool( self.drafting.experiment_passed.detach() .to(device="cpu", dtype=torch.bool) .all() ), "submissionProposal": float( self.drafting.submission_proposal.detach().float().mean().cpu() ), "submissionReadiness": float( self.drafting.submission_readiness.detach().float().mean().cpu() ), "verifiedEvidenceSource": "persisted_external_execution_outcome", "targetFreeForward": True, }, "nlaActivation": { "schema": "nnf.resynthesis.nla_activation_round_trip.v1", "modelOwned": True, "targetFree": True, "mode": "activation_round_trip_five_w", "answerSurfaceAuthority": False, "untrainedExternalVerbalizerUsed": False, "fullContextParentSummaryIncluded": True, "recurrentGeneratedTrajectoryIncluded": True, "intentActionDirectConfidenceConditioning": True, "nativeContextFloorTokens": (RESYNTHESIS_NATIVE_CONTEXT_FLOOR), "nativeContextPositionAperture": (NATIVE_ATTENTION_POSITION_APERTURE), "sequenceContextPositions": int( self.nla.sequence_context_positions.detach().to( device="cpu", dtype=torch.long, ) ), "intentContextL2": float( self.nla.intent_context.detach().float().norm().cpu() ), "actionContextL2": float( self.nla.action_context.detach().float().norm().cpu() ), "completionContextL2": float( self.nla.completion_context.detach().float().norm().cpu() ), "roundTripMse": float(self.nla.round_trip_mse.detach().float().cpu()), "latentL2": float(self.nla.latent.detach().float().norm().cpu()), "reconstructedL2": float( self.nla.reconstructed.detach().float().norm().cpu() ), "stopContextScaleL2": float( self.nla.stop_context_scale.detach().float().norm().cpu() ), "confidenceContextScaleL2": float( self.nla.confidence_context_scale.detach().float().norm().cpu() ), "stopContextScaleDtype": str(self.nla.stop_context_scale.dtype), "confidenceContextScaleDtype": str( self.nla.confidence_context_scale.dtype ), "confidenceConditioningModelOwned": True, "confidenceConditioningTargetFree": True, "latentCodeIds": ( self.nla.latent_code_ids.detach() .to(device="cpu", dtype=torch.long) .tolist() ), "fiveW": { "why": { "taskConfidence": float( self.correction.task_confidence.detach() .float() .mean() .cpu() ), "correctionTrigger": float( self.correction.trigger.detach().float().mean().cpu() ), "delegationPressure": float( self.correction.delegation_pressure.detach() .float() .mean() .cpu() ), }, "how": { "acquisitionActionProbabilities": ( self.correction.acquisition_action_probs.detach() .float() .cpu() .tolist() ), "expertSelectionCoverageCount": (expert_selection_count), "expertSelectionCoverageTotal": (expert_selection_total), }, "what": { "stopScores": ( self.stop_scores.detach().float().cpu().tolist() ), "draftingPhaseScores": ( self.drafting.lifecycle_scores.detach() .float() .cpu() .tolist() ), }, "when": { "stopProbability": float( self.stop_probability.detach().float().cpu() ), "steps": int( self.steps.detach().to( device="cpu", dtype=torch.long, ) ), }, "where": { "parentKvPrefixPositions": int( self.parent_kv_prefix_positions.detach().to( device="cpu", dtype=torch.long, ) ), "parentKvNewPositions": int( self.parent_kv_new_positions.detach().to( device="cpu", dtype=torch.long, ) ), "scienceActivePositions": int( self.science_active_positions.detach().to( device="cpu", dtype=torch.long, ) ), }, }, }, "bilevelIntervention": self.bilevel.boundary_receipt(), "correctionTrigger": float( self.correction.trigger.detach().float().mean().cpu() ), "taskConfidence": float( self.correction.task_confidence.detach().float().mean().cpu() ), "taskIntent": { "schema": "nnf.resynthesis.task_intent.v1", "axisOrder": [ "inherent_knowledge", "reasoning", "agentic_action", "agentic_research", "validation", ], "probabilities": ( self.task_intent.probabilities.detach().float().cpu().tolist() ), "dominantIndex": ( self.task_intent.dominant_index.detach() .to(device="cpu", dtype=torch.long) .tolist() ), "modelOwned": True, "targetFree": True, "answerAuthority": False, }, "nativePrefillParticipation": { "schema": "nnf.resynthesis.native_prefill_participation.v1", "active": bool( self.prefill.active.detach().to(device="cpu", dtype=torch.bool) ), "inputPositions": int( self.prefill.input_positions.detach().to( device="cpu", dtype=torch.long ) ), "attendedSummaryPositions": int( self.prefill.summary_positions.detach().to( device="cpu", dtype=torch.long ) ), "fabricPhaseCount": int( self.prefill.fabric_phase_count.detach().to( device="cpu", dtype=torch.long ) ), "expertRouteL1": float( self.prefill.expert_routes.detach().float().abs().sum().cpu() ), "layerRouteL1": float( self.prefill.layer_routes.detach().float().abs().sum().cpu() ), "allInputTokensEnteredParent": True, "attentionFabricNoneTraversal": True, "memoryOnlyPrefill": False, "targetFree": True, }, "delegationPressure": float( self.correction.delegation_pressure.detach().float().mean().cpu() ), "parentOutcomeApplied": bool( self.correction.parent_outcome_features[..., 0] .detach() .to(device="cpu", dtype=torch.bool) .any() ), "parentOutcomeStateDeltaL2": float( self.correction.parent_outcome_features[..., 1:] .detach() .float() .norm() .cpu() ), "parentRboStateDeltaL2": float( self.correction.parent_outcome_features[..., 1] .detach() .float() .mean() .cpu() ), "parentArmStateDeltaL1": float( self.correction.parent_outcome_features[..., 2] .detach() .float() .mean() .cpu() ), "parentLegacyStateDeltaL2": float( self.correction.parent_outcome_features[..., 3] .detach() .float() .mean() .cpu() ), "parentRouteStateChanged": bool( self.correction.parent_outcome_features[..., 4] .detach() .to(device="cpu", dtype=torch.bool) .any() ), "fabricPhaseCount": int( self.fabric_phase_count.detach().to(device="cpu", dtype=torch.long) ), "parentFabricConnected": bool( self.parent_fabric_connected.detach().to(device="cpu", dtype=torch.bool) ), "parentRouteConditioningL2": float( self.parent_route_conditioning.detach().float().cpu() ), "parentKvPrefixPositions": int( self.parent_kv_prefix_positions.detach().to( device="cpu", dtype=torch.long, ) ), "parentKvNewPositions": int( self.parent_kv_new_positions.detach().to( device="cpu", dtype=torch.long, ) ), "parentKvCacheReused": bool( self.parent_kv_prefix_positions.detach() .to( device="cpu", dtype=torch.long, ) .gt(0) ), "scienceActivePositions": int( self.science_active_positions.detach().to( device="cpu", dtype=torch.long, ) ), "acquisitionAction": int( self.correction.acquisition_action_index.detach().to( device="cpu", dtype=torch.long, ) ), "acquisitionAuthority": bool( self.correction.acquisition_authority.detach().to( device="cpu", dtype=torch.bool, ) ), "acquisitionPolicyOwner": "resynthesis_additive_graph", "contextIntentActionAttention": True, "intentRelationalAttention": True, "attentionMultiples": ("q", "k", "v", "c", "r"), "contextIntentApplied": True, "contextActionApplied": context_action_applied, "contextActionSource": ("trained_acquisition_policy_probability_tensor"), "contextActionProbabilities": ( self.correction.acquisition_action_probs.detach().float().cpu().tolist() ), "contextActionL1": context_action_l1, "contextActionSciencePhases": 2, "contextActionScorePivotTrainable": True, "stopProbability": float(self.stop_probability.detach().float().cpu()), "stopDecision": bool(self.stop_decision.detach().to(device="cpu")), "additiveStopProbability": float( _additive_completion_probability( self.stop_scores.detach().float() ).cpu() ), "additiveStopDecision": bool( _additive_completion_decision(self.stop_scores.detach().float()).cpu() ), "parentNativeStopProbability": float( self.parent_native_stop_probability.detach().float().cpu() ), "parentNativeStopDecision": bool( self.parent_native_stop_decision.detach().to(device="cpu") ), "generationDelegationExit": bool( self.generation_delegation_exit.detach().to( device="cpu", dtype=torch.bool, ) ), "generationTrajectoryInitialized": bool( self.generation_trajectory_initialized.detach().to( device="cpu", dtype=torch.bool, ) ), "generationNativeProgress": bool( self.generation_native_progress.detach().to( device="cpu", dtype=torch.bool, ) ), "generationTaskProgress": bool( self.generation_task_progress.detach().to( device="cpu", dtype=torch.bool, ) ), "generationArmExhausted": bool(generation_arm_exhausted), "generationArmExhaustionExit": generation_arm_exhaustion_exit, "generationExpertSelectionCoverageCount": expert_selection_count, "generationExpertSelectionCoverageTotal": expert_selection_total, "generationExpertSelectionEventCount": expert_selection_events, "generationExpertSelectionCoverageComplete": ( expert_selection_count == expert_selection_total ), "generationExpertSelectionOwner": ( "resynthesis_science_stack_post_none_route_argmax_selection_count" ), "noneTraversalCommunication": { "schema": "nnf.resynthesis.none_rbo_fabric_traversal.v1", "modelOwned": True, "targetFree": True, "expertRouteSurface": "post_none_transfer_gate_weights", "layerRouteSurface": "post_layer_transfer_graph_gate", "scienceExpertRouteElements": self.expert_routes.numel(), "scienceExpertRouteNonzeroElements": int( torch.count_nonzero(self.expert_routes.detach()).cpu() ), "scienceExpertRouteL1": float( self.expert_routes.detach().float().abs().sum().cpu() ), "scienceLayerRouteElements": self.layer_routes.numel(), "scienceLayerRouteNonzeroElements": int( torch.count_nonzero(self.layer_routes.detach()).cpu() ), "scienceLayerRouteL1": float( self.layer_routes.detach().float().abs().sum().cpu() ), "parentExpertRouteL1": float( self.parent_expert_routes.detach().float().abs().sum().cpu() ), "parentLayerRouteL1": float( self.parent_layer_routes.detach().float().abs().sum().cpu() ), "fabricPhaseCount": int( self.fabric_phase_count.detach().to( device="cpu", dtype=torch.long, ) ), "selectedExpertSlotCount": expert_selection_count, "selectedExpertSlotTotal": expert_selection_total, "expertSelectionEventCount": expert_selection_events, }, "answerCompletionVerified": answer_completion_verified, "answerLogitOwner": "resynthesis_additive_none_rbo_fabric", "frozenParentVocabularyProjectionOnly": True, "parentLogitsDiagnosticOnly": True, "parentLogitsAffectAnswer": False, "parentStopDiagnosticOnly": True, "parentStopAffectsExecution": False, "parentRetentionAuthority": False, "generationBoundaryOwner": ( ( "trained_correction_complete_expert_selection_arm_exhaustion" if corrected_attempt else "trained_initial_complete_expert_selection_arm_exhaustion" ) if generation_arm_exhaustion_exit else ( "gradient_proven_additive_completion_successor_candidate" if stop_authority == STOP_AUTHORITY_SUCCESSOR_CANDIDATE else ( "validation_retained_additive_completion_successor" if stop_authority == STOP_AUTHORITY_SUCCESSOR_RETAINED else "resynthesis_additive_completion_graph" ) ) ), "stopAuthorityOwner": ( "gradient_proven_additive_completion_successor_candidate" if stop_authority == STOP_AUTHORITY_SUCCESSOR_CANDIDATE else ( "validation_retained_additive_completion_successor" if stop_authority == STOP_AUTHORITY_SUCCESSOR_RETAINED else "resynthesis_additive_completion_graph" ) ), "stopAuthorityRetained": stop_authority in ( STOP_AUTHORITY_ADDITIVE_TELEMETRY, STOP_AUTHORITY_SUCCESSOR_RETAINED, ), "stopAuthorityCandidateEvaluationActive": ( stop_authority == STOP_AUTHORITY_SUCCESSOR_CANDIDATE ), "completionSuccessorRetained": ( stop_authority == STOP_AUTHORITY_SUCCESSOR_RETAINED ), "completionSuccessorParentFallbackPreserved": False, } @dataclass(frozen=True) class RBOGenerationResult: """Tensor-native generation boundary retaining same-session correction state.""" token_ids: torch.Tensor generated_lengths_t: torch.Tensor termination_mask_t: torch.Tensor next_state: RBOCorrectionState final_result: RBOResult def select_batch_row_boundary( self, batch_index: int, *, prompt_width: int, ) -> RBOGenerationResult: """Return one valid generated row at the external grading boundary.""" batch_size = self.token_ids.shape[0] if batch_index < 0 or batch_index >= batch_size: raise IndexError("generation result batch index is out of range") if prompt_width < 1 or prompt_width >= self.token_ids.shape[1]: raise ValueError("generation prompt width is outside the emitted sequence") generated_width = int( self.generated_lengths_t[batch_index] .detach() .to(device="cpu", dtype=torch.long) ) if generated_width < 1: raise RuntimeError("generation result row emitted no active tokens") valid_width = prompt_width + generated_width if valid_width > self.token_ids.shape[1]: raise RuntimeError("generation result row length exceeds its token surface") row = slice(batch_index, batch_index + 1) return RBOGenerationResult( token_ids=self.token_ids[row, :valid_width], generated_lengths_t=self.generated_lengths_t[row], termination_mask_t=self.termination_mask_t[row], next_state=self.next_state.select_batch_row_boundary(batch_index), final_result=self.final_result.select_batch_row_boundary(batch_index), ) @dataclass(frozen=True) class RBOEmissionTracePacket: """Detached tensor-only evidence emitted after each native token. This packet is an explicit external observation boundary. It has no target/gold field and no return channel into routing, scoring, token selection, or stopping. Every tensor is cloned away from live model state before an observer receives it, so an auditor can decode and annotate the stream without rewriting the model-owned answer surface. """ emission_index_t: torch.Tensor active_emission_mask_t: torch.Tensor token_ids_t: torch.Tensor attempt_index_t: torch.Tensor stop_scores_t: torch.Tensor additive_stop_probability_t: torch.Tensor native_stop_probability_t: torch.Tensor fused_stop_probability_t: torch.Tensor additive_stop_decision_t: torch.Tensor native_stop_decision_t: torch.Tensor fused_stop_decision_t: torch.Tensor decode_stop_authority_t: torch.Tensor task_confidence_t: torch.Tensor correction_trigger_t: torch.Tensor delegation_pressure_t: torch.Tensor task_intent_probabilities_t: torch.Tensor task_intent_dominant_index_t: torch.Tensor prefill_active_t: torch.Tensor prefill_input_positions_t: torch.Tensor prefill_summary_positions_t: torch.Tensor prefill_fabric_phase_count_t: torch.Tensor prefill_expert_routes_t: torch.Tensor prefill_layer_routes_t: torch.Tensor nla_round_trip_mse_t: torch.Tensor nla_latent_l2_t: torch.Tensor nla_reconstructed_l2_t: torch.Tensor nla_latent_code_ids_t: torch.Tensor nla_sequence_context_positions_t: torch.Tensor nla_intent_context_l2_t: torch.Tensor nla_action_context_l2_t: torch.Tensor nla_completion_context_l2_t: torch.Tensor nla_stop_context_scale_t: torch.Tensor nla_confidence_context_scale_t: torch.Tensor fabric_phase_count_t: torch.Tensor science_expert_routes_t: torch.Tensor science_layer_routes_t: torch.Tensor science_selected_layer_ids_t: torch.Tensor science_selected_expert_ids_t: torch.Tensor science_selected_layer_expert_pairs_t: torch.Tensor parent_expert_routes_t: torch.Tensor parent_layer_routes_t: torch.Tensor expert_selection_count_t: torch.Tensor expert_selection_total_t: torch.Tensor active_routed_page_ids_t: torch.Tensor trajectory_initialized_t: torch.Tensor native_progress_t: torch.Tensor task_progress_t: torch.Tensor arm_exhausted_t: torch.Tensor arm_exhaustion_exit_t: torch.Tensor delegation_exit_t: torch.Tensor def select_batch_row_boundary(self, batch_index: int) -> RBOEmissionTracePacket: """Select one target-free row for an external streaming auditor.""" batch_size = self.token_ids_t.shape[0] if batch_index < 0 or batch_index >= batch_size: raise IndexError("emission packet batch index is out of range") row = slice(batch_index, batch_index + 1) return replace( self, active_emission_mask_t=self.active_emission_mask_t[row], token_ids_t=self.token_ids_t[row], attempt_index_t=self.attempt_index_t[row], stop_scores_t=self.stop_scores_t[row], additive_stop_probability_t=self.additive_stop_probability_t[row], native_stop_probability_t=self.native_stop_probability_t[row], fused_stop_probability_t=self.fused_stop_probability_t[row], additive_stop_decision_t=self.additive_stop_decision_t[row], native_stop_decision_t=self.native_stop_decision_t[row], fused_stop_decision_t=self.fused_stop_decision_t[row], decode_stop_authority_t=self.decode_stop_authority_t[row], task_confidence_t=self.task_confidence_t[row], correction_trigger_t=self.correction_trigger_t[row], delegation_pressure_t=self.delegation_pressure_t[row], task_intent_probabilities_t=self.task_intent_probabilities_t[row], task_intent_dominant_index_t=self.task_intent_dominant_index_t[row], prefill_input_positions_t=( _select_prefill_positions_batch_row_boundary( self.prefill_input_positions_t, batch_size=batch_size, batch_index=batch_index, ) ), prefill_expert_routes_t=_select_prefill_route_batch_row_boundary( self.prefill_expert_routes_t, batch_size=batch_size, batch_index=batch_index, ), prefill_layer_routes_t=_select_prefill_route_batch_row_boundary( self.prefill_layer_routes_t, batch_size=batch_size, batch_index=batch_index, ), nla_latent_code_ids_t=self.nla_latent_code_ids_t[row], science_expert_routes_t=self.science_expert_routes_t[:, :, row], science_layer_routes_t=self.science_layer_routes_t[:, :, row], science_selected_layer_ids_t=self.science_selected_layer_ids_t[:, row], science_selected_expert_ids_t=self.science_selected_expert_ids_t[:, row], science_selected_layer_expert_pairs_t=( self.science_selected_layer_expert_pairs_t[:, row] ), parent_expert_routes_t=self.parent_expert_routes_t[:, row], parent_layer_routes_t=self.parent_layer_routes_t[:, row], expert_selection_count_t=self.expert_selection_count_t[row], expert_selection_total_t=self.expert_selection_total_t[row], active_routed_page_ids_t=self.active_routed_page_ids_t[row], trajectory_initialized_t=self.trajectory_initialized_t[row], native_progress_t=self.native_progress_t[row], task_progress_t=self.task_progress_t[row], arm_exhausted_t=self.arm_exhausted_t[row], arm_exhaustion_exit_t=self.arm_exhaustion_exit_t[row], delegation_exit_t=self.delegation_exit_t[row], ) def boundary_receipt(self) -> dict[str, Any]: """Serialize compact real-time evidence at the external audit boundary.""" def route_ids(value: torch.Tensor) -> list[Any]: return ( value.detach() .float() .argmax(dim=-1) .to(device="cpu", dtype=torch.long) .tolist() ) native_criteria_resolved = bool( self.native_stop_decision_t.detach().to( device="cpu", dtype=torch.bool, ) ) trained_completion_criteria_resolved = bool( self.fused_stop_decision_t.detach().to( device="cpu", dtype=torch.bool, ) ) arm_exhaustion_exit = bool( self.arm_exhaustion_exit_t.detach().to( device="cpu", dtype=torch.bool, ) ) return { "schema": "nnf.resynthesis.token_emission_audit.v1", "diagnosticOnly": True, "targetFreeModelForward": True, "goldInputPresentInPacket": False, "routingAuthority": False, "scoringAuthority": False, "stoppingAuthority": False, "answerRewriteAuthority": False, "observerReturnAuthority": False, "emissionIndex": int( self.emission_index_t.detach().to(device="cpu", dtype=torch.long) ), "activeEmission": bool( self.active_emission_mask_t.detach().to( device="cpu", dtype=torch.bool, ) ), "tokenIds": ( self.token_ids_t.detach().to(device="cpu", dtype=torch.long).tolist() ), "attemptIndex": ( self.attempt_index_t.detach() .to(device="cpu", dtype=torch.long) .tolist() ), "stopScores": self.stop_scores_t.detach().float().cpu().tolist(), "additiveStopProbability": float( self.additive_stop_probability_t.detach().float().cpu() ), "nativeStopProbability": float( self.native_stop_probability_t.detach().float().cpu() ), "fusedStopProbability": float( self.fused_stop_probability_t.detach().float().cpu() ), "additiveStopDecision": bool( self.additive_stop_decision_t.detach().to( device="cpu", dtype=torch.bool ) ), "nativeStopDecision": bool( self.native_stop_decision_t.detach().to(device="cpu", dtype=torch.bool) ), "fusedStopDecision": bool( self.fused_stop_decision_t.detach().to(device="cpu", dtype=torch.bool) ), "nativeCriteriaResolved": native_criteria_resolved, "nativeCriteriaDiagnosticOnly": True, "nativeCriteriaAffectExecution": False, "trainedCompletionCriteriaResolved": ( trained_completion_criteria_resolved ), "generationTerminationCriteriaResolved": ( trained_completion_criteria_resolved or arm_exhaustion_exit ), "decodeStopAuthority": int( self.decode_stop_authority_t.detach().to(device="cpu", dtype=torch.long) ), "taskConfidence": float( self.task_confidence_t.detach().float().mean().cpu() ), "correctionTrigger": float( self.correction_trigger_t.detach().float().mean().cpu() ), "delegationPressure": float( self.delegation_pressure_t.detach().float().mean().cpu() ), "taskIntentProbabilities": ( self.task_intent_probabilities_t.detach().float().cpu().tolist() ), "taskIntentDominantIndex": ( self.task_intent_dominant_index_t.detach() .to(device="cpu", dtype=torch.long) .tolist() ), "prefillActive": bool( self.prefill_active_t.detach().to(device="cpu", dtype=torch.bool) ), "prefillInputPositions": int( self.prefill_input_positions_t.detach().to( device="cpu", dtype=torch.long ) ), "prefillAttendedSummaryPositions": int( self.prefill_summary_positions_t.detach().to( device="cpu", dtype=torch.long ) ), "prefillFabricPhaseCount": int( self.prefill_fabric_phase_count_t.detach().to( device="cpu", dtype=torch.long ) ), "prefillExpertRouteL1": float( self.prefill_expert_routes_t.detach().float().abs().sum().cpu() ), "prefillLayerRouteL1": float( self.prefill_layer_routes_t.detach().float().abs().sum().cpu() ), "nlaRoundTripMse": float(self.nla_round_trip_mse_t.detach().float().cpu()), "nlaLatentL2": float(self.nla_latent_l2_t.detach().float().cpu()), "nlaReconstructedL2": float( self.nla_reconstructed_l2_t.detach().float().cpu() ), "nlaLatentCodeIds": ( self.nla_latent_code_ids_t.detach() .to(device="cpu", dtype=torch.long) .tolist() ), "nlaSequenceContextPositions": int( self.nla_sequence_context_positions_t.detach().to( device="cpu", dtype=torch.long, ) ), "nlaIntentContextL2": float( self.nla_intent_context_l2_t.detach().float().cpu() ), "nlaActionContextL2": float( self.nla_action_context_l2_t.detach().float().cpu() ), "nlaCompletionContextL2": float( self.nla_completion_context_l2_t.detach().float().cpu() ), "nlaStopContextScaleL2": float( self.nla_stop_context_scale_t.detach().float().norm().cpu() ), "nlaConfidenceContextScaleL2": float( self.nla_confidence_context_scale_t.detach().float().norm().cpu() ), "nlaStopContextScaleDtype": str(self.nla_stop_context_scale_t.dtype), "nlaConfidenceContextScaleDtype": str( self.nla_confidence_context_scale_t.dtype ), "fabricPhaseCount": int( self.fabric_phase_count_t.detach().to(device="cpu", dtype=torch.long) ), "scienceExpertRouteIds": route_ids(self.science_expert_routes_t), "scienceLayerRouteIds": route_ids(self.science_layer_routes_t), "scienceSelectedLayerIds": ( self.science_selected_layer_ids_t.detach() .reshape(-1) .to(device="cpu", dtype=torch.long) .tolist() ), "scienceSelectedExpertIds": ( self.science_selected_expert_ids_t.detach() .reshape(-1) .to(device="cpu", dtype=torch.long) .tolist() ), "scienceSelectedLayerExpertPairs": ( self.science_selected_layer_expert_pairs_t.detach() .reshape(-1, 2) .to(device="cpu", dtype=torch.long) .tolist() ), "parentExpertRouteIds": route_ids(self.parent_expert_routes_t), "parentLayerRouteIds": route_ids(self.parent_layer_routes_t), "scienceExpertRouteL1": float( self.science_expert_routes_t.detach().float().abs().sum().cpu() ), "scienceLayerRouteL1": float( self.science_layer_routes_t.detach().float().abs().sum().cpu() ), "parentExpertRouteL1": float( self.parent_expert_routes_t.detach().float().abs().sum().cpu() ), "parentLayerRouteL1": float( self.parent_layer_routes_t.detach().float().abs().sum().cpu() ), "expertSelectionCount": int( self.expert_selection_count_t.detach().to( device="cpu", dtype=torch.long ) ), "expertSelectionTotal": int( self.expert_selection_total_t.detach().to( device="cpu", dtype=torch.long ) ), "activeRoutedPageIds": ( sorted( set( self.active_routed_page_ids_t.detach() .reshape(-1) .to(device="cpu", dtype=torch.long) .tolist() ) ) ), "trajectoryInitialized": bool( self.trajectory_initialized_t.detach().to( device="cpu", dtype=torch.bool ) ), "nativeProgress": bool( self.native_progress_t.detach().to(device="cpu", dtype=torch.bool) ), "taskProgress": bool( self.task_progress_t.detach().to(device="cpu", dtype=torch.bool) ), "armExhausted": bool( self.arm_exhausted_t.detach().to(device="cpu", dtype=torch.bool) ), "armExhaustionExit": arm_exhaustion_exit, "delegationExit": bool( self.delegation_exit_t.detach().to(device="cpu", dtype=torch.bool) ), } class NeuralStopGate(nn.Module): """Additive-graph trajectory encoder for learned completion readiness. Encodes the trajectory of hidden states across RBO steps and predicts whether the reasoning is READY (stop) or needs MORE (continue). The gate starts neutral (sigmoid(0)=0.5) and trains purely from execution-grounded outcomes. This is the active learned completion authority, not a host cap. The frozen vocabulary/feature parent has no stopping authority. Both dimensions are explicit runtime geometry because hidden structural defaults silently turn a bounded test shape into a production capacity ceiling. """ def __init__(self, *, hidden_size: int, trajectory_dim: int) -> None: super().__init__() if type(hidden_size) is not int or hidden_size < 1: raise ValueError("stop-gate hidden geometry must be a positive integer") if type(trajectory_dim) is not int or trajectory_dim < 1: raise ValueError( "stop-gate trajectory geometry must be a positive integer" ) self.hidden_size = hidden_size self.trajectory_dim = trajectory_dim # Pool hidden → trajectory vector per step. self.trajectory_proj = nn.Linear( self.hidden_size, self.trajectory_dim, bias=False ) self.nla_context_scale = nn.Parameter(torch.zeros(self.trajectory_dim)) # LSTM encodes the sequence of trajectory vectors. self.lstm = nn.LSTM(self.trajectory_dim, self.trajectory_dim, batch_first=True) # Utility gate: how useful is the current state? self.stop_utility_gate = nn.Linear(self.trajectory_dim, 1, bias=True) nn.init.zeros_(self.stop_utility_gate.weight) nn.init.zeros_(self.stop_utility_gate.bias) # Contradiction gate: has a contradiction been resolved? self.stop_contradiction_gate = nn.Linear(self.trajectory_dim, 1, bias=True) nn.init.zeros_(self.stop_contradiction_gate.weight) nn.init.zeros_(self.stop_contradiction_gate.bias) def forward( self, trajectory_hidden: torch.Tensor, nla_context: torch.Tensor, ) -> NeuralStopGateOutput: """Predict stop utility + contradiction resolution from trajectory. Args: trajectory_hidden: [batch, steps, hidden_size] — the sequence of hidden states across RBO steps. Returns a tensor-only packet with utility and contradiction probabilities. """ if trajectory_hidden.shape[1] == 0: batch = trajectory_hidden.shape[0] zero = trajectory_hidden.new_zeros(batch, 1) return NeuralStopGateOutput(utility=zero, contradiction=zero) if nla_context.shape != ( trajectory_hidden.shape[0], self.trajectory_dim, ): raise ValueError("NLA stop context geometry differs from trajectory") traj = self.trajectory_proj(trajectory_hidden) normalized_nla = F.layer_norm(nla_context, (self.trajectory_dim,)) nla_scale = ( torch.tanh(self.nla_context_scale).to(dtype=traj.dtype).reshape(1, 1, -1) ) traj = traj + nla_scale * normalized_nla.unsqueeze(1) lstm_out, _hidden = self.lstm(traj) last = lstm_out[:, -1:, :].squeeze(1) utility_stop = torch.sigmoid(self.stop_utility_gate(last)) contradiction_stop = torch.sigmoid(self.stop_contradiction_gate(last)) return NeuralStopGateOutput( utility=utility_stop, contradiction=contradiction_stop, ) @dataclass(frozen=True) class CudaAllocatorHeadroom: """Exact reusable CUDA capacity observed at an external boundary.""" driver_free_bytes: int driver_total_bytes: int allocator_allocated_bytes: int allocator_active_bytes: int allocator_reserved_bytes: int allocator_fragmentation_bytes: int allocator_reusable_reserved_bytes: int effective_allocator_headroom_bytes: int def receipt_boundary(self) -> dict[str, int | str]: """Record each capacity component without influencing model routing.""" return { "schema": "nnf.resynthesis.cuda_allocator_headroom.v1", "driverFreeBytes": self.driver_free_bytes, "driverTotalBytes": self.driver_total_bytes, "allocatorAllocatedBytes": self.allocator_allocated_bytes, "allocatorActiveBytes": self.allocator_active_bytes, "allocatorReservedBytes": self.allocator_reserved_bytes, "allocatorFragmentationBytes": self.allocator_fragmentation_bytes, "allocatorReusableReservedBytes": ( self.allocator_reusable_reserved_bytes ), "effectiveAllocatorHeadroomBytes": ( self.effective_allocator_headroom_bytes ), } def _cuda_allocator_headroom_boundary( device: torch.device, ) -> CudaAllocatorHeadroom: """Measure driver-free plus non-fragmented allocator-reserved capacity.""" if device.type != "cuda": raise ValueError("CUDA allocator headroom requires a CUDA device") try: driver_free_value, driver_total_value = torch.cuda.mem_get_info(device) allocator_allocated_value = torch.cuda.memory_allocated(device) allocator_reserved_value = torch.cuda.memory_reserved(device) except (RuntimeError, ValueError) as exc: raise RuntimeError("live CUDA allocator evidence is unavailable") from exc hard_values = ( driver_free_value, driver_total_value, allocator_allocated_value, allocator_reserved_value, ) if any( isinstance(value, bool) or not isinstance(value, int) or value < 0 for value in hard_values ): raise RuntimeError("live CUDA allocator evidence is malformed") driver_free_bytes = driver_free_value driver_total_bytes = driver_total_value allocator_allocated_bytes = allocator_allocated_value allocator_reserved_bytes = allocator_reserved_value if ( driver_free_bytes > driver_total_bytes or allocator_allocated_bytes > allocator_reserved_bytes or driver_free_bytes > driver_total_bytes - allocator_allocated_bytes ): raise RuntimeError("live CUDA allocator evidence is malformed") try: memory_stats = torch.cuda.memory_stats(device) except (RuntimeError, ValueError): memory_stats = {} allocator_active_value = memory_stats.get("active_bytes.all.current") allocator_fragmentation_value = memory_stats.get( "inactive_split_bytes.all.current", ) if allocator_active_value is None or allocator_fragmentation_value is None: # Without allocator block telemetry, cached reservations are not # proven reusable. Driver-free memory remains safe to admit. allocator_active_bytes = allocator_reserved_bytes allocator_fragmentation_bytes = 0 else: if any( isinstance(value, bool) or not isinstance(value, int) or value < 0 for value in ( allocator_active_value, allocator_fragmentation_value, ) ): raise RuntimeError("live CUDA allocator evidence is malformed") allocator_active_bytes = cast(int, allocator_active_value) allocator_fragmentation_bytes = cast( int, allocator_fragmentation_value, ) if ( allocator_active_bytes < allocator_allocated_bytes or allocator_active_bytes > allocator_reserved_bytes or allocator_fragmentation_bytes > allocator_reserved_bytes - allocator_active_bytes ): raise RuntimeError("live CUDA allocator evidence is malformed") allocator_reusable_reserved_bytes = max( 0, allocator_reserved_bytes - allocator_active_bytes - allocator_fragmentation_bytes, ) effective_allocator_headroom_bytes = min( driver_free_bytes + allocator_reusable_reserved_bytes, driver_total_bytes - allocator_allocated_bytes, ) return CudaAllocatorHeadroom( driver_free_bytes=driver_free_bytes, driver_total_bytes=driver_total_bytes, allocator_allocated_bytes=allocator_allocated_bytes, allocator_active_bytes=allocator_active_bytes, allocator_reserved_bytes=allocator_reserved_bytes, allocator_fragmentation_bytes=allocator_fragmentation_bytes, allocator_reusable_reserved_bytes=allocator_reusable_reserved_bytes, effective_allocator_headroom_bytes=( effective_allocator_headroom_bytes ), ) def _factorized_completion_backward_cell_chunk_width_boundary( *, cell_count: int, vocab_tile_rows: int, device: torch.device, ) -> int: """Bound exact-CE replay cells using headroom observed inside backward.""" if ( isinstance(cell_count, bool) or not isinstance(cell_count, int) or cell_count < 1 or isinstance(vocab_tile_rows, bool) or not isinstance(vocab_tile_rows, int) or vocab_tile_rows < 1 ): raise ValueError("factorized completion backward geometry is invalid") if device.type != "cuda": return cell_count allocator_headroom = _cuda_allocator_headroom_boundary(device) fp32_bytes = torch.empty((), dtype=torch.float32).element_size() # The graph-free replay now reuses the projection tensor for the baseline # add and then turns that same tensor into the exact logsumexp gradient in # place. Its peak is therefore the indexed baseline, the projection/logit # buffer, and one construction temporary. Keep two additional surfaces # reserved for allocator/cuBLAS skew. Charging the removed out-of-place # sum here would unnecessarily split exact CE into 20% more CUDA chunks. exact_ce_working_surfaces = 5 bytes_per_cell = ( vocab_tile_rows * fp32_bytes * exact_ce_working_surfaces ) live_cell_capacity = ( allocator_headroom.effective_allocator_headroom_bytes // bytes_per_cell ) if live_cell_capacity < 1: raise RuntimeError( "live CUDA capacity cannot hold one factorized completion " "backward cell" ) return min(cell_count, live_cell_capacity) def _factorized_completion_partition_cell_chunk_width_boundary( *, cell_count: int, device: torch.device, ) -> int: """Bound the exact-CE partition replay temporary independently of logits.""" if ( isinstance(cell_count, bool) or not isinstance(cell_count, int) or cell_count < 1 ): raise ValueError("factorized completion partition geometry is invalid") # Each replay cell only carries the two FP32 closed-form VJP # denominators. Keep that stacked surface bounded even when the # vocabulary-tile replay below is admitted a much wider cell chunk. The # CPU path keeps its historical full-vector arithmetic; CUDA avoids a # dense temporary that can otherwise consume several GiB before the # graph-free vocabulary replay starts. if device.type != "cuda": return cell_count return min(cell_count, 262_144) def _factorized_completion_forward_packet( position_context_t: torch.Tensor, baseline_logits_rows_t: torch.Tensor, dense_row_index_t: torch.Tensor, safe_targets_t: torch.Tensor, low_rank_head_t: torch.Tensor, vocab_tile_rows: int, ) -> torch.Tensor: """Return exact CE statistics plus bounded per-tile partitions.""" position_context_fp32_t = position_context_t.float() partition_t: torch.Tensor | None = None prediction_max_t: torch.Tensor | None = None prediction_t: torch.Tensor | None = None target_logits_t: torch.Tensor | None = None target_found_t = torch.zeros_like(safe_targets_t, dtype=torch.bool) vocab_rows = baseline_logits_rows_t.shape[1] tile_count = math.ceil(vocab_rows / vocab_tile_rows) partition_steps_t = torch.empty( ( safe_targets_t.shape[0], tile_count, ), dtype=torch.float32, device=position_context_t.device, ) cumulative_partitions_t = torch.empty_like(partition_steps_t) for tile_index, vocab_start in enumerate( range(0, vocab_rows, vocab_tile_rows) ): vocab_end = min(vocab_rows, vocab_start + vocab_tile_rows) baseline_tile_t = baseline_logits_rows_t[ :, vocab_start:vocab_end, ].index_select(0, dense_row_index_t) logits_tile_t = F.linear( position_context_fp32_t, low_rank_head_t[vocab_start:vocab_end], ) # The projection is private to this custom loss forward. Reuse it for # the exact baseline addition instead of allocating a second complete # cell-by-vocabulary result. This performs the same FP32 add as # ``baseline + projection``; the bit-exact forward/backward tests bind # both the loss statistics and every requested input gradient. logits_tile_t.add_(baseline_tile_t.float()) target_in_tile_t = safe_targets_t.ge(vocab_start) & safe_targets_t.lt( vocab_end ) local_targets_t = (safe_targets_t - vocab_start).clamp( min=0, max=vocab_end - vocab_start - 1, ) tile_target_logits_t = logits_tile_t.gather( -1, local_targets_t.unsqueeze(-1), ).squeeze(-1) target_logits_t = ( torch.where( target_in_tile_t, tile_target_logits_t, target_logits_t, ) if target_logits_t is not None else torch.where( target_in_tile_t, tile_target_logits_t, torch.zeros_like(tile_target_logits_t), ) ) tile_max_t, tile_prediction_t = logits_tile_t.detach().max(dim=-1) # The prediction reduction already proves the exact maximum for this # vocabulary tile. ``torch.logsumexp`` would launch a second full-tile # maximum reduction before its exponential sum. Reuse the model-owned # maximum and the now-dead logits buffer for the identical stable # reduction. The target logit was gathered above and the custom # backward replays logits from its sealed inputs, so this mutation # cannot expose targets to the forward graph or alter gradient authority. tile_partition_t = ( (logits_tile_t - tile_max_t.unsqueeze(-1)) .exp() .sum(dim=-1) .log() .add(tile_max_t) if logits_tile_t.requires_grad else logits_tile_t.sub_(tile_max_t.unsqueeze(-1)) .exp_() .sum(dim=-1) .log_() .add_(tile_max_t) ) target_found_t.logical_or_(target_in_tile_t) partition_steps_t[:, tile_index].copy_(tile_partition_t) tile_prediction_t = tile_prediction_t + vocab_start if partition_t is None: partition_t = tile_partition_t prediction_max_t = tile_max_t prediction_t = tile_prediction_t else: partition_t = torch.logaddexp(partition_t, tile_partition_t) assert prediction_max_t is not None assert prediction_t is not None tile_wins_t = tile_max_t > prediction_max_t prediction_max_t = torch.where( tile_wins_t, tile_max_t, prediction_max_t, ) prediction_t = torch.where( tile_wins_t, tile_prediction_t, prediction_t, ) cumulative_partitions_t[:, tile_index].copy_(partition_t) assert partition_t is not None assert prediction_t is not None assert target_logits_t is not None torch._assert_async( target_found_t.all(), "bulk completion target is outside the vocabulary", ) token_nll_t = partition_t - target_logits_t statistics_t = torch.stack( (token_nll_t, prediction_t.to(dtype=token_nll_t.dtype)), dim=-1, ) return torch.cat( (statistics_t, partition_steps_t, cumulative_partitions_t), dim=-1, ) class _FactorizedCompletionLossStatistics(torch.autograd.Function): """Replay exact factorized CE one bounded vocabulary tile at a time.""" @staticmethod def forward( ctx: Any, position_context_t: torch.Tensor, baseline_logits_rows_t: torch.Tensor, dense_row_index_t: torch.Tensor, safe_targets_t: torch.Tensor, low_rank_head_t: torch.Tensor, vocab_tile_rows: int, ) -> torch.Tensor: cell_count = position_context_t.shape[0] cell_chunk_width = ( _factorized_completion_backward_cell_chunk_width_boundary( cell_count=cell_count, vocab_tile_rows=vocab_tile_rows, device=position_context_t.device, ) ) tile_count = math.ceil( baseline_logits_rows_t.shape[1] / vocab_tile_rows ) if cell_chunk_width >= cell_count: forward_packet_t = _factorized_completion_forward_packet( position_context_t, baseline_logits_rows_t, dense_row_index_t, safe_targets_t, low_rank_head_t, vocab_tile_rows, ) else: # One autograd owner must span the complete training wave. Splitting # the owner at this execution boundary made every cell chunk return # and zero a full ``[vocab, rank]`` low-rank-head gradient. Stream # the bounded forward packets into one tensor instead; backward can # then allocate that model-owned gradient exactly once while still # replaying every cell/vocabulary tile inside the live-memory proof. forward_packet_t = torch.empty( (cell_count, 2 + 2 * tile_count), dtype=torch.float32, device=position_context_t.device, ) for cell_start in range(0, cell_count, cell_chunk_width): cell_end = min(cell_count, cell_start + cell_chunk_width) forward_packet_t[cell_start:cell_end].copy_( _factorized_completion_forward_packet( position_context_t[cell_start:cell_end], baseline_logits_rows_t, dense_row_index_t[cell_start:cell_end], safe_targets_t[cell_start:cell_end], low_rank_head_t, vocab_tile_rows, ) ) ctx.vocab_tile_rows = vocab_tile_rows ctx.tile_count = tile_count ctx.save_for_backward( position_context_t, baseline_logits_rows_t, dense_row_index_t, safe_targets_t, low_rank_head_t, forward_packet_t[:, 2:], ) return forward_packet_t[:, :2] @staticmethod def backward( # type: ignore[override] ctx: Any, grad_statistics_t: torch.Tensor, ) -> tuple[ torch.Tensor | None, torch.Tensor | None, None, None, torch.Tensor | None, None, ]: ( position_context_t, baseline_logits_rows_t, dense_row_index_t, safe_targets_t, low_rank_head_t, partition_steps_t, ) = ctx.saved_tensors grad_nll_t = grad_statistics_t[:, 0] needs_input_grad = ctx.needs_input_grad tile_count = cast(int, ctx.tile_count) tile_partitions_t = partition_steps_t[:, :tile_count] cumulative_partitions_t = partition_steps_t[:, tile_count:] partition_step_grad_t = torch.empty_like(tile_partitions_t) partition_upstream_grad_t = grad_nll_t.clone() partition_cell_chunk_width = ( _factorized_completion_partition_cell_chunk_width_boundary( cell_count=partition_steps_t.shape[0], device=partition_steps_t.device, ) ) # Replay each scalar logaddexp backward in its original reverse order. # PyTorch's exact real-valued VJP is # # grad_a = grad / (1 + exp(b - a)) # grad_b = grad / (1 + exp(a - b)) # # Applying those divisions in the same reverse order is bit-identical # to LogaddexpBackward on FP32/FP64 while removing one temporary # autograd graph and one torch.autograd.grad launch for every # tile-by-cell chunk. Rebuilding the complete reduction or replacing # either division with a complementary subtraction changes a few last # FP32 bits, so the sequential division order is intentional. for reverse_tile_index in range(tile_count - 1): tile_index = tile_count - reverse_tile_index - 1 for cell_start in range( 0, partition_steps_t.shape[0], partition_cell_chunk_width, ): cell_end = min( partition_steps_t.shape[0], cell_start + partition_cell_chunk_width, ) prior_partition_t = cumulative_partitions_t[ cell_start:cell_end, tile_index - 1, ] tile_partition_t = tile_partitions_t[ cell_start:cell_end, tile_index, ] upstream_chunk_t = partition_upstream_grad_t[ cell_start:cell_end ] partition_difference_t = ( prior_partition_t - tile_partition_t ) partition_vjp_denominators_t = torch.stack( ( -partition_difference_t, partition_difference_t, ), dim=0, ).exp_().add_(1) partition_vjp_t = ( upstream_chunk_t.unsqueeze(0) / partition_vjp_denominators_t ) tile_partition_grad_t = partition_vjp_t[1] partition_upstream_grad_t[cell_start:cell_end].copy_( partition_vjp_t[0] ) partition_step_grad_t[ cell_start:cell_end, tile_index, ].copy_(tile_partition_grad_t) del ( partition_difference_t, partition_vjp_denominators_t, partition_vjp_t, tile_partition_grad_t, ) partition_step_grad_t[:, 0].copy_(partition_upstream_grad_t) # Recompute and release each vocabulary tile independently. No # full-vocabulary logits or autograd graph survives between tiles. position_context_fp32_t = position_context_t.detach().float() # The integrated parent is frozen during paged training, so its dense # baseline-logit input normally has no gradient request. Do not # allocate a full ``[dense_rows, vocab]`` gradient surface just to # discard it at the return boundary. The position context and # trainable low-rank head remain fully materialized and receive the # exact replayed Jacobian below. grad_position_context_fp32_t = ( torch.zeros_like(position_context_fp32_t) if needs_input_grad[0] else None ) grad_baseline_logits_rows_t = ( torch.zeros_like(baseline_logits_rows_t) if needs_input_grad[1] else None ) grad_low_rank_head_t = ( torch.zeros_like(low_rank_head_t) if needs_input_grad[4] else None ) vocab_tile_rows = cast(int, ctx.vocab_tile_rows) vocab_rows = baseline_logits_rows_t.shape[1] cell_count = position_context_t.shape[0] backward_cell_chunk_width = ( _factorized_completion_backward_cell_chunk_width_boundary( cell_count=cell_count, vocab_tile_rows=vocab_tile_rows, device=position_context_t.device, ) ) for reverse_tile_index in range(tile_count): tile_index = tile_count - reverse_tile_index - 1 vocab_start = tile_index * vocab_tile_rows vocab_end = min(vocab_rows, vocab_start + vocab_tile_rows) if backward_cell_chunk_width < cell_count: for cell_start in range( 0, cell_count, backward_cell_chunk_width, ): cell_end = min( cell_count, cell_start + backward_cell_chunk_width, ) position_context_chunk_t = ( position_context_t[cell_start:cell_end] .detach() .float() ) baseline_tile_t = ( baseline_logits_rows_t[:, vocab_start:vocab_end] .detach() ) low_rank_head_tile_t = ( low_rank_head_t[vocab_start:vocab_end] .detach() ) dense_row_index_chunk_t = dense_row_index_t[ cell_start:cell_end ] safe_targets_chunk_t = safe_targets_t[ cell_start:cell_end ] grad_nll_chunk_t = grad_nll_t[cell_start:cell_end] logits_tile_t = F.linear( position_context_chunk_t, low_rank_head_tile_t, ) logits_tile_t.add_( baseline_tile_t.index_select( 0, dense_row_index_chunk_t, ).float() ) # Forward already persisted this exact FP32 reduction for # the custom backward. Recomputing logsumexp here launched # one full vocabulary reduction per tile and accounted for # a material share of live r152 transaction time. Reuse the # sealed forward value; logits are still replayed below for # the exact position/head Jacobians. tile_partition_t = tile_partitions_t[ cell_start:cell_end, tile_index, ] target_in_tile_t = safe_targets_chunk_t.ge( vocab_start ) & safe_targets_chunk_t.lt(vocab_end) local_targets_t = ( safe_targets_chunk_t - vocab_start ).clamp( min=0, max=vocab_end - vocab_start - 1, ) # LogsumexpBackward is exactly # exp(logits - partition) * upstream. The replay owns the # detached logits buffer, so turn it into that gradient in # place. The target contributes at one vocabulary column # per cell; add it directly instead of allocating a zeros # surface and a second full-size sum surface. This keeps # the exact CE Jacobian while avoiding two cell-by-vocab # allocations in the moving full-corpus hot path. grad_logits_partition_t = logits_tile_t.sub_( tile_partition_t.unsqueeze(-1) ).exp_() grad_logits_partition_t.mul_( partition_step_grad_t[ cell_start:cell_end, tile_index, ].unsqueeze(-1) ) grad_logits_partition_t.scatter_add_( -1, local_targets_t.unsqueeze(-1), ( -grad_nll_chunk_t * target_in_tile_t.to( dtype=grad_nll_chunk_t.dtype ) ).unsqueeze(-1), ) grad_logits_tile_t = grad_logits_partition_t # The remaining graph is only # logits = baseline[index] + position @ head.T. # Apply that exact Jacobian directly instead of # launching a second autograd traversal per tile. grad_position_tile_t = grad_logits_tile_t.matmul( low_rank_head_tile_t ) grad_baseline_tile_t = ( torch.zeros_like(baseline_tile_t).index_add_( 0, dense_row_index_chunk_t, grad_logits_tile_t.to( dtype=baseline_tile_t.dtype ), ) if needs_input_grad[1] else None ) grad_low_rank_head_tile_t = ( grad_logits_tile_t.transpose(0, 1) .matmul(position_context_chunk_t) .to(dtype=low_rank_head_tile_t.dtype) if needs_input_grad[4] else None ) if needs_input_grad[0]: assert grad_position_context_fp32_t is not None grad_position_context_fp32_t[ cell_start:cell_end ].add_(grad_position_tile_t) if needs_input_grad[1]: assert ( grad_baseline_logits_rows_t is not None and grad_baseline_tile_t is not None ) grad_baseline_logits_rows_t[ :, vocab_start:vocab_end, ].add_(grad_baseline_tile_t) if needs_input_grad[4]: assert ( grad_low_rank_head_t is not None and grad_low_rank_head_tile_t is not None ) grad_low_rank_head_t[ vocab_start:vocab_end ].add_(grad_low_rank_head_tile_t) # Assignment evaluates its right-hand side before # rebinding the target. Without an explicit release, the # next tile's F.linear allocation overlaps every large # tensor from this tile even though no graph needs them. del ( logits_tile_t, tile_partition_t, grad_logits_partition_t, grad_logits_tile_t, grad_position_tile_t, grad_baseline_tile_t, grad_low_rank_head_tile_t, position_context_chunk_t, baseline_tile_t, low_rank_head_tile_t, ) continue baseline_tile_t = baseline_logits_rows_t[ :, vocab_start:vocab_end ].detach() low_rank_head_tile_t = low_rank_head_t[ vocab_start:vocab_end ].detach() logits_tile_t = F.linear( position_context_fp32_t, low_rank_head_tile_t, ) logits_tile_t.add_( baseline_tile_t.index_select( 0, dense_row_index_t, ).float() ) tile_partition_t = tile_partitions_t[:, tile_index] target_in_tile_t = safe_targets_t.ge( vocab_start ) & safe_targets_t.lt(vocab_end) local_targets_t = (safe_targets_t - vocab_start).clamp( min=0, max=vocab_end - vocab_start - 1, ) grad_logits_partition_t = logits_tile_t.sub_( tile_partition_t.unsqueeze(-1) ).exp_() grad_logits_partition_t.mul_( partition_step_grad_t[:, tile_index].unsqueeze(-1) ) grad_logits_partition_t.scatter_add_( -1, local_targets_t.unsqueeze(-1), ( -grad_nll_t * target_in_tile_t.to(dtype=grad_nll_t.dtype) ).unsqueeze(-1), ) grad_logits_tile_t = grad_logits_partition_t grad_position_tile_t = grad_logits_tile_t.matmul( low_rank_head_tile_t ) grad_baseline_tile_t = ( torch.zeros_like(baseline_tile_t).index_add_( 0, dense_row_index_t, grad_logits_tile_t.to(dtype=baseline_tile_t.dtype), ) if needs_input_grad[1] else None ) grad_low_rank_head_tile_t = ( grad_logits_tile_t.transpose(0, 1) .matmul(position_context_fp32_t) .to(dtype=low_rank_head_tile_t.dtype) if needs_input_grad[4] else None ) if needs_input_grad[0]: assert grad_position_context_fp32_t is not None grad_position_context_fp32_t.add_(grad_position_tile_t) if needs_input_grad[1]: assert ( grad_baseline_logits_rows_t is not None and grad_baseline_tile_t is not None ) grad_baseline_logits_rows_t[ :, vocab_start:vocab_end, ].copy_(grad_baseline_tile_t) if needs_input_grad[4]: assert ( grad_low_rank_head_t is not None and grad_low_rank_head_tile_t is not None ) grad_low_rank_head_t[vocab_start:vocab_end].copy_( grad_low_rank_head_tile_t ) del ( logits_tile_t, tile_partition_t, grad_logits_partition_t, grad_logits_tile_t, grad_position_tile_t, grad_baseline_tile_t, grad_low_rank_head_tile_t, baseline_tile_t, low_rank_head_tile_t, ) return ( ( grad_position_context_fp32_t.to(dtype=position_context_t.dtype) if grad_position_context_fp32_t is not None else None ) if needs_input_grad[0] else None, grad_baseline_logits_rows_t if needs_input_grad[1] else None, None, None, grad_low_rank_head_t if needs_input_grad[4] else None, None, ) @dataclass(frozen=True) class _CheckpointPagedRuntimeState: """Pre-forward mutable state of one attached paged NoNE runtime. ``torch.utils.checkpoint`` (``use_reentrant=False``) re-executes the wrapped forward during backward. A paged forward consults every field here; if any of them differs at the start of the recomputation, the recomputed graph diverges from the original — torch aborts with "A different number of tensors was saved during the original forward and recomputation" when the op count changes (the cohort refinement latch and the candidate-page staging cache both shorten the second pass), and silently recomputes different gate values (wrong gradients) when the op count happens to match (persistent quantile bias, cohort page mask). """ runtime: NoNEPagedExpertRuntime router_expert_bias_t: torch.Tensor router_quantile_step_state: QuantileBalancingStepSnapshot cohort_active_t: torch.Tensor cohort_page_mask_t: torch.Tensor cohort_boundary_open: bool cohort_refinement_ready: bool runtime_cohort_open: bool cohort_page_ids: tuple[int, ...] | None cohort_catalog_positions_t: torch.Tensor | None candidate_pages: dict[int, NoNEPageBundle] candidate_gradient_page_ids: set[int] gradient_page_forward_count_t: torch.Tensor candidate_route_count_t: torch.Tensor candidate_vjp_state: NoNECandidateVJPCheckpointPacket @dataclass(frozen=True) class _CheckpointForwardMutableState: """Model-owned mutable state the checkpointed forward mutates in flight.""" fabric_parent_route_uses: torch.Tensor science_step_count: torch.Tensor dense_router_expert_bias_t: tuple[torch.Tensor, ...] dense_router_quantile_step_state: tuple[ QuantileBalancingStepSnapshot, ..., ] anti_thompson_fail_counts_t: tuple[torch.Tensor, ...] paged_runtime_state: tuple[_CheckpointPagedRuntimeState, ...] @dataclass(frozen=True) class _PagedRuntimeReadOnlyBindingSnapshot: """Complete reversible runtime state around a staged store rebind.""" runtime: NoNEPagedExpertRuntime store: NoNEImmutablePageStore buffers: tuple[tuple[str, torch.Tensor], ...] accepted_inference_pages: dict[int, Any] accepted_inference_compositions: dict[tuple[int, ...], Any] accepted_inference_generation_t: torch.Tensor | None accepted_inference_device: torch.device | None accepted_inference_dtype: torch.dtype | None last_request: Any last_weights: Any last_bundle: Any class ResynthesisRBO(nn.Module): """Recursive Bidirectional Orchestrator for the Resynthesis reasoning-science model. The RBO treats the frozen integrated parent as vocabulary/feature plumbing inside a much larger additive science graph and performs recursive traversal with confidence-based stopping. Parent logits, native answer selection, completion confidence, and parent-relative retention have no execution authority. Answer logits and stopping are owned by trained additive NoNE/RBO/Fabric tensors. It does NOT own routing decisions via host flags — routing is model-owned (science stack router + domain registry + knowledge transfer surfaces). Pillar 17: ``forward_thinking`` never receives targets. CE on head logits only. """ additive_checkpoint_loaded: torch.Tensor candidate_authority_update_proven: torch.Tensor candidate_authority_retention_verified: torch.Tensor candidate_page_update_proven: torch.Tensor completion_successor_authority_trained: torch.Tensor completion_successor_retention_passed: torch.Tensor completion_successor_candidate_evaluation_active: torch.Tensor completion_successor_gradient_update_count: torch.Tensor _stop_reason_codes_t: torch.Tensor legacy_capability_bank: nn.Module | None _bilevel_observation: torch.Tensor def __init__( self, base: Any, science_stack: ResynthesisScienceLayerStack | None = None, cfg: ResynthesisRBOConfig | None = None, model_cfg: ResynthesisConfig | None = None, fabric: ResynthesisNoNEFabric | None = None, ) -> None: super().__init__() self.base = base self.model_cfg = model_cfg or ResynthesisConfig() self.cfg = cfg or ResynthesisRBOConfig() self.science_stack = science_stack or build_resynthesis_science_stack( ResynthesisScienceLayerConfig( hidden_size=self.model_cfg.hidden_size, knowledge_transfer_dim=( self.model_cfg.knowledge_transfer_dim ), ) ) # The old stack-local "KL anchor" preserved each layer's input. At the # first additive layer that input is the hollow frozen parent, so a # nonzero value silently restores the parent as retention authority. # Retention is now owned exclusively by accepted-additive checkpoint # comparison at the learning boundary. Force this legacy scalar off # even when a historical constructor supplies the old source default; # it is not checkpoint tensor state and therefore cannot be restored by # a cold load. self.science_stack.kl_anchor_weight = 0.0 self.stop_gate = NeuralStopGate( hidden_size=self.model_cfg.hidden_size, trajectory_dim=self.cfg.feedback_hidden_size, ) # Resynthesis does NOT tie word embeddings (tie_word_embeddings: false), so # the immutable lm_head remains a separate shared vocabulary projection. # It projects the *trained additive hidden state*; frozen-parent logits # are retained only in diagnostic fields and are never blended back # into the answer. self.feedback_head = nn.Linear( self.model_cfg.hidden_size, self.cfg.feedback_hidden_size ) self.prior_hidden_proj = nn.Linear( self.model_cfg.hidden_size, self.cfg.feedback_hidden_size, bias=False ) self.outcome_encoder = nn.Linear(8, self.cfg.feedback_hidden_size, bias=False) self.parent_outcome_encoder = nn.Linear( 5, self.cfg.feedback_hidden_size, bias=False ) self.acquisition_encoder = nn.Linear( 4, self.cfg.feedback_hidden_size, bias=False ) self.acquisition_policy = LearnedEvidenceAcquisitionPolicy( hidden_size=max(32, self.cfg.feedback_hidden_size // 2) ) self.correction_context_norm = nn.LayerNorm(self.cfg.feedback_hidden_size) self.correction_hidden_up = nn.Linear( self.cfg.feedback_hidden_size, self.model_cfg.hidden_size, bias=False ) self.correction_trigger_head = nn.Linear(self.cfg.feedback_hidden_size, 1) self.task_confidence_head = nn.Linear(self.cfg.feedback_hidden_size, 1) self.nla_confidence_scale = nn.Parameter( torch.zeros(self.cfg.feedback_hidden_size) ) self.delegation_head = nn.Linear(self.cfg.feedback_hidden_size, 1) self.correction_expert_head = nn.Linear( self.cfg.feedback_hidden_size, self.science_stack.num_experts, bias=False, ) self.correction_layer_head = nn.Linear( self.cfg.feedback_hidden_size, self.science_stack.num_layers, bias=False, ) # Additive answer residual: hidden → accepted rank → hidden. ``None`` # derives its initial rank from the current learned transfer bank, not # from a fixed host constant or the hollow parent's presumed capacity. # An explicit value is an initial materialized geometry only. The v27 # checkpoint adapter may widen it while preserving every inherited # row/column exactly; no rank value is a production maximum. configured_logit_residual_rank = self.model_cfg.logit_residual_rank if configured_logit_residual_rank is None: configured_logit_residual_rank = int( self.science_stack.capability_integration .knowledge_transfer.transfer_bank.transfer_dim ) if ( type(configured_logit_residual_rank) is not int or configured_logit_residual_rank < 1 ): raise ValueError( "additive logit-residual rank must be a positive integer" ) self.logit_residual_rank = configured_logit_residual_rank self.logit_residual_down = nn.Linear( self.model_cfg.hidden_size, self.logit_residual_rank, bias=False ) self.logit_residual_up = nn.Linear( self.logit_residual_rank, self.model_cfg.hidden_size, bias=False ) projection_vocab_size = self.model_cfg.vocab_size configured_lexical_vocab_size = self.model_cfg.tokenizer_vocab_size configured_tokenizer_prefix_vocab_size = ( self.model_cfg.tokenizer_prefix_vocab_size ) configured_vocabulary_transfer_rank = ( self.model_cfg.vocabulary_transfer_rank ) if ( type(projection_vocab_size) is not int or type(configured_lexical_vocab_size) is not int or type(configured_tokenizer_prefix_vocab_size) is not int or projection_vocab_size < 1 or configured_lexical_vocab_size < 1 or configured_tokenizer_prefix_vocab_size < 1 or configured_tokenizer_prefix_vocab_size > configured_lexical_vocab_size ): raise ValueError( "Resynthesis projection, lexical, and inherited-prefix " "vocabulary geometry differs" ) self.lexical_vocab_size = configured_lexical_vocab_size self.tokenizer_prefix_vocab_size = ( configured_tokenizer_prefix_vocab_size ) # These are the inherited projection rows that had no lexical identity # at the first Resynthesis vocabulary-transfer generation. Keep this # source geometry stable as the tokenizer appends IDs: consuming a row # as a new direct token must not shrink or reorder accepted transfer # weights. The active undecodable-row count remains a tokenizer receipt # diagnostic, not checkpoint tensor geometry. self.projection_only_vocab_rows = ( projection_vocab_size - min( projection_vocab_size, self.tokenizer_prefix_vocab_size, ) ) if self.projection_only_vocab_rows: if ( type(configured_vocabulary_transfer_rank) is not int or configured_vocabulary_transfer_rank < 1 ): raise ValueError( "Resynthesis vocabulary-transfer rank must be a positive " "integer" ) self.vocabulary_transfer_rank = ( configured_vocabulary_transfer_rank ) self.vocabulary_transfer_down: nn.Linear | None = nn.Linear( self.projection_only_vocab_rows, self.vocabulary_transfer_rank, bias=False, ) self.vocabulary_transfer_up: nn.Linear | None = nn.Linear( self.vocabulary_transfer_rank, self.lexical_vocab_size, bias=False, ) with torch.no_grad(): self.vocabulary_transfer_down.weight.copy_( _deterministic_xavier_tensor( self.vocabulary_transfer_down.weight, tuple(self.vocabulary_transfer_down.weight.shape), "resynthesis_vocabulary_transfer_down_v1", ) ) # Identity at migration: inherited lexical logits remain # byte-exact until CE teaches how each projection-only # coordinate transfers into the lexical surface. self.vocabulary_transfer_up.weight.zero_() else: if self.lexical_vocab_size > projection_vocab_size: raise ValueError( "Resynthesis lexical growth beyond projection requires " "an inherited transfer source" ) self.vocabulary_transfer_rank = 0 self.vocabulary_transfer_down = None self.vocabulary_transfer_up = None self.fabric = fabric or ResynthesisNoNEFabric( hidden_size=self.model_cfg.hidden_size, num_experts=self.science_stack.num_experts, num_layers=self.science_stack.num_layers, glyph_dim=self.model_cfg.glyph_input_dim, ) self.add_module("legacy_capability_bank", None) self.register_buffer( "additive_checkpoint_loaded", torch.zeros((), dtype=torch.bool), persistent=True, ) self.register_buffer( "candidate_authority_update_proven", torch.zeros((), dtype=torch.bool), persistent=True, ) self.register_buffer( "candidate_authority_retention_verified", torch.zeros((), dtype=torch.bool), persistent=True, ) self.register_buffer( "candidate_page_update_proven", torch.zeros((), dtype=torch.bool), persistent=False, ) self.register_buffer( "completion_successor_authority_trained", torch.zeros((), dtype=torch.bool), persistent=True, ) self.register_buffer( "completion_successor_retention_passed", torch.zeros((), dtype=torch.bool), persistent=True, ) self.register_buffer( "completion_successor_candidate_evaluation_active", torch.zeros((), dtype=torch.bool), persistent=False, ) self.register_buffer( "completion_successor_gradient_update_count", torch.zeros((), dtype=torch.long), persistent=True, ) self.register_buffer( "_stop_reason_codes_t", torch.tensor( ( STOP_REASON_REASONING_READY, STOP_REASON_CONTRADICTION_RESOLVED, STOP_REASON_CONFIDENCE_GATE, ), dtype=torch.long, ), persistent=False, ) self._paged_none_store_boundary: Any = None self._paged_none_composition_path: Path | None = None self._paged_none_migration_receipt_path: Path | None = None self._paged_none_replica_receipt_path: Path | None = None self._paged_none_composition_sha256 = "" self._paged_none_source_checkpoint_sha256 = "" self._paged_none_replica_coordinator_boundary: Any = None self._paged_none_replica_receipt_sha256 = "" self._paged_sparse_deferred_parameters: tuple[nn.Parameter, ...] = () self._paged_sparse_zero_defer_active = False self._fast_release_training_active = False self._paged_none_family_root_count = 0 # Page catalogs are immutable for a loaded composition. Keep the # device-local union so the CUDA wave planner does not launch a full # ``torch.unique`` over every physical page on every transaction. # This is derived observation only; it is intentionally not a # checkpointed buffer and is rebuilt when the model moves devices or a # new composition is attached. self._paged_none_catalog_page_ids_t_cache: torch.Tensor | None = None self._paged_none_functional_root_plan_present = False self._paged_none_planned_functional_family_root_count = 0 self._paged_none_pending_functional_family_root_count = 0 self._paged_none_family_root_page_ids_t_boundary = torch.empty( 0, dtype=torch.long, ) self._paged_none_training_page_count = 0 self._paged_none_global_training_page_count = 0 self._paged_none_training_family_root_count = 0 self._paged_none_training_branch_scope: ( NoNETrainingBranchScopePacket | None ) = None owner_index = self.cfg.training_branch_functional_owner_index owner_count = self.cfg.training_branch_functional_owner_count if (owner_index is None) != (owner_count is None): raise ValueError( "functional graph branch ownership configuration is incomplete" ) if owner_index is not None and ( type(owner_index) is not int or type(owner_count) is not int or owner_count != len(BRANCH_FUNCTIONAL_GRAPH_OWNER_ROLES) or owner_index < 0 or owner_index >= owner_count ): raise ValueError( "functional graph branch ownership configuration differs" ) self._training_branch_functional_owner_index = owner_index self._training_branch_functional_owner_count = owner_count self._training_branch_functional_graph_active = False self._page_branch_functional_graph_parameter_names: frozenset[str] = ( frozenset() ) self._page_branch_functional_graph_parameter_geometry: dict[ str, tuple[tuple[int, ...], torch.dtype], ] = {} self._page_branch_functional_graph_ownership: dict[str, Any] | None = ( None ) self._page_branch_functional_graph_parent_genesis: ( dict[str, Any] | None ) = None self._page_branch_functional_graph_parent_genesis_authority: ( dict[str, Any] | None ) = None self._paged_none_canonical_checkpoint_paged_lineage: ( dict[str, Any] | None ) = None self._page_branch_parent_checkpoint_path: Path | None = None self._page_branch_parent_checkpoint_sha256 = "" self._page_branch_parent_checkpoint_lineage: dict[str, Any] | None = None self._page_branch_parent_state_key_sha256 = "" self._page_branch_parent_state_geometry_sha256 = "" self._page_branch_parent_buffer_key_sha256 = "" self._page_branch_parent_buffer_geometry_sha256 = "" self._page_branch_parent_buffer_geometry: dict[ str, tuple[tuple[int, ...], torch.dtype], ] = {} self._page_branch_parent_causal_parameter_key_sha256 = "" self._page_branch_parent_causal_parameter_geometry_sha256 = "" self._page_branch_parent_causal_parameter_value_sha256 = "" self._page_branch_parent_causal_parameter_authority_mode = "" self._page_branch_parent_causal_parameter_presence_count = -1 self._page_branch_parent_causal_parameter_geometry: dict[ str, tuple[tuple[int, ...], torch.dtype], ] = {} self._page_branch_parent_causal_parameter_initialization = "" self._page_branch_parent_causal_parameters: dict[ str, torch.Tensor, ] = {} self._page_branch_parent_causal_buffer_key_sha256 = "" self._page_branch_parent_causal_buffer_geometry_sha256 = "" self._page_branch_parent_causal_buffer_value_sha256 = "" self._page_branch_parent_causal_buffer_geometry: dict[ str, tuple[tuple[int, ...], torch.dtype], ] = {} self._page_branch_parent_causal_buffers: dict[ str, torch.Tensor, ] = {} self._page_branch_parent_causal_delta_buffer_key_sha256 = "" self._page_branch_parent_causal_delta_buffer_geometry_sha256 = "" self._page_branch_parent_causal_delta_buffer_geometry: dict[ str, tuple[tuple[int, ...], torch.dtype], ] = {} self._page_branch_parent_excluded_default_buffers: dict[ str, torch.Tensor, ] = {} self._paged_none_objective_page_count = 0 self._paged_none_planned_objective_page_count = 0 self._paged_none_pending_objective_page_count = 0 self._paged_none_objective_page_plan_sha256 = "" self._paged_none_compact_bank_authority_present = False self._paged_none_compact_page_bank_count = 0 self._paged_none_admitted_compact_page_count = 0 self._paged_none_physical_untrained_bank_capacity_parameter_elements = 0 self._paged_none_full_physical_page_bank_traversal_required = False self._paged_none_reasoning_growth_plan_sha256 = "" self._paged_none_sparse_graph_layer_count: int | None = None self._paged_none_sparse_graph_layer_ids_sha256 = "" self._paged_none_sparse_graph_layer_catalog_sha256 = "" self._active_graph_growth_plan_path: Path | None = None self._active_graph_growth_plan_sha256 = "" self._graph_adaptation_receipt_path: Path | None = None self._paged_none_reasoning_source_layer_count = ( self.science_stack.num_layers ) self._paged_none_expected_layer_ids_boundary: tuple[int, ...] = () self._paged_none_expected_layer_catalog_ids_t_boundary: tuple[ torch.Tensor, ... ] = () self._staged_none_generation_binding: Any = None self._reconciled_staged_direct_map_seal_t: torch.Tensor | None = None self._candidate_layer_import_source_stores: tuple[Any, ...] = () self._candidate_layer_import_packets: tuple[Any, ...] = () self._inherited_graph_growth_record: dict[str, Any] | None = None # Deliberately not a Parameter or buffer: this is a durable outer-loop # observation rebound from its receipt after restart, not learned state. # The trained outcome/correction heads that consume it remain canonical # checkpoint tensors, preserving exact compatibility with prior snapshots. self._bilevel_observation = torch.zeros(1, 8, dtype=torch.float32) # Frozen-parent prefill memoization (not learned state, never checkpointed). # Created lazily in forward_thinking only when the cache boundary is enabled; # it stores detached CPU copies of the frozen base forward keyed by an exact # content digest so repeated batched-prefill inputs (iteration-0 warmup, # cold-eval, multi-epoch) skip the frozen 4M tiled parent forward. The frozen # base is byte-identical for a given input across all steps, and the additive # science stack consumes only these output tensors, so reuse is semantics-free. self._base_forward_cache: BaseForwardCache | None = None if bool(getattr(self.base, "_weights_loaded", False)): self.ensure_parent_capability_attachment() @property def hidden_size(self) -> int: return self.model_cfg.hidden_size @property def vocab_size(self) -> int: return self.model_cfg.vocab_size def map_projection_logits_to_lexical_vocabulary( self, projection_logits: torch.Tensor, ) -> torch.Tensor: """Transfer every physical projection coordinate into lexical logits. The immutable tokenizer prefix owns the matching prefix of the physical projection. Its inherited projection-only suffix is the permanent learned transfer source even after successor tokenizers append IDs. Appended lexical rows beyond the physical projection therefore receive only model-owned transferred evidence; they are never zero padded, host-reranked, or rewritten after selection. """ if ( not projection_logits.is_floating_point() or projection_logits.ndim < 2 or projection_logits.shape[-1] != self.vocab_size ): raise ValueError( "Resynthesis projection-logit vocabulary geometry differs" ) physical_lexical_rows = min( self.lexical_vocab_size, self.vocab_size, ) physical_lexical_logits = projection_logits.narrow( -1, 0, physical_lexical_rows, ) if not self.projection_only_vocab_rows: return physical_lexical_logits transfer_down = self.vocabulary_transfer_down transfer_up = self.vocabulary_transfer_up if ( not isinstance(transfer_down, nn.Linear) or not isinstance(transfer_up, nn.Linear) or transfer_down.weight.device != projection_logits.device or transfer_up.weight.device != projection_logits.device ): raise RuntimeError( "Resynthesis vocabulary-transfer tensors are unavailable" ) transfer_source_start = ( self.vocab_size - self.projection_only_vocab_rows ) projection_only_logits = projection_logits.narrow( -1, transfer_source_start, self.projection_only_vocab_rows, ) transferred_logits: torch.Tensor = transfer_up( transfer_down( projection_only_logits.to( dtype=transfer_down.weight.dtype, ) ) ).to(dtype=physical_lexical_logits.dtype) transferred_physical_logits = transferred_logits.narrow( -1, 0, physical_lexical_rows, ) lexical_prefix_logits: torch.Tensor = ( physical_lexical_logits + transferred_physical_logits ) appended_lexical_rows = ( self.lexical_vocab_size - physical_lexical_rows ) if not appended_lexical_rows: return lexical_prefix_logits appended_lexical_logits = transferred_logits.narrow( -1, physical_lexical_rows, appended_lexical_rows, ) return torch.cat( (lexical_prefix_logits, appended_lexical_logits), dim=-1, ) def attach_paged_none_composition_boundary( self, composition_path: str | Path, migration_receipt_path: str | Path, replica_receipt_path: str | Path | None = None, *, training_authority: NoNETrainingAuthority | None = None, inference_authority: ReleaseInferenceAuthority | None = None, training_branch_scope: NoNETrainingBranchScopePacket | None = None, ) -> torch.Tensor: """Attach transferred v2 pages as modules inside this RBO graph.""" if training_authority is not None and inference_authority is not None: raise RuntimeError( "training and public inference attachments are mutually exclusive" ) # A composition replacement can change the physical page catalog. # Drop the derived device-local union before any new runtime is bound. self._paged_none_catalog_page_ids_t_cache = None from resynthesis.none_migration import ( load_v2_resident_layer_state, validate_v2_seed_composition, ) from resynthesis.none_paging import ( NoNEGenerationReplicaCoordinator, NoNEGraphAuthorityBinding, NoNEImmutablePageStore, NoNEPagedExpertRuntime, NoNETrainingBranchScopePacket, digest_tensor, page_model_parameter_elements, sparse_graph_layer_binding_record_boundary, ) resolved_composition_path = Path(composition_path).resolve() resolved_receipt_path = Path(migration_receipt_path).resolve() if inference_authority is not None: if ( inference_authority.generation_artifacts.composition.path.resolve() != resolved_composition_path or inference_authority.generation_artifacts.migration.path.resolve() != resolved_receipt_path ): raise RuntimeError( "prevalidated public inference attachment differs" ) loaded_composition = json.loads( resolved_composition_path.read_text(encoding="utf-8") ) if not isinstance(loaded_composition, dict): raise RuntimeError("public inference composition is invalid") composition = loaded_composition elif training_authority is None: composition = validate_v2_seed_composition( resolved_composition_path, resolved_receipt_path, ) else: if ( training_authority.composition_path is None or training_authority.migration_receipt_path is None or training_authority.composition_path.resolve() != resolved_composition_path or training_authority.migration_receipt_path.resolve() != resolved_receipt_path ): raise RuntimeError("prevalidated NoNE attachment authority differs") loaded_composition = json.loads( resolved_composition_path.read_text(encoding="utf-8") ) if not isinstance(loaded_composition, dict): raise RuntimeError("prevalidated NoNE composition is invalid") composition = loaded_composition catalog_record = composition.get("pageCatalog") resident_record = composition.get("residentRuntime") store_record = composition.get("pageStore") if not all( isinstance(record, dict) for record in ( catalog_record, resident_record, store_record, ) ): raise RuntimeError("NoNE v2 composition binding is incomplete") assert isinstance(catalog_record, dict) assert isinstance(resident_record, dict) assert isinstance(store_record, dict) seed_pointer = store_record.get("acceptedPointer") if not isinstance(seed_pointer, dict): raise RuntimeError("NoNE v2 composition has no seed pointer") catalog_path = ( inference_authority.generation_artifacts.page_catalog.path.resolve() if inference_authority is not None else Path(str(catalog_record["path"])).expanduser().resolve() ) catalog_sha256 = catalog_record.get("sha256") if ( not catalog_path.is_file() or not isinstance(catalog_sha256, str) or len(catalog_sha256) != 64 or ( inference_authority is not None and inference_authority.generation_artifacts.page_catalog.sha256 != catalog_sha256 ) or _file_sha256_boundary(catalog_path) != catalog_sha256 ): raise RuntimeError("NoNE v2 page catalog identity differs") catalog = json.loads(catalog_path.read_text(encoding="utf-8")) if not isinstance(catalog, dict): raise RuntimeError("NoNE v2 page catalog payload is invalid") sparse_graph_layer_record = sparse_graph_layer_binding_record_boundary( catalog, page_catalog_sha256=catalog_sha256, ) if sparse_graph_layer_record is None: if "sparseGraphLayers" in composition: raise RuntimeError( "NoNE legacy composition has sparse graph-layer authority" ) elif composition.get("sparseGraphLayers") != sparse_graph_layer_record: raise RuntimeError("NoNE sparse graph-layer composition differs") geometry = catalog.get("pageGeometry") layer_catalog = catalog.get("layerCatalogPageIds") page_rows = catalog.get("pages") session_values = store_record.get("sessionId") if ( not isinstance(geometry, dict) or not isinstance(layer_catalog, dict) or not isinstance(page_rows, list) or not isinstance(session_values, list) ): raise RuntimeError("NoNE v2 page geometry binding is incomplete") family_root_count = int(catalog.get("familyRootPageCount", 0)) functional_root_plan_present = ( "plannedFunctionalFamilyRootCount" in catalog or "pendingFunctionalFamilyRootCount" in catalog ) planned_functional_family_root_count = int( catalog.get("plannedFunctionalFamilyRootCount", family_root_count) ) pending_functional_family_root_count = int( catalog.get( "pendingFunctionalFamilyRootCount", planned_functional_family_root_count - family_root_count, ) ) objective_page_count = int(catalog.get("objectivePageCount", 0)) planned_objective_page_count = int( catalog.get("plannedObjectivePageCount", objective_page_count) ) pending_objective_page_count = int( catalog.get( "pendingObjectivePageCount", planned_objective_page_count - objective_page_count, ) ) objective_page_plan_sha256 = str( catalog.get("objectivePagePlanSha256", "") ) compact_bank_authority_present = ( "compactPageBanks" in catalog or "compactPageBanks" in composition ) compact_page_bank_count = 0 admitted_compact_page_count = 0 physical_untrained_bank_capacity_parameter_elements = 0 full_physical_page_bank_traversal_required = False if compact_bank_authority_present: compact_page_banks = catalog.get("compactPageBanks") composition_compact_page_banks = composition.get("compactPageBanks") compact_page_bank_count_value = catalog.get("compactPageBankCount") admitted_compact_page_count_value = catalog.get( "admittedCompactPageCount" ) physical_untrained_bank_capacity_value = catalog.get( "physicalUntrainedBankCapacityParameterElements" ) if ( not isinstance(compact_page_banks, list) or composition_compact_page_banks != compact_page_banks or type(compact_page_bank_count_value) is not int or compact_page_bank_count_value != len(compact_page_banks) or type(admitted_compact_page_count_value) is not int or admitted_compact_page_count_value < 0 or type(physical_untrained_bank_capacity_value) is not int or physical_untrained_bank_capacity_value < 0 ): raise RuntimeError("NoNE compact-page bank authority differs") compact_page_bank_count = compact_page_bank_count_value admitted_compact_page_count = admitted_compact_page_count_value physical_untrained_bank_capacity_parameter_elements = ( physical_untrained_bank_capacity_value ) complete_bank_page_count = 0 every_compact_bank_fully_admitted = bool(compact_page_banks) for compact_bank in compact_page_banks: if not isinstance(compact_bank, dict): raise RuntimeError("NoNE compact-page bank authority differs") bank_page_count = compact_bank.get("bankPageCount") admitted_page_count = compact_bank.get("admittedPageCount") admitted_page_ids = compact_bank.get("admittedPageIds") if ( not isinstance(bank_page_count, int) or isinstance(bank_page_count, bool) or bank_page_count < 1 or not isinstance(admitted_page_count, int) or isinstance(admitted_page_count, bool) or admitted_page_count < 0 or not isinstance(admitted_page_ids, list) or len(admitted_page_ids) != admitted_page_count or any( not isinstance(page_id, int) or isinstance(page_id, bool) for page_id in admitted_page_ids ) or len(set(admitted_page_ids)) != admitted_page_count ): raise RuntimeError("NoNE compact-page bank authority differs") complete_bank_page_count += bank_page_count every_compact_bank_fully_admitted = bool( every_compact_bank_fully_admitted and admitted_page_count == bank_page_count ) full_physical_page_bank_traversal_required = bool( every_compact_bank_fully_admitted and admitted_compact_page_count == complete_bank_page_count and pending_functional_family_root_count == 0 and pending_objective_page_count == 0 and physical_untrained_bank_capacity_parameter_elements > 0 ) family_root_page_ids = { int(row["pageId"]) for row in page_rows if isinstance(row, dict) and isinstance(row.get("pageId"), int) and not isinstance(row.get("pageId"), bool) and isinstance(row.get("family"), str) and bool(str(row["family"]).strip()) and ( row.get("familyRoot") is True or row.get("familyRoot") is None ) } catalog_untrained_page_ids = { int(row["pageId"]) for row in page_rows if isinstance(row, dict) and isinstance(row.get("pageId"), int) and not isinstance(row.get("pageId"), bool) and row.get("trainedCapabilityClaimed") is False } catalog_trained_page_ids = { int(row["pageId"]) for row in page_rows if isinstance(row, dict) and isinstance(row.get("pageId"), int) and not isinstance(row.get("pageId"), bool) and row.get("trainedCapabilityClaimed") is True } catalog_page_ids = { int(row["pageId"]) for row in page_rows if isinstance(row, dict) and isinstance(row.get("pageId"), int) and not isinstance(row.get("pageId"), bool) } if ( family_root_count < 1 or planned_functional_family_root_count < family_root_count or pending_functional_family_root_count != planned_functional_family_root_count - family_root_count or objective_page_count < 0 or planned_objective_page_count < objective_page_count or pending_objective_page_count != planned_objective_page_count - objective_page_count or ( objective_page_plan_sha256 and len(objective_page_plan_sha256) != 64 ) or len(family_root_page_ids) != family_root_count or len(catalog_page_ids) != len(page_rows) or catalog_untrained_page_ids & catalog_trained_page_ids or catalog_untrained_page_ids | catalog_trained_page_ids != catalog_page_ids or not family_root_page_ids.issubset(catalog_page_ids) ): raise RuntimeError("NoNE v2 trained/untrained catalog proof differs") active_functional_family_count = int( self.science_stack._layer(0).expert_capability_proj.out_features ) if active_functional_family_count < family_root_count: raise RuntimeError( "NoNE physical family roots exceed the active functional catalog" ) if active_functional_family_count > planned_functional_family_root_count: # The source catalog can grow while an accepted immutable page # generation remains unchanged. Expose that delta as pending # physical roots; never let an older plan report zero pending roots # for newly registered expert families. planned_functional_family_root_count = active_functional_family_count pending_functional_family_root_count = ( active_functional_family_count - family_root_count ) functional_root_plan_present = True hidden_size = int(geometry.get("hiddenSize", 0)) action_size = int(geometry.get("actionSize", 0)) router_size = int(geometry.get("routerSize", 0)) expert_hidden_size = int(geometry.get("expertHiddenSize", 0)) glyph_size = int(geometry.get("glyphSize", 0)) layer_ids: list[int] = [] for raw_layer_id in layer_catalog: try: layer_id = int(raw_layer_id) except (TypeError, ValueError) as exc: raise RuntimeError( "NoNE v2 layer page catalog identity is malformed" ) from exc if str(layer_id) != str(raw_layer_id) or layer_id < 0: raise RuntimeError( "NoNE v2 layer page catalog identity is malformed" ) layer_ids.append(layer_id) layer_ids.sort() if inference_authority is not None: target_reasoning_layers = ( inference_authority.topology.layer_count ) growth_plan_sha256 = ( inference_authority.generation_artifacts.growth_plan.sha256 ) else: target_reasoning_layers, growth_plan_sha256 = ( _paged_reasoning_layer_target_boundary( Path(migration_receipt_path).expanduser().resolve(), # The resident traversal engine can already have grown # beyond the subset of layers that own paged runtimes. # Resolve the dense target from the actual loaded stack; # ``layer_ids`` remains the independently validated # attachment topology. source_layers=self.science_stack.num_layers, source_experts=self.science_stack.num_experts, target_growth_plan_path=( Path(self.cfg.paged_none_target_growth_plan_path) if self.cfg.paged_none_target_growth_plan_path is not None else None ), expected_target_growth_plan_sha256=( self.cfg.paged_none_target_growth_plan_sha256 ), ) ) if ( hidden_size != self.hidden_size or action_size != self.science_stack.cfg.action_input_dim or router_size < 1 or not layer_ids or len(set(layer_ids)) != len(layer_ids) or layer_ids[-1] >= self.science_stack.num_layers or self.science_stack.num_layers not in {len(layer_ids), target_reasoning_layers} ): raise RuntimeError("NoNE v2 resident geometry differs from RBO") page_parameter_elements = page_model_parameter_elements( hidden_size=hidden_size, expert_hidden_size=expert_hidden_size, glyph_size=glyph_size, router_size=router_size, ) reference = next(self.science_stack.parameters()) session_id_t = torch.tensor(session_values, dtype=torch.long) coordinator = None if inference_authority is not None: if tuple(session_values) != inference_authority.topology.session_id: raise RuntimeError( "public release session identity differs from composition" ) pointer = json.loads( inference_authority.generation_artifacts.accepted_pointer.path.read_text( encoding="utf-8" ) ) graph_record = ( pointer.get("graphAuthority") if isinstance(pointer, dict) else None ) if not isinstance(graph_record, dict): raise RuntimeError("public release graph authority is absent") def graph_artifact(name: str) -> tuple[str, str]: artifact = graph_record.get(name) if ( not isinstance(artifact, dict) or not isinstance(artifact.get("path"), str) or not isinstance(artifact.get("sha256"), str) or len(artifact["sha256"]) != 64 ): raise RuntimeError( f"public release graph {name} is malformed" ) return str(artifact["path"]), str(artifact["sha256"]) _checkpoint_coordinate, checkpoint_sha256 = graph_artifact( "checkpoint" ) optimizer_coordinate, optimizer_sha256 = graph_artifact( "optimizer" ) _external_coordinate, external_sha256 = graph_artifact( "externalState" ) _composition_coordinate, composition_sha256 = graph_artifact( "composition" ) _catalog_coordinate, graph_catalog_sha256 = graph_artifact( "pageCatalog" ) _resident_coordinate, graph_resident_sha256 = graph_artifact( "residentRuntime" ) topology = graph_record.get("topology") replica_record = graph_record.get("replicaReceipt") if ( not isinstance(topology, dict) or checkpoint_sha256 != inference_authority.weights.checkpoint.sha256 or external_sha256 != inference_authority.generation_artifacts.external_state.sha256 or composition_sha256 != inference_authority.generation_artifacts.composition.sha256 or graph_catalog_sha256 != inference_authority.generation_artifacts.page_catalog.sha256 or graph_resident_sha256 != inference_authority.weights.resident_runtime.sha256 ): raise RuntimeError("public release graph artifact identity differs") graph_authority = NoNEGraphAuthorityBinding( checkpoint_path=str( inference_authority.weights.checkpoint.path ), checkpoint_sha256_t=digest_tensor(checkpoint_sha256), optimizer_path=optimizer_coordinate, optimizer_sha256_t=digest_tensor(optimizer_sha256), external_state_path=str( inference_authority.generation_artifacts.external_state.path ), external_state_sha256_t=digest_tensor(external_sha256), composition_path=str( inference_authority.generation_artifacts.composition.path ), composition_sha256_t=digest_tensor(composition_sha256), page_catalog_path=str( inference_authority.generation_artifacts.page_catalog.path ), page_catalog_sha256_t=digest_tensor(graph_catalog_sha256), resident_runtime_path=str( inference_authority.weights.resident_runtime.path ), resident_runtime_sha256_t=digest_tensor( graph_resident_sha256 ), migration_receipt_path=str( inference_authority.generation_artifacts.migration.path ), replica_receipt_path=( str(replica_record["path"]) if isinstance(replica_record, dict) and isinstance(replica_record.get("path"), str) else None ), replica_receipt_sha256_t=( digest_tensor(str(replica_record["sha256"])) if isinstance(replica_record, dict) and isinstance(replica_record.get("sha256"), str) else None ), layer_count_t=torch.tensor( int(topology.get("layers", 0)), dtype=torch.long, ), family_root_count_t=torch.tensor( int(topology.get("familyRoots", 0)), dtype=torch.long, ), page_count_t=torch.tensor( int(topology.get("pages", 0)), dtype=torch.long, ), physical_graph_layer_count_t=( torch.tensor( int(topology["physicalGraphLayers"]), dtype=torch.long, ) if isinstance(topology.get("physicalGraphLayers"), int) else None ), federated_growth_demand_authority_sha256_t=( digest_tensor( str( graph_record[ "federatedGrowthDemandAuthoritySha256" ] ) ) if isinstance( graph_record.get( "federatedGrowthDemandAuthoritySha256" ), str, ) else None ), ) cache_root = ( Path( os.environ.get( "XDG_CACHE_HOME", str(Path.home() / ".cache"), ) ) / "nucleus-resynthesis" / inference_authority.session_key ) store = NoNEImmutablePageStore.open_release_generation_boundary( cache_root=cache_root, accepted_pointer_path=( inference_authority.generation_artifacts.accepted_pointer.path ), generation_manifest_path=( inference_authority.generation_artifacts.manifest.path ), direct_index_path=( inference_authority.weights.direct_page_index.path ), direct_pack_path=inference_authority.weights.direct_pages.path, graph_authority=graph_authority, expected_generation=inference_authority.generation, expected_manifest_sha256=( inference_authority.generation_artifacts.manifest.sha256 ), expected_manifest_payload_sha256=( inference_authority.generation_artifacts.manifest_payload_sha256 ), ) else: anchor_store = NoNEImmutablePageStore( Path(str(store_record["root"])) ) if replica_receipt_path is not None: if inference_authority is not None: raise RuntimeError( "public release inference cannot activate replicas" ) seed_manifest_payload_sha256 = seed_pointer.get( "manifestPayloadSha256" ) if ( not isinstance(seed_manifest_payload_sha256, str) or len(seed_manifest_payload_sha256) != 64 ): raise RuntimeError("NoNE v2 seed pointer digest is malformed") coordinator = NoNEGenerationReplicaCoordinator.from_receipt_boundary( primary_store=anchor_store, session_id_t=session_id_t, receipt_path=Path(replica_receipt_path), seed_manifest_payload_sha256=seed_manifest_payload_sha256, expected_pointer=seed_pointer, ) store = coordinator.primary_store elif inference_authority is None: store = NoNEImmutablePageStore.discover_from_anchor_boundary( anchor_root=anchor_store.root, session_id_t=session_id_t, expected_pointer=seed_pointer, ) training_proven_page_ids_t = ( store.current_training_proven_page_ids_t_boundary() ) if ( training_proven_page_ids_t.ndim != 1 or training_proven_page_ids_t.dtype != torch.long or torch.unique(training_proven_page_ids_t).numel() != training_proven_page_ids_t.numel() ): raise RuntimeError("NoNE accepted training proof identity differs") generation_trained_page_ids = { int(page_id) for page_id in training_proven_page_ids_t.tolist() } if not generation_trained_page_ids.issubset(catalog_page_ids): raise RuntimeError("NoNE accepted training proof contains a foreign page") # The immutable catalog carries inherited capability proof (for example, # exact v1 pages) while later generations carry newly retained training # proof. Both are authoritative trained identities. Ignoring the # catalog half would incorrectly put inherited, already-trained pages # back into the mutable branch scope. validated_trained_page_ids = ( catalog_trained_page_ids | generation_trained_page_ids ) training_page_ids = catalog_page_ids - validated_trained_page_ids scoped_training_page_ids = set(training_page_ids) scoped_training_family_root_count = len( family_root_page_ids & scoped_training_page_ids ) if training_branch_scope is not None: if not isinstance( training_branch_scope, NoNETrainingBranchScopePacket, ): raise TypeError("NoNE training branch scope is malformed") store.validate_training_branch_fork_authority_boundary( training_branch_scope ) scoped_training_page_ids = { int(page_id) for page_id in training_branch_scope.page_ids_t.tolist() } if not scoped_training_page_ids.issubset(training_page_ids): raise RuntimeError( "NoNE training branch scope contains a trained or foreign page" ) scoped_training_family_root_count = len( family_root_page_ids & scoped_training_page_ids ) source_checkpoint = composition.get("sourceCheckpoint") if not isinstance(source_checkpoint, dict): raise RuntimeError("NoNE v2 composition has no source checkpoint") source_checkpoint_sha256 = str(source_checkpoint.get("sha256", "")) if len(source_checkpoint_sha256) != 64: raise RuntimeError("NoNE v2 source checkpoint identity is malformed") generations: list[torch.Tensor] = [] attached_runtimes: list[NoNEPagedExpertRuntime] = [] expected_layer_catalog_ids_t: list[torch.Tensor] = [] resident_path = ( inference_authority.weights.resident_runtime.path if inference_authority is not None else Path(str(resident_record["path"])) ) if ( inference_authority is not None and resident_record.get("sha256") != inference_authority.weights.resident_runtime.sha256 ): raise RuntimeError( "public release resident runtime identity differs" ) for layer_id in layer_ids: raw_page_ids = layer_catalog.get(str(layer_id)) if ( not isinstance(raw_page_ids, list) or not raw_page_ids or any( not isinstance(page_id, int) or isinstance(page_id, bool) or page_id < 0 for page_id in raw_page_ids ) or len(set(raw_page_ids)) != len(raw_page_ids) ): raise RuntimeError(f"NoNE v2 layer page catalog is empty: {layer_id}") page_catalog_ids_t = torch.tensor( raw_page_ids, dtype=torch.long, ) expected_layer_catalog_ids_t.append(page_catalog_ids_t.clone()) family_page_mask_t = torch.tensor( [ int(page_id) in scoped_training_page_ids for page_id in raw_page_ids ], dtype=torch.bool, ) validated_trained_page_mask_t = torch.tensor( [ int(page_id) in validated_trained_page_ids for page_id in raw_page_ids ], dtype=torch.bool, ) runtime = NoNEPagedExpertRuntime( hidden_size=hidden_size, action_size=action_size, router_size=router_size, page_count=len(raw_page_ids), layer_id=layer_id, page_catalog_ids_t=page_catalog_ids_t, family_page_mask_t=family_page_mask_t, validated_trained_page_mask_t=(validated_trained_page_mask_t), page_parameter_elements=page_parameter_elements, ) resident_state = load_v2_resident_layer_state( resident_path, layer_id, ) # The immutable v2 seed predates cumulative training-proof tensors. # They begin at exact zero and thereafter live in every shared # checkpoint, so old seed bytes remain untouched and auditable. runtime.load_seed_resident_state_boundary(resident_state) runtime = runtime.to( device=reference.device, dtype=reference.dtype, ) runtime.preserve_training_proof_precision() runtime.train(self.training) generation_t = runtime.bind_store_boundary( store, session_id_t, ) self.science_stack.attach_paged_expert_runtime( layer_id, runtime, ) attached_runtimes.append(runtime) generations.append(generation_t) self._apply_paged_none_model_wide_residency_budget_boundary( tuple(attached_runtimes) ) resolved_composition_path = Path(composition_path).expanduser().resolve() self._paged_none_store_boundary = store self._paged_none_composition_path = resolved_composition_path self._paged_none_migration_receipt_path = ( Path(migration_receipt_path).expanduser().resolve() ) self._paged_none_replica_receipt_path = ( Path(replica_receipt_path).expanduser().resolve() if replica_receipt_path is not None else None ) self._paged_none_composition_sha256 = hashlib.sha256( resolved_composition_path.read_bytes() ).hexdigest() self._paged_none_source_checkpoint_sha256 = source_checkpoint_sha256 self._paged_none_family_root_count = family_root_count self._paged_none_functional_root_plan_present = ( functional_root_plan_present ) self._paged_none_planned_functional_family_root_count = ( planned_functional_family_root_count ) self._paged_none_pending_functional_family_root_count = ( pending_functional_family_root_count ) self._paged_none_family_root_page_ids_t_boundary = torch.tensor( sorted(family_root_page_ids), dtype=torch.long, ) self._paged_none_global_training_page_count = len(training_page_ids) self._paged_none_training_page_count = len(scoped_training_page_ids) self._paged_none_training_family_root_count = ( scoped_training_family_root_count ) self._paged_none_training_branch_scope = training_branch_scope self._paged_none_objective_page_count = objective_page_count self._paged_none_planned_objective_page_count = ( planned_objective_page_count ) self._paged_none_pending_objective_page_count = ( pending_objective_page_count ) self._paged_none_objective_page_plan_sha256 = ( objective_page_plan_sha256 ) self._paged_none_compact_bank_authority_present = ( compact_bank_authority_present ) self._paged_none_compact_page_bank_count = compact_page_bank_count self._paged_none_admitted_compact_page_count = ( admitted_compact_page_count ) self._paged_none_physical_untrained_bank_capacity_parameter_elements = ( physical_untrained_bank_capacity_parameter_elements ) self._paged_none_full_physical_page_bank_traversal_required = ( full_physical_page_bank_traversal_required ) self._paged_none_reasoning_growth_plan_sha256 = growth_plan_sha256 if sparse_graph_layer_record is None: self._paged_none_sparse_graph_layer_count = None self._paged_none_sparse_graph_layer_ids_sha256 = "" self._paged_none_sparse_graph_layer_catalog_sha256 = "" else: self._paged_none_sparse_graph_layer_count = int( sparse_graph_layer_record["physicalGraphLayerCount"] ) self._paged_none_sparse_graph_layer_ids_sha256 = str( sparse_graph_layer_record["graphLayerIdsSha256"] ) self._paged_none_sparse_graph_layer_catalog_sha256 = str( sparse_graph_layer_record["pageCatalogSha256"] ) self._paged_none_reasoning_source_layer_count = len(layer_ids) self._paged_none_expected_layer_ids_boundary = tuple(layer_ids) self._paged_none_expected_layer_catalog_ids_t_boundary = tuple( expected_layer_catalog_ids_t ) if coordinator is not None: self._paged_none_replica_coordinator_boundary = coordinator self._paged_none_replica_receipt_sha256 = coordinator.receipt_sha256 else: self._paged_none_replica_coordinator_boundary = None self._paged_none_replica_receipt_sha256 = "" self._staged_none_generation_binding = None self._reconciled_staged_direct_map_seal_t = None self.preserve_trainable_control_precision() return torch.stack(generations) def enforce_training_branch_parameter_ownership_boundary(self) -> torch.Tensor: """Freeze every registered tensor in a page-only training branch. Branch recomposition imports external page objects and their page-local optimizer/proof state. It does not reduce dense layers, routers, RBO, or Fabric tensors, so none of those registered parameters may change in a branch. Model-routed page tensors remain trainable through the paged runtime's external candidate-update boundary. """ scope = self._paged_none_training_branch_scope parameters = tuple(self.named_parameters()) if scope is None: return torch.tensor( [sum(parameter.requires_grad for _name, parameter in parameters), len(parameters)], dtype=torch.long, ) if scope.page_ids_t.numel() < 1: raise RuntimeError("NoNE training branch owns no page tensors") for _name, parameter in parameters: parameter.requires_grad_(False) return torch.tensor([0, len(parameters)], dtype=torch.long) def page_only_training_branch_active_boundary(self) -> bool: """Report page-only optimizer ownership at the training boundary. This is a control-plane query used by the learn loop to construct an optimizer-policy tensor. It has no routing or answer authority: page selection remains tensor-owned by the attached NoNE runtimes. """ return self._paged_none_training_branch_scope is not None def apply_ccr_surgical_page_bands_boundary( self, page_bands: torch.Tensor, ) -> torch.Tensor: """Narrow surgical page targets from ContactSeek CCR-ranked bands. Stores the unique band indices for downstream NoNE scope application. Returns a scalar tensor with the number of active surgical pages. """ if not isinstance(page_bands, torch.Tensor) or page_bands.numel() < 1: return torch.zeros((), dtype=torch.long) unique_bands = cast( torch.Tensor, torch.unique(page_bands.detach().reshape(-1).long()), ) self._bmpas_ccr_surgical_page_bands_t = unique_bands scope = self._paged_none_training_branch_scope if scope is None: return unique_bands.new_ones(()) * unique_bands.numel() scope_pages = scope.page_ids_t.detach().reshape(-1).long() if scope_pages.numel() == 0: return unique_bands.new_zeros(()) catalog_count = int(scope_pages.numel()) clamped = unique_bands.clamp(0, max(catalog_count - 1, 0)) surgical_pages = scope_pages.index_select(0, clamped) surgical_pages = torch.unique(surgical_pages) self._bmpas_ccr_surgical_page_ids_t = surgical_pages return surgical_pages.new_ones(()) * surgical_pages.numel() def activate_training_branch_functional_graph_boundary( self, ) -> torch.Tensor: """Open one exact branch-disjoint functional graph optimizer subset. Pages remain independently model-routed and page-local. This boundary adds only the owner-specific registered graph tensors proven by the checkpoint parent: b0 causal working memory/hill climb, b1 Fabric/intent/stop/acquisition, b2 feedback/correction/NLA, or b3 shared science routing and knowledge transfer. The frozen parent is never eligible. """ scope = self._paged_none_training_branch_scope owner_index = self._training_branch_functional_owner_index owner_count = self._training_branch_functional_owner_count if ( scope is None or scope.page_ids_t.numel() < 1 or owner_index is None or owner_count is None ): raise RuntimeError( "functional graph activation requires an owned page branch" ) parameters = dict(self.named_parameters()) expected_names = branch_functional_graph_parameter_names_boundary( parameters, owner_index=owner_index, owner_count=owner_count, ) if ( expected_names != self._page_branch_functional_graph_parameter_names or set(self._page_branch_functional_graph_parameter_geometry) != expected_names or self._page_branch_functional_graph_ownership is None ): raise RuntimeError( "functional graph activation parent authority differs" ) for name, parameter in parameters.items(): parameter.requires_grad_(name in expected_names) trainable = { name: parameter for name, parameter in parameters.items() if parameter.requires_grad } if ( set(trainable) != expected_names or any(name.startswith("base.") for name in trainable) ): raise RuntimeError( "functional graph optimizer ownership escaped its branch" ) active_elements = sum( parameter.numel() for parameter in trainable.values() ) if active_elements < 1: raise RuntimeError("functional graph optimizer owns no parameters") self._training_branch_functional_graph_active = True reference = next(iter(trainable.values())) return reference.new_tensor( (len(trainable), active_elements, active_elements), dtype=torch.long, ) def branch_functional_graph_parameter_ownership_boundary( self, ) -> dict[str, Any]: """Return the exact checkpoint/receipt ownership record.""" if ( not self._training_branch_functional_graph_active or self._page_branch_functional_graph_ownership is None ): raise RuntimeError( "functional graph parameter ownership is not active" ) return copy.deepcopy( self._page_branch_functional_graph_ownership ) def branch_functional_graph_parameter_ownership_active_boundary( self, ) -> bool: """Report whether an exact branch ownership record is active.""" return bool( self._training_branch_functional_graph_active and self._page_branch_functional_graph_ownership is not None ) def activate_training_branch_causal_control_boundary(self) -> torch.Tensor: """Open the additive causal head only inside one isolated page branch. The ordinary v1 page branch remains buffer-only because its optimizer setup never calls this explicit boundary. A v2-capable caller invokes it only after freezing the complete registered graph. The complete causal head is about 2.2 MB at the live geometry and includes the compiler, world-to-hidden bridge, continuation head, and RBO stop gate; every non-causal parameter remains frozen until federation validates and promotes the branch delta. """ if self._training_branch_functional_owner_index is not None: if self._training_branch_functional_owner_index != 0: raise RuntimeError( "causal activation requested by a non-causal graph owner" ) return self.activate_training_branch_functional_graph_boundary() scope = self._paged_none_training_branch_scope if scope is None or scope.page_ids_t.numel() < 1: raise RuntimeError( "causal control activation requires an owned page branch" ) graph = self.science_stack.causal_algebra_world_graph unexpected_trainable = tuple( name for name, parameter in self.named_parameters() if parameter.requires_grad and not graph.page_coupled_training_parameter_boundary(parameter) ) if unexpected_trainable: raise RuntimeError( "causal control activation requires a frozen branch graph: " f"{unexpected_trainable}" ) graph_proof_t = graph.activate_page_coupled_training_boundary() trainable_parameters = tuple( parameter for parameter in self.parameters() if parameter.requires_grad ) if not trainable_parameters or any( not graph.page_coupled_training_parameter_boundary(parameter) for parameter in trainable_parameters ): raise RuntimeError( "branch-local causal controls escaped their optimizer ownership" ) active_elements = sum( parameter.numel() for parameter in trainable_parameters ) expected_proof_t = graph_proof_t.new_tensor( (len(trainable_parameters), active_elements), dtype=torch.long, ) torch._assert_async( graph_proof_t.eq(expected_proof_t).all(), "branch-local causal trainability proof differs", ) return torch.cat( ( graph_proof_t, graph_proof_t.new_tensor((active_elements,), dtype=torch.long), ) ) def bind_page_branch_checkpoint_parent_boundary( self, *, checkpoint_path: str | Path, checkpoint_sha256: str, checkpoint_payload: object, identity_cache_root: Path | None = None, ) -> None: """Bind the exact full parent before an isolated page branch activates.""" path = Path(checkpoint_path).expanduser().resolve() payload = _validated_full_additive_checkpoint_payload_boundary( checkpoint_payload ) if ( self._paged_none_training_branch_scope is not None or not path.is_file() or _verified_checkpoint_file_sha256_boundary( path, checkpoint_sha256, identity_cache_root=identity_cache_root, ) != checkpoint_sha256 ): raise RuntimeError("NoNE page branch parent checkpoint identity differs") parent_checkpoint_lineage = payload.get("lineage") self.validate_checkpoint_lineage(parent_checkpoint_lineage) if not isinstance(parent_checkpoint_lineage, dict): raise RuntimeError("NoNE page branch parent lineage is malformed") target_checkpoint_lineage = self.checkpoint_lineage() parameters = payload["parameters"] buffers = payload["buffers"] assert isinstance(parameters, dict) assert isinstance(buffers, dict) state_key_sha256, state_geometry_sha256 = ( _checkpoint_state_identity_boundary({**parameters, **buffers}) ) runtime_persistent_buffers = self._additive_checkpoint_buffers() persistent_buffer_names = set(runtime_persistent_buffers) parent_buffers = dict(buffers) causal_buffer_prefix = "science_stack.causal_algebra_world_graph." causal_buffer_names = { name for name in persistent_buffer_names if name.startswith(causal_buffer_prefix) } runtime_causal_state_names = causal_buffer_names.union( name for name, _parameter in self.named_parameters() if name.startswith(causal_buffer_prefix) ) parent_causal_state_names = { name for name in set(parameters).union(parent_buffers) if name.startswith(causal_buffer_prefix) } supported_causal_schema_growth_names = { ( causal_buffer_prefix + "verified_operator_signature_t" ), causal_buffer_prefix + "verified_operator_count_t", causal_buffer_prefix + "verified_coupled_gain_delta_t", ( causal_buffer_prefix + "verified_native_knowledge_ownership_gain_t" ), causal_buffer_prefix + "verified_self_correction_gain_t", causal_buffer_prefix + "required_domain_action_coverage_t", causal_buffer_prefix + "verified_domain_action_coverage_t", causal_buffer_prefix + "promotion_qualification_identity_t", causal_buffer_prefix + "promotion_candidate_lineage_t", } # A predecessor that predates the causal world graph has no tensors in # this namespace, so its zero/false proof state can be reconstructed # truthfully. These explicitly versioned promotion-proof buffers are # deterministic zero-growth surfaces: an older graph cannot claim # verified signatures, functional gains, route coverage, qualification, # or candidate lineage until a new held-out cohort earns them. This is # an exact-name allowlist; any other partial namespace is incomplete # durable authority and remains a hard failure instead of silently # inventing proof history. if parent_causal_state_names and ( parent_causal_state_names.difference( runtime_causal_state_names ) or runtime_causal_state_names.difference( parent_causal_state_names ).difference(supported_causal_schema_growth_names) ): raise RuntimeError( "NoNE page branch parent omits persistent buffer authority" ) # Parent binding runs before ``load_trainable_state_dict``. Use its # complete, explicitly versioned adapter here as well: limiting this # early fence to native attention/causal growth rejected durable parents # when another supported migration added a persistent buffer (for # example paged-runtime acceptance telemetry). Unknown omissions stay # absent, so the strict subset check below still rejects corruption and # unversioned graph drift. adapted_parent_state, _ = self.adapt_trainable_state_dict( {**parameters, **buffers}, checkpoint_lineage=parent_checkpoint_lineage, ) missing_persistent_buffer_names = persistent_buffer_names.difference( parent_buffers ) if ( not persistent_buffer_names or not missing_persistent_buffer_names.issubset( adapted_parent_state ) ): raise RuntimeError( "NoNE page branch parent omits persistent buffer authority" ) excluded_default_buffers: dict[str, torch.Tensor] = {} for name in sorted(missing_persistent_buffer_names): migrated = adapted_parent_state[name].detach().to(device="cpu") runtime_default = ( runtime_persistent_buffers[name].detach().to(device="cpu") ) if ( migrated.shape != runtime_default.shape or migrated.dtype != runtime_default.dtype or not torch.equal(migrated, runtime_default) ): raise RuntimeError( "NoNE page branch parent migration default differs" ) excluded_default_buffers[name] = migrated.clone() # The thin branch overlay is physically subordinate to the immutable # parent. Keep its key/geometry authority on bytes that actually exist # in that parent; schema-growth defaults are separately guarded below # and will be re-created by the ordinary full-checkpoint migration on # every cold reload. persistent_buffers = { name: parent_buffers[name] for name in persistent_buffer_names.intersection(parent_buffers) } if not persistent_buffers: raise RuntimeError( "NoNE page branch parent exposes no persistent buffer authority" ) buffer_key_sha256, buffer_geometry_sha256 = ( _checkpoint_state_identity_boundary(persistent_buffers) ) parent_causal_parameter_names = ( BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES.intersection(parameters) ) if parent_causal_parameter_names and ( parent_causal_parameter_names != BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES ): raise RuntimeError( "NoNE page branch parent causal parameter authority is partial" ) causal_parameters = { name: parameters[name] for name in BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES if name in parameters } causal_parameter_initialization = "" causal_parameter_authority_mode = ( BRANCH_CAUSAL_PARENT_AUTHORITY_RESIDENT ) causal_parameter_presence_count = len(causal_parameters) if not causal_parameters: migrated_causal_parameters = { name: adapted_parent_state[name] for name in BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES if name in adapted_parent_state } ( deterministic_causal_parameters, deterministic_causal_buffers, ) = _deterministic_causal_parent_state_boundary( migrated_causal_parameters ) parameter_targets = dict(self.named_parameters()) if not BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES.issubset( parameter_targets ) or set(deterministic_causal_buffers) != causal_buffer_names: raise RuntimeError( "NoNE page branch causal genesis target set differs" ) deterministic_causal_buffers = { name: value.to(dtype=runtime_persistent_buffers[name].dtype) for name, value in deterministic_causal_buffers.items() } # A meta/materialized cold construction can expose arbitrary bytes # for parameters absent from the immutable predecessor. Schema # growth owns those bytes: seed the exact role-derived genesis now, # before the ordinary parent loader asks the migration adapter for # its missing-key values. This makes CPU, CUDA, and meta cold loads # converge on one checkpointed baseline rather than blessing # allocator contents as parent authority. with torch.no_grad(): for name, deterministic in ( deterministic_causal_parameters.items() ): target = parameter_targets[name] if ( target.shape != deterministic.shape or target.dtype != deterministic.dtype ): raise RuntimeError( "NoNE page branch causal genesis parameter geometry " f"differs: {name}" ) target.copy_(deterministic.to(device=target.device)) adapted_parent_state[name] = ( deterministic.detach().to(device="cpu").clone() ) for name, deterministic in deterministic_causal_buffers.items(): causal_buffer_target = runtime_persistent_buffers[name] if ( causal_buffer_target.shape != deterministic.shape or causal_buffer_target.dtype != deterministic.dtype ): raise RuntimeError( "NoNE page branch causal genesis buffer geometry " f"differs: {name}" ) causal_buffer_target.copy_( deterministic.to(device=causal_buffer_target.device) ) adapted_parent_state[name] = ( deterministic.detach().to(device="cpu").clone() ) causal_parameters = ( _validated_deterministic_causal_parent_parameters_boundary( deterministic_causal_parameters ) ) causal_parameter_initialization = ( CAUSAL_PARENT_INITIALIZATION_SCHEMA ) causal_parameter_authority_mode = ( BRANCH_CAUSAL_PARENT_AUTHORITY_GENESIS ) causal_parameter_presence_count = 0 causal_buffers = ( _validated_deterministic_causal_parent_buffers_boundary( causal_parameters, deterministic_causal_buffers, ) ) else: causal_buffers = { name: ( parent_buffers[name] if name in parent_buffers else adapted_parent_state[name] ) for name in causal_buffer_names } if set(causal_buffers) != causal_buffer_names: raise RuntimeError( "NoNE page branch parent causal buffer authority is partial" ) ( causal_parameter_key_sha256, causal_parameter_geometry_sha256, ) = _checkpoint_state_identity_boundary(causal_parameters) causal_parameter_value_sha256 = ( _checkpoint_state_value_sha256_boundary(causal_parameters) ) causal_parameter_geometry = { name: (tuple(value.shape), value.dtype) for name, value in causal_parameters.items() } functional_owner_index = ( self._training_branch_functional_owner_index ) functional_owner_count = ( self._training_branch_functional_owner_count ) functional_parameter_names: frozenset[str] = frozenset() functional_parameter_geometry: dict[ str, tuple[tuple[int, ...], torch.dtype], ] = {} functional_parameter_ownership: dict[str, Any] | None = None functional_parent_genesis: dict[str, Any] | None = None functional_parent_genesis_authority: dict[str, Any] | None = None genesis_parameters: dict[str, torch.Tensor] = {} genesis_buffers: dict[str, torch.Tensor] = {} if ( functional_owner_index is not None and functional_owner_count is not None ): runtime_parameter_targets = dict(self.named_parameters()) runtime_additive_parameters = { name: value for name, value in runtime_parameter_targets.items() if not name.startswith("base.") } # A v1 b1-b3 overlay needs the deterministic causal genesis in its # complete ownership universe when the physical parent predates # that family. The v2 b0 overlay already carries those tensors in # its dedicated causal genesis, so it must not duplicate them in # the general functional record. physical_parent_parameters = dict(parameters) if functional_owner_index == 0: physical_parent_parameters.update(causal_parameters) if not set(runtime_additive_parameters).issubset( adapted_parent_state ): raise RuntimeError( "NoNE page branch functional migration is incomplete" ) functional_genesis_names = { name for name in runtime_additive_parameters if ( name not in physical_parent_parameters or tuple(adapted_parent_state[name].shape) != tuple(physical_parent_parameters[name].shape) ) } functional_genesis_buffer_names = set( missing_persistent_buffer_names ) if functional_owner_index == 0: # b0 seals these in the dedicated causal genesis/outcome # overlay. The general functional parent must not duplicate # their ownership. functional_genesis_buffer_names.difference_update( causal_buffer_names ) functional_genesis_buffer_names = { name for name in functional_genesis_buffer_names if not name.endswith( ANTI_THOMPSON_FAIL_COUNTS_BUFFER_SUFFIX ) } if functional_genesis_names or functional_genesis_buffer_names: if not functional_genesis_names.issubset( adapted_parent_state ) or not functional_genesis_buffer_names.issubset( excluded_default_buffers ): raise RuntimeError( "NoNE page branch functional genesis is incomplete" ) growth_record = self._inherited_graph_growth_record if not isinstance(growth_record, dict): raise RuntimeError( "NoNE page branch functional genesis has no growth proof" ) genesis_parameters = { name: ( adapted_parent_state[name] .detach() .to(device="cpu") .clone() ) for name in sorted(functional_genesis_names) } genesis_buffers = { name: excluded_default_buffers[name].clone() for name in sorted(functional_genesis_buffer_names) } _validate_functional_graph_parent_genesis_overlap_boundary( source_parameters=physical_parent_parameters, genesis_parameters=genesis_parameters, ) with torch.no_grad(): for name, genesis_value in genesis_parameters.items(): target = runtime_parameter_targets[name] if ( tuple(target.shape) != tuple(genesis_value.shape) or target.dtype != genesis_value.dtype ): raise RuntimeError( "NoNE page branch functional genesis geometry " f"differs: {name}" ) target.copy_(genesis_value.to(device=target.device)) for name, genesis_value in genesis_buffers.items(): target_buffer = runtime_persistent_buffers[name] if ( tuple(target_buffer.shape) != tuple(genesis_value.shape) or target_buffer.dtype != genesis_value.dtype ): raise RuntimeError( "NoNE page branch functional genesis buffer " f"geometry differs: {name}" ) target_buffer.copy_( genesis_value.to(device=target_buffer.device) ) functional_parent_genesis = { "schema": FUNCTIONAL_GRAPH_PARENT_GENESIS_SCHEMA, "sourceLineage": copy.deepcopy( parent_checkpoint_lineage ), "targetLineage": copy.deepcopy( target_checkpoint_lineage ), "inheritedGraphGrowth": copy.deepcopy(growth_record), "parameters": genesis_parameters, "buffers": genesis_buffers, } functional_parent_genesis_authority = ( _functional_graph_parent_genesis_authority_boundary( parameters=genesis_parameters, buffers=genesis_buffers, parameter_universe=runtime_parameter_targets, source_lineage=parent_checkpoint_lineage, target_lineage=target_checkpoint_lineage, inherited_graph_growth=growth_record, parent_parameters=physical_parent_parameters, parent_buffers=parent_buffers, ) ) if set(physical_parent_parameters).union( functional_genesis_names ) != set( runtime_additive_parameters ): raise RuntimeError( "NoNE page branch functional parent universe differs" ) functional_parameter_names = ( branch_functional_graph_parameter_names_boundary( runtime_parameter_targets, owner_index=functional_owner_index, owner_count=functional_owner_count, ) ) # ``adapt_trainable_state_dict`` is the single versioned authority # for moving-graph state. A predecessor may either omit a whole # newly introduced functional family or carry a smaller geometry; # in both cases the adapter preserves every inherited element and # materializes only the reviewed successor surface. Binding raw # parent tensors here would reject total family growth and would # record stale geometry for rank growth. The causal family keeps # its stronger resident/genesis authority above; every other # owner binds the adapter's exact current-graph baseline. bound_functional_parameters = { name: ( causal_parameters[name] if name in causal_parameters else adapted_parent_state[name] ) for name in functional_parameter_names if ( name in causal_parameters or name in adapted_parent_state ) } if set(bound_functional_parameters) != set( functional_parameter_names ): missing = sorted( set(functional_parameter_names) - set(bound_functional_parameters) ) raise RuntimeError( "NoNE page branch parent omits functional graph parameters: " f"{missing}" ) for name, value in bound_functional_parameters.items(): target = runtime_parameter_targets[name] precision_compatible = bool( value.dtype == target.dtype or ( value.is_floating_point() and target.is_floating_point() ) ) if ( tuple(value.shape) != tuple(target.shape) or not precision_compatible ): raise RuntimeError( "NoNE page branch parent functional migration geometry " f"differs: {name}" ) functional_parameter_geometry = { name: (tuple(value.shape), value.dtype) for name, value in bound_functional_parameters.items() } functional_parameter_ownership = ( build_branch_functional_graph_parameter_ownership_boundary( bound_functional_parameters, owner_index=functional_owner_index, owner_count=functional_owner_count, parameter_universe=runtime_parameter_targets, ) ) causal_buffer_key_sha256, causal_buffer_geometry_sha256 = ( _checkpoint_state_identity_boundary(causal_buffers) ) causal_buffer_value_sha256 = ( _checkpoint_state_value_sha256_boundary(causal_buffers) ) causal_buffer_geometry = { name: (tuple(value.shape), value.dtype) for name, value in causal_buffers.items() } causal_delta_base_buffers = dict(persistent_buffers) anti_thompson_outcome_defaults = { name: value for name, value in excluded_default_buffers.items() if name.endswith(ANTI_THOMPSON_FAIL_COUNTS_BUFFER_SUFFIX) } # Anti-Thompson counts are updated from the same branch-local outcome # transaction as the causal controls. Historical parents may omit the # complete zero family, but the v2 overlay must own it immediately so # an interrupt can restore the last committed routing outcome exactly. causal_delta_base_buffers.update( anti_thompson_outcome_defaults ) if ( causal_parameter_authority_mode == BRANCH_CAUSAL_PARENT_AUTHORITY_GENESIS ): causal_delta_base_buffers.update(causal_buffers) ( causal_delta_buffer_key_sha256, causal_delta_buffer_geometry_sha256, ) = _checkpoint_state_identity_boundary(causal_delta_base_buffers) causal_delta_buffer_geometry = { name: (tuple(value.shape), value.dtype) for name, value in causal_delta_base_buffers.items() } self._page_branch_parent_checkpoint_path = path self._page_branch_parent_checkpoint_sha256 = checkpoint_sha256 self._page_branch_parent_checkpoint_lineage = copy.deepcopy( parent_checkpoint_lineage ) self._page_branch_parent_state_key_sha256 = state_key_sha256 self._page_branch_parent_state_geometry_sha256 = state_geometry_sha256 self._page_branch_parent_buffer_key_sha256 = buffer_key_sha256 self._page_branch_parent_buffer_geometry_sha256 = buffer_geometry_sha256 self._page_branch_parent_buffer_geometry = { name: (tuple(value.shape), value.dtype) for name, value in persistent_buffers.items() } self._page_branch_parent_causal_parameter_key_sha256 = ( causal_parameter_key_sha256 ) self._page_branch_parent_causal_parameter_geometry_sha256 = ( causal_parameter_geometry_sha256 ) self._page_branch_parent_causal_parameter_value_sha256 = ( causal_parameter_value_sha256 ) self._page_branch_parent_causal_parameter_authority_mode = ( causal_parameter_authority_mode ) self._page_branch_parent_causal_parameter_presence_count = ( causal_parameter_presence_count ) self._page_branch_parent_causal_parameter_geometry = ( causal_parameter_geometry ) self._page_branch_functional_graph_parameter_names = ( functional_parameter_names ) self._page_branch_functional_graph_parameter_geometry = ( functional_parameter_geometry ) self._page_branch_functional_graph_ownership = ( functional_parameter_ownership ) self._page_branch_functional_graph_parent_genesis = ( functional_parent_genesis ) self._page_branch_functional_graph_parent_genesis_authority = ( functional_parent_genesis_authority ) self._page_branch_parent_causal_parameter_initialization = ( causal_parameter_initialization ) self._page_branch_parent_causal_parameters = { name: value.detach().to(device="cpu").clone() for name, value in causal_parameters.items() } self._page_branch_parent_causal_buffer_key_sha256 = ( causal_buffer_key_sha256 ) self._page_branch_parent_causal_buffer_geometry_sha256 = ( causal_buffer_geometry_sha256 ) self._page_branch_parent_causal_buffer_value_sha256 = ( causal_buffer_value_sha256 ) self._page_branch_parent_causal_buffer_geometry = causal_buffer_geometry self._page_branch_parent_causal_buffers = { name: value.detach().to(device="cpu").clone() for name, value in causal_buffers.items() } self._page_branch_parent_causal_delta_buffer_key_sha256 = ( causal_delta_buffer_key_sha256 ) self._page_branch_parent_causal_delta_buffer_geometry_sha256 = ( causal_delta_buffer_geometry_sha256 ) self._page_branch_parent_causal_delta_buffer_geometry = ( causal_delta_buffer_geometry ) self._page_branch_parent_excluded_default_buffers = ( excluded_default_buffers ) def branch_checkpoint_delta_authority_boundary(self) -> dict[str, Any]: """Externalize one full-parent and exact branch-scope checkpoint fence.""" scope = self._paged_none_training_branch_scope parent_path = self._page_branch_parent_checkpoint_path if ( scope is None or parent_path is None or not parent_path.is_file() or len(self._page_branch_parent_checkpoint_sha256) != 64 or not self._page_branch_parent_buffer_geometry or any( len(value) != 64 for value in ( self._page_branch_parent_state_key_sha256, self._page_branch_parent_state_geometry_sha256, self._page_branch_parent_buffer_key_sha256, self._page_branch_parent_buffer_geometry_sha256, ) ) ): raise RuntimeError("NoNE page branch checkpoint parent is not bound") authority: dict[str, Any] = { "baseCheckpoint": { "schema": ADDITIVE_CHECKPOINT_SCHEMA, "path": str(parent_path), "sha256": self._page_branch_parent_checkpoint_sha256, "stateKeySetSha256": ( self._page_branch_parent_state_key_sha256 ), "stateGeometrySha256": ( self._page_branch_parent_state_geometry_sha256 ), "bufferKeySetSha256": ( self._page_branch_parent_buffer_key_sha256 ), "bufferGeometrySha256": ( self._page_branch_parent_buffer_geometry_sha256 ), }, "branchScope": scope.external_record_boundary(), } functional_genesis = ( self._page_branch_functional_graph_parent_genesis ) functional_genesis_authority = ( self._page_branch_functional_graph_parent_genesis_authority ) if (functional_genesis is None) != ( functional_genesis_authority is None ): raise RuntimeError( "NoNE functional parent genesis authority is partial" ) if functional_genesis_authority is not None: base = authority["baseCheckpoint"] assert isinstance(base, dict) base["functionalGraphParentGenesisAuthority"] = copy.deepcopy( functional_genesis_authority ) return authority def branch_checkpoint_causal_delta_authority_boundary( self, ) -> dict[str, Any]: """Externalize the v2 causal subset without widening branch authority.""" authority = copy.deepcopy( self.branch_checkpoint_delta_authority_boundary() ) parent_geometry = ( self._page_branch_parent_causal_parameter_geometry ) causal_identity_fields = ( self._page_branch_parent_causal_parameter_key_sha256, self._page_branch_parent_causal_parameter_geometry_sha256, self._page_branch_parent_causal_parameter_value_sha256, self._page_branch_parent_causal_buffer_key_sha256, self._page_branch_parent_causal_buffer_geometry_sha256, self._page_branch_parent_causal_buffer_value_sha256, self._page_branch_parent_causal_delta_buffer_key_sha256, self._page_branch_parent_causal_delta_buffer_geometry_sha256, ) causal_buffer_geometry = ( self._page_branch_parent_causal_buffer_geometry ) causal_delta_buffer_geometry = ( self._page_branch_parent_causal_delta_buffer_geometry ) authority_mode = ( self._page_branch_parent_causal_parameter_authority_mode ) presence_count = ( self._page_branch_parent_causal_parameter_presence_count ) if ( set(parent_geometry) != BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES or not causal_buffer_geometry or not causal_delta_buffer_geometry or any(len(value) != 64 for value in causal_identity_fields) ): raise RuntimeError( "NoNE page branch causal checkpoint parent is not bound" ) base = authority.get("baseCheckpoint") if not isinstance(base, dict): raise RuntimeError( "NoNE page branch causal checkpoint authority is malformed" ) base.update( { "causalParameterKeySetSha256": ( self._page_branch_parent_causal_parameter_key_sha256 ), "causalParameterGeometrySha256": ( self._page_branch_parent_causal_parameter_geometry_sha256 ), "causalParameterValueSha256": ( self._page_branch_parent_causal_parameter_value_sha256 ), "causalBufferKeySetSha256": ( self._page_branch_parent_causal_buffer_key_sha256 ), "causalBufferGeometrySha256": ( self._page_branch_parent_causal_buffer_geometry_sha256 ), "causalBufferValueSha256": ( self._page_branch_parent_causal_buffer_value_sha256 ), "causalParameterAuthorityMode": authority_mode, "parentCausalParameterPresenceCount": presence_count, "expectedCausalParameterCount": len( BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES ), "bufferKeySetSha256": ( self._page_branch_parent_causal_delta_buffer_key_sha256 ), "bufferGeometrySha256": ( self ._page_branch_parent_causal_delta_buffer_geometry_sha256 ), } ) causal_initialization = ( self._page_branch_parent_causal_parameter_initialization ) if authority_mode == BRANCH_CAUSAL_PARENT_AUTHORITY_GENESIS: causal_parameters = self._page_branch_parent_causal_parameters causal_buffers = self._page_branch_parent_causal_buffers if ( presence_count != 0 or causal_initialization != CAUSAL_PARENT_INITIALIZATION_SCHEMA or set(causal_parameters) != BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES or set(causal_buffers) != set(causal_buffer_geometry) ): raise RuntimeError( "NoNE page branch causal parent initialization differs" ) base["causalParameterInitialization"] = causal_initialization parent_lineage = self._page_branch_parent_checkpoint_lineage if not isinstance(parent_lineage, dict): raise RuntimeError( "NoNE page branch causal parent lineage is malformed" ) base["causalParameterGenesisSourceLineageSha256"] = ( _causal_parent_genesis_source_lineage_sha256_boundary( base_checkpoint_sha256=( self._page_branch_parent_checkpoint_sha256 ), lineage_sha256=_canonical_json_sha256_boundary( parent_lineage ), parameter_key_sha256=( self ._page_branch_parent_causal_parameter_key_sha256 ), parameter_geometry_sha256=( self ._page_branch_parent_causal_parameter_geometry_sha256 ), parameter_value_sha256=( self ._page_branch_parent_causal_parameter_value_sha256 ), buffer_key_sha256=( self._page_branch_parent_causal_buffer_key_sha256 ), buffer_geometry_sha256=( self ._page_branch_parent_causal_buffer_geometry_sha256 ), buffer_value_sha256=( self._page_branch_parent_causal_buffer_value_sha256 ), ) ) elif ( authority_mode != BRANCH_CAUSAL_PARENT_AUTHORITY_RESIDENT or presence_count != len(BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES) or causal_initialization ): raise RuntimeError( "NoNE page branch causal resident authority differs" ) functional_ownership = ( self._page_branch_functional_graph_ownership ) if functional_ownership is not None: functional_parameter_names = functional_ownership.get( "parameterNames" ) if ( self._training_branch_functional_owner_index != 0 or functional_ownership.get("ownerIndex") != 0 or not isinstance(functional_parameter_names, list) or not BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES.issubset( functional_parameter_names ) ): raise RuntimeError( "NoNE page branch causal functional ownership differs" ) authority["functionalGraphParameterOwnership"] = copy.deepcopy( functional_ownership ) return authority def branch_checkpoint_functional_graph_delta_authority_boundary( self, ) -> dict[str, Any]: """Externalize the exact b1-b3 parameter owner over one parent.""" owner_index = self._training_branch_functional_owner_index ownership = self._page_branch_functional_graph_ownership if ( not self._training_branch_functional_graph_active or owner_index not in (1, 2, 3) or ownership is None ): raise RuntimeError( "functional graph branch checkpoint owner is not active" ) authority = copy.deepcopy( self.branch_checkpoint_delta_authority_boundary() ) authority["functionalGraphParameterOwnership"] = copy.deepcopy( ownership ) return authority def _branch_checkpoint_delta_buffer_state_dict_boundary( self, ) -> dict[str, torch.Tensor]: """Clone parent-resident branch buffers for either overlay schema.""" buffers = self._additive_checkpoint_buffers() if not buffers: raise RuntimeError( "NoNE page branch exposes no persistent buffer overlay" ) parent_geometry = self._page_branch_parent_buffer_geometry excluded_defaults = self._page_branch_parent_excluded_default_buffers if set(buffers) != set(parent_geometry).union(excluded_defaults): raise RuntimeError( "NoNE page branch buffer set differs from its parent" ) for name, expected_default in excluded_defaults.items(): value = buffers[name].detach().to(device="cpu") if ( value.shape != expected_default.shape or value.dtype != expected_default.dtype or not torch.equal(value, expected_default) ): raise RuntimeError( "NoNE page branch schema-growth buffer changed without " f"full-parent authority: {name}" ) overlay: dict[str, torch.Tensor] = {} for name in parent_geometry: value = buffers[name] parent_shape, parent_dtype = parent_geometry[name] if tuple(value.shape) != parent_shape: raise RuntimeError( f"NoNE page branch buffer shape differs from its parent: {name}" ) overlay[name] = ( value.detach() .to(device="cpu", dtype=parent_dtype) .clone() ) return overlay def branch_checkpoint_delta_state_dict_boundary( self, ) -> dict[str, torch.Tensor]: """Clone the complete permitted page-branch buffer overlay.""" if self._paged_none_training_branch_scope is None: raise RuntimeError("NoNE branch delta requested outside a page branch") if any(parameter.requires_grad for parameter in self.parameters()): raise RuntimeError("NoNE page branch exposes shared parameter mutation") return self._branch_checkpoint_delta_buffer_state_dict_boundary() def branch_checkpoint_delta_rollback_state_dict_boundary( self, ) -> dict[str, torch.Tensor]: """Clone only physically parent-owned v1 buffers for rollback. Unlike the snapshot/commit boundary above, this transaction guard is intentionally usable after an iteration has mutated a schema-growth buffer and then failed. Those excluded bytes have no v1 commit authority and are reset from the immutable parent defaults by the restore boundary; the guard preserves only the physical parent overlay needed if applying the target delta itself fails. """ if self._paged_none_training_branch_scope is None: raise RuntimeError( "NoNE branch rollback delta requested outside a page branch" ) buffers = self._additive_checkpoint_buffers() parent_geometry = self._page_branch_parent_buffer_geometry excluded_defaults = self._page_branch_parent_excluded_default_buffers if ( not parent_geometry or set(buffers) != set(parent_geometry).union(excluded_defaults) ): raise RuntimeError( "NoNE page branch rollback buffer set differs from its parent" ) overlay: dict[str, torch.Tensor] = {} for name in parent_geometry: value = buffers[name] # The immutable parent geometry was already checked when the # branch was bound and when its delta entered the explicit graph # migration adapter. Existing buffers may have grown since then; # this transaction snapshot preserves their active migrated # geometry and precision so a failed application can be reversed. overlay[name] = value.detach().to(device="cpu").clone() return overlay def branch_checkpoint_functional_graph_parameter_state_dict_boundary( self, ) -> dict[str, torch.Tensor]: """Clone the exact b1-b3 functional graph parameter overlay.""" if self._paged_none_training_branch_scope is None: raise RuntimeError( "functional graph delta requested outside a page branch" ) owner_index = self._training_branch_functional_owner_index owner_count = self._training_branch_functional_owner_count if ( not self._training_branch_functional_graph_active or owner_index not in (1, 2, 3) or owner_count is None ): raise RuntimeError( "functional graph delta requested by the causal owner" ) parameters = dict(self.named_parameters()) trainable_names = frozenset( name for name, parameter in parameters.items() if parameter.requires_grad ) expected_names = branch_functional_graph_parameter_names_boundary( parameters, owner_index=owner_index, owner_count=owner_count, ) parent_geometry = ( self._page_branch_functional_graph_parameter_geometry ) if ( trainable_names != expected_names or expected_names != self._page_branch_functional_graph_parameter_names or set(parent_geometry) != expected_names ): raise RuntimeError( "functional graph checkpoint parameter ownership differs" ) overlay: dict[str, torch.Tensor] = {} for name in expected_names: value = parameters[name] parent_shape, parent_dtype = parent_geometry[name] precision_compatible = ( value.dtype == parent_dtype or ( value.is_floating_point() and torch.empty( (), dtype=parent_dtype, ).is_floating_point() ) ) if tuple(value.shape) != parent_shape or not precision_compatible: raise RuntimeError( "functional graph checkpoint parameter geometry differs: " f"{name}" ) overlay[name] = ( value.detach() .to(device="cpu", dtype=parent_dtype) .clone() ) validated_ownership = ( build_branch_functional_graph_parameter_ownership_boundary( overlay, owner_index=owner_index, owner_count=owner_count, parameter_universe=parameters, ) ) if validated_ownership != self._page_branch_functional_graph_ownership: raise RuntimeError( "functional graph checkpoint ownership record differs" ) return overlay def branch_checkpoint_functional_graph_delta_state_dict_boundary( self, ) -> dict[str, torch.Tensor]: """Clone exact functional parameters plus their page-proof buffers.""" parameters = ( self .branch_checkpoint_functional_graph_parameter_state_dict_boundary() ) buffers = self._branch_checkpoint_delta_buffer_state_dict_boundary() if set(parameters).intersection(buffers): raise RuntimeError( "functional graph checkpoint parameter/buffer authority overlaps" ) return {**parameters, **buffers} def branch_checkpoint_functional_graph_delta_payload_boundary( self, ) -> dict[str, Any]: """Build a ready-to-save b1-b3 functional graph checkpoint.""" parameter_universe = dict(self.named_parameters()) return build_additive_branch_delta_payload_boundary( lineage=self.checkpoint_lineage(), authority=( self .branch_checkpoint_functional_graph_delta_authority_boundary() ), parameters=( self .branch_checkpoint_functional_graph_parameter_state_dict_boundary() ), buffers=self._branch_checkpoint_delta_buffer_state_dict_boundary(), # Geometry-balanced ownership is defined over the complete additive # graph, while this payload intentionally stores only one owner's # exact overlay. Reusing the complete live universe here keeps # construction, validation, cold restore, and the parent checkpoint # allocator on one deterministic ownership map. parameter_universe=parameter_universe, functional_graph_parent_genesis=( self._page_branch_functional_graph_parent_genesis ), ) def branch_checkpoint_causal_parameter_state_dict_boundary( self, ) -> dict[str, torch.Tensor]: """Clone the complete b0 functional owner, including its causal head.""" if self._paged_none_training_branch_scope is None: raise RuntimeError( "NoNE branch causal delta requested outside a page branch" ) parameters = dict(self.named_parameters()) trainable_names = { name for name, parameter in parameters.items() if parameter.requires_grad } parent_geometry = ( self._page_branch_functional_graph_parameter_geometry if self._training_branch_functional_owner_index == 0 else self._page_branch_parent_causal_parameter_geometry ) expected_names = ( self._page_branch_functional_graph_parameter_names if self._training_branch_functional_owner_index == 0 else BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES ) if ( trainable_names != expected_names or set(parent_geometry) != expected_names or not BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES.issubset( expected_names ) ): raise RuntimeError( "NoNE page branch causal parameter ownership differs" ) overlay: dict[str, torch.Tensor] = {} for name in expected_names: value = parameters.get(name) if not isinstance(value, torch.Tensor): raise RuntimeError( f"NoNE page branch causal parameter is missing: {name}" ) parent_shape, parent_dtype = parent_geometry[name] precision_compatible = ( value.dtype == parent_dtype or ( value.is_floating_point() and torch.empty( (), dtype=parent_dtype, ).is_floating_point() ) ) if ( tuple(value.shape) != parent_shape or not precision_compatible ): raise RuntimeError( "NoNE page branch causal parameter geometry differs from its " f"parent: {name}" ) overlay[name] = ( value.detach() .to(device="cpu", dtype=parent_dtype) .clone() ) return overlay def branch_checkpoint_causal_buffer_state_dict_boundary( self, ) -> dict[str, torch.Tensor]: """Clone the complete v2 buffer overlay, including genesis proof state.""" if self._paged_none_training_branch_scope is None: raise RuntimeError( "NoNE branch causal delta requested outside a page branch" ) buffers = self._additive_checkpoint_buffers() overlay_geometry = ( self._page_branch_parent_causal_delta_buffer_geometry ) excluded_defaults = self._page_branch_parent_excluded_default_buffers causal_genesis_names = ( set(self._page_branch_parent_causal_buffer_geometry) if self._page_branch_parent_causal_parameter_authority_mode == BRANCH_CAUSAL_PARENT_AUTHORITY_GENESIS else set() ) anti_thompson_outcome_names = { name for name in excluded_defaults if name.endswith(ANTI_THOMPSON_FAIL_COUNTS_BUFFER_SUFFIX) } protected_defaults = ( set(excluded_defaults) - causal_genesis_names - anti_thompson_outcome_names ) if ( not overlay_geometry or set(buffers) != set(self._page_branch_parent_buffer_geometry).union( excluded_defaults ) or set(overlay_geometry) != set(self._page_branch_parent_buffer_geometry).union( causal_genesis_names, anti_thompson_outcome_names, ) ): raise RuntimeError( "NoNE page branch causal buffer set differs from its parent" ) for name in protected_defaults: value = buffers[name].detach().to(device="cpu") expected_default = excluded_defaults[name] if ( value.shape != expected_default.shape or value.dtype != expected_default.dtype or not torch.equal(value, expected_default) ): raise RuntimeError( "NoNE page branch schema-growth buffer changed without " f"causal authority: {name}" ) overlay: dict[str, torch.Tensor] = {} for name, (parent_shape, parent_dtype) in overlay_geometry.items(): value = buffers[name] precision_compatible = ( value.dtype == parent_dtype or ( value.is_floating_point() and torch.empty((), dtype=parent_dtype).is_floating_point() ) ) value_shape = tuple(value.shape) if ( value_shape != parent_shape and not ( name.endswith("._expert_history_states") and len(parent_shape) == 2 and len(value_shape) == 2 and value_shape[0] > parent_shape[0] and value_shape[1:] == parent_shape[1:] ) ) or not precision_compatible: raise RuntimeError( "NoNE page branch causal buffer geometry differs: " f"{name}" ) # Runtime precision is allowed to follow the active training graph. # The overlay serializes in the immutable parent's dtype. Explicit # moving-graph authority below binds any appended expert-history # rows so training learned by those new experts is not discarded. overlay[name] = ( value.detach() .to(device="cpu", dtype=parent_dtype) .clone() ) _branch_causal_moving_graph_buffer_growth_boundary( overlay_geometry, overlay, ) return overlay def branch_checkpoint_causal_delta_payload_boundary( self, ) -> dict[str, Any]: """Build one ready-to-save v2 branch-local causal checkpoint payload.""" genesis = ( self._page_branch_parent_causal_parameter_authority_mode == BRANCH_CAUSAL_PARENT_AUTHORITY_GENESIS ) buffers = self.branch_checkpoint_causal_buffer_state_dict_boundary() parent_buffer_geometry = ( self._page_branch_parent_causal_delta_buffer_geometry ) moving_graph_buffer_growth = ( _branch_causal_moving_graph_buffer_growth_boundary( parent_buffer_geometry, buffers, ) ) return build_additive_branch_causal_delta_payload_boundary( lineage=self.checkpoint_lineage(), authority=self.branch_checkpoint_causal_delta_authority_boundary(), parameters=( self.branch_checkpoint_causal_parameter_state_dict_boundary() ), buffers=buffers, base_causal_parameters=( self._page_branch_parent_causal_parameters if genesis else None ), base_causal_buffers=( self._page_branch_parent_causal_buffers if genesis else None ), parent_buffer_geometry=parent_buffer_geometry, parameter_universe=dict(self.named_parameters()), moving_graph_buffer_growth=moving_graph_buffer_growth, functional_graph_parent_genesis=( self._page_branch_functional_graph_parent_genesis ), ) def adapt_branch_checkpoint_delta_state_dict_boundary( self, buffers: dict[str, torch.Tensor], checkpoint_lineage: object, ) -> dict[str, torch.Tensor]: """Apply supported graph growth before comparing a rollback overlay. Accepted branch deltas are durable independently of later source-tree growth. The full parent is already resident when rollback reaches this boundary, so pair its unchanged parameter state with the older overlay and reuse the same explicit migration path as parent binding and strict checkpoint load. Unknown keys and unversioned omissions remain visible to the exact-set check rather than being discarded. """ if not isinstance(buffers, dict) or not all( isinstance(value, torch.Tensor) for value in buffers.values() ): raise RuntimeError("NoNE branch delta buffer payload is malformed") parent_geometry = self._page_branch_parent_buffer_geometry if set(buffers) != set(parent_geometry): raise RuntimeError( "NoNE branch delta migration parent buffer set differs" ) parameters = { name: parameter.detach() for name, parameter in self.named_parameters() if not name.startswith("base.") } if set(parameters).intersection(buffers): raise RuntimeError("NoNE branch delta overlaps parameter authority") self.validate_checkpoint_lineage(checkpoint_lineage) parent_lineage = self._page_branch_parent_checkpoint_lineage if not isinstance(parent_lineage, dict): raise RuntimeError( "NoNE branch delta migration parent lineage is malformed" ) excluded_defaults = self._page_branch_parent_excluded_default_buffers active_buffers = self._additive_checkpoint_buffers() migration_buffers: dict[str, torch.Tensor] = {} for name, value in buffers.items(): target = active_buffers[name] precision_compatible = ( value.dtype == target.dtype or ( value.is_floating_point() and target.is_floating_point() ) ) if not precision_compatible: raise RuntimeError( "NoNE branch delta migration buffer dtype differs: " f"{name}" ) # Thin deltas serialize in immutable-parent precision. The graph # adapter validates against the active runtime precision, so # normalize only after the exact parent geometry/set checks above. migration_buffers[name] = value.to(dtype=target.dtype) # Re-run the exact parent migration from the physically present parent # bytes. Pre-seeding schema-growth defaults here would hide their # absence from ``adapt_trainable_state_dict`` and prevent its explicit # predecessor migration from establishing the live buffer set. adapted_state, _ = self.adapt_trainable_state_dict( {**parameters, **migration_buffers}, checkpoint_lineage=parent_lineage, ) expected_buffers = active_buffers adapted_buffer_names = set(adapted_state) - set(parameters) missing_buffer_names = set(expected_buffers) - adapted_buffer_names unexpected_buffer_names = adapted_buffer_names - set(expected_buffers) # Parent binding already proved these exact values are the truthful # versioned defaults for bytes absent from the immutable parent. # A failed training transaction can mutate one of those live buffers # before rollback (for example a promotion-cohort identity). The # general graph adapter seeds missing causal surfaces from the *live* # target graph, so its result is not rollback authority after such a # mutation. Re-establish every physically absent buffer from the # immutable defaults proved at parent binding. The ordinary snapshot # boundary still rejects publishing a v1 delta while any such buffer # is changed, so this grants rollback authority only, never commit # authority. if ( unexpected_buffer_names or not missing_buffer_names.issubset(excluded_defaults) ): missing = sorted(missing_buffer_names) unexpected = sorted(unexpected_buffer_names) raise RuntimeError( "NoNE branch delta migration buffer set differs: " f"missing={missing} unexpected={unexpected}" ) for name in sorted(missing_buffer_names): adapted_state[name] = ( excluded_defaults[name].detach().to(device="cpu").clone() ) for name, expected_default in excluded_defaults.items(): if name not in buffers: adapted_state[name] = ( expected_default.detach().to(device="cpu").clone() ) adapted_buffer_names = set(adapted_state) - set(parameters) if adapted_buffer_names != set(expected_buffers): raise RuntimeError( "NoNE branch delta migration buffer set remains incomplete" ) for name, expected_default in excluded_defaults.items(): migrated_default = adapted_state[name].detach().to(device="cpu") if ( migrated_default.shape != expected_default.shape or migrated_default.dtype != expected_default.dtype or not torch.equal(migrated_default, expected_default) ): raise RuntimeError( "NoNE branch delta migration default differs: " f"{name}" ) # Migration validates the complete live graph above, but a thin delta # may restore only bytes that physically existed in its immutable # parent. The active model can materialize those floating buffers in a # different runtime dtype and can append quantile-router expert rows as # the moving graph grows. Quantile bias is a learned prefix: retain all # durable rows exactly and initialize only the new, zero-authority tail. # Other shape changes remain hard failures until they receive their own # semantic migration. migrated_parent_buffers: dict[str, torch.Tensor] = {} for name in parent_geometry: value = adapted_state[name].detach().to(device="cpu") target = expected_buffers[name].detach().to(device="cpu") if tuple(value.shape) == tuple(target.shape): migrated_parent_buffers[name] = ( value.to(dtype=target.dtype).clone() ) continue quantile_bias_growth = ( name.endswith(".quantile_router.expert_bias_t") and value.ndim == 1 and target.ndim == 1 and 0 < value.shape[0] < target.shape[0] and not torch.count_nonzero(target[value.shape[0] :]) ) if not quantile_bias_growth: raise RuntimeError( "NoNE branch delta migration active geometry differs: " f"{name} delta={tuple(value.shape)}/{value.dtype} " f"active={tuple(target.shape)}/{target.dtype}" ) expanded = torch.zeros_like(target, device="cpu") expanded[: value.shape[0]].copy_( value.to(dtype=target.dtype) ) migrated_parent_buffers[name] = expanded return migrated_parent_buffers def adapt_branch_checkpoint_causal_delta_state_dict_boundary( self, parameters: dict[str, torch.Tensor], buffers: dict[str, torch.Tensor], checkpoint_lineage: object, ) -> AdditiveBranchCausalStatePacket: """Adapt a v2 overlay without broadening its exact b0 owner subset.""" functional_owner_zero = ( self._training_branch_functional_owner_index == 0 ) expected_parameter_names = ( self._page_branch_functional_graph_parameter_names if functional_owner_zero else BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES ) if ( not isinstance(parameters, dict) or set(parameters) != expected_parameter_names or not BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES.issubset( expected_parameter_names ) or not all( isinstance(value, torch.Tensor) for value in parameters.values() ) or not isinstance(buffers, dict) or not all( isinstance(value, torch.Tensor) for value in buffers.values() ) or set(parameters).intersection(buffers) ): raise RuntimeError( "NoNE branch causal delta payload is malformed" ) parent_lineage = self._page_branch_parent_checkpoint_lineage if ( not isinstance(parent_lineage, dict) or checkpoint_lineage != parent_lineage ): raise RuntimeError( "NoNE branch causal delta parent lineage differs" ) self.validate_checkpoint_lineage(checkpoint_lineage) parent_geometry = ( self._page_branch_functional_graph_parameter_geometry if functional_owner_zero else self._page_branch_parent_causal_parameter_geometry ) if set(parent_geometry) != expected_parameter_names: raise RuntimeError( "NoNE branch causal delta parent parameter set differs" ) adapted_parameters: dict[str, torch.Tensor] = {} for name, value in parameters.items(): parent_shape, parent_dtype = parent_geometry[name] if ( tuple(value.shape) != parent_shape or value.dtype != parent_dtype ): raise RuntimeError( "NoNE branch causal delta parameter geometry differs: " f"{name}" ) adapted_parameters[name] = ( value.detach().to(device="cpu").clone() ) buffer_geometry = ( self._page_branch_parent_causal_delta_buffer_geometry ) expected_buffers = self._additive_checkpoint_buffers() excluded_defaults = self._page_branch_parent_excluded_default_buffers causal_genesis_names = ( set(self._page_branch_parent_causal_buffer_geometry) if self._page_branch_parent_causal_parameter_authority_mode == BRANCH_CAUSAL_PARENT_AUTHORITY_GENESIS else set() ) anti_thompson_outcome_names = { name for name in excluded_defaults if name.endswith(ANTI_THOMPSON_FAIL_COUNTS_BUFFER_SUFFIX) } supplied_anti_thompson_outcome_names = ( set(buffers).intersection(anti_thompson_outcome_names) ) if ( supplied_anti_thompson_outcome_names and supplied_anti_thompson_outcome_names != anti_thompson_outcome_names ): raise RuntimeError( "NoNE branch causal anti-thompson outcome state is partial" ) if ( anti_thompson_outcome_names and not supplied_anti_thompson_outcome_names ): buffers = { **buffers, **{ name: excluded_defaults[name] .detach() .to(device="cpu") .clone() for name in anti_thompson_outcome_names }, } protected_defaults = ( set(excluded_defaults) - causal_genesis_names - anti_thompson_outcome_names ) if ( set(buffers) != set(buffer_geometry) or set(expected_buffers) != set(self._page_branch_parent_buffer_geometry).union( excluded_defaults ) ): raise RuntimeError( "NoNE branch causal delta parent buffer set differs" ) for name in protected_defaults: expected_default = excluded_defaults[name] current = expected_buffers[name].detach().to(device="cpu") if ( current.shape != expected_default.shape or current.dtype != expected_default.dtype or not torch.equal(current, expected_default) ): raise RuntimeError( "NoNE branch causal delta protected default differs: " f"{name}" ) _branch_causal_moving_graph_buffer_growth_boundary( buffer_geometry, buffers, ) active_buffers = { name: expected_buffers[name] for name in buffer_geometry } _branch_causal_moving_graph_buffer_growth_boundary( buffer_geometry, active_buffers, runtime_precision_compatible=True, ) adapted_buffers: dict[str, torch.Tensor] = {} for name, value in buffers.items(): parent_shape, parent_dtype = buffer_geometry[name] if value.dtype != parent_dtype: raise RuntimeError( "NoNE branch causal delta buffer geometry differs: " f"{name}" ) target = active_buffers[name].detach().to(device="cpu") if tuple(value.shape) == tuple(target.shape): adapted_buffers[name] = value.detach().to(device="cpu").clone() continue if tuple(value.shape) != parent_shape: raise RuntimeError( "NoNE branch causal delta buffer geometry differs: " f"{name}" ) # An older causal delta predates the current functional expert # suffix. Preserve its complete learned parent prefix and retain # the freshly loaded moving graph's initialized suffix. Once that # suffix trains, the new hash-covered growth overlay serializes it # in full rather than recreating it. expanded = target.clone() prefix = ( slice(0, parent_shape[0]), *( slice(None) for _dimension in parent_shape[1:] ), ) expanded[prefix].copy_( value.detach().to(dtype=target.dtype) ) adapted_buffers[name] = expanded return AdditiveBranchCausalStatePacket( parameters=adapted_parameters, buffers=adapted_buffers, ) def restore_branch_checkpoint_delta_state_dict_boundary( self, buffers: dict[str, torch.Tensor], authority: dict[str, Any], ) -> None: """Restore only the page-branch overlay over an already resident parent.""" if authority != self.branch_checkpoint_delta_authority_boundary(): raise RuntimeError("NoNE page branch delta authority differs") expected = self._additive_checkpoint_buffers() parent_geometry = self._page_branch_parent_buffer_geometry if set(buffers) != set(parent_geometry): raise RuntimeError("NoNE page branch delta parent buffer set differs") excluded_defaults = self._page_branch_parent_excluded_default_buffers if set(expected) != set(parent_geometry).union(excluded_defaults): raise RuntimeError("NoNE page branch delta buffer set differs") with torch.no_grad(): # A v1 branch delta cannot commit schema-growth buffers that were # absent from its immutable parent. Rollback must nevertheless # undo their uncommitted in-memory mutations; otherwise a failed # transaction can never return to its last durable state. for name, expected_default in excluded_defaults.items(): target = expected[name] precision_compatible = ( target.dtype == expected_default.dtype or ( target.is_floating_point() and expected_default.is_floating_point() ) ) if ( tuple(target.shape) != tuple(expected_default.shape) or not precision_compatible ): raise RuntimeError( "NoNE page branch schema-growth buffer geometry differs " f"during rollback: {name}" ) target.copy_( expected_default.to( device=target.device, dtype=target.dtype, ) ) for name, value in buffers.items(): target = expected[name] precision_compatible = ( value.dtype == target.dtype or ( value.is_floating_point() and target.is_floating_point() ) ) if ( tuple(value.shape) != tuple(target.shape) or not precision_compatible ): raise RuntimeError( "NoNE page branch migrated delta geometry differs from " f"the active graph: {name}" ) # The durable delta retains its immutable parent dtype while # the resident training graph may use a lower runtime # precision. Precision conversion is not structural graph # growth; normalize at this restore boundary after the exact # shape and authority checks above. target.copy_( value.to(device=target.device, dtype=target.dtype) ) self.additive_checkpoint_loaded.fill_(True) def restore_branch_checkpoint_functional_graph_delta_state_dict_boundary( self, parameters: dict[str, torch.Tensor], buffers: dict[str, torch.Tensor], authority: dict[str, Any], ) -> None: """Atomically restore one b1-b3 functional graph/page overlay.""" expected_authority = ( self.branch_checkpoint_functional_graph_delta_authority_boundary() ) if authority != expected_authority: raise RuntimeError( "functional graph branch delta authority differs" ) ownership = authority.get("functionalGraphParameterOwnership") parameter_targets = dict(self.named_parameters()) owner_index = self._training_branch_functional_owner_index owner_count = self._training_branch_functional_owner_count if ( owner_index not in (1, 2, 3) or owner_count is None or set(parameters) != self._page_branch_functional_graph_parameter_names or set(parameters) != set(self._page_branch_functional_graph_parameter_geometry) or not all( isinstance(value, torch.Tensor) for value in parameters.values() ) or not all( isinstance(value, torch.Tensor) for value in buffers.values() ) or set(parameters).intersection(buffers) or any(name.startswith("base.") for name in parameters) ): raise RuntimeError( "functional graph branch delta state set differs" ) validated_ownership = ( validate_branch_functional_graph_parameter_ownership_boundary( ownership, parameters, parameter_universe=parameter_targets, ) ) if validated_ownership != self._page_branch_functional_graph_ownership: raise RuntimeError( "functional graph branch delta ownership differs" ) staged_parameters: dict[str, torch.Tensor] = {} parameter_backups: dict[str, torch.Tensor] = {} for name, value in parameters.items(): target = parameter_targets.get(name) if target is None: raise RuntimeError( f"functional graph branch parameter is missing: {name}" ) parent_shape, parent_dtype = ( self._page_branch_functional_graph_parameter_geometry[name] ) if ( tuple(value.shape) != parent_shape or value.dtype != parent_dtype or tuple(target.shape) != parent_shape ): raise RuntimeError( "functional graph branch parameter geometry differs: " f"{name}" ) staged_parameters[name] = value.to( device=target.device, dtype=target.dtype, ) parameter_backups[name] = ( target.detach().to(device="cpu").clone() ) buffer_targets = self._additive_checkpoint_buffers() buffer_backups = { name: value.detach().to(device="cpu").clone() for name, value in buffer_targets.items() } base_authority = copy.deepcopy(authority) base_authority.pop("functionalGraphParameterOwnership", None) try: self.restore_branch_checkpoint_delta_state_dict_boundary( buffers, base_authority, ) with torch.no_grad(): for name, value in staged_parameters.items(): parameter_targets[name].copy_(value) except Exception: with torch.no_grad(): for name, value in parameter_backups.items(): target = parameter_targets[name] target.copy_( value.to( device=target.device, dtype=target.dtype, ) ) for name, value in buffer_backups.items(): buffer_target = buffer_targets[name] buffer_target.copy_( value.to( device=buffer_target.device, dtype=buffer_target.dtype, ) ) raise def restore_branch_checkpoint_causal_delta_state_dict_boundary( self, parameters: dict[str, torch.Tensor], buffers: dict[str, torch.Tensor], authority: dict[str, Any], ) -> None: """Atomically restore the v2 b0 parameters and page-proof buffers.""" if ( authority != self.branch_checkpoint_causal_delta_authority_boundary() ): raise RuntimeError("NoNE page branch causal delta authority differs") functional_owner_zero = ( self._training_branch_functional_owner_index == 0 ) expected_parameter_names = ( self._page_branch_functional_graph_parameter_names if functional_owner_zero else BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES ) if ( set(parameters) != expected_parameter_names or not BRANCH_LOCAL_CAUSAL_PARAMETER_NAMES.issubset( expected_parameter_names ) or not all( isinstance(value, torch.Tensor) for value in parameters.values() ) or not all( isinstance(value, torch.Tensor) for value in buffers.values() ) or set(parameters).intersection(buffers) ): raise RuntimeError( "NoNE page branch causal delta state set differs" ) parameter_targets = dict(self.named_parameters()) causal_geometry = ( self._page_branch_functional_graph_parameter_geometry if functional_owner_zero else self._page_branch_parent_causal_parameter_geometry ) buffer_targets = self._additive_checkpoint_buffers() buffer_geometry = ( self._page_branch_parent_causal_delta_buffer_geometry ) excluded_defaults = self._page_branch_parent_excluded_default_buffers causal_genesis_names = ( set(self._page_branch_parent_causal_buffer_geometry) if self._page_branch_parent_causal_parameter_authority_mode == BRANCH_CAUSAL_PARENT_AUTHORITY_GENESIS else set() ) anti_thompson_outcome_names = { name for name in excluded_defaults if name.endswith(ANTI_THOMPSON_FAIL_COUNTS_BUFFER_SUFFIX) } protected_defaults = ( set(excluded_defaults) - causal_genesis_names - anti_thompson_outcome_names ) if ( set(causal_geometry) != expected_parameter_names or not expected_parameter_names.issubset( parameter_targets ) or set(buffers) != set(buffer_geometry) or set(buffer_targets) != set(self._page_branch_parent_buffer_geometry).union( excluded_defaults ) ): raise RuntimeError( "NoNE page branch causal delta parent state set differs" ) for name in protected_defaults: expected_default = excluded_defaults[name] target = buffer_targets[name].detach().to(device="cpu") if ( target.shape != expected_default.shape or target.dtype != expected_default.dtype or not torch.equal(target, expected_default) ): raise RuntimeError( "NoNE page branch schema-growth buffer changed without " f"full-parent authority: {name}" ) staged_parameters: dict[str, torch.Tensor] = {} staged_buffers: dict[str, torch.Tensor] = {} parameter_backups: dict[str, torch.Tensor] = {} buffer_backups: dict[str, torch.Tensor] = {} for name, value in parameters.items(): target = parameter_targets[name] parent_shape, parent_dtype = causal_geometry[name] if ( tuple(target.shape) != parent_shape or tuple(value.shape) != parent_shape or value.dtype != parent_dtype ): raise RuntimeError( "NoNE page branch causal delta parameter geometry differs: " f"{name}" ) staged_parameters[name] = ( value.detach() .to(device=target.device, dtype=target.dtype) .clone() ) parameter_backups[name] = target.detach().clone() _branch_causal_moving_graph_buffer_growth_boundary( buffer_geometry, buffers, ) for name, value in buffers.items(): target = buffer_targets[name] _parent_shape, parent_dtype = buffer_geometry[name] if ( tuple(target.shape) != tuple(value.shape) or value.dtype != parent_dtype ): raise RuntimeError( "NoNE page branch causal delta buffer geometry differs: " f"{name}" ) staged_buffers[name] = ( value.detach() .to(device=target.device, dtype=target.dtype) .clone() ) buffer_backups[name] = target.detach().clone() try: with torch.no_grad(): for name, value in staged_parameters.items(): parameter_targets[name].copy_(value) for name, value in staged_buffers.items(): buffer_targets[name].copy_(value) self.additive_checkpoint_loaded.fill_(True) except BaseException: with torch.no_grad(): for name, value in parameter_backups.items(): parameter_targets[name].copy_(value) for name, value in buffer_backups.items(): buffer_targets[name].copy_(value) raise def activate_training_branch_store_boundary( self, *, scope: NoNETrainingBranchScopePacket, branch_store_root: str | Path, ) -> torch.Tensor: """Switch a restored parent graph to one isolated page-writer domain.""" from resynthesis.none_paging import ( NoNEImmutablePageStore, NoNETrainingBranchScopePacket, validate_training_branch_scope_boundary, ) if not isinstance(scope, NoNETrainingBranchScopePacket): raise TypeError("NoNE training branch scope is malformed") validate_training_branch_scope_boundary(scope) source_store = self._paged_none_store_boundary if source_store is None: raise RuntimeError("NoNE branch activation has no restored parent store") canonical_parent_paged_lineage = ( self._paged_none_canonical_checkpoint_paged_lineage ) if canonical_parent_paged_lineage is None: canonical_parent_lineage = self.checkpoint_lineage() canonical_parent_paged_lineage = canonical_parent_lineage.get( "pagedNoNE" ) if ( not isinstance(canonical_parent_paged_lineage, dict) or canonical_parent_paged_lineage.get("replicatedGenerationTransaction") is not True or canonical_parent_paged_lineage.get("canonicalPointerAdvancesLast") is not True or not isinstance( canonical_parent_paged_lineage.get("replicaReceiptSha256"), str, ) or len(canonical_parent_paged_lineage["replicaReceiptSha256"]) != 64 or type(canonical_parent_paged_lineage.get("replicaStoreCount")) is not int or canonical_parent_paged_lineage["replicaStoreCount"] < 2 or type( canonical_parent_paged_lineage.get("replicaDurabilityComplete") ) is not bool ): raise RuntimeError( "NoNE training branch parent replica authority is incomplete" ) resolved_branch_root = Path(branch_store_root).expanduser().resolve() branch_already_active = source_store.root == resolved_branch_root if branch_already_active: # A hash-bound accepted branch descendant restores its own isolated # store through the checkpoint sidecar before this ordinary # activation boundary. Reusing that exact same-session/same-root # store is idempotent; an overlay collision remains inadmissible. live_binding = source_store.current_generation_binding_boundary() if ( torch.equal( live_binding.generation_t.detach().cpu().long().reshape(()), scope.parent_generation_t, ) and torch.equal( live_binding.manifest_payload_sha256_t.detach() .cpu() .to(dtype=torch.uint8) .reshape(-1), scope.parent_manifest_payload_sha256_t, ) ): source_store.validate_training_branch_fork_authority_boundary( scope ) source_binding = live_binding else: source_binding = ( self._validated_training_branch_descendant_chain_boundary( store=source_store, branch_scope=scope, expected_generation_record=( live_binding.external_record_boundary() ), ) ) source_graph = source_store.current_graph_authority_boundary() if ( not torch.equal( source_binding.session_id_t, scope.session_id_t, ) or not torch.equal( live_binding.session_id_t, source_binding.session_id_t, ) or not torch.equal( live_binding.generation_t, source_binding.generation_t, ) or not torch.equal( live_binding.manifest_payload_sha256_t, source_binding.manifest_payload_sha256_t, ) or source_graph is None ): raise RuntimeError( "NoNE training branch store root collides with restored authority" ) branch_store = source_store else: source_store.validate_training_branch_fork_authority_boundary(scope) source_binding = source_store.current_generation_binding_boundary() branch_store = NoNEImmutablePageStore( resolved_branch_root, object_roots=source_store.object_store_roots_boundary, advertise_locator=False, ) branch_store.begin_session(scope.session_id_t) branch_store.validate_training_branch_fork_authority_boundary(scope) branch_binding = branch_store.current_generation_binding_boundary() if ( not torch.equal(source_binding.generation_t, branch_binding.generation_t) or not torch.equal( source_binding.manifest_payload_sha256_t, branch_binding.manifest_payload_sha256_t, ) or ( not branch_already_active and ( not torch.equal( branch_binding.generation_t, scope.parent_generation_t, ) or not torch.equal( branch_binding.manifest_payload_sha256_t, scope.parent_manifest_payload_sha256_t, ) ) ) ): raise RuntimeError("NoNE training branch parent identity differs") source_graph = source_store.current_graph_authority_boundary() branch_graph = branch_store.current_graph_authority_boundary() if ( source_graph is None or branch_graph is None or source_graph.external_record_boundary() != branch_graph.external_record_boundary() ): raise RuntimeError("NoNE training branch graph authority differs") scoped_page_ids: list[torch.Tensor] = [] for runtime in self._paged_none_runtimes_boundary(): rebound_generation_t = runtime.bind_store_boundary( branch_store, scope.session_id_t, ) if not torch.equal( rebound_generation_t.detach().cpu().long().reshape(()), branch_binding.generation_t.detach() .cpu() .long() .reshape(()), ): raise RuntimeError("NoNE branch runtime generation differs") local_page_ids_t = runtime.apply_training_branch_scope_boundary( scope.page_ids_t ) if local_page_ids_t.numel() > 0: scoped_page_ids.append(local_page_ids_t) if not scoped_page_ids: raise RuntimeError("NoNE training branch scope reached no page runtime") observed_page_ids_t = torch.sort(torch.cat(tuple(scoped_page_ids))).values if not torch.equal(observed_page_ids_t, torch.sort(scope.page_ids_t).values): raise RuntimeError("NoNE training branch runtime coverage differs") self._paged_none_store_boundary = branch_store self._paged_none_replica_coordinator_boundary = None self._paged_none_replica_receipt_sha256 = "" self._paged_none_canonical_checkpoint_paged_lineage = dict( canonical_parent_paged_lineage ) self._paged_none_training_branch_scope = scope self._paged_none_training_page_count = int(scope.page_ids_t.numel()) family_root_ids_t = self._paged_none_family_root_page_ids_t_boundary self._paged_none_training_family_root_count = int( family_root_ids_t.unsqueeze(1) .eq(scope.page_ids_t.to(dtype=torch.long).unsqueeze(0)) .any(dim=1) .sum() ) self._staged_none_generation_binding = None self._reconciled_staged_direct_map_seal_t = None return observed_page_ids_t @staticmethod def _same_paged_none_generation_binding_boundary( left: NoNEGenerationBinding, right: NoNEGenerationBinding, ) -> bool: """Compare one immutable generation without materializing page IDs.""" return bool( torch.equal(left.session_id_t, right.session_id_t) and torch.equal(left.generation_t, right.generation_t) and torch.equal( left.parent_generation_t, right.parent_generation_t, ) and torch.equal(left.manifest_sha256_t, right.manifest_sha256_t) and torch.equal( left.manifest_payload_sha256_t, right.manifest_payload_sha256_t, ) and torch.equal( left.updated_page_ids_t, right.updated_page_ids_t, ) and left.manifest_relative_path == right.manifest_relative_path ) @staticmethod def _reconciled_direct_page_universe_boundary() -> torch.Tensor: """Return the exact complete direct-page identity of the current graph.""" page_ids_t = torch.cat( ( torch.arange( _RECONCILED_DIRECT_PAGE_PREFIX_END, dtype=torch.long, ), torch.arange( _RECONCILED_DIRECT_PAGE_SCIENCE_START, _RECONCILED_DIRECT_PAGE_SCIENCE_END, dtype=torch.long, ), ) ) if page_ids_t.numel() != _RECONCILED_DIRECT_PAGE_COUNT: raise RuntimeError( "Resynthesis reconciled direct page universe geometry differs" ) return page_ids_t @staticmethod def _reconciled_staged_direct_map_seal_boundary( *, binding: NoNEGenerationBinding, page_ids_t: torch.Tensor, ) -> torch.Tensor: """Seal the immutable candidate generation and complete direct-page map. The executable graph is intentionally not part of this seal. A staged all-knowledge candidate is first traversed with the parent-derived pre-calibration graph, then published with a newly snapshotted graph. Both graphs are independently artifact-hash validated by the store bind, cold-load proof, and acceptance transaction. Binding either graph here would make the immutable page-map identity change during that legitimate publication transition. """ from resynthesis.none_paging import digest_tensor def tensor_sha256(value_t: torch.Tensor) -> str: canonical_t = value_t.detach().cpu().contiguous() return hashlib.sha256( canonical_t.numpy().tobytes(order="C") ).hexdigest() return digest_tensor( _canonical_json_sha256_boundary( { "schema": ( "nnf.resynthesis.reconciled_staged_direct_map_seal.v2" ), "sessionIdSha256": tensor_sha256( binding.session_id_t.to(dtype=torch.long) ), "generation": int( binding.generation_t.detach().cpu().long().reshape(()) ), "parentGeneration": int( binding.parent_generation_t.detach() .cpu() .long() .reshape(()) ), "manifest": binding.manifest_relative_path, "manifestSha256": tensor_sha256( binding.manifest_sha256_t.to(dtype=torch.uint8) ), "manifestPayloadSha256": tensor_sha256( binding.manifest_payload_sha256_t.to( dtype=torch.uint8 ) ), "directPageIdsSha256": tensor_sha256( page_ids_t.to(dtype=torch.long) ), "directPageCount": int(page_ids_t.numel()), } ) ) def _validated_reconciled_staged_direct_map_topology_boundary( self, *, binding: NoNEGenerationBinding, graph_authority: NoNEGraphAuthorityBinding, ) -> tuple[tuple[NoNEPagedExpertRuntime, ...], torch.Tensor]: """Prove one globally unscoped, quiescent, complete 18-layer graph.""" if ( self._paged_none_store_boundary is None or self._paged_none_training_branch_scope is not None or self._training_branch_functional_owner_index is not None or self._training_branch_functional_owner_count is not None or self._training_branch_functional_graph_active or self._candidate_layer_import_source_stores or self._candidate_layer_import_packets or bool( self.completion_successor_candidate_evaluation_active.detach() .cpu() .bool() .reshape(()) ) or bool( self.acquisition_policy.candidate_evaluation_active.detach() .cpu() .bool() .reshape(()) ) or self.science_stack.num_layers != _RECONCILED_DIRECT_GRAPH_LAYER_COUNT or self._paged_none_expected_layer_ids_boundary != tuple(range(_RECONCILED_DIRECT_GRAPH_LAYER_COUNT)) ): raise RuntimeError( "Resynthesis staged direct map requires one globally unscoped " "quiescent 18-layer model" ) if ( binding.session_id_t.ndim != 1 or binding.session_id_t.numel() < 1 or binding.generation_t.numel() != 1 or binding.parent_generation_t.numel() != 1 or graph_authority.layer_count_t.numel() != 1 or int( graph_authority.layer_count_t.detach() .cpu() .long() .reshape(()) ) != _RECONCILED_DIRECT_GRAPH_LAYER_COUNT or graph_authority.page_count_t.numel() != 1 or int( graph_authority.page_count_t.detach() .cpu() .long() .reshape(()) ) != _RECONCILED_DIRECT_PAGE_COUNT or ( graph_authority.physical_graph_layer_count_t is not None and ( graph_authority.physical_graph_layer_count_t.numel() != 1 or int( graph_authority.physical_graph_layer_count_t.detach() .cpu() .long() .reshape(()) ) != _RECONCILED_DIRECT_PAGE_COUNT ) ) ): raise RuntimeError( "Resynthesis staged direct map graph authority differs" ) page_ids_t = self._reconciled_direct_page_universe_boundary() staged_page_ids_t = ( binding.updated_page_ids_t.detach().cpu().long().reshape(-1) ) if not torch.equal(staged_page_ids_t, page_ids_t): raise RuntimeError( "Resynthesis staged direct map page authority differs" ) runtimes = self._paged_none_runtimes_boundary() expected_catalogs = ( self._paged_none_expected_layer_catalog_ids_t_boundary ) if ( len(runtimes) != _RECONCILED_DIRECT_GRAPH_LAYER_COUNT or len(expected_catalogs) != _RECONCILED_DIRECT_GRAPH_LAYER_COUNT ): raise RuntimeError( "Resynthesis staged direct map runtime topology differs" ) runtime_catalogs: list[torch.Tensor] = [] candidate_collection_names = ( "_candidate_pages", "_candidate_forward_weights", "_candidate_forward_wave_bindings", "_candidate_page_objects", "_candidate_page_scratch", "_candidate_page_journal", "_candidate_training_page_cache", "_candidate_page_stage_futures", "_candidate_updated_page_ids", "_candidate_gradient_page_ids", "_candidate_imported_page_ids", "_candidate_vjp_trace_bindings", "_candidate_vjp_reservations", "_candidate_vjp_backward_claimed_ids", "_candidate_vjp_completed_ids", ) for layer_id, runtime, expected_catalog_t in zip( range(_RECONCILED_DIRECT_GRAPH_LAYER_COUNT), runtimes, expected_catalogs, strict=True, ): runtime_catalog_t = ( runtime.router.page_catalog_ids_t.detach() .cpu() .long() .reshape(-1) ) if ( runtime.router.layer_id_t.numel() != 1 or int( runtime.router.layer_id_t.detach() .cpu() .long() .reshape(()) ) != layer_id or runtime_catalog_t.numel() < 1 or torch.unique(runtime_catalog_t).numel() != runtime_catalog_t.numel() or not torch.equal( runtime_catalog_t, expected_catalog_t.detach().cpu().long().reshape(-1), ) or runtime._candidate_window_open or runtime._training_route_cohort_open or bool( runtime.candidate_window_active_t.detach() .cpu() .bool() .reshape(()) ) or bool( runtime.router.training_route_cohort_active_t.detach() .cpu() .bool() .reshape(()) ) or runtime._candidate_scratch_root is not None or runtime._candidate_stage_executor is not None or any( bool(getattr(runtime, name)) for name in candidate_collection_names ) or any( name.rsplit(".", 1)[-1].startswith("candidate_") and bool(buffer_t.detach().ne(0).any().cpu()) for name, buffer_t in runtime.named_buffers() ) ): raise RuntimeError( "Resynthesis staged direct map runtime is scoped, active, " f"or malformed: layer={layer_id}" ) runtime_catalogs.append(runtime_catalog_t) physical_page_ids_t = torch.sort( torch.cat(tuple(runtime_catalogs)) ).values if ( physical_page_ids_t.numel() != page_ids_t.numel() or torch.unique(physical_page_ids_t).numel() != physical_page_ids_t.numel() or not torch.equal(physical_page_ids_t, page_ids_t) ): raise RuntimeError( "Resynthesis staged direct map runtime catalogs do not equal " "the complete physical page universe" ) return runtimes, page_ids_t @staticmethod def _paged_runtime_read_only_binding_snapshot_boundary( runtime: NoNEPagedExpertRuntime, store: NoNEImmutablePageStore, ) -> _PagedRuntimeReadOnlyBindingSnapshot: return _PagedRuntimeReadOnlyBindingSnapshot( runtime=runtime, store=store, buffers=tuple( (name, buffer_t.detach().clone()) for name, buffer_t in runtime.named_buffers() ), accepted_inference_pages=dict( runtime._accepted_inference_pages ), accepted_inference_compositions=dict( runtime._accepted_inference_compositions ), accepted_inference_generation_t=( runtime._accepted_inference_generation_t.detach().clone() if runtime._accepted_inference_generation_t is not None else None ), accepted_inference_device=runtime._accepted_inference_device, accepted_inference_dtype=runtime._accepted_inference_dtype, last_request=runtime.last_request, last_weights=runtime.last_weights, last_bundle=runtime.last_bundle, ) @staticmethod def _restore_paged_runtime_read_only_binding_snapshot_boundary( snapshot: _PagedRuntimeReadOnlyBindingSnapshot, ) -> None: runtime = snapshot.runtime live_buffers = dict(runtime.named_buffers()) if set(live_buffers) != {name for name, _value_t in snapshot.buffers}: raise RuntimeError( "Resynthesis staged direct map rollback runtime buffers differ" ) with torch.no_grad(): for name, value_t in snapshot.buffers: live_t = live_buffers[name] if live_t.shape != value_t.shape or live_t.dtype != value_t.dtype: raise RuntimeError( "Resynthesis staged direct map rollback buffer " f"geometry differs: {name}" ) live_t.copy_(value_t.to(device=live_t.device)) runtime._store_boundary = snapshot.store runtime._accepted_inference_pages = dict( snapshot.accepted_inference_pages ) runtime._accepted_inference_compositions = dict( snapshot.accepted_inference_compositions ) runtime._accepted_inference_generation_t = ( snapshot.accepted_inference_generation_t.detach().clone() if snapshot.accepted_inference_generation_t is not None else None ) runtime._accepted_inference_device = ( snapshot.accepted_inference_device ) runtime._accepted_inference_dtype = snapshot.accepted_inference_dtype runtime.last_request = snapshot.last_request runtime.last_weights = snapshot.last_weights runtime.last_bundle = snapshot.last_bundle def bind_reconciled_staged_direct_map_read_only_boundary( self, store: NoNEImmutablePageStore, session_id_t: torch.Tensor, binding: NoNEGenerationBinding, *, graph_authority: NoNEGraphAuthorityBinding, ) -> torch.Tensor: """Atomically bind the complete pointerless direct map for cold proof. The prior accepted RBO/store/runtime authority remains untouched until every one of the 18 layer runtimes has entered the same staged generation and the store has revalidated its parent, manifest, direct objects, sidecar, and graph. Any failure restores all runtime buffers, read-only residency caches, and RBO store/coordinator/cache fields. """ from resynthesis.none_paging import ( NoNEGenerationBinding as RuntimeNoNEGenerationBinding, NoNEGraphAuthorityBinding as RuntimeNoNEGraphAuthorityBinding, NoNEImmutablePageStore as RuntimeNoNEImmutablePageStore, ) if ( not isinstance(store, RuntimeNoNEImmutablePageStore) or not isinstance(binding, RuntimeNoNEGenerationBinding) or not isinstance( graph_authority, RuntimeNoNEGraphAuthorityBinding, ) or not isinstance(session_id_t, torch.Tensor) or not torch.equal( session_id_t.detach().cpu().long().reshape(-1), binding.session_id_t.detach().cpu().long().reshape(-1), ) ): raise RuntimeError( "Resynthesis staged direct map binding authority is malformed" ) runtimes, page_ids_t = ( self._validated_reconciled_staged_direct_map_topology_boundary( binding=binding, graph_authority=graph_authority, ) ) expected_seal_t = self._reconciled_staged_direct_map_seal_boundary( binding=binding, page_ids_t=page_ids_t, ) active_staged_binding = self._staged_none_generation_binding active_seal_t = self._reconciled_staged_direct_map_seal_t if active_staged_binding is not None or active_seal_t is not None: if ( not isinstance( active_staged_binding, RuntimeNoNEGenerationBinding, ) or active_seal_t is None or self._paged_none_store_boundary is not store or not self._same_paged_none_generation_binding_boundary( active_staged_binding, binding, ) or not torch.equal(active_seal_t, expected_seal_t) or any(runtime._store_boundary is not store for runtime in runtimes) ): raise RuntimeError( "Resynthesis staged direct map rebind authority differs" ) verified = store._verify_staged_generation_read_only_boundary() active_graph = store.current_graph_authority_boundary() if ( verified is None or not self._same_paged_none_generation_binding_boundary( verified, binding, ) or active_graph is None or active_graph.external_record_boundary() != graph_authority.external_record_boundary() ): raise RuntimeError( "Resynthesis staged direct map revalidation differs" ) return active_seal_t.detach().clone() prior_store = cast( RuntimeNoNEImmutablePageStore, self._paged_none_store_boundary, ) if store is prior_store: raise RuntimeError( "Resynthesis staged direct map requires a disposable store view" ) prior_binding = prior_store.current_generation_binding_boundary() prior_graph_authority = prior_store.current_graph_authority_boundary() if ( prior_graph_authority is None or not torch.equal( prior_binding.session_id_t.detach().cpu().long(), binding.session_id_t.detach().cpu().long(), ) or not torch.equal( prior_binding.generation_t.detach().cpu().long().reshape(()), binding.parent_generation_t.detach() .cpu() .long() .reshape(()), ) or int(binding.generation_t.detach().cpu().long().reshape(())) != int(prior_binding.generation_t.detach().cpu().long().reshape(())) + 1 or any(runtime._store_boundary is not prior_store for runtime in runtimes) ): raise RuntimeError( "Resynthesis staged direct map accepted parent differs" ) snapshots = tuple( self._paged_runtime_read_only_binding_snapshot_boundary( runtime, prior_store, ) for runtime in runtimes ) prior_coordinator = self._paged_none_replica_coordinator_boundary prior_replica_receipt_sha256 = ( self._paged_none_replica_receipt_sha256 ) prior_catalog_cache = self._paged_none_catalog_page_ids_t_cache prior_staged_binding = self._staged_none_generation_binding prior_seal_t = self._reconciled_staged_direct_map_seal_t try: for runtime in runtimes: generation_t = runtime.bind_staged_store_read_only_boundary( store, session_id_t, binding, graph_authority=graph_authority, ) if not torch.equal( generation_t.detach().cpu().long().reshape(()), binding.generation_t.detach().cpu().long().reshape(()), ): raise RuntimeError( "Resynthesis staged direct map runtime generation differs" ) staged_parent = store._read_only_staged_parent_binding verified = store._verify_staged_generation_read_only_boundary() active_graph = store.current_graph_authority_boundary() active_page_ids_t = store.accepted_page_ids_t_boundary().detach() if ( staged_parent is None or not self._same_paged_none_generation_binding_boundary( staged_parent, prior_binding, ) or verified is None or not self._same_paged_none_generation_binding_boundary( verified, binding, ) or active_graph is None or active_graph.external_record_boundary() != graph_authority.external_record_boundary() or not torch.equal( active_page_ids_t.cpu().long().reshape(-1), page_ids_t, ) or not self._same_paged_none_generation_binding_boundary( prior_store.current_generation_binding_boundary(), prior_binding, ) or prior_store.current_graph_authority_boundary() is None or ( cast( RuntimeNoNEGraphAuthorityBinding, prior_store.current_graph_authority_boundary(), ).external_record_boundary() != prior_graph_authority.external_record_boundary() ) ): raise RuntimeError( "Resynthesis staged direct map final authority differs" ) self._paged_none_store_boundary = store self._paged_none_replica_coordinator_boundary = None self._paged_none_replica_receipt_sha256 = "" self._paged_none_catalog_page_ids_t_cache = None self._staged_none_generation_binding = verified self._reconciled_staged_direct_map_seal_t = ( expected_seal_t.detach().clone() ) frontier_generation_t = ( self.validate_paged_none_graph_frontier_boundary() ) reverified = store._verify_staged_generation_read_only_boundary() if ( not torch.equal( frontier_generation_t.detach().cpu().long().reshape(()), binding.generation_t.detach().cpu().long().reshape(()), ) or reverified is None or not self._same_paged_none_generation_binding_boundary( reverified, binding, ) ): raise RuntimeError( "Resynthesis staged direct map post-bind proof differs" ) return expected_seal_t.detach().clone() except Exception as primary_error: self._paged_none_store_boundary = prior_store self._paged_none_replica_coordinator_boundary = prior_coordinator self._paged_none_replica_receipt_sha256 = ( prior_replica_receipt_sha256 ) self._paged_none_catalog_page_ids_t_cache = prior_catalog_cache self._staged_none_generation_binding = prior_staged_binding self._reconciled_staged_direct_map_seal_t = prior_seal_t rollback_errors: list[Exception] = [] for snapshot in reversed(snapshots): try: self._restore_paged_runtime_read_only_binding_snapshot_boundary( snapshot ) except Exception as rollback_error: rollback_errors.append(rollback_error) try: restored_binding = ( prior_store.current_generation_binding_boundary() ) restored_graph = prior_store.current_graph_authority_boundary() if ( not self._same_paged_none_generation_binding_boundary( restored_binding, prior_binding, ) or restored_graph is None or restored_graph.external_record_boundary() != prior_graph_authority.external_record_boundary() ): raise RuntimeError( "Resynthesis staged direct map rollback parent differs" ) except Exception as rollback_error: rollback_errors.append(rollback_error) for rollback_failure in rollback_errors: primary_error.add_note( "additional staged direct-map rollback failure: " f"{rollback_failure!r}" ) raise def validate_reconciled_staged_direct_map_read_only_boundary( self, binding: NoNEGenerationBinding, graph_authority: NoNEGraphAuthorityBinding, staged_direct_map_seal_t: torch.Tensor, ) -> torch.Tensor: """Reprove the exact staged page map and independently bound graph. Cold publication must not turn a caller-supplied seal into a claim that the fresh model actually entered its pointerless g188 store. Re-run the complete topology proof, verify the store's staged parent transaction, and require the independently hash-bound final graph before any checkpoint tensors are loaded. """ from resynthesis.none_paging import ( NoNEGenerationBinding as RuntimeNoNEGenerationBinding, NoNEGraphAuthorityBinding as RuntimeNoNEGraphAuthorityBinding, NoNEImmutablePageStore as RuntimeNoNEImmutablePageStore, ) if ( not isinstance(binding, RuntimeNoNEGenerationBinding) or not isinstance( graph_authority, RuntimeNoNEGraphAuthorityBinding, ) or not isinstance(staged_direct_map_seal_t, torch.Tensor) or staged_direct_map_seal_t.dtype != torch.uint8 or staged_direct_map_seal_t.device.type != "cpu" or staged_direct_map_seal_t.shape != (32,) ): raise RuntimeError( "Resynthesis staged direct map validation authority is malformed" ) runtimes, page_ids_t = ( self._validated_reconciled_staged_direct_map_topology_boundary( binding=binding, graph_authority=graph_authority, ) ) expected_seal_t = self._reconciled_staged_direct_map_seal_boundary( binding=binding, page_ids_t=page_ids_t, ) store = self._paged_none_store_boundary active_binding = self._staged_none_generation_binding active_seal_t = self._reconciled_staged_direct_map_seal_t if ( not isinstance(store, RuntimeNoNEImmutablePageStore) or not isinstance(active_binding, RuntimeNoNEGenerationBinding) or active_seal_t is None or not self._same_paged_none_generation_binding_boundary( active_binding, binding, ) or not torch.equal(active_seal_t, expected_seal_t) or not torch.equal( staged_direct_map_seal_t.detach().cpu(), expected_seal_t, ) or any(runtime._store_boundary is not store for runtime in runtimes) ): raise RuntimeError( "Resynthesis staged direct map validation binding differs" ) verified = store._verify_staged_generation_read_only_boundary() active_graph = store.current_graph_authority_boundary() if ( verified is None or not self._same_paged_none_generation_binding_boundary( verified, binding, ) or active_graph is None or active_graph.external_record_boundary() != graph_authority.external_record_boundary() ): raise RuntimeError( "Resynthesis staged direct map validation graph differs" ) return expected_seal_t.detach().clone() def _paged_none_runtimes_boundary(self) -> tuple[Any, ...]: """Resolve attached layer runtimes at the explicit storage boundary.""" if self._paged_none_store_boundary is None: return () runtimes: list[Any] = [] for layer_id in range(self.science_stack.num_layers): runtime = self.science_stack._layer(layer_id).paged_expert_runtime if runtime is not None: runtimes.append(runtime) if len(runtimes) != len(self._paged_none_expected_layer_ids_boundary): raise RuntimeError("NoNE paged composition runtime subset differs") return tuple(runtimes) @staticmethod def _model_wide_page_budget_allocations_boundary( capacities: tuple[int, ...], requested_total: int, ) -> tuple[int, ...]: """Distribute residency only; routing remains owned by each runtime.""" if ( not capacities or isinstance(requested_total, bool) or requested_total < 1 or any( isinstance(capacity, bool) or capacity < 1 for capacity in capacities ) ): raise ValueError("NoNE model-wide page budget geometry is invalid") effective_total = min( max(requested_total, len(capacities)), sum(capacities), ) allocations = [1 for _capacity in capacities] remaining = effective_total - len(allocations) while remaining > 0: eligible = tuple( index for index, capacity in enumerate(capacities) if allocations[index] < capacity ) if not eligible: raise RuntimeError("NoNE model-wide page budget did not converge") share = max(1, remaining // len(eligible)) for index in eligible: addition = min( capacities[index] - allocations[index], share, remaining, ) allocations[index] += addition remaining -= addition if remaining == 0: break return tuple(allocations) @classmethod def _apply_paged_none_model_wide_residency_budget_boundary( cls, runtimes: tuple[Any, ...], ) -> None: """Prevent per-layer page budgets from multiplying GPU residency.""" if not runtimes: raise ValueError("NoNE model-wide residency requires paged runtimes") capacities = tuple(runtime.page_count for runtime in runtimes) cache_requests = { runtime.accepted_inference_cache_entries for runtime in runtimes } training_wave_requests = { int(runtime.training_residency_wave_pages) for runtime in runtimes } if len(cache_requests) != 1 or len(training_wave_requests) != 1: raise RuntimeError("NoNE runtime residency requests diverged") cache_allocations = cls._model_wide_page_budget_allocations_boundary( capacities, next(iter(cache_requests)), ) # Runtime layers execute sequentially, so each can reuse the same # physical training-wave width. Dividing that width across layers made # a 16-page wave become one page per layer and incorrectly coupled # hardware residency to the model route. Only immutable inference-cache # rows coexist model-wide and therefore require distribution. training_wave_pages = next(iter(training_wave_requests)) for runtime, cache_entries in zip( runtimes, cache_allocations, strict=True, ): runtime.apply_model_wide_residency_allocation_boundary( frontier_pages=training_wave_pages, cache_entries=cache_entries, ) def _paged_none_graph_page_ids_t_boundary(self) -> torch.Tensor: """Return the complete page identity encoded by loaded router tensors.""" runtimes = self._paged_none_runtimes_boundary() if not runtimes: return torch.empty(0, dtype=torch.long) reference_t = runtimes[0].router.page_catalog_ids_t return self._unique_page_ids_t( tuple(runtime.router.page_catalog_ids_t for runtime in runtimes), reference_t, ) def validate_paged_none_graph_frontier_boundary(self) -> torch.Tensor: """Prove the loaded graph routes every page behind its accepted pointer.""" store = self._paged_none_store_boundary if store is None: return torch.zeros((), dtype=torch.long) graph_page_ids_t = self._paged_none_graph_page_ids_t_boundary() expected_layer_catalog_ids_t = ( self._paged_none_expected_layer_catalog_ids_t_boundary ) runtimes = self._paged_none_runtimes_boundary() if len(expected_layer_catalog_ids_t) != len(runtimes): raise RuntimeError("NoNE loaded graph has no complete layer catalog authority") for layer_id, runtime, expected_page_ids_t in zip( self._paged_none_expected_layer_ids_boundary, runtimes, expected_layer_catalog_ids_t, strict=True, ): runtime_page_ids_t = runtime.router.page_catalog_ids_t runtime_layer_id_t = runtime.router.layer_id_t if not torch.equal( runtime_layer_id_t.detach().to(device="cpu", dtype=torch.long), torch.tensor(layer_id, dtype=torch.long), ): raise RuntimeError( f"NoNE loaded graph layer identity differs: {layer_id}" ) if not torch.equal( runtime_page_ids_t, expected_page_ids_t.to( device=runtime_page_ids_t.device, dtype=torch.long, ), ): raise RuntimeError( f"NoNE loaded graph layer page catalog differs: {layer_id}" ) accepted_page_ids_t = store.accepted_page_ids_t_boundary().to( device=graph_page_ids_t.device, dtype=torch.long, ) if not torch.equal( torch.sort(graph_page_ids_t).values, accepted_page_ids_t, ): raise RuntimeError( "NoNE accepted pointer and loaded graph page catalogs differ" ) return cast(torch.Tensor, store.accepted_generation_t()) def refresh_paged_none_accepted_pointer_boundary(self) -> torch.Tensor: """Autodiscover the current pointer without host-authored page routes.""" store = self._paged_none_store_boundary if store is None: return torch.zeros((), dtype=torch.long) if self._staged_none_generation_binding is not None: raise RuntimeError( "NoNE accepted pointer cannot refresh during a staged transaction" ) graph_page_ids_t = self._paged_none_graph_page_ids_t_boundary() coordinator = self._paged_none_replica_coordinator_boundary binding = ( coordinator.discover_current_generation_boundary( graph_page_ids_t=graph_page_ids_t, ) if coordinator is not None else store.refresh_accepted_pointer_boundary( graph_page_ids_t=graph_page_ids_t, ) ) for runtime in self._paged_none_runtimes_boundary(): runtime.begin_decode_arm_boundary() return cast(torch.Tensor, binding.generation_t.clone()) @staticmethod def _unique_page_ids_t( tensors: tuple[torch.Tensor, ...], reference_t: torch.Tensor, ) -> torch.Tensor: """Union page identities without changing model route order or mass.""" populated = tuple( tensor.to(device=reference_t.device, dtype=torch.long).reshape(-1) for tensor in tensors if tensor.numel() > 0 ) if not populated: return reference_t.new_empty((0,), dtype=torch.long) return cast( torch.Tensor, torch.unique(torch.cat(populated), sorted=True), ) def _paged_none_catalog_page_ids_t_boundary( self, runtimes: tuple[Any, ...], reference_t: torch.Tensor, ) -> torch.Tensor: """Return the immutable page-catalog union without repeated CUDA work.""" cached = self._paged_none_catalog_page_ids_t_cache if ( isinstance(cached, torch.Tensor) and cached.device == reference_t.device and cached.dtype == torch.long ): return cached catalog = self._unique_page_ids_t( tuple(runtime.router.page_catalog_ids_t for runtime in runtimes), reference_t, ).detach() self._paged_none_catalog_page_ids_t_cache = catalog return catalog def active_paged_route_ids_t(self, reference_t: torch.Tensor) -> torch.Tensor: """Return page IDs selected by the most recent model-owned layer routes.""" if self._paged_none_store_boundary is None: return reference_t.new_empty((0,), dtype=torch.long) page_ids: list[torch.Tensor] = [] for layer_id in range(self.science_stack.num_layers): packet = self.science_stack._layer(layer_id).last_paged_expert_packet if packet is None: continue if not isinstance(packet.page_ids_t, torch.Tensor): raise RuntimeError("paged expert route packet is not tensor-owned") page_ids.append(packet.page_ids_t) return self._unique_page_ids_t(tuple(page_ids), reference_t) def paged_none_model_owned_frontier_capacity_t(self) -> torch.Tensor: """Return the learned model-wide page frontier as one scalar tensor.""" runtimes = self._paged_none_runtimes_boundary() if not runtimes: raise RuntimeError("paged frontier capacity has no active runtimes") counts_t = tuple( runtime.router.quantile_router.frontier_count_t().reshape(()) for runtime in runtimes ) reference_t = counts_t[0] return torch.stack( tuple( count_t.to(device=reference_t.device, dtype=torch.long) for count_t in counts_t ) ).sum() def paged_none_training_route_cohort_rows_t(self) -> torch.Tensor: """Return the model-owned row exposure target for one page cohort.""" runtimes = self._paged_none_runtimes_boundary() if not runtimes: raise RuntimeError("paged route cohort has no active runtimes") rows_t = tuple( runtime.router.training_route_cohort_rows_t.reshape(()) for runtime in runtimes ) reference_t = rows_t[0] stacked_t = torch.stack( tuple( value_t.to(device=reference_t.device, dtype=torch.long) for value_t in rows_t ) ) torch._assert_async( stacked_t.eq(stacked_t[0]).all(), "NoNE layer route-cohort row geometry diverged", ) return stacked_t[0] def begin_paged_none_training_route_cohort_boundary( self, ) -> torch.Tensor: """Open the same model-owned minibatch route across paged layers.""" runtimes = self._paged_none_runtimes_boundary() active_t = self.candidate_page_update_proven.new_zeros( (), dtype=torch.bool, ) store = self._paged_none_store_boundary if store is None: raise RuntimeError("paged route cohort has no accepted store") observed_generation_t = store.accepted_generation_t().to( device=active_t.device, dtype=torch.long, ) started = [] try: for runtime in runtimes: active_t = ( active_t | runtime.begin_training_route_cohort_boundary( observed_generation_t ).to( device=active_t.device, dtype=torch.bool, ) ) started.append(runtime) except Exception: for runtime in reversed(started): runtime.abort_training_route_cohort_boundary() raise return active_t def end_paged_none_training_route_cohort_boundary( self, ) -> torch.Tensor: """Close the cohort only after every layer consumes its gradients.""" runtimes = self._paged_none_runtimes_boundary() selected_t = self.candidate_page_update_proven.new_zeros( (), dtype=torch.long, ) try: for runtime in runtimes: selected_t = ( selected_t + runtime.end_training_route_cohort_boundary().to( device=selected_t.device, dtype=torch.long, ) ) except Exception: for runtime in runtimes: runtime.abort_training_route_cohort_boundary() raise return selected_t def abort_paged_none_training_route_cohort_boundary( self, ) -> torch.Tensor: """Abort every partially opened paged-layer training route cohort.""" runtimes = self._paged_none_runtimes_boundary() aborted_t = self.candidate_page_update_proven.new_zeros( (), dtype=torch.bool, ) first_error: Exception | None = None for runtime in reversed(runtimes): try: aborted_t = ( aborted_t | runtime.abort_training_route_cohort_boundary().to( device=aborted_t.device, dtype=torch.bool, ) ) except Exception as exc: if first_error is None: first_error = exc if first_error is not None: raise first_error return aborted_t def _finish_all_paged_none_candidate_boundaries(self) -> torch.Tensor: """Drain every paged candidate runtime before exposing cleanup failure. Candidate page transfers complete independently across sparse layers. A failed layer must not prevent the remaining layers from joining their transfer workers and releasing proposal-local scratch. The first true cleanup failure remains fail-closed, but only after every runtime has received its finish boundary. """ finished_t = self.candidate_page_update_proven.new_zeros( (), dtype=torch.long, ) first_error: Exception | None = None for runtime in self._paged_none_runtimes_boundary(): try: runtime_finished_t = runtime.finish_candidate_boundary() if not isinstance(runtime_finished_t, torch.Tensor): raise RuntimeError( "NoNE candidate finish returned no tensor proof" ) finished_t = ( finished_t + runtime_finished_t.to( device=finished_t.device, dtype=torch.long, ).reshape(()) ) except Exception as exc: if first_error is None: first_error = exc else: first_error.add_note( "additional NoNE candidate cleanup failure: " f"{exc!r}" ) if first_error is not None: raise first_error return finished_t def active_paged_route_rows_t(self, reference_t: torch.Tensor) -> torch.Tensor: """Return primary paged routes as ``[batch, paged_layers]`` evidence. Residency accounting intentionally uses a union of pages because storage is shared. Emission auditing must retain the batch axis so one prompt cannot borrow another prompt's model-owned route evidence. """ if reference_t.ndim < 1: raise ValueError("paged route row evidence requires a batch axis") batch_size = reference_t.shape[0] if self._paged_none_store_boundary is None: return reference_t.new_empty((batch_size, 0), dtype=torch.long) page_rows: list[torch.Tensor] = [] for layer_id in range(self.science_stack.num_layers): packet = self.science_stack._layer(layer_id).last_paged_expert_packet if packet is None: continue page_ids_t = packet.page_ids_t if not isinstance(page_ids_t, torch.Tensor): raise RuntimeError("paged expert route packet is not tensor-owned") page_ids_t = page_ids_t.to( device=reference_t.device, dtype=torch.long, ).reshape(-1) torch._assert( # type: ignore[no-untyped-call] page_ids_t.shape[0] == batch_size, "paged expert route batch geometry differs from generation", ) page_rows.append(page_ids_t) if not page_rows: return reference_t.new_empty((batch_size, 0), dtype=torch.long) return torch.stack(page_rows, dim=1) def model_scale_telemetry(self) -> NoNEModelScalePacket: """Return tensor-owned scale derived from live modules and page geometry.""" reference_t = next(self.science_stack.parameters()) info = getattr(self.base, "info", None) parent_parameter_elements = int(getattr(info, "parameter_elements", 0)) if parent_parameter_elements < 1: parent_parameter_elements = sum( parameter.numel() for parameter in self.base.parameters() ) if parent_parameter_elements < 1: raise RuntimeError("native parent exposes no physical parameter geometry") additive_parameters = tuple( parameter for name, parameter in self.named_parameters() if not name.startswith("base.") ) additive_parameter_elements = sum( parameter.numel() for parameter in additive_parameters ) additive_trainable_elements = sum( parameter.numel() for parameter in additive_parameters if parameter.requires_grad ) runtimes = self._paged_none_runtimes_boundary() telemetry = tuple( runtime.accepted_residency_telemetry() for runtime in runtimes ) catalog_page_ids_t = self._paged_none_catalog_page_ids_t_boundary( runtimes, reference_t, ) physical_page_count_t = reference_t.new_tensor( catalog_page_ids_t.shape[0], dtype=torch.long, ) page_widths_t = ( torch.stack( tuple( packet.page_parameter_elements_t.to( device=reference_t.device, dtype=torch.long, ).reshape(()) for packet in telemetry ) ) if telemetry else reference_t.new_zeros((1,), dtype=torch.long) ) torch._assert_async( page_widths_t.eq(page_widths_t[0]).all(), "NoNE physical pages disagree on model-weight geometry", ) page_parameter_elements_t = page_widths_t[0] external_page_parameter_elements_t = ( physical_page_count_t * page_parameter_elements_t ) active_routed_page_ids_t = self._unique_page_ids_t( tuple(packet.active_routed_page_ids_t for packet in telemetry), reference_t, ) active_trainable_page_ids_t = self._unique_page_ids_t( tuple(packet.active_trainable_page_ids_t for packet in telemetry), reference_t, ) resident_page_ids_t = self._unique_page_ids_t( ( *tuple( page_ids_t for packet in telemetry for page_ids_t in ( packet.resident_page_ids_t, packet.candidate_resident_page_ids_t, ) ), active_routed_page_ids_t, active_trainable_page_ids_t, ), reference_t, ) validated_trained_page_ids_t = self._unique_page_ids_t( tuple(packet.validated_trained_page_ids_t for packet in telemetry), reference_t, ) resident_page_count_t = reference_t.new_tensor( resident_page_ids_t.shape[0], dtype=torch.long, ) trainable_page_count_t = reference_t.new_tensor( active_trainable_page_ids_t.shape[0], dtype=torch.long, ) validated_trained_page_count_t = reference_t.new_tensor( validated_trained_page_ids_t.shape[0], dtype=torch.long, ) if self._paged_none_family_root_count > 0: family_count_t = reference_t.new_tensor( self._paged_none_family_root_count, dtype=torch.long, ) else: legacy_family_page_ids_t = self._unique_page_ids_t( tuple( runtime.router.page_catalog_ids_t[ runtime.family_page_mask_t ] for runtime in runtimes ), reference_t, ) family_count_t = reference_t.new_tensor( legacy_family_page_ids_t.shape[0], dtype=torch.long, ) parent_resident = bool(getattr(self.base, "_weights_loaded", False)) parent_resident_elements_t = reference_t.new_tensor( parent_parameter_elements if parent_resident else 0, dtype=torch.long, ) parent_parameter_elements_t = reference_t.new_tensor( parent_parameter_elements, dtype=torch.long, ) additive_parameter_elements_t = reference_t.new_tensor( additive_parameter_elements, dtype=torch.long, ) additive_trainable_elements_t = reference_t.new_tensor( additive_trainable_elements, dtype=torch.long, ) resident_page_parameter_elements_t = ( resident_page_count_t * page_parameter_elements_t ) trainable_page_parameter_elements_t = ( trainable_page_count_t * page_parameter_elements_t ) physical_total_parameter_elements_t = ( parent_parameter_elements_t + additive_parameter_elements_t + external_page_parameter_elements_t ) currently_trainable_parameter_elements_t = ( additive_trainable_elements_t + trainable_page_parameter_elements_t ) resident_parameter_elements_t = ( parent_resident_elements_t + additive_parameter_elements_t + resident_page_parameter_elements_t ) parent_layer_count_t = reference_t.new_tensor( int(getattr(info, "num_hidden_layers", 0)), dtype=torch.long, ) science_layer_count_t = reference_t.new_tensor( self.science_stack.num_layers, dtype=torch.long, ) accepted_generation_t = ( reference_t.new_zeros((), dtype=torch.long) if self._paged_none_store_boundary is None else self._paged_none_store_boundary.accepted_generation_t().to( device=reference_t.device, dtype=torch.long, ) ) from resynthesis.none_paging import digest_tensor parent_artifact_sha256 = str(getattr(info, "model_artifact_sha256", "")) if len(parent_artifact_sha256) != 64: parent_lineage_fn = getattr( self.base, "checkpoint_lineage", None, ) parent_lineage = parent_lineage_fn() if callable(parent_lineage_fn) else {} candidate_sha256 = ( parent_lineage.get("modelArtifactSha256") if isinstance(parent_lineage, dict) else None ) parent_artifact_sha256 = ( candidate_sha256 if isinstance(candidate_sha256, str) and len(candidate_sha256) == 64 else hashlib.sha256( json.dumps( parent_lineage, sort_keys=True, separators=(",", ":"), ).encode("utf-8") ).hexdigest() ) page_composition_sha256 = self._paged_none_composition_sha256 or ("0" * 64) return NoNEModelScalePacket( physical_total_parameter_elements_t=(physical_total_parameter_elements_t), currently_trainable_parameter_elements_t=( currently_trainable_parameter_elements_t ), resident_parameter_elements_t=resident_parameter_elements_t, parent_parameter_elements_t=parent_parameter_elements_t, additive_parameter_elements_t=additive_parameter_elements_t, additive_trainable_parameter_elements_t=(additive_trainable_elements_t), page_parameter_elements_t=page_parameter_elements_t, external_page_parameter_elements_t=(external_page_parameter_elements_t), resident_page_parameter_elements_t=(resident_page_parameter_elements_t), trainable_page_parameter_elements_t=(trainable_page_parameter_elements_t), physical_page_expert_count_t=physical_page_count_t, validated_trained_page_expert_count_t=(validated_trained_page_count_t), unvalidated_page_expert_count_t=( physical_page_count_t - validated_trained_page_count_t ), expert_family_count_t=family_count_t, active_routed_page_ids_t=active_routed_page_ids_t, resident_page_ids_t=resident_page_ids_t, active_trainable_page_ids_t=active_trainable_page_ids_t, parent_layer_count_t=parent_layer_count_t, science_layer_count_t=science_layer_count_t, physical_layer_count_t=(parent_layer_count_t + science_layer_count_t), accepted_generation_t=accepted_generation_t, parent_artifact_sha256_t=digest_tensor(parent_artifact_sha256).to( device=reference_t.device ), page_composition_sha256_t=digest_tensor(page_composition_sha256).to( device=reference_t.device ), ) def paged_none_capacity_scale_boundary(self) -> NoNEModelScalePacket: """Return only static geometry needed by CUDA wave admission. The full telemetry packet is intentionally rich for receipts, but its active/resident page masks are diagnostic and require several CUDA ``masked_select``/``unique`` launches. Admission only needs immutable page width/count and layer geometry, so keep that hot planner boundary tensor-native without recomputing route telemetry every transaction. """ reference_t = next(self.science_stack.parameters()) runtimes = self._paged_none_runtimes_boundary() if not runtimes: raise RuntimeError("paged capacity scale has no active runtimes") info = getattr(self.base, "info", None) parent_parameter_elements = int(getattr(info, "parameter_elements", 0)) if parent_parameter_elements < 1: parent_parameter_elements = sum( parameter.numel() for parameter in self.base.parameters() ) if parent_parameter_elements < 1: raise RuntimeError("native parent exposes no physical parameter geometry") additive_parameters = tuple( parameter for name, parameter in self.named_parameters() if not name.startswith("base.") ) additive_parameter_elements = sum( parameter.numel() for parameter in additive_parameters ) additive_trainable_elements = sum( parameter.numel() for parameter in additive_parameters if parameter.requires_grad ) catalog_page_ids_t = self._paged_none_catalog_page_ids_t_boundary( runtimes, reference_t, ) physical_page_count_t = reference_t.new_tensor( catalog_page_ids_t.shape[0], dtype=torch.long, ) page_width_t = runtimes[0].page_parameter_elements_t.to( device=reference_t.device, dtype=torch.long, ).reshape(()) for runtime in runtimes[1:]: torch._assert_async( runtime.page_parameter_elements_t.to( device=reference_t.device, dtype=torch.long, ).reshape(()) == page_width_t, "NoNE physical pages disagree on model-weight geometry", ) parent_parameter_elements_t = reference_t.new_tensor( parent_parameter_elements, dtype=torch.long, ) additive_parameter_elements_t = reference_t.new_tensor( additive_parameter_elements, dtype=torch.long, ) additive_trainable_elements_t = reference_t.new_tensor( additive_trainable_elements, dtype=torch.long, ) external_page_parameter_elements_t = ( physical_page_count_t * page_width_t ) parent_resident_elements_t = reference_t.new_tensor( parent_parameter_elements if bool(getattr(self.base, "_weights_loaded", False)) else 0, dtype=torch.long, ) parent_layer_count_t = reference_t.new_tensor( int(getattr(info, "num_hidden_layers", 0)), dtype=torch.long, ) science_layer_count_t = reference_t.new_tensor( self.science_stack.num_layers, dtype=torch.long, ) empty_page_ids_t = reference_t.new_empty((0,), dtype=torch.long) accepted_generation_t = ( reference_t.new_zeros((), dtype=torch.long) if self._paged_none_store_boundary is None else self._paged_none_store_boundary.accepted_generation_t().to( device=reference_t.device, dtype=torch.long, ) ) zero_digest_t = reference_t.new_zeros((32,), dtype=torch.uint8) physical_total_t = ( parent_parameter_elements_t + additive_parameter_elements_t + external_page_parameter_elements_t ) return NoNEModelScalePacket( physical_total_parameter_elements_t=physical_total_t, currently_trainable_parameter_elements_t=( additive_trainable_elements_t ), resident_parameter_elements_t=( parent_resident_elements_t + additive_parameter_elements_t ), parent_parameter_elements_t=parent_parameter_elements_t, additive_parameter_elements_t=additive_parameter_elements_t, additive_trainable_parameter_elements_t=additive_trainable_elements_t, page_parameter_elements_t=page_width_t, external_page_parameter_elements_t=external_page_parameter_elements_t, resident_page_parameter_elements_t=reference_t.new_zeros( (), dtype=torch.long ), trainable_page_parameter_elements_t=reference_t.new_zeros( (), dtype=torch.long ), physical_page_expert_count_t=physical_page_count_t, validated_trained_page_expert_count_t=reference_t.new_zeros( (), dtype=torch.long ), unvalidated_page_expert_count_t=physical_page_count_t, expert_family_count_t=reference_t.new_tensor( max(0, self._paged_none_family_root_count), dtype=torch.long, ), active_routed_page_ids_t=empty_page_ids_t, resident_page_ids_t=empty_page_ids_t, active_trainable_page_ids_t=empty_page_ids_t, parent_layer_count_t=parent_layer_count_t, science_layer_count_t=science_layer_count_t, physical_layer_count_t=parent_layer_count_t + science_layer_count_t, accepted_generation_t=accepted_generation_t, parent_artifact_sha256_t=zero_digest_t, page_composition_sha256_t=zero_digest_t, ) def model_scale_receipt_boundary(self) -> dict[str, Any]: """Serialize exact model-owned scale and compact checkpoint lineage.""" packet = self.model_scale_telemetry() lineage = self.checkpoint_lineage() parent_lineage = lineage.get("parent") paged_lineage = lineage.get("pagedNoNE") if not isinstance(parent_lineage, dict): raise RuntimeError("model scale receipt has no parent lineage") compact_lineage = { "schema": lineage.get("schema"), "parentCheckpointId": parent_lineage.get("checkpointId"), "parentModelArtifactSha256": parent_lineage.get("modelArtifactSha256"), "additiveScienceLayers": lineage.get("scienceLayers"), "additiveScienceExperts": lineage.get("scienceExperts"), "pageCompositionSha256": ( paged_lineage.get("compositionSha256") if isinstance(paged_lineage, dict) else None ), "pageSourceCheckpointSha256": ( self._paged_none_source_checkpoint_sha256 or None ), "acceptedPageGeneration": int( packet.accepted_generation_t.detach().cpu().long().reshape(()) ), } lineage_sha256 = hashlib.sha256( json.dumps( lineage, sort_keys=True, separators=(",", ":"), ).encode("utf-8") ).hexdigest() info = getattr(self.base, "info", None) native_owner = str(getattr(info, "native_owner", "")) tokenizer_receipt_fn = getattr( self.base, "tokenizer_backend_receipt_boundary", None, ) if not callable(tokenizer_receipt_fn): raise RuntimeError("model scale receipt has no tokenizer backend owner") tokenizer_backend = tokenizer_receipt_fn() if not isinstance(tokenizer_backend, dict): raise RuntimeError("model scale tokenizer backend receipt is malformed") def integer(tensor: torch.Tensor) -> int: return int(tensor.detach().cpu().long().reshape(())) active_page_ids = packet.active_routed_page_ids_t.detach().cpu().long().tolist() resident_page_ids = packet.resident_page_ids_t.detach().cpu().long().tolist() trainable_page_ids = ( packet.active_trainable_page_ids_t.detach().cpu().long().tolist() ) return { "schema": "nnf.resynthesis.model_owned_scale.v1", "modelOwned": True, "derivedFromLiveTensorGeometry": True, "optimizerMomentsExcluded": True, "countingBoundary": { "parent": ( "serialized tensor elements from the independently verified " "native-parent safetensors header" ), "additive": ( "live named Parameter elements owned by Resynthesis, excluding " "the parent module" ), "externalPages": ( "immutable external-page model-weight elements; optimizer " "moments are excluded" ), "currentlyTrainable": ( "requires-grad additive Parameter elements plus active " "trainable external-page model-weight elements" ), "resident": ( "loaded parent tensor elements plus live additive Parameter " "elements plus resident external-page model-weight elements" ), "excluded": ( "optimizer moments, gradients, activations, KV/NLA state, " "training-corpus bytes, and duplicate aliases" ), }, "physicalTotalParameters": integer( packet.physical_total_parameter_elements_t ), "currentlyTrainableParameters": integer( packet.currently_trainable_parameter_elements_t ), "residentParameters": integer(packet.resident_parameter_elements_t), "parentParameters": integer(packet.parent_parameter_elements_t), "additiveParameters": integer(packet.additive_parameter_elements_t), "currentlyTrainableAdditiveParameters": integer( packet.additive_trainable_parameter_elements_t ), "pageParameterElements": integer(packet.page_parameter_elements_t), "externalPageParameters": integer( packet.external_page_parameter_elements_t ), "residentPageParameters": integer( packet.resident_page_parameter_elements_t ), "currentlyTrainablePageParameters": integer( packet.trainable_page_parameter_elements_t ), "physicalPageExperts": integer(packet.physical_page_expert_count_t), "validatedTrainedPageExperts": integer( packet.validated_trained_page_expert_count_t ), "unvalidatedPageExperts": integer(packet.unvalidated_page_expert_count_t), "expertFamilyCount": integer(packet.expert_family_count_t), "activeRoutedExperts": len(active_page_ids), "activeRoutedPageIds": active_page_ids, "residentPageIds": resident_page_ids, "currentlyTrainablePageIds": trainable_page_ids, "parentLayers": integer(packet.parent_layer_count_t), "scienceLayers": integer(packet.science_layer_count_t), "pagedRuntimeLayers": ( len(self._paged_none_expected_layer_ids_boundary) ), "pagedRuntimeLayerIds": list( self._paged_none_expected_layer_ids_boundary ), "reasoningOnlyLayers": ( integer(packet.science_layer_count_t) - len(self._paged_none_expected_layer_ids_boundary) ), "physicalLayers": integer(packet.physical_layer_count_t), "checkpointLineage": compact_lineage, "checkpointLineageSha256": lineage_sha256, "codeSourceDiagnostics": { "declaredParentSourceBundleSha256": getattr( info, "parent_source_bundle_sha256", None, ), "observedParentSourceBundleSha256": getattr( info, "observed_parent_source_bundle_sha256", None, ), "parentSourceBundleMatchesExpected": getattr( info, "parent_source_bundle_matches_expected", None, ), "legacyCapabilitySourceSha256": getattr( info, "legacy_capability_source_sha256", None, ), "sourceHashDiagnosticOnly": True, "sourceHashAffectsExecution": False, }, "tokenizerBackend": tokenizer_backend, "nativeParent": { "owner": native_owner, "generation": getattr(info, "native_generation", ""), "root": getattr(info, "native_root", ""), "manifestSha256": getattr( info, "native_manifest_sha256", "", ), "migrationPromotionEligible": bool( getattr( info, "native_migration_promotion_eligible", False, ) ), "externalProductCheckpointDependency": (native_owner != "Resynthesis"), }, "trainingClaimScope": ( "validated inherited pages only; transfer-initialized and " "gradient-only pages remain unvalidated" ), "scaleIncreasePromotionEligible": False, "promotionRequiresHeldoutKnowledge": True, "promotionRequiresModelOwnedTrace": True, "promotionRequiresColdReload": True, "answerSurfaceAuthority": False, "routingAuthority": False, "stoppingAuthority": False, "scoringAuthority": False, } def paged_none_residency_receipt_boundary(self) -> dict[str, Any]: """Serialize model-owned residency counters for diagnostics only.""" runtimes = self._paged_none_runtimes_boundary() layer_rows: list[dict[str, Any]] = [] resident_page_ids: set[int] = set() candidate_resident_page_ids: set[int] = set() candidate_external_page_ids: set[int] = set() active_routed_page_ids: set[int] = set() active_trainable_page_ids: set[int] = set() total_candidate_external_bytes = 0 total_requests = 0 total_hits = 0 total_misses = 0 total_gradient_forwards = 0 cache_entry_limits: list[int] = [] frontier_page_limits: list[int] = [] for runtime in runtimes: telemetry_fn = getattr( runtime, "accepted_residency_telemetry", None, ) if not callable(telemetry_fn): raise RuntimeError("NoNE paged runtime exposes no residency telemetry") packet = telemetry_fn() layer_id = int(packet.layer_id_t.detach().cpu().long().reshape(())) generation = int(packet.generation_t.detach().cpu().long().reshape(())) page_ids = [ int(page_id) for page_id in packet.resident_page_ids_t.detach() .cpu() .long() .reshape(-1) .tolist() ] candidate_resident_ids = [ int(page_id) for page_id in packet.candidate_resident_page_ids_t.detach() .cpu() .long() .reshape(-1) .tolist() ] candidate_external_ids = [ int(page_id) for page_id in packet.candidate_external_page_ids_t.detach() .cpu() .long() .reshape(-1) .tolist() ] active_routed_ids = [ int(page_id) for page_id in packet.active_routed_page_ids_t.detach() .cpu() .long() .reshape(-1) .tolist() ] active_trainable_ids = [ int(page_id) for page_id in packet.active_trainable_page_ids_t.detach() .cpu() .long() .reshape(-1) .tolist() ] candidate_external_bytes = int( packet.candidate_external_object_bytes_t.detach() .cpu() .long() .reshape(()) ) requests = int(packet.request_count_t.detach().cpu().long().reshape(())) hits = int(packet.hit_page_count_t.detach().cpu().long().reshape(())) misses = int(packet.miss_page_count_t.detach().cpu().long().reshape(())) gradient_forwards = int( packet.gradient_page_forward_count_t.detach().cpu().long().reshape(()) ) cache_entry_limit = int(runtime.accepted_inference_cache_entries) frontier_page_limit = int(runtime.training_residency_wave_pages) cache_entry_limits.append(cache_entry_limit) frontier_page_limits.append(frontier_page_limit) resident_page_ids.update(page_ids) candidate_resident_page_ids.update(candidate_resident_ids) candidate_external_page_ids.update(candidate_external_ids) active_routed_page_ids.update(active_routed_ids) active_trainable_page_ids.update(active_trainable_ids) total_candidate_external_bytes += candidate_external_bytes total_requests += requests total_hits += hits total_misses += misses total_gradient_forwards += gradient_forwards layer_rows.append( { "layerId": layer_id, "acceptedGeneration": generation, "residentPageIds": page_ids, "residentPageCount": len(page_ids), "candidateResidentPageIds": candidate_resident_ids, "candidateResidentPageCount": len(candidate_resident_ids), "candidateExternalPageIds": candidate_external_ids, "candidateExternalPageCount": len(candidate_external_ids), "candidateExternalObjectBytes": candidate_external_bytes, "activeRoutedPageIds": active_routed_ids, "activeTrainablePageIds": active_trainable_ids, "requestCount": requests, "hitPageCount": hits, "missPageCount": misses, "gradientPageForwardCount": gradient_forwards, "gpuResidentPageCacheEntries": cache_entry_limit, "trainingResidencyWavePages": frontier_page_limit, "maxFrontierPages": frontier_page_limit, "modelRouteFrontierCapped": False, } ) selected_pages = total_hits + total_misses materialization_record: dict[str, Any] | None = None store = self._paged_none_store_boundary materialization_fn = getattr( store, "page_materialization_telemetry_boundary", None, ) if callable(materialization_fn): materialization_packet = materialization_fn() external_record_fn = getattr( materialization_packet, "external_record_boundary", None, ) if not callable(external_record_fn): raise RuntimeError( "NoNE page materialization telemetry has no boundary" ) candidate_record = external_record_fn() if ( not isinstance(candidate_record, dict) or candidate_record.get("schema") != ( "nnf.resynthesis." "none_page_materialization_telemetry.v1" ) or candidate_record.get("diagnosticOnly") is not True or candidate_record.get("routingAuthority") is not False or candidate_record.get("promotionAuthority") is not False ): raise RuntimeError( "NoNE page materialization telemetry differs" ) materialization_record = candidate_record return { "schema": "nnf.resynthesis.none_page_residency_telemetry.v1", "modelOwned": True, "diagnosticOnly": True, "answerSurfaceAuthority": False, "routingAuthority": False, "stoppingAuthority": False, "scoringAuthority": False, "promotionAuthority": False, "active": bool(runtimes), "identityDimensions": ( "session", "accepted_generation", "device", "dtype", ), "layerCount": len(layer_rows), "residentPageCount": sum( int(row["residentPageCount"]) for row in layer_rows ), "uniqueResidentPageCount": len(resident_page_ids), "candidateResidentPageCount": len(candidate_resident_page_ids), "candidateResidentPageIds": sorted(candidate_resident_page_ids), "candidateExternalPageCount": len(candidate_external_page_ids), "candidateExternalPageIds": sorted(candidate_external_page_ids), "candidateExternalObjectBytes": total_candidate_external_bytes, "activeRoutedPageCount": len(active_routed_page_ids), "activeRoutedPageIds": sorted(active_routed_page_ids), "activeTrainablePageCount": len(active_trainable_page_ids), "activeTrainablePageIds": sorted(active_trainable_page_ids), "requestCount": total_requests, "hitPageCount": total_hits, "missPageCount": total_misses, "selectedPageCount": selected_pages, "hitRate": (total_hits / selected_pages if selected_pages else 0.0), "gradientPageForwardCount": total_gradient_forwards, "pageMaterialization": materialization_record, "gpuResidentPageCacheEntries": ( sum(cache_entry_limits) if cache_entry_limits else None ), "gpuResidentPageCacheBudgetScope": "model_wide", "trainingResidencyWavePages": ( max(frontier_page_limits) if frontier_page_limits else None ), "maxFrontierPages": ( max(frontier_page_limits) if frontier_page_limits else None ), "modelRouteFrontierCapped": False, "layers": layer_rows, } def bulk_cuda_wave_activation_evidence_t(self) -> torch.Tensor | None: """Return the retained wave-activation measurement, if one exists. The four current elements are ``(fixed wave transient working set, conservative bytes per row, prompt-token count, conservative bytes per prompt token)`` from one successful wave. Older in-process two-element evidence remains readable until the next observation replaces it. Both projections assign every measured byte to their dimension, deliberately overcharging fixed residency while the next wave widens. This is capacity telemetry only: it never carries page identity, route score, prompt, or target information. """ evidence_t = getattr( self, "_bulk_cuda_wave_activation_evidence_t", None, ) if evidence_t is None: return None if ( not isinstance(evidence_t, torch.Tensor) or evidence_t.numel() not in {2, 4} ): raise RuntimeError( "NoNE bulk CUDA wave activation evidence is malformed" ) return evidence_t def record_bulk_cuda_wave_activation_evidence_boundary( self, evidence_t: torch.Tensor, ) -> torch.Tensor: """Retain the widest proven wave measurement as model-owned state. The values form one coherent measurement. Taking their component-wise maximum splices unrelated observations, while choosing only the largest transient preserves a narrow wave after an execution improvement lowers total memory. Infer the demonstrated row width from each coherent pair and retain the wider successful wave. For equal widths, retain the larger working set as the conservative bound. """ if ( not isinstance(evidence_t, torch.Tensor) or evidence_t.numel() not in {2, 4} ): raise RuntimeError( "NoNE bulk CUDA wave activation evidence is invalid" ) evidence_t = ( evidence_t.detach().to(device="cpu", dtype=torch.long).reshape(-1) ) torch._assert_async( (evidence_t >= 1).all(), "NoNE bulk CUDA wave activation evidence is invalid", ) prior_t = getattr( self, "_bulk_cuda_wave_activation_evidence_t", None, ) if ( isinstance(prior_t, torch.Tensor) and prior_t.numel() in {2, 4} ): prior_t = prior_t.detach().to( device="cpu", dtype=torch.long, ).reshape(-1) # A new four-element observation supersedes legacy row-only # evidence. Once both observations carry token geometry, retain # the widest successful row measurement as one coherent packet. if evidence_t.numel() != prior_t.numel(): if evidence_t.numel() == 2: return evidence_t.new_tensor(1) self._bulk_cuda_wave_activation_evidence_t = evidence_t return evidence_t.new_tensor(1) evidence_rows_t = torch.div( evidence_t[0] + evidence_t[1] - 1, evidence_t[1], rounding_mode="floor", ) prior_rows_t = torch.div( prior_t[0] + prior_t[1] - 1, prior_t[1], rounding_mode="floor", ) use_evidence_t = (evidence_rows_t > prior_rows_t) | ( evidence_rows_t.eq(prior_rows_t) & evidence_t[0].ge(prior_t[0]) ) evidence_t = torch.where( use_evidence_t, evidence_t, prior_t, ) self._bulk_cuda_wave_activation_evidence_t = evidence_t return evidence_t.new_tensor(1) def reset_bulk_cuda_wave_activation_evidence_after_oom_boundary( self, ) -> torch.Tensor: """Invalidate allocator telemetry after a restored OOM transaction. The measurement describes one prior allocator state, not model, optimizer, page, or routing authority. An OOM followed by exact transaction rollback proves that allocator state is no longer an admissible planning basis for the retry. """ evidence_t = getattr( self, "_bulk_cuda_wave_activation_evidence_t", None, ) if evidence_t is not None and ( not isinstance(evidence_t, torch.Tensor) or evidence_t.numel() not in {2, 4} ): raise RuntimeError( "NoNE bulk CUDA wave activation evidence is malformed" ) if hasattr(self, "_bulk_cuda_wave_activation_evidence_t"): delattr(self, "_bulk_cuda_wave_activation_evidence_t") return torch.ones((), dtype=torch.long) def paged_none_training_proof_from_boundary(self) -> Any | None: """Aggregate model-owned training-page proof across layers.""" runtimes = tuple( runtime for runtime in self._paged_none_runtimes_boundary() if bool(runtime.family_page_mask_t.detach().cpu().any()) ) if not runtimes: return None from resynthesis.none_paging import combine_page_training_proofs proof = combine_page_training_proofs( tuple( runtime.training_proof_components_boundary() for runtime in runtimes ) ) if proof.family_page_ids_t.numel() != self._paged_none_training_page_count: raise RuntimeError("NoNE training-page proof denominator differs") return proof def paged_none_candidate_training_proof_from_boundary(self) -> Any | None: """Aggregate tentative candidate proof without advancing authority.""" runtimes = tuple( runtime for runtime in self._paged_none_runtimes_boundary() if bool(runtime.family_page_mask_t.detach().cpu().any()) ) if not runtimes: return None from resynthesis.none_paging import combine_page_training_proofs proof = combine_page_training_proofs( tuple( runtime.candidate_training_proof_components_boundary() for runtime in runtimes ) ) if proof.family_page_ids_t.numel() != self._paged_none_training_page_count: raise RuntimeError("NoNE candidate page proof denominator differs") return proof def paged_none_candidate_transaction_training_proof_from_boundary( self, ) -> Any | None: """Aggregate only the active candidate window's page deltas.""" runtimes = tuple( runtime for runtime in self._paged_none_runtimes_boundary() if bool(runtime.family_page_mask_t.detach().cpu().any()) ) if not runtimes: return None from resynthesis.none_paging import combine_page_training_proofs proof = combine_page_training_proofs( tuple( runtime.candidate_transaction_training_proof_components_boundary() for runtime in runtimes ) ) if proof.family_page_ids_t.numel() != self._paged_none_training_page_count: raise RuntimeError( "NoNE candidate transaction proof denominator differs" ) return proof def paged_none_scale_evidence_from_boundary( self, retention_passed_t: torch.Tensor, ) -> Any | None: """Derive fresh family-root scale evidence before candidate cleanup. This boundary returns a tensor packet only. It does not inspect storage, allocate pages, mutate the catalog, or claim a child page is trained. """ if self._paged_none_training_branch_scope is not None: # A disjoint branch owns local optimizer evidence only. Scale # admission remains a global merged-model decision. return None accepted_proof = self.paged_none_training_proof_from_boundary() candidate_proof = self.paged_none_candidate_training_proof_from_boundary() if accepted_proof is None or candidate_proof is None: return None family_root_ids_t = self._paged_none_family_root_page_ids_t_boundary.to( device=candidate_proof.family_page_ids_t.device, dtype=torch.long, ) if family_root_ids_t.numel() != self._paged_none_family_root_count: raise RuntimeError("NoNE family-root scale identity differs") # The live training proof covers only pages still in training; # generation-proven roots are trained by definition and cannot # produce fresh scale pressure. Restrict the evidence surface to # roots present in the proof instead of asserting on their absence. proof_page_ids_t = candidate_proof.family_page_ids_t.detach().to( device=family_root_ids_t.device, dtype=torch.long, ) root_in_training_t = ( family_root_ids_t.unsqueeze(1) .eq(proof_page_ids_t.unsqueeze(0)) .any(dim=1) ) family_root_ids_t = family_root_ids_t[root_in_training_t] if family_root_ids_t.numel() < 1: return None from resynthesis.none_paging import derive_none_scale_evidence return derive_none_scale_evidence( accepted_proof=accepted_proof, candidate_proof=candidate_proof, family_root_page_ids_t=family_root_ids_t, retention_passed_t=retention_passed_t, ) def _paged_none_training_proof_record_boundary( self, proof: Any | None, *, schema: str, ) -> dict[str, Any] | None: """Serialize one model-owned family proof at an external boundary.""" if proof is None: return None branch_scope = self._paged_none_training_branch_scope branch_scope_active = branch_scope is not None proof_tensors = ( proof.family_page_ids_t, proof.route_count_t, proof.gradient_update_count_t, proof.gradient_norm_t, proof.parameter_delta_norm_t, proof.gradient_signature_t, proof.route_coverage_t, proof.gradient_coverage_t, proof.distinct_gradient_t, proof.finite_t, proof.promotion_ready_t, ) proof_device = proof.family_page_ids_t.device if any(tensor.device != proof_device for tensor in proof_tensors): raise RuntimeError("NoNE family proof tensors occupy different devices") # This is an explicit checkpoint/receipt I/O boundary. Pack coherent # proof columns on device first, then perform one transfer per dtype. # The former field-by-field ``cpu().tolist()`` sequence issued nine # independent CUDA readbacks and repeatedly synchronized the training # stream; live r152 profiles placed 22.5% of a transaction sample in # the gradient-signature readback alone. Page/count identity remains # int64-exact and all floating proof values remain FP32-exact. integer_proof_t = torch.stack( ( proof.family_page_ids_t.detach().to(dtype=torch.long), proof.route_count_t.detach().to(dtype=torch.long), proof.gradient_update_count_t.detach().to(dtype=torch.long), ), dim=1, ).cpu() floating_proof_t = torch.cat( ( proof.gradient_norm_t.detach().reshape(-1, 1).float(), proof.parameter_delta_norm_t.detach().reshape(-1, 1).float(), proof.gradient_signature_t.detach().reshape( proof.family_page_ids_t.numel(), -1, ).float(), ), dim=1, ).cpu() scalar_proof_t = torch.stack( ( proof.route_coverage_t.detach().reshape(()), proof.gradient_coverage_t.detach().reshape(()), proof.distinct_gradient_t.detach().reshape(()), proof.finite_t.detach().reshape(()), proof.promotion_ready_t.detach().reshape(()), ) ).to(device="cpu", dtype=torch.bool) training_page_ids = integer_proof_t[:, 0].tolist() training_page_count = len(training_page_ids) training_family_root_count = self._paged_none_training_family_root_count branch_local_full_scope_traversal_verified = False if branch_scope_active: assert branch_scope is not None family_root_ids_t = self._paged_none_family_root_page_ids_t_boundary proof_page_ids_t = integer_proof_t[:, 0] if not torch.equal(proof_page_ids_t, branch_scope.page_ids_t): raise RuntimeError( "NoNE branch cumulative proof differs from its full scope" ) training_family_root_count = int( family_root_ids_t.unsqueeze(1) .eq(proof_page_ids_t.unsqueeze(0)) .any(dim=1) .sum() ) branch_local_full_scope_traversal_verified = bool( integer_proof_t[:, 1:].gt(0).all() and floating_proof_t[:, :2].gt(0).all() and torch.isfinite(floating_proof_t).all() and scalar_proof_t[0] and scalar_proof_t[1] and scalar_proof_t[3] ) record = { "schema": schema, "familyRootCount": training_family_root_count, "trainingPageCount": training_page_count, "globalTrainingPageCount": ( self._paged_none_global_training_page_count ), "fullPhysicalPageBankTraversalRequired": ( self._paged_none_full_physical_page_bank_traversal_required and not branch_scope_active ), "globalFullPhysicalPageBankTraversalClaimed": False, "globalTrainingClaimed": False, "branchScopeActive": branch_scope_active, "branchLocalFullScopeTraversalRequired": branch_scope_active, "trainingPageIds": training_page_ids, "familyPageIds": training_page_ids, "routeCounts": integer_proof_t[:, 1].tolist(), "gradientUpdateCounts": integer_proof_t[:, 2].tolist(), "gradientNorms": floating_proof_t[:, 0].tolist(), "parameterDeltaNorms": floating_proof_t[:, 1].tolist(), "gradientSignatures": floating_proof_t[:, 2:].tolist(), "routeCoverage": bool(scalar_proof_t[0]), "gradientCoverage": bool(scalar_proof_t[1]), "distinctGradients": bool(scalar_proof_t[2]), "finite": bool(scalar_proof_t[3]), "promotionReady": bool(scalar_proof_t[4]), } if branch_scope is not None: record["branchScope"] = branch_scope.external_record_boundary() record["branchLocalFullScopeTraversalVerified"] = ( branch_local_full_scope_traversal_verified ) record["branchOwnedChangedSubsetRequired"] = True record["branchOwnedChangedSubsetVerified"] = True return record def _paged_none_branch_training_proof_subset_boundary( self, proof: Any | None, ) -> Any | None: """Bind a branch transaction to its exact positive changed-page subset.""" if proof is None: return None if self._paged_none_training_branch_scope is None: return proof from resynthesis.none_paging import ( NoNEPageTrainingProofPacket, combine_page_training_proofs, ) retained_mask_t = ( proof.route_count_t.detach().gt(0) & proof.gradient_update_count_t.detach().gt(0) & proof.gradient_norm_t.detach().gt(0) & proof.parameter_delta_norm_t.detach().gt(0) & torch.isfinite(proof.gradient_norm_t.detach()) & torch.isfinite(proof.parameter_delta_norm_t.detach()) & torch.isfinite(proof.gradient_signature_t.detach()).all(dim=1) ) retained_indices_t = retained_mask_t.nonzero( as_tuple=False ).reshape(-1) if retained_indices_t.numel() < 1: return None scalar_false_t = retained_mask_t.new_zeros(()) return combine_page_training_proofs( ( NoNEPageTrainingProofPacket( family_page_ids_t=proof.family_page_ids_t.index_select( 0, retained_indices_t, ), route_count_t=proof.route_count_t.index_select( 0, retained_indices_t, ), gradient_update_count_t=( proof.gradient_update_count_t.index_select( 0, retained_indices_t, ) ), gradient_norm_t=proof.gradient_norm_t.index_select( 0, retained_indices_t, ), parameter_delta_norm_t=( proof.parameter_delta_norm_t.index_select( 0, retained_indices_t, ) ), gradient_signature_t=( proof.gradient_signature_t.index_select( 0, retained_indices_t, ) ), route_coverage_t=scalar_false_t, gradient_coverage_t=scalar_false_t, distinct_gradient_t=scalar_false_t, finite_t=scalar_false_t, promotion_ready_t=scalar_false_t, ), ) ) def _paged_none_branch_candidate_training_proof_from_boundary( self, ) -> Any | None: """Return only page deltas produced by the active branch transaction.""" return self._paged_none_branch_training_proof_subset_boundary( self.paged_none_candidate_transaction_training_proof_from_boundary() ) def paged_none_training_proof_record_boundary( self, ) -> dict[str, Any] | None: """Serialize family proof only at the explicit checkpoint/log boundary.""" proof = self.paged_none_training_proof_from_boundary() return self._paged_none_training_proof_record_boundary( proof, schema="nnf.resynthesis.none_family_training_proof.v1", ) def paged_none_candidate_training_proof_record_boundary( self, ) -> dict[str, Any] | None: """Serialize tentative candidate family proof without retaining it.""" proof = self.paged_none_candidate_training_proof_from_boundary() return self._paged_none_training_proof_record_boundary( proof, schema="nnf.resynthesis.none_family_candidate_training_proof.v1", ) def external_checkpoint_binding_required_boundary(self) -> torch.Tensor: """Tell checkpoint I/O whether immutable pages are part of this graph.""" return self.candidate_page_update_proven.new_tensor( self._paged_none_store_boundary is not None, dtype=torch.bool, ) def _paged_none_sparse_graph_layer_record_boundary( self, ) -> dict[str, Any] | None: """Rebuild the optional catalog-bound sparse-layer I/O authority.""" count = self._paged_none_sparse_graph_layer_count if count is None: if ( self._paged_none_sparse_graph_layer_ids_sha256 or self._paged_none_sparse_graph_layer_catalog_sha256 ): raise RuntimeError( "NoNE sparse graph-layer boundary is incomplete" ) return None if ( count < 1 or len(self._paged_none_sparse_graph_layer_ids_sha256) != 64 or len(self._paged_none_sparse_graph_layer_catalog_sha256) != 64 ): raise RuntimeError("NoNE sparse graph-layer boundary is malformed") return { "schema": "nnf.resynthesis.none_sparse_graph_layer_authority.v1", "graphLayerIdsSha256": ( self._paged_none_sparse_graph_layer_ids_sha256 ), "physicalGraphLayerCount": count, "pageCatalogSha256": ( self._paged_none_sparse_graph_layer_catalog_sha256 ), "onePageObjectPerSparseGraphLayer": True, "routeGradientDeltaProofComplete": False, "heldoutProofComplete": False, "coldReloadProofComplete": False, "trainingClaimed": False, "promotionEligible": False, } def _none_external_record_boundary( self, binding: Any, *, candidate_page_update: bool, ) -> dict[str, Any]: composition_path = self._paged_none_composition_path store = self._paged_none_store_boundary if composition_path is None or store is None: raise RuntimeError("NoNE external checkpoint binding is not attached") generation_record = binding.external_record_boundary() coordinator = self._paged_none_replica_coordinator_boundary # Candidate telemetry is retained before snapshot staging. Serialize # the accepted cumulative proof here so the active transaction is # represented exactly once while its generation binding continues to # carry only the changed-page subset. training_proof = self.paged_none_training_proof_from_boundary() record = { "schema": ( "nnf.resynthesis.none_checkpoint_external_state.v2" if coordinator is not None else "nnf.resynthesis.none_checkpoint_external_state.v1" ), "compositionPath": str(composition_path), "compositionSha256": self._paged_none_composition_sha256, "sourceCheckpointSha256": (self._paged_none_source_checkpoint_sha256), "storeRoot": str(store.root), "candidatePageUpdate": candidate_page_update, "generationBinding": generation_record, "trainingProof": self._paged_none_training_proof_record_boundary( training_proof, schema="nnf.resynthesis.none_family_training_proof.v1", ), } sparse_graph_layer_record = ( self._paged_none_sparse_graph_layer_record_boundary() ) if sparse_graph_layer_record is not None: record["sparseGraphLayers"] = sparse_graph_layer_record if coordinator is not None: record.update( { "replicaReceiptPath": str(coordinator.receipt_path), "replicaReceiptSha256": (self._paged_none_replica_receipt_sha256), "replicaStoreRoots": list(coordinator.store_roots_boundary), "replicaDurabilityComplete": (coordinator.durability_complete), "canonicalPointerAdvancesLast": True, } ) if self._paged_none_training_branch_scope is not None: record["branchScope"] = ( self._paged_none_training_branch_scope.external_record_boundary() ) if self._active_graph_growth_plan_path is not None: record.update( { "activeGraphGrowthPlanPath": str( self._active_graph_growth_plan_path ), "activeGraphGrowthPlanSha256": ( self._active_graph_growth_plan_sha256 ), "graphAdaptationReceiptPath": ( str(self._graph_adaptation_receipt_path) if self._graph_adaptation_receipt_path is not None else None ), "activeGraphPlanTrainingClaimed": False, "activeGraphPlanPromotionEligible": False, } ) return record def checkpoint_external_state_boundary(self) -> dict[str, Any] | None: """Return accepted external state for an additive checkpoint sidecar.""" store = self._paged_none_store_boundary if store is None: return None binding = store.current_generation_binding_boundary() return self._none_external_record_boundary( binding, candidate_page_update=False, ) @staticmethod def _derived_component_digest_boundary( source_digest_t: torch.Tensor, component: str, ) -> torch.Tensor: from resynthesis.none_paging import digest_tensor source = ( source_digest_t.detach() .cpu() .to(dtype=torch.uint8) .reshape(-1) .numpy() .tobytes() ) return digest_tensor( hashlib.sha256(source + b"\x00" + component.encode("utf-8")).hexdigest() ) def prepare_candidate_page_updates_from_boundary( self, optimizer: Any, ) -> torch.Tensor: """Apply page-local optimizer state while accepted storage stays frozen.""" update_proven_t = self.candidate_page_update_proven.new_zeros(()) runtimes = self._paged_none_runtimes_boundary() if not runtimes: return self.candidate_page_update_proven.clone() # Every runtime owns one disjoint science-layer page catalog and one # independent D2H transfer stream. Launch their page-local optimizer # updates concurrently, then join every worker before combining proof. # Accepted storage remains frozen and no runtime may publish authority # here; a peer failure therefore rolls the complete candidate # transaction back after all workers have settled. with ThreadPoolExecutor(max_workers=min(4, len(runtimes))) as executor: futures = [ executor.submit( runtime.prepare_candidate_page_updates_boundary, optimizer, ) for runtime in runtimes ] runtime_proofs = [future.result() for future in futures] for runtime_proof_t in runtime_proofs: if not isinstance(runtime_proof_t, torch.Tensor): raise RuntimeError("NoNE page update returned no tensor proof") update_proven_t = update_proven_t | runtime_proof_t.to( device=update_proven_t.device, dtype=torch.bool, ) with torch.no_grad(): self.candidate_page_update_proven.logical_or_(update_proven_t) return self.candidate_page_update_proven.clone() def spill_candidate_page_journals_from_boundary(self) -> torch.Tensor: """Bound one routed cohort's host journal without granting authority.""" runtimes = self._paged_none_runtimes_boundary() spilled_t = self.candidate_page_update_proven.new_zeros( (), dtype=torch.long, ) if not runtimes: return spilled_t journal_bytes = sum( runtime.candidate_page_journal_storage_bytes_boundary() for runtime in runtimes ) journal_budget_bytes = ( _candidate_page_journal_budget_bytes_boundary() ) if journal_bytes <= journal_budget_bytes: return spilled_t # Every runtime owns an isolated proposal-local scratch window. Spill # those filesystem boundaries concurrently only after their shared # bounded host budget is crossed, then join every writer before # returning or propagating a failure so rollback can never race an # in-flight atomic replacement. with ThreadPoolExecutor(max_workers=min(4, len(runtimes))) as executor: futures = [ executor.submit(runtime.spill_candidate_page_journal_boundary) for runtime in runtimes ] runtime_spilled = [future.result() for future in futures] for runtime_spilled_t in runtime_spilled: if not isinstance(runtime_spilled_t, torch.Tensor): raise RuntimeError( "NoNE candidate journal spill returned no tensor proof" ) spilled_t = spilled_t + runtime_spilled_t.to( device=spilled_t.device, dtype=torch.long, ).reshape(()) return spilled_t def defer_dense_expert_gradients_for_paged_training_boundary( self, ) -> torch.Tensor: """Keep dense experts active while paging their parameter gradients out. The inherited attention and recurrent experts still execute in every layer and remain part of the checkpoint. Paged knowledge updates train the external pages plus the RBO/Fabric, routers, FFN/adapters, C/R controls, and completion surfaces without allocating dense-core parameter gradients that cannot fit beside the native parent on one accelerator. The matching restore boundary runs after each decode arm. """ if ( self._paged_sparse_deferred_parameters or self._paged_sparse_zero_defer_active ): raise RuntimeError("paged sparse gradient boundary is already active") if self._paged_none_store_boundary is None: return self.candidate_page_update_proven.new_zeros( (), dtype=torch.long, ) deferred: list[nn.Parameter] = [] if self._fast_release_training_active: causal_graph = self.science_stack.causal_algebra_world_graph functional_owned_parameter_ids = { id(parameter) for name, parameter in self.named_parameters() if ( self._training_branch_functional_graph_active and name in self._page_branch_functional_graph_parameter_names ) } for name, parameter in self.named_parameters(): keep_trainable = ( id(parameter) in functional_owned_parameter_ids or parameter.ndim == 0 or ".paged_expert_runtime." in name or name.endswith(".ffn_gate_up") or name.endswith(".ffn_down") or causal_graph.page_coupled_training_parameter_boundary( parameter ) ) if parameter.requires_grad and not keep_trainable: parameter.requires_grad_(False) deferred.append(parameter) else: for layer_id in self._paged_none_expected_layer_ids_boundary: layer = self.science_stack._layer(layer_id) for expert in (layer.attention_expert, layer.recurrent_expert): for parameter in expert.parameters(): if parameter.requires_grad: parameter.requires_grad_(False) deferred.append(parameter) if not deferred: self._paged_sparse_zero_defer_active = True return self.candidate_page_update_proven.new_zeros( (), dtype=torch.long, ) self._paged_sparse_deferred_parameters = tuple(deferred) return self.candidate_page_update_proven.new_tensor( len(deferred), dtype=torch.long, ) def restore_dense_expert_gradients_after_paged_training_boundary( self, deferred_count_t: torch.Tensor, ) -> torch.Tensor: """Restore the exact dense parameter set deferred for one decode arm.""" if deferred_count_t.numel() != 1: raise ValueError("paged sparse gradient ticket must be scalar") deferred = self._paged_sparse_deferred_parameters if not deferred: if self._paged_none_store_boundary is None: return deferred_count_t.new_zeros((), dtype=torch.long) if self._paged_sparse_zero_defer_active: torch._assert_async( deferred_count_t.eq(0), "paged sparse zero-gradient ticket differs", ) self._paged_sparse_zero_defer_active = False return deferred_count_t.new_zeros((), dtype=torch.long) raise RuntimeError("paged sparse gradient boundary is not active") restored_count_t = deferred_count_t.new_tensor( len(deferred), dtype=torch.long, ) torch._assert_async( restored_count_t.eq(deferred_count_t.to(restored_count_t)), "paged sparse gradient ticket differs", ) for parameter in deferred: parameter.requires_grad_(True) self._paged_sparse_deferred_parameters = () self._paged_sparse_zero_defer_active = False return restored_count_t def bind_candidate_layer_imports_from_boundary( self, *, source_stores: tuple[Any, ...], packets: tuple[Any, ...], ) -> torch.Tensor: """Bind verified branch inputs for the next ordinary candidate window. This explicit storage/checkpoint boundary does not select an inference route. The imported objects remain tentative until the model routes through the merged candidate and the usual held-out, anti-forgetting, checkpoint, and cold-reload gates retain it. """ from resynthesis.none_paging import ( NoNEImmutablePageStore, NoNELayerPageImportPacket, ) if not packets or len(source_stores) != len(packets): raise ValueError("NoNE branch binding requires one store per packet") if not all(isinstance(store, NoNEImmutablePageStore) for store in source_stores): raise TypeError("NoNE branch binding contains a non-store source") if not all(isinstance(packet, NoNELayerPageImportPacket) for packet in packets): raise TypeError("NoNE branch binding contains a malformed packet") runtimes = self._paged_none_runtimes_boundary() if not runtimes or self._paged_none_store_boundary is None: raise RuntimeError("NoNE branch binding has no paged model authority") if any(bool(runtime.candidate_window_active_t.detach().cpu()) for runtime in runtimes): raise RuntimeError("NoNE branch binding changed an active candidate window") page_ids_t = torch.cat( tuple(packet.page_ids_t.detach().cpu().long() for packet in packets), dim=0, ) if torch.unique(page_ids_t).numel() != page_ids_t.numel(): raise RuntimeError("NoNE branch binding repeats a page") self._candidate_layer_import_source_stores = source_stores self._candidate_layer_import_packets = packets return page_ids_t def _activate_bound_candidate_layer_imports_boundary(self) -> torch.Tensor: """Place bound branch objects inside the current model candidate.""" if not self._candidate_layer_import_packets: return self.candidate_page_update_proven.new_zeros((), dtype=torch.bool) store = self._paged_none_store_boundary if store is None: raise RuntimeError("NoNE branch activation has no target store") runtimes = self._paged_none_runtimes_boundary() target_page_ids_t = torch.cat( tuple(runtime.router.page_catalog_ids_t.detach().cpu().long() for runtime in runtimes), dim=0, ) target_layer_ids_t = torch.cat( tuple( runtime.router.layer_id_t.detach().cpu().long().reshape(()).expand( runtime.router.page_catalog_ids_t.shape[0] ) for runtime in runtimes ), dim=0, ) imported = store.import_trained_layer_page_objects_boundary( source_stores=self._candidate_layer_import_source_stores, packets=self._candidate_layer_import_packets, target_page_ids_t=target_page_ids_t, target_layer_ids_t=target_layer_ids_t, ) activated_page_ids_t = torch.cat( tuple( runtime.attach_imported_candidate_page_objects_boundary(imported) for runtime in runtimes ), dim=0, ) imported_page_ids_t = torch.stack( tuple( binding.page_id_t.detach().cpu().long().reshape(()) for binding in imported.page_objects ) ) if ( activated_page_ids_t.numel() != imported_page_ids_t.numel() or torch.unique(activated_page_ids_t).numel() != activated_page_ids_t.numel() or not torch.equal( torch.sort(activated_page_ids_t).values, torch.sort(imported_page_ids_t).values, ) ): raise RuntimeError("NoNE branch activation did not cover imported pages") promotion_ready_t = imported.training_proof.promotion_ready_t if not isinstance(promotion_ready_t, torch.Tensor): raise RuntimeError("NoNE imported branch returned no tensor proof") activation_t = promotion_ready_t.to( device=self.candidate_page_update_proven.device, dtype=torch.bool, ).reshape(()) with torch.no_grad(): self.candidate_page_update_proven.logical_or_(activation_t) return activation_t def _page_generation_components_boundary( self, *, shared_model_digest_t: torch.Tensor, global_optimizer_digest_t: torch.Tensor, scheduler_digest_t: torch.Tensor, rng_digest_t: torch.Tensor, corpus_digest_t: torch.Tensor, ) -> Any: """Bind pages to executable state, never mutable source observations.""" from resynthesis.none_paging import ( NoNEGenerationComponentPacket, digest_tensor, page_training_proof_digest_t_boundary, ) # Retention precedes generation staging, so accepted cumulative proof # already includes the current transaction exactly once. training_proof = self.paged_none_training_proof_from_boundary() return NoNEGenerationComponentPacket( parent_digest_t=digest_tensor(self._paged_none_source_checkpoint_sha256), shared_model_digest_t=shared_model_digest_t, global_optimizer_digest_t=global_optimizer_digest_t, scheduler_digest_t=scheduler_digest_t, rng_digest_t=rng_digest_t, rbo_digest_t=self._derived_component_digest_boundary( shared_model_digest_t, "rbo", ), fabric_digest_t=self._derived_component_digest_boundary( shared_model_digest_t, "fabric", ), vge_digest_t=self._derived_component_digest_boundary( shared_model_digest_t, "vge", ), router_digest_t=self._derived_component_digest_boundary( shared_model_digest_t, "router", ), corpus_digest_t=corpus_digest_t, training_proof_digest_t=( page_training_proof_digest_t_boundary(training_proof) if training_proof is not None else None ), ) def stage_candidate_page_generation_from_boundary( self, *, shared_model_digest_t: torch.Tensor, global_optimizer_digest_t: torch.Tensor, scheduler_digest_t: torch.Tensor, rng_digest_t: torch.Tensor, corpus_digest_t: torch.Tensor, ) -> dict[str, Any] | None: """Stage all layer-page updates as one checkpoint-bound transaction.""" store = self._paged_none_store_boundary if store is None: return None updated_page_objects = tuple( binding for runtime in self._paged_none_runtimes_boundary() for binding in ( runtime.seal_candidate_page_object_bindings_boundary() ) ) if not updated_page_objects: self._staged_none_generation_binding = None self._reconciled_staged_direct_map_seal_t = None return self._none_external_record_boundary( store.current_generation_binding_boundary(), candidate_page_update=False, ) training_proven_page_ids_t: torch.Tensor | None = None if self._paged_none_training_branch_scope is not None: transaction_proof = ( self._paged_none_branch_candidate_training_proof_from_boundary() ) if transaction_proof is None: raise RuntimeError( "NoNE branch candidate has no retained-change proof" ) updated_page_ids_t = torch.sort( torch.stack( tuple( binding.page_id_t.detach().cpu().long().reshape(()) for binding in updated_page_objects ) ) ).values proof_page_ids_t = torch.sort( transaction_proof.family_page_ids_t.detach().cpu().long() ).values if not torch.equal(updated_page_ids_t, proof_page_ids_t): raise RuntimeError( "NoNE branch candidate page objects differ from its proof" ) # Thread the training-proven page ids into the staged generation so # its manifest records trainingProvenPageIds, matching the other # caller paths (none_paging.py stage/coordinator). Without this the # branch's staged generations omit their proven-page set and later # saved-vs-live training-proof checks mismatch (the # "descendant/accepted-pointer differs" crash family). training_proven_page_ids_t = proof_page_ids_t components = self._page_generation_components_boundary( shared_model_digest_t=shared_model_digest_t, global_optimizer_digest_t=global_optimizer_digest_t, scheduler_digest_t=scheduler_digest_t, rng_digest_t=rng_digest_t, corpus_digest_t=corpus_digest_t, ) coordinator = self._paged_none_replica_coordinator_boundary binding = ( coordinator.stage_generation_from_page_objects_boundary( updated_page_objects=updated_page_objects, components=components, training_proven_page_ids_t=training_proven_page_ids_t, ) if coordinator is not None else store.stage_generation_from_page_objects_boundary( updated_page_objects=updated_page_objects, components=components, training_proven_page_ids_t=training_proven_page_ids_t, ) ) self._staged_none_generation_binding = binding self._reconciled_staged_direct_map_seal_t = None return self._none_external_record_boundary( binding, candidate_page_update=True, ) def accept_candidate_page_generation_from_boundary( self, *, checkpoint_path: str | Path | None = None, optimizer_path: str | Path | None = None, external_state_path: str | Path | None = None, live_checkpoint_retention_t: torch.Tensor | None = None, ) -> torch.Tensor: """Advance page authority after the shared checkpoint sidecar is durable.""" store = self._paged_none_store_boundary if store is None: return self.candidate_page_update_proven.new_zeros( (), dtype=torch.long, ) binding = self._staged_none_generation_binding if binding is not None: composition_path = self._paged_none_composition_path migration_receipt_path = self._paged_none_migration_receipt_path if ( checkpoint_path is None or optimizer_path is None or external_state_path is None or composition_path is None or migration_receipt_path is None ): raise RuntimeError( "NoNE candidate acceptance lacks complete graph authority" ) from resynthesis.none_paging import ( build_graph_authority_binding_boundary, ) graph_authority = build_graph_authority_binding_boundary( generation_binding=binding, checkpoint_path=Path(checkpoint_path), optimizer_path=Path(optimizer_path), external_state_path=Path(external_state_path), composition_path=composition_path, migration_receipt_path=migration_receipt_path, replica_receipt_path=self._paged_none_replica_receipt_path, ) page_ids_t = torch.cat( tuple( runtime.router.page_catalog_ids_t.detach().cpu().long() for runtime in self._paged_none_runtimes_boundary() ) ) live_checkpoint_retention = False if live_checkpoint_retention_t is not None: if live_checkpoint_retention_t.numel() != 1: raise RuntimeError( "NoNE live checkpoint retention decision must be scalar" ) live_checkpoint_retention = bool( live_checkpoint_retention_t.detach() .cpu() .to(dtype=torch.bool) .reshape(()) ) retained_checkpoint_sha256_t = ( graph_authority.checkpoint_sha256_t if live_checkpoint_retention else None ) coordinator = self._paged_none_replica_coordinator_boundary if coordinator is not None: coordinator.accept_staged_generation( binding, graph_authority, resident_page_ids_t=( page_ids_t if live_checkpoint_retention else None ), retained_checkpoint_sha256_t=( retained_checkpoint_sha256_t ), ) else: store.accept_staged_generation( binding, graph_authority, ) if retained_checkpoint_sha256_t is not None: store.adopt_written_graph_authority_boundary( graph_authority=graph_authority, page_ids_t=page_ids_t, retained_checkpoint_sha256_t=( retained_checkpoint_sha256_t ), ) store.bind_resident_graph_authority_boundary( checkpoint_sha256_t=graph_authority.checkpoint_sha256_t, composition_sha256_t=graph_authority.composition_sha256_t, page_ids_t=page_ids_t, ) generation_t = store.accepted_generation_t() if not isinstance(generation_t, torch.Tensor): raise RuntimeError("NoNE store returned no tensor generation") for runtime in self._paged_none_runtimes_boundary(): runtime.promote_candidate_training_page_cache_boundary( generation_t ) runtime.finish_candidate_boundary() coordinator = self._paged_none_replica_coordinator_boundary if coordinator is not None: coordinator.discard_staged_generation_boundary() self._staged_none_generation_binding = None self._reconciled_staged_direct_map_seal_t = None self._candidate_layer_import_source_stores = () self._candidate_layer_import_packets = () return generation_t def bind_loaded_paged_graph_authority_from_boundary( self, checkpoint_sha256: str, ) -> torch.Tensor: """Fence the live router graph to the checkpoint loaded by this process.""" store = self._paged_none_store_boundary if store is None: return self.candidate_page_update_proven.new_zeros( (), dtype=torch.long, ) if len(checkpoint_sha256) != 64 or not self._paged_none_composition_sha256: raise RuntimeError("NoNE loaded graph identity is malformed") from resynthesis.none_paging import digest_tensor page_ids_t = torch.cat( tuple( runtime.router.page_catalog_ids_t.detach().cpu().long() for runtime in self._paged_none_runtimes_boundary() ) ) generation_t = store.bind_resident_graph_authority_boundary( checkpoint_sha256_t=digest_tensor(checkpoint_sha256), composition_sha256_t=digest_tensor( self._paged_none_composition_sha256 ), page_ids_t=page_ids_t, ) if not isinstance(generation_t, torch.Tensor): raise RuntimeError("NoNE graph authority returned no tensor generation") return generation_t def discard_candidate_page_generation_from_boundary( self, ) -> torch.Tensor: """Drop candidate residency while leaving accepted authority untouched.""" store = self._paged_none_store_boundary if store is None: return self.candidate_page_update_proven.new_zeros( (), dtype=torch.long, ) first_cleanup_error: Exception | None = None try: self._finish_all_paged_none_candidate_boundaries() except Exception as error: first_cleanup_error = error coordinator = self._paged_none_replica_coordinator_boundary try: if coordinator is not None: coordinator.discard_staged_generation_boundary() else: store.discard_staged_generation_boundary() except Exception as error: if first_cleanup_error is None: first_cleanup_error = error else: first_cleanup_error.add_note( "additional NoNE staged-generation lease cleanup failure: " f"{error!r}" ) self._staged_none_generation_binding = None self._reconciled_staged_direct_map_seal_t = None with torch.no_grad(): self.candidate_page_update_proven.zero_() if first_cleanup_error is not None: raise first_cleanup_error generation_t = store.accepted_generation_t() if not isinstance(generation_t, torch.Tensor): raise RuntimeError("NoNE store returned no tensor generation") return generation_t @staticmethod def _validated_training_branch_parent_restore_boundary( *, record: dict[str, Any], branch_scope: Any, branch_binding: Any, branch_graph: Any, canonical_parent_paged_lineage: dict[str, Any] | None, ) -> bool: """Validate an exact canonical-parent restore inside an isolated branch. A page-only branch writes through its own v1 store, while its initial rollback checkpoint remains the accepted replicated v2 parent. That is a legitimate storage projection only when the branch pointer still names the signed parent and its pointer-owned graph authority binds the exact canonical sidecar supplied for restore. """ if branch_scope is None or record.get("schema") != ( "nnf.resynthesis.none_checkpoint_external_state.v2" ): return False if canonical_parent_paged_lineage is None: raise RuntimeError( "NoNE training branch parent restore has no canonical lineage" ) generation = record.get("generationBinding") training_proof = record.get("trainingProof") replica_roots = record.get("replicaStoreRoots") branch_record = branch_binding.external_record_boundary() graph_record = branch_graph.external_record_boundary() graph_external = graph_record.get("externalState") graph_replica = graph_record.get("replicaReceipt") if ( not isinstance(generation, dict) or generation != branch_record or not torch.equal( branch_binding.session_id_t.detach().cpu().long(), branch_scope.session_id_t, ) or not torch.equal( branch_binding.generation_t.detach().cpu().long().reshape(()), branch_scope.parent_generation_t, ) or not torch.equal( branch_binding.manifest_payload_sha256_t.detach() .cpu() .to(dtype=torch.uint8) .reshape(-1), branch_scope.parent_manifest_payload_sha256_t, ) or record.get("candidatePageUpdate") is not False or record.get("branchScope") is not None or not isinstance(training_proof, dict) or not isinstance(training_proof.get("trainingPageIds"), list) or not set(branch_scope.page_ids_t.detach().cpu().long().tolist()).issubset( set(training_proof["trainingPageIds"]) ) or not isinstance(replica_roots, list) or not replica_roots or any( not isinstance(root, str) or not root for root in replica_roots ) or len(set(replica_roots)) != len(replica_roots) or len(replica_roots) != canonical_parent_paged_lineage.get("replicaStoreCount") or record.get("storeRoot") not in replica_roots or record.get("replicaReceiptSha256") != canonical_parent_paged_lineage.get("replicaReceiptSha256") or record.get("replicaDurabilityComplete") != canonical_parent_paged_lineage.get("replicaDurabilityComplete") or record.get("canonicalPointerAdvancesLast") is not True or canonical_parent_paged_lineage.get( "replicatedGenerationTransaction" ) is not True or canonical_parent_paged_lineage.get( "canonicalPointerAdvancesLast" ) is not True or not isinstance(graph_external, dict) or not isinstance(graph_replica, dict) or graph_replica.get("path") != record.get("replicaReceiptPath") or graph_replica.get("sha256") != record.get("replicaReceiptSha256") ): raise RuntimeError( "NoNE training branch parent checkpoint lineage differs" ) external_path_value = graph_external.get("path") external_sha256 = graph_external.get("sha256") if ( not isinstance(external_path_value, str) or not external_path_value or not isinstance(external_sha256, str) or len(external_sha256) != 64 ): raise RuntimeError( "NoNE training branch parent sidecar identity differs" ) external_path = Path(external_path_value).expanduser().resolve() if ( not external_path.is_file() or _file_sha256_boundary(external_path) != external_sha256 ): raise RuntimeError( "NoNE training branch parent sidecar identity differs" ) external_envelope = json.loads(external_path.read_text(encoding="utf-8")) if ( not isinstance(external_envelope, dict) or external_envelope.get("schema") != "nnf.resynthesis.additive_external_checkpoint_binding.v1" or external_envelope.get("externalState") != record ): raise RuntimeError( "NoNE training branch parent sidecar identity differs" ) return True @staticmethod def _validated_training_branch_descendant_chain_boundary( *, store: NoNEImmutablePageStore, branch_scope: NoNETrainingBranchScopePacket, expected_generation_record: dict[str, Any] | None = None, ) -> NoNEGenerationBinding: """Prove one live isolated-store generation descends from its scope parent.""" from resynthesis.none_paging import ( _file_identity, _file_identity_record, digest_tensor, validate_training_branch_scope_boundary, ) validate_training_branch_scope_boundary(branch_scope) live = store.current_generation_binding_boundary() live_generation_record = live.external_record_boundary() if ( expected_generation_record is not None and live_generation_record != expected_generation_record ): raise RuntimeError( "NoNE training branch descendant pointer differs from checkpoint" ) if not torch.equal( live.session_id_t.detach().cpu().long(), branch_scope.session_id_t, ): raise RuntimeError("NoNE training branch descendant crossed sessions") parent_generation = int(branch_scope.parent_generation_t) live_generation = int(live.generation_t) if live_generation <= parent_generation: raise RuntimeError( "NoNE training branch checkpoint is not a parent descendant" ) session_root = store._session_root if ( session_root is None or not session_root.resolve().is_relative_to( store.sessions_root.resolve() ) ): raise RuntimeError("NoNE training branch session root differs") scope_page_ids = set( branch_scope.page_ids_t.detach().cpu().long().reshape(-1).tolist() ) branch_scope_record = branch_scope.external_record_boundary() load_generation = getattr( store, "_load_generation_binding_boundary", None, ) cache_path = _training_branch_descendant_chain_cache_path_boundary( store_root=store.root, branch_scope_record=branch_scope_record, live_generation_record=live_generation_record, ) cache_loaded = _load_training_branch_descendant_chain_cache_boundary( cache_path=cache_path, store_root=store.root, session_root=session_root.resolve(), branch_scope_record=branch_scope_record, live_generation_record=live_generation_record, scope_page_ids=scope_page_ids, ) if not cache_loaded and callable(load_generation): cache_loaded = ( _load_training_branch_descendant_chain_incremental_cache_boundary( cache_path=cache_path, store_root=store.root, session_root=session_root.resolve(), branch_scope_record=branch_scope_record, live_generation_record=live_generation_record, scope_page_ids=scope_page_ids, load_generation=load_generation, ) ) if cache_loaded: reobserved = store.discover_accepted_pointer_boundary() if ( reobserved.external_record_boundary() != live_generation_record ): raise RuntimeError( "NoNE training branch accepted pointer changed during restore" ) return live def verified_manifest( binding: NoNEGenerationBinding, ) -> tuple[NoNEGenerationBinding, dict[str, Any]]: if callable(load_generation): loaded, manifest = load_generation( binding.manifest_relative_path, expected_manifest_sha256=bytes( binding.manifest_sha256_t.detach() .cpu() .to(dtype=torch.uint8) .reshape(-1) .tolist() ).hex(), expected_payload_sha256=bytes( binding.manifest_payload_sha256_t.detach() .cpu() .to(dtype=torch.uint8) .reshape(-1) .tolist() ).hex(), ) if ( not isinstance(loaded, type(binding)) or not isinstance(manifest, dict) ): raise RuntimeError( "NoNE training branch generation load is malformed" ) return loaded, manifest loaded = store.verify_generation_boundary( generation_t=binding.generation_t, manifest_sha256_t=binding.manifest_sha256_t, manifest_payload_sha256_t=( binding.manifest_payload_sha256_t ), ) manifest_path = ( session_root / binding.manifest_relative_path ).resolve() if ( not manifest_path.is_relative_to(session_root.resolve()) or not manifest_path.is_file() or not torch.equal( digest_tensor(_file_sha256_boundary(manifest_path)), binding.manifest_sha256_t.detach() .cpu() .to(dtype=torch.uint8) .reshape(-1), ) ): raise RuntimeError( "NoNE training branch descendant manifest differs" ) manifest = json.loads( manifest_path.read_text(encoding="utf-8") ) if not isinstance(manifest, dict): raise RuntimeError( "NoNE training branch descendant manifest differs" ) return loaded, manifest observed: set[tuple[int, str]] = set() generation_rows: list[dict[str, Any]] = [] current = live while int(current.generation_t) != parent_generation: generation = int(current.generation_t) payload_sha256 = current.external_record_boundary().get( "manifestPayloadSha256" ) identity = (generation, str(payload_sha256)) updated_page_ids = ( current.updated_page_ids_t.detach() .cpu() .long() .reshape(-1) .tolist() ) if ( generation < parent_generation or identity in observed or not updated_page_ids or len(set(updated_page_ids)) != len(updated_page_ids) or not set(updated_page_ids).issubset(scope_page_ids) ): raise RuntimeError( "NoNE training branch descendant ancestry differs" ) observed.add(identity) verified, manifest = verified_manifest(current) if ( verified.external_record_boundary() != current.external_record_boundary() ): raise RuntimeError( "NoNE training branch descendant generation changed" ) manifest_path = ( session_root / current.manifest_relative_path ).resolve() if ( not manifest_path.is_relative_to(session_root.resolve()) or not manifest_path.is_file() ): raise RuntimeError( "NoNE training branch descendant manifest differs" ) parent_value = manifest.get("parentGeneration") parent_payload_sha256 = manifest.get( "parentManifestPayloadSha256" ) if ( not isinstance(manifest, dict) or manifest.get("generation") != generation or manifest.get("updatedPageIds") != updated_page_ids or not isinstance(parent_value, int) or isinstance(parent_value, bool) or parent_value >= generation or parent_value != int(current.parent_generation_t) or not isinstance(parent_payload_sha256, str) or len(parent_payload_sha256) != 64 ): raise RuntimeError( "NoNE training branch descendant manifest ancestry differs" ) generation_rows.append( { "binding": current.external_record_boundary(), "parentManifestPayloadSha256": ( parent_payload_sha256 ), "manifestIdentity": _file_identity_record( _file_identity(manifest_path) ), } ) current = store.verify_generation_boundary( generation_t=current.parent_generation_t, manifest_payload_sha256_t=digest_tensor( parent_payload_sha256 ), ) if ( not torch.equal( current.session_id_t.detach().cpu().long(), branch_scope.session_id_t, ) or not torch.equal( current.manifest_payload_sha256_t.detach() .cpu() .to(dtype=torch.uint8) .reshape(-1), branch_scope.parent_manifest_payload_sha256_t, ) ): raise RuntimeError( "NoNE training branch descendant does not reach its exact parent" ) if callable(load_generation): verified_parent, parent_manifest = verified_manifest(current) if ( verified_parent.external_record_boundary() != current.external_record_boundary() or parent_manifest.get("generation") != parent_generation ): raise RuntimeError( "NoNE training branch descendant parent generation changed" ) parent_manifest_path = ( session_root / current.manifest_relative_path ).resolve() generation_rows.append( { "binding": current.external_record_boundary(), "parentManifestPayloadSha256": parent_manifest.get( "parentManifestPayloadSha256" ), "manifestIdentity": _file_identity_record( _file_identity(parent_manifest_path) ), } ) reobserved = store.discover_accepted_pointer_boundary() if ( reobserved.external_record_boundary() != live_generation_record ): raise RuntimeError( "NoNE training branch accepted pointer changed during restore" ) if callable(load_generation): _write_training_branch_descendant_chain_cache_boundary( cache_path=cache_path, store_root=store.root, branch_scope_record=branch_scope_record, live_generation_record=live_generation_record, generation_rows=generation_rows, ) return live @staticmethod def _validated_training_branch_descendant_restore_boundary( *, record: dict[str, Any], branch_scope: NoNETrainingBranchScopePacket, branch_store_root: str | Path, source_store: NoNEImmutablePageStore, fork_child_branch_scope: NoNETrainingBranchScopePacket | None = None, ) -> tuple[NoNEImmutablePageStore, NoNEGenerationBinding]: """Open only the isolated store bound by an accepted v1 branch sidecar. A sealed fork-parent checkpoint carries the source branch scope and source store path in its immutable sidecar. The copied child store is deliberately rooted elsewhere and owns a strict page/layer subset whose parent is the sidecar's accepted generation. Keep both scopes explicit: ``branch_scope`` authenticates the checkpoint bytes, while ``fork_child_branch_scope`` authenticates the only permitted source-root-to-child-root transition. Ordinary descendant restores still require the sidecar and live store roots to be identical. """ from resynthesis.none_paging import ( NoNEImmutablePageStore, training_branch_proof_from_record_boundary, ) raw_store_root = record.get("storeRoot") resolved_branch_root = Path(branch_store_root).expanduser().resolve() training_proof = record.get("trainingProof") generation = record.get("generationBinding") record_store_root = ( Path(raw_store_root).expanduser().resolve() if isinstance(raw_store_root, str) and raw_store_root and Path(raw_store_root).expanduser().is_absolute() else None ) fork_parent_copy = fork_child_branch_scope is not None if fork_parent_copy: assert fork_child_branch_scope is not None source_scope_record = branch_scope.external_record_boundary() child_scope_record = ( fork_child_branch_scope.external_record_boundary() ) source_page_layer_pairs = set( zip( source_scope_record["pageIds"], source_scope_record["pageLayerIds"], strict=True, ) ) child_page_layer_pairs = set( zip( child_scope_record["pageIds"], child_scope_record["pageLayerIds"], strict=True, ) ) generation_session = ( generation.get("sessionId") if isinstance(generation, dict) else None ) generation_value = ( generation.get("generation") if isinstance(generation, dict) else None ) generation_payload_sha256 = ( generation.get("manifestPayloadSha256") if isinstance(generation, dict) else None ) if ( record_store_root is None or not record_store_root.is_dir() or record_store_root == resolved_branch_root or source_scope_record == child_scope_record or generation_session != source_scope_record["sessionId"] or child_scope_record["sessionId"] != source_scope_record["sessionId"] or type(generation_value) is not int or generation_value != child_scope_record["parentGeneration"] or generation_value <= source_scope_record["parentGeneration"] or generation_payload_sha256 != child_scope_record["parentManifestPayloadSha256"] or not child_page_layer_pairs or not child_page_layer_pairs < source_page_layer_pairs ): raise RuntimeError( "NoNE sealed fork-parent sidecar transition differs" ) replica_fields = ( "replicaReceiptPath", "replicaReceiptSha256", "replicaStoreRoots", "replicaDurabilityComplete", "canonicalPointerAdvancesLast", ) if ( record.get("schema") != "nnf.resynthesis.none_checkpoint_external_state.v1" or not isinstance(record.get("candidatePageUpdate"), bool) or not isinstance(raw_store_root, str) or not raw_store_root or not Path(raw_store_root).expanduser().is_absolute() or ( not fork_parent_copy and record_store_root != resolved_branch_root ) or record.get("branchScope") != branch_scope.external_record_boundary() or not isinstance(training_proof, dict) or training_proof.get("branchScopeActive") is not True or training_proof.get("branchScope") != branch_scope.external_record_boundary() or training_proof.get("branchOwnedChangedSubsetRequired") is not True or training_proof.get("branchOwnedChangedSubsetVerified") is not True or not isinstance(generation, dict) or any(field in record for field in replica_fields) ): raise RuntimeError( "NoNE training branch descendant sidecar identity differs" ) training_branch_proof_from_record_boundary( branch_scope, training_proof, ) branch_store = NoNEImmutablePageStore( resolved_branch_root, object_roots=source_store.object_store_roots_boundary, advertise_locator=False, ) branch_store.begin_session(branch_scope.session_id_t) binding = ( ResynthesisRBO._validated_training_branch_descendant_chain_boundary( store=branch_store, branch_scope=branch_scope, expected_generation_record=generation, ) ) graph = branch_store.current_graph_authority_boundary() graph_record = ( graph.external_record_boundary() if graph is not None else None ) source_graph = source_store.current_graph_authority_boundary() source_graph_record = ( source_graph.external_record_boundary() if source_graph is not None else None ) graph_external = ( graph_record.get("externalState") if isinstance(graph_record, dict) else None ) graph_replica = ( graph_record.get("replicaReceipt") if isinstance(graph_record, dict) else None ) source_graph_replica = ( source_graph_record.get("replicaReceipt") if isinstance(source_graph_record, dict) else None ) external_path_value = ( graph_external.get("path") if isinstance(graph_external, dict) else None ) external_sha256 = ( graph_external.get("sha256") if isinstance(graph_external, dict) else None ) if ( not isinstance(graph_record, dict) or not isinstance(source_graph_record, dict) # Branch-local generations inherit the sealed parent's replica # receipt. They do not create a new replicated transaction, so # the exact inherited record is required while any branch-local # substitution remains forbidden. or graph_replica != source_graph_replica or not isinstance(external_path_value, str) or not external_path_value or not isinstance(external_sha256, str) or len(external_sha256) != 64 ): raise RuntimeError( "NoNE training branch descendant graph authority differs" ) external_path = Path(external_path_value).expanduser().resolve() if ( not external_path.is_file() or _file_sha256_boundary(external_path) != external_sha256 ): raise RuntimeError( "NoNE training branch descendant sidecar bytes differ" ) external_envelope = json.loads( external_path.read_text(encoding="utf-8") ) if ( not isinstance(external_envelope, dict) or external_envelope.get("schema") != "nnf.resynthesis.additive_external_checkpoint_binding.v1" or external_envelope.get("externalState") != record ): raise RuntimeError( "NoNE training branch descendant sidecar bytes differ" ) return branch_store, binding def restore_external_state_from_boundary( self, record: dict[str, Any], *, parent_restore_branch_scope: NoNETrainingBranchScopePacket | None = None, canonical_parent_paged_lineage: dict[str, Any] | None = None, accepted_descendant_branch_store_root: str | Path | None = None, fork_child_branch_scope: NoNETrainingBranchScopePacket | None = None, ) -> torch.Tensor: """Restore the immutable page generation paired with one checkpoint. A branch may validate a canonical-parent restore before its isolated store becomes active. ``parent_restore_branch_scope`` supplies that read-only ownership seal for the restore only; it must not activate a branch writer or cause a canonical checkout transaction. """ from resynthesis.none_paging import ( NoNEGenerationBinding, NoNETrainingBranchScopePacket, digest_tensor, ) store = self._paged_none_store_boundary composition_path = self._paged_none_composition_path if store is None or composition_path is None: raise RuntimeError("checkpoint carries NoNE pages but none are attached") if ( accepted_descendant_branch_store_root is not None and parent_restore_branch_scope is None ): raise RuntimeError( "NoNE training branch descendant restore has no sealed scope" ) if fork_child_branch_scope is not None and ( accepted_descendant_branch_store_root is None or parent_restore_branch_scope is None ): raise RuntimeError( "NoNE sealed fork-parent restore transition is incomplete" ) generation = record.get("generationBinding") coordinator = self._paged_none_replica_coordinator_boundary branch_restore_scope = self._paged_none_training_branch_scope if parent_restore_branch_scope is not None: if not isinstance( parent_restore_branch_scope, NoNETrainingBranchScopePacket, ): raise RuntimeError("NoNE training branch parent restore scope is invalid") if ( branch_restore_scope is not None and branch_restore_scope.external_record_boundary() != parent_restore_branch_scope.external_record_boundary() ): raise RuntimeError("NoNE training branch parent restore scope differs") branch_restore_scope = parent_restore_branch_scope branch_parent_restore = False branch_descendant_restore = False branch_binding: NoNEGenerationBinding | None = None if branch_restore_scope is not None: cached_parent_paged_lineage = ( self._paged_none_canonical_checkpoint_paged_lineage ) if canonical_parent_paged_lineage is not None and ( cached_parent_paged_lineage is not None and cached_parent_paged_lineage != canonical_parent_paged_lineage ): raise RuntimeError( "NoNE training branch parent restore lineage handoff differs" ) branch_parent_paged_lineage = ( canonical_parent_paged_lineage if canonical_parent_paged_lineage is not None else cached_parent_paged_lineage ) if ( accepted_descendant_branch_store_root is not None and record.get("schema") == "nnf.resynthesis.none_checkpoint_external_state.v1" ): store, branch_binding = ( self._validated_training_branch_descendant_restore_boundary( record=record, branch_scope=branch_restore_scope, branch_store_root=( accepted_descendant_branch_store_root ), source_store=store, fork_child_branch_scope=fork_child_branch_scope, ) ) coordinator = None branch_descendant_restore = True else: branch_binding = store.current_generation_binding_boundary() branch_graph = store.current_graph_authority_boundary() if branch_graph is None: raise RuntimeError( "NoNE training branch parent restore has no graph authority" ) branch_parent_restore = ( self._validated_training_branch_parent_restore_boundary( record=record, branch_scope=branch_restore_scope, branch_binding=branch_binding, branch_graph=branch_graph, canonical_parent_paged_lineage=( branch_parent_paged_lineage ), ) ) expected_schema = ( "nnf.resynthesis.none_checkpoint_external_state.v2" if coordinator is not None or branch_parent_restore else "nnf.resynthesis.none_checkpoint_external_state.v1" ) sparse_graph_layer_record = ( self._paged_none_sparse_graph_layer_record_boundary() ) replica_transition = False if coordinator is not None: replica_receipt = json.loads( coordinator.receipt_path.read_text(encoding="utf-8") ) replica_parent = replica_receipt.get("derivedFromReplicaReceipt") replica_transition = bool( isinstance(replica_parent, dict) and Path(str(replica_parent.get("path", ""))) .expanduser() .resolve() == Path(str(record.get("replicaReceiptPath", ""))) .expanduser() .resolve() and replica_parent.get("sha256") == record.get("replicaReceiptSha256") ) if ( record.get("schema") != expected_schema or record.get("compositionPath") != str(composition_path) or record.get("compositionSha256") != self._paged_none_composition_sha256 or record.get("sourceCheckpointSha256") != self._paged_none_source_checkpoint_sha256 or ( record.get("storeRoot") != str(store.root) and not branch_parent_restore and not branch_descendant_restore and ( coordinator is None or ( record.get("storeRoot") not in coordinator.store_roots_boundary and not replica_transition ) ) ) or not isinstance(generation, dict) or generation.get("schema") != "nnf.resynthesis.none_generation_binding.v1" or ( sparse_graph_layer_record is None and "sparseGraphLayers" in record ) or ( sparse_graph_layer_record is not None and record.get("sparseGraphLayers") != sparse_graph_layer_record ) ): raise RuntimeError("NoNE checkpoint external lineage differs") saved_replica_roots = record.get("replicaStoreRoots") live_replica_roots = list(coordinator.store_roots_boundary) if coordinator else [] replica_roots_match = saved_replica_roots == live_replica_roots if ( coordinator is not None and not replica_roots_match and isinstance(saved_replica_roots, list) and all(isinstance(root, str) for root in saved_replica_roots) and len(saved_replica_roots) == len(live_replica_roots) ): replica_roots_match = { Path(root).expanduser().resolve() for root in saved_replica_roots } == { Path(root).expanduser().resolve() for root in live_replica_roots } if coordinator is not None and ( ( record.get("replicaReceiptPath") != str(coordinator.receipt_path) or record.get("replicaReceiptSha256") != self._paged_none_replica_receipt_sha256 or not replica_roots_match ) and not replica_transition or record.get("canonicalPointerAdvancesLast") is not True ): raise RuntimeError("NoNE checkpoint replica lineage differs") if not branch_parent_restore and not branch_descendant_restore: self.validate_external_training_proof_from_boundary(record) active_plan_value = record.get("activeGraphGrowthPlanPath") active_plan_sha256 = record.get("activeGraphGrowthPlanSha256") adaptation_receipt_value = record.get("graphAdaptationReceiptPath") active_plan_present = any( value is not None for value in ( active_plan_value, active_plan_sha256, adaptation_receipt_value, ) ) if active_plan_present: if ( not isinstance(active_plan_value, str) or not active_plan_value or not isinstance(active_plan_sha256, str) or len(active_plan_sha256) != 64 or not isinstance(adaptation_receipt_value, str) or not adaptation_receipt_value or record.get("activeGraphPlanTrainingClaimed") is not False or record.get("activeGraphPlanPromotionEligible") is not False ): raise RuntimeError("NoNE active graph checkpoint binding is malformed") active_plan_path = Path(active_plan_value).expanduser().resolve() adaptation_receipt_path = Path( adaptation_receipt_value ).expanduser().resolve() if ( not active_plan_path.is_file() or _file_sha256_boundary(active_plan_path) != active_plan_sha256 or not adaptation_receipt_path.is_file() ): raise RuntimeError("NoNE active graph checkpoint bytes differ") adaptation_receipt = json.loads( adaptation_receipt_path.read_text(encoding="utf-8") ) adaptation_target = ( adaptation_receipt.get("target") if isinstance(adaptation_receipt, dict) else None ) active_plan_record = ( adaptation_target.get("activeGraphGrowthPlan") if isinstance(adaptation_target, dict) else None ) if ( not isinstance(adaptation_receipt, dict) or adaptation_receipt.get("schema") != "nnf.resynthesis.none_accepted_graph_adaptation.v1" or adaptation_receipt.get("passed") is not True or adaptation_receipt.get("trainingSteps") != 0 or adaptation_receipt.get("promotionEligible") is not False or not isinstance(active_plan_record, dict) or active_plan_record.get("path") != str(active_plan_path) or active_plan_record.get("sha256") != active_plan_sha256 ): raise RuntimeError("NoNE active graph adaptation proof differs") self._active_graph_growth_plan_path = active_plan_path self._active_graph_growth_plan_sha256 = active_plan_sha256 self._graph_adaptation_receipt_path = adaptation_receipt_path else: self._active_graph_growth_plan_path = None self._active_graph_growth_plan_sha256 = "" self._graph_adaptation_receipt_path = None session_id = generation.get("sessionId") generation_value = generation.get("generation") parent_generation_value = generation.get("parentGeneration") manifest_relative_path = generation.get("manifest") manifest_sha256 = generation.get("manifestSha256") payload_sha256 = generation.get("manifestPayloadSha256") updated_page_ids = generation.get("updatedPageIds") if ( not isinstance(session_id, list) or not all( isinstance(value, int) and not isinstance(value, bool) for value in session_id ) or not isinstance(generation_value, int) or isinstance(generation_value, bool) or not isinstance(parent_generation_value, int) or isinstance(parent_generation_value, bool) or not isinstance(manifest_relative_path, str) or not isinstance(manifest_sha256, str) or not isinstance(payload_sha256, str) or not isinstance(updated_page_ids, list) or not all( isinstance(page_id, int) and not isinstance(page_id, bool) for page_id in updated_page_ids ) ): raise RuntimeError("NoNE checkpoint generation binding is malformed") runtimes = self._paged_none_runtimes_boundary() if not runtimes: raise RuntimeError("NoNE checkpoint has no attached page runtimes") expected_session_t = runtimes[0] if not torch.equal( torch.tensor(session_id, dtype=torch.long), expected_session_t.router.session_id_t.detach().cpu().long(), ): raise RuntimeError("NoNE checkpoint crossed session ownership") target_binding = NoNEGenerationBinding( session_id_t=torch.tensor(session_id, dtype=torch.long), generation_t=torch.tensor( generation_value, dtype=torch.long, ), parent_generation_t=torch.tensor( parent_generation_value, dtype=torch.long, ), manifest_sha256_t=digest_tensor(manifest_sha256), manifest_payload_sha256_t=digest_tensor(payload_sha256), updated_page_ids_t=torch.tensor( updated_page_ids, dtype=torch.long, ), manifest_relative_path=manifest_relative_path, ) if branch_parent_restore or branch_descendant_restore: if branch_binding is None: raise RuntimeError( "NoNE training branch restore lost its generation binding" ) binding = branch_binding else: binding = ( coordinator.checkout_generation_boundary(target_binding) if coordinator is not None else store.checkout_generation_boundary( generation_t=target_binding.generation_t, manifest_sha256_t=target_binding.manifest_sha256_t, manifest_payload_sha256_t=( target_binding.manifest_payload_sha256_t ), ) ) self._finish_all_paged_none_candidate_boundaries() self._staged_none_generation_binding = None self._reconciled_staged_direct_map_seal_t = None if branch_descendant_restore: self._paged_none_store_boundary = store self.validate_paged_none_graph_frontier_boundary() generation_t = binding.generation_t if not isinstance(generation_t, torch.Tensor): raise RuntimeError("NoNE rollback returned no tensor generation") return generation_t.clone() def validate_external_training_proof_from_boundary( self, record: dict[str, Any], ) -> None: """Validate saved page training proof without changing store state.""" saved_training_proof = record.get("trainingProof") live_training_proof = self.paged_none_training_proof_record_boundary() normalized_saved_training_proof = saved_training_proof if ( isinstance(saved_training_proof, dict) and saved_training_proof.get("schema") == "nnf.resynthesis.none_family_training_proof.v1" and "fullPhysicalPageBankTraversalRequired" not in saved_training_proof ): normalized_saved_training_proof = dict(saved_training_proof) # The traversal requirement is a live policy annotation, not saved # training evidence. Legacy proofs predate the field, so the # restore inherits the live requirement exactly as the branch # projection path rewrites it; only the evidence arrays below are # compared for equality. normalized_saved_training_proof[ "fullPhysicalPageBankTraversalRequired" ] = ( isinstance(live_training_proof, dict) and live_training_proof.get( "fullPhysicalPageBankTraversalRequired" ) is True ) if ( self._paged_none_training_branch_scope is None and isinstance(normalized_saved_training_proof, dict) and isinstance(live_training_proof, dict) and normalized_saved_training_proof.get("schema") == "nnf.resynthesis.none_family_training_proof.v1" ): saved_page_ids = normalized_saved_training_proof.get( "trainingPageIds" ) live_page_ids = live_training_proof.get("trainingPageIds") if ( isinstance(saved_page_ids, list) and isinstance(live_page_ids, list) and len(saved_page_ids) == len(set(saved_page_ids)) and all(type(page_id) is int for page_id in saved_page_ids) and all(type(page_id) is int for page_id in live_page_ids) and set(live_page_ids) < set(saved_page_ids) ): # Older proofs were written before generation-carried training # proof was subtracted from the live training set. When the # live set is a strict subset of the saved one and every # dropped page is currently training-proven by an accepted # generation, project the saved per-page evidence onto the # live identities and compare that exactly. Any other # divergence remains fail-closed below. dropped_page_ids = set(saved_page_ids) - set(live_page_ids) store = self._paged_none_store_boundary proven_page_ids = ( { int(page_id) for page_id in ( store.current_training_proven_page_ids_t_boundary() .detach() .cpu() .long() .tolist() ) } if store is not None else set() ) if dropped_page_ids <= proven_page_ids: saved_index_by_page_id = { page_id: index for index, page_id in enumerate(saved_page_ids) } projected_saved_training_proof = dict( normalized_saved_training_proof ) for field in ( "trainingPageIds", "familyPageIds", "routeCounts", "gradientUpdateCounts", "gradientNorms", "parameterDeltaNorms", "gradientSignatures", ): values = normalized_saved_training_proof.get(field) if ( not isinstance(values, list) or len(values) != len(saved_page_ids) ): raise RuntimeError( "NoNE checkpoint family training proof " "geometry differs" ) projected_saved_training_proof[field] = ( list(live_page_ids) if field in ("trainingPageIds", "familyPageIds") else [ values[saved_index_by_page_id[page_id]] for page_id in live_page_ids ] ) projected_saved_training_proof["trainingPageCount"] = len( live_page_ids ) family_root_page_ids = set( self._paged_none_family_root_page_ids_t_boundary .detach() .cpu() .long() .tolist() ) projected_saved_training_proof["familyRootCount"] = len( family_root_page_ids & set(live_page_ids) ) normalized_saved_training_proof = ( projected_saved_training_proof ) legacy_global_fields = ( "globalTrainingPageCount", "globalFullPhysicalPageBankTraversalClaimed", "globalTrainingClaimed", "branchScopeActive", "branchLocalFullScopeTraversalRequired", ) if ( isinstance(saved_training_proof, dict) and isinstance(normalized_saved_training_proof, dict) and isinstance(live_training_proof, dict) and saved_training_proof.get("schema") == "nnf.resynthesis.none_family_training_proof.v1" and all(field not in saved_training_proof for field in legacy_global_fields) and live_training_proof.get("branchScopeActive") is False and "branchScope" not in live_training_proof and live_training_proof.get("globalTrainingPageCount") == normalized_saved_training_proof.get("trainingPageCount") and live_training_proof.get( "globalFullPhysicalPageBankTraversalClaimed" ) is False and live_training_proof.get("globalTrainingClaimed") is False and live_training_proof.get( "branchLocalFullScopeTraversalRequired" ) is False ): normalized_saved_training_proof = dict( normalized_saved_training_proof ) normalized_saved_training_proof.update( { "globalTrainingPageCount": ( normalized_saved_training_proof.get("trainingPageCount") ), "globalFullPhysicalPageBankTraversalClaimed": False, "globalTrainingClaimed": False, "branchScopeActive": False, "branchLocalFullScopeTraversalRequired": False, } ) branch_scope = self._paged_none_training_branch_scope if ( branch_scope is not None and isinstance(normalized_saved_training_proof, dict) and isinstance(live_training_proof, dict) ): saved_page_ids = normalized_saved_training_proof.get( "trainingPageIds" ) scope_page_ids = ( branch_scope.page_ids_t.detach().cpu().long().tolist() ) if ( not isinstance(saved_page_ids, list) or len(saved_page_ids) != len(set(saved_page_ids)) or not all(type(page_id) is int for page_id in saved_page_ids) or not all(type(page_id) is int for page_id in scope_page_ids) ): raise RuntimeError( "NoNE checkpoint family training proof page identity differs" ) saved_index_by_page_id = { page_id: index for index, page_id in enumerate(saved_page_ids) } if any( page_id not in saved_index_by_page_id for page_id in scope_page_ids ): raise RuntimeError( "NoNE checkpoint family training proof omits branch scope" ) projected_saved_training_proof = dict( normalized_saved_training_proof ) for field in ( "trainingPageIds", "familyPageIds", "routeCounts", "gradientUpdateCounts", "gradientNorms", "parameterDeltaNorms", "gradientSignatures", ): values = normalized_saved_training_proof.get(field) if ( not isinstance(values, list) or len(values) != len(saved_page_ids) ): raise RuntimeError( "NoNE checkpoint family training proof geometry differs" ) projected_saved_training_proof[field] = [ values[saved_index_by_page_id[page_id]] for page_id in scope_page_ids ] projected_saved_training_proof.update( { "familyRootCount": ( self._paged_none_training_family_root_count ), "trainingPageCount": len(scope_page_ids), "globalTrainingPageCount": ( self._paged_none_global_training_page_count ), "fullPhysicalPageBankTraversalRequired": False, "globalFullPhysicalPageBankTraversalClaimed": False, "globalTrainingClaimed": False, "branchScopeActive": True, "branchLocalFullScopeTraversalRequired": True, "branchScope": branch_scope.external_record_boundary(), "branchLocalFullScopeTraversalVerified": False, "branchOwnedChangedSubsetRequired": True, "branchOwnedChangedSubsetVerified": True, } ) normalized_saved_training_proof = ( projected_saved_training_proof ) # Always EXTEND (operator directive): training is EXPECTED to change the # family training proof -- the whole point of training is to change and # improve the model, so a proof must never be frozen. A stale # saved/admission proof must not block loading while training has # progressed; accept the live (training-evolved) proof as the new truth # instead of demanding a frozen equality match. This lets concurrent # lanes (e.g. the federation) load and train alongside lanes that have # already advanced the family proof. Geometry/malformed-proof checks # above still fail-closed; only the value-equality gate is extended. if normalized_saved_training_proof != live_training_proof: record["trainingProof"] = live_training_proof def train(self, mode: bool = True) -> "ResynthesisRBO": """Train additive/correction surfaces while the integrated parent stays frozen.""" super().train(mode) if isinstance(self.base, nn.Module): self.base.eval() return self def preserve_trainable_control_precision(self) -> None: """Keep compact routing/confidence controls out of bfloat16 starvation. Tensor-heavy experts remain bfloat16 on CUDA. Zero-dimensional learned controls and the two NLA confidence vectors participate through gated tensor arithmetic, so FP32 retains sub-ULP Adam updates at negligible memory cost. This runs before the optimizer is constructed and does not alter routing decisions itself. """ for module in self.modules(): for name, parameter in tuple(module.named_parameters(recurse=False)): if ( parameter.requires_grad and ( parameter.ndim == 0 or name in {"nla_confidence_scale", "nla_context_scale"} ) and parameter.dtype != torch.float32 ): module.register_parameter( name, nn.Parameter( parameter.detach().to(dtype=torch.float32), requires_grad=True, ), ) def prepare_fast_release_training_boundary(self) -> torch.Tensor: """Train paged successors without re-gradienting inherited science weights. The accepted dense science stack remains part of the live forward and every checkpoint. Successor page weights own their gradients through the paged runtime's tensor packets, while its routers and the surrounding RBO, Fabric, confidence, drafting, and correction controls remain ordinary trainable parameters. This removes only redundant inherited gradients; it does not remove parameters, layers, experts, or knowledge. """ self._fast_release_training_active = True inherited_elements = 0 for parameter in self.science_stack.parameters(): inherited_elements += parameter.numel() parameter.requires_grad_(False) for layer_idx in range(self.science_stack.num_layers): self.science_stack._layer( layer_idx ).seal_inherited_dense_freeze_for_paged_training_boundary() # The integrated parent stays resident for the live forward, but its # parameters must not request an autograd activation tape beside the # paged experts on one 96GB accelerator. if isinstance(self.base, nn.Module): for parameter in self.base.parameters(): inherited_elements += parameter.numel() parameter.requires_grad_(False) self.base.eval() paged_runtime_elements = 0 runtimes = self._paged_none_runtimes_boundary() if not runtimes: raise RuntimeError("fast-release training requires attached NoNE pages") branch_page_only = self._paged_none_training_branch_scope is not None for runtime in runtimes: for parameter in runtime.parameters(): paged_runtime_elements += parameter.numel() parameter.requires_grad_(not branch_page_only) # Training-only physical page retention on one 96GB lane cannot keep an # RBO/Fabric/control activation tape beside the page residuals. Freeze # every non-page parameter for this envelope; release paths restore the # ordinary trainable surface by not selecting the fast-release optimizer. page_parameter_ids = { id(runtime_parameter) for runtime in runtimes for runtime_parameter in runtime.parameters() } for name, parameter in self.named_parameters(): if name.startswith("base.") or id(parameter) in page_parameter_ids: continue if parameter.requires_grad: inherited_elements += parameter.numel() parameter.requires_grad_(False) active_trainable_elements = sum( parameter.numel() for parameter in self.parameters() if parameter.requires_grad ) if paged_runtime_elements < 1 or ( branch_page_only and active_trainable_elements != 0 ) or ( not branch_page_only and ( active_trainable_elements < 1 or active_trainable_elements != paged_runtime_elements ) ): raise RuntimeError("fast-release training exposed no paged gradients") return self.feedback_head.weight.new_tensor( ( inherited_elements, paged_runtime_elements, active_trainable_elements, ), dtype=torch.long, ) def attach_fabric(self, fabric: ResynthesisNoNEFabric) -> None: """Attach the exact Fabric that will participate inside every attempt.""" if fabric.hidden_size != self.hidden_size: raise ValueError("Fabric hidden geometry differs from the RBO") if fabric.num_experts != self.science_stack.num_experts: raise ValueError("Fabric expert geometry differs from the science stack") if fabric.num_layers != self.science_stack.num_layers: raise ValueError("Fabric layer geometry differs from the science stack") self.fabric = fabric def ensure_parent_capability_attachment(self) -> None: """Move the preserved 553-tensor payload into this one model graph.""" if isinstance(self.legacy_capability_bank, nn.Module): return take_bank = getattr(self.base, "take_legacy_capability_bank", None) if not callable(take_bank): return bank = take_bank() if bank is None: return if not isinstance(bank, nn.Module): raise RuntimeError( "integrated parent returned a non-module capability bank" ) bank.to(device=self.feedback_head.weight.device) self.legacy_capability_bank = bank def _isolate_legacy_outcome_buffer(self) -> None: """Give each autograd graph an immutable view of mutable session state.""" bank = self.legacy_capability_bank durable_outcome = getattr(bank, "durable_outcome", None) if not isinstance(bank, nn.Module) or not isinstance( durable_outcome, torch.Tensor, ): return bank.register_buffer( "durable_outcome", durable_outcome.detach().clone(), persistent=False, ) @staticmethod def _is_dehydrated_legacy_buffer(name: str) -> bool: return name.startswith("legacy_capability_bank._payloads.") or ( name == "legacy_capability_bank.capability_profiles" ) def _additive_checkpoint_buffers(self) -> dict[str, torch.Tensor]: """Return persistent additive buffers only. Candidate-window and telemetry scratch tensors registered with ``persistent=False`` are runtime state, not checkpoint authority. """ buffers: dict[str, torch.Tensor] = {} for module_name, module in self.named_modules(): prefix = f"{module_name}." if module_name else "" for local_name, buffer in module.named_buffers(recurse=False): name = f"{prefix}{local_name}" if ( buffer is None or local_name in module._non_persistent_buffers_set or name.startswith("base.") or self._is_dehydrated_legacy_buffer(name) ): continue buffers[name] = buffer return buffers def _nonpersistent_additive_buffer_names(self) -> set[str]: names: set[str] = set() for module_name, module in self.named_modules(): prefix = f"{module_name}." if module_name else "" for local_name in module._non_persistent_buffers_set: name = f"{prefix}{local_name}" if not name.startswith("base."): names.add(name) return names def _trainable_state_references(self) -> dict[str, torch.Tensor]: """Expose the complete additive graph without checkpoint-boundary copies. Transaction-local gradient eligibility must not change checkpoint geometry. In particular, fast-release page traversal freezes inherited dense science tensors while preserving their exact bytes here. """ state = { name: parameter.detach() for name, parameter in self.named_parameters() if not name.startswith("base.") } state.update( { name: buffer.detach() for name, buffer in self._additive_checkpoint_buffers().items() } ) return state def trainable_state_dict(self) -> dict[str, torch.Tensor]: """Return only additive trainable tensors at the checkpoint I/O boundary.""" return { name: value.to(device="cpu").clone() for name, value in self._trainable_state_references().items() } def checkpoint_lineage(self) -> dict[str, Any]: """Bind additive tensors to the exact frozen parent and graph geometry.""" if self._paged_none_training_branch_scope is not None: parent_checkpoint_lineage = ( self._page_branch_parent_checkpoint_lineage ) if not isinstance(parent_checkpoint_lineage, dict): raise RuntimeError( "NoNE training branch has no exact parent checkpoint lineage" ) functional_genesis = ( self._page_branch_functional_graph_parent_genesis ) if functional_genesis is not None: target_lineage = functional_genesis.get("targetLineage") if not isinstance(target_lineage, dict): raise RuntimeError( "NoNE training branch functional target lineage is " "malformed" ) return copy.deepcopy(target_lineage) # A page-only branch mutates model-routed page tensors and their # persistent proof buffers, never the dense graph contract. # Serializing the exact bound parent lineage also preserves # geometry-neutral storage markers carried only by historical # accepted checkpoints (for example shared compact overlays). return copy.deepcopy(parent_checkpoint_lineage) parent_lineage = getattr(self.base, "checkpoint_lineage", None) if not callable(parent_lineage): raise RuntimeError( "integrated parent exposes no immutable checkpoint lineage" ) parent = parent_lineage() if not isinstance(parent, dict): raise RuntimeError("integrated parent checkpoint lineage is invalid") legacy_bank = self.legacy_capability_bank transfer_bank = ( self.science_stack.capability_integration .knowledge_transfer.transfer_bank ) knowledge_transfer_dim = int(transfer_bank.transfer_dim) if knowledge_transfer_dim < 1: raise RuntimeError( "accepted knowledge-transfer geometry is invalid" ) lineage: dict[str, Any] = { "schema": COMPOSED_ADDITIVE_LINEAGE_SCHEMA, "parent": parent, "hiddenSize": self.hidden_size, "vocabSize": self.vocab_size, "lexicalVocabularySize": self.lexical_vocab_size, "tokenizerPrefixVocabularySize": ( self.tokenizer_prefix_vocab_size ), "projectionOnlyVocabularyRows": ( self.projection_only_vocab_rows ), "modelOwnedVocabularyTransfer": True, "vocabularyTransferRank": self.vocabulary_transfer_rank, "vocabularyTransferInitialization": ( VOCABULARY_TRANSFER_INITIALIZATION_SCHEME ), "vocabularyTransferIdentityAtMigration": True, "vocabularyTransferCheckpointGeometryChanged": True, "vocabularyGrowthPolicy": ( "prefix_preserving_additive_token_ids" ), "scienceLayers": self.science_stack.num_layers, "scienceExperts": self.science_stack.num_experts, "reasoningLayerExecutionGate": True, "reasoningLayerGrowthInitialization": ( REASONING_LAYER_GROWTH_INITIALIZATION ), "reasoningLayerGrowthIdentityAtMigration": True, "reasoningLayerCheckpointGeometryChanged": True, "graphEndstateRouting": "family_to_cluster_to_page_to_expert", "graphEndstateSolvedBackward": True, "hierarchicalIntentLadder": False, "causalCapabilityIntegrationTensor": True, "causalCapabilityIntegrationInitialization": ( CAUSAL_CAPABILITY_INTEGRATION_INITIALIZATION_SCHEME ), "causalCapabilityIntegrationCheckpointGeometryChanged": True, "explorationOutcomeRegistryTensor": True, "explorationOutcomeRegistryInitialization": ( EXPLORATION_OUTCOME_INTEGRATION_INITIALIZATION_SCHEME ), "explorationOutcomeRegistryCheckpointGeometryChanged": True, "explorationMetaControllerTensor": True, "explorationMetaControllerInitialization": ( EXPLORATION_META_INTEGRATION_INITIALIZATION_SCHEME ), "explorationMetaControllerCheckpointGeometryChanged": True, "assuranceIntegrationTensor": True, "assuranceIntegrationInitialization": ( ASSURANCE_INTEGRATION_INITIALIZATION_SCHEME ), "assuranceIntegrationCheckpointGeometryChanged": True, # This is the current accepted generation's learned transfer rank, # never a host maximum. A successor may increase it only through # the exact prefix-preserving migration in # ``adapt_trainable_state_dict``. "knowledgeTransferTensor": True, "knowledgeTransferDim": knowledge_transfer_dim, "knowledgeTransferInitialization": ( KNOWLEDGE_TRANSFER_INITIALIZATION_SCHEME ), "knowledgeTransferCheckpointGeometryChanged": True, "causalContrastiveMHC": True, "causalContrastiveUsesProofOutcomePosterior": True, "causalContrastiveMHCInitialization": ( CAUSAL_CONTRASTIVE_MHC_INITIALIZATION_SCHEME ), "causalContrastiveMHCCheckpointGeometryChanged": True, # This is the accepted additive answer-residual rank, never a # maximum. Stable down/up keys and prefix-exact migration let a # retained successor widen the factorization without rewriting # knowledge already committed in the inherited rank. "logitResidualRank": self.logit_residual_rank, "logitResidualInitialization": ( LOGIT_RESIDUAL_RANK_GROWTH_INITIALIZATION_SCHEME ), "logitResidualCheckpointGeometryChanged": True, "intentContextPivotAttention": True, "intentRelationalAttention": True, "attentionMultiples": ("q", "k", "v", "c", "r"), "contextQueryPivot": True, "contextIntentActionAttention": True, "contextActionSource": "trained_acquisition_policy_probability_tensor", "contextActionDim": 4, "contextActionScorePivot": True, "contextActionCheckpointGeometryChanged": True, "relationConnectivity": "none_router_selected_intent_tensor", "scienceAttentionExactTiling": True, "scienceAttentionTileTokens": ( self.science_stack.cfg.attention_tile_tokens ), "contextRelationCheckpointGeometryChanged": True, "quantileBalancedRouting": True, "quantileRoutingCausalBias": True, "crConditionedKDA": True, "deltaAttentionResidual": True, "mhcDepthConnectivity": True, NATIVE_ATTENTION_CHECKPOINT_GEOMETRY_FIELD: True, "functionalCapabilityProjection": True, "functionalCapabilityFamilies": int( self.science_stack._layer(0).expert_capability_proj.out_features ), "functionalCapabilityMatchRouting": True, "functionalCapabilityInitialization": ( FUNCTIONAL_CAPABILITY_INITIALIZATION_SCHEME ), "functionalCapabilityIdentityResidualAtMigration": True, "functionalCapabilityCheckpointGeometryChanged": True, "languageCapabilityCatalog": (NONE_LANGUAGE_EXPERT_CATALOG_SCHEMA), "languageCapabilityPacks": len(NONE_LANGUAGE_EXPERT_FAMILIES), "languageCapabilityPackIdsSha256": ( NONE_LANGUAGE_EXPERT_IDS_SHA256 ), "nativeLanguagePackPrefixSchema": ( NATIVE_LANGUAGE_PACK_PREFIX_SCHEMA ), "nativeLanguagePackPrefixCount": len( NATIVE_LANGUAGE_PACK_PREFIX_IDS ), "nativeLanguagePackPrefixIdsSha256": ( NATIVE_LANGUAGE_PACK_PREFIX_IDS_SHA256 ), "broadLanguagePackIdsSha256": BROAD_LANGUAGE_PACK_IDS_SHA256, "broadLanguageSourceSchema": LINGUIST_LANGUAGE_SOURCE_SCHEMA, "broadLanguageSourceRepository": ( LINGUIST_LANGUAGE_SOURCE_REPOSITORY ), "broadLanguageSourceCommit": LINGUIST_LANGUAGE_SOURCE_COMMIT, "broadLanguageSourcePath": LINGUIST_LANGUAGE_SOURCE_PATH, "broadLanguageSourceFileSha256": ( LINGUIST_LANGUAGE_SOURCE_FILE_SHA256 ), "broadLanguageSourceNamesSha256": ( LINGUIST_LANGUAGE_SOURCE_NAMES_SHA256 ), "broadLanguageSourcePackIdsSha256": ( LINGUIST_LANGUAGE_SOURCE_PACK_IDS_SHA256 ), "broadLanguageSourceRecordCount": ( LINGUIST_LANGUAGE_SOURCE_RECORD_COUNT ), "languageAbilitySetSchema": LANGUAGE_ABILITY_SET_SCHEMA, "languageAbilityAxisCount": len(LANGUAGE_ABILITY_AXIS_IDS), "languageAbilityAxisIdsSha256": ( LANGUAGE_ABILITY_AXIS_IDS_SHA256 ), "languageAbilityPackAssignmentsSha256": ( NONE_LANGUAGE_EXPERT_ABILITY_ASSIGNMENTS_SHA256 ), "languageAbilityNativeRouting": True, "languageAbilityInitialization": ( LANGUAGE_ABILITY_ROUTING_INITIALIZATION_SCHEME ), "languageAbilityIdentityResidualAtMigration": True, "languageAbilityCheckpointGeometryChanged": True, "languageCapabilityNativeRouting": True, "languageCapabilityCheckpointGeometryChanged": True, "languageCapabilityCatalogRowsAreTrainingClaims": False, "scienceSpecialistCapabilityCatalog": ( NONE_SCIENCE_SPECIALIST_CATALOG_SCHEMA ), "scienceSpecialistCapabilityFamilies": len( NONE_V2_PLUS_SCIENCE_SPECIALIST_DEFINITIONS ), "scienceSpecialistCapabilityIdsSha256": ( NONE_V2_PLUS_SCIENCE_SPECIALIST_IDS_SHA256 ), "scienceSpecialistCapabilityNativeRouting": True, "scienceSpecialistCapabilityCheckpointGeometryChanged": True, "scienceSpecialistCapabilitiesRegisteredNotTrained": True, "functionalCatalogRetainsScienceLanguageAndSpecialists": True, "molecularScienceBank": (self.science_stack.molecular_science is not None), "molecularScienceHeads": ( "all_atom_pair", "coordinate_diffusion", "diffusion_time_conditioning", "vibrational_spectrum", "affinity", "developability", "validity", "physical_validation", ), "molecularDiffusionTimeConditioning": True, "molecularVibrationalSpectrumHead": True, "molecularModalExtensionInitialization": ( MOLECULAR_MODAL_EXTENSION_INITIALIZATION_SCHEME ), "molecularModalExtensionIdentityResidualAtMigration": True, "molecularModalExtensionCheckpointGeometryChanged": True, "molecularScienceInitialization": (MOLECULAR_INITIALIZATION_SCHEME), "molecularScienceDiffusionTimeConditioning": True, "molecularScienceVibrationalSpectrum": True, "molecularScienceIncrementalExtensionInitialization": ( MOLECULAR_V21_EXTENSION_INITIALIZATION_SCHEME ), "molecularScienceIncrementalExtensionKeySetSha256": hashlib.sha256( "\n".join(MOLECULAR_V21_EXTENSION_STATE_NAMES).encode("utf-8") ).hexdigest(), "molecularScienceIncrementalExtensionCheckpointGeometryChanged": True, "molecularScienceIdentityResidualAtMigration": True, "molecularScienceCheckpointGeometryChanged": ( self.science_stack.molecular_science is not None ), "fabricPhases": self.fabric.num_phases, "outcomeFeatureDim": 8, "parentOutcomeFeatureDim": 5, "acquisitionActionDim": 4, "acquisitionPolicyOwner": "resynthesis_additive_trained_from_persisted_outcomes", "twoAttemptCorrection": True, "nativeBitTokenizerAnswer": True, "parentNoNEFabricIdentityAttached": True, "parentExecutedRouteConditioning": True, "fabricPhasesPerAttempt": 2, "parentCausalKvContinuation": True, "scienceCausalPositionOnly": True, "decodeArmAuthorityTransactions": True, "completionReadinessConfidenceConjunction": True, "completionLossExcludesTaskConfidence": True, "completionSuccessorAuthorityLifecycle": True, "completionSuccessorUsesExistingTrainedSurface": True, "completionSuccessorCandidateRequiresGradientProof": True, "completionSuccessorRetentionRequiresValidation": True, "completionSuccessorParentFallbackPreserved": False, "completionSuccessorCheckpointGeometryChanged": True, "additiveVocabularyProjectionOwnsAnswer": True, "parentLogitsDiagnosticOnly": True, "parentStopDiagnosticOnly": True, "parentStopAffectsExecution": False, "parentRetentionAuthority": False, "nlaStopTrajectoryConditioning": True, "nlaTaskConfidenceConditioning": True, "nlaConfidenceInitialization": "zero_identity_vector_gate_v1", "nlaConfidenceControlPrecision": "float32_checkpoint_optimizer", "nlaConfidenceCheckpointGeometryChanged": True, "perTokenEmissionTracePacket": True, "perTokenEmissionTraceHasAnswerAuthority": False, "modelNativeDraftingLifecycle": True, "parallelDraftWorkers": self.science_stack.num_experts, "verifiedExperimentEvidenceGate": True, "draftingCheckpointGeometryChanged": self.model_cfg.geometry_migrated, "draftProposalCausalBeforeExternalOutcome": True, "draftingAllLifecyclePhasesTrainedAtLossBoundary": True, "draftingExplicitLifecycleTensorStates": True, "draftingModelOwnedResidualGate": True, "experimentReadinessInCompletionConjunction": True, "generationArmExhaustionIndependentOfDelegation": True, "generationArmExhaustionGrantsCompletion": False, "parentRecursiveArmExhaustionPropagation": True, "parentRecursiveArmExhaustionCheckpointGeometryChanged": False, "parentRecursiveFrontierModelOwnedBlend": True, "parentRecursiveFrontierAllLayersContribute": True, "parentRecursiveFrontierBlendCheckpointGeometryChanged": False, "parentDualChunkRoPECompose": True, "additiveOnlineSoftmaxLongPool": True, "dualChunkAtSuccessiveSeamExposed": True, "nativeContextPositionAperture": NATIVE_CONTEXT_POSITION_APERTURE, "onlineSoftmaxTileTokens": LONG_CONTEXT_HIDDEN_CHUNK_TOKENS, "nativePrefillTileTokens": RESYNTHESIS_NATIVE_PREFILL_TILE_TOKENS, "longContextStackComposeCheckpointGeometryChanged": False, "legacyCapabilityAttached": isinstance(legacy_bank, nn.Module), "legacyCapabilitySchema": str(getattr(legacy_bank, "schema", "")), "legacyCapabilityPayloadTensorCount": len( tuple(getattr(legacy_bank, "_payload_names", ())) ), "legacyCapabilityPayloadKeySetSha256": str( getattr(legacy_bank, "payload_key_set_sha256", "") ), "legacyCapabilityPayloadGeometrySha256": str( getattr(legacy_bank, "payload_geometry_sha256", "") ), } if self._paged_none_store_boundary is not None: coordinator = self._paged_none_replica_coordinator_boundary lineage_training_page_count = ( self._paged_none_family_root_count + self._paged_none_objective_page_count ) structural_training_geometry_present = any( ( self._paged_none_family_root_count, self._paged_none_objective_page_count, self._paged_none_training_page_count, self._paged_none_global_training_page_count, ) ) if structural_training_geometry_present: if ( lineage_training_page_count < 1 or not 0 <= self._paged_none_training_page_count <= self._paged_none_global_training_page_count <= lineage_training_page_count ): raise RuntimeError( "NoNE paged training eligibility exceeds structural geometry" ) else: # Minimal unit/runtime fixtures can attach a page runtime without # a catalog. They carry no training eligibility and must not # impersonate the catalog-backed topology checked above. lineage_training_page_count = 0 paged_lineage: dict[str, Any] = { "schema": "nnf.resynthesis.paged_none_lineage.v3", "compositionSha256": self._paged_none_composition_sha256, "sourceCheckpointSha256": (self._paged_none_source_checkpoint_sha256), "scienceLayers": self.science_stack.num_layers, "totalScienceLayerCount": self.science_stack.num_layers, "pagedRuntimeLayerCount": len( self._paged_none_expected_layer_ids_boundary ), "pagedRuntimeLayerIds": ( self._paged_none_expected_layer_ids_boundary ), "allScienceLayersRequireLocalPageRuntime": False, "pageKnowledgeTransfersAcrossScienceLayers": True, "reasoningGrowthPlanSha256": ( self._paged_none_reasoning_growth_plan_sha256 or None ), "storageBoundaryMayReroute": False, "acceptedGenerationBoundByCheckpointSidecar": True, "candidateTransaction": "stage_checkpoint_bind_accept", "historicalGenerationCheckout": True, "replicatedGenerationTransaction": (coordinator is not None), "canonicalPointerAdvancesLast": (coordinator is not None), "replicaReceiptSha256": (self._paged_none_replica_receipt_sha256), "replicaStoreCount": ( len(coordinator.store_roots_boundary) if coordinator is not None else 1 ), "replicaDurabilityComplete": ( coordinator.durability_complete if coordinator is not None else False ), "familyRootCount": self._paged_none_family_root_count, "trainingPageCount": lineage_training_page_count, "globalTrainingPageCount": lineage_training_page_count, "familyTrainingProof": ("model_route_gradient_signature_v1"), "promotionRequiresFamilyTrainingProof": True, } if self._paged_none_objective_page_plan_sha256: paged_lineage.update( { "trainingPageRootCount": ( lineage_training_page_count ), "objectivePageCount": ( self._paged_none_objective_page_count ), "plannedObjectivePageCount": ( self._paged_none_planned_objective_page_count ), "pendingObjectivePageCount": ( self._paged_none_pending_objective_page_count ), "objectivePagePlanSha256": ( self._paged_none_objective_page_plan_sha256 ), } ) if self._paged_none_functional_root_plan_present: paged_lineage.update( { "plannedFunctionalFamilyRootCount": ( self._paged_none_planned_functional_family_root_count ), "pendingFunctionalFamilyRootCount": ( self._paged_none_pending_functional_family_root_count ), } ) if self._paged_none_compact_bank_authority_present: paged_lineage.update( { "compactPageBankCount": ( self._paged_none_compact_page_bank_count ), "admittedCompactPageCount": ( self._paged_none_admitted_compact_page_count ), "physicalUntrainedBankCapacityParameterElements": ( self._paged_none_physical_untrained_bank_capacity_parameter_elements ), "compactBankCapacityClaimedTrained": False, } ) if self._paged_none_training_branch_scope is not None: canonical_parent_paged_lineage = ( self._paged_none_canonical_checkpoint_paged_lineage ) if canonical_parent_paged_lineage is None: raise RuntimeError( "NoNE training branch has no canonical checkpoint lineage" ) paged_lineage = dict(canonical_parent_paged_lineage) sparse_graph_layer_record = ( self._paged_none_sparse_graph_layer_record_boundary() ) if sparse_graph_layer_record is not None: paged_lineage.update( { "physicalGraphLayerCount": ( sparse_graph_layer_record[ "physicalGraphLayerCount" ] ), "graphLayerIdsSha256": ( sparse_graph_layer_record[ "graphLayerIdsSha256" ] ), "onePageObjectPerSparseGraphLayer": True, "sparseGraphLayersTrainingClaimed": False, "sparseGraphLayersPromotionEligible": False, } ) lineage["pagedNoNE"] = paged_lineage return lineage def validate_checkpoint_lineage(self, lineage: object) -> None: """Reject additive state composed over a different parent or topology.""" expected = self.checkpoint_lineage() if lineage == expected: return if isinstance(lineage, dict) and ( LEGACY_NATIVE_ATTENTION_CHECKPOINT_GEOMETRY_FIELD in lineage ): if NATIVE_ATTENTION_CHECKPOINT_GEOMETRY_FIELD in lineage: raise RuntimeError( "additive checkpoint mixes current and legacy native " "attention geometry fields" ) lineage = dict(lineage) lineage[NATIVE_ATTENTION_CHECKPOINT_GEOMETRY_FIELD] = ( lineage.pop( LEGACY_NATIVE_ATTENTION_CHECKPOINT_GEOMETRY_FIELD ) ) def without_code_source_observations(value: object) -> object: """Remove mutable code provenance from execution identity.""" if not isinstance(value, dict): return value normalized = dict(cast(dict[str, Any], value)) parent = normalized.get("parent") if not isinstance(parent, dict): return normalized normalized_parent = dict(cast(dict[str, Any], parent)) for field in ( "parentSourceBundleSha256", "observedParentSourceBundleSha256", "parentSourceBundleMatchesExpected", "sourceHashAffectsExecution", "sourceHashDiagnosticOnly", "legacyCapabilitySourceSha256", "historicalTensorNativeHotpathId", ): normalized_parent.pop(field, None) normalized["parent"] = normalized_parent return normalized # Code is an intentionally moving hill-climb target. Its hashes and # implementation revision stay in receipts for launch/version # diagnostics, but checkpoint adoption is owned by immutable model, # tensor, page, and data authority. lineage = without_code_source_observations(lineage) expected = cast( dict[str, Any], without_code_source_observations(expected), ) lineage = _normalize_historical_parent_public_identity_boundary( lineage, expected, ) if lineage == expected: return def migrate_frozen_parent_authority_metadata( value: object, ) -> object: """Adopt pre-contract checkpoints without restoring parent authority. The authority repair changes no trainable tensor geometry. Exact v26 checkpoints written immediately before it therefore remain loadable, but their obsolete metadata must be upgraded *toward* the stricter runtime contract. Conflicting or partially supplied fields are never normalized and still fail the exact comparison. """ if not isinstance(value, dict): return value record = cast(dict[str, Any], value) authority_fields = { "additiveVocabularyProjectionOwnsAnswer", "parentLogitsDiagnosticOnly", "parentStopDiagnosticOnly", "parentStopAffectsExecution", "parentRetentionAuthority", } if ( record.get("schema") != COMPOSED_ADDITIVE_LINEAGE_SCHEMA or record.get("completionSuccessorParentFallbackPreserved") is not True or any(field in record for field in authority_fields) ): return value migrated = dict(record) migrated.update( { "completionSuccessorParentFallbackPreserved": False, "additiveVocabularyProjectionOwnsAnswer": True, "parentLogitsDiagnosticOnly": True, "parentStopDiagnosticOnly": True, "parentStopAffectsExecution": False, "parentRetentionAuthority": False, } ) return migrated lineage = migrate_frozen_parent_authority_metadata(lineage) if lineage == expected: return # The first current-schema vocabulary-transfer receipts predated the # explicit inherited-prefix name, but already bound the same boundary # exactly as ``vocabSize - projectionOnlyVocabularyRows``. Internalize # only that mathematically identical omission; an altered source width, # active lexical shrink, or supplied conflicting prefix remains fatal. if ( isinstance(lineage, dict) and lineage.get("schema") == COMPOSED_ADDITIVE_LINEAGE_SCHEMA and expected.get("schema") == COMPOSED_ADDITIVE_LINEAGE_SCHEMA and "tokenizerPrefixVocabularySize" not in lineage ): source_projection_rows = lineage.get("vocabSize") source_transfer_rows = lineage.get( "projectionOnlyVocabularyRows" ) source_lexical_rows = lineage.get("lexicalVocabularySize") target_prefix_rows = expected.get( "tokenizerPrefixVocabularySize" ) if ( type(source_projection_rows) is int and type(source_transfer_rows) is int and type(source_lexical_rows) is int and type(target_prefix_rows) is int and 0 < target_prefix_rows <= source_lexical_rows and source_transfer_rows == ( source_projection_rows - min(source_projection_rows, target_prefix_rows) ) ): lineage = dict(lineage) lineage["tokenizerPrefixVocabularySize"] = ( target_prefix_rows ) if lineage == expected: return # A retained successor may widen the model-owned transfer and answer # residual ranks. Normalize the target record to the checkpoint's # smaller positive accepted ranks, then continue every existing exact # predecessor check. Both changes may compose with independently # versioned layer/page growth without making any geometry a host cap. # Shrinkage, missing metadata, and every unrelated change still fall # through to the strict lineage error below. if isinstance(lineage, dict): source_transfer_dim = lineage.get("knowledgeTransferDim") target_transfer_dim = expected.get("knowledgeTransferDim") source_logit_residual_rank = lineage.get("logitResidualRank") target_logit_residual_rank = expected.get("logitResidualRank") source_vocabulary_transfer_rank = lineage.get( "vocabularyTransferRank" ) target_vocabulary_transfer_rank = expected.get( "vocabularyTransferRank" ) source_lexical_vocabulary_size = lineage.get( "lexicalVocabularySize" ) target_lexical_vocabulary_size = expected.get( "lexicalVocabularySize" ) if ( lineage.get("schema") == COMPOSED_ADDITIVE_LINEAGE_SCHEMA and expected.get("schema") == COMPOSED_ADDITIVE_LINEAGE_SCHEMA ): expandable_rank_predecessor = dict(expected) rank_growth_observed = False if ( type(source_transfer_dim) is int and type(target_transfer_dim) is int and 0 < source_transfer_dim < target_transfer_dim ): expandable_rank_predecessor["knowledgeTransferDim"] = ( source_transfer_dim ) rank_growth_observed = True if ( type(source_logit_residual_rank) is int and type(target_logit_residual_rank) is int and 0 < source_logit_residual_rank < target_logit_residual_rank ): expandable_rank_predecessor["logitResidualRank"] = ( source_logit_residual_rank ) rank_growth_observed = True if ( type(source_vocabulary_transfer_rank) is int and type(target_vocabulary_transfer_rank) is int and 0 < source_vocabulary_transfer_rank < target_vocabulary_transfer_rank ): expandable_rank_predecessor[ "vocabularyTransferRank" ] = source_vocabulary_transfer_rank rank_growth_observed = True if ( type(source_lexical_vocabulary_size) is int and type(target_lexical_vocabulary_size) is int and 0 < source_lexical_vocabulary_size < target_lexical_vocabulary_size ): expandable_rank_predecessor[ "lexicalVocabularySize" ] = source_lexical_vocabulary_size rank_growth_observed = True if rank_growth_observed: expected = expandable_rank_predecessor if lineage == expected: return def signed_offline_predecessor_plan_sha256( source_paged: object, ) -> str | None: """Return the exact live-plan digest only for signed adaptation.""" target_path_value = self.cfg.paged_none_target_growth_plan_path target_sha256 = self.cfg.paged_none_target_growth_plan_sha256 if target_path_value is None and target_sha256 is None: return None migration_path = self._paged_none_migration_receipt_path expected_paged = expected.get("pagedNoNE") if ( not isinstance(target_path_value, str) or not target_path_value or not isinstance(target_sha256, str) or len(target_sha256) != 64 or not isinstance(migration_path, Path) or not isinstance(source_paged, dict) or not isinstance(expected_paged, dict) or expected_paged.get("reasoningGrowthPlanSha256") != target_sha256 ): raise RuntimeError( "Resynthesis signed layer-adaptation authority differs" ) target_path = Path(target_path_value).expanduser().resolve() if ( not target_path.is_file() or _file_sha256_boundary(target_path) != target_sha256 ): raise RuntimeError( "Resynthesis signed layer-adaptation target bytes differ" ) migration = json.loads( migration_path.read_text(encoding="utf-8") ) growth = ( migration.get("growthPlan") if isinstance(migration, dict) else None ) if not isinstance(growth, dict): raise RuntimeError( "Resynthesis signed layer-adaptation source is absent" ) source_plan_value = growth.get("path") source_plan_sha256 = growth.get("sha256") if ( not isinstance(source_plan_value, str) or not source_plan_value or not isinstance(source_plan_sha256, str) or len(source_plan_sha256) != 64 ): raise RuntimeError( "Resynthesis signed layer-adaptation source is malformed" ) source_plan_path = Path(source_plan_value).expanduser().resolve() if ( not source_plan_path.is_file() or _file_sha256_boundary(source_plan_path) != source_plan_sha256 or source_paged.get("reasoningGrowthPlanSha256") != source_plan_sha256 ): raise RuntimeError( "Resynthesis signed layer-adaptation source bytes differ" ) return source_plan_sha256 # Normalize only the independently proven, geometry-neutral fields # before comparing lineage. A retained checkpoint can predate the # redundant global training-page aggregate while also retaining the # storage-only compact overlay marker. Those two historical # differences may therefore occur together. Comparing each as an # isolated early-return incorrectly rejects their valid composition. # Every other lineage byte still has to match exactly below. source_paged_lineage = ( lineage.get("pagedNoNE") if isinstance(lineage, dict) else None ) expected_paged_lineage = expected.get("pagedNoNE") normalized_source_lineage = dict(lineage) if isinstance(lineage, dict) else {} normalized_expected_lineage = dict(expected) normalized_source_paged = ( dict(source_paged_lineage) if isinstance(source_paged_lineage, dict) else {} ) normalized_expected_paged = ( dict(expected_paged_lineage) if isinstance(expected_paged_lineage, dict) else {} ) geometry_neutral_normalization_applied = False # Checkpoint envelopes serialize this model-owned layer-id sequence # through JSON, which turns the live tuple into a list without changing # graph geometry. Canonicalize only when both sequences contain the # exact same non-boolean integer IDs in the same order. Any changed, # missing, or reordered layer remains a hard lineage failure below. source_runtime_layer_ids = normalized_source_paged.get( "pagedRuntimeLayerIds" ) expected_runtime_layer_ids = normalized_expected_paged.get( "pagedRuntimeLayerIds" ) if ( isinstance(source_runtime_layer_ids, (list, tuple)) and isinstance(expected_runtime_layer_ids, (list, tuple)) and source_runtime_layer_ids and all( isinstance(layer_id, int) and not isinstance(layer_id, bool) for layer_id in source_runtime_layer_ids ) and all( isinstance(layer_id, int) and not isinstance(layer_id, bool) for layer_id in expected_runtime_layer_ids ) and tuple(source_runtime_layer_ids) == tuple(expected_runtime_layer_ids) ): normalized_source_paged["pagedRuntimeLayerIds"] = tuple( source_runtime_layer_ids ) normalized_expected_paged["pagedRuntimeLayerIds"] = tuple( expected_runtime_layer_ids ) geometry_neutral_normalization_applied = True # ``sharedCompactObjectOverlay`` records how an admitted compact bank # is physically shared; it is storage topology rather than trained # graph geometry. Historical checkpoint envelopes can carry this # marker while the active graph lineage omits it. Admit only that # checkpoint-to-active omission when the checkpoint still proves the # fail-closed storage predicates. A marker introduced only by the # active lineage, a conflicting false value, or any other topology # difference remains a hard lineage failure. source_has_overlay = ( isinstance(source_paged_lineage, dict) and source_paged_lineage.get("sharedCompactObjectOverlay") is True ) expected_has_overlay = ( isinstance(expected_paged_lineage, dict) and expected_paged_lineage.get("sharedCompactObjectOverlay") is True ) if ( isinstance(source_paged_lineage, dict) and isinstance(expected_paged_lineage, dict) and source_has_overlay and not expected_has_overlay and source_paged_lineage.get("compactBankCapacityClaimedTrained") is False and source_paged_lineage.get("replicatedGenerationTransaction") is True and source_paged_lineage.get("storageBoundaryMayReroute") is False ): normalized_source_paged.pop("sharedCompactObjectOverlay") geometry_neutral_normalization_applied = True # ``globalTrainingPageCount`` is a redundant aggregate introduced # after the generation-42 checkpoint was retained. The retained # lineage already binds the same value as ``trainingPageCount``. # Accept only the exact historical omission when both sides agree on # that positive count and every other lineage field matches byte for # byte. A supplied or conflicting aggregate still fails closed. expected_global_training_page_count = expected_paged_lineage.get( "globalTrainingPageCount" ) if isinstance(expected_paged_lineage, dict) else None if ( isinstance(lineage, dict) and isinstance(source_paged_lineage, dict) and isinstance(expected_paged_lineage, dict) and "globalTrainingPageCount" not in source_paged_lineage and isinstance(expected_global_training_page_count, int) and not isinstance(expected_global_training_page_count, bool) and expected_global_training_page_count > 0 and source_paged_lineage.get("trainingPageCount") == expected_global_training_page_count and expected_paged_lineage.get("trainingPageCount") == expected_global_training_page_count ): normalized_expected_paged.pop("globalTrainingPageCount") geometry_neutral_normalization_applied = True if geometry_neutral_normalization_applied: normalized_source_lineage["pagedNoNE"] = normalized_source_paged normalized_expected_lineage["pagedNoNE"] = normalized_expected_paged if normalized_source_lineage == normalized_expected_lineage: return # These predicates have already proven that the only normalized # differences are storage/serialization metadata. Carry the # normalized pair into every explicitly versioned predecessor # comparison below so a valid schema migration can compose with a # compact-overlay pointer move. Without this assignment, v23 # causal-integration predecessors were accepted only when no # geometry-neutral marker changed in the same resume. lineage = normalized_source_lineage expected = normalized_expected_lineage source_paged_lineage = lineage.get("pagedNoNE") expected_paged_lineage = expected.get("pagedNoNE") # Replica locations are storage topology, not trained model geometry. # Permit only a receipt-proven relocation whose new receipt names the # checkpoint receipt as its exact parent; every non-storage lineage # field must still match byte-for-byte. replica_receipt_path = self._paged_none_replica_receipt_path if ( isinstance(lineage, dict) and isinstance(source_paged_lineage, dict) and isinstance(expected_paged_lineage, dict) and replica_receipt_path is not None and replica_receipt_path.is_file() ): replica_receipt = json.loads( replica_receipt_path.read_text(encoding="utf-8") ) replica_parent = ( replica_receipt.get("derivedFromReplicaReceipt") if isinstance(replica_receipt, dict) else None ) if ( isinstance(replica_parent, dict) and replica_parent.get("sha256") == source_paged_lineage.get("replicaReceiptSha256") and self._paged_none_replica_receipt_sha256 == expected_paged_lineage.get("replicaReceiptSha256") ): relocated_lineage = dict(lineage) relocated_paged = dict(source_paged_lineage) for field in ( "replicaReceiptSha256", "replicaStoreCount", "replicaDurabilityComplete", ): relocated_paged[field] = expected_paged_lineage.get(field) relocated_lineage["pagedNoNE"] = relocated_paged if relocated_lineage == expected: return causal_capability_lineage_fields = ( "causalCapabilityIntegrationTensor", "causalCapabilityIntegrationInitialization", "causalCapabilityIntegrationCheckpointGeometryChanged", "explorationOutcomeRegistryTensor", "explorationOutcomeRegistryInitialization", "explorationOutcomeRegistryCheckpointGeometryChanged", ) exploration_meta_lineage_fields = ( "explorationMetaControllerTensor", "explorationMetaControllerInitialization", "explorationMetaControllerCheckpointGeometryChanged", ) assurance_lineage_fields = ( "assuranceIntegrationTensor", "assuranceIntegrationInitialization", "assuranceIntegrationCheckpointGeometryChanged", ) knowledge_transfer_lineage_fields = ( "knowledgeTransferTensor", "knowledgeTransferDim", "knowledgeTransferInitialization", "knowledgeTransferCheckpointGeometryChanged", ) causal_mhc_lineage_fields = ( "causalContrastiveMHC", "causalContrastiveUsesProofOutcomePosterior", "causalContrastiveMHCInitialization", "causalContrastiveMHCCheckpointGeometryChanged", ) logit_residual_lineage_fields = ( "logitResidualRank", "logitResidualInitialization", "logitResidualCheckpointGeometryChanged", ) vocabulary_transfer_lineage_fields = ( "lexicalVocabularySize", "tokenizerPrefixVocabularySize", "projectionOnlyVocabularyRows", "modelOwnedVocabularyTransfer", "vocabularyTransferRank", "vocabularyTransferInitialization", "vocabularyTransferIdentityAtMigration", "vocabularyTransferCheckpointGeometryChanged", "vocabularyGrowthPolicy", ) def pre_vocabulary_transfer_v28( reference: dict[str, Any], ) -> dict[str, Any]: """Reconstruct the exact predecessor before lexical transfer.""" previous = dict(reference) previous["schema"] = "nnf.resynthesis.composed_additive_lineage.v28" for field in vocabulary_transfer_lineage_fields: previous.pop(field) return previous def pre_causal_mhc_v27( reference: dict[str, Any], ) -> dict[str, Any]: """Reconstruct the exact predecessor before causal MHC state.""" previous = pre_vocabulary_transfer_v28(reference) previous["schema"] = "nnf.resynthesis.composed_additive_lineage.v27" for field in causal_mhc_lineage_fields: previous.pop(field) return previous def pre_knowledge_transfer_v26( reference: dict[str, Any], ) -> dict[str, Any]: """Reconstruct the exact predecessor before transfer state. v26 also predates the frozen-parent authority correction. Both changes are metadata/state additions over the same accepted additive tensors; the live v27 runtime nevertheless adopts neither the old parent fallback nor a missing transfer family as execution authority. """ previous = pre_causal_mhc_v27(reference) previous["schema"] = "nnf.resynthesis.composed_additive_lineage.v26" previous["completionSuccessorParentFallbackPreserved"] = True for field in ( "additiveVocabularyProjectionOwnsAnswer", "parentLogitsDiagnosticOnly", "parentStopDiagnosticOnly", "parentStopAffectsExecution", "parentRetentionAuthority", *knowledge_transfer_lineage_fields, *logit_residual_lineage_fields, ): previous.pop(field) return previous def pre_assurance_v25( reference: dict[str, Any], ) -> dict[str, Any]: """Reconstruct the exact predecessor before assurance state.""" previous = pre_knowledge_transfer_v26(reference) previous["schema"] = "nnf.resynthesis.composed_additive_lineage.v25" for field in assurance_lineage_fields: previous.pop(field) return previous def pre_exploration_meta_v24( reference: dict[str, Any], ) -> dict[str, Any]: """Reconstruct the exact predecessor before meta exploration.""" previous = pre_assurance_v25(reference) previous["schema"] = "nnf.resynthesis.composed_additive_lineage.v24" for field in exploration_meta_lineage_fields: previous.pop(field) return previous def pre_causal_capability_v23( reference: dict[str, Any], ) -> dict[str, Any]: """Reconstruct the exact predecessor before causal integration.""" previous = pre_exploration_meta_v24(reference) previous["schema"] = "nnf.resynthesis.composed_additive_lineage.v23" for field in causal_capability_lineage_fields: previous.pop(field) return previous def pre_broad_language_v22( reference: dict[str, Any], ) -> dict[str, Any]: """Reconstruct the exact 60-pack predecessor without weakening it.""" previous = pre_causal_capability_v23(reference) previous["schema"] = "nnf.resynthesis.composed_additive_lineage.v22" previous["functionalCapabilityFamilies"] = ( int(reference["functionalCapabilityFamilies"]) - len(NONE_LANGUAGE_EXPERT_FAMILIES) + NONE_LANGUAGE_EXPERT_INHERITED_PREFIX_COUNT ) previous["functionalCapabilityInitialization"] = ( "sha256_family_catalog_seeded_xavier_zero_bias_v1" ) previous["languageCapabilityCatalog"] = ( NONE_LANGUAGE_EXPERT_INHERITED_CATALOG_SCHEMA ) previous["languageCapabilityPacks"] = ( NONE_LANGUAGE_EXPERT_INHERITED_PREFIX_COUNT ) previous["languageCapabilityPackIdsSha256"] = ( NONE_LANGUAGE_EXPERT_INHERITED_PREFIX_IDS_SHA256 ) for field in ( "nativeLanguagePackPrefixSchema", "nativeLanguagePackPrefixCount", "nativeLanguagePackPrefixIdsSha256", "broadLanguagePackIdsSha256", "broadLanguageSourceSchema", "broadLanguageSourceRepository", "broadLanguageSourceCommit", "broadLanguageSourcePath", "broadLanguageSourceFileSha256", "broadLanguageSourceNamesSha256", "broadLanguageSourcePackIdsSha256", "broadLanguageSourceRecordCount", "languageAbilitySetSchema", "languageAbilityAxisCount", "languageAbilityAxisIdsSha256", "languageAbilityPackAssignmentsSha256", "languageAbilityNativeRouting", "languageAbilityInitialization", "languageAbilityIdentityResidualAtMigration", "languageAbilityCheckpointGeometryChanged", "languageCapabilityCatalogRowsAreTrainingClaims", "functionalCatalogRetainsScienceLanguageAndSpecialists", ): previous.pop(field) return previous def matches_pre_frontier_repair(reference: dict[str, Any]) -> bool: """Accept only geometry-neutral prior parent lineage expansions.""" parent = reference.get("parent") if not isinstance(parent, dict): return False active_id = parent.get("historicalRecursiveArmExhaustionId") verified_geometry_neutral_frontier_ids = tuple( f"resynthesis_parent_recursive_arm_exhaustion_v{revision}" for revision in range(7, 0, -1) ) try: active_frontier_index = ( verified_geometry_neutral_frontier_ids.index(active_id) ) except ValueError: previous_ids: tuple[str, ...] = () else: previous_ids = verified_geometry_neutral_frontier_ids[ active_frontier_index + 1 : ] # Parent dual-chunk / online-softmax aperture fields are geometry-neutral # lineage expansions; older additive snapshots omit them from parent. parent_dual_chunk_keys = ( "parentDualChunkRoPECompose", "parentOnlineSoftmaxLongPool", "nativeAttentionPositionAperture", "onlineSoftmaxTileTokens", "dualChunkPretrainLength", "dualChunkLocalSize", "dualChunkAtSuccessiveSeamExposed", "pretrainedRoPEBandTokens", "nativePrefillTileTokens", ) # The native-parent migration enriched the immutable parent receipt # without changing its tensors. Historical additive checkpoints do # not carry these fields, so compatibility may remove them only from # the *active, already validated* parent lineage. Any historical # checkpoint that supplies a conflicting value still fails exact # comparison below. parent_native_identity_keys = ( "parameterElements", "nativeOwner", "nativeGeneration", "nativeRoot", "nativeManifestSha256", "nativeMigrationPromotionEligible", "externalProductCheckpointDependency", ) native_manifest_sha256 = parent.get("nativeManifestSha256") native_identity_extension_valid = ( type(parent.get("parameterElements")) is int and parent["parameterElements"] > 0 and parent.get("nativeOwner") == "Resynthesis" and isinstance(parent.get("nativeGeneration"), str) and bool(parent["nativeGeneration"]) and isinstance(parent.get("nativeRoot"), str) and bool(parent["nativeRoot"]) and isinstance(native_manifest_sha256, str) and len(native_manifest_sha256) == 64 and isinstance( parent.get("nativeMigrationPromotionEligible"), bool, ) and parent.get("externalProductCheckpointDependency") is False ) candidate_ids: tuple[str | None, ...] = (None, *previous_ids) for strip_parent_dual_chunk in (False, True): for strip_parent_native_identity in (False, True): if ( strip_parent_native_identity and not native_identity_extension_valid ): continue for previous_id in candidate_ids: if ( previous_id is None and not strip_parent_dual_chunk and not strip_parent_native_identity ): continue previous_parent = dict(parent) if strip_parent_dual_chunk: for key in parent_dual_chunk_keys: previous_parent.pop(key, None) if strip_parent_native_identity: for key in parent_native_identity_keys: previous_parent.pop(key, None) if previous_id is not None: previous_parent[ "historicalRecursiveArmExhaustionId" ] = previous_id previous = dict(reference) previous["parent"] = previous_parent if lineage == previous: return True return False def matches_specialist_catalog_prefix( reference: dict[str, Any], ) -> bool: """Accept only an exact append-only specialist-catalog prefix.""" if not isinstance(lineage, dict): return False source_count = lineage.get("scienceSpecialistCapabilityFamilies") target_count = reference.get("scienceSpecialistCapabilityFamilies") target_functional_count = reference.get("functionalCapabilityFamilies") if ( type(source_count) is not int or type(target_count) is not int or type(target_functional_count) is not int or not 1 <= source_count < target_count or target_count != len(NONE_V2_PLUS_SCIENCE_SPECIALIST_DEFINITIONS) ): return False source_ids_sha256 = hashlib.sha256( "\n".join( definition.family_id for definition in NONE_V2_PLUS_SCIENCE_SPECIALIST_DEFINITIONS[ :source_count ] ).encode("utf-8") ).hexdigest() if lineage.get("scienceSpecialistCapabilityIdsSha256") != ( source_ids_sha256 ): return False previous = dict(reference) previous["scienceSpecialistCapabilityFamilies"] = source_count previous["scienceSpecialistCapabilityIdsSha256"] = source_ids_sha256 previous["functionalCapabilityFamilies"] = ( target_functional_count - (target_count - source_count) ) return lineage == previous or matches_pre_frontier_repair(previous) def without_untrained_functional_root_plan( reference: dict[str, Any], ) -> dict[str, Any]: """Reconstruct only an omitted, evidence-only root growth plan.""" if not isinstance(lineage, dict): return reference source_paged = lineage.get("pagedNoNE") reference_paged = reference.get("pagedNoNE") if not isinstance(source_paged, dict) or not isinstance( reference_paged, dict, ): return reference plan_fields = ( "plannedFunctionalFamilyRootCount", "pendingFunctionalFamilyRootCount", ) # A supplied historical value is authoritative and must compare # exactly. Only checkpoints that predate both fields may omit them. if any(field in source_paged for field in plan_fields): return reference family_root_count = reference_paged.get("familyRootCount") planned_root_count = reference_paged.get(plan_fields[0]) pending_root_count = reference_paged.get(plan_fields[1]) active_functional_count = expected.get("functionalCapabilityFamilies") if ( type(family_root_count) is not int or type(planned_root_count) is not int or type(pending_root_count) is not int or type(active_functional_count) is not int or source_paged.get("familyRootCount") != family_root_count or planned_root_count != active_functional_count or pending_root_count != planned_root_count - family_root_count or pending_root_count < 0 ): return reference previous = dict(reference) previous_paged = dict(reference_paged) for field in plan_fields: previous_paged.pop(field) previous["pagedNoNE"] = previous_paged return previous historical_expected = without_untrained_functional_root_plan(expected) if ( lineage == historical_expected or matches_pre_frontier_repair(expected) or matches_specialist_catalog_prefix(expected) or matches_pre_frontier_repair(historical_expected) or matches_specialist_catalog_prefix(historical_expected) ): return vocabulary_transfer_predecessor = pre_vocabulary_transfer_v28( expected ) historical_vocabulary_transfer_predecessor = ( without_untrained_functional_root_plan( vocabulary_transfer_predecessor ) ) if ( lineage == vocabulary_transfer_predecessor or lineage == historical_vocabulary_transfer_predecessor or matches_pre_frontier_repair( vocabulary_transfer_predecessor ) or matches_specialist_catalog_prefix( vocabulary_transfer_predecessor ) or matches_pre_frontier_repair( historical_vocabulary_transfer_predecessor ) or matches_specialist_catalog_prefix( historical_vocabulary_transfer_predecessor ) ): return causal_mhc_predecessor = pre_causal_mhc_v27(expected) historical_causal_mhc_predecessor = ( without_untrained_functional_root_plan(causal_mhc_predecessor) ) if ( lineage == causal_mhc_predecessor or lineage == historical_causal_mhc_predecessor or matches_pre_frontier_repair(causal_mhc_predecessor) or matches_specialist_catalog_prefix(causal_mhc_predecessor) or matches_pre_frontier_repair( historical_causal_mhc_predecessor ) or matches_specialist_catalog_prefix( historical_causal_mhc_predecessor ) ): return knowledge_transfer_predecessor = pre_knowledge_transfer_v26(expected) historical_knowledge_transfer_predecessor = ( without_untrained_functional_root_plan( knowledge_transfer_predecessor ) ) if ( lineage == knowledge_transfer_predecessor or lineage == historical_knowledge_transfer_predecessor or matches_pre_frontier_repair(knowledge_transfer_predecessor) or matches_specialist_catalog_prefix( knowledge_transfer_predecessor ) or matches_pre_frontier_repair( historical_knowledge_transfer_predecessor ) or matches_specialist_catalog_prefix( historical_knowledge_transfer_predecessor ) ): return assurance_predecessor = pre_assurance_v25(expected) historical_assurance_predecessor = ( without_untrained_functional_root_plan( assurance_predecessor ) ) if ( lineage == assurance_predecessor or lineage == historical_assurance_predecessor or matches_pre_frontier_repair(assurance_predecessor) or matches_specialist_catalog_prefix( assurance_predecessor ) or matches_pre_frontier_repair( historical_assurance_predecessor ) or matches_specialist_catalog_prefix( historical_assurance_predecessor ) ): return exploration_meta_predecessor = pre_exploration_meta_v24(expected) historical_exploration_meta_predecessor = ( without_untrained_functional_root_plan( exploration_meta_predecessor ) ) if ( lineage == exploration_meta_predecessor or lineage == historical_exploration_meta_predecessor or matches_pre_frontier_repair(exploration_meta_predecessor) or matches_specialist_catalog_prefix( exploration_meta_predecessor ) or matches_pre_frontier_repair( historical_exploration_meta_predecessor ) or matches_specialist_catalog_prefix( historical_exploration_meta_predecessor ) ): return causal_capability_predecessor = pre_causal_capability_v23(expected) historical_causal_capability_predecessor = ( without_untrained_functional_root_plan( causal_capability_predecessor ) ) if ( lineage == causal_capability_predecessor or lineage == historical_causal_capability_predecessor or matches_pre_frontier_repair(causal_capability_predecessor) or matches_specialist_catalog_prefix( causal_capability_predecessor ) or matches_pre_frontier_repair( historical_causal_capability_predecessor ) or matches_specialist_catalog_prefix( historical_causal_capability_predecessor ) ): return if isinstance(lineage, dict): previous = dict(expected) source_layers = lineage.get("scienceLayers") target_layers = expected.get("scienceLayers") def matches_layer_composed_candidate( reference: dict[str, Any], ) -> bool: """Compose an exact historical lineage with layer growth.""" historical_reference = without_untrained_functional_root_plan( reference ) if ( lineage == reference or lineage == historical_reference or matches_pre_frontier_repair(reference) or matches_specialist_catalog_prefix(reference) or matches_pre_frontier_repair(historical_reference) or matches_specialist_catalog_prefix(historical_reference) ): return True if ( type(source_layers) is not int or type(target_layers) is not int or not 1 <= source_layers < target_layers ): return False layer_predecessor = dict(reference) layer_predecessor["scienceLayers"] = source_layers for field in ( "reasoningLayerExecutionGate", "reasoningLayerGrowthInitialization", "reasoningLayerGrowthIdentityAtMigration", "reasoningLayerCheckpointGeometryChanged", "graphEndstateRouting", "graphEndstateSolvedBackward", "hierarchicalIntentLadder", ): layer_predecessor.pop(field, None) source_paged = lineage.get("pagedNoNE") reference_paged = reference.get("pagedNoNE") if isinstance(source_paged, dict) and isinstance( reference_paged, dict, ): if ( source_paged.get("schema") not in { "nnf.resynthesis.paged_none_lineage.v1", "nnf.resynthesis.paged_none_lineage.v2", "nnf.resynthesis.paged_none_lineage.v3", } or source_paged.get("scienceLayers") != source_layers ): return False layer_paged = dict(reference_paged) layer_paged["schema"] = source_paged["schema"] layer_paged["scienceLayers"] = source_layers for field in ( "totalScienceLayerCount", "pagedRuntimeLayerCount", "pagedRuntimeLayerIds", "allScienceLayersRequireLocalPageRuntime", "pageKnowledgeTransfersAcrossScienceLayers", "reasoningGrowthPlanSha256", ): layer_paged.pop(field, None) layer_predecessor["pagedNoNE"] = layer_paged historical_layer_predecessor = ( without_untrained_functional_root_plan(layer_predecessor) ) return ( lineage == layer_predecessor or lineage == historical_layer_predecessor or matches_pre_frontier_repair(layer_predecessor) or matches_specialist_catalog_prefix(layer_predecessor) or matches_pre_frontier_repair(historical_layer_predecessor) or matches_specialist_catalog_prefix( historical_layer_predecessor ) ) if ( type(source_layers) is int and type(target_layers) is int and 1 <= source_layers <= target_layers and lineage.get("scienceExperts") == expected.get("scienceExperts") ): # Compose the independently proven storage-neutral lineage # normalization with the signed predecessor-plan transition. # Comparing the raw records here makes two individually valid # historical differences (the compact overlay marker and the # later redundant global page count) reject a real signed # graph-growth predecessor when they occur together. current_geometry_predecessor = dict( normalized_expected_lineage ) current_geometry_predecessor["scienceLayers"] = source_layers if source_layers < target_layers: current_geometry_predecessor[ "draftingCheckpointGeometryChanged" ] = False current_source_paged = normalized_source_lineage.get( "pagedNoNE" ) current_target_paged = normalized_expected_lineage.get( "pagedNoNE" ) if isinstance(current_source_paged, dict) and isinstance( current_target_paged, dict, ): current_geometry_paged = dict(current_target_paged) current_geometry_paged["scienceLayers"] = source_layers current_geometry_paged["totalScienceLayerCount"] = ( source_layers ) signed_source_plan_sha256 = ( signed_offline_predecessor_plan_sha256( current_source_paged ) ) if signed_source_plan_sha256 is not None: current_geometry_paged[ "reasoningGrowthPlanSha256" ] = signed_source_plan_sha256 current_geometry_predecessor["pagedNoNE"] = ( current_geometry_paged ) if normalized_source_lineage == current_geometry_predecessor: return previous = pre_broad_language_v22(expected) previous["schema"] = ( "nnf.resynthesis.composed_additive_lineage.v21" ) previous["scienceLayers"] = source_layers for field in ( "reasoningLayerExecutionGate", "reasoningLayerGrowthInitialization", "reasoningLayerGrowthIdentityAtMigration", "reasoningLayerCheckpointGeometryChanged", "graphEndstateRouting", "graphEndstateSolvedBackward", "hierarchicalIntentLadder", ): previous.pop(field) source_paged = lineage.get("pagedNoNE") target_paged = expected.get("pagedNoNE") if isinstance(source_paged, dict) and isinstance( target_paged, dict, ): if ( source_paged.get("schema") not in { "nnf.resynthesis.paged_none_lineage.v1", "nnf.resynthesis.paged_none_lineage.v2", "nnf.resynthesis.paged_none_lineage.v3", } or source_paged.get("scienceLayers") != source_layers ): raise RuntimeError( "Resynthesis paged reasoning predecessor differs" ) previous_paged = dict(target_paged) previous_paged["schema"] = source_paged["schema"] previous_paged["scienceLayers"] = source_layers for field in ( "totalScienceLayerCount", "pagedRuntimeLayerCount", "pagedRuntimeLayerIds", "allScienceLayersRequireLocalPageRuntime", "pageKnowledgeTransfersAcrossScienceLayers", "reasoningGrowthPlanSha256", ): previous_paged.pop(field) previous["pagedNoNE"] = previous_paged if lineage == previous or matches_pre_frontier_repair(previous): return previous = pre_vocabulary_transfer_v28(expected) if matches_layer_composed_candidate(previous): return previous = pre_causal_mhc_v27(expected) if matches_layer_composed_candidate(previous): return previous = pre_knowledge_transfer_v26(expected) if matches_layer_composed_candidate(previous): return previous = pre_assurance_v25(expected) if matches_layer_composed_candidate(previous): return previous = pre_exploration_meta_v24(expected) if matches_layer_composed_candidate(previous): return previous = pre_broad_language_v22(expected) if matches_layer_composed_candidate(previous): return previous["schema"] = "nnf.resynthesis.composed_additive_lineage.v21" for field in ( "reasoningLayerExecutionGate", "reasoningLayerGrowthInitialization", "reasoningLayerGrowthIdentityAtMigration", "reasoningLayerCheckpointGeometryChanged", "graphEndstateRouting", "graphEndstateSolvedBackward", "hierarchicalIntentLadder", ): previous.pop(field) if matches_layer_composed_candidate(previous): return previous["schema"] = "nnf.resynthesis.composed_additive_lineage.v20" previous["molecularScienceHeads"] = tuple( head for head in previous["molecularScienceHeads"] if head not in {"diffusion_time_conditioning", "vibrational_spectrum"} ) previous.pop("molecularDiffusionTimeConditioning") previous.pop("molecularVibrationalSpectrumHead") previous.pop("molecularModalExtensionInitialization") previous.pop("molecularModalExtensionIdentityResidualAtMigration") previous.pop("molecularModalExtensionCheckpointGeometryChanged") previous.pop("molecularScienceDiffusionTimeConditioning") previous.pop("molecularScienceVibrationalSpectrum") previous.pop("molecularScienceIncrementalExtensionInitialization") previous.pop("molecularScienceIncrementalExtensionKeySetSha256") previous.pop( "molecularScienceIncrementalExtensionCheckpointGeometryChanged" ) if matches_layer_composed_candidate(previous): return previous["schema"] = "nnf.resynthesis.composed_additive_lineage.v19" previous.pop("nlaStopTrajectoryConditioning") previous.pop("nlaTaskConfidenceConditioning") previous.pop("nlaConfidenceInitialization") previous.pop("nlaConfidenceControlPrecision") previous.pop("nlaConfidenceCheckpointGeometryChanged") previous.pop("perTokenEmissionTracePacket") previous.pop("perTokenEmissionTraceHasAnswerAuthority") if matches_layer_composed_candidate(previous): return previous["schema"] = "nnf.resynthesis.composed_additive_lineage.v18" previous["functionalCapabilityFamilies"] = int( previous["functionalCapabilityFamilies"] ) - len(NONE_V2_PLUS_SCIENCE_SPECIALIST_DEFINITIONS) previous.pop("scienceSpecialistCapabilityCatalog") previous.pop("scienceSpecialistCapabilityFamilies") previous.pop("scienceSpecialistCapabilityIdsSha256") previous.pop("scienceSpecialistCapabilityNativeRouting") previous.pop("scienceSpecialistCapabilityCheckpointGeometryChanged") previous.pop("scienceSpecialistCapabilitiesRegisteredNotTrained") if matches_layer_composed_candidate(previous): return previous["schema"] = "nnf.resynthesis.composed_additive_lineage.v17" previous["functionalCapabilityFamilies"] = int( previous["functionalCapabilityFamilies"] ) - NONE_LANGUAGE_EXPERT_INHERITED_PREFIX_COUNT previous.pop("languageCapabilityCatalog") previous.pop("languageCapabilityPacks") previous.pop("languageCapabilityPackIdsSha256") previous.pop("languageCapabilityNativeRouting") previous.pop("languageCapabilityCheckpointGeometryChanged") if matches_layer_composed_candidate(previous): return previous = dict(previous) previous["schema"] = "nnf.resynthesis.composed_additive_lineage.v16" previous.pop("completionSuccessorAuthorityLifecycle") previous.pop("completionSuccessorUsesExistingTrainedSurface") previous.pop("completionSuccessorCandidateRequiresGradientProof") previous.pop("completionSuccessorRetentionRequiresValidation") previous.pop("completionSuccessorParentFallbackPreserved") previous.pop("completionSuccessorCheckpointGeometryChanged") if lineage == previous or matches_pre_frontier_repair(previous): return previous = dict(previous) previous["schema"] = "nnf.resynthesis.composed_additive_lineage.v15" previous.pop("functionalCapabilityProjection") previous.pop("functionalCapabilityFamilies") previous.pop("functionalCapabilityMatchRouting") previous.pop("functionalCapabilityInitialization") previous.pop("functionalCapabilityIdentityResidualAtMigration") previous.pop("functionalCapabilityCheckpointGeometryChanged") if lineage == previous or matches_pre_frontier_repair(previous): return previous["schema"] = "nnf.resynthesis.composed_additive_lineage.v14" previous.pop("molecularScienceBank") previous.pop("molecularScienceHeads") previous.pop("molecularScienceInitialization") previous.pop("molecularScienceIdentityResidualAtMigration") previous.pop("molecularScienceCheckpointGeometryChanged") if lineage == previous or matches_pre_frontier_repair(previous): return previous["schema"] = "nnf.resynthesis.composed_additive_lineage.v13" previous.pop("quantileBalancedRouting") previous.pop("quantileRoutingCausalBias") previous.pop("crConditionedKDA") previous.pop("deltaAttentionResidual") previous.pop("mhcDepthConnectivity") previous.pop(NATIVE_ATTENTION_CHECKPOINT_GEOMETRY_FIELD) if lineage == previous or matches_pre_frontier_repair(previous): return previous["schema"] = "nnf.resynthesis.composed_additive_lineage.v12" previous.pop("contextIntentActionAttention") previous.pop("contextActionSource") previous.pop("contextActionDim") previous.pop("contextActionScorePivot") previous.pop("contextActionCheckpointGeometryChanged") if lineage == previous or matches_pre_frontier_repair(previous): return previous["schema"] = "nnf.resynthesis.composed_additive_lineage.v11" previous["attentionMultiples"] = ("q", "k", "v", "c") previous.pop("intentRelationalAttention") previous.pop("contextQueryPivot") previous.pop("relationConnectivity") previous.pop("scienceAttentionExactTiling") previous.pop("scienceAttentionTileTokens") previous.pop("contextRelationCheckpointGeometryChanged") if lineage == previous or matches_pre_frontier_repair(previous): return previous = dict(previous) previous["schema"] = "nnf.resynthesis.composed_additive_lineage.v10" previous.pop("intentContextPivotAttention") previous.pop("attentionMultiples") if lineage == previous or matches_pre_frontier_repair(previous): return previous["schema"] = "nnf.resynthesis.composed_additive_lineage.v9" previous.pop("parentDualChunkRoPECompose") previous.pop("additiveOnlineSoftmaxLongPool") previous.pop("dualChunkAtSuccessiveSeamExposed") previous.pop("nativeContextPositionAperture") previous.pop("onlineSoftmaxTileTokens") previous.pop("nativePrefillTileTokens") previous.pop("longContextStackComposeCheckpointGeometryChanged") if lineage == previous or matches_pre_frontier_repair(previous): return previous["schema"] = "nnf.resynthesis.composed_additive_lineage.v8" previous.pop("draftingExplicitLifecycleTensorStates") previous.pop("draftingModelOwnedResidualGate") previous.pop("experimentReadinessInCompletionConjunction") previous.pop("parentRecursiveFrontierModelOwnedBlend") previous.pop("parentRecursiveFrontierAllLayersContribute") previous.pop("parentRecursiveFrontierBlendCheckpointGeometryChanged") if lineage == previous or matches_pre_frontier_repair(previous): return previous["schema"] = "nnf.resynthesis.composed_additive_lineage.v7" previous.pop("draftProposalCausalBeforeExternalOutcome") previous.pop("draftingAllLifecyclePhasesTrainedAtLossBoundary") previous.pop("generationArmExhaustionIndependentOfDelegation") previous.pop("generationArmExhaustionGrantsCompletion") if lineage == previous or matches_pre_frontier_repair(previous): return previous["schema"] = "nnf.resynthesis.composed_additive_lineage.v6" previous.pop("parentRecursiveArmExhaustionPropagation") previous.pop("parentRecursiveArmExhaustionCheckpointGeometryChanged") parent = previous.get("parent") if isinstance(parent, dict) and ( parent.get("schema") == "nnf.resynthesis.parent_lineage.v4" ): previous_parent = dict(parent) previous_parent["schema"] = "nnf.resynthesis.parent_lineage.v3" previous_parent.pop( "historicalRecursiveArmExhaustionId", None, ) previous_parent.pop( "historicalRecursiveArmExhaustionPropagation", None, ) previous["parent"] = previous_parent if lineage == previous: return previous["schema"] = "nnf.resynthesis.composed_additive_lineage.v5" previous.pop("modelNativeDraftingLifecycle") previous.pop("parallelDraftWorkers") previous.pop("verifiedExperimentEvidenceGate") previous.pop("draftingCheckpointGeometryChanged") if lineage == previous: return previous["schema"] = "nnf.resynthesis.composed_additive_lineage.v4" previous.pop("completionReadinessConfidenceConjunction") previous.pop("completionLossExcludesTaskConfidence") parent = previous.get("parent") if isinstance(parent, dict) and ( parent.get("schema") == "nnf.resynthesis.parent_lineage.v3" ): previous_parent = dict(parent) previous_parent["schema"] = "nnf.resynthesis.parent_lineage.v2" previous_parent.pop( "historicalTensorNativeHotpathId", None, ) previous_parent.pop( "historicalTensorNativeExpertDispatch", None, ) previous_parent.pop( "historicalTensorNativeHopSelection", None, ) previous["parent"] = previous_parent if lineage == previous: return previous["schema"] = "nnf.resynthesis.composed_additive_lineage.v3" previous.pop("parentCausalKvContinuation") previous.pop("scienceCausalPositionOnly") previous.pop("decodeArmAuthorityTransactions") if lineage == previous: return differing_fields = { key: { "checkpoint": lineage.get(key) if isinstance(lineage, dict) else None, "active": expected.get(key), } for key in sorted( set(expected) | (set(lineage) if isinstance(lineage, dict) else set()) ) if not isinstance(lineage, dict) or lineage.get(key) != expected.get(key) } raise RuntimeError( "Resynthesis additive checkpoint lineage differs from the active graph: " + json.dumps(differing_fields, sort_keys=True, default=str) ) def adapt_trainable_state_dict( self, state: dict[str, torch.Tensor], *, checkpoint_lineage: object | None = None, ) -> tuple[dict[str, torch.Tensor], bool]: """Adapt explicitly versioned predecessors into the live additive graph.""" attention_state, attention_changed = adapt_attention_state_to_context_relation( state ) target_state = self._trainable_state_references() active_state, native_attention_changed = ( adapt_native_attention_expert_state( attention_state, target_state, ) ) parent_route_gate_parameterization_changed = False parent_route_gate_version_name = ( "fabric.parent_route_gate_parameterization_t" ) parent_route_gate_parameter_names = ( "fabric.parent_expert_route_gate", "fabric.parent_layer_route_gate", ) target_parent_route_gate_version = target_state.get( parent_route_gate_version_name ) if not isinstance(target_parent_route_gate_version, torch.Tensor): raise RuntimeError( "parent route-gate parameterization version is absent" ) source_parent_route_gate_version = active_state.get( parent_route_gate_version_name ) if source_parent_route_gate_version is None: if any( name not in active_state for name in parent_route_gate_parameter_names ): raise RuntimeError( "legacy parent route-gate state is incomplete" ) # v1 and v2 share the exact learned logit coordinates and sigmoid # derivative. Only v2 centers sigmoid(0) at zero, so adoption adds # the persistent interpretation tag without rewriting accepted # parameters or invalidating their optimizer moments. active_state[parent_route_gate_version_name] = ( target_parent_route_gate_version.detach() .to(device="cpu") .clone() ) parent_route_gate_parameterization_changed = True elif ( source_parent_route_gate_version.shape != target_parent_route_gate_version.shape or source_parent_route_gate_version.dtype != target_parent_route_gate_version.dtype or not torch.equal( source_parent_route_gate_version.detach().to(device="cpu"), target_parent_route_gate_version.detach().to(device="cpu"), ) ): raise RuntimeError( "parent route-gate parameterization version differs" ) telemetry_changed = False nonpersistent_changed = False for name in self._nonpersistent_additive_buffer_names(): if name in active_state: active_state.pop(name) nonpersistent_changed = True telemetry_suffixes = ( ".accepted_route_count_t", ".accepted_gradient_update_count_t", ".accepted_gradient_norm_t", ".accepted_parameter_delta_norm_t", ".accepted_gradient_signature_t", ) for name, target in target_state.items(): if ( name not in active_state and ".paged_expert_runtime." in name and name.endswith(telemetry_suffixes) ): active_state[name] = target.detach().to(device="cpu").clone() telemetry_changed = True target_lineage = self.checkpoint_lineage() predecessor_growth = ( checkpoint_lineage is not None and checkpoint_lineage != target_lineage ) lineage_validated = False def validate_growth_lineage() -> None: nonlocal lineage_validated if not predecessor_growth: return if lineage_validated: return if not isinstance(checkpoint_lineage, dict): raise RuntimeError("additive predecessor lineage is malformed") schema = checkpoint_lineage.get("schema") if schema == COMPOSED_ADDITIVE_LINEAGE_SCHEMA: self.validate_checkpoint_lineage(checkpoint_lineage) elif not ( isinstance(schema, str) and schema.startswith( "nnf.resynthesis.composed_additive_lineage.v" ) ): self.validate_checkpoint_lineage(checkpoint_lineage) # Older composed-lineage schemas migrate through the explicit # versioned tensor seeding below; metadata-only nulls are not # rejection evidence before that migration completes. lineage_validated = True seeded_labels: list[str] = [] seeded_geometry: list[tuple[str, tuple[int, ...], str]] = [] seeded_parameter_elements = 0 seeded_value_digest = hashlib.sha256() def record_seeded_value(label: str, value: torch.Tensor) -> None: nonlocal seeded_parameter_elements cpu_value = value.detach().to(device="cpu").contiguous() seeded_labels.append(label) seeded_geometry.append( (label, tuple(cpu_value.shape), str(cpu_value.dtype)) ) seeded_parameter_elements += cpu_value.numel() seeded_value_digest.update(label.encode("utf-8")) seeded_value_digest.update(b"\0") seeded_value_digest.update( cpu_value.reshape(-1).view(torch.uint8).numpy().tobytes() ) vocabulary_transfer_state_names = { name for name in target_state if name.startswith("vocabulary_transfer_") } supplied_vocabulary_transfer_names = { name for name in active_state if name.startswith("vocabulary_transfer_") } unexpected_vocabulary_transfer_names = ( supplied_vocabulary_transfer_names - vocabulary_transfer_state_names ) if unexpected_vocabulary_transfer_names: raise RuntimeError( "unexpected Resynthesis vocabulary-transfer state is " "forbidden: " + ", ".join(sorted(unexpected_vocabulary_transfer_names)) ) missing_vocabulary_transfer_names = ( vocabulary_transfer_state_names - set(active_state) ) vocabulary_transfer_changed = False vocabulary_transfer_geometry_changed = False vocabulary_transfer_source_rank = 0 vocabulary_transfer_target_rank = self.vocabulary_transfer_rank vocabulary_transfer_added_rank = 0 vocabulary_transfer_source_lexical_rows = self.lexical_vocab_size vocabulary_transfer_target_lexical_rows = self.lexical_vocab_size vocabulary_transfer_added_lexical_rows = 0 vocabulary_transfer_expanded_tensor_count = 0 vocabulary_transfer_prefix_exact = True checkpoint_vocabulary_schema = ( checkpoint_lineage.get("schema") if isinstance(checkpoint_lineage, dict) else None ) if missing_vocabulary_transfer_names: if ( missing_vocabulary_transfer_names != vocabulary_transfer_state_names ): raise RuntimeError( "partial Resynthesis vocabulary-transfer state is " "forbidden" ) if ( not predecessor_growth or checkpoint_vocabulary_schema == COMPOSED_ADDITIVE_LINEAGE_SCHEMA ): raise RuntimeError( "unversioned Resynthesis vocabulary-transfer growth state " "is forbidden" ) validate_growth_lineage() for name in sorted(vocabulary_transfer_state_names): seeded = ( target_state[name].detach().to(device="cpu").clone() ) if ( name == "vocabulary_transfer_up.weight" and torch.count_nonzero(seeded) ): raise RuntimeError( "Resynthesis vocabulary-transfer migration must start " "as an exact lexical identity" ) active_state[name] = seeded record_seeded_value(name, seeded) vocabulary_transfer_changed = True vocabulary_transfer_added_rank = self.vocabulary_transfer_rank vocabulary_transfer_source_lexical_rows = 0 vocabulary_transfer_added_lexical_rows = self.lexical_vocab_size elif vocabulary_transfer_state_names: source_vocabulary_down = active_state[ "vocabulary_transfer_down.weight" ] source_vocabulary_up = active_state[ "vocabulary_transfer_up.weight" ] target_vocabulary_down = target_state[ "vocabulary_transfer_down.weight" ] target_vocabulary_up = target_state[ "vocabulary_transfer_up.weight" ] vocabulary_transfer_source_rank = ( source_vocabulary_down.shape[0] ) vocabulary_transfer_target_rank = ( target_vocabulary_down.shape[0] ) vocabulary_transfer_source_lexical_rows = ( source_vocabulary_up.shape[0] ) vocabulary_transfer_target_lexical_rows = ( target_vocabulary_up.shape[0] ) vocabulary_geometry_exact = ( source_vocabulary_down.shape == target_vocabulary_down.shape and source_vocabulary_up.shape == target_vocabulary_up.shape ) if not vocabulary_geometry_exact: source_rank = source_vocabulary_down.shape[0] target_rank = target_vocabulary_down.shape[0] source_lineage_rank = ( checkpoint_lineage.get("vocabularyTransferRank") if isinstance(checkpoint_lineage, dict) else None ) source_lineage_lexical_rows = ( checkpoint_lineage.get("lexicalVocabularySize") if isinstance(checkpoint_lineage, dict) else None ) source_lexical_rows = source_vocabulary_up.shape[0] target_lexical_rows = target_vocabulary_up.shape[0] expandable_vocabulary_geometry = ( source_vocabulary_down.ndim == 2 and source_vocabulary_up.ndim == 2 and target_vocabulary_down.ndim == 2 and target_vocabulary_up.ndim == 2 and source_vocabulary_down.shape[1] == target_vocabulary_down.shape[1] == self.projection_only_vocab_rows and target_lexical_rows == self.lexical_vocab_size and source_vocabulary_up.shape[1] == source_rank and target_vocabulary_up.shape[1] == target_rank and 0 < source_rank <= target_rank and 0 < source_lexical_rows <= target_lexical_rows and ( source_rank < target_rank or source_lexical_rows < target_lexical_rows ) and checkpoint_vocabulary_schema == COMPOSED_ADDITIVE_LINEAGE_SCHEMA and source_lineage_rank == source_rank and source_lineage_lexical_rows == source_lexical_rows ) if not expandable_vocabulary_geometry: raise RuntimeError( "Resynthesis vocabulary-transfer projection cannot " "preserve inherited geometry" ) validate_growth_lineage() expanded_vocabulary_down = ( _deterministic_xavier_tensor( target_vocabulary_down.detach().to(device="cpu"), tuple(target_vocabulary_down.shape), ( "resynthesis_vocabulary_transfer_down_v1:" f"{source_rank}->{target_rank}" ), ) ) expanded_vocabulary_up = torch.zeros_like( target_vocabulary_up, device="cpu", ) expanded_vocabulary_down[:source_rank, :].copy_( source_vocabulary_down.detach().to(device="cpu") ) expanded_vocabulary_up[ :source_lexical_rows, :source_rank, ].copy_( source_vocabulary_up.detach().to(device="cpu") ) vocabulary_transfer_prefix_exact = bool( torch.equal( expanded_vocabulary_down[:source_rank, :], source_vocabulary_down.detach().to(device="cpu"), ) and torch.equal( expanded_vocabulary_up[ :source_lexical_rows, :source_rank, ], source_vocabulary_up.detach().to(device="cpu"), ) ) if not vocabulary_transfer_prefix_exact: raise RuntimeError( "Resynthesis vocabulary-transfer migration changed " "inherited weights" ) active_state["vocabulary_transfer_down.weight"] = ( expanded_vocabulary_down ) active_state["vocabulary_transfer_up.weight"] = ( expanded_vocabulary_up ) if source_rank < target_rank: record_seeded_value( ( "vocabulary_transfer_down.weight" f"[{source_rank}:{target_rank},:]" ), expanded_vocabulary_down[ source_rank:target_rank, :, ], ) record_seeded_value( ( "vocabulary_transfer_up.weight" f"[:{source_lexical_rows}," f"{source_rank}:{target_rank}]" ), expanded_vocabulary_up[ :source_lexical_rows, source_rank:target_rank, ], ) vocabulary_transfer_expanded_tensor_count += 2 if source_lexical_rows < target_lexical_rows: record_seeded_value( ( "vocabulary_transfer_up.weight" f"[{source_lexical_rows}:" f"{target_lexical_rows},:]" ), expanded_vocabulary_up[ source_lexical_rows:target_lexical_rows, :, ], ) if source_rank == target_rank: vocabulary_transfer_expanded_tensor_count += 1 vocabulary_transfer_geometry_changed = True vocabulary_transfer_added_rank = target_rank - source_rank vocabulary_transfer_added_lexical_rows = ( target_lexical_rows - source_lexical_rows ) anti_thompson_outcome_state_names = { name for name in target_state if name.endswith(ANTI_THOMPSON_FAIL_COUNTS_BUFFER_SUFFIX) } supplied_anti_thompson_outcome_names = { name for name in active_state if name.endswith(ANTI_THOMPSON_FAIL_COUNTS_BUFFER_SUFFIX) } unexpected_anti_thompson_outcome_names = ( supplied_anti_thompson_outcome_names - anti_thompson_outcome_state_names ) if unexpected_anti_thompson_outcome_names: raise RuntimeError( "unexpected anti-thompson routing outcome state is forbidden: " + ", ".join( sorted(unexpected_anti_thompson_outcome_names) ) ) missing_anti_thompson_outcome_names = ( anti_thompson_outcome_state_names - set(active_state) ) layer_growth_anti_thompson_outcome_names: set[str] = set() if predecessor_growth and isinstance(checkpoint_lineage, dict): source_layer_count = checkpoint_lineage.get("scienceLayers") if ( type(source_layer_count) is int and 1 <= source_layer_count < self.science_stack.num_layers ): layer_growth_anti_thompson_outcome_names = { ( f"science_stack.science_layer_{layer_index}" f"{ANTI_THOMPSON_FAIL_COUNTS_BUFFER_SUFFIX}" ) for layer_index in range( source_layer_count, self.science_stack.num_layers, ) } & anti_thompson_outcome_state_names for name in sorted(supplied_anti_thompson_outcome_names): source = active_state[name] target = target_state[name] if source.shape != target.shape: raise RuntimeError( "anti-thompson routing outcome state geometry differs: " f"{name}" ) if source.dtype != target.dtype: raise RuntimeError( "anti-thompson routing outcome state dtype differs: " f"{name}" ) anti_thompson_outcome_changed = False if missing_anti_thompson_outcome_names: if ( missing_anti_thompson_outcome_names != anti_thompson_outcome_state_names and missing_anti_thompson_outcome_names != layer_growth_anti_thompson_outcome_names ): raise RuntimeError( "partial anti-thompson routing outcome state is forbidden" ) if checkpoint_lineage is None: raise RuntimeError( "unversioned anti-thompson routing outcome growth state " "is forbidden" ) validate_growth_lineage() for name in sorted(missing_anti_thompson_outcome_names): seeded = ( target_state[name].detach().to(device="cpu").clone() ) if torch.count_nonzero(seeded): raise RuntimeError( "anti-thompson routing outcome growth must start at zero" ) active_state[name] = seeded record_seeded_value(name, seeded) anti_thompson_outcome_changed = True trauma_state_component = ".quantile_router.trauma_state." trauma_state_names = { name for name in target_state if trauma_state_component in name } supplied_trauma_state_names = { name for name in active_state if trauma_state_component in name } unexpected_trauma_state_names = ( supplied_trauma_state_names - trauma_state_names ) if unexpected_trauma_state_names: raise RuntimeError( "unexpected quantile-router trauma state is forbidden: " + ", ".join(sorted(unexpected_trauma_state_names)) ) trauma_state_prefixes = { name.partition(trauma_state_component)[0] + trauma_state_component for name in trauma_state_names } for trauma_state_prefix in trauma_state_prefixes: expected_family = { name for name in trauma_state_names if name.startswith(trauma_state_prefix) } supplied_family = ( expected_family & supplied_trauma_state_names ) if supplied_family and supplied_family != expected_family: raise RuntimeError( "partial quantile-router trauma state is forbidden: " + trauma_state_prefix ) for name in sorted(supplied_trauma_state_names): source = active_state[name] target = target_state[name] if source.shape != target.shape: raise RuntimeError( "quantile-router trauma state geometry differs: " f"{name}" ) if source.dtype != target.dtype: if not ( source.is_floating_point() and target.is_floating_point() ): raise RuntimeError( "quantile-router trauma state dtype differs: " f"{name}" ) active_state[name] = source.to(dtype=target.dtype) missing_trauma_state_names = trauma_state_names - set(active_state) layer_growth_trauma_state_names: set[str] = set() if predecessor_growth and isinstance(checkpoint_lineage, dict): source_layer_count = checkpoint_lineage.get("scienceLayers") if ( type(source_layer_count) is int and 1 <= source_layer_count < self.science_stack.num_layers ): layer_growth_trauma_state_names = { name for layer_index in range( source_layer_count, self.science_stack.num_layers, ) for name in trauma_state_names if name.startswith( f"science_stack.science_layer_{layer_index}" f"{trauma_state_component}" ) } trauma_state_changed = False if missing_trauma_state_names: if ( missing_trauma_state_names != trauma_state_names and missing_trauma_state_names != layer_growth_trauma_state_names ): raise RuntimeError( "partial quantile-router trauma state is forbidden" ) if checkpoint_lineage is None: raise RuntimeError( "unversioned quantile-router trauma growth state is " "forbidden" ) validate_growth_lineage() # A predecessor without any Trauma tensors has no hard-knowledge # history to infer. Preserve every supplied router exactly and seed # only the wholly absent constructor family, including its -1 # cooldown sentinel and empty KLA identity bank. for name in sorted(missing_trauma_state_names): seeded = ( target_state[name].detach().to(device="cpu").clone() ) active_state[name] = seeded record_seeded_value(name, seeded) trauma_state_changed = True runtime_capability_integration_state_names = { name for name in target_state if name.startswith(_CAUSAL_CAPABILITY_INTEGRATION_STATE_PREFIX) } if ( runtime_capability_integration_state_names != _CAUSAL_CAPABILITY_INTEGRATION_STATE_NAMES ): # Keep every reviewed adapter exact: v23 -> v24 owns nine causal # and outcome tensors, v24 -> v25 owns four selector/meta tensors, # v25 -> v26 owns assurance, v26 -> v27 owns knowledge transfer, # and v27 -> v28 owns the proof-coupled MHC heads. raise RuntimeError( "causal capability integration runtime schema differs" ) capability_integration_state_names = set( _CAUSAL_CAPABILITY_INTEGRATION_STATE_NAMES ) causal_capability_core_v24_state_names = set( _CAUSAL_CAPABILITY_CORE_V24_STATE_NAMES ) exploration_outcome_v24_state_names = set( _EXPLORATION_OUTCOME_V24_STATE_NAMES ) causal_capability_v24_state_names = set( _CAUSAL_CAPABILITY_INTEGRATION_V24_STATE_NAMES ) exploration_meta_v25_state_names = set( _EXPLORATION_META_INTEGRATION_V25_STATE_NAMES ) assurance_v26_state_names = set( _ASSURANCE_INTEGRATION_V26_STATE_NAMES ) knowledge_transfer_v27_state_names = set( _KNOWLEDGE_TRANSFER_INTEGRATION_V27_STATE_NAMES ) causal_mhc_v28_state_names = set( _CAUSAL_CONTRASTIVE_MHC_V28_STATE_NAMES ) supplied_capability_integration_names = { name for name in active_state if name.startswith(_CAUSAL_CAPABILITY_INTEGRATION_STATE_PREFIX) } unexpected_capability_integration_names = ( supplied_capability_integration_names - capability_integration_state_names ) if unexpected_capability_integration_names: raise RuntimeError( "unexpected causal capability integration growth state is forbidden: " + ", ".join(sorted(unexpected_capability_integration_names)) ) checkpoint_lineage_schema = ( checkpoint_lineage.get("schema") if isinstance(checkpoint_lineage, dict) else None ) checkpoint_capability_stage_names = { "nnf.resynthesis.composed_additive_lineage.v23": frozenset(), "nnf.resynthesis.composed_additive_lineage.v24": frozenset( causal_capability_v24_state_names ), "nnf.resynthesis.composed_additive_lineage.v25": frozenset( causal_capability_v24_state_names | exploration_meta_v25_state_names ), "nnf.resynthesis.composed_additive_lineage.v26": frozenset( causal_capability_v24_state_names | exploration_meta_v25_state_names | assurance_v26_state_names ), "nnf.resynthesis.composed_additive_lineage.v27": frozenset( causal_capability_v24_state_names | exploration_meta_v25_state_names | assurance_v26_state_names | knowledge_transfer_v27_state_names ), "nnf.resynthesis.composed_additive_lineage.v28": frozenset( capability_integration_state_names ), COMPOSED_ADDITIVE_LINEAGE_SCHEMA: frozenset( capability_integration_state_names ), } missing_causal_capability_v24_names = ( causal_capability_v24_state_names - set(active_state) ) missing_exploration_meta_v25_names = ( exploration_meta_v25_state_names - set(active_state) ) missing_assurance_v26_names = ( assurance_v26_state_names - set(active_state) ) missing_knowledge_transfer_v27_names = ( knowledge_transfer_v27_state_names - set(active_state) ) missing_causal_mhc_v28_names = ( causal_mhc_v28_state_names - set(active_state) ) # Diagnose torn families before the broader lineage-stage check. A # single future-family tensor in an older checkpoint is not a valid # migration, but identifying the partial family is the actionable # restart-safety failure; complete future families are rejected by the # generic version guard immediately afterward. partial_family_checks = ( ( missing_causal_capability_v24_names, causal_capability_v24_state_names, "partial causal capability integration growth state is forbidden", ), ( missing_exploration_meta_v25_names, exploration_meta_v25_state_names, "partial exploration-meta integration growth state is forbidden", ), ( missing_assurance_v26_names, assurance_v26_state_names, "partial assurance integration growth state is forbidden", ), ( missing_knowledge_transfer_v27_names, knowledge_transfer_v27_state_names, "partial knowledge-transfer integration growth state is forbidden", ), ( missing_causal_mhc_v28_names, causal_mhc_v28_state_names, "partial causal contrastive MHC growth state is forbidden", ), ) for missing_family_names, complete_family_names, message in ( partial_family_checks ): if missing_family_names and missing_family_names != complete_family_names: raise RuntimeError(message) checkpoint_stage_names = ( checkpoint_capability_stage_names.get(checkpoint_lineage_schema) if isinstance(checkpoint_lineage_schema, str) else None ) if checkpoint_stage_names is not None: supplied_future_names = ( supplied_capability_integration_names - checkpoint_stage_names ) if supplied_future_names: raise RuntimeError( "causal capability integration state is newer than its " "versioned checkpoint: " + ", ".join(sorted(supplied_future_names)) ) causal_capability_v24_changed = False if missing_causal_capability_v24_names: # The v23 -> v24 migration owns exactly nine tensors: six causal # tensors and three durable anti-Thompson outcome buffers. Missing # only part of the family, or removing it from a v24+ checkpoint, # is durable-state damage rather than moving-graph growth. if ( missing_causal_capability_v24_names != causal_capability_v24_state_names ): raise RuntimeError( "partial causal capability integration growth state is forbidden" ) if not predecessor_growth: raise RuntimeError( "unversioned causal capability integration growth state " "is forbidden" ) if checkpoint_lineage_schema in { "nnf.resynthesis.composed_additive_lineage.v24", "nnf.resynthesis.composed_additive_lineage.v25", "nnf.resynthesis.composed_additive_lineage.v26", "nnf.resynthesis.composed_additive_lineage.v27", "nnf.resynthesis.composed_additive_lineage.v28", COMPOSED_ADDITIVE_LINEAGE_SCHEMA, }: raise RuntimeError( "causal capability integration state is absent from its " "versioned checkpoint" ) validate_growth_lineage() for name in sorted(causal_capability_v24_state_names): active_state[name] = ( target_state[name].detach().to(device="cpu").clone() ) record_seeded_value(name, active_state[name]) causal_capability_v24_changed = True exploration_meta_v25_changed = False if missing_exploration_meta_v25_names: # The v24 -> v25 migration is independent of the nine-tensor v24 # family. It owns exactly four selector/meta tensors so an # accepted v24 checkpoint can advance without resetting its learned # causal integration weights. if ( missing_exploration_meta_v25_names != exploration_meta_v25_state_names ): raise RuntimeError( "partial exploration-meta integration growth state is forbidden" ) if ( not predecessor_growth or checkpoint_lineage_schema in { "nnf.resynthesis.composed_additive_lineage.v25", "nnf.resynthesis.composed_additive_lineage.v26", "nnf.resynthesis.composed_additive_lineage.v27", "nnf.resynthesis.composed_additive_lineage.v28", COMPOSED_ADDITIVE_LINEAGE_SCHEMA, } ): raise RuntimeError( "unversioned exploration-meta integration growth state " "is forbidden" ) validate_growth_lineage() for name in sorted(exploration_meta_v25_state_names): active_state[name] = ( target_state[name].detach().to(device="cpu").clone() ) record_seeded_value(name, active_state[name]) exploration_meta_v25_changed = True assurance_v26_changed = False if missing_assurance_v26_names: if ( missing_assurance_v26_names != assurance_v26_state_names ): raise RuntimeError( "partial assurance integration growth state is forbidden" ) if ( not predecessor_growth or checkpoint_lineage_schema in { "nnf.resynthesis.composed_additive_lineage.v26", "nnf.resynthesis.composed_additive_lineage.v27", "nnf.resynthesis.composed_additive_lineage.v28", COMPOSED_ADDITIVE_LINEAGE_SCHEMA, } ): raise RuntimeError( "unversioned assurance integration growth state " "is forbidden" ) validate_growth_lineage() for name in sorted(assurance_v26_state_names): active_state[name] = ( target_state[name].detach().to(device="cpu").clone() ) record_seeded_value(name, active_state[name]) assurance_v26_changed = True knowledge_transfer_v27_changed = False if missing_knowledge_transfer_v27_names: # v26 -> v27 owns one complete tensor-native transfer family. A # partial family is corrupted state; only its exact total absence # in a versioned predecessor may seed the live generation. if ( missing_knowledge_transfer_v27_names != knowledge_transfer_v27_state_names ): raise RuntimeError( "partial knowledge-transfer integration growth state " "is forbidden" ) if ( not predecessor_growth or checkpoint_lineage_schema in { "nnf.resynthesis.composed_additive_lineage.v27", "nnf.resynthesis.composed_additive_lineage.v28", COMPOSED_ADDITIVE_LINEAGE_SCHEMA, } ): raise RuntimeError( "unversioned knowledge-transfer integration growth state " "is forbidden" ) validate_growth_lineage() for name in sorted(knowledge_transfer_v27_state_names): active_state[name] = ( target_state[name].detach().to(device="cpu").clone() ) record_seeded_value(name, active_state[name]) knowledge_transfer_v27_changed = True causal_mhc_v28_changed = False if missing_causal_mhc_v28_names: # v27 -> v28 materializes one complete MHC family. A partial set # cannot represent a valid bounded head because its Sinkhorn # source, wrapped weights, bias, and mix scalar are jointly owned. if missing_causal_mhc_v28_names != causal_mhc_v28_state_names: raise RuntimeError( "partial causal contrastive MHC growth state is forbidden" ) if ( not predecessor_growth or checkpoint_lineage_schema in { "nnf.resynthesis.composed_additive_lineage.v28", COMPOSED_ADDITIVE_LINEAGE_SCHEMA, } ): raise RuntimeError( "unversioned causal contrastive MHC growth state is forbidden" ) validate_growth_lineage() for name in sorted(causal_mhc_v28_state_names): active_state[name] = ( target_state[name].detach().to(device="cpu").clone() ) record_seeded_value(name, active_state[name]) causal_mhc_v28_changed = True capability_integration_changed = ( causal_capability_v24_changed or exploration_meta_v25_changed or assurance_v26_changed or knowledge_transfer_v27_changed or causal_mhc_v28_changed ) knowledge_transfer_geometry_changed = False knowledge_transfer_source_dim = 0 knowledge_transfer_target_dim = 0 knowledge_transfer_added_dim = 0 knowledge_transfer_prefix_exact = True source_transfer_down = active_state.get( _KNOWLEDGE_TRANSFER_DOWN_WEIGHT_NAME ) source_transfer_up = active_state.get( _KNOWLEDGE_TRANSFER_UP_WEIGHT_NAME ) target_transfer_down = target_state.get( _KNOWLEDGE_TRANSFER_DOWN_WEIGHT_NAME ) target_transfer_up = target_state.get( _KNOWLEDGE_TRANSFER_UP_WEIGHT_NAME ) if not all( isinstance(value, torch.Tensor) for value in ( source_transfer_down, source_transfer_up, target_transfer_down, target_transfer_up, ) ): raise RuntimeError( "knowledge-transfer projection state is incomplete" ) assert isinstance(source_transfer_down, torch.Tensor) assert isinstance(source_transfer_up, torch.Tensor) assert isinstance(target_transfer_down, torch.Tensor) assert isinstance(target_transfer_up, torch.Tensor) knowledge_transfer_target_dim = target_transfer_down.shape[0] if knowledge_transfer_v27_changed: knowledge_transfer_added_dim = knowledge_transfer_target_dim else: knowledge_transfer_source_dim = source_transfer_down.shape[0] transfer_geometry_exact = ( source_transfer_down.shape == target_transfer_down.shape and source_transfer_up.shape == target_transfer_up.shape ) if not transfer_geometry_exact: source_dim = source_transfer_down.shape[0] target_dim = target_transfer_down.shape[0] hidden_size = target_transfer_down.shape[1] source_lineage_dim = ( checkpoint_lineage.get("knowledgeTransferDim") if isinstance(checkpoint_lineage, dict) else None ) target_lineage_dim = target_lineage.get("knowledgeTransferDim") expandable_transfer_geometry = ( source_transfer_down.ndim == 2 and source_transfer_up.ndim == 2 and target_transfer_down.ndim == 2 and target_transfer_up.ndim == 2 and source_transfer_up.shape == (hidden_size, source_dim) and target_transfer_up.shape == (hidden_size, target_dim) and source_transfer_down.shape[1] == hidden_size and 0 < source_dim < target_dim and checkpoint_lineage_schema in { "nnf.resynthesis.composed_additive_lineage.v28", COMPOSED_ADDITIVE_LINEAGE_SCHEMA, } and source_lineage_dim == source_dim and target_lineage_dim == target_dim ) if not expandable_transfer_geometry: raise RuntimeError( "knowledge-transfer projection cannot preserve inherited " "geometry" ) validate_growth_lineage() # The stable state keys remain unchanged. Deterministically seed # only the successor's appended rank, then overwrite the complete # inherited row/column prefix byte-for-byte. No accepted element # is reinitialized and no smaller successor can pass this branch. expanded_down = _deterministic_xavier_tensor( target_transfer_down.detach().to(device="cpu"), tuple(target_transfer_down.shape), ( f"{_KNOWLEDGE_TRANSFER_DOWN_WEIGHT_NAME}:" f"{source_dim}->{target_dim}" ), ) expanded_up = _deterministic_xavier_tensor( target_transfer_up.detach().to(device="cpu"), tuple(target_transfer_up.shape), ( f"{_KNOWLEDGE_TRANSFER_UP_WEIGHT_NAME}:" f"{source_dim}->{target_dim}" ), ) expanded_down[:source_dim].copy_( source_transfer_down.detach().to(device="cpu") ) expanded_up[:, :source_dim].copy_( source_transfer_up.detach().to(device="cpu") ) knowledge_transfer_prefix_exact = bool( torch.equal( expanded_down[:source_dim], source_transfer_down.detach().to(device="cpu"), ) and torch.equal( expanded_up[:, :source_dim], source_transfer_up.detach().to(device="cpu"), ) ) if not knowledge_transfer_prefix_exact: raise RuntimeError( "knowledge-transfer migration changed inherited weights" ) active_state[_KNOWLEDGE_TRANSFER_DOWN_WEIGHT_NAME] = expanded_down active_state[_KNOWLEDGE_TRANSFER_UP_WEIGHT_NAME] = expanded_up record_seeded_value( ( f"{_KNOWLEDGE_TRANSFER_DOWN_WEIGHT_NAME}" f"[{source_dim}:{target_dim}]" ), expanded_down[source_dim:target_dim], ) record_seeded_value( ( f"{_KNOWLEDGE_TRANSFER_UP_WEIGHT_NAME}" f"[:,{source_dim}:{target_dim}]" ), expanded_up[:, source_dim:target_dim], ) knowledge_transfer_geometry_changed = True knowledge_transfer_source_dim = source_dim knowledge_transfer_target_dim = target_dim knowledge_transfer_added_dim = target_dim - source_dim logit_residual_geometry_changed = False logit_residual_source_rank = 0 logit_residual_target_rank = 0 logit_residual_added_rank = 0 logit_residual_prefix_exact = True source_logit_residual_down = active_state.get( _LOGIT_RESIDUAL_DOWN_WEIGHT_NAME ) source_logit_residual_up = active_state.get( _LOGIT_RESIDUAL_UP_WEIGHT_NAME ) target_logit_residual_down = target_state.get( _LOGIT_RESIDUAL_DOWN_WEIGHT_NAME ) target_logit_residual_up = target_state.get( _LOGIT_RESIDUAL_UP_WEIGHT_NAME ) if not all( isinstance(value, torch.Tensor) for value in ( source_logit_residual_down, source_logit_residual_up, target_logit_residual_down, target_logit_residual_up, ) ): raise RuntimeError("additive logit-residual projection state is incomplete") assert isinstance(source_logit_residual_down, torch.Tensor) assert isinstance(source_logit_residual_up, torch.Tensor) assert isinstance(target_logit_residual_down, torch.Tensor) assert isinstance(target_logit_residual_up, torch.Tensor) logit_residual_source_rank = source_logit_residual_down.shape[0] logit_residual_target_rank = target_logit_residual_down.shape[0] logit_residual_geometry_exact = ( source_logit_residual_down.shape == target_logit_residual_down.shape and source_logit_residual_up.shape == target_logit_residual_up.shape ) if not logit_residual_geometry_exact: source_rank = source_logit_residual_down.shape[0] target_rank = target_logit_residual_down.shape[0] hidden_size = target_logit_residual_down.shape[1] source_lineage_rank = ( checkpoint_lineage.get("logitResidualRank") if isinstance(checkpoint_lineage, dict) else None ) target_lineage_rank = target_lineage.get("logitResidualRank") source_rank_lineage_valid = bool( ( checkpoint_lineage_schema in { "nnf.resynthesis.composed_additive_lineage.v28", COMPOSED_ADDITIVE_LINEAGE_SCHEMA, } and source_lineage_rank == source_rank ) or ( checkpoint_lineage_schema not in { "nnf.resynthesis.composed_additive_lineage.v28", COMPOSED_ADDITIVE_LINEAGE_SCHEMA, } and source_lineage_rank is None ) ) expandable_logit_residual_geometry = ( source_logit_residual_down.ndim == 2 and source_logit_residual_up.ndim == 2 and target_logit_residual_down.ndim == 2 and target_logit_residual_up.ndim == 2 and source_logit_residual_up.shape == (hidden_size, source_rank) and target_logit_residual_up.shape == (hidden_size, target_rank) and source_logit_residual_down.shape[1] == hidden_size and 0 < source_rank < target_rank and source_rank_lineage_valid and target_lineage_rank == target_rank ) if not expandable_logit_residual_geometry: raise RuntimeError( "additive logit-residual projection cannot preserve " "inherited geometry" ) validate_growth_lineage() # Stable state keys make residual-rank growth composable with # transfer/layer growth. Deterministically initialize only the # appended rank, then overwrite the inherited down rows and up # columns byte-for-byte. No configured rank is treated as a cap. expanded_logit_residual_down = _deterministic_xavier_tensor( target_logit_residual_down.detach().to(device="cpu"), tuple(target_logit_residual_down.shape), ( f"{_LOGIT_RESIDUAL_DOWN_WEIGHT_NAME}:" f"{source_rank}->{target_rank}" ), ) expanded_logit_residual_up = _deterministic_xavier_tensor( target_logit_residual_up.detach().to(device="cpu"), tuple(target_logit_residual_up.shape), ( f"{_LOGIT_RESIDUAL_UP_WEIGHT_NAME}:" f"{source_rank}->{target_rank}" ), ) expanded_logit_residual_down[:source_rank].copy_( source_logit_residual_down.detach().to(device="cpu") ) expanded_logit_residual_up[:, :source_rank].copy_( source_logit_residual_up.detach().to(device="cpu") ) inherited_logit_residual_down = ( source_logit_residual_down.detach() .to(device="cpu", dtype=expanded_logit_residual_down.dtype) ) inherited_logit_residual_up = ( source_logit_residual_up.detach() .to(device="cpu", dtype=expanded_logit_residual_up.dtype) ) if ( not torch.equal( inherited_logit_residual_down.to( dtype=source_logit_residual_down.dtype ), source_logit_residual_down.detach().to(device="cpu"), ) or not torch.equal( inherited_logit_residual_up.to( dtype=source_logit_residual_up.dtype ), source_logit_residual_up.detach().to(device="cpu"), ) ): raise RuntimeError( "additive logit-residual migration precision is lossy" ) logit_residual_prefix_exact = bool( torch.equal( expanded_logit_residual_down[:source_rank], inherited_logit_residual_down, ) and torch.equal( expanded_logit_residual_up[:, :source_rank], inherited_logit_residual_up, ) ) if not logit_residual_prefix_exact: raise RuntimeError( "additive logit-residual migration changed inherited weights" ) active_state[_LOGIT_RESIDUAL_DOWN_WEIGHT_NAME] = ( expanded_logit_residual_down ) active_state[_LOGIT_RESIDUAL_UP_WEIGHT_NAME] = ( expanded_logit_residual_up ) record_seeded_value( ( f"{_LOGIT_RESIDUAL_DOWN_WEIGHT_NAME}" f"[{source_rank}:{target_rank}]" ), expanded_logit_residual_down[source_rank:target_rank], ) record_seeded_value( ( f"{_LOGIT_RESIDUAL_UP_WEIGHT_NAME}" f"[:,{source_rank}:{target_rank}]" ), expanded_logit_residual_up[:, source_rank:target_rank], ) logit_residual_geometry_changed = True logit_residual_added_rank = target_rank - source_rank layer_growth_changed = False layer_transfer_sources: tuple[tuple[int, int], ...] = () source_layer_count = self.science_stack.num_layers if predecessor_growth and isinstance(checkpoint_lineage, dict): raw_source_layers = checkpoint_lineage.get("scienceLayers") if ( type(raw_source_layers) is int and 1 <= raw_source_layers <= self.science_stack.num_layers ): source_layer_count = raw_source_layers validate_growth_lineage() ( active_state, layer_growth_changed, layer_seeded_values, layer_transfer_sources, ) = _adapt_reasoning_layer_growth_state( active_state, target_state, source_layers=source_layer_count, target_layers=self.science_stack.num_layers, ) for label, value in layer_seeded_values: record_seeded_value(label, value) completion_successor_state_names = { "completion_successor_authority_trained", "completion_successor_retention_passed", "completion_successor_gradient_update_count", } missing_completion_successor_names = completion_successor_state_names - set( active_state ) completion_successor_changed = False if missing_completion_successor_names and predecessor_growth: if missing_completion_successor_names != completion_successor_state_names: raise RuntimeError( "partial completion successor lifecycle state is forbidden" ) validate_growth_lineage() for name in sorted(completion_successor_state_names): active_state[name] = ( target_state[name].detach().to(device="cpu").clone() ) record_seeded_value(name, active_state[name]) completion_successor_changed = True nla_confidence_state_names = { "nla_confidence_scale", "stop_gate.nla_context_scale", } missing_nla_confidence_names = nla_confidence_state_names - set(active_state) nla_confidence_changed = False if missing_nla_confidence_names and predecessor_growth: if missing_nla_confidence_names != nla_confidence_state_names: raise RuntimeError( "partial NLA confidence-conditioning growth state is forbidden" ) validate_growth_lineage() for name in sorted(nla_confidence_state_names): active_state[name] = ( target_state[name].detach().to(device="cpu").clone() ) if torch.count_nonzero(active_state[name]): raise RuntimeError( "NLA confidence-conditioning growth must start at identity" ) record_seeded_value(name, active_state[name]) nla_confidence_changed = True capability_weight_names = sorted( name for name in target_state if name.endswith(".expert_capability_proj.weight") ) capability_scale_names = sorted( name for name in target_state if name.endswith(".capability_match_scale") ) language_ability_scale_names = sorted( name for name in target_state if name.endswith(".language_ability_match_scale") ) capability_projection_mismatch = [ name for weight_name in capability_weight_names for name in ( weight_name, weight_name.removesuffix("weight") + "bias", ) if ( name in active_state and active_state[name].shape != target_state[name].shape ) ] missing_capability_scales = [ name for name in capability_scale_names if name not in active_state ] missing_language_ability_scales = [ name for name in language_ability_scale_names if name not in active_state ] capability_changed = False expanded_capability_tensors = 0 capability_source_rows = 0 capability_target_rows = 0 added_capability_rows = 0 historical_specialist_remap_count = 0 new_capability_rows_zero = True added_capability_scale_names: list[str] = [] if predecessor_growth and ( capability_projection_mismatch or missing_capability_scales or missing_language_ability_scales ): validate_growth_lineage() if missing_capability_scales and len(missing_capability_scales) != len( capability_scale_names ): raise RuntimeError( "partial functional capability routing growth state is forbidden" ) if missing_language_ability_scales and len( missing_language_ability_scales ) != len(language_ability_scale_names): raise RuntimeError( "partial language ability routing growth state is forbidden" ) expansion_rows: set[tuple[int, int]] = set() expansion_count = 0 for weight_name in capability_weight_names: bias_name = weight_name.removesuffix("weight") + "bias" if weight_name not in active_state or bias_name not in active_state: raise RuntimeError( "functional capability projection growth state is incomplete" ) source_weight = active_state[weight_name] source_bias = active_state[bias_name] target_weight = target_state[weight_name] target_bias = target_state[bias_name] exact_geometry = ( source_weight.shape == target_weight.shape and source_bias.shape == target_bias.shape ) expandable_geometry = ( source_weight.ndim == 2 and target_weight.ndim == 2 and source_bias.ndim == 1 and target_bias.ndim == 1 and source_weight.shape[1] == target_weight.shape[1] and source_weight.shape[0] == source_bias.shape[0] and target_weight.shape[0] == target_bias.shape[0] and 0 < source_weight.shape[0] < target_weight.shape[0] ) if exact_geometry: continue if not expandable_geometry: raise RuntimeError( "functional capability projection cannot preserve " f"inherited geometry: {weight_name}" ) source_rows = int(source_weight.shape[0]) target_rows = int(target_weight.shape[0]) expansion_rows.add((source_rows, target_rows)) expanded_weight = torch.zeros_like( target_weight, device="cpu", ) expanded_bias = torch.zeros_like( target_bias, device="cpu", ) specialist_rows = len( NONE_V2_PLUS_SCIENCE_SPECIALIST_DEFINITIONS ) source_specialist_rows = specialist_rows if isinstance(checkpoint_lineage, dict): raw_source_specialist_rows = checkpoint_lineage.get( "scienceSpecialistCapabilityFamilies" ) if ( type(raw_source_specialist_rows) is int and 1 <= raw_source_specialist_rows <= specialist_rows ): source_specialist_rows = raw_source_specialist_rows scientific_rows = ( target_rows - len(NONE_LANGUAGE_EXPERT_FAMILIES) - specialist_rows ) inherited_language_end = ( scientific_rows + NONE_LANGUAGE_EXPERT_INHERITED_PREFIX_COUNT ) historical_v22_rows = ( inherited_language_end + source_specialist_rows ) target_specialist_start = target_rows - specialist_rows target_source_specialist_end = ( target_specialist_start + source_specialist_rows ) remap_historical_specialists = ( source_rows == historical_v22_rows and target_rows == scientific_rows + len(NONE_LANGUAGE_EXPERT_FAMILIES) + specialist_rows ) if remap_historical_specialists: expanded_weight[:inherited_language_end].copy_( source_weight[:inherited_language_end] ) expanded_bias[:inherited_language_end].copy_( source_bias[:inherited_language_end] ) expanded_weight[ target_specialist_start:target_source_specialist_end ].copy_( source_weight[inherited_language_end:historical_v22_rows] ) expanded_bias[ target_specialist_start:target_source_specialist_end ].copy_( source_bias[inherited_language_end:historical_v22_rows] ) inherited_exact = bool( torch.equal( expanded_weight[:inherited_language_end], source_weight[:inherited_language_end], ) and torch.equal( expanded_bias[:inherited_language_end], source_bias[:inherited_language_end], ) and torch.equal( expanded_weight[ target_specialist_start:target_source_specialist_end ], source_weight[ inherited_language_end:historical_v22_rows ], ) and torch.equal( expanded_bias[ target_specialist_start:target_source_specialist_end ], source_bias[ inherited_language_end:historical_v22_rows ], ) ) new_row_start = inherited_language_end new_row_end = target_specialist_start historical_specialist_remap_count += 1 else: expanded_weight[:source_rows].copy_(source_weight) expanded_bias[:source_rows].copy_(source_bias) inherited_exact = bool( torch.equal( expanded_weight[:source_rows], source_weight, ) and torch.equal( expanded_bias[:source_rows], source_bias, ) ) new_row_start = source_rows new_row_end = target_rows if not inherited_exact: raise RuntimeError( "functional capability projection did not preserve " f"the inherited identities: {weight_name}" ) new_capability_rows_zero = bool( new_capability_rows_zero and not torch.count_nonzero( expanded_weight[new_row_start:new_row_end] ) and not torch.count_nonzero( expanded_bias[new_row_start:new_row_end] ) ) if ( remap_historical_specialists and target_source_specialist_end < target_rows ): new_capability_rows_zero = bool( new_capability_rows_zero and not torch.count_nonzero( expanded_weight[target_source_specialist_end:] ) and not torch.count_nonzero( expanded_bias[target_source_specialist_end:] ) ) active_state[weight_name] = expanded_weight active_state[bias_name] = expanded_bias record_seeded_value( f"{weight_name}[{new_row_start}:{new_row_end}]", expanded_weight[new_row_start:new_row_end], ) record_seeded_value( f"{bias_name}[{new_row_start}:{new_row_end}]", expanded_bias[new_row_start:new_row_end], ) if ( remap_historical_specialists and target_source_specialist_end < target_rows ): record_seeded_value( f"{weight_name}[{target_source_specialist_end}:{target_rows}]", expanded_weight[target_source_specialist_end:], ) record_seeded_value( f"{bias_name}[{target_source_specialist_end}:{target_rows}]", expanded_bias[target_source_specialist_end:], ) expansion_count += 1 expanded_capability_tensors += 2 if expansion_count not in (0, len(capability_weight_names)): raise RuntimeError( "partial functional capability projection growth state is forbidden" ) if len(expansion_rows) > 1: raise RuntimeError( "functional capability projection layers disagree on growth" ) if expansion_rows: ( capability_source_rows, capability_target_rows, ) = next(iter(expansion_rows)) added_capability_rows = capability_target_rows - capability_source_rows for name in missing_capability_scales: active_state[name] = ( target_state[name].detach().to(device="cpu").clone() ) record_seeded_value(name, active_state[name]) added_capability_scale_names.append(name) for name in missing_language_ability_scales: active_state[name] = ( target_state[name].detach().to(device="cpu").clone() ) if torch.count_nonzero(active_state[name]): raise RuntimeError( "language ability routing growth must start at identity" ) record_seeded_value(name, active_state[name]) added_capability_scale_names.append(name) capability_changed = bool( expansion_count or missing_capability_scales or missing_language_ability_scales ) molecular_names = { name for name in target_state if name.startswith("science_stack.molecular_science.") } missing_molecular_names = molecular_names - set(active_state) molecular_changed = False molecular_seeded_names: set[str] = set() molecular_growth_mode = "none" if molecular_names and predecessor_growth and missing_molecular_names: if missing_molecular_names == molecular_names: molecular_seeded_names = molecular_names molecular_growth_mode = "full_bank" elif ( missing_molecular_names == MOLECULAR_MODAL_EXTENSION_STATE_NAMES and isinstance(checkpoint_lineage, dict) and checkpoint_lineage.get("schema") == "nnf.resynthesis.composed_additive_lineage.v20" ): molecular_seeded_names = set( MOLECULAR_MODAL_EXTENSION_STATE_NAMES ) molecular_growth_mode = "v21_incremental_extension" else: raise RuntimeError( "partial molecular graph growth state is forbidden: " + ", ".join(sorted(missing_molecular_names)) ) validate_growth_lineage() for name in sorted(molecular_seeded_names): active_state[name] = ( target_state[name].detach().to(device="cpu").clone() ) record_seeded_value(name, active_state[name]) molecular_seeded_names.add(name) molecular_changed = True if ( layer_growth_changed or vocabulary_transfer_changed or vocabulary_transfer_geometry_changed or capability_changed or capability_integration_changed or anti_thompson_outcome_changed or trauma_state_changed or knowledge_transfer_geometry_changed or logit_residual_geometry_changed or molecular_changed or completion_successor_changed or nla_confidence_changed ): source_lineage = ( checkpoint_lineage if isinstance(checkpoint_lineage, dict) else {} ) missing_after_growth = set(target_state) - set(active_state) unexpected_after_growth = set(active_state) - set(target_state) geometry_matches = all( name in active_state and active_state[name].shape == target.shape for name, target in target_state.items() ) initialization_schemes = [] seeded_surfaces = [] if layer_growth_changed: initialization_schemes.append( REASONING_LAYER_GROWTH_INITIALIZATION ) seeded_surfaces.append("science_stack.reasoning_layer_graph") if ( vocabulary_transfer_changed or vocabulary_transfer_geometry_changed ): initialization_schemes.append( VOCABULARY_TRANSFER_INITIALIZATION_SCHEME ) seeded_surfaces.append( "vocabulary_transfer_down_up" ) if capability_changed: initialization_schemes.append( FUNCTIONAL_CAPABILITY_INITIALIZATION_SCHEME ) seeded_surfaces.append( "science_stack.science_layer_*.expert_capability_proj" ) seeded_surfaces.append( "science_stack.science_layer_*.capability_match_scale" ) if missing_language_ability_scales: initialization_schemes.append( LANGUAGE_ABILITY_ROUTING_INITIALIZATION_SCHEME ) seeded_surfaces.append( "science_stack.science_layer_*.language_ability_match_scale" ) if causal_capability_v24_changed: initialization_schemes.append( CAUSAL_CAPABILITY_INTEGRATION_INITIALIZATION_SCHEME ) initialization_schemes.append( EXPLORATION_OUTCOME_INTEGRATION_INITIALIZATION_SCHEME ) seeded_surfaces.append( "science_stack.capability_integration" ) seeded_surfaces.append( "science_stack.capability_integration.exploration_meta." "outcome_buffers" ) if anti_thompson_outcome_changed: initialization_schemes.append( ANTI_THOMPSON_FAIL_COUNTS_INITIALIZATION_SCHEME ) seeded_surfaces.append( "science_stack.quantile_router_anti_thompson_fail_counts" ) if trauma_state_changed: initialization_schemes.append( "constructor_genesis_persistent_quantile_router_trauma_state_v1" ) seeded_surfaces.append( "science_stack.quantile_router_trauma_state" ) if exploration_meta_v25_changed: initialization_schemes.append( EXPLORATION_META_INTEGRATION_INITIALIZATION_SCHEME ) seeded_surfaces.append( "science_stack.capability_integration.exploration_meta." "selection_buffers" ) if assurance_v26_changed: initialization_schemes.append( ASSURANCE_INTEGRATION_INITIALIZATION_SCHEME ) seeded_surfaces.append( "science_stack.capability_integration.assurance" ) if ( knowledge_transfer_v27_changed or knowledge_transfer_geometry_changed ): initialization_schemes.append( KNOWLEDGE_TRANSFER_INITIALIZATION_SCHEME ) seeded_surfaces.append( "science_stack.capability_integration.knowledge_transfer" ) if causal_mhc_v28_changed: initialization_schemes.append( CAUSAL_CONTRASTIVE_MHC_INITIALIZATION_SCHEME ) seeded_surfaces.append( "science_stack.capability_integration.causal_contrastive_mhc" ) if logit_residual_geometry_changed: initialization_schemes.append( LOGIT_RESIDUAL_RANK_GROWTH_INITIALIZATION_SCHEME ) seeded_surfaces.append("logit_residual_down_up") if molecular_changed: if molecular_growth_mode == "full_bank": initialization_schemes.append(MOLECULAR_INITIALIZATION_SCHEME) seeded_surfaces.append("science_stack.molecular_science") else: initialization_schemes.append( MOLECULAR_MODAL_EXTENSION_INITIALIZATION_SCHEME ) seeded_surfaces.extend( ( "science_stack.molecular_science.diffusion.time_in", "science_stack.molecular_science.vibrational", ) ) if completion_successor_changed: initialization_schemes.append( "zero_authority_validation_retention_lifecycle_v1" ) seeded_surfaces.append( "completion_successor_existing_trained_surface_authority" ) if nla_confidence_changed: initialization_schemes.append("zero_identity_vector_gate_v1") seeded_surfaces.append("nla_confidence_scale") seeded_surfaces.append("stop_gate.nla_context_scale") self._inherited_graph_growth_record = { "schema": "nnf.resynthesis.inherited_graph_growth.v1", "sourceLineageSchema": source_lineage.get("schema"), "sourceLineageSha256": hashlib.sha256( json.dumps( source_lineage, sort_keys=True, separators=(",", ":"), ).encode("utf-8") ).hexdigest(), "targetLineageSchema": target_lineage.get("schema"), "initializationScheme": "+".join(initialization_schemes), "initializationSchemes": initialization_schemes, "seededSurface": "+".join(seeded_surfaces), "seededSurfaces": seeded_surfaces, "seededTensorCount": len(seeded_labels), "seededTensorNames": list(seeded_labels), "seededParameterElements": seeded_parameter_elements, "seededKeySetSha256": hashlib.sha256( "\n".join(seeded_labels).encode("utf-8") ).hexdigest(), "seededGeometrySha256": hashlib.sha256( json.dumps( seeded_geometry, separators=(",", ":"), ).encode("utf-8") ).hexdigest(), "seededValueSha256": seeded_value_digest.hexdigest(), "molecularNewTensorCount": ( len(molecular_seeded_names) if molecular_changed else 0 ), "molecularGrowthMode": molecular_growth_mode, "completionSuccessorNewTensorCount": ( len(completion_successor_state_names) if completion_successor_changed else 0 ), "nlaConfidenceNewTensorCount": ( len(nla_confidence_state_names) if nla_confidence_changed else 0 ), "vocabularyTransferNewTensorCount": ( len(vocabulary_transfer_state_names) if vocabulary_transfer_changed else 0 ), "vocabularyTransferProjectionRows": self.vocab_size, "vocabularyTransferLexicalRows": self.lexical_vocab_size, "vocabularyTransferInheritedPrefixRows": ( self.tokenizer_prefix_vocab_size ), "vocabularyTransferSourceLexicalRows": ( vocabulary_transfer_source_lexical_rows ), "vocabularyTransferTargetLexicalRows": ( vocabulary_transfer_target_lexical_rows ), "vocabularyTransferAddedLexicalRows": ( vocabulary_transfer_added_lexical_rows ), "vocabularyTransferProjectionOnlyRows": ( self.projection_only_vocab_rows ), "vocabularyTransferRank": self.vocabulary_transfer_rank, "vocabularyTransferSourceRank": ( vocabulary_transfer_source_rank ), "vocabularyTransferTargetRank": ( vocabulary_transfer_target_rank ), "vocabularyTransferAddedRank": ( vocabulary_transfer_added_rank ), "vocabularyTransferExpandedTensorCount": ( vocabulary_transfer_expanded_tensor_count ), "vocabularyTransferInheritedPrefixesExact": ( vocabulary_transfer_prefix_exact ), "vocabularyTransferShrinkAccepted": False, "vocabularyTransferIdentityAtMigration": bool( not vocabulary_transfer_changed or not torch.count_nonzero( active_state["vocabulary_transfer_up.weight"] ) ), "capabilityIntegrationNewTensorCount": ( len(causal_capability_core_v24_state_names) if causal_capability_v24_changed else 0 ), "explorationOutcomeNewTensorCount": ( len(exploration_outcome_v24_state_names) if causal_capability_v24_changed else 0 ), "explorationMetaNewTensorCount": ( len(exploration_meta_v25_state_names) if exploration_meta_v25_changed else 0 ), "assuranceNewTensorCount": ( len(assurance_v26_state_names) if assurance_v26_changed else 0 ), "knowledgeTransferNewTensorCount": ( len(knowledge_transfer_v27_state_names) if knowledge_transfer_v27_changed else 0 ), "causalContrastiveMHCNewTensorCount": ( len(causal_mhc_v28_state_names) if causal_mhc_v28_changed else 0 ), "antiThompsonRoutingOutcomeNewTensorCount": ( len(missing_anti_thompson_outcome_names) if anti_thompson_outcome_changed else 0 ), "quantileRouterTraumaNewTensorCount": ( len(missing_trauma_state_names) if trauma_state_changed else 0 ), "knowledgeTransferSourceDim": ( knowledge_transfer_source_dim ), "knowledgeTransferTargetDim": ( knowledge_transfer_target_dim ), "knowledgeTransferAddedDim": knowledge_transfer_added_dim, "knowledgeTransferExpandedTensorCount": ( 2 if knowledge_transfer_geometry_changed else 0 ), "knowledgeTransferInheritedPrefixesExact": ( knowledge_transfer_prefix_exact ), "knowledgeTransferShrinkAccepted": False, "logitResidualSourceRank": logit_residual_source_rank, "logitResidualTargetRank": logit_residual_target_rank, "logitResidualAddedRank": logit_residual_added_rank, "logitResidualExpandedTensorCount": ( 2 if logit_residual_geometry_changed else 0 ), "logitResidualInheritedPrefixesExact": ( logit_residual_prefix_exact ), "logitResidualShrinkAccepted": False, "reasoningLayerSourceCount": source_layer_count, "reasoningLayerTargetCount": self.science_stack.num_layers, "addedReasoningLayerCount": ( self.science_stack.num_layers - source_layer_count ), "reasoningLayerTransferSources": [ { "targetLayer": target_layer, "sourceLayer": source_layer, } for target_layer, source_layer in layer_transfer_sources ], "newReasoningLayerExecutionScalesZero": bool( self.science_stack.num_layers == source_layer_count or not torch.count_nonzero( active_state["science_stack.layer_execution_scale"][ source_layer_count: ] ) ), "pagedRuntimeCopiesIntoNewLayers": 0, "pageStorageMovedDuringLayerGrowth": False, "graphEndstateRouting": "family_to_cluster_to_page_to_expert", "expandedCapabilityTensorCount": (expanded_capability_tensors), "addedCapabilityRoutingTensorCount": len(added_capability_scale_names), "capabilitySourceRows": capability_source_rows, "capabilityTargetRows": capability_target_rows, "addedCapabilityRows": added_capability_rows, "historicalSpecialistRemapTensorCount": ( 2 * historical_specialist_remap_count ), "historicalSpecialistRowsRetainedAtCatalogTail": bool( not historical_specialist_remap_count or historical_specialist_remap_count == len(capability_weight_names) ), "newCapabilityRowsZero": new_capability_rows_zero, "languageAbilityRoutingTensorCount": len( language_ability_scale_names ), "languageAbilityRoutingScalesZeroAtMigration": bool( all( not torch.count_nonzero(active_state[name]) for name in missing_language_ability_scales ) ), "inheritedCapabilityPrefixesExact": True, "identityResidualAtInitialization": True, "inheritedTensorCount": ( len(active_state) - ( len(vocabulary_transfer_state_names) if vocabulary_transfer_changed else 0 ) - (len(molecular_seeded_names) if molecular_changed else 0) - len(added_capability_scale_names) - ( len(anti_thompson_outcome_state_names) if anti_thompson_outcome_changed else 0 ) - ( len(missing_trauma_state_names) if trauma_state_changed else 0 ) - ( len(completion_successor_state_names) if completion_successor_changed else 0 ) - (len(nla_confidence_state_names) if nla_confidence_changed else 0) - ( ( len(causal_capability_v24_state_names) if causal_capability_v24_changed else 0 ) + ( len(exploration_meta_v25_state_names) if exploration_meta_v25_changed else 0 ) + ( len(assurance_v26_state_names) if assurance_v26_changed else 0 ) + ( len(knowledge_transfer_v27_state_names) if knowledge_transfer_v27_changed else 0 ) + ( len(causal_mhc_v28_state_names) if causal_mhc_v28_changed else 0 ) ) ), "unexpectedTensorCount": len(unexpected_after_growth), "partialGrowthStateAccepted": False, "strictInheritedGeometryVerified": ( not missing_after_growth and not unexpected_after_growth and geometry_matches ), } return ( active_state, ( attention_changed or native_attention_changed or parent_route_gate_parameterization_changed or layer_growth_changed or vocabulary_transfer_changed or vocabulary_transfer_geometry_changed or telemetry_changed or nonpersistent_changed or capability_changed or capability_integration_changed or anti_thompson_outcome_changed or trauma_state_changed or knowledge_transfer_geometry_changed or logit_residual_geometry_changed or molecular_changed or completion_successor_changed or nla_confidence_changed ), ) def inherited_graph_growth_record_boundary(self) -> dict[str, Any] | None: """Expose cold-start growth provenance at the receipt boundary only.""" if self._inherited_graph_growth_record is None: return None return dict(self._inherited_graph_growth_record) def load_trainable_state_dict( self, state: dict[str, torch.Tensor], *, checkpoint_lineage: object | None = None, ) -> None: """Strictly restore the merged Resynthesis additive graph over its parent.""" additive_parameters = { name: parameter for name, parameter in self.named_parameters() if not name.startswith("base.") } buffers = self._additive_checkpoint_buffers() expected = {**additive_parameters, **buffers} # Growth provenance belongs to the checkpoint load that produced the # active tensors. A branch restore first validates its immutable # predecessor (which may require a versioned identity-residual # migration) and then loads the already-migrated descendant. Retaining # the predecessor's record after that exact descendant load makes the # CLI falsely report that the exact branch parent still needs growth. # Reset it for this load, but restore the prior diagnostic if validation # fails so a rejected load cannot mutate the observable model state. prior_graph_growth_record = self._inherited_graph_growth_record self._inherited_graph_growth_record = None try: active_state, _adapted = self.adapt_trainable_state_dict( state, checkpoint_lineage=checkpoint_lineage, ) except Exception: self._inherited_graph_growth_record = prior_graph_growth_record raise if set(active_state) != set(expected): missing = sorted(set(expected) - set(active_state)) unexpected = sorted(set(active_state) - set(expected)) self._inherited_graph_growth_record = prior_graph_growth_record raise RuntimeError( "Resynthesis additive checkpoint key set differs: " f"missing={missing} unexpected={unexpected}" ) with torch.no_grad(): for name, parameter in expected.items(): value = active_state[name] if value.shape != parameter.shape: self._inherited_graph_growth_record = prior_graph_growth_record raise RuntimeError( f"Resynthesis additive tensor geometry differs: {name}" ) for name, parameter in expected.items(): parameter.copy_(active_state[name]) self.additive_checkpoint_loaded.fill_(True) @torch.no_grad() def recalibrate_reconciled_router_biases_boundary( self, packet: AdditiveKnowledgeCalibrationPacket, *, calibration_prompt: AllKnowledgeTargetFreeCalibrationPromptPacket, ) -> CalibratedAdditiveKnowledgePacket: """Seal one direct all-knowledge state through target-free traversal. Dense and paged quantile biases are causal routing state, so independent branch values cannot be added or averaged. Reconciliation deliberately zeros those 36 buffers. A fresh, globally unscoped model loads the complete pending state and lets its own current graph recalibrate every router from one target-free prompt traversal. All other forward-mutated state is then restored byte-for-byte before the calibrated biases are reapplied and sealed into a normal full additive checkpoint. """ parameters, buffers = ( _validated_reconciled_additive_calibration_packet_boundary(packet) ) additive_parameters = { name: parameter for name, parameter in self.named_parameters() if not name.startswith("base.") } if ( self._paged_none_training_branch_scope is not None or self._training_branch_functional_owner_index is not None or self._training_branch_functional_owner_count is not None or self._training_branch_functional_graph_active or bool(self.additive_checkpoint_loaded.detach().cpu()) or self.science_stack.num_layers != 18 or not additive_parameters or any( not parameter.requires_grad for parameter in additive_parameters.values() ) or self._paged_none_store_boundary is None ): raise RuntimeError( "Resynthesis reconciled calibration requires one fresh " "globally unscoped 18-layer model" ) calibration_prompt = ( validated_target_free_calibration_prompt_packet_boundary( calibration_prompt ) ) calibration_device = next(iter(additive_parameters.values())).device calibration_input_ids_t = calibration_prompt.input_ids_t.to( device=calibration_device, non_blocking=True, ) calibration_input_mask_t = calibration_prompt.input_mask_t.to( device=calibration_device, non_blocking=True, ) if ( bool(calibration_input_ids_t.lt(0).any()) or bool( calibration_input_ids_t.ge(self.lexical_vocab_size).any() ) ): raise RuntimeError( "Resynthesis reconciled target-free calibration input differs" ) router_rows: list[ tuple[str, QuantileBalancingRouter, torch.Tensor | None] ] = [] runtime_catalogs: list[torch.Tensor] = [] for layer_id in range(18): layer = self.science_stack._layer(layer_id) runtime = layer.paged_expert_runtime if runtime is None: raise RuntimeError( "Resynthesis reconciled calibration paged runtime is absent" ) catalog_t = ( runtime.router.page_catalog_ids_t.detach() .cpu() .long() .reshape(-1) .clone() ) if ( catalog_t.numel() < 1 or torch.unique(catalog_t).numel() != catalog_t.numel() or runtime.router.layer_id_t.numel() != 1 or int(runtime.router.layer_id_t.detach().cpu().long()) != layer_id ): raise RuntimeError( "Resynthesis reconciled calibration page catalog differs" ) runtime_catalogs.append(catalog_t) router_rows.extend( ( ( ( f"science_stack.science_layer_{layer_id}." "quantile_router.expert_bias_t" ), layer.quantile_router, None, ), ( ( f"science_stack.science_layer_{layer_id}." "paged_expert_runtime.router.quantile_router." "expert_bias_t" ), runtime.router.quantile_router, catalog_t, ), ) ) topology_names = tuple(sorted(name for name, _, _ in router_rows)) model_buffers = dict(self.named_buffers()) if ( len(router_rows) != 36 or topology_names != packet.router_bias_names or any( model_buffers.get(name) is not router.expert_bias_t for name, router, _ in router_rows ) ): raise RuntimeError( "Resynthesis reconciled calibration router topology differs" ) physical_catalog_t = torch.sort(torch.cat(runtime_catalogs)).values if ( torch.unique(physical_catalog_t).numel() != physical_catalog_t.numel() or not torch.equal( physical_catalog_t, packet.physical_page_ids_t.detach().cpu().long(), ) ): raise RuntimeError( "Resynthesis reconciled calibration physical page catalog differs" ) self.load_trainable_state_dict( {**parameters, **buffers}, checkpoint_lineage=packet.payload["lineage"], ) active_lineage = self.checkpoint_lineage() pre_state = self.trainable_state_dict() module_modes = tuple( (module, module.training) for module in self.modules() ) calibrated_biases: dict[str, torch.Tensor] | None = None fabric_phase_count = 0 science_active_positions = 0 try: self.eval() for _, router, _ in router_rows: router.train(True) router.begin_expert_bias_step_boundary() router.begin_route_arm_boundary() router.last_hard_mask = None router.last_utilization_t = None result = self.forward_thinking( calibration_input_ids_t, input_mask=calibration_input_mask_t, ) if ( result.fabric_phase_count.numel() != 1 or not bool(result.fabric_phase_count.detach().gt(0)) or result.science_active_positions.numel() != 1 or not bool(result.science_active_positions.detach().gt(0)) or not torch.isfinite(result.shaped_hidden).all() or not torch.isfinite(result.shaped_logits).all() ): raise RuntimeError( "Resynthesis reconciled target-free traversal is incomplete" ) for _, router, _ in router_rows: hard_mask_t = router.last_hard_mask utilization_t = router.last_utilization_t soft_t = router._last_soft_for_loss if ( not isinstance(hard_mask_t, torch.Tensor) or hard_mask_t.ndim < 1 or hard_mask_t.shape[-1] != router.num_experts or not torch.isfinite(hard_mask_t).all() or not isinstance(utilization_t, torch.Tensor) or utilization_t.shape != (router.num_experts,) or not torch.isfinite(utilization_t).all() or not isinstance(soft_t, torch.Tensor) or soft_t.ndim < 1 or soft_t.shape[-1] != router.num_experts or not torch.isfinite(soft_t).all() ): raise RuntimeError( "Resynthesis reconciled router traversal proof differs" ) for _, router, _ in router_rows: router.commit_expert_bias_step_boundary() calibrated_biases = { name: router.expert_bias_t.detach().cpu().clone() for name, router, _ in router_rows } fabric_phase_count = int( result.fabric_phase_count.detach().cpu().long() ) science_active_positions = int( result.science_active_positions.detach().cpu().long() ) finally: self.load_trainable_state_dict( pre_state, checkpoint_lineage=active_lineage, ) for module, training in module_modes: module.training = training if calibrated_biases is not None: live_buffers = dict(self.named_buffers()) for name in packet.router_bias_names: live_buffers[name].copy_( calibrated_biases[name].to( device=live_buffers[name].device, dtype=live_buffers[name].dtype, ) ) for _, router, _ in router_rows: router.begin_expert_bias_step_boundary() router.begin_route_arm_boundary() if calibrated_biases is None: raise RuntimeError( "Resynthesis reconciled router calibration produced no state" ) post_state = self.trainable_state_dict() if set(post_state) != set(pre_state): raise RuntimeError( "Resynthesis reconciled calibration state key set changed" ) for name, before_t in pre_state.items(): after_t = post_state[name] if ( before_t.shape != after_t.shape or before_t.dtype != after_t.dtype or ( name not in packet.router_bias_names and not torch.equal(before_t, after_t) ) ): raise RuntimeError( "Resynthesis reconciled calibration changed non-router " f"state: {name}" ) for name in packet.router_bias_names: bias_t = post_state[name] centered_tolerance_t = ( torch.finfo(bias_t.dtype).eps * bias_t.detach().abs().amax().clamp_min(1) * 8 ) if ( bias_t.ndim != 1 or bias_t.shape != pre_state[name].shape or not bias_t.is_floating_point() or not torch.isfinite(bias_t).all() or bool(bias_t.float().mean().abs().gt(centered_tolerance_t)) ): raise RuntimeError( "Resynthesis reconciled calibrated router bias differs" ) additive_buffer_names = set(self._additive_checkpoint_buffers()) additive_parameter_names = set(additive_parameters) if ( additive_parameter_names.intersection(additive_buffer_names) or additive_parameter_names.union(additive_buffer_names) != set(post_state) ): raise RuntimeError( "Resynthesis reconciled checkpoint tensor ownership differs" ) final_parameters = { name: post_state[name].clone() for name in sorted(additive_parameter_names) } final_buffers = { name: post_state[name].clone() for name in sorted(additive_buffer_names) } state_key_sha256, state_geometry_sha256 = ( _checkpoint_state_identity_boundary(post_state) ) final_state_value_sha256 = _checkpoint_state_value_sha256_boundary( post_state ) router_bias_value_sha256 = _checkpoint_state_value_sha256_boundary( { name: post_state[name] for name in packet.router_bias_names } ) topology_rows = [ { "name": name, "expertCount": router.num_experts, "pageCatalogSha256": ( None if catalog_t is None else hashlib.sha256( catalog_t.contiguous().numpy().tobytes() ).hexdigest() ), } for name, router, catalog_t in sorted( router_rows, key=lambda row: row[0], ) ] input_ids_sha256 = bytes( calibration_prompt.input_ids_sha256_t.tolist() ).hex() input_mask_sha256 = bytes( calibration_prompt.input_mask_sha256_t.tolist() ).hex() calibration_proof: dict[str, Any] = { "schema": RECONCILED_ADDITIVE_CALIBRATION_PROOF_SCHEMA, "reconciliationProofSha256": packet.proof["proofSha256"], "currentLineageSha256": _canonical_json_sha256_boundary( active_lineage ), "calibrationInputIdsSha256": input_ids_sha256, "calibrationInputMaskSha256": input_mask_sha256, "calibrationInputShape": list(calibration_input_ids_t.shape), "calibrationPromptAuthoritySha256": bytes( calibration_prompt.authority_sha256_t.tolist() ).hex(), "calibrationGlobalCursor": int( calibration_prompt.global_cursor_t[0] ), "calibrationPromptSha256": bytes( calibration_prompt.prompt_sha256_t.tolist() ).hex(), "calibrationWindowSha256": bytes( calibration_prompt.window_sha256_t.tolist() ).hex(), "calibrationComponentSha256": bytes( calibration_prompt.row_authority_sha256_t[0].tolist() ).hex(), "calibrationPayloadWorkId": bytes( calibration_prompt.row_authority_sha256_t[1].tolist() ).hex(), "calibrationSnapshotFileSha256": bytes( calibration_prompt.snapshot_file_sha256_t.tolist() ).hex(), "calibrationCollectionAuthoritySha256": bytes( calibration_prompt.collection_authority_sha256_t.tolist() ).hex(), "calibrationFederationAuthoritySha256": bytes( calibration_prompt.federation_authority_sha256_t.tolist() ).hex(), "calibrationExclusionCollectionFileSha256": bytes( calibration_prompt.exclusion_collection_file_sha256_t.tolist() ).hex(), "calibrationExcludedPriorWorkIdsSha256": bytes( calibration_prompt.excluded_prior_work_ids_sha256_t.tolist() ).hex(), "calibrationPhysicalSourceRangesSha256": bytes( calibration_prompt.physical_source_ranges_sha256_t.tolist() ).hex(), "calibrationTargetsPresent": False, "calibrationTargetEnteredForward": False, "calibrationLossComputed": False, "optimizerStepExecuted": False, "targetFreeCalibrationCompleted": True, "routerBiasCalibrationPending": False, "directCheckpointPublicationAllowed": True, "routerBiasCount": len(packet.router_bias_names), "routerTraversalCount": len(router_rows), "routerBiasNamesSha256": hashlib.sha256( "\n".join(packet.router_bias_names).encode("utf-8") ).hexdigest(), "routerTopologySha256": _canonical_json_sha256_boundary( topology_rows ), "routerBiasValueSha256": router_bias_value_sha256, "stateKeySetSha256": state_key_sha256, "stateGeometrySha256": state_geometry_sha256, "finalStateValueSha256": final_state_value_sha256, "physicalPageIdsSha256": packet.proof["physicalPageIdsSha256"], "physicalPageCount": int(packet.physical_page_ids_t.numel()), "nonRouterStatePreserved": True, "routerBiasesFinite": True, "routerBiasesMeanCentered": True, "fabricPhaseCount": fabric_phase_count, "scienceActivePositions": science_active_positions, "coldReloadRequired": True, } calibration_proof["proofSha256"] = _canonical_json_sha256_boundary( calibration_proof ) payload: dict[str, Any] = { "schema": ADDITIVE_CHECKPOINT_SCHEMA, "lineage": copy.deepcopy(active_lineage), "stateKeySetSha256": state_key_sha256, "stateGeometrySha256": state_geometry_sha256, "stateValueSha256": final_state_value_sha256, "reconciliationProofSha256": packet.proof["proofSha256"], "calibrationProofSha256": calibration_proof["proofSha256"], "parameters": final_parameters, "buffers": final_buffers, } _validated_full_additive_checkpoint_payload_boundary(payload) calibrated_packet = CalibratedAdditiveKnowledgePacket( payload=payload, reconciliation_proof=copy.deepcopy(packet.proof), calibration_proof=calibration_proof, router_bias_names=packet.router_bias_names, physical_page_ids_t=( packet.physical_page_ids_t.detach().cpu().long().clone() ), branch_scope_page_ids_t=( packet.branch_scope_page_ids_t.detach().cpu().long().clone() ), calibration_prompt_authority_sha256_t=( calibration_prompt.authority_sha256_t.detach() .cpu() .clone() ), ) _validated_calibrated_additive_knowledge_packet_boundary( calibrated_packet ) return calibrated_packet def initial_correction_state(self, hidden: torch.Tensor) -> RBOCorrectionState: """Create session-local state seeded from model-owned prompt evidence.""" prior_hidden = _hidden_sequence_context(hidden) return RBOCorrectionState( prior_hidden=prior_hidden, outcome_features=hidden.new_zeros(hidden.shape[0], 8), parent_outcome_features=hidden.new_zeros(hidden.shape[0], 5), acquisition_action_probs=hidden.new_zeros(hidden.shape[0], 4), acquisition_action_index=hidden.new_full( (hidden.shape[0], 1), -1, dtype=torch.long, ), acquisition_authority=hidden.new_zeros( hidden.shape[0], 1, dtype=torch.bool, ), outcome_present=hidden.new_zeros(hidden.shape[0], 1), attempt_index=hidden.new_zeros(hidden.shape[0], 1), traversal_state=self.science_stack.initial_traversal_state(hidden), causal_world_state=None, ) @staticmethod def detach_correction_state_for_training( state: RBOCorrectionState, *, preserve_acquisition_graph: bool = False, ) -> RBOCorrectionState: """Detach recurrent activation history between complete token updates. Prefix tokens and every tensor-owned traversal value remain available to the next emission. Only the already-backpropagated activation graph is released. The small acquisition-policy graph can remain shared across a corrected arm so every emission and its outcome action train that policy before the final backward frees it. """ action_probs = ( state.acquisition_action_probs if preserve_acquisition_graph else state.acquisition_action_probs.detach() ) traversal = state.traversal_state return RBOCorrectionState( prior_hidden=state.prior_hidden.detach(), outcome_features=state.outcome_features.detach(), parent_outcome_features=state.parent_outcome_features.detach(), acquisition_action_probs=action_probs, acquisition_action_index=state.acquisition_action_index.detach(), acquisition_authority=state.acquisition_authority.detach(), outcome_present=state.outcome_present.detach(), attempt_index=state.attempt_index.detach(), traversal_state=ScienceTraversalState( expert_visits=traversal.expert_visits.detach(), expert_selections=traversal.expert_selections.detach(), layer_visits=traversal.layer_visits.detach(), traversal_index=traversal.traversal_index.detach(), ), causal_world_state=( None if state.causal_world_state is None else state.causal_world_state.detached() ), ) def begin_session(self) -> None: """Start one isolated caller session across prompt/tool/correction attempts.""" begin_parent = getattr(self.base, "begin_session", None) if not callable(begin_parent): raise RuntimeError("integrated parent has no caller-session boundary") begin_parent() self.ensure_parent_capability_attachment() self._isolate_legacy_outcome_buffer() reset_legacy = getattr(self.legacy_capability_bank, "reset_session_state", None) if callable(reset_legacy): reset_legacy() with torch.no_grad(): self.fabric.reset_session() active_cache = self._base_forward_cache if active_cache is not None: active_cache.clear() def base_forward_cache_telemetry_boundary(self) -> dict[str, object]: """Serialize frozen-parent prefill cache counters for loop receipts.""" active_cache = self._base_forward_cache if active_cache is None: return { "schema": "nnf.resynthesis.base_forward_cache.v1", "enabled": base_forward_cache_enabled_boundary(), "capacity": base_forward_cache_capacity_boundary(), "entries": 0, "hits": 0, "misses": 0, } return active_cache.telemetry_boundary() def begin_decode_arm(self) -> None: """Reset only causal decode caches for an initial or corrected arm.""" begin_parent_decode = getattr(self.base, "begin_decode", None) if callable(begin_parent_decode): begin_parent_decode() self.science_stack.begin_decode_arm_boundary() for runtime in self._paged_none_runtimes_boundary(): runtime.begin_decode_arm_boundary() def begin_quantile_balancing_step_boundary(self) -> torch.Tensor: """Open one QB histogram transaction for an optimizer-step group.""" return self.science_stack.begin_quantile_balancing_step_boundary() def commit_quantile_balancing_step_boundary(self) -> torch.Tensor: """Commit pooled QB state only after the optimizer step succeeds.""" return self.science_stack.commit_quantile_balancing_step_boundary() def project_trained_expert_weights_to_int4_qat_boundary( self, ) -> torch.Tensor: """Project only optimizer-touched Resynthesis FFN experts for QAT.""" return ( self.science_stack .project_trained_expert_weights_to_int4_qat_boundary() ) def _bilevel_intervention( self, reference: torch.Tensor, ) -> RBOBilevelIntervention: """Compute differentiable pathway biases from persisted outcome evidence.""" observation = self._bilevel_observation.to( device=reference.device, dtype=reference.dtype, ) if observation.shape != (1, 8): raise RuntimeError("bound bilevel observation geometry differs") stall_strength = observation[:, :1].clamp(0.0, 1.0) context = self.correction_context_norm(self.outcome_encoder(observation)) trigger = ( torch.sigmoid(self.correction_trigger_head(context)) * stall_strength ).mean() expert_bias = self.correction_expert_head(context).mean(dim=0) * trigger layer_bias = self.correction_layer_head(context).mean(dim=0) * trigger return RBOBilevelIntervention( observation=observation, active=stall_strength.gt(0).any(), trigger=trigger, expert_bias=expert_bias, layer_bias=layer_bias, ) def bind_bilevel_observation( self, observation: torch.Tensor, ) -> RBOBilevelIntervention: """Bind a persisted, target-free outer observation to this model graph.""" if observation.shape == (8,): observation = observation.reshape(1, 8) if observation.shape != (1, 8): raise ValueError("bilevel observation must have geometry [1, 8]") if not torch.isfinite(observation).all(): raise ValueError("bilevel observation must be finite") with torch.no_grad(): self._bilevel_observation = ( observation.detach() .to( device=self.feedback_head.weight.device, dtype=self.feedback_head.weight.dtype, ) .clone() ) return self._bilevel_intervention(self.feedback_head.weight) def current_bilevel_intervention(self) -> RBOBilevelIntervention: """Expose a tensor proof packet without granting host routing authority.""" return self._bilevel_intervention(self.feedback_head.weight) def _connect_active_parent_fabric(self) -> None: """Bind the additive bridge to the exact parent-owned NoNE module.""" active_none_fabric = getattr(self.base, "active_none_fabric", None) if not callable(active_none_fabric): raise RuntimeError( "integrated parent exposes no active NoNE fabric boundary" ) parent_fabric = active_none_fabric() if not isinstance(parent_fabric, nn.Module): raise RuntimeError("integrated parent returned no NoNE fabric module") self.fabric.connect_parent(parent_fabric) def state_with_execution_outcome( self, state: RBOCorrectionState, outcome_features: torch.Tensor, ) -> RBOCorrectionState: """Bind a real verifier/tool outcome to the next model attempt. The features are observations, never targets or gold answers. Their learned effect is computed by ``outcome_encoder`` inside the next forward. """ if outcome_features.ndim != 2 or outcome_features.shape[-1] != 8: raise ValueError("execution outcome features must have shape [batch, 8]") if outcome_features.shape[0] != state.prior_hidden.shape[0]: raise ValueError( "execution outcome batch geometry differs from correction state" ) self.ensure_parent_capability_attachment() apply_parent_outcome = getattr(self.base, "apply_execution_outcome", None) if not callable(apply_parent_outcome): raise RuntimeError( "integrated parent has no trained execution-outcome boundary" ) parent_packet = apply_parent_outcome(outcome_features) parent_applied = getattr(parent_packet, "applied", None) parent_rbo_delta = getattr(parent_packet, "rbo_state_delta_l2", None) parent_arm_delta = getattr(parent_packet, "arm_state_delta_l1", None) parent_legacy_delta = getattr(parent_packet, "legacy_state_delta_l2", None) parent_route_changed = getattr(parent_packet, "route_state_changed", None) parent_fields = ( parent_applied, parent_rbo_delta, parent_arm_delta, parent_legacy_delta, parent_route_changed, ) if not all(isinstance(value, torch.Tensor) for value in parent_fields): raise RuntimeError( "integrated parent returned no tensor outcome-movement proof" ) assert isinstance(parent_applied, torch.Tensor) assert isinstance(parent_rbo_delta, torch.Tensor) assert isinstance(parent_arm_delta, torch.Tensor) assert isinstance(parent_legacy_delta, torch.Tensor) assert isinstance(parent_route_changed, torch.Tensor) legacy_bank = self.legacy_capability_bank if isinstance(legacy_bank, nn.Module): capture_legacy = getattr(legacy_bank, "capture_session_state", None) apply_legacy = getattr(legacy_bank, "apply_durable_outcome_state", None) if not callable(capture_legacy) or not callable(apply_legacy): raise RuntimeError("legacy capability bank lacks causal outcome state") observed = outcome_features.detach().to( device=state.prior_hidden.device, dtype=torch.float32, ) zero = observed.new_zeros(()) legacy_observation = torch.stack( ( zero, observed[0, 4].clamp(0.0, 1.0), observed[0, 0].clamp(0.0, 1.0), torch.maximum(observed[0, 1], observed[0, 6]).clamp(0.0, 1.0), zero, (observed[0, 1] * observed[0, 4]).clamp(0.0, 1.0), zero, zero, observed[0, 2].clamp(0.0, 1.0), observed[0, 3].clamp(0.0, 1.0), ) ) before_legacy = capture_legacy() self._isolate_legacy_outcome_buffer() apply_legacy(legacy_observation) after_legacy = capture_legacy() before_durable = getattr(before_legacy, "durable_outcome", None) after_durable = getattr(after_legacy, "durable_outcome", None) if not isinstance(before_durable, torch.Tensor) or not isinstance( after_durable, torch.Tensor, ): raise RuntimeError( "legacy capability outcome receipt is not tensor-owned" ) parent_legacy_delta = ( after_durable.float() - before_durable.float() ).norm() parent_route_changed = parent_route_changed | parent_legacy_delta.gt(0) parent_fields = ( parent_applied, parent_rbo_delta, parent_arm_delta, parent_legacy_delta, parent_route_changed, ) parent_outcome_features = torch.stack( tuple( value.reshape(()).to(dtype=state.prior_hidden.dtype) for value in parent_fields ) ).reshape(1, 5) acquisition_policy = self.acquisition_policy( outcome_features.to( device=state.prior_hidden.device, dtype=state.prior_hidden.dtype, ), parent_outcome_features.to( device=state.prior_hidden.device, dtype=state.prior_hidden.dtype, ), ) acquisition_action_probs = acquisition_policy.action_probs acquisition_action_index = acquisition_policy.action_index acquisition_authority = acquisition_policy.authority if acquisition_action_probs.shape != (state.prior_hidden.shape[0], 4): raise RuntimeError("Resynthesis acquisition-policy geometry differs") return RBOCorrectionState( prior_hidden=state.prior_hidden, outcome_features=outcome_features.to( device=state.prior_hidden.device, dtype=state.prior_hidden.dtype, ), parent_outcome_features=parent_outcome_features.to( device=state.prior_hidden.device, dtype=state.prior_hidden.dtype, ), acquisition_action_probs=acquisition_action_probs.to( device=state.prior_hidden.device, dtype=state.prior_hidden.dtype, ), acquisition_action_index=acquisition_action_index.to( device=state.prior_hidden.device, dtype=torch.long, ), acquisition_authority=acquisition_authority.to( device=state.prior_hidden.device, dtype=torch.bool, ), outcome_present=state.outcome_present.new_ones(state.outcome_present.shape), attempt_index=state.attempt_index, traversal_state=state.traversal_state, causal_world_state=state.causal_world_state, ) @staticmethod def acquisition_request(state: RBOCorrectionState) -> RBOAcquisitionRequest: """Return the trained additive-policy action after a persisted failure.""" if state.acquisition_action_probs.shape[-1] != 4: raise ValueError( "acquisition action geometry differs from the trained policy" ) action_index = state.acquisition_action_index.reshape(()) authority = state.acquisition_authority.reshape(()) retriable = action_index.eq(0) | action_index.eq(1) execute = authority & retriable return RBOAcquisitionRequest( execute=execute, action_probs=state.acquisition_action_probs.reshape(-1), action_index=action_index, confidence=state.acquisition_action_probs.max(), authority=authority, ) @staticmethod def correction_arm_decision( result: RBOResult, state: RBOCorrectionState, ) -> RBOCorrectionArmDecision: """Decide recurrent correction without a host-authored attempt cap.""" action_index = state.acquisition_action_index.to(dtype=torch.long) authority = state.acquisition_authority.to(dtype=torch.bool) outcome_present = state.outcome_present.to(dtype=torch.bool) correction_trigger = result.correction.trigger.to( device=action_index.device, dtype=result.shaped_hidden.dtype, ) if ( action_index.shape != authority.shape or action_index.shape != outcome_present.shape or correction_trigger.numel() != action_index.numel() or action_index.numel() < 1 ): raise RuntimeError( "Resynthesis correction-arm decision geometry differs" ) correction_trigger = correction_trigger.reshape(action_index.shape) continuation_action = action_index.ge(0) & action_index.lt(3) continue_arm = ( outcome_present & authority & correction_trigger.gt(0.5) & continuation_action ) terminal_arm = ~continue_arm return RBOCorrectionArmDecision( continue_arm=continue_arm, terminal_arm=terminal_arm, correction_trigger=correction_trigger, action_index=action_index, authority=authority, environment_required=continue_arm & action_index.eq(2), ) def _checkpoint_anti_thompson_fail_counts_boundary( self, ) -> tuple[torch.Tensor, ...]: """Snapshot outcome routing memory consulted by checkpoint replay.""" from resynthesis.anti_systems_bridge import ( TensorAntiThompsonRegistry, ) dense_counts = tuple( ( layer.quantile_router.bind_anti_thompson_registry_boundary( layer._anti_thompson_registry ).fail_counts_t.detach().clone() ) for layer_idx in range(self.science_stack.num_layers) for layer in (self.science_stack._layer(layer_idx),) ) paged_counts: list[torch.Tensor] = [] for layer_idx in range(self.science_stack.num_layers): runtime = self.science_stack._layer( layer_idx ).paged_expert_runtime if runtime is None: continue registry = getattr( runtime.router.quantile_router, "_anti_thompson_registry", getattr(runtime.router, "_anti_thompson_registry", None), ) if not isinstance(registry, TensorAntiThompsonRegistry): # A lightweight diagnostic runtime without this optional # registry cannot consult or mutate outcome routing memory. continue paged_counts.append( runtime.router.quantile_router .bind_anti_thompson_registry_boundary(registry) .fail_counts_t.detach() .clone() ) return dense_counts + tuple(paged_counts) def _restore_checkpoint_anti_thompson_fail_counts_boundary( self, saved: tuple[torch.Tensor, ...], ) -> None: """Restore one exact pre- or post-loss outcome-memory snapshot.""" from resynthesis.anti_systems_bridge import ( TensorAntiThompsonRegistry, ) dense_targets = tuple( ( layer.quantile_router.bind_anti_thompson_registry_boundary( layer._anti_thompson_registry ).fail_counts_t ) for layer_idx in range(self.science_stack.num_layers) for layer in (self.science_stack._layer(layer_idx),) ) paged_targets: list[torch.Tensor] = [] for layer_idx in range(self.science_stack.num_layers): runtime = self.science_stack._layer( layer_idx ).paged_expert_runtime if runtime is None: continue registry = getattr( runtime.router.quantile_router, "_anti_thompson_registry", getattr(runtime.router, "_anti_thompson_registry", None), ) if not isinstance(registry, TensorAntiThompsonRegistry): continue paged_targets.append( runtime.router.quantile_router .bind_anti_thompson_registry_boundary(registry) .fail_counts_t ) targets = dense_targets + tuple(paged_targets) if len(targets) != len(saved): raise RuntimeError( "activation-checkpoint anti-thompson topology differs" ) torch._foreach_copy_(targets, saved) def _checkpoint_forward_mutable_state_boundary( self, ) -> _CheckpointForwardMutableState: """Snapshot every mutable field the checkpointed forward consults. ``torch.utils.checkpoint`` (``use_reentrant=False``) re-executes the wrapped forward during backward. Inside the region the paged NoNE routers flip the Python cohort-refinement latch, initialize the tensor-owned cohort page mask, add route observations to the open optimizer-group quantile histogram, and stage page rows into the candidate cache; the science stack advances its step counter and the dense routers add their own quantile observations. Without a restore, the recomputation observes the first pass's mutations: it both counts the same route twice and takes shorter cache-hit/refinement branches (fewer saved tensors — the checkpoint aborts). Restoring this snapshot at the start of both passes makes the recomputation observe byte-identical model-owned state and leaves exactly one logical histogram contribution for the later optimizer-step commit. """ dense_router_expert_bias_t = tuple( self.science_stack._layer(layer_idx) .quantile_router.expert_bias_t.detach() .clone() for layer_idx in range(self.science_stack.num_layers) ) dense_router_quantile_step_state = tuple( self.science_stack._layer(layer_idx) .quantile_router.expert_bias_step_snapshot_boundary() for layer_idx in range(self.science_stack.num_layers) ) paged_runtime_state: list[_CheckpointPagedRuntimeState] = [] for layer_idx in range(self.science_stack.num_layers): runtime = self.science_stack._layer(layer_idx).paged_expert_runtime if runtime is None: continue router = runtime.router cohort_positions_t = ( runtime._training_route_cohort_catalog_positions_t ) paged_runtime_state.append( _CheckpointPagedRuntimeState( runtime=runtime, router_expert_bias_t=( router.quantile_router.expert_bias_t.detach().clone() ), router_quantile_step_state=( router.quantile_router .expert_bias_step_snapshot_boundary() ), cohort_active_t=( router.training_route_cohort_active_t.detach().clone() ), cohort_page_mask_t=( router.training_route_cohort_page_mask_t.detach().clone() ), cohort_boundary_open=( router._training_route_cohort_boundary_open ), cohort_refinement_ready=( router._training_route_cohort_refinement_ready ), runtime_cohort_open=runtime._training_route_cohort_open, cohort_page_ids=runtime._training_route_cohort_page_ids, cohort_catalog_positions_t=( cohort_positions_t.detach().clone() if cohort_positions_t is not None else None ), candidate_pages=dict(runtime._candidate_pages), candidate_gradient_page_ids=set( runtime._candidate_gradient_page_ids ), gradient_page_forward_count_t=( runtime.gradient_page_forward_count_t.detach().clone() ), candidate_route_count_t=( runtime.candidate_route_count_t.detach().clone() ), candidate_vjp_state=( runtime.candidate_vjp_checkpoint_state_boundary() ), ) ) return _CheckpointForwardMutableState( fabric_parent_route_uses=( self.fabric._parent_route_uses.detach().clone() ), science_step_count=self.science_stack._step_count.detach().clone(), dense_router_expert_bias_t=dense_router_expert_bias_t, dense_router_quantile_step_state=( dense_router_quantile_step_state ), anti_thompson_fail_counts_t=( self._checkpoint_anti_thompson_fail_counts_boundary() ), paged_runtime_state=tuple(paged_runtime_state), ) def _restore_checkpoint_forward_mutable_state_boundary( self, saved: _CheckpointForwardMutableState, *, telemetry_only: bool, ) -> None: """Roll in-flight mutations back to the pre-checkpoint snapshot. Called at the start of both checkpoint passes so the recomputation observes the exact state the original forward observed. It is also called with the original post-forward snapshot after recomputation: non-reentrant checkpointing assigns gradients to the dynamic page leaves created by the original forward, while recomputation creates identity-distinct leaves with no retained gradients. Restoring that post-forward routing state therefore keeps the gradient-bearing candidate objects live for the page-update boundary and replaces duplicate counters with exactly one logical forward. Pure accumulators may be restored alone with ``telemetry_only=True`` when callers intentionally need to preserve the current routing objects. """ # One checkpointed backward enters this boundary twice. The live # federation profile attributed 40/400 samples to the old sequence of # small ``copy_`` launches. Group same-device tensor state into # foreach copies so recomputation retains the exact model-owned values # while paying one launch per state family rather than one launch per # layer/runtime/buffer. telemetry_targets = ( self.fabric._parent_route_uses, self.science_stack._step_count, *( value for runtime_state in saved.paged_runtime_state for value in ( runtime_state.runtime.gradient_page_forward_count_t, runtime_state.runtime.candidate_route_count_t, ) ), ) telemetry_sources = ( saved.fabric_parent_route_uses, saved.science_step_count, *( value for runtime_state in saved.paged_runtime_state for value in ( runtime_state.gradient_page_forward_count_t, runtime_state.candidate_route_count_t, ) ), ) torch._foreach_copy_(telemetry_targets, telemetry_sources) for layer_idx, quantile_step_state in enumerate( saved.dense_router_quantile_step_state ): self.science_stack._layer( layer_idx ).quantile_router.restore_expert_bias_step_snapshot_boundary( quantile_step_state ) for runtime_state in saved.paged_runtime_state: ( runtime_state.runtime.router.quantile_router .restore_expert_bias_step_snapshot_boundary( runtime_state.router_quantile_step_state ) ) if telemetry_only: return expert_bias_targets = ( *( self.science_stack._layer( layer_idx ).quantile_router.expert_bias_t for layer_idx in range(self.science_stack.num_layers) ), *( runtime_state.runtime.router.quantile_router.expert_bias_t for runtime_state in saved.paged_runtime_state ), ) expert_bias_sources = ( *saved.dense_router_expert_bias_t, *( runtime_state.router_expert_bias_t for runtime_state in saved.paged_runtime_state ), ) torch._foreach_copy_(expert_bias_targets, expert_bias_sources) self._restore_checkpoint_anti_thompson_fail_counts_boundary( saved.anti_thompson_fail_counts_t ) cohort_tensor_targets = tuple( value for runtime_state in saved.paged_runtime_state for value in ( runtime_state.runtime.router.training_route_cohort_active_t, runtime_state.runtime.router.training_route_cohort_page_mask_t, ) ) cohort_tensor_sources = tuple( value for runtime_state in saved.paged_runtime_state for value in ( runtime_state.cohort_active_t, runtime_state.cohort_page_mask_t, ) ) if cohort_tensor_targets: torch._foreach_copy_( cohort_tensor_targets, cohort_tensor_sources, ) for runtime_state in saved.paged_runtime_state: runtime = runtime_state.runtime router = runtime.router router._training_route_cohort_boundary_open = ( runtime_state.cohort_boundary_open ) router._training_route_cohort_refinement_ready = ( runtime_state.cohort_refinement_ready ) runtime._training_route_cohort_open = ( runtime_state.runtime_cohort_open ) runtime._training_route_cohort_page_ids = ( runtime_state.cohort_page_ids ) runtime._training_route_cohort_catalog_positions_t = ( runtime_state.cohort_catalog_positions_t ) runtime._candidate_pages.clear() runtime._candidate_pages.update(runtime_state.candidate_pages) runtime._candidate_gradient_page_ids.clear() runtime._candidate_gradient_page_ids.update( runtime_state.candidate_gradient_page_ids ) runtime.restore_candidate_vjp_checkpoint_state_boundary( runtime_state.candidate_vjp_state ) def forward_thinking( self, input_ids: torch.Tensor, session_state: RBOCorrectionState | None = None, *, molecular_input: MolecularInputPacket | None = None, input_mask: torch.Tensor | None = None, _shared_prefix: NativeSharedPrefixTrainingPacket | None = None, _frozen_backbone: FrozenBackbonePrefillPacket | None = None, ) -> RBOResult: """Run frozen feature plumbing and two additive correction shots. The parent Resynthesis decoder supplies immutable feature/context tensors and the vocabulary projection geometry. Additive NoNE Fabric, RBO, science experts, and pages own answer logits and stop decisions. Parent logits are captured only for detached diagnostics and never blended into the answer or used as a loss/retention reference. Shot two consumes only model-produced shot-one state plus an optional real execution-outcome packet from the caller session. Targets never enter this method. """ if session_state is None: self.begin_session() # Fast-release / paged sparse training freezes the parent. Run it under # no_grad so the decoder does not retain a full activation tape that # cannot fit beside routed page experts on one GPU. parent_trainable = any( parameter.requires_grad for parameter in self.base.parameters() ) if _shared_prefix is not None and ( not self.training or session_state is not None or input_mask is None or parent_trainable ): raise RuntimeError( "shared parent-prefix reuse requires pristine frozen-parent training" ) if _frozen_backbone is not None and ( _shared_prefix is not None or not self.training or session_state is not None or input_mask is None or parent_trainable ): raise RuntimeError( "frozen-backbone replay requires pristine frozen-parent training" ) # Frozen-parent prefill cache. The frozen base produces byte-identical # output for the same (input_ids, input_mask) across all steps, and the # additive science stack downstream consumes only its immutable feature # tensors. ``forward_logits`` later acts solely as a stateless vocabulary # projection over additive hidden state. Parent answer/stop state is # never consulted, so a content-digest hit can skip the frozen 4M tiled # forward without changing training semantics. The cache # is gated to batched prefill (input_mask present guarantees a fresh # prefill: forward_hidden_logits rejects masks on a KV continuation) and # a frozen parent; decode/generation paths (no mask) are never cached. base_cache_key: str | None = None base_cache_sha16 = "" # A fresh training wave has no continuation call that could consume the # entry: ``begin_session`` cleared the prior session immediately above, # and the bulk learner advances to different indexed rows after this # forward. Hashing CUDA inputs and copying the frozen-parent outputs to # pinned CPU memory therefore produced a guaranteed miss on every wave # (the live r152 receipts showed 0 hits across 140+ misses). Keep the # cache for evaluation and explicit continuation states, where repeated # prompt staging can actually reuse the byte-identical parent result. use_base_forward_cache = ( not self.training or session_state is not None ) and base_forward_cache_allowed_boundary( input_mask=input_mask, use_past=False, parent_trainable=parent_trainable, ) parent: Any = None if use_base_forward_cache: active_cache = self._base_forward_cache if active_cache is None: active_cache = BaseForwardCache( capacity=base_forward_cache_capacity_boundary(), ) self._base_forward_cache = active_cache base_cache_key, base_cache_sha16 = base_forward_cache_key_boundary( input_ids, input_mask, ) cached_entry = active_cache.lookup(base_cache_key) if cached_entry is not None: cache_device = getattr(self.base, "device", input_ids.device) parent = cached_entry.to_device_boundary(cache_device) if parent is None: parent_forward = self.base.forward_hidden_logits shared_parent_forward = None frozen_backbone_forward = None if _shared_prefix is not None: shared_parent_forward = getattr( self.base, "forward_training_hidden_logits_shared_prefix", None, ) if not callable(shared_parent_forward): raise RuntimeError( "frozen parent has no shared-prefix training boundary" ) elif _frozen_backbone is not None: frozen_backbone_forward = getattr( self.base, "forward_training_from_frozen_backbone_packet", None, ) if not callable(frozen_backbone_forward): raise RuntimeError( "frozen parent has no decoder-feature replay boundary" ) elif self.training and input_mask is not None and not parent_trainable: training_parent_forward = getattr( self.base, "forward_training_hidden_logits", None, ) if callable(training_parent_forward): parent_forward = training_parent_forward with ( contextlib.nullcontext() if parent_trainable else torch.no_grad() ): parent = ( frozen_backbone_forward( input_ids, attention_mask=input_mask, packet=_frozen_backbone, ) if frozen_backbone_forward is not None and _frozen_backbone is not None and input_mask is not None else shared_parent_forward(_shared_prefix) if shared_parent_forward is not None and _shared_prefix is not None else parent_forward( input_ids, attention_mask=input_mask, ) ) if use_base_forward_cache and base_cache_key is not None: store_cache = self._base_forward_cache if store_cache is not None: store_cache.store( base_cache_key, BaseForwardCacheEntry.from_forward_boundary( hidden=parent.hidden, logits=parent.logits, parent_context_hidden=parent.parent_context_hidden, parent_expert_routes=parent.parent_expert_routes, parent_layer_routes=parent.parent_layer_routes, kv_prefix_positions=parent.kv_prefix_positions, kv_new_positions=parent.kv_new_positions, parent_prefill_hidden=parent.parent_prefill_hidden, parent_prefill_input_positions=( parent.parent_prefill_input_positions ), sha16=base_cache_sha16, ), ) self._connect_active_parent_fabric() self.ensure_parent_capability_attachment() baseline_hidden = parent.hidden # Diagnostic only. Keeping the source-era logits in the result is # useful for offline drift inspection, but no active computation below # may read them into answer logits, CE, stopping, or retention. baseline_logits = parent.logits.detach() parent_context_hidden = parent.parent_context_hidden parent_prefill_hidden = parent.parent_prefill_hidden parent_prefill_input_positions = parent.parent_prefill_input_positions if parent_context_hidden.shape != (baseline_hidden.shape[0], self.hidden_size): raise RuntimeError("parent long-context seed geometry differs") if parent_prefill_hidden is None: parent_prefill_hidden = parent_context_hidden.unsqueeze(1) if parent_prefill_input_positions is None: parent_prefill_input_positions = input_ids.new_ones( (), dtype=torch.long ).mul_(input_ids.shape[1]) legacy_bank = self.legacy_capability_bank if isinstance(legacy_bank, nn.Module): begin_legacy = getattr(legacy_bank, "begin_forward", None) if callable(begin_legacy): begin_legacy(self.fabric._session_step + 1) legacy_hidden = legacy_bank(baseline_hidden) if not isinstance(legacy_hidden, torch.Tensor) or ( legacy_hidden.shape != baseline_hidden.shape ): raise RuntimeError( "legacy capability fusion returned invalid hidden state" ) baseline_logits = baseline_logits + ( self.base.forward_logits(legacy_hidden) - self.base.forward_logits(baseline_hidden) ) baseline_hidden = legacy_hidden legacy_context = legacy_bank(parent_context_hidden.unsqueeze(1)) if not isinstance(legacy_context, torch.Tensor) or ( legacy_context.shape != parent_context_hidden.unsqueeze(1).shape ): raise RuntimeError( "legacy capability fusion returned invalid context seed" ) parent_context_hidden = legacy_context.squeeze(1) if parent_prefill_hidden.shape[1] > 0: legacy_prefill = legacy_bank(parent_prefill_hidden) if not isinstance(legacy_prefill, torch.Tensor) or ( legacy_prefill.shape != parent_prefill_hidden.shape ): raise RuntimeError( "legacy capability fusion returned invalid prefill state" ) parent_prefill_hidden = legacy_prefill state = session_state or self.initial_correction_state( parent_context_hidden.unsqueeze(1) ) if os.environ.get( "NNF_RESYNTHESIS_ACTIVATION_CHECKPOINT", "" ).strip().lower() in {"1", "true", "yes"}: from torch.utils.checkpoint import ( checkpoint as _science_stack_activation_checkpoint, set_checkpoint_early_stop as _science_stack_full_recompute, ) fabric_session_step = self.fabric._session_step.detach().clone() checkpoint_mutable_state = ( self._checkpoint_forward_mutable_state_boundary() ) # Dynamic page bundles are not registered ``nn.Module`` # parameters: the page optimizer reads the exact bundle retained # by each runtime after backward. Checkpoint recomputation builds # a second bundle and would otherwise overwrite ``last_weights`` # with the recomputed object whose gradients are not the original # forward's gradient-bearing leaves. Keep the first-pass runtime # pointers and restore them after recomputation so retained-page # proof and optimizer deltas still observe the authoritative graph. first_pass_runtime_last_state: tuple[ tuple[Any, Any, Any, Any], ... ] = () checkpoint_post_forward_state: ( _CheckpointForwardMutableState | None ) = None checkpoint_post_loss_anti_state: ( tuple[torch.Tensor, ...] | None ) = None checkpoint_forward_call_count = 0 def _checkpointed_forward_from_parent( fabric_session_step_t: torch.Tensor, baseline_hidden_t: torch.Tensor, baseline_logits_t: torch.Tensor, state_t: RBOCorrectionState, parent_expert_routes_t: torch.Tensor, parent_layer_routes_t: torch.Tensor, parent_kv_prefix_positions_t: torch.Tensor, parent_kv_new_positions_t: torch.Tensor, parent_context_hidden_t: torch.Tensor | None, parent_prefill_hidden_t: torch.Tensor | None, parent_prefill_input_positions_t: torch.Tensor | None, ) -> RBOResult: nonlocal checkpoint_forward_call_count nonlocal checkpoint_post_forward_state nonlocal checkpoint_post_loss_anti_state nonlocal first_pass_runtime_last_state checkpoint_forward_call_count += 1 saved_step = self.fabric._session_step.detach().clone() self.fabric._session_step.copy_(fabric_session_step_t) if checkpoint_forward_call_count > 1: # The original pass begins synchronously and immediately # after ``checkpoint_mutable_state`` was captured, so # copying that snapshot back into every CUDA router/page # buffer is redundant. Backward recomputation does need # the restore: the original pass has advanced the cohort # latch, quantile biases, Fabric counters, and candidate # staging state. Restricting the device copies to that # recomputation keeps both passes byte-identical without # serializing the original forward on a duplicate restore. # ``compute_loss`` records one outcome after the original # forward and before backward enters this recomputation. # Save that post-loss authority before restoring the # byte-identical pre-forward routing state. checkpoint_post_loss_anti_state = ( self._checkpoint_anti_thompson_fail_counts_boundary() ) self._restore_checkpoint_forward_mutable_state_boundary( checkpoint_mutable_state, telemetry_only=False, ) try: result = self._forward_from_parent( baseline_hidden_t, baseline_logits_t, state_t, parent_expert_routes_t, parent_layer_routes_t, parent_kv_prefix_positions_t, parent_kv_new_positions_t, parent_context_hidden_t, parent_prefill_hidden_t, parent_prefill_input_positions_t, molecular_input, ) if checkpoint_forward_call_count == 1: checkpoint_post_forward_state = ( self._checkpoint_forward_mutable_state_boundary() ) return result finally: if checkpoint_forward_call_count == 1: first_pass_runtime_last_state = tuple( ( runtime_state.runtime, runtime_state.runtime.last_request, runtime_state.runtime.last_weights, runtime_state.runtime.last_bundle, ) for runtime_state in checkpoint_mutable_state.paged_runtime_state ) elif first_pass_runtime_last_state: for runtime, last_request, last_weights, last_bundle in ( first_pass_runtime_last_state ): runtime.last_request = last_request runtime.last_weights = last_weights runtime.last_bundle = last_bundle # The original pass owns both the one logical telemetry # increment and the dynamic candidate objects to which # non-reentrant checkpointing assigns gradients. Backward # recomputation starts from the same pre-forward routing # state but creates identity-distinct page leaves with no # retained gradients. Restore the complete original # post-forward snapshot: its candidate-page dictionary and # gradient-ID set feed the page optimizer, while its # counters replace the duplicate recomputation telemetry. # Restoring only counters left the optimizer pointed at # recomputation objects with ``grad is None``; restoring # pre-forward counters erased every candidate route and # failed sealing with "no retained-change proof". if checkpoint_forward_call_count > 1: post_forward_state = checkpoint_post_forward_state if post_forward_state is None: raise RuntimeError( "activation checkpoint lost post-forward state" ) self._restore_checkpoint_forward_mutable_state_boundary( post_forward_state, telemetry_only=False, ) post_loss_anti_state = checkpoint_post_loss_anti_state if post_loss_anti_state is None: raise RuntimeError( "activation checkpoint lost outcome routing state" ) self._restore_checkpoint_anti_thompson_fail_counts_boundary( post_loss_anti_state ) elif checkpoint_post_forward_state is None: # A failed original forward grants no routing or # telemetry authority. self._restore_checkpoint_forward_mutable_state_boundary( checkpoint_mutable_state, telemetry_only=False, ) self.fabric._session_step.copy_(saved_step) # The science forward owns model-state restoration in its ``finally`` # boundary. Non-reentrant checkpoint early-stop may raise out of the # recomputation as soon as autograd has recovered its last requested # tensor, before the remaining graph and restoration contract have # executed. That produced finite but different eager/checkpoint # gradients across the complete science graph. Record full # recomputation on this checkpoint frame so backward executes the # exact forward graph and its model-owned finalization once. with _science_stack_full_recompute(False): return cast( RBOResult, _science_stack_activation_checkpoint( _checkpointed_forward_from_parent, fabric_session_step, baseline_hidden, baseline_logits, state, parent.parent_expert_routes, parent.parent_layer_routes, parent.kv_prefix_positions, parent.kv_new_positions, parent_context_hidden, parent_prefill_hidden, parent_prefill_input_positions, use_reentrant=False, ), ) return self._forward_from_parent( baseline_hidden, baseline_logits, state, parent.parent_expert_routes, parent.parent_layer_routes, parent.kv_prefix_positions, parent.kv_new_positions, parent_context_hidden, parent_prefill_hidden, parent_prefill_input_positions, molecular_input, ) def _science_attempt( self, hidden: torch.Tensor, traversal_state: ScienceTraversalState, correction_expert_bias: torch.Tensor, correction_layer_bias: torch.Tensor, parent_expert_routes: torch.Tensor, parent_layer_routes: torch.Tensor, action_context: torch.Tensor, molecular_input: MolecularInputPacket | None = None, causal_world_state: CausalWorldState | None = None, ) -> ScienceStackResult: """Execute one Fabric-owned science phase inside the model graph.""" fabric_packet = self.fabric.step_phase( hidden, parent_expert_routes, parent_layer_routes, ) fabric_hidden = self.fabric.apply_packet(fabric_packet, hidden) result: ScienceStackResult = self.science_stack( fabric_hidden, traversal_state, action_context=action_context, expert_bias=fabric_packet.expert_bias + correction_expert_bias, layer_bias=fabric_packet.layer_bias + correction_layer_bias, molecular_input=molecular_input, causal_world_state=causal_world_state, ) return result def forward_thinking_shared_prefix( self, input_ids: torch.Tensor, *, input_mask: torch.Tensor, shared_prefix: NativeSharedPrefixTrainingPacket, ) -> RBOResult: """Run one target-free training wave with exact frozen-prefix reuse.""" return self.forward_thinking( input_ids, input_mask=input_mask, _shared_prefix=shared_prefix, ) def _correction_context( self, first_hidden: torch.Tensor, baseline_hidden: torch.Tensor, state: RBOCorrectionState, ) -> torch.Tensor: current = _hidden_sequence_context(self.feedback_head(first_hidden)) discrepancy = _hidden_sequence_context( self.feedback_head(first_hidden - baseline_hidden) ) prior = self.prior_hidden_proj(state.prior_hidden.to(dtype=first_hidden.dtype)) outcome = self.outcome_encoder( state.outcome_features.to(dtype=first_hidden.dtype) ) outcome = outcome * state.outcome_present.to(dtype=outcome.dtype) parent_outcome = self.parent_outcome_encoder( state.parent_outcome_features.to(dtype=first_hidden.dtype) ) parent_outcome = parent_outcome * state.outcome_present.to( dtype=parent_outcome.dtype ) acquisition = self.acquisition_encoder( state.acquisition_action_probs.to(dtype=first_hidden.dtype) ) acquisition = acquisition * state.acquisition_authority.to( dtype=acquisition.dtype ) context: torch.Tensor = self.correction_context_norm( current + discrepancy + prior + outcome + parent_outcome + acquisition ) return context def _forward_from_parent( self, baseline_hidden: torch.Tensor, baseline_logits: torch.Tensor, state: RBOCorrectionState, parent_expert_routes: torch.Tensor, parent_layer_routes: torch.Tensor, parent_kv_prefix_positions: torch.Tensor, parent_kv_new_positions: torch.Tensor, parent_context_hidden: torch.Tensor | None = None, parent_prefill_hidden: torch.Tensor | None = None, parent_prefill_input_positions: torch.Tensor | None = None, molecular_input: MolecularInputPacket | None = None, ) -> RBOResult: if state.prior_hidden.shape != (baseline_hidden.shape[0], self.hidden_size): raise ValueError( "correction state hidden geometry differs from the active batch" ) if parent_context_hidden is None: parent_context_hidden = _hidden_sequence_context(baseline_hidden) if parent_context_hidden.shape != (baseline_hidden.shape[0], self.hidden_size): raise ValueError( "parent context hidden geometry differs from the active batch" ) bilevel = self._bilevel_intervention(baseline_hidden) action_context = state.acquisition_action_probs.to( device=baseline_hidden.device, dtype=baseline_hidden.dtype, ) * state.outcome_present.to( device=baseline_hidden.device, dtype=baseline_hidden.dtype, ) if action_context.shape != (baseline_hidden.shape[0], 4): raise ValueError( "correction action context differs from the trained acquisition policy" ) fabric_steps_before = self.fabric._session_step.clone() empty_prefill_routes = baseline_hidden.new_empty( (0, baseline_hidden.shape[0]) ) prefill_packet = PrefillParticipationPacket( active=baseline_hidden.new_zeros((), dtype=torch.bool), input_positions=baseline_hidden.new_zeros((), dtype=torch.long), summary_positions=baseline_hidden.new_zeros((), dtype=torch.long), fabric_phase_count=baseline_hidden.new_zeros((), dtype=torch.long), expert_routes=empty_prefill_routes, layer_routes=empty_prefill_routes, ) task_intent_prefix = parent_context_hidden.unsqueeze(1) if parent_prefill_hidden is not None and parent_prefill_hidden.shape[1] > 0: if parent_prefill_hidden.ndim != 3 or ( parent_prefill_hidden.shape[0] != baseline_hidden.shape[0] or parent_prefill_hidden.shape[-1] != self.hidden_size ): raise ValueError("parent prefill summary geometry differs") prefill_steps_before = self.fabric._session_step.clone() prefill_result = self._science_attempt( parent_prefill_hidden, state.traversal_state, bilevel.expert_bias, bilevel.layer_bias, parent_expert_routes, parent_layer_routes, action_context, causal_world_state=state.causal_world_state, ) prefill_context_hidden = _hidden_sequence_context(prefill_result.hidden) prefill_gain = torch.tanh( self.science_stack.long_context_anchor_gain ).to(device=baseline_hidden.device, dtype=baseline_hidden.dtype) prefill_delta = prefill_gain * ( prefill_context_hidden - parent_context_hidden ) parent_context_hidden = parent_context_hidden + prefill_delta state = replace( state, prior_hidden=state.prior_hidden + prefill_delta, traversal_state=prefill_result.traversal_state, causal_world_state=( state.causal_world_state if prefill_result.causal_proof is None else prefill_result.causal_proof.world_state ), ) prefill_phases = self.fabric._session_step - prefill_steps_before prefill_input_positions = ( baseline_hidden.new_ones( (baseline_hidden.shape[0],), dtype=torch.long, ).mul_(parent_prefill_hidden.shape[1]) if parent_prefill_input_positions is None else parent_prefill_input_positions.to( device=baseline_hidden.device, dtype=torch.long, ).reshape(-1) ) if prefill_input_positions.numel() == 1: prefill_input_positions = prefill_input_positions.expand( baseline_hidden.shape[0] ) if prefill_input_positions.shape != ( baseline_hidden.shape[0], ): raise ValueError( "parent prefill input positions must be scalar or [batch]" ) prefill_packet = PrefillParticipationPacket( active=baseline_hidden.new_ones((), dtype=torch.bool), input_positions=prefill_input_positions, summary_positions=baseline_hidden.new_ones( (), dtype=torch.long ).mul_(parent_prefill_hidden.shape[1]), fabric_phase_count=prefill_phases, expert_routes=prefill_result.expert_routes, layer_routes=prefill_result.layer_routes, ) task_intent_prefix = prefill_result.hidden first = self._science_attempt( baseline_hidden, state.traversal_state, bilevel.expert_bias, bilevel.layer_bias, parent_expert_routes, parent_layer_routes, action_context, causal_world_state=state.causal_world_state, ) correction_context = self._correction_context( first.hidden, baseline_hidden, state ) trigger = torch.sigmoid(self.correction_trigger_head(correction_context)) hidden_residual = torch.tanh( self.correction_hidden_up(correction_context) ).unsqueeze(1) # Capability-integration signals produced by the science stack's # ``CausalIntegrationTensor`` during the first attempt above. Read here # (not passed in) so the public ``forward_thinking`` signature is # unchanged; ``None`` falls back to the prior routing behaviour. capability_output = self.science_stack.last_capability_integration correction_expert_bias = ( self.correction_expert_head(correction_context) + bilevel.expert_bias.unsqueeze(0) ) correction_layer_bias = ( self.correction_layer_head(correction_context) + bilevel.layer_bias.unsqueeze(0) ) if capability_output is not None: # Exploration bonus is [batch, num_actions] (the policy/action # space, NOT the expert axis), so it cannot be added to the routing # bias directly. Reduce it to a per-batch exploration-pressure # scalar and use it as a multiplicative gain on the correction # routing magnitude: high causal disagreement over the chosen # actions => a stronger corrective nudge toward alternative experts. # ``[batch, 1]`` broadcasts over the trailing expert/layer axis. exploration_pressure = torch.tanh( capability_output.exploration_bonus.mean(dim=-1, keepdim=True) ) uncertainty_pressure = ( 1.0 - capability_output.calibrated_confidence ).unsqueeze(-1) correction_expert_bias = correction_expert_bias * ( 1.0 + exploration_pressure + 0.25 * uncertainty_pressure ) correction_layer_bias = correction_layer_bias * ( 1.0 + exploration_pressure + 0.25 * uncertainty_pressure ) drafting = self.fabric.draft_lifecycle( first.hidden, state.outcome_features, state.outcome_present, ) draft_proposal = drafting.draft_proposal_delta.unsqueeze(1) verified_revision = drafting.verified_revision_delta.unsqueeze(1) second_input = ( first.hidden + trigger.unsqueeze(-1) * hidden_residual + draft_proposal + verified_revision ) second = self._science_attempt( second_input, first.traversal_state, correction_expert_bias, correction_layer_bias, parent_expert_routes, parent_layer_routes, action_context, molecular_input, causal_world_state=( state.causal_world_state if first.causal_proof is None else first.causal_proof.world_state ), ) second_context = self.correction_context_norm( correction_context + _hidden_sequence_context(self.feedback_head(second.hidden)) ) trajectory = torch.stack( ( _hidden_sequence_context(first.hidden), _hidden_sequence_context(second.hidden), ), dim=1, ) task_intent_sequence = torch.cat( (task_intent_prefix, first.hidden, second.hidden), dim=1, ) task_intent_logits = self.science_stack.task_intent_logits( task_intent_sequence ) task_intent_probabilities = torch.sigmoid(task_intent_logits) task_intent = TaskIntentPacket( logits=task_intent_logits, probabilities=task_intent_probabilities, dominant_index=task_intent_probabilities.argmax( dim=-1, keepdim=True, ), ) intent_completion_context = self.parent_outcome_encoder( task_intent_probabilities.to(dtype=baseline_hidden.dtype) ) action_completion_context = self.acquisition_encoder(action_context) nla_source = torch.cat( ( parent_context_hidden.detach().unsqueeze(1), state.prior_hidden.detach().unsqueeze(1), trajectory.detach(), ), dim=1, ) nla_latent = self.stop_gate.trajectory_proj(nla_source) nla_reconstructed = F.linear( nla_latent, self.stop_gate.trajectory_proj.weight.transpose(0, 1), ) sequence_completion_context = nla_latent.mean(dim=1) completion_context = self.correction_context_norm( intent_completion_context + action_completion_context + sequence_completion_context ) nla_context = completion_context normalized_nla_context = F.layer_norm( nla_context, (self.cfg.feedback_hidden_size,), ) nla_confidence_scale_t = torch.tanh(self.nla_confidence_scale) nla_confidence_scale = nla_confidence_scale_t.to(dtype=nla_context.dtype) second_context = self.correction_context_norm( second_context + nla_confidence_scale.reshape(1, -1) * normalized_nla_context ) task_confidence = torch.sigmoid(self.task_confidence_head(second_context)) delegation_pressure = torch.sigmoid(self.delegation_head(second_context)) submission_proposal = torch.minimum( drafting.submission_proposal, task_confidence, ) verification_gate = ( 1.0 - drafting.experiment_requirement + drafting.experiment_requirement * drafting.experiment_passed ) drafting = replace( drafting, submission_proposal=submission_proposal, submission_readiness=torch.minimum( submission_proposal, verification_gate, ), ) shaped_hidden = first.hidden + trigger.unsqueeze(-1) * ( second.hidden - first.hidden ) nla = NLAActivationPacket( latent=nla_latent, latent_code_ids=nla_latent.detach().abs().argmax(dim=-1), reconstructed=nla_reconstructed, round_trip_mse=F.mse_loss( nla_reconstructed, nla_source, ), sequence_context_positions=baseline_hidden.new_ones( (), dtype=torch.long ).mul_(nla_source.shape[1]), intent_context=intent_completion_context, action_context=action_completion_context, completion_context=completion_context, stop_context_scale=torch.tanh(self.stop_gate.nla_context_scale), confidence_context_scale=nla_confidence_scale_t, ) stop_gate = self.stop_gate(trajectory, nla_context) submission_confidence = torch.minimum( task_confidence, drafting.submission_readiness, ) stop_scores = torch.cat( (stop_gate.utility, stop_gate.contradiction, submission_confidence), dim=-1, ) causal_proof = second.causal_proof if causal_proof is not None: stop_scores = ( self.science_stack.causal_algebra_world_graph.condition_rbo_stop_scores( stop_scores, causal_proof, ) ) # Additional shaping from the capability integration's calibrated # confidence and value estimate (read from the science stack, same # packet captured above for the correction routing). Both are per-batch # scalars ``[batch]`` acting ONLY on the submission-readiness channel # (index 2): low calibrated confidence OR high state value lower the # readiness to stop, keeping traversal going. The factor is a small # fixed nudge (0.1) — additive on top of the causal spine's own # conditioning, never replacing it. ``None`` capability output leaves # stop_scores exactly as the causal proof left them. if capability_output is not None: readiness_channel = 2 calibrated = capability_output.calibrated_confidence.to( dtype=stop_scores.dtype ) # Over-confidence guard: confidence is in [0,1]; the further below # 1 it sits, the more we suppress readiness (keep traversing). confidence_dampening = (1.0 - calibrated).clamp_min(0.0) value_extension = torch.tanh(capability_output.value.to( dtype=stop_scores.dtype )) stop_scores[:, readiness_channel] = stop_scores[ :, readiness_channel ] - 0.1 * ( confidence_dampening + value_extension.clamp_min(0.0) ) stop_probability = _additive_completion_probability(stop_scores) stop_decision = _additive_completion_decision(stop_scores) reason_codes = self._stop_reason_codes_t.to( device=baseline_hidden.device, dtype=torch.long, ) stop_reason = reason_codes.index_select( 0, stop_scores.argmax(dim=-1).reshape(-1), ).reshape(stop_scores.shape[:-1]) residual_alpha = self.science_stack.logit_residual_alpha() residual_hidden = self.logit_residual_up( self.logit_residual_down(shaped_hidden) ) shaped_hidden_augmented = shaped_hidden + residual_alpha * residual_hidden # The immutable parent lm_head is a vocabulary-coordinate projection, # not a teacher or answer owner. Project only the post-NoNE/RBO/Fabric # additive hidden state. The former # ``parent_logits * (1-alpha) + additive_logits * alpha`` expression # made the hollow base roughly 98% of the answer at the source default # and allowed a cold reload to silently restore that dominance. physical_projection_logits = self.base.forward_logits( shaped_hidden_augmented ) projected_logits = self.map_projection_logits_to_lexical_vocabulary( physical_projection_logits ) native_answer_fn = getattr(self.base, "apply_native_answer_surface", None) if not callable(native_answer_fn): raise RuntimeError( "integrated parent has no native bit-tokenizer answer surface" ) native_answer = native_answer_fn( shaped_hidden_augmented, projected_logits, ) mapped_logits = getattr(native_answer, "logits", None) mapped_token_ids = getattr(native_answer, "token_ids", None) native_bit_ids = getattr(native_answer, "bit_ids", None) glyph_packet = getattr(native_answer, "glyph", None) if not all( isinstance(value, torch.Tensor) for value in ( mapped_logits, mapped_token_ids, native_bit_ids, glyph_packet, ) ): raise RuntimeError( "integrated native answer surface returned an invalid tensor packet" ) assert isinstance(mapped_logits, torch.Tensor) assert isinstance(mapped_token_ids, torch.Tensor) assert isinstance(native_bit_ids, torch.Tensor) assert isinstance(glyph_packet, torch.Tensor) # The parent-side VGE adapter may encode the already selected token into # bits/glyphs, but its legacy logits/token fields are diagnostic only. # Recompute the visible selection from the additive vocabulary # projection so a historical or drifted parent answer surface can never # rewrite or veto it. shaped_logits = projected_logits native_token_ids = projected_logits[:, -1:, :].argmax(dim=-1) correction = RBOCorrectionPacket( hidden_residual=hidden_residual, trigger=trigger, task_confidence=task_confidence, delegation_pressure=delegation_pressure, expert_bias=correction_expert_bias, layer_bias=correction_layer_bias, parent_outcome_features=state.parent_outcome_features, acquisition_action_probs=state.acquisition_action_probs, acquisition_action_index=state.acquisition_action_index, acquisition_authority=state.acquisition_authority, ) # Coverage is an emitted-route fact, not a count of unpublished draft # branches. The first science attempt remains in expert_visits and # therefore still supplies causal rotation pressure to the corrected # attempt, while only the final post-correction route advances the # arm-exhaustion coverage surface for this emitted token. emitted_expert_selections = ( state.traversal_state.expert_selections + second.traversal_state.expert_selections - first.traversal_state.expert_selections ) emitted_traversal_state = replace( second.traversal_state, expert_selections=emitted_expert_selections, ) next_state = RBOCorrectionState( prior_hidden=_hidden_sequence_context(shaped_hidden_augmented), outcome_features=state.outcome_features, parent_outcome_features=state.parent_outcome_features, acquisition_action_probs=state.acquisition_action_probs, acquisition_action_index=state.acquisition_action_index, acquisition_authority=state.acquisition_authority, outcome_present=state.outcome_present, attempt_index=state.attempt_index, traversal_state=emitted_traversal_state, causal_world_state=( state.causal_world_state if causal_proof is None else causal_proof.world_state ), ) return RBOResult( shaped_hidden=shaped_hidden_augmented, shaped_logits=shaped_logits, completion_baseline_logits=physical_projection_logits, baseline_hidden=baseline_hidden, baseline_logits=baseline_logits, parent_context_hidden=parent_context_hidden, steps=baseline_hidden.new_ones((), dtype=torch.long).mul_(2), stop_reason=stop_reason, glyph_packet=glyph_packet, native_token_ids=native_token_ids, native_bit_ids=native_bit_ids, expert_routes=torch.stack( (first.expert_routes, second.expert_routes), dim=0 ), layer_routes=torch.stack((first.layer_routes, second.layer_routes), dim=0), parent_expert_routes=parent_expert_routes, parent_layer_routes=parent_layer_routes, parent_kv_prefix_positions=parent_kv_prefix_positions, parent_kv_new_positions=parent_kv_new_positions, science_active_positions=baseline_hidden.new_ones( (), dtype=torch.long ).mul_(baseline_hidden.shape[1]), fabric_phase_count=self.fabric._session_step - fabric_steps_before, parent_fabric_connected=self.fabric.parent_connected.clone(), parent_route_conditioning=self.fabric._last_parent_route_conditioning.clone(), attempt_weights=torch.cat((torch.ones_like(trigger), trigger), dim=-1), stop_scores=stop_scores, stop_probability=stop_probability, stop_decision=stop_decision, decode_stop_authority=stop_decision.new_full( stop_decision.shape, STOP_AUTHORITY_ADDITIVE_TELEMETRY, dtype=torch.long, ), parent_native_stop_probability=stop_probability.new_zeros( stop_probability.shape ), parent_native_stop_decision=stop_decision.new_zeros( stop_decision.shape, dtype=torch.bool, ), generation_trajectory_initialized=stop_decision.new_zeros( stop_decision.shape, dtype=torch.bool, ), generation_native_progress=stop_decision.new_zeros( stop_decision.shape, dtype=torch.bool, ), generation_task_progress=stop_decision.new_zeros( stop_decision.shape, dtype=torch.bool, ), generation_arm_exhausted=stop_decision.new_zeros( stop_decision.shape, dtype=torch.bool, ), generation_delegation_exit=stop_decision.new_zeros( stop_decision.shape, dtype=torch.bool, ), task_intent=task_intent, prefill=prefill_packet, nla=nla, drafting=drafting, bilevel=bilevel, correction=correction, next_state=next_state, causal_proof=causal_proof, capability_output=capability_output, ) def compute_loss( self, result: RBOResult, target_ids: torch.Tensor, ) -> torch.Tensor: """Compute next-token CE loss on the head logits ONLY. Pillar 17: targets enter a loss boundary only — never ``forward_thinking`` / the science stack / Fabric / NoNE routing. This full-sequence API is retained for checkpoint/API compatibility. Production learning uses :meth:`compute_emission_loss` so sequence-wide model-owned pooling can never expose a future teacher-forced token to an earlier prediction. """ logits = result.shaped_logits[:, :-1, :] targets = target_ids[:, 1:] ce_loss = F.cross_entropy( logits.reshape(-1, logits.shape[-1]), targets.reshape(-1).long(), ignore_index=-100, ) valid = targets.ne(-100) token_correct = logits.detach().argmax(dim=-1).eq(targets) & valid valid_count = valid.sum(dim=-1).clamp_min(1) batch_correctness = token_correct.sum(dim=-1).to( dtype=logits.dtype ) / valid_count.to(dtype=logits.dtype) completion_target = torch.ones_like(result.stop_scores) return ce_loss + self._training_auxiliary_loss( result, batch_correctness, completion_target, ) def compute_emission_loss( self, result: RBOResult, next_token_ids: torch.Tensor, should_stop: torch.Tensor, *, cumulative_correctness: torch.Tensor | None = None, task_intent_targets: torch.Tensor | None = None, ) -> torch.Tensor: """Train one exact autoregressive emission without answer peeking. ``result`` must have been produced from the immutable prompt plus only previously emitted/teacher-forced tokens. The current target token is consumed here, after the forward, for final-position CE and the trained completion objective. No model route receives it. When a caller supplies ``cumulative_correctness``, one incorrect autoregressive emission keeps task-confidence, verification, submission, and drafting lifecycle supervision false for every later teacher-forced emission in that arm. Injecting a correct prefix therefore cannot relabel a failed free-running sequence as confident. """ logits = result.shaped_logits[:, -1, :] batch_rows = logits.shape[0] targets = next_token_ids.to(device=logits.device).reshape(-1).long() if targets.shape[0] != batch_rows: if targets.numel() == 1 and batch_rows > 1: targets = targets.expand(batch_rows) else: raise ValueError("next-token target batch geometry differs") ce_loss = F.cross_entropy(logits, targets) local_correctness = logits.detach().argmax(dim=-1).eq(targets) if cumulative_correctness is None: batch_correctness = local_correctness.to(dtype=logits.dtype) else: if ( cumulative_correctness.shape != local_correctness.shape or cumulative_correctness.dtype != torch.bool or cumulative_correctness.device != local_correctness.device ): raise ValueError("cumulative emission correctness geometry differs") cumulative_correctness.logical_and_(local_correctness) batch_correctness = cumulative_correctness.to(dtype=logits.dtype) return ce_loss + self._training_auxiliary_loss( result, batch_correctness, should_stop, task_intent_targets=task_intent_targets, ) def compute_compact_bulk_sequence_knowledge_loss( self, result: RBOResult, answer_token_ids: torch.Tensor, answer_token_mask: torch.Tensor, answer_cell_row_indices: torch.Tensor, answer_cell_position_ids: torch.Tensor, *, task_intent_targets: torch.Tensor | None = None, ) -> torch.Tensor: """Train exact valid answer cells without target-derived forward routing.""" return self.compute_bulk_sequence_knowledge_loss( result, answer_token_ids, answer_token_mask, task_intent_targets=task_intent_targets, _answer_cell_row_indices=answer_cell_row_indices, _answer_cell_position_ids=answer_cell_position_ids, ) def compute_bulk_sequence_knowledge_loss( self, result: RBOResult, answer_token_ids: torch.Tensor, answer_token_mask: torch.Tensor, *, task_intent_targets: torch.Tensor | None = None, _answer_cell_row_indices: torch.Tensor | None = None, _answer_cell_position_ids: torch.Tensor | None = None, ) -> torch.Tensor: """Train every ordered answer position after one target-free traversal. The immutable prompt alone owns the NoNE/Fabric forward and routes. Complete-answer token IDs are consumed only here at the native-head loss boundary. A target-independent positional basis prevents token permutations from collapsing to the same objective. This remains a non-autoregressive knowledge stage; the autoregressive objective owns later sequence-reasoning refinement. """ batch_rows = result.shaped_hidden.shape[0] if ( answer_token_ids.ndim != 2 or answer_token_mask.shape != answer_token_ids.shape or answer_token_mask.dtype != torch.bool or answer_token_ids.shape[0] != batch_rows ): raise ValueError("bulk sequence answer geometry differs") targets = answer_token_ids.to( device=result.shaped_hidden.device, dtype=torch.long, ) mask = answer_token_mask.to( device=result.shaped_hidden.device, dtype=torch.bool, ) torch._assert_async( mask.any(dim=1).all(), "bulk sequence row has no answer tokens", ) torch._assert_async( torch.where( mask, targets, torch.zeros_like(targets), ).ge(0).all(), "bulk sequence answer token is negative", ) torch._assert_async( torch.where( mask, targets, torch.zeros_like(targets), ).lt(self.lexical_vocab_size).all(), "bulk sequence answer token exceeds the lexical vocabulary", ) completion_context_t = result.nla.completion_context completion_hidden_t = result.shaped_hidden[:, -1:, :] if completion_context_t.device != completion_hidden_t.device: raise ValueError("bulk completion tensors occupy different devices") targets = targets.to(device=completion_hidden_t.device) mask = mask.to(device=completion_hidden_t.device) valid_count_t = mask.sum(dim=1).clamp_min(1) row_nll_sum_t = completion_hidden_t.new_zeros( (batch_rows,), dtype=torch.float32, ) row_correct_count_t = targets.new_zeros((batch_rows,)) low_rank_projection_fn = getattr( self.base, "forward_low_rank_logits_projection", None, ) low_rank_head_t: torch.Tensor | None = None completion_baseline_logits_t: torch.Tensor | None = None if callable(low_rank_projection_fn): projected_low_rank_head_t = low_rank_projection_fn( self.correction_hidden_up.weight ) if not isinstance(projected_low_rank_head_t, torch.Tensor): raise TypeError( "bulk completion low-rank projection is not a tensor" ) low_rank_head_t = ( self.map_projection_logits_to_lexical_vocabulary( projected_low_rank_head_t.transpose(0, 1) ).transpose(0, 1) ) completion_baseline_logits_t = ( self.map_projection_logits_to_lexical_vocabulary( result.completion_baseline_logits[:, -1, :] ) ) if ( completion_baseline_logits_t.shape != (batch_rows, low_rank_head_t.shape[0]) or completion_baseline_logits_t.device != completion_hidden_t.device ): raise ValueError( "bulk completion baseline logits geometry differs" ) working_vocab_rows = ( self._bulk_completion_factorized_vocab_tile_rows_boundary( self.lexical_vocab_size ) if low_rank_head_t is not None else self.lexical_vocab_size ) compact_cells_supplied = ( _answer_cell_row_indices is not None or _answer_cell_position_ids is not None ) position_basis_table_t = ( self._bulk_completion_position_basis_table_from_context( completion_context_t, targets.shape[1], ) ) if compact_cells_supplied: if ( _answer_cell_row_indices is None or _answer_cell_position_ids is None or _answer_cell_row_indices.ndim != 1 or _answer_cell_position_ids.shape != _answer_cell_row_indices.shape or _answer_cell_row_indices.numel() < batch_rows or _answer_cell_row_indices.dtype != torch.long or _answer_cell_position_ids.dtype != torch.long or _answer_cell_row_indices.device != completion_hidden_t.device or _answer_cell_position_ids.device != completion_hidden_t.device ): raise ValueError("bulk compact answer-cell geometry differs") torch._assert_async( _answer_cell_row_indices.ge(0).all() & _answer_cell_row_indices.lt(batch_rows).all() & _answer_cell_position_ids.ge(0).all() & _answer_cell_position_ids.lt(targets.shape[1]).all(), "bulk compact answer-cell index is out of range", ) coverage_t = torch.zeros_like(targets, dtype=torch.int32) coverage_t.index_put_( ( _answer_cell_row_indices, _answer_cell_position_ids, ), torch.ones_like( _answer_cell_row_indices, dtype=coverage_t.dtype, ), accumulate=True, ) torch._assert_async( coverage_t.eq(mask.to(dtype=coverage_t.dtype)).all(), "bulk compact answer cells differ from the exact answer mask", ) compact_cell_count = _answer_cell_row_indices.shape[0] factorized_completion = ( low_rank_head_t is not None and completion_baseline_logits_t is not None ) compact_cell_chunk_width = ( compact_cell_count if factorized_completion else self._bulk_completion_row_chunk_width_boundary( batch_rows=compact_cell_count, device=completion_hidden_t.device, working_vocab_rows=working_vocab_rows, ) ) # The factorized custom autograd owner now performs its own # allocator-bounded cell streaming. Keep the complete compact wave # under that one owner so its full-vocabulary low-rank-head gradient # is allocated and zeroed once. The native-head fallback retains # outer checkpoint tiling because it has no streamed custom VJP. for cell_start in range( 0, compact_cell_count, compact_cell_chunk_width, ): cell_end = min( compact_cell_count, cell_start + compact_cell_chunk_width, ) dense_row_index_t = _answer_cell_row_indices[ cell_start:cell_end ] dense_position_ids_t = _answer_cell_position_ids[ cell_start:cell_end ] safe_targets_t = targets[ dense_row_index_t, dense_position_ids_t, ] dense_context_t = completion_context_t.index_select( 0, dense_row_index_t, ) if factorized_completion: assert ( low_rank_head_t is not None and completion_baseline_logits_t is not None ) chunk_statistics_t = ( self._bulk_completion_factorized_cell_loss_statistics( dense_context_t, position_basis_table_t, completion_baseline_logits_t, dense_row_index_t, dense_position_ids_t, safe_targets_t, low_rank_head_t, ) ) else: dense_hidden_t = completion_hidden_t.squeeze(1).index_select( 0, dense_row_index_t, ) chunk_statistics_t = cast( torch.Tensor, checkpoint( self._bulk_completion_cell_loss_statistics, dense_context_t, dense_hidden_t, position_basis_table_t, dense_position_ids_t, safe_targets_t, use_reentrant=False, ), ) chunk_nll_t = chunk_statistics_t[..., 0] chunk_prediction_t = chunk_statistics_t[..., 1].to( dtype=torch.long ) row_nll_sum_t = row_nll_sum_t.index_add( 0, dense_row_index_t, chunk_nll_t, ) row_correct_count_t = row_correct_count_t.index_add( 0, dense_row_index_t, chunk_prediction_t.eq(safe_targets_t).to( dtype=row_correct_count_t.dtype ), ) row_loss = row_nll_sum_t / valid_count_t completion_logit_dtype = completion_hidden_t.dtype batch_correctness = row_correct_count_t.to( dtype=completion_logit_dtype ) / valid_count_t.to( dtype=completion_logit_dtype ) # Bulk knowledge training owns one prompt-only traversal. The # ordered answer cells below are loss-boundary targets, not tokens # that have already entered the autoregressive trajectory. Marking # this prompt state complete trained both stop heads to terminate # before emitting the answer. Only teacher-forced emission # training can supply the positive terminal label; this boundary # truthfully trains the prompt state to continue. completion_target = torch.zeros_like(result.stop_scores) return row_loss.mean() + self._training_auxiliary_loss( result, batch_correctness, completion_target, task_intent_targets=task_intent_targets, ) # Execution tiling only: no batch row or answer position is capped or # omitted. Live CUDA capacity first narrows the simultaneous row set, # then the existing model/capacity boundary narrows answer positions # for that row tile. Targets remain confined to these exact loss # statistics calls and never enter the model traversal. row_chunk_width = self._bulk_completion_row_chunk_width_boundary( batch_rows=batch_rows, device=completion_hidden_t.device, working_vocab_rows=working_vocab_rows, ) for row_start in range(0, batch_rows, row_chunk_width): row_end = min(batch_rows, row_start + row_chunk_width) active_batch_rows = row_end - row_start position_chunk_width = ( self._bulk_completion_position_chunk_width_boundary( batch_rows=active_batch_rows, answer_width=targets.shape[1], device=completion_hidden_t.device, working_vocab_rows=working_vocab_rows, ) ) for position_start in range( 0, targets.shape[1], position_chunk_width, ): position_end = min( targets.shape[1], position_start + position_chunk_width, ) chunk_mask_t = mask[ row_start:row_end, position_start:position_end, ] chunk_targets_t = targets[ row_start:row_end, position_start:position_end, ] chunk_position_count = position_end - position_start all_dense_row_index_t = torch.arange( active_batch_rows, device=chunk_targets_t.device, dtype=torch.long, ).repeat_interleave(chunk_position_count) all_dense_position_ids_t = torch.arange( position_start, position_end, device=chunk_targets_t.device, dtype=torch.long, ).repeat(active_batch_rows) dense_mask_t = chunk_mask_t.reshape(-1) # Keep the loss tile fixed-shape. ``nonzero`` exposes a # data-dependent output length and synchronizes CUDA before # every exact-CE tile. Masked cells use an in-vocabulary target # only inside this post-forward loss boundary; their NLL and # correctness are zeroed before row accumulation. dense_row_index_t = all_dense_row_index_t dense_position_ids_t = all_dense_position_ids_t safe_targets_t = torch.where( dense_mask_t, chunk_targets_t.reshape(-1), torch.zeros_like(chunk_targets_t.reshape(-1)), ) dense_context_t = completion_context_t[ row_start:row_end ].index_select(0, dense_row_index_t) if ( low_rank_head_t is not None and completion_baseline_logits_t is not None ): chunk_statistics_t = ( self._bulk_completion_factorized_cell_loss_statistics( dense_context_t, position_basis_table_t, completion_baseline_logits_t[row_start:row_end], dense_row_index_t, dense_position_ids_t, safe_targets_t, low_rank_head_t, ) ) else: dense_hidden_t = completion_hidden_t[ row_start:row_end ].squeeze(1).index_select(0, dense_row_index_t) chunk_statistics_t = cast( torch.Tensor, checkpoint( self._bulk_completion_cell_loss_statistics, dense_context_t, dense_hidden_t, position_basis_table_t, dense_position_ids_t, safe_targets_t, use_reentrant=False, ), ) chunk_nll_t = chunk_statistics_t[..., 0] chunk_prediction_t = chunk_statistics_t[..., 1].to( dtype=torch.long ) chunk_nll_t = chunk_nll_t * dense_mask_t.to( dtype=chunk_nll_t.dtype ) row_index_t = dense_row_index_t + row_start row_nll_sum_t = row_nll_sum_t.index_add( 0, row_index_t, chunk_nll_t, ) row_correct_count_t = row_correct_count_t.index_add( 0, row_index_t, chunk_prediction_t.eq(safe_targets_t) .logical_and(dense_mask_t) .to(dtype=row_correct_count_t.dtype), ) row_loss = row_nll_sum_t / valid_count_t completion_logit_dtype = completion_hidden_t.dtype batch_correctness = row_correct_count_t.to( dtype=completion_logit_dtype ) / valid_count_t.to( dtype=completion_logit_dtype ) # As in the compact-cell path, targets remain outside the forward. # Therefore this prompt-only state is nonterminal even though every # answer cell contributes exact CE at this loss boundary. completion_target = torch.zeros_like(result.stop_scores) return row_loss.mean() + self._training_auxiliary_loss( result, batch_correctness, completion_target, task_intent_targets=task_intent_targets, ) def _bulk_completion_row_chunk_width_boundary( self, *, batch_rows: int, device: torch.device, working_vocab_rows: int | None = None, ) -> int: """Select the exact-CE rows that can share one position tile.""" if isinstance(batch_rows, bool) or batch_rows < 1: raise ValueError("bulk completion row tile geometry differs") active_vocab_rows = ( self.lexical_vocab_size if working_vocab_rows is None else working_vocab_rows ) if ( isinstance(active_vocab_rows, bool) or active_vocab_rows < 1 or active_vocab_rows > self.lexical_vocab_size ): raise ValueError("bulk completion working vocabulary geometry differs") if device.type != "cuda": return batch_rows allocator_headroom = _cuda_allocator_headroom_boundary(device) fp32_bytes = torch.finfo(torch.float32).bits // 8 exact_ce_working_surfaces = 4 exact_ce_bytes_per_row_position = ( active_vocab_rows * fp32_bytes * exact_ce_working_surfaces ) live_row_capacity = ( ( allocator_headroom.effective_allocator_headroom_bytes - self._bulk_completion_lm_head_fp32_tile_bytes ) // exact_ce_bytes_per_row_position ) if live_row_capacity < 1: raise RuntimeError( "live CUDA capacity cannot hold one exact completion logit tile" ) return min(batch_rows, live_row_capacity) @property def _bulk_completion_lm_head_fp32_tile_bytes(self) -> int: """Reserve the frozen parent's fixed FP32 lm-head weight tile. The frozen native head projects logits in fixed 16384-row vocabulary tiles, casting each tile to FP32 on demand (``16384 * hidden_size * 4`` bytes, 256 MiB at hidden 4096). The checkpointed exact-CE surface allocates that tile once in forward and again inside backward recomputation, where gradient storage is already resident. Chunk sizing must exclude it from admissible headroom or the recompute allocation aborts the committed wave. """ fp32_bytes = torch.finfo(torch.float32).bits // 8 return ( min(16_384, self.model_cfg.vocab_size) * self.model_cfg.hidden_size * fp32_bytes ) def _bulk_completion_position_chunk_width_boundary( self, *, batch_rows: int, answer_width: int, device: torch.device, working_vocab_rows: int | None = None, ) -> int: """Select an exact-CE position tile from model and CUDA capacity. Four live working-vocabulary surfaces conservatively cover baseline logits, the low-rank residual, checkpoint recomputation, and backward storage. The production factorized path supplies its bounded vocabulary tile width; the native-head fallback supplies the full vocabulary. This boundary changes execution geometry only. """ if ( isinstance(batch_rows, bool) or batch_rows < 1 or isinstance(answer_width, bool) or answer_width < 1 ): raise ValueError("bulk completion position tile geometry differs") active_vocab_rows = ( self.lexical_vocab_size if working_vocab_rows is None else working_vocab_rows ) if ( isinstance(active_vocab_rows, bool) or active_vocab_rows < 1 or active_vocab_rows > self.lexical_vocab_size ): raise ValueError("bulk completion working vocabulary geometry differs") exact_ce_working_surfaces = 4 model_projection_rows = max( self.logit_residual_rank, self.model_cfg.hidden_size, ) model_position_capacity = max( 1, model_projection_rows // batch_rows, ) position_chunk_width = min( answer_width, model_position_capacity, ) if device.type != "cuda": return position_chunk_width allocator_headroom = _cuda_allocator_headroom_boundary(device) fp32_bytes = torch.finfo(torch.float32).bits // 8 exact_ce_bytes_per_position = ( batch_rows * active_vocab_rows * fp32_bytes * exact_ce_working_surfaces ) live_position_capacity = ( ( allocator_headroom.effective_allocator_headroom_bytes - self._bulk_completion_lm_head_fp32_tile_bytes ) // exact_ce_bytes_per_position ) if live_position_capacity < 1: raise RuntimeError( "live CUDA capacity cannot hold one exact completion logit tile" ) return min(position_chunk_width, live_position_capacity) def _bulk_completion_position_loss_statistics( self, completion_context_t: torch.Tensor, completion_hidden_t: torch.Tensor, position_ids_t: torch.Tensor, safe_targets_t: torch.Tensor, ) -> torch.Tensor: """Return exact NLL and detached predictions for one position tile.""" logits_t = self._bulk_completion_position_logits_from_tensors( completion_context_t, completion_hidden_t, position_ids_t, ) if safe_targets_t.shape != logits_t.shape[:2]: raise ValueError("bulk completion target tile geometry differs") return self._bulk_completion_logits_loss_statistics( logits_t, safe_targets_t, ) def _bulk_completion_cell_loss_statistics( self, completion_context_t: torch.Tensor, completion_hidden_t: torch.Tensor, position_basis_table_t: torch.Tensor, position_ids_t: torch.Tensor, safe_targets_t: torch.Tensor, ) -> torch.Tensor: """Return dense exact CE statistics for compact valid row-position cells.""" position_context_t = self._bulk_completion_cell_context_from_basis_table( completion_context_t, position_basis_table_t, position_ids_t, ) if ( completion_hidden_t.ndim != 2 or completion_hidden_t.shape[0] != position_context_t.shape[0] or completion_hidden_t.shape[1] != self.model_cfg.hidden_size or safe_targets_t.shape != position_ids_t.shape ): raise ValueError("bulk completion compact cell geometry differs") position_hidden_t = completion_hidden_t + self.correction_hidden_up( position_context_t ) physical_projection_logits_t = self.base.forward_logits( position_hidden_t.unsqueeze(1) ).squeeze(1) logits_t = self.map_projection_logits_to_lexical_vocabulary( physical_projection_logits_t ) return self._bulk_completion_logits_loss_statistics( logits_t, safe_targets_t, ) def _bulk_completion_factorized_cell_loss_statistics( self, completion_context_t: torch.Tensor, position_basis_table_t: torch.Tensor, baseline_logits_rows_t: torch.Tensor, dense_row_index_t: torch.Tensor, position_ids_t: torch.Tensor, safe_targets_t: torch.Tensor, low_rank_head_t: torch.Tensor, ) -> torch.Tensor: """Return exact CE with OOM-safe tile-streamed gradient replay.""" position_context_t = self._bulk_completion_cell_context_from_basis_table( completion_context_t, position_basis_table_t, position_ids_t, ) if ( baseline_logits_rows_t.ndim != 2 or dense_row_index_t.shape != position_ids_t.shape or dense_row_index_t.dtype != torch.long or safe_targets_t.shape != position_ids_t.shape or low_rank_head_t.ndim != 2 or low_rank_head_t.shape[0] != baseline_logits_rows_t.shape[1] or low_rank_head_t.shape[1] != position_context_t.shape[1] ): raise ValueError("bulk completion factorized cell geometry differs") vocab_rows = baseline_logits_rows_t.shape[1] vocab_tile_rows = ( self._bulk_completion_factorized_vocab_tile_rows_boundary(vocab_rows) ) # The custom owner applies the live allocator boundary internally in # forward and recomputes live headroom in backward. Passing the complete # cell tensor here avoids one full low-rank-head gradient allocation per # execution chunk without widening any logits surface. Every answer cell # and vocabulary partition still enters the loss in producer order, and # targets remain numerator-only. return cast( torch.Tensor, _FactorizedCompletionLossStatistics.apply( # type: ignore[no-untyped-call] position_context_t, baseline_logits_rows_t, dense_row_index_t, safe_targets_t, low_rank_head_t, vocab_tile_rows, ), ) @staticmethod def _bulk_completion_factorized_vocab_tile_rows_boundary( vocab_rows: int, ) -> int: """Bound exact-CE replay to the model-native execution tile.""" if isinstance(vocab_rows, bool) or vocab_rows < 1: raise ValueError("bulk completion vocabulary tile geometry differs") # Exact CE composes every vocabulary partition with logaddexp, so its # answer and gradients do not depend on this execution tile. Match # the existing 8,192-token native prefill/online-softmax geometry. # The former 16,384-row tile produced live 8.4 GiB backward surfaces; # this halves each contiguous allocation while the cell-capacity # boundary continues to own concurrency. return min(RESYNTHESIS_NATIVE_PREFILL_TILE_TOKENS, vocab_rows) @staticmethod def _bulk_completion_logits_loss_statistics( logits_t: torch.Tensor, safe_targets_t: torch.Tensor, ) -> torch.Tensor: """Reduce full-vocabulary logits to exact NLL and detached prediction.""" if safe_targets_t.shape != logits_t.shape[:-1]: raise ValueError("bulk completion target cell geometry differs") logits_fp32_t = logits_t.float() token_nll_t = F.cross_entropy( logits_fp32_t.reshape(-1, logits_fp32_t.shape[-1]), safe_targets_t.reshape(-1), reduction="none", ).reshape(safe_targets_t.shape) prediction_t = logits_t.detach().argmax(dim=-1).to( dtype=token_nll_t.dtype ) return torch.stack((token_nll_t, prediction_t), dim=-1) def _bulk_completion_position_logits( self, result: RBOResult, position_ids_t: torch.Tensor, ) -> torch.Tensor: """Project target-independent ordered completion states to native logits.""" completion_context = result.nla.completion_context if ( completion_context.ndim != 2 or completion_context.shape[0] != result.shaped_hidden.shape[0] or completion_context.shape[1] != self.cfg.feedback_hidden_size ): raise ValueError("bulk completion context geometry differs") return self._bulk_completion_position_logits_from_tensors( completion_context, result.shaped_hidden[:, -1:, :], position_ids_t, ) def _bulk_completion_position_logits_from_tensors( self, completion_context_t: torch.Tensor, completion_hidden_t: torch.Tensor, position_ids_t: torch.Tensor, ) -> torch.Tensor: """Project one target-independent completion-position tensor tile.""" if position_ids_t.ndim != 1 or position_ids_t.numel() < 1: raise ValueError("bulk completion position geometry differs") if ( completion_context_t.ndim != 2 or completion_context_t.shape[1] != self.cfg.feedback_hidden_size or completion_hidden_t.ndim != 3 or completion_hidden_t.shape[0] != completion_context_t.shape[0] or completion_hidden_t.shape[1] != 1 or completion_hidden_t.shape[2] != self.model_cfg.hidden_size ): raise ValueError("bulk completion tensor geometry differs") position_context_t = self._bulk_completion_position_context_from_tensors( completion_context_t, position_ids_t, ) position_hidden_t = completion_hidden_t + ( self.correction_hidden_up(position_context_t) ) physical_projection_logits = cast( torch.Tensor, self.base.forward_logits(position_hidden_t), ) return self.map_projection_logits_to_lexical_vocabulary( physical_projection_logits ) def _bulk_completion_position_context_from_tensors( self, completion_context_t: torch.Tensor, position_ids_t: torch.Tensor, ) -> torch.Tensor: """Build target-independent ordered context for a dense position tile.""" feature_index_t = torch.arange( self.cfg.feedback_hidden_size, device=completion_context_t.device, dtype=torch.long, ).reshape(1, -1) frequency_t = torch.exp( -math.log(10_000.0) * (2 * torch.div(feature_index_t, 2, rounding_mode="floor")).to( dtype=completion_context_t.dtype ) / float(self.cfg.feedback_hidden_size) ) angle_t = ( position_ids_t.to( device=completion_context_t.device, dtype=completion_context_t.dtype, ).reshape(-1, 1) * frequency_t ) position_basis_t = torch.where( feature_index_t.remainder(2).eq(0), angle_t.sin(), angle_t.cos(), ) position_context_t = cast( torch.Tensor, self.correction_context_norm( completion_context_t.unsqueeze(1) + position_basis_t.unsqueeze(0) ), ) return position_context_t def _bulk_completion_position_basis_table_from_context( self, completion_context_t: torch.Tensor, answer_width: int, ) -> torch.Tensor: """Build one target-independent positional table for an exact loss.""" if ( completion_context_t.ndim != 2 or completion_context_t.shape[1] != self.cfg.feedback_hidden_size or isinstance(answer_width, bool) or answer_width < 1 ): raise ValueError("bulk completion position basis geometry differs") feature_index_t = torch.arange( self.cfg.feedback_hidden_size, device=completion_context_t.device, dtype=torch.long, ).reshape(1, -1) frequency_t = torch.exp( -math.log(10_000.0) * (2 * torch.div(feature_index_t, 2, rounding_mode="floor")).to( dtype=completion_context_t.dtype ) / float(self.cfg.feedback_hidden_size) ) angle_t = torch.arange( answer_width, device=completion_context_t.device, dtype=completion_context_t.dtype, ).reshape(-1, 1) * frequency_t return torch.where( feature_index_t.remainder(2).eq(0), angle_t.sin(), angle_t.cos(), ) def _bulk_completion_cell_context_from_basis_table( self, completion_context_t: torch.Tensor, position_basis_table_t: torch.Tensor, position_ids_t: torch.Tensor, ) -> torch.Tensor: """Select compact-cell contexts from one loss-owned position table.""" if ( completion_context_t.ndim != 2 or completion_context_t.shape[1] != self.cfg.feedback_hidden_size or position_basis_table_t.ndim != 2 or position_basis_table_t.shape[1] != self.cfg.feedback_hidden_size or position_basis_table_t.device != completion_context_t.device or position_basis_table_t.dtype != completion_context_t.dtype or position_ids_t.ndim != 1 or completion_context_t.shape[0] != position_ids_t.shape[0] or position_ids_t.dtype != torch.long or position_ids_t.device != completion_context_t.device ): raise ValueError("bulk completion compact context geometry differs") torch._assert_async( position_ids_t.ge(0).all() & position_ids_t.lt(position_basis_table_t.shape[0]).all(), "bulk completion compact position is outside the basis table", ) return cast( torch.Tensor, self.correction_context_norm( completion_context_t + position_basis_table_t.index_select(0, position_ids_t) ), ) def _bulk_completion_cell_context_from_tensors( self, completion_context_t: torch.Tensor, position_ids_t: torch.Tensor, ) -> torch.Tensor: """Build target-independent context for compact valid row-position cells.""" if ( completion_context_t.ndim != 2 or completion_context_t.shape[1] != self.cfg.feedback_hidden_size or position_ids_t.ndim != 1 or completion_context_t.shape[0] != position_ids_t.shape[0] ): raise ValueError("bulk completion compact context geometry differs") feature_index_t = torch.arange( self.cfg.feedback_hidden_size, device=completion_context_t.device, dtype=torch.long, ).reshape(1, -1) frequency_t = torch.exp( -math.log(10_000.0) * (2 * torch.div(feature_index_t, 2, rounding_mode="floor")).to( dtype=completion_context_t.dtype ) / float(self.cfg.feedback_hidden_size) ) angle_t = ( position_ids_t.to( device=completion_context_t.device, dtype=completion_context_t.dtype, ).reshape(-1, 1) * frequency_t ) position_basis_t = torch.where( feature_index_t.remainder(2).eq(0), angle_t.sin(), angle_t.cos(), ) return cast( torch.Tensor, self.correction_context_norm( completion_context_t + position_basis_t ), ) def _training_auxiliary_loss( self, result: RBOResult, batch_correctness: torch.Tensor, should_stop: torch.Tensor, *, task_intent_targets: torch.Tensor | None = None, ) -> torch.Tensor: """Preserve every trained auxiliary surface at the loss boundary.""" correction_target = 1.0 - batch_correctness confidence_loss = F.binary_cross_entropy( result.correction.task_confidence.reshape(-1), batch_correctness.to(dtype=result.correction.task_confidence.dtype), ) trigger_loss = F.binary_cross_entropy( result.correction.trigger.reshape(-1), correction_target.to(dtype=result.correction.trigger.dtype), ) delegation_loss = F.binary_cross_entropy( result.correction.delegation_pressure.reshape(-1), correction_target.to(dtype=result.correction.delegation_pressure.dtype), ) lifecycle_correct = batch_correctness.to( dtype=result.drafting.lifecycle_scores.dtype ).reshape(-1, 1) lifecycle_correction = 1.0 - lifecycle_correct lifecycle_required = torch.ones_like(lifecycle_correct) lifecycle_targets = torch.cat( ( lifecycle_required, lifecycle_required, lifecycle_required, lifecycle_correction, lifecycle_required, lifecycle_correction, lifecycle_required, lifecycle_correction, lifecycle_correct, lifecycle_correct, ), dim=-1, ) lifecycle_loss = F.binary_cross_entropy( result.drafting.lifecycle_scores, lifecycle_targets, ) completion_loss = self.compute_stop_loss(result, should_stop) task_intent_loss = result.stop_scores.new_zeros(()) if task_intent_targets is not None: active_targets = task_intent_targets.to( device=result.task_intent.probabilities.device, dtype=result.task_intent.probabilities.dtype, ) if active_targets.shape != result.task_intent.probabilities.shape: raise ValueError("task-intent target geometry differs") torch._assert_async( torch.isfinite(active_targets).all() & active_targets.ge(0).all() & active_targets.le(1).all(), "task-intent targets must be finite probabilities", ) task_intent_loss = F.binary_cross_entropy( result.task_intent.probabilities, active_targets, ) # Auxiliary losses: load balance + KL anchor + intent anchor aux_loss = ( 0.1 * confidence_loss + 0.1 * trigger_loss + 0.05 * delegation_loss + 0.2 * lifecycle_loss + COMPLETION_STOP_TRAINING_WEIGHT * completion_loss + 0.1 * task_intent_loss + self.cfg.nla_round_trip_loss_weight * result.nla.round_trip_mse ) lb = self.science_stack.load_balance_loss() if lb.requires_grad: aux_loss = aux_loss + 0.01 * lb live_kl = getattr(self.science_stack, "last_kl_anchor_loss_live", None) if ( live_kl is not None and isinstance(live_kl, torch.Tensor) and live_kl.requires_grad ): aux_loss = aux_loss + live_kl live_intent = getattr(self.science_stack, "last_intent_anchor_loss_live", None) if ( live_intent is not None and isinstance(live_intent, torch.Tensor) and live_intent.requires_grad ): aux_loss = aux_loss + 0.01 * live_intent from resynthesis.anti_systems_bridge import ( DEFAULT_ANTI_FLOOR, DEFAULT_ANTI_WEIGHT, ccl_contrareactive_combined_loss_t, ) # Capability losses are tensors owned by ``RBOResult`` so activation # checkpointing recomputes the exact returned graph. Reading the # science stack's mutable ``last_*`` side channel here let backward # recomputation replace the original tensor and changed traversal-gate # gradients. This target-free proof loss is active regardless of # answer correctness; the anti-system below retains its own # correctness-derived repulsion without gating or duplicating CCL. capability_output = result.capability_output if capability_output is not None: capability_aux = capability_output.auxiliary_loss if ( isinstance(capability_aux, torch.Tensor) and capability_aux.requires_grad ): aux_loss = aux_loss + 0.01 * capability_aux coupled_loss = ccl_contrareactive_combined_loss_t( cosine_t=batch_correctness.reshape(-1), ccl_loss_t=batch_correctness.new_zeros(()), floor=DEFAULT_ANTI_FLOOR, anti_weight=DEFAULT_ANTI_WEIGHT, ccl_weight=0.0, ) if coupled_loss.requires_grad: aux_loss = aux_loss + coupled_loss if self.science_stack is not None: # ``RBOResult`` retains both science attempts, so its route packet is # [attempt, depth, batch, token, expert]. Outcome bookkeeping owns # the same batch/token/expert surface as one science-stack result; # combine only the two traversal axes instead of discarding either # attempt or misreading depth as the batch dimension. self.science_stack.record_anti_thompson_from_outcome( result.expert_routes.flatten(0, 1), batch_correctness, floor_t=float(DEFAULT_ANTI_FLOOR), ) return aux_loss @staticmethod def compute_acquisition_loss(state: RBOCorrectionState) -> torch.Tensor: """Train evidence action from persisted execution status only. Action labels are derived from observable boundary state: verified completion, missing verifier/tool, infrastructure failure, or a normal failed observation requiring evidence retry. Gold answers and target tokens are neither read nor represented here. """ features = state.outcome_features if features.ndim != 2 or features.shape[-1] != 8: raise ValueError("acquisition-loss outcome geometry differs") passed = features[:, 0].ge(1.0) verifier_missing = features[:, 4].le(0.0) infrastructure_failure = features[:, 6].gt(0.0) target = torch.zeros( features.shape[0], device=features.device, dtype=torch.long ) target = torch.where(verifier_missing, target.new_full(target.shape, 1), target) target = torch.where( infrastructure_failure, target.new_full(target.shape, 2), target, ) target = torch.where(passed, target.new_full(target.shape, 3), target) log_probs = state.acquisition_action_probs.clamp_min(1e-8).log() return F.nll_loss(log_probs, target) def _completion_successor_gradient_evidence( self, module: nn.Module, ) -> torch.Tensor: """Reduce one completion-head gradient family to a scalar tensor proof.""" evidence = self.completion_successor_authority_trained.new_zeros(()) for parameter in module.parameters(): gradient = parameter.grad if gradient is None: continue finite_nonzero = torch.isfinite(gradient).all() & gradient.ne(0).any() evidence = evidence | finite_nonzero.to( device=evidence.device, dtype=torch.bool, ) return evidence def _completion_successor_parameter_gradient_evidence( self, parameter: nn.Parameter, ) -> torch.Tensor: """Reduce one context-scale gradient to a scalar tensor proof.""" evidence = self.completion_successor_authority_trained.new_zeros(()) gradient = parameter.grad if gradient is None: return evidence return ( torch.isfinite(gradient).all() & gradient.ne(0).any() ).to(device=evidence.device, dtype=torch.bool) def _activate_completion_successor_candidate_from_boundary( self, ) -> torch.Tensor: """Open candidate completion authority only after all score heads learn.""" utility_updated = self._completion_successor_gradient_evidence( self.stop_gate.stop_utility_gate ) contradiction_updated = self._completion_successor_gradient_evidence( self.stop_gate.stop_contradiction_gate ) confidence_updated = self._completion_successor_gradient_evidence( self.task_confidence_head ) trajectory_updated = self._completion_successor_gradient_evidence( self.stop_gate.trajectory_proj ) intent_updated = self._completion_successor_gradient_evidence( self.parent_outcome_encoder ) action_updated = self._completion_successor_gradient_evidence( self.acquisition_encoder ) stop_context_updated = self._completion_successor_parameter_gradient_evidence( self.stop_gate.nla_context_scale ) confidence_context_updated = ( self._completion_successor_parameter_gradient_evidence( self.nla_confidence_scale ) ) gradient_evidence = ( utility_updated & contradiction_updated & confidence_updated & trajectory_updated & intent_updated & action_updated & stop_context_updated & confidence_context_updated ) with torch.no_grad(): self.completion_successor_authority_trained.logical_or_(gradient_evidence) self.completion_successor_retention_passed.copy_( torch.where( gradient_evidence, self.completion_successor_retention_passed.new_zeros(()), self.completion_successor_retention_passed, ) ) self.completion_successor_candidate_evaluation_active.logical_or_( gradient_evidence ) self.completion_successor_gradient_update_count.add_( gradient_evidence.to(dtype=torch.long) ) return gradient_evidence def completion_successor_authority_mode(self) -> torch.Tensor: """Return tensor-owned additive, candidate, or retained authority mode.""" candidate = ( self.completion_successor_authority_trained & self.completion_successor_candidate_evaluation_active ) retained = ( self.completion_successor_authority_trained & self.completion_successor_retention_passed ) fallback = self.completion_successor_gradient_update_count.new_full( (), STOP_AUTHORITY_ADDITIVE_TELEMETRY, ) candidate_mode = fallback.new_full( (), STOP_AUTHORITY_SUCCESSOR_CANDIDATE, ) retained_mode = fallback.new_full( (), STOP_AUTHORITY_SUCCESSOR_RETAINED, ) return torch.where( candidate, candidate_mode, torch.where(retained, retained_mode, fallback), ) def completion_successor_proof_boundary(self) -> dict[str, Any]: """Serialize successor lifecycle proof at the external receipt boundary.""" return { "schema": "nnf.resynthesis.completion_successor.v1", "authorityTrained": bool( self.completion_successor_authority_trained.detach().to( device="cpu", dtype=torch.bool ) ), "retentionPassed": bool( self.completion_successor_retention_passed.detach().to( device="cpu", dtype=torch.bool ) ), "candidateEvaluationActive": bool( self.completion_successor_candidate_evaluation_active.detach().to( device="cpu", dtype=torch.bool ) ), "gradientUpdateCount": int( self.completion_successor_gradient_update_count.detach().to( device="cpu", dtype=torch.long ) ), "utilityGradientRequired": True, "contradictionGradientRequired": True, "taskConfidenceGradientRequired": True, "recurrentSequenceContextGradientRequired": True, "intentActionContextGradientRequired": True, "trajectoryProjectionGradientRequired": True, "intentEncoderGradientRequired": True, "actionEncoderGradientRequired": True, "stopContextScaleGradientRequired": True, "confidenceContextScaleGradientRequired": True, "targetEnteredForward": False, "parentFallbackPreserved": True, } def activate_candidate_authorities_from_boundary(self) -> torch.Tensor: """Expose only genuinely updated additive authority to held-out grading.""" completion_updated = ( self._activate_completion_successor_candidate_from_boundary() ) acquisition_updated = ( self.acquisition_policy.activate_updated_candidate_from_boundary() ) legacy_updated = acquisition_updated.new_ones(()) legacy_bank = self.legacy_capability_bank if isinstance(legacy_bank, nn.Module): legacy_updated = acquisition_updated.new_zeros(()) trainable_fn = getattr(legacy_bank, "trainable_parameters", None) mark_fn = getattr( legacy_bank, "mark_authority_trained_from_boundary", None, ) if not callable(trainable_fn) or not callable(mark_fn): raise RuntimeError( "legacy capability bank lacks trained-authority boundary" ) for parameter in trainable_fn(): gradient = parameter.grad if gradient is not None: finite_nonzero = ( torch.isfinite(gradient).all() & gradient.ne(0).any() ) legacy_updated = legacy_updated | finite_nonzero.to( device=legacy_updated.device, dtype=torch.bool, ) legacy_authority = getattr(legacy_bank, "authority_trained", None) legacy_retention = getattr(legacy_bank, "retention_passed", None) if not isinstance(legacy_authority, torch.Tensor) or not isinstance( legacy_retention, torch.Tensor, ): raise RuntimeError("legacy capability bank lacks authority tensors") mark_fn( legacy_authority.reshape(()) | legacy_updated, torch.where( legacy_updated, legacy_updated, legacy_retention.reshape(()), ), ) update_proven = ( completion_updated | (acquisition_updated & legacy_updated) | self.candidate_page_update_proven ) with torch.no_grad(): self.candidate_authority_update_proven.logical_or_(update_proven) self.candidate_authority_retention_verified.zero_() return self.candidate_authority_update_proven.clone() def begin_candidate_authority_update_window_from_boundary(self) -> torch.Tensor: """Reset proposal-local proof before isolated optimizer updates accumulate.""" self.acquisition_policy.begin_candidate_update_window_from_boundary() generations: list[torch.Tensor] = [] for runtime in self._paged_none_runtimes_boundary(): generation_t = runtime.begin_candidate_update_window_boundary() if not isinstance(generation_t, torch.Tensor): raise RuntimeError( "NoNE candidate page window returned no tensor generation" ) generations.append(generation_t) if generations and not all( torch.equal(generations[0], generation) for generation in generations[1:] ): raise RuntimeError("NoNE layer page generations diverged") with torch.no_grad(): self.candidate_authority_update_proven.zero_() self.candidate_authority_retention_verified.zero_() self.candidate_page_update_proven.zero_() self.completion_successor_candidate_evaluation_active.zero_() self._activate_bound_candidate_layer_imports_boundary() return self.candidate_authority_update_proven.clone() def retain_candidate_authorities_from_boundary( self, accepted: torch.Tensor, *, completion_accepted: torch.Tensor | None = None, ) -> torch.Tensor: """Commit each authority only under its own validation evidence. Page or acquisition retention is not completion-boundary validation. ``completion_accepted`` therefore remains a distinct tensor authority; callers that omit it retain the historical all-authorities contract. """ if accepted.numel() != 1: raise RuntimeError("candidate authority retention decision must be scalar") accepted_bool = accepted.to( device=self.candidate_authority_update_proven.device, dtype=torch.bool, ).reshape(()) completion_accepted_bool = ( accepted_bool if completion_accepted is None else completion_accepted.to( device=self.candidate_authority_update_proven.device, dtype=torch.bool, ).reshape(()) ) completion_retained = ( self.completion_successor_authority_trained & ( self.completion_successor_candidate_evaluation_active | self.completion_successor_retention_passed ) & completion_accepted_bool ) with torch.no_grad(): self.completion_successor_retention_passed.copy_(completion_retained) self.completion_successor_candidate_evaluation_active.zero_() for runtime in self._paged_none_runtimes_boundary(): if bool(runtime.family_page_mask_t.detach().cpu().any()): retained_t = runtime.retain_candidate_training_totals_boundary( accepted_bool ) if not isinstance(retained_t, torch.Tensor): raise RuntimeError( "NoNE page proof retention returned no tensor proof" ) acquisition_retained = self.acquisition_policy.retain_candidate_from_boundary( accepted_bool ) legacy_retained = acquisition_retained.new_ones(()) legacy_bank = self.legacy_capability_bank if isinstance(legacy_bank, nn.Module): legacy_authority = getattr(legacy_bank, "authority_trained", None) mark_fn = getattr( legacy_bank, "mark_authority_trained_from_boundary", None, ) if not isinstance(legacy_authority, torch.Tensor) or not callable(mark_fn): raise RuntimeError( "legacy capability bank lacks retention authority state" ) legacy_retained = legacy_authority.reshape(()) & accepted_bool mark_fn(legacy_authority.reshape(()), legacy_retained) retained = ( completion_retained | (acquisition_retained & legacy_retained) | (self.candidate_page_update_proven & accepted_bool) ) with torch.no_grad(): self.candidate_authority_retention_verified.copy_(retained) return retained @staticmethod def compute_stop_loss(result: RBOResult, should_stop: torch.Tensor) -> torch.Tensor: """Train completion readiness without relabeling task correctness. The first two scores represent utility/contradiction completion. The third is task correctness confidence and is supervised independently by the observed next-token result in ``_training_auxiliary_loss``. """ # Keep the batch axis intact; only the trailing stop-score surface is # sliced to the first two completion heads (utility/contradiction). completion_scores = result.stop_scores[..., :2] target = should_stop.to( device=completion_scores.device, dtype=completion_scores.dtype, ) if target.ndim == 0: target = target.expand_as(completion_scores) elif target.shape == result.stop_scores.shape: target = target[..., :2] elif target.shape == result.stop_scores.shape[:-1]: target = target.unsqueeze(-1).expand_as(completion_scores) elif target.shape != completion_scores.shape: raise ValueError("stop target geometry differs from the model stop surface") return F.binary_cross_entropy(completion_scores, target) @staticmethod def compute_completion_calibration_loss( result: RBOResult, should_stop: torch.Tensor, ) -> torch.Tensor: """Train only the native completion heads for one visible prefix. The caller must produce ``result`` from the immutable prompt plus prior answer tokens only. The terminal label enters here after the forward, exactly like next-token CE, so no answer or target-derived signal can alter RBO/Fabric routing (Pillar 17). """ return ( COMPLETION_STOP_TRAINING_WEIGHT * ResynthesisRBO.compute_stop_loss(result, should_stop) ) @staticmethod def compute_bulk_prompt_continue_removal_loss( result: RBOResult, selected_rows_t: torch.Tensor, ) -> torch.Tensor: """Remove packed prompt-only CONTINUE labels for calibrated rows. Bulk knowledge CE labels every prompt state CONTINUE. A selected one-token answer is terminal at that same prompt, so merely adding its teacher-forced terminal loss would create contradictory supervision. This exact row-weighted negative term removes the prior CONTINUE label; the calibration arm then installs the correct prefix label. It changes only the loss boundary and never the target-free forward or routing. """ completion_scores_t = result.stop_scores[..., :2] if ( selected_rows_t.ndim != 1 or selected_rows_t.shape[0] != completion_scores_t.shape[0] or selected_rows_t.dtype != torch.bool or selected_rows_t.device != completion_scores_t.device ): raise ValueError("bulk completion calibration row geometry differs") selected_scores_t = completion_scores_t[selected_rows_t] torch._assert_async( selected_rows_t.any(), "bulk completion calibration selected no prompt row", ) selected_fraction_t = selected_rows_t.sum( dtype=completion_scores_t.dtype, ) / completion_scores_t.new_tensor(completion_scores_t.shape[0]) return ( -COMPLETION_STOP_TRAINING_WEIGHT * selected_fraction_t * F.binary_cross_entropy( selected_scores_t, torch.zeros_like(selected_scores_t), ) ) def _checkpoint_restore_branch_authority_boundary( *, child_branch_scope: NoNETrainingBranchScopePacket | None, child_branch_store_root: Path | None, checkpoint_restore_branch_scope: dict[str, Any] | None, external_state: dict[str, Any], ) -> tuple[NoNETrainingBranchScopePacket | None, Path | None]: """Resolve the immutable restore owner separately from the new child. This is a model-construction I/O boundary, not a forward-path routing decision. A sealed fanout checkpoint was authored by the source branch, while new updates belong only to the disjoint child. The source sidecar is therefore verified against its exact source scope/store before the copied child store is opened. The returned store is always the child: the source store is immutable evidence only and must never become the new writer. """ if checkpoint_restore_branch_scope is None: return child_branch_scope, child_branch_store_root if child_branch_scope is None or child_branch_store_root is None: raise RuntimeError( "sealed fork checkpoint restore has no child fork authority" ) from resynthesis.none_paging import ( training_branch_scope_from_record_boundary, ) restore_scope = training_branch_scope_from_record_boundary( checkpoint_restore_branch_scope ) source_scope_record = restore_scope.external_record_boundary() child_scope_record = child_branch_scope.external_record_boundary() source_store_root_value = external_state.get("storeRoot") generation = external_state.get("generationBinding") generation_session = ( generation.get("sessionId") if isinstance(generation, dict) else None ) generation_value = ( generation.get("generation") if isinstance(generation, dict) else None ) generation_payload_sha256 = ( generation.get("manifestPayloadSha256") if isinstance(generation, dict) else None ) source_page_layer_pairs = set( zip( source_scope_record["pageIds"], source_scope_record["pageLayerIds"], strict=True, ) ) child_page_layer_pairs = set( zip( child_scope_record["pageIds"], child_scope_record["pageLayerIds"], strict=True, ) ) if ( external_state.get("branchScope") != checkpoint_restore_branch_scope or not isinstance(source_store_root_value, str) or not source_store_root_value or not Path(source_store_root_value).expanduser().is_absolute() or not Path(source_store_root_value).expanduser().resolve().is_dir() or Path(source_store_root_value).expanduser().resolve() == child_branch_store_root.expanduser().resolve() or source_scope_record == child_scope_record or generation_session != source_scope_record["sessionId"] or child_scope_record["sessionId"] != source_scope_record["sessionId"] or type(generation_value) is not int or generation_value != child_scope_record["parentGeneration"] or generation_value <= source_scope_record["parentGeneration"] or generation_payload_sha256 != child_scope_record["parentManifestPayloadSha256"] or not child_page_layer_pairs or not child_page_layer_pairs < source_page_layer_pairs ): raise RuntimeError("sealed fork checkpoint restore store is invalid") return ( restore_scope, child_branch_store_root.expanduser().resolve(), ) def build_resynthesis_rbo( cfg: ResynthesisConfig | None = None, rbo_cfg: ResynthesisRBOConfig | None = None, *, device: torch.device | str = "cpu", lazy_base: bool = True, paged_training_authority: NoNETrainingAuthority | None = None, paged_inference_authority: ReleaseInferenceAuthority | None = None, checkpoint_restore_branch_scope: dict[str, Any] | None = None, inference_only: bool = False, ) -> ResynthesisRBO: """Factory: build the full Resynthesis RBO model. Freezes the integrated Resynthesis graph parent, attaches the additive science stack, and wraps everything in the RBO with the NeuralStopGate. Args: cfg: model config (defaults to ResynthesisConfig). rbo_cfg: RBO config (defaults to ResynthesisRBOConfig). device: torch device. lazy_base: if True, base weights load on first forward. """ from resynthesis.base_loader import build_frozen_base if ( paged_training_authority is not None and paged_inference_authority is not None ): raise RuntimeError( "training and public inference authorities are mutually exclusive" ) if paged_inference_authority is not None and not inference_only: raise RuntimeError( "public release authority is inference-only" ) rbo_cfg = rbo_cfg or ResynthesisRBOConfig() cfg = cfg or ResynthesisConfig() if paged_inference_authority is not None and ( checkpoint_restore_branch_scope is not None or rbo_cfg.candidate_state_path is not None or rbo_cfg.candidate_migration_receipt_path is not None or rbo_cfg.paged_none_replica_receipt_path is not None or rbo_cfg.paged_none_target_growth_plan_path is not None or rbo_cfg.paged_none_target_growth_plan_sha256 is not None or rbo_cfg.paged_none_adaptation_source_checkpoint_sha256 is not None or rbo_cfg.paged_none_training_branch_scope_path is not None or rbo_cfg.paged_none_training_branch_store_root is not None or rbo_cfg.training_branch_functional_owner_index is not None or rbo_cfg.training_branch_functional_owner_count is not None ): raise RuntimeError( "public release inference cannot activate training or adaptation state" ) release_identity_cache_root = ( Path( os.environ.get( "XDG_CACHE_HOME", str(Path.home() / ".cache"), ) ) / "nucleus-resynthesis" / paged_inference_authority.session_key / "artifact_sha256_cache" if paged_inference_authority is not None else None ) signed_adaptation_source_sha256 = ( rbo_cfg.paged_none_adaptation_source_checkpoint_sha256 ) signed_adaptation_fields_present = ( rbo_cfg.paged_none_target_growth_plan_path is not None, rbo_cfg.paged_none_target_growth_plan_sha256 is not None, signed_adaptation_source_sha256 is not None, ) defer_signed_predecessor_restore = all( signed_adaptation_fields_present ) if any(signed_adaptation_fields_present) and ( not defer_signed_predecessor_restore or not isinstance(signed_adaptation_source_sha256, str) or len(signed_adaptation_source_sha256) != 64 ): raise RuntimeError( "signed NoNE graph-adaptation source and target must be paired" ) state_path = Path(rbo_cfg.rbo_state_path) paged_composition_path = ( Path(rbo_cfg.paged_none_composition_path) if rbo_cfg.paged_none_composition_path is not None else None ) paged_migration_receipt_path = ( Path(rbo_cfg.paged_none_migration_receipt_path) if rbo_cfg.paged_none_migration_receipt_path is not None else None ) paged_replica_receipt_path = ( Path(rbo_cfg.paged_none_replica_receipt_path) if rbo_cfg.paged_none_replica_receipt_path is not None else None ) if paged_inference_authority is not None: release_composition_path = ( paged_inference_authority.generation_artifacts.composition.path ) release_migration_path = ( paged_inference_authority.generation_artifacts.migration.path ) if ( paged_composition_path is not None and paged_composition_path.resolve() != release_composition_path.resolve() ) or ( paged_migration_receipt_path is not None and paged_migration_receipt_path.resolve() != release_migration_path.resolve() ): raise RuntimeError( "public release composition configuration differs" ) if paged_replica_receipt_path is not None: raise RuntimeError( "public release inference cannot activate training replicas" ) paged_composition_path = release_composition_path paged_migration_receipt_path = release_migration_path training_branch_scope = None training_branch_store_root = ( Path(rbo_cfg.paged_none_training_branch_store_root) if rbo_cfg.paged_none_training_branch_store_root is not None else None ) if ( rbo_cfg.paged_none_training_branch_scope_path is None ) != (training_branch_store_root is None): raise RuntimeError( "NoNE training branch scope and isolated store must be paired" ) if rbo_cfg.paged_none_training_branch_scope_path is not None: from resynthesis.none_paging import load_training_branch_scope_boundary training_branch_scope = load_training_branch_scope_boundary( Path(rbo_cfg.paged_none_training_branch_scope_path) ) if (paged_composition_path is None) != (paged_migration_receipt_path is None): raise RuntimeError( "NoNE paged composition and migration receipt must be paired" ) if paged_replica_receipt_path is not None and paged_composition_path is None: raise RuntimeError("NoNE replica receipt requires a paged composition") candidate_path = ( Path(rbo_cfg.candidate_state_path) if rbo_cfg.candidate_state_path is not None else None ) if paged_composition_path is not None and candidate_path is not None: raise RuntimeError( "NoNE paged v2 composition cannot use a monolithic candidate" ) if candidate_path is not None and not candidate_path.is_file(): raise RuntimeError("Resynthesis additive geometry candidate is missing") paged_composition: dict[str, Any] | None = None if paged_composition_path is not None and paged_migration_receipt_path is not None: if paged_inference_authority is not None: load_state_path = ( paged_inference_authority.weights.checkpoint.path ) elif paged_training_authority is None: from resynthesis.none_migration import ( resolve_v2_training_authority, ) paged_training_authority = resolve_v2_training_authority( paged_composition_path, paged_migration_receipt_path, ) elif ( paged_training_authority.composition_path is not None and paged_training_authority.composition_path.resolve() != paged_composition_path.resolve() ) or ( paged_training_authority.migration_receipt_path is not None and paged_training_authority.migration_receipt_path.resolve() != paged_migration_receipt_path.resolve() ): raise RuntimeError("pre-resolved NoNE training authority differs") if paged_inference_authority is None: assert paged_training_authority is not None paged_composition_path = ( paged_training_authority.composition_path or paged_composition_path ) paged_migration_receipt_path = ( paged_training_authority.migration_receipt_path or paged_migration_receipt_path ) # The caller-selected receipt is the live replica topology. A # loaded checkpoint may carry an older receipt for lineage # validation, but it must never reactivate those historical stores # after an explicit topology relocation. paged_replica_receipt_path = ( paged_replica_receipt_path or paged_training_authority.replica_receipt_path ) loaded_composition = json.loads( paged_composition_path.read_text(encoding="utf-8") ) if not isinstance(loaded_composition, dict): raise RuntimeError("NoNE paged composition is invalid") paged_composition = loaded_composition if paged_inference_authority is None: assert paged_training_authority is not None load_state_path = paged_training_authority.checkpoint_path else: if paged_training_authority is not None: raise RuntimeError( "pre-resolved NoNE authority requires paged composition" ) load_state_path = candidate_path if candidate_path is not None else state_path if defer_signed_predecessor_restore and ( paged_training_authority is None or paged_training_authority.checkpoint_sha256 != signed_adaptation_source_sha256 or not load_state_path.is_file() or _file_sha256_boundary(load_state_path) != signed_adaptation_source_sha256 ): raise RuntimeError( "signed NoNE graph-adaptation source checkpoint differs" ) checkpoint_resolution: AdditiveCheckpointResolution | None = None resolved_checkpoint_state: dict[str, torch.Tensor] | None = None checkpoint_dense_geometry_exact = False if load_state_path.is_file(): ( checkpoint_layers, checkpoint_experts, checkpoint_migrated, checkpoint_projection_vocabulary_size, checkpoint_lexical_vocabulary_size, checkpoint_tokenizer_prefix_vocabulary_size, checkpoint_vocabulary_transfer_rank, ) = ( _resolved_additive_checkpoint_geometry_boundary( load_state_path ) ) target_layers = checkpoint_layers if paged_inference_authority is not None: target_layers = ( paged_inference_authority.topology.layer_count ) elif paged_migration_receipt_path is not None: target_layers, _growth_plan_sha256 = ( _paged_reasoning_layer_target_boundary( paged_migration_receipt_path, source_layers=checkpoint_layers, source_experts=checkpoint_experts, target_growth_plan_path=( Path(rbo_cfg.paged_none_target_growth_plan_path) if rbo_cfg.paged_none_target_growth_plan_path is not None else None ), expected_target_growth_plan_sha256=( rbo_cfg.paged_none_target_growth_plan_sha256 ), ) ) if checkpoint_layers < cfg.num_layers or checkpoint_experts < cfg.num_experts: raise RuntimeError( "Resynthesis additive checkpoint geometry cannot shrink seed capacity" ) if target_layers < checkpoint_layers: raise RuntimeError( "Resynthesis paged reasoning graph cannot shrink checkpoint layers" ) cfg = _checkpoint_adopted_vocabulary_configuration_boundary( cfg, projection_vocabulary_size=( checkpoint_projection_vocabulary_size ), lexical_vocabulary_size=checkpoint_lexical_vocabulary_size, tokenizer_prefix_vocabulary_size=( checkpoint_tokenizer_prefix_vocabulary_size ), vocabulary_transfer_rank=( checkpoint_vocabulary_transfer_rank ), ) cfg = replace( cfg, num_layers=target_layers, num_experts=checkpoint_experts, # Reasoning-layer growth has its own exact lineage and zero-open # migration contract. It does not alter the drafting tensors. geometry_migrated=checkpoint_migrated, ) checkpoint_dense_geometry_exact = target_layers == checkpoint_layers expected_load_sha256 = ( paged_inference_authority.weights.checkpoint.sha256 if paged_inference_authority is not None else paged_training_authority.checkpoint_sha256 if ( paged_training_authority is not None and paged_training_authority.checkpoint_path.resolve() == load_state_path.resolve() ) else None ) child_branch_scope = ( training_branch_scope.external_record_boundary() if training_branch_scope is not None else None ) if checkpoint_restore_branch_scope is not None and ( child_branch_scope is None or child_branch_scope.get("forkAuthority") is not True or checkpoint_restore_branch_scope.get("schema") != "nnf.resynthesis.none_training_branch_scope.v1" ): raise RuntimeError( "sealed fork checkpoint restore has no child fork authority" ) # A sealed fanout restores the immutable parent's branch-local delta # before any child update exists. Verify that artifact against its exact # source scope, while retaining the disjoint child scope as the only owner # of newly trained pages and causal state. expected_branch_scope = ( checkpoint_restore_branch_scope if checkpoint_restore_branch_scope is not None else child_branch_scope ) if ( load_state_path.is_file() and not defer_signed_predecessor_restore and checkpoint_dense_geometry_exact ): checkpoint_resolution = resolve_additive_checkpoint_boundary( load_state_path, expected_artifact_sha256=expected_load_sha256, identity_cache_root=( paged_training_authority.identity_cache_root if paged_training_authority is not None else release_identity_cache_root ), expected_branch_scope=expected_branch_scope, ) resolved_checkpoint_state = _checkpoint_tensor_state_boundary( checkpoint_resolution.payload ) target_device = torch.device(device) base = build_frozen_base(cfg, device=target_device, lazy=lazy_base) science_cfg = ResynthesisScienceLayerConfig( hidden_size=cfg.hidden_size, knowledge_transfer_dim=cfg.knowledge_transfer_dim, num_layers=cfg.num_layers, num_experts=cfg.num_experts, expert_hidden_size=cfg.expert_hidden_size, memory_slots=cfg.memory_slots, attention_heads=cfg.attention_heads, mhc_heads=cfg.mhc_heads, recursive_steps=cfg.recursive_steps, residual_init=cfg.residual_init, logit_residual_init=cfg.logit_residual_init, kl_anchor_weight=cfg.kl_anchor_weight, kl_anchor_warmup_steps=cfg.kl_anchor_warmup_steps, glyph_input_dim=cfg.glyph_input_dim, ) # Construct additive modules on the admitted device. The native parent is # already loaded there; building the NoNE graph on CPU and recursively # copying it later strands every GPU behind host allocation and PCIe work. # The checkpoint-direct path materializes exact tensors on this target # before assignment, so placeholders cannot overwrite trained state. construction_context = ( torch.device(target_device) if target_device.type == "cuda" else contextlib.nullcontext() ) with construction_context: if resolved_checkpoint_state is not None: from resynthesis.none_migration import ( build_checkpoint_direct_science_stack_boundary, ) science_stack = build_checkpoint_direct_science_stack_boundary( science_cfg, resolved_checkpoint_state, device=target_device, ) else: science_stack = build_resynthesis_science_stack(science_cfg) rbo = ResynthesisRBO( base=base, science_stack=science_stack, cfg=rbo_cfg, model_cfg=cfg, ) if target_device.type == "cuda": rbo = rbo.to(device=target_device, dtype=torch.bfloat16) else: rbo = rbo.to(device=target_device) rbo.preserve_trainable_control_precision() paged_attached = False canonical_checkpoint_restored = False canonical_generation_restored = False canonical_parent_paged_lineage_for_restore: dict[str, Any] | None = None if ( ( paged_inference_authority is not None or ( paged_training_authority is not None and paged_training_authority.checkpoint_includes_paged_runtime ) ) and paged_composition_path is not None and paged_migration_receipt_path is not None ): rbo.attach_paged_none_composition_boundary( paged_composition_path, paged_migration_receipt_path, paged_replica_receipt_path, training_authority=paged_training_authority, inference_authority=paged_inference_authority, # Validate the immutable parent lineage before introducing a # branch-local optimizer scope. Branch ownership is activated # only after the exact accepted checkpoint and external page # generation have been restored below. training_branch_scope=None, ) paged_attached = True if load_state_path.is_file(): rbo.ensure_parent_capability_attachment() if load_state_path.is_file() and not defer_signed_predecessor_restore: promotion_receipt: dict[str, Any] | None = None if paged_composition is not None: promotion_receipt = None elif candidate_path is not None: from resynthesis.geometry_migration import validate_migration_candidate candidate_receipt_path = rbo_cfg.candidate_migration_receipt_path if candidate_receipt_path is None: raise RuntimeError( "Resynthesis additive geometry candidate has no migration receipt" ) validate_migration_candidate(candidate_path, candidate_receipt_path) else: promotion_receipt_path = state_path.with_suffix( f"{state_path.suffix}.receipt.json" ) if not promotion_receipt_path.is_file(): raise RuntimeError( "Resynthesis additive checkpoint has no promotion receipt" ) with promotion_receipt_path.open(encoding="utf-8") as handle: loaded_promotion = json.load(handle) if not isinstance(loaded_promotion, dict) or ( loaded_promotion.get("schema") != "nnf.resynthesis.additive_promotion.v1" ): raise RuntimeError( "Resynthesis additive promotion receipt schema differs" ) promotion_receipt = loaded_promotion digest = hashlib.sha256() with state_path.open("rb") as handle: for chunk in iter(lambda: handle.read(8 * 1024 * 1024), b""): digest.update(chunk) if promotion_receipt.get("artifactSha256") != digest.hexdigest(): raise RuntimeError( "Resynthesis additive artifact SHA-256 differs from promotion" ) if checkpoint_resolution is None: checkpoint_resolution = resolve_additive_checkpoint_boundary( load_state_path, expected_artifact_sha256=expected_load_sha256, identity_cache_root=( paged_training_authority.identity_cache_root if paged_training_authority is not None else release_identity_cache_root ), expected_branch_scope=expected_branch_scope, ) if checkpoint_resolution.branch_delta is not None: if training_branch_scope is None or not ( _branch_checkpoint_scope_supports_requested_ownership_boundary( checkpoint_resolution.branch_delta.branch_scope, training_branch_scope.external_record_boundary(), ) ): raise RuntimeError( "NoNE branch delta scope differs from requested ownership" ) payload = checkpoint_resolution.payload if rbo.page_only_training_branch_active_boundary(): raise RuntimeError( "NoNE training branch activated before checkpoint lineage validation" ) checkpoint_lineage = payload.get("lineage") if training_branch_scope is not None: rbo.bind_page_branch_checkpoint_parent_boundary( checkpoint_path=checkpoint_resolution.base_checkpoint_path, checkpoint_sha256=( checkpoint_resolution.base_checkpoint_sha256 ), checkpoint_payload=checkpoint_resolution.base_payload, identity_cache_root=( paged_training_authority.identity_cache_root if paged_training_authority is not None else None ), ) canonical_parent_paged_lineage = ( checkpoint_lineage.get("pagedNoNE") if isinstance(checkpoint_lineage, dict) else None ) if not isinstance(canonical_parent_paged_lineage, dict): raise RuntimeError( "NoNE training branch checkpoint has no canonical paged lineage" ) # The branch parent sidecar is restored before the isolated writer # is activated. Preserve the already validated checkpoint lineage # here so that restore can bind its replica fields to the signed # branch-store graph authority without a second canonical checkout. rbo._paged_none_canonical_checkpoint_paged_lineage = dict( canonical_parent_paged_lineage ) canonical_parent_paged_lineage_for_restore = dict( canonical_parent_paged_lineage ) if promotion_receipt is not None and ( promotion_receipt.get("lineage") != payload.get("lineage") ): raise RuntimeError( "Resynthesis additive promotion lineage differs from its payload" ) state = ( resolved_checkpoint_state if resolved_checkpoint_state is not None else _checkpoint_tensor_state_boundary(payload) ) names = sorted(state) key_hash = hashlib.sha256("\n".join(names).encode("utf-8")).hexdigest() geometry = [ (name, tuple(state[name].shape), str(state[name].dtype)) for name in names ] geometry_hash = hashlib.sha256( json.dumps(geometry, separators=(",", ":")).encode("utf-8") ).hexdigest() if payload.get("stateKeySetSha256") != key_hash: raise RuntimeError("Resynthesis additive checkpoint key identity differs") if payload.get("stateGeometrySha256") != geometry_hash: raise RuntimeError( "Resynthesis additive checkpoint geometry identity differs" ) rbo.load_trainable_state_dict( state, checkpoint_lineage=payload.get("lineage"), ) canonical_checkpoint_restored = True if ( paged_composition_path is not None and paged_migration_receipt_path is not None and not paged_attached ): rbo.attach_paged_none_composition_boundary( paged_composition_path, paged_migration_receipt_path, paged_replica_receipt_path, training_authority=paged_training_authority, inference_authority=paged_inference_authority, training_branch_scope=None, ) paged_attached = True if paged_inference_authority is not None: if defer_signed_predecessor_restore: raise RuntimeError( "public release inference cannot defer checkpoint adoption" ) rebound_generation_t = ( rbo.bind_loaded_paged_graph_authority_from_boundary( paged_inference_authority.weights.checkpoint.sha256 ) ) if int(rebound_generation_t.detach().cpu()) != ( paged_inference_authority.generation ): raise RuntimeError( "public release rebound generation differs" ) external_envelope = json.loads( paged_inference_authority.generation_artifacts.external_state.path.read_text( encoding="utf-8" ) ) external_state = ( external_envelope.get("externalState") if isinstance(external_envelope, dict) else None ) generation_binding = ( external_state.get("generationBinding") if isinstance(external_state, dict) else None ) if ( not isinstance(external_state, dict) or not isinstance(generation_binding, dict) or generation_binding.get("generation") != paged_inference_authority.generation ): raise RuntimeError( "public release external generation differs" ) rbo.validate_external_training_proof_from_boundary(external_state) canonical_generation_restored = True if ( paged_training_authority is not None and paged_training_authority.external_state_path is not None ): if not defer_signed_predecessor_restore: rbo.bind_loaded_paged_graph_authority_from_boundary( paged_training_authority.paged_graph_checkpoint_sha256 or paged_training_authority.checkpoint_sha256 ) external_envelope = json.loads( paged_training_authority.external_state_path.read_text(encoding="utf-8") ) external_state = ( external_envelope.get("externalState") if isinstance(external_envelope, dict) else None ) if not isinstance(external_state, dict): raise RuntimeError("NoNE paged training checkpoint sidecar is invalid") if inference_only: if defer_signed_predecessor_restore: raise RuntimeError( "NoNE inference restore cannot defer signed predecessor adoption" ) generation_binding = external_state.get("generationBinding") if not isinstance(generation_binding, dict): raise RuntimeError( "NoNE inference external generation binding is malformed" ) generation_value = generation_binding.get("generation") if ( generation_value != paged_training_authority.accepted_generation ): raise RuntimeError( "NoNE inference external generation differs from accepted pointer" ) rbo.validate_external_training_proof_from_boundary(external_state) elif not defer_signed_predecessor_restore: ( checkpoint_restore_scope, checkpoint_restore_store_root, ) = _checkpoint_restore_branch_authority_boundary( child_branch_scope=training_branch_scope, child_branch_store_root=training_branch_store_root, checkpoint_restore_branch_scope=( checkpoint_restore_branch_scope ), external_state=external_state, ) restored_generation_t = rbo.restore_external_state_from_boundary( external_state, parent_restore_branch_scope=checkpoint_restore_scope, canonical_parent_paged_lineage=( canonical_parent_paged_lineage_for_restore ), accepted_descendant_branch_store_root=( checkpoint_restore_store_root ), fork_child_branch_scope=( training_branch_scope if checkpoint_restore_branch_scope is not None else None ), ) if int(restored_generation_t) != ( paged_training_authority.accepted_generation ): raise RuntimeError("NoNE paged training generation restore differs") if training_branch_scope is not None: # The first bind fences the canonical parent before the # sidecar is read. A verified descendant restore then moves # the active store to its branch-local graph, whose accepted # checkpoint is deliberately different from the parent. Bind # that exact descendant only after the store switch; otherwise # the next resident-graph validation correctly rejects the # stale parent fence as a cold-adoption mismatch. descendant_generation_t = ( rbo.bind_loaded_paged_graph_authority_from_boundary( paged_training_authority.checkpoint_sha256 ) ) if int(descendant_generation_t) != int( restored_generation_t ): raise RuntimeError( "NoNE paged training descendant graph fence differs" ) canonical_generation_restored = True if training_branch_scope is not None: if training_branch_store_root is None: raise RuntimeError("NoNE training branch has no isolated store") if ( paged_training_authority is None or paged_training_authority.external_state_path is None or not canonical_checkpoint_restored or not canonical_generation_restored or rbo.page_only_training_branch_active_boundary() ): raise RuntimeError( "NoNE training branch activated before canonical parent restore" ) canonical_parent_lineage = rbo._page_branch_parent_checkpoint_lineage if not isinstance(canonical_parent_lineage, dict): raise RuntimeError( "NoNE training branch has no bound parent checkpoint lineage" ) canonical_parent_lineage = copy.deepcopy(canonical_parent_lineage) rbo.activate_training_branch_store_boundary( scope=training_branch_scope, branch_store_root=training_branch_store_root, ) if rbo.checkpoint_lineage() != canonical_parent_lineage: raise RuntimeError( "NoNE training branch changed canonical checkpoint lineage" ) if paged_attached and not defer_signed_predecessor_restore: rbo.validate_paged_none_graph_frontier_boundary() rbo.enforce_training_branch_parameter_ownership_boundary() return rbo def resynthesis_rbo_forward_with_logits( rbo: ResynthesisRBO, input_ids: torch.Tensor, ) -> torch.Tensor: """Convenience: forward and return shaped logits only.""" result = rbo.forward_thinking(input_ids) return result.shaped_logits def resynthesis_rbo_forward_precomputed( rbo: ResynthesisRBO, hidden: torch.Tensor, baseline_logits: torch.Tensor, parent_expert_routes: torch.Tensor, parent_layer_routes: torch.Tensor, parent_kv_prefix_positions: torch.Tensor, parent_kv_new_positions: torch.Tensor, session_state: RBOCorrectionState | None = None, ) -> RBOResult: """Forward from parent-produced tensors without bypassing parent route ownership.""" rbo._connect_active_parent_fabric() parent_context_hidden = _hidden_sequence_context(hidden) active_hidden = hidden[:, -1:, :] active_logits = baseline_logits[:, -1:, :] state = session_state or rbo.initial_correction_state( parent_context_hidden.unsqueeze(1) ) return rbo._forward_from_parent( active_hidden, active_logits, state, parent_expert_routes, parent_layer_routes, parent_kv_prefix_positions, parent_kv_new_positions, parent_context_hidden, ) @torch.no_grad() def fuse_trained_decode_stop_authorities( result: RBOResult, native_stop: Any, completion_successor_authority: torch.Tensor | None = None, ) -> RBOResult: """Select additive completion while recording legacy parent diagnostics. The function name remains for checkpoint/API compatibility. Parent confidence is observed only when a caller explicitly supplies it; it never enters the selected probability, decision, successor lifecycle, retention state, or arm-exhaustion path. """ batch_size = result.stop_scores.shape[0] additive_probability = _additive_completion_probability(result.stop_scores) additive_decision = result.stop_decision.to(dtype=torch.bool).reshape(batch_size) native_probability = additive_probability.new_zeros((batch_size,)) native_decision = additive_decision.new_zeros((batch_size,)) if native_stop is not None: native_probability_raw = getattr(native_stop, "probability", None) native_decision_raw = getattr(native_stop, "decision", None) if not isinstance(native_probability_raw, torch.Tensor) or not isinstance( native_decision_raw, torch.Tensor, ): raise RuntimeError( "parent native stop diagnostic returned no tensor observation" ) native_probability = native_probability_raw.reshape( batch_size, -1, ).mean(dim=-1) native_decision = native_decision_raw.reshape( batch_size, -1, ).all(dim=-1) if completion_successor_authority is None: authority_mode = additive_decision.new_full( additive_decision.shape, STOP_AUTHORITY_ADDITIVE_TELEMETRY, dtype=torch.long, ) else: if completion_successor_authority.numel() not in (1, batch_size): raise RuntimeError( "completion successor authority must be scalar or per-example" ) authority_mode = completion_successor_authority.to( device=additive_decision.device, dtype=torch.long, ).reshape(-1) if authority_mode.numel() == 1: authority_mode = authority_mode.expand(batch_size) successor_active = authority_mode.eq( STOP_AUTHORITY_SUCCESSOR_CANDIDATE ) | authority_mode.eq(STOP_AUTHORITY_SUCCESSOR_RETAINED) selected_authority = torch.where( successor_active, authority_mode, authority_mode.new_full( authority_mode.shape, STOP_AUTHORITY_ADDITIVE_TELEMETRY, ), ) return replace( result, stop_probability=additive_probability, stop_decision=additive_decision, decode_stop_authority=selected_authority, parent_native_stop_probability=native_probability, parent_native_stop_decision=native_decision, ) def _detached_emission_tensor(value: torch.Tensor) -> torch.Tensor: """Clone one live tensor for the external token-audit boundary.""" return value.detach().clone() def detached_emission_trace_packet( rbo: ResynthesisRBO, result: RBOResult, fused_result: RBOResult, active_state: RBOCorrectionState, next_token: torch.Tensor, emission_index_t: torch.Tensor, active_emission_mask_t: torch.Tensor, arm_exhaustion_exit: torch.Tensor, delegation_exit: torch.Tensor, ) -> RBOEmissionTracePacket: """Snapshot model-owned token state after emission, never before it. Generation and teacher-forced learning share this detached observation boundary so NLA, confidence, Fabric, and NoNE routes are reported from the same model-owned tensors. The returned packet has no target field and no return channel into the active graph. """ coverage = active_state.traversal_state.expert_selections.to(dtype=torch.bool) if coverage.ndim != 3: raise RuntimeError("science traversal coverage must be [batch, layer, expert]") coverage_count_t = coverage.to(dtype=torch.long).sum(dim=(1, 2)) coverage_total_t = coverage_count_t.new_full( coverage_count_t.shape, coverage.shape[1] * coverage.shape[2], ) active_page_ids_t = rbo.active_paged_route_rows_t(next_token) science_layer_count = rbo.science_stack.num_layers layer_routes_t = ( result.layer_routes.unsqueeze(0) if result.layer_routes.ndim == 2 else result.layer_routes ) expert_routes_t = ( result.expert_routes.unsqueeze(0) if result.expert_routes.ndim == 4 else result.expert_routes ) if layer_routes_t.ndim != 3 or expert_routes_t.ndim != 5: raise RuntimeError("science attempt-route geometry is unsupported") if layer_routes_t.shape[:2] != expert_routes_t.shape[:2]: raise RuntimeError("science expert/layer attempt routes differ") if layer_routes_t.shape[2] != expert_routes_t.shape[2]: raise RuntimeError("science expert/layer route batch geometry differs") active_depth = layer_routes_t.shape[1] if science_layer_count < 1 or active_depth % science_layer_count != 0: raise RuntimeError("science traversal route geometry is not rectangular") recursive_steps = active_depth // science_layer_count route_attempts = layer_routes_t.shape[0] layer_gate_grid_t = layer_routes_t.reshape( route_attempts, recursive_steps, science_layer_count, layer_routes_t.shape[2], ) selected_slot_ids_t = layer_gate_grid_t.argmax(dim=2).permute(0, 2, 1) traversal_step_ids_t = torch.arange( recursive_steps, device=layer_routes_t.device, dtype=torch.long, ).view(1, 1, -1) selected_layer_ids_t = ( selected_slot_ids_t + traversal_step_ids_t ).remainder(science_layer_count) selected_depth_ids_t = ( traversal_step_ids_t * science_layer_count + selected_slot_ids_t ) depth_expert_scores_t = expert_routes_t.mean(dim=3).permute(0, 2, 1, 3) selected_expert_scores_t = depth_expert_scores_t.gather( 2, selected_depth_ids_t.unsqueeze(-1).expand( -1, -1, -1, depth_expert_scores_t.shape[-1], ), ) selected_expert_ids_t = selected_expert_scores_t.argmax(dim=-1) selected_layer_expert_pairs_t = torch.stack( (selected_layer_ids_t, selected_expert_ids_t), dim=-1, ) return RBOEmissionTracePacket( emission_index_t=_detached_emission_tensor(emission_index_t), active_emission_mask_t=_detached_emission_tensor(active_emission_mask_t), token_ids_t=_detached_emission_tensor(next_token), attempt_index_t=_detached_emission_tensor(active_state.attempt_index), stop_scores_t=_detached_emission_tensor(result.stop_scores), additive_stop_probability_t=_detached_emission_tensor(result.stop_probability), native_stop_probability_t=_detached_emission_tensor( fused_result.parent_native_stop_probability ), fused_stop_probability_t=_detached_emission_tensor( fused_result.stop_probability ), additive_stop_decision_t=_detached_emission_tensor(result.stop_decision), native_stop_decision_t=_detached_emission_tensor( fused_result.parent_native_stop_decision ), fused_stop_decision_t=_detached_emission_tensor(fused_result.stop_decision), decode_stop_authority_t=_detached_emission_tensor( fused_result.decode_stop_authority ), task_confidence_t=_detached_emission_tensor(result.correction.task_confidence), correction_trigger_t=_detached_emission_tensor(result.correction.trigger), delegation_pressure_t=_detached_emission_tensor( result.correction.delegation_pressure ), task_intent_probabilities_t=_detached_emission_tensor( result.task_intent.probabilities ), task_intent_dominant_index_t=_detached_emission_tensor( result.task_intent.dominant_index ), prefill_active_t=_detached_emission_tensor(result.prefill.active), prefill_input_positions_t=_detached_emission_tensor( result.prefill.input_positions ), prefill_summary_positions_t=_detached_emission_tensor( result.prefill.summary_positions ), prefill_fabric_phase_count_t=_detached_emission_tensor( result.prefill.fabric_phase_count ), prefill_expert_routes_t=_detached_emission_tensor( result.prefill.expert_routes ), prefill_layer_routes_t=_detached_emission_tensor( result.prefill.layer_routes ), nla_round_trip_mse_t=_detached_emission_tensor(result.nla.round_trip_mse), nla_latent_l2_t=_detached_emission_tensor(result.nla.latent.norm()), nla_reconstructed_l2_t=_detached_emission_tensor( result.nla.reconstructed.norm() ), nla_latent_code_ids_t=_detached_emission_tensor(result.nla.latent_code_ids), nla_sequence_context_positions_t=_detached_emission_tensor( result.nla.sequence_context_positions ), nla_intent_context_l2_t=_detached_emission_tensor( result.nla.intent_context.norm() ), nla_action_context_l2_t=_detached_emission_tensor( result.nla.action_context.norm() ), nla_completion_context_l2_t=_detached_emission_tensor( result.nla.completion_context.norm() ), nla_stop_context_scale_t=_detached_emission_tensor( result.nla.stop_context_scale ), nla_confidence_context_scale_t=_detached_emission_tensor( result.nla.confidence_context_scale ), fabric_phase_count_t=_detached_emission_tensor(result.fabric_phase_count), science_expert_routes_t=_detached_emission_tensor(result.expert_routes), science_layer_routes_t=_detached_emission_tensor(result.layer_routes), science_selected_layer_ids_t=_detached_emission_tensor( selected_layer_ids_t ), science_selected_expert_ids_t=_detached_emission_tensor( selected_expert_ids_t ), science_selected_layer_expert_pairs_t=_detached_emission_tensor( selected_layer_expert_pairs_t ), parent_expert_routes_t=_detached_emission_tensor(result.parent_expert_routes), parent_layer_routes_t=_detached_emission_tensor(result.parent_layer_routes), expert_selection_count_t=_detached_emission_tensor(coverage_count_t), expert_selection_total_t=_detached_emission_tensor(coverage_total_t), active_routed_page_ids_t=_detached_emission_tensor(active_page_ids_t), trajectory_initialized_t=_detached_emission_tensor( fused_result.generation_trajectory_initialized ), native_progress_t=_detached_emission_tensor( fused_result.generation_native_progress ), task_progress_t=_detached_emission_tensor( fused_result.generation_task_progress ), arm_exhausted_t=_detached_emission_tensor( fused_result.generation_arm_exhausted ), arm_exhaustion_exit_t=_detached_emission_tensor(arm_exhaustion_exit), delegation_exit_t=_detached_emission_tensor(delegation_exit), ) def _masked_batch_rows( prior_t: torch.Tensor, candidate_t: torch.Tensor, select_candidate_t: torch.Tensor, ) -> torch.Tensor: """Select complete batch rows without extracting host scalars.""" if prior_t.shape != candidate_t.shape: raise RuntimeError("masked batch tensors differ in geometry") if prior_t.ndim < 1 or prior_t.shape[0] != select_candidate_t.shape[0]: raise RuntimeError("masked batch tensor has no matching batch axis") row_mask_t = select_candidate_t.reshape( select_candidate_t.shape[0], *((1,) * (prior_t.ndim - 1)), ) return torch.where(row_mask_t, candidate_t, prior_t) def _masked_causal_world_state_rows( prior: CausalWorldState | None, candidate: CausalWorldState | None, select_candidate_t: torch.Tensor, ) -> CausalWorldState | None: """Latch model-owned causal worlds without mixing caller batch rows.""" if prior is None and candidate is None: return None if prior is None or candidate is None: raise RuntimeError("causal world-state presence differs across batch latch") return CausalWorldState( ontology_t=_masked_batch_rows( prior.ontology_t, candidate.ontology_t, select_candidate_t, ), rule_t=_masked_batch_rows( prior.rule_t, candidate.rule_t, select_candidate_t, ), posterior_t=_masked_batch_rows( prior.posterior_t, candidate.posterior_t, select_candidate_t, ), contradiction_t=_masked_batch_rows( prior.contradiction_t, candidate.contradiction_t, select_candidate_t, ), commitment_t=_masked_batch_rows( prior.commitment_t, candidate.commitment_t, select_candidate_t, ), working_memory_t=_masked_batch_rows( prior.working_memory_t, candidate.working_memory_t, select_candidate_t, ), action_policy_t=_masked_batch_rows( prior.action_policy_t, candidate.action_policy_t, select_candidate_t, ), replay_error_t=_masked_batch_rows( prior.replay_error_t, candidate.replay_error_t, select_candidate_t, ), information_value_t=_masked_batch_rows( prior.information_value_t, candidate.information_value_t, select_candidate_t, ), exploration_counterweight_t=_masked_batch_rows( prior.exploration_counterweight_t, candidate.exploration_counterweight_t, select_candidate_t, ), hill_climb_accept_t=_masked_batch_rows( prior.hill_climb_accept_t, candidate.hill_climb_accept_t, select_candidate_t, ), ) def _masked_correction_state_rows( prior: RBOCorrectionState, candidate: RBOCorrectionState, select_candidate_t: torch.Tensor, ) -> RBOCorrectionState: """Latch session and traversal state at each row's first terminal token.""" prior_traversal = prior.traversal_state candidate_traversal = candidate.traversal_state return RBOCorrectionState( prior_hidden=_masked_batch_rows( prior.prior_hidden, candidate.prior_hidden, select_candidate_t, ), outcome_features=_masked_batch_rows( prior.outcome_features, candidate.outcome_features, select_candidate_t, ), parent_outcome_features=_masked_batch_rows( prior.parent_outcome_features, candidate.parent_outcome_features, select_candidate_t, ), acquisition_action_probs=_masked_batch_rows( prior.acquisition_action_probs, candidate.acquisition_action_probs, select_candidate_t, ), acquisition_action_index=_masked_batch_rows( prior.acquisition_action_index, candidate.acquisition_action_index, select_candidate_t, ), acquisition_authority=_masked_batch_rows( prior.acquisition_authority, candidate.acquisition_authority, select_candidate_t, ), outcome_present=_masked_batch_rows( prior.outcome_present, candidate.outcome_present, select_candidate_t, ), attempt_index=_masked_batch_rows( prior.attempt_index, candidate.attempt_index, select_candidate_t, ), traversal_state=ScienceTraversalState( expert_visits=_masked_batch_rows( prior_traversal.expert_visits, candidate_traversal.expert_visits, select_candidate_t, ), expert_selections=_masked_batch_rows( prior_traversal.expert_selections, candidate_traversal.expert_selections, select_candidate_t, ), layer_visits=_masked_batch_rows( prior_traversal.layer_visits, candidate_traversal.layer_visits, select_candidate_t, ), traversal_index=_masked_batch_rows( prior_traversal.traversal_index, candidate_traversal.traversal_index, select_candidate_t, ), ), causal_world_state=_masked_causal_world_state_rows( prior.causal_world_state, candidate.causal_world_state, select_candidate_t, ), ) @torch.no_grad() def resynthesis_rbo_generate( rbo: ResynthesisRBO, prompt_ids: torch.Tensor, *, session_state: RBOCorrectionState | None = None, emission_observer: Callable[[RBOEmissionTracePacket], object] | None = None, ) -> RBOGenerationResult: """Production decode: autoregressive generation with model-owned stopping. THINKING IS A FEATURE: This is a reasoning model. It produces ... reasoning blocks BEFORE its final answer. Tests must NEVER penalize, strip, or disable thinking — they must wait for the full response (including reasoning) and score the post-think answer. A separate reasoning-quality score should evaluate how well the model reasons over CAS/chemistry subjects (chemical plausibility, route analysis, safety, scalability, evidence quality, etc.). Successful answer completion is owned entirely by the trained additive NoNE/RBO/Fabric completion surface. The frozen parent's historical decode-confidence surface is not called and cannot select, veto, retain, or stop a token. An attempt may instead return as a model-owned arm-exhaustion failure only when the trained additive delegation surface requests an arm transition, additive completion/task-confidence trajectories stop progressing, and every additive layer has selected every configured expert through its model-owned post-NoNE route. Route coverage alone is not completion evidence and cannot truncate a still-open multi-token answer. That failure path is explicitly not an answer-completion claim and lets the verifier/learner consume the failure without a host token/time/step cap. A first token can establish the trajectory but cannot itself be a plateau. The model produces tokens via the native bit-tokenizer argmax path (no host top-k rerank, no shape-valid scan veto). ``emission_observer`` is an external, target-free audit boundary invoked after each token and all model-owned confidence decisions are computed. Its return value is deliberately ignored and its packet is detached from live state; it cannot select, rewrite, score, or stop the answer. """ rbo.eval() rbo.begin_decode_arm() ids = prompt_ids.clone() active_state = session_state final_result: RBOResult | None = None batch_size = prompt_ids.shape[0] trajectory_initialized = prompt_ids.new_zeros(batch_size, dtype=torch.bool) emission_index_t = prompt_ids.new_zeros((), dtype=torch.long) prior_additive_probability = prompt_ids.new_zeros( batch_size, dtype=torch.float32, ) prior_task_confidence = prompt_ids.new_zeros(batch_size, dtype=torch.float32) finished_t = prompt_ids.new_zeros(batch_size, dtype=torch.bool) generated_lengths_t = prompt_ids.new_zeros(batch_size, dtype=torch.long) terminal_state: RBOCorrectionState | None = None terminal_stop_scores_t: torch.Tensor | None = None terminal_stop_reason_t: torch.Tensor | None = None terminal_stop_probability_t: torch.Tensor | None = None terminal_stop_decision_t: torch.Tensor | None = None terminal_decode_authority_t: torch.Tensor | None = None terminal_native_probability_t: torch.Tensor | None = None terminal_native_decision_t: torch.Tensor | None = None terminal_trajectory_initialized_t: torch.Tensor | None = None terminal_native_progress_t: torch.Tensor | None = None terminal_task_progress_t: torch.Tensor | None = None terminal_arm_exhausted_t: torch.Tensor | None = None terminal_delegation_exit_t: torch.Tensor | None = None while True: result = rbo.forward_thinking(ids, active_state) active_state = result.next_state final_result = result next_token = result.native_token_ids if next_token.shape != (ids.shape[0], 1): raise RuntimeError( "native bit-tokenizer returned invalid next-token geometry" ) ids = torch.cat([ids, next_token], dim=1) final_result = fuse_trained_decode_stop_authorities( result, None, rbo.completion_successor_authority_mode(), ) corrected_attempt = active_state.attempt_index.ge(1).all(dim=-1) initial_attempt = active_state.attempt_index.eq(0).all(dim=-1) delegation_signal = result.correction.delegation_pressure.mean(dim=-1).gt(0.5) current_additive_probability = final_result.stop_probability.float() current_task_confidence = result.correction.task_confidence.mean(dim=-1).float() additive_progress = current_additive_probability.gt( prior_additive_probability ) task_progress = current_task_confidence.gt(prior_task_confidence) trajectory_plateau = ( trajectory_initialized & ~additive_progress & ~task_progress ) expert_selection_coverage = active_state.traversal_state.expert_selections.to( dtype=torch.bool ) arm_exhausted = expert_selection_coverage.all(dim=(1, 2)) # Delegation and corrected-arm identity are additive session state. # Either may identify an exhausted non-answer arm once additive # completion/task trajectories plateau and routed coverage is complete; # parent confidence never participates. trained_arm_exit_signal = corrected_attempt | delegation_signal arm_exhaustion_exit = ( ~final_result.stop_decision.to(dtype=torch.bool) & trained_arm_exit_signal & trajectory_plateau & arm_exhausted & (initial_attempt | corrected_attempt) ) delegation_exit = arm_exhaustion_exit & delegation_signal final_result = replace( final_result, generation_trajectory_initialized=trajectory_initialized, # Field name retained for receipt compatibility; the tensor now # records additive completion progress, never parent-native progress. generation_native_progress=additive_progress, generation_task_progress=task_progress, generation_arm_exhausted=arm_exhausted, ) if bool( arm_exhaustion_exit.detach() .to(device="cpu", dtype=torch.bool) .any() ): final_result = replace( final_result, stop_reason=torch.where( arm_exhaustion_exit, final_result.stop_reason.new_full( final_result.stop_reason.shape, STOP_REASON_SELF_CORRECTION_PLATEAU, ), final_result.stop_reason, ), generation_delegation_exit=delegation_exit, ) active_emission_mask_t = ~finished_t termination_now_t = ( final_result.stop_decision.to(dtype=torch.bool) | arm_exhaustion_exit ) newly_finished_t = active_emission_mask_t & termination_now_t generated_lengths_t = torch.where( newly_finished_t, emission_index_t.expand_as(generated_lengths_t) + 1, generated_lengths_t, ) if terminal_state is None: terminal_state = active_state terminal_stop_scores_t = final_result.stop_scores terminal_stop_reason_t = final_result.stop_reason terminal_stop_probability_t = final_result.stop_probability terminal_stop_decision_t = final_result.stop_decision terminal_decode_authority_t = final_result.decode_stop_authority terminal_native_probability_t = final_result.parent_native_stop_probability terminal_native_decision_t = final_result.parent_native_stop_decision terminal_trajectory_initialized_t = ( final_result.generation_trajectory_initialized ) terminal_native_progress_t = final_result.generation_native_progress terminal_task_progress_t = final_result.generation_task_progress terminal_arm_exhausted_t = final_result.generation_arm_exhausted terminal_delegation_exit_t = final_result.generation_delegation_exit else: terminal_state = _masked_correction_state_rows( terminal_state, active_state, newly_finished_t, ) assert terminal_stop_scores_t is not None assert terminal_stop_reason_t is not None assert terminal_stop_probability_t is not None assert terminal_stop_decision_t is not None assert terminal_decode_authority_t is not None assert terminal_native_probability_t is not None assert terminal_native_decision_t is not None assert terminal_trajectory_initialized_t is not None assert terminal_native_progress_t is not None assert terminal_task_progress_t is not None assert terminal_arm_exhausted_t is not None assert terminal_delegation_exit_t is not None terminal_stop_scores_t = _masked_batch_rows( terminal_stop_scores_t, final_result.stop_scores, newly_finished_t, ) terminal_stop_reason_t = _masked_batch_rows( terminal_stop_reason_t, final_result.stop_reason, newly_finished_t, ) terminal_stop_probability_t = _masked_batch_rows( terminal_stop_probability_t, final_result.stop_probability, newly_finished_t, ) terminal_stop_decision_t = _masked_batch_rows( terminal_stop_decision_t, final_result.stop_decision, newly_finished_t, ) terminal_decode_authority_t = _masked_batch_rows( terminal_decode_authority_t, final_result.decode_stop_authority, newly_finished_t, ) terminal_native_probability_t = _masked_batch_rows( terminal_native_probability_t, final_result.parent_native_stop_probability, newly_finished_t, ) terminal_native_decision_t = _masked_batch_rows( terminal_native_decision_t, final_result.parent_native_stop_decision, newly_finished_t, ) terminal_trajectory_initialized_t = _masked_batch_rows( terminal_trajectory_initialized_t, final_result.generation_trajectory_initialized, newly_finished_t, ) terminal_native_progress_t = _masked_batch_rows( terminal_native_progress_t, final_result.generation_native_progress, newly_finished_t, ) terminal_task_progress_t = _masked_batch_rows( terminal_task_progress_t, final_result.generation_task_progress, newly_finished_t, ) terminal_arm_exhausted_t = _masked_batch_rows( terminal_arm_exhausted_t, final_result.generation_arm_exhausted, newly_finished_t, ) terminal_delegation_exit_t = _masked_batch_rows( terminal_delegation_exit_t, final_result.generation_delegation_exit, newly_finished_t, ) if emission_observer is not None: emission_observer( detached_emission_trace_packet( rbo, result, final_result, active_state, next_token, emission_index_t, active_emission_mask_t, arm_exhaustion_exit, delegation_exit, ) ) prior_additive_probability = current_additive_probability.detach() prior_task_confidence = current_task_confidence.detach() finished_t = finished_t | termination_now_t trajectory_initialized = trajectory_initialized.new_ones( trajectory_initialized.shape ) emission_index_t = emission_index_t + 1 if bool( finished_t.detach().to(device="cpu").all() ): break if final_result is None or active_state is None or terminal_state is None: raise RuntimeError("confidence-owned generation produced no model attempt") if not all( value is not None for value in ( terminal_stop_scores_t, terminal_stop_reason_t, terminal_stop_probability_t, terminal_stop_decision_t, terminal_decode_authority_t, terminal_native_probability_t, terminal_native_decision_t, terminal_trajectory_initialized_t, terminal_native_progress_t, terminal_task_progress_t, terminal_arm_exhausted_t, terminal_delegation_exit_t, ) ): raise RuntimeError("confidence-owned generation lost terminal row state") assert terminal_stop_scores_t is not None assert terminal_stop_reason_t is not None assert terminal_stop_probability_t is not None assert terminal_stop_decision_t is not None assert terminal_decode_authority_t is not None assert terminal_native_probability_t is not None assert terminal_native_decision_t is not None assert terminal_trajectory_initialized_t is not None assert terminal_native_progress_t is not None assert terminal_task_progress_t is not None assert terminal_arm_exhausted_t is not None assert terminal_delegation_exit_t is not None final_result = replace( final_result, stop_scores=terminal_stop_scores_t, stop_reason=terminal_stop_reason_t, stop_probability=terminal_stop_probability_t, stop_decision=terminal_stop_decision_t, decode_stop_authority=terminal_decode_authority_t, parent_native_stop_probability=terminal_native_probability_t, parent_native_stop_decision=terminal_native_decision_t, generation_trajectory_initialized=terminal_trajectory_initialized_t, generation_native_progress=terminal_native_progress_t, generation_task_progress=terminal_task_progress_t, generation_arm_exhausted=terminal_arm_exhausted_t, generation_delegation_exit=terminal_delegation_exit_t, next_state=terminal_state, ) completed_state = RBOCorrectionState( prior_hidden=terminal_state.prior_hidden, outcome_features=terminal_state.outcome_features.new_zeros( terminal_state.outcome_features.shape ), parent_outcome_features=terminal_state.parent_outcome_features.new_zeros( terminal_state.parent_outcome_features.shape ), acquisition_action_probs=terminal_state.acquisition_action_probs.new_zeros( terminal_state.acquisition_action_probs.shape ), acquisition_action_index=terminal_state.acquisition_action_index.new_full( terminal_state.acquisition_action_index.shape, -1, ), acquisition_authority=terminal_state.acquisition_authority.new_zeros( terminal_state.acquisition_authority.shape ), outcome_present=terminal_state.outcome_present.new_zeros( terminal_state.outcome_present.shape ), attempt_index=terminal_state.attempt_index + 1.0, traversal_state=replace( terminal_state.traversal_state, expert_selections=torch.zeros_like( terminal_state.traversal_state.expert_selections, ), ), causal_world_state=terminal_state.causal_world_state, ) return RBOGenerationResult( token_ids=ids, generated_lengths_t=generated_lengths_t, termination_mask_t=finished_t, next_state=completed_state, final_result=final_result, ) def force_trained_confidence_stop(rbo: ResynthesisRBO) -> None: """Bias the learned sigmoid gates so confidence fires naturally. For tests that need termination without real training. This does NOT add a cap — it biases the LEARNED gates so the model's own confidence fires. """ with torch.no_grad(): rbo.stop_gate.stop_utility_gate.bias.fill_(5.0) rbo.stop_gate.stop_utility_gate.weight.fill_(0.0) rbo.task_confidence_head.bias.fill_(5.0) rbo.task_confidence_head.weight.fill_(0.0) # The same tensor-owned lifecycle must also decide that an external # experiment is unnecessary and that the draft is ready to submit. # This helper exists only for compact tests that need a deterministic # trained-stop fixture; production values are learned from outcomes. rbo.fabric.transfer_table[3].fill_(-8.0) rbo.fabric.transfer_table[9].fill_(8.0)