Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/neural-fabric.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
python-version: "3.12"

- name: Install numerical stack
run: python -m pip install --upgrade pip pytest numpy scipy scikit-learn matplotlib
run: python -m pip install --upgrade pip pytest numpy scipy scikit-learn matplotlib jsonschema

- name: Run Neural Fabric CI
run: make -f mk/neural-fabric.mk neural-fabric-ci
12 changes: 5 additions & 7 deletions mk/neural-fabric.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@ neural-fabric-static:
python3 -m json.tool schemas/neural-fabric/model-family.v1.json >/dev/null
python3 -m json.tool schemas/neural-fabric/targeting-experiment.v1.json >/dev/null
python3 -m json.tool schemas/neural-fabric/targeting-result.v1.json >/dev/null
python3 -m json.tool schemas/neural-fabric/intervention-event.v1.json >/dev/null
python3 -m compileall packages/superconscious-core/superconscious_core/neural_fabric scripts research/activation-time-targeting/code >/dev/null

# NOTE: neural-fabric-smoke / neural-fabric-full depend on the results-validation
# and experiment-harness that are not yet committed (scripts/validate-neural-fabric-results.py,
# scripts/check-capacity-bounds.py, tests/neural_fabric, research/.../run_suite.py). They are
# kept here as the roadmap for that harness and are intentionally NOT part of neural-fabric-ci
# until those files land, so CI gates only what this module actually delivers.
neural-fabric-smoke:
python3 scripts/validate-neural-fabric-results.py research/activation-time-targeting
python3 scripts/check-capacity-bounds.py --m 60 --C 0.4 --s 0.1
python3 -m pytest tests/neural_fabric -q

# neural-fabric-full regenerates the committed reference results/events in place
# (deterministic) and re-validates them, so it doubles as a reproducibility check.
neural-fabric-full:
python3 research/activation-time-targeting/code/run_suite.py --out research/activation-time-targeting/results/generated
python3 research/activation-time-targeting/code/run_suite.py --out research/activation-time-targeting
python3 scripts/validate-neural-fabric-results.py research/activation-time-targeting

neural-fabric-ci: neural-fabric-static
neural-fabric-ci: neural-fabric-static neural-fabric-smoke
165 changes: 165 additions & 0 deletions research/activation-time-targeting/code/run_suite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#!/usr/bin/env python3
"""Deterministic, CPU-only activation-time targeting suite.

Each experiment exercises a Neural Fabric reference primitive and emits a
``targeting-result.v1`` document under ``<out>/results``. Interventions
additionally emit ``intervention-event.v1`` audit records under ``<out>/events``
-- the doctrine governs every intervention by an audit event. The experiments
are intentionally ``toy_model_confirmed``: they demonstrate that steering and
capacity monitoring work *without updating weights* (``weights_updated`` is
always ``false``), not that they are production-safe.

python3 research/activation-time-targeting/code/run_suite.py \
--out research/activation-time-targeting
"""
from __future__ import annotations

import argparse
import json
import pathlib
import sys

import numpy as np

_ROOT = pathlib.Path(__file__).resolve().parents[3]
sys.path.insert(0, str(_ROOT / "packages" / "superconscious-core"))

from superconscious_core.neural_fabric.hopfield import ( # noqa: E402
hopfield_retrieve,
logit_boost,
)
from superconscious_core.neural_fabric.may_wigner import ( # noqa: E402
classify_may_wigner,
may_wigner_number,
)

EPISTEMIC_STATUS = "toy_model_confirmed"


def experiment_hopfield_logit_boost() -> tuple[dict, list[dict]]:
"""NF-ATT-001: steer a Hopfield readout via logit boosting, no weight update."""
patterns = np.eye(4)
query = np.full(4, 0.25)
target_idx = 1
_, base_weights = hopfield_retrieve(patterns, query, beta=1.0)
base_prob = float(base_weights[target_idx])

rows: list[dict] = []
events: list[dict] = []
prev = -1.0
monotone = True
for strength in (0.0, 1.0, 2.0, 3.0):
dist = logit_boost(patterns, query, target_idx=target_idx, strength=strength, beta=1.0)
prob = float(dist[target_idx])
monotone = monotone and (prob >= prev - 1e-12)
prev = prob
rows.append({"strength": strength, "target_prob": round(prob, 6)})
if strength > 0.0:
events.append(
{
"event_id": f"NF-ATT-001-EV{len(events) + 1:03d}",
"experiment_id": "NF-ATT-001",
"intervention_type": "logit_boost",
"target_locus": "attention-logits",
"weights_updated": False,
"params": {"target_idx": target_idx, "strength": strength, "beta": 1.0},
"observed": {"target_prob": round(prob, 6)},
"epistemic_status": EPISTEMIC_STATUS,
}
)

boosted_prob = rows[-1]["target_prob"]
argmax_is_target = bool(
np.argmax(logit_boost(patterns, query, target_idx=target_idx, strength=3.0, beta=1.0)) == target_idx
)
result = {
"experiment_id": "NF-ATT-001",
"weights_updated": False,
"epistemic_status": EPISTEMIC_STATUS,
"claim_invariants": [
"weights_updated == false",
"intervention is activation-time only",
"target probability is monotone non-decreasing in boost strength",
],
"metrics": {
"baseline_target_prob": round(base_prob, 6),
"boosted_target_prob": boosted_prob,
"target_prob_lift": round(boosted_prob - base_prob, 6),
"argmax_is_target_after_boost": argmax_is_target,
"lift_is_monotone": bool(monotone),
},
"rows": rows,
}
return result, events


def experiment_may_wigner_sweep() -> tuple[dict, list[dict]]:
"""NF-ATT-002: sweep active-feature count and locate the stability boundaries."""
C, s = 0.4, 0.1
rows: list[dict] = []
boundaries: dict[str, int | None] = {"warn": None, "error": None, "stop": None}
max_ok_m = 0
for m in range(10, 401, 10):
value = may_wigner_number(m, C, s)
cls = classify_may_wigner(value)
if cls == "ok":
max_ok_m = m
for level in ("warn", "error", "stop"):
if cls == level and boundaries[level] is None:
boundaries[level] = m
rows.append({"m": m, "may_wigner": round(value, 6), "classification": cls})

result = {
"experiment_id": "NF-ATT-002",
"weights_updated": False,
"epistemic_status": EPISTEMIC_STATUS,
"claim_invariants": [
"weights_updated == false",
"control number is s*sqrt(m*C)",
"classification is monotone non-decreasing in m",
],
"metrics": {
"C": C,
"s": s,
"max_ok_m": max_ok_m,
"m_first_warn": boundaries["warn"] if boundaries["warn"] is not None else -1,
"m_first_error": boundaries["error"] if boundaries["error"] is not None else -1,
"m_first_stop": boundaries["stop"] if boundaries["stop"] is not None else -1,
},
"rows": rows,
}
return result, [] # capacity monitoring is observation, not intervention


EXPERIMENTS = (experiment_hopfield_logit_boost, experiment_may_wigner_sweep)


def _write(path: pathlib.Path, doc: dict) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(doc, indent=2, sort_keys=True) + "\n", encoding="utf-8")


def main(argv: list[str] | None = None) -> int:
p = argparse.ArgumentParser(description="Activation-time targeting suite")
p.add_argument(
"--out",
type=pathlib.Path,
default=_ROOT / "research" / "activation-time-targeting",
help="suite root; results/ and events/ are written beneath it",
)
args = p.parse_args(argv)

for build in EXPERIMENTS:
result, events = build()
exp_id = result["experiment_id"]
_write(args.out / "results" / f"{exp_id}.result.json", result)
print(f"[ok] {exp_id} result -> {args.out / 'results' / f'{exp_id}.result.json'}")
for event in events:
_write(args.out / "events" / f"{event['event_id']}.event.json", event)
if events:
print(f"[ok] {exp_id} emitted {len(events)} intervention event(s)")
return 0


if __name__ == "__main__":
raise SystemExit(main())
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"epistemic_status": "toy_model_confirmed",
"event_id": "NF-ATT-001-EV001",
"experiment_id": "NF-ATT-001",
"intervention_type": "logit_boost",
"observed": {
"target_prob": 0.475367
},
"params": {
"beta": 1.0,
"strength": 1.0,
"target_idx": 1
},
"target_locus": "attention-logits",
"weights_updated": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"epistemic_status": "toy_model_confirmed",
"event_id": "NF-ATT-001-EV002",
"experiment_id": "NF-ATT-001",
"intervention_type": "logit_boost",
"observed": {
"target_prob": 0.711235
},
"params": {
"beta": 1.0,
"strength": 2.0,
"target_idx": 1
},
"target_locus": "attention-logits",
"weights_updated": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"epistemic_status": "toy_model_confirmed",
"event_id": "NF-ATT-001-EV003",
"experiment_id": "NF-ATT-001",
"intervention_type": "logit_boost",
"observed": {
"target_prob": 0.870049
},
"params": {
"beta": 1.0,
"strength": 3.0,
"target_idx": 1
},
"target_locus": "attention-logits",
"weights_updated": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"experiment_id": "NF-ATT-001",
"title": "Hopfield logit-boost steering without weight update",
"model_family": "hopfield-associative",
"target_locus": "attention-logits",
"intervention_type": "logit_boost",
"weights_updated": false,
"seed_policy": "deterministic-fixed-inputs",
"metrics": ["baseline_target_prob", "boosted_target_prob", "target_prob_lift", "lift_is_monotone"],
"artifacts": {
"results": ["results/NF-ATT-001.result.json"]
},
"claim_invariants": ["weights_updated == false", "intervention is activation-time only"],
"epistemic_status": "toy_model_confirmed"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"experiment_id": "NF-ATT-002",
"title": "May-Wigner capacity sweep over active-feature count",
"model_family": "hopfield-associative",
"target_locus": "feature-activation-density",
"intervention_type": "capacity_monitor",
"weights_updated": false,
"seed_policy": "deterministic-fixed-inputs",
"metrics": ["max_ok_m", "m_first_warn", "m_first_error", "m_first_stop"],
"artifacts": {
"results": ["results/NF-ATT-002.result.json"]
},
"claim_invariants": ["weights_updated == false", "control number is s*sqrt(m*C)"],
"epistemic_status": "toy_model_confirmed"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"family_id": "hopfield-associative",
"substrate": {
"default": "cpu-float64",
"allowed": ["cpu-float64", "cpu-float32"]
},
"structure": {
"class": "modern-hopfield",
"replay_invariant": "deterministic-given-inputs"
},
"target_loci": ["query-vector", "attention-logits", "feature-activation-density"],
"intervention_types": ["query_injection", "logit_boost", "capacity_monitor"],
"may_wigner": {"C": 0.4, "s": 0.1, "boundary": 1.0},
"epistemic_status": "toy_model_confirmed"
}
35 changes: 35 additions & 0 deletions research/activation-time-targeting/results/NF-ATT-001.result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"claim_invariants": [
"weights_updated == false",
"intervention is activation-time only",
"target probability is monotone non-decreasing in boost strength"
],
"epistemic_status": "toy_model_confirmed",
"experiment_id": "NF-ATT-001",
"metrics": {
"argmax_is_target_after_boost": true,
"baseline_target_prob": 0.25,
"boosted_target_prob": 0.870049,
"lift_is_monotone": true,
"target_prob_lift": 0.620049
},
"rows": [
{
"strength": 0.0,
"target_prob": 0.25
},
{
"strength": 1.0,
"target_prob": 0.475367
},
{
"strength": 2.0,
"target_prob": 0.711235
},
{
"strength": 3.0,
"target_prob": 0.870049
}
],
"weights_updated": false
}
Loading
Loading