Download source/src/e_jepa_ttc/simplex_t/current_inputs.py from AlvaroSchwiedop/PHASE-TTC: direct link, hf CLI and curl.
- Browser
- Download file 6.64 kB
-
https://huggingface.co/AlvaroSchwiedop/PHASE-TTC/resolve/main/source/src/e_jepa_ttc/simplex_t/current_inputs.py
- Command line
-
hf download hf://AlvaroSchwiedop/PHASE-TTC/source/src/e_jepa_ttc/simplex_t/current_inputs.py
-
curl -L -o current_inputs.py https://huggingface.co/AlvaroSchwiedop/PHASE-TTC/resolve/main/source/src/e_jepa_ttc/simplex_t/current_inputs.py
6.64 kB
| """Verified current-only inputs; not an authorization to fit or invent histories.""" | |
| from __future__ import annotations | |
| import json | |
| from pathlib import Path | |
| from typing import Any | |
| import numpy as np | |
| import pandas as pd | |
| import torch | |
| from torch import Tensor | |
| from e_jepa_ttc.artifacts.simplex_t_preflight import sha256 | |
| from .cache import Normalizer, fit_normalizer, training_mass | |
| from .training import state_digest | |
| class CurrentQueries: | |
| """H1 gather with caller-validated current timing; no inferred temporal history.""" | |
| def __init__( | |
| self, table: dict[str, Any], normalizer: Normalizer, mass: np.ndarray, timing: np.ndarray | |
| ) -> None: | |
| arrays = table["arrays"] | |
| self.population = len(arrays["features17"]) | |
| if timing.shape != (self.population, 4) or not np.isfinite(timing).all(): | |
| raise ValueError("explicit current timing required") | |
| if mass.shape != (self.population,) or not np.isclose(mass.sum(), 1): | |
| raise ValueError("invalid current query mass") | |
| self.features = torch.from_numpy( | |
| ((arrays["features17"] - normalizer.mean) / normalizer.scale).astype(np.float32) | |
| ) | |
| self.experts = torch.from_numpy(arrays["expert_phase"].astype(np.float32)) | |
| self.truth = torch.from_numpy(arrays["target_phase"].astype(np.float32)) | |
| self.mass = torch.from_numpy(mass.astype(np.float32)) | |
| self.timing = torch.from_numpy(timing.astype(np.float32)) | |
| self.identity_sha256 = state_digest( | |
| { | |
| "table": table["reference"], | |
| "features": self.features, | |
| "experts": self.experts, | |
| "truth": self.truth, | |
| "mass": self.mass, | |
| "timing": self.timing, | |
| "mean": torch.from_numpy(normalizer.mean), | |
| "scale": torch.from_numpy(normalizer.scale), | |
| } | |
| ) | |
| def gather(self, query_ids: Tensor) -> tuple[Tensor, Tensor, Tensor, Tensor, Tensor, Tensor]: | |
| """Return one current observation per selected query, with original experts.""" | |
| ids = query_ids | |
| return ( | |
| self.features[ids, None], | |
| self.timing[ids, None], | |
| torch.ones(len(ids), 1, dtype=torch.bool), | |
| self.experts[ids], | |
| self.truth[ids], | |
| self.mass[ids], | |
| ) | |
| def load_current_inputs( | |
| root: Path, fold: int, role: str, *, ancestry_sha256: str, allowed_sequences: set[str] | |
| ) -> dict[str, Any]: | |
| """Validate table bytes and per-row frozen producer lineage before array use.""" | |
| if fold not in {0, 1, 2} or role not in {"inner_oof", "outer_dev"}: | |
| raise ValueError("unregistered current table") | |
| ancestry_path = root / "NESTED_ANCESTRY_AUDIT.json" | |
| if sha256(ancestry_path) != ancestry_sha256: | |
| raise ValueError("ancestry changed") | |
| ancestry = json.loads(ancestry_path.read_text()) | |
| stem = root / "tables" / f"outer{fold}_{role}" | |
| index = json.loads((root / "FROZEN_EXPERT_TABLE_INDEX.json").read_text()) | |
| entries = [row for row in index if row["outer_fold"] == fold and row["role"] == role] | |
| if len(entries) != 1 or entries[0]["ancestry_sha256"] != ancestry_sha256: | |
| raise ValueError("ambiguous or foreign table lineage") | |
| expected = entries[0] | |
| for suffix, key in ((".csv", "metadata_sha256"), (".npz", "arrays_sha256")): | |
| if sha256(stem.with_suffix(suffix)) != expected[key]: | |
| raise ValueError("table payload changed") | |
| columns = { | |
| "sample_token", | |
| "sequence_id", | |
| "target_ttc", | |
| "producer_outer_fold", | |
| "checkpoint_sha256", | |
| "pair_checkpoint_sha256", | |
| } | |
| if role == "inner_oof": | |
| columns.add("inner_fold") | |
| metadata = pd.read_csv(stem.with_suffix(".csv"), usecols=lambda name: name in columns) | |
| if set(metadata.columns) != columns or not metadata.sample_token.is_unique: | |
| raise ValueError("missing identity fields or duplicate query") | |
| if not set(metadata.sequence_id) <= allowed_sequences: | |
| raise ValueError("current input includes closed role") | |
| producers = {(p["outer_fold"], p["expert"], p["role"]): p for p in ancestry["producers"]} | |
| for row in metadata.to_dict("records"): | |
| family = f"inner{int(row['inner_fold'])}" if role == "inner_oof" else "outer_dev" | |
| if row["producer_outer_fold"] != fold: | |
| raise ValueError("wrong outer producer") | |
| a5 = producers[(fold, "A5", family)] | |
| pair = producers[(fold, "PAIR", family)] | |
| if pair["nested_a5_ancestor_sha256"] != a5["checkpoint_sha256"]: | |
| raise ValueError("PAIR teacher family mismatch") | |
| if row["pair_checkpoint_sha256"] != pair["checkpoint_sha256"]: | |
| raise ValueError("row uses another PAIR producer") | |
| for expert in ("A5", "C2F"): | |
| split = producers[(fold, expert, family)]["split_validation"] | |
| if ( | |
| row["sequence_id"] in split["train_sequence_ids"] | |
| or row["sequence_id"] not in split["dev_sequence_ids"] | |
| or split["ancestry_validated"] is not True | |
| ): | |
| raise ValueError("row not excluded from expert training ancestors") | |
| with np.load(stem.with_suffix(".npz"), allow_pickle=False) as archive: | |
| arrays = { | |
| name: archive[name] | |
| for name in ("features17", "expert_phase", "expert_ttc", "target_phase") | |
| } | |
| for name, value in arrays.items(): | |
| if list(value.shape) != expected["arrays"][name]["shape"] or not np.isfinite(value).all(): | |
| raise ValueError("current array schema/finite failure") | |
| if not np.array_equal( | |
| arrays["features17"][:, 8:11].astype(np.float32), | |
| arrays["expert_phase"].astype(np.float32), | |
| ): | |
| raise ValueError("RISK17 expert anchor columns disagree") | |
| return { | |
| "metadata": metadata, | |
| "arrays": arrays, | |
| "reference": expected, | |
| "role": role, | |
| "scientific_ready": False, | |
| } | |
| def normalize_current_train(table: dict[str, Any]) -> tuple[Normalizer, np.ndarray]: | |
| """Use unique TRAIN rows and recomputed registered weighting, never dev.""" | |
| if table["role"] != "inner_oof": | |
| raise ValueError("normalizer and training weights require inner_oof TRAIN") | |
| features = table["arrays"]["features17"] | |
| ids = np.arange(len(features), dtype=np.int64) | |
| normalizer = fit_normalizer(features, ids, np.ones(len(features), dtype=bool)) | |
| metadata = table["metadata"] | |
| mass = training_mass(metadata.target_ttc.to_numpy(), metadata.sequence_id.to_numpy()) | |
| return normalizer, mass | |