Layer 1.5: architecture-general neural existence proof

Re-scopes Layer 2 into a cheaper, architecture-general neural collapse proof
before the LLM rung. Realises the same Wright–Fisher abstractions in real trained
generative models on a fully-synthetic sandbox with an exact oracle, reusing
knowledge.metrics/truth/seeding and the output contract so neural curves overlay
the Layer-1 analytic curves.

  - src/neural/: synthetic token-grammar sandbox (lossless identity + stochastic
    style), ExactOracle, HistogramModel bridge, generation loop, experiment runner
  - HARD GATE passed: histogram lineage reproduces Layer 1 exactly (neutral decay,
    exact H_eq, tracks run_lineage) — tests/test_neural_validation.py
  - torch models: autoregressive RNN + MLP (VAE implemented, not yet fidelity-
    passing); determinism seeding derived from the SeedSequence stream
  - N0 bridge (neural g*=0.047 ≈ Layer-1 0.048), N1 collapse-in-weights, N2 phase
    boundary, N5 architecture-generality (collapse + grounding-rescue in histogram
    + RNN + MLP). Manifests/configs committed; parquet gitignored, hashes tracked
  - additive backward-compatible save_artifacts extension; Makefile neural targets

Finding: neural smoothing partially resists H-collapse, so forward-KL and tail
survival are the sharp neural collapse metrics (H is smooth, per Layer 1).

92 tests green. Remaining (tasks/todo.md): N4 merge, N2 refine, N3/N6, VAE
fidelity, MNIST tier, figures. LLM/LoRA rung and C3 deferred.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Giorgio Gilestro 2026-07-04 21:02:49 +01:00
parent 1721d047fa
commit 840b6b00b3
35 changed files with 3679 additions and 23 deletions

124
src/neural/config.py Normal file
View file

@ -0,0 +1,124 @@
"""Resolved run configuration for a neural (Layer 1.5) lineage.
Mirrors the ``knowledge.config`` idiom exactly: frozen dataclasses with a ``from_dict``
that fills defaults and rejects unknown keys via ``knowledge.config._sub``. The grounding,
re-mint, and metrics blocks are *reused verbatim* from ``knowledge.config`` so the neural
runner speaks the same schema as Layer 1 (grounding ``m``, the ``g -> m`` conversion, the
re-mint gate, and the KL/support floors are all identical). Only the data source
(``synthetic``) and the model (``model``) are neural-specific.
"""
from __future__ import annotations
import math
from dataclasses import dataclass, field, replace
from typing import Any, Mapping
from knowledge.config import GroundingCfg, MetricsCfg, RemintCfg, _sub
@dataclass(frozen=True)
class SyntheticCfg:
"""The fully-synthetic mode-truth and observation grammar.
The first seven fields are the Layer-1 ``TruthCfg`` knobs (they build ``p*`` over the
``K`` modes via ``knowledge.truth.make_true_distribution``). The remaining fields
define how a mode is rendered to a categorical token sequence: an *identity* segment
that encodes the mode losslessly (read by the exact oracle) followed by a *style*
segment of within-mode stochastic tokens (so a real generative model has a
distribution to learn, not just a lookup table).
"""
K: int
R: int = 1
tail: str = "zipf"
zipf_s: float = 1.1
tail_frac: float = 0.5
tail_threshold: float = 1e-3
init: str = "uniform" # initial p_0 over modes: {uniform, truth}
style_len: int = 4 # style-segment length (within-mode entropy)
style_vocab: int = 6 # style token alphabet size
id_base: int = 2 # identity segment encodes the mode in this base
@property
def id_len(self) -> int:
"""Identity-segment length: fewest base-``id_base`` digits to index ``K`` modes."""
if self.K <= 1:
return 1
return max(1, math.ceil(math.log(self.K, self.id_base)))
@property
def vocab(self) -> int:
"""Token alphabet size (shared by identity and style segments)."""
return max(self.id_base, self.style_vocab)
@property
def seq_len(self) -> int:
"""Total observation length in tokens."""
return self.id_len + self.style_len
@dataclass(frozen=True)
class ModelCfg:
"""The generative learner. ``kind`` selects the architecture behind a thin adapter.
Neural hyperparameters are ignored by the ``histogram`` bridge model.
"""
kind: str = "histogram" # {histogram, rnn, vae, mlp}
hidden: int = 64
embed: int = 16
epochs: int = 30
lr: float = 1.0e-3
batch_size: int = 256
device: str = "auto" # {auto, cpu, cuda}
latent: int = 16 # VAE latent dimension (VAE only)
beta: float = 1.0 # VAE KL weight (VAE only)
# Samples used to estimate a neural model's mode distribution by generate-and-classify
# (ignored by the exact histogram bridge). Larger -> less measurement noise on p_hat.
n_eval: int = 8000
@dataclass(frozen=True)
class NeuralDynamicsCfg:
"""Generational dynamics: drift strength ``n`` + reused grounding/re-mint blocks."""
n: int = 4000 # pupil training-sample size (drift strength ~ 1/n)
grounding: GroundingCfg = field(default_factory=GroundingCfg)
remint: RemintCfg = field(default_factory=RemintCfg)
@dataclass(frozen=True)
class NeuralLineageCfg:
"""A fully-resolved neural lineage configuration."""
synthetic: SyntheticCfg
model: ModelCfg = field(default_factory=ModelCfg)
dynamics: NeuralDynamicsCfg = field(default_factory=NeuralDynamicsCfg)
generations: int = 30
metrics: MetricsCfg = field(default_factory=MetricsCfg)
@staticmethod
def from_dict(cfg: Mapping[str, Any]) -> "NeuralLineageCfg":
"""Build a validated NeuralLineageCfg from a nested mapping, filling defaults."""
if isinstance(cfg, NeuralLineageCfg):
return cfg
synthetic = _sub(cfg.get("synthetic", {}), SyntheticCfg)
model = _sub(cfg.get("model", {}), ModelCfg)
dyn_raw = dict(cfg.get("dynamics", {}))
dynamics = NeuralDynamicsCfg(
n=dyn_raw.get("n", NeuralDynamicsCfg.n),
grounding=_sub(dyn_raw.get("grounding", {}), GroundingCfg),
remint=_sub(dyn_raw.get("remint", {}), RemintCfg),
)
metrics = _sub(cfg.get("metrics", {}), MetricsCfg)
return NeuralLineageCfg(
synthetic=synthetic,
model=model,
dynamics=dynamics,
generations=int(cfg.get("generations", NeuralLineageCfg.generations)),
metrics=metrics,
)
def replace(self, **kw) -> "NeuralLineageCfg":
return replace(self, **kw)