|
| 1 | +"""Typed structural proposals for isolated goal-gated master candidates. |
| 2 | +
|
| 3 | +No source is imported on the controller. Native GGUF weights are never relabeled |
| 4 | +as another architecture. Warm growth is limited to compatible typed predecessors. |
| 5 | +""" |
| 6 | + |
| 7 | +import ast |
| 8 | +import json |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +from rlm.v100.activity import ActivityLog |
| 12 | +from rlm.v100.architectures import candidate_path, create_candidate, verify_candidate |
| 13 | +from rlm.v100.goals import load_goal |
| 14 | +from rlm.v100.protection import file_hash |
| 15 | + |
| 16 | +MODEL_SOURCE = """ |
| 17 | +import torch |
| 18 | +from torch import nn |
| 19 | +
|
| 20 | +class MorphModel(nn.Module): |
| 21 | + def __init__(self, config): |
| 22 | + super().__init__() |
| 23 | + s = MORPH_SHAPE |
| 24 | + w = s['width'] |
| 25 | + self.embedding = nn.Embedding(257, w) |
| 26 | + if s['architecture'] == 'gru': |
| 27 | + self.body = nn.GRU(w, w, s['layers'], batch_first=True) |
| 28 | + else: |
| 29 | + self.body = nn.TransformerEncoder( |
| 30 | + nn.TransformerEncoderLayer(w, s['heads'], w * 2, dropout=0, batch_first=True), |
| 31 | + s['layers'], enable_nested_tensor=False) |
| 32 | + self.head = nn.Linear(w, 257) |
| 33 | + self.residual = nn.ModuleList([ |
| 34 | + nn.Sequential(nn.Linear(257, s['bottleneck']), nn.GELU(), nn.Linear(s['bottleneck'], 257)) |
| 35 | + for _ in range(s['residual_layers'])]) |
| 36 | + for block in self.residual: |
| 37 | + nn.init.zeros_(block[-1].weight) |
| 38 | + nn.init.zeros_(block[-1].bias) |
| 39 | +
|
| 40 | + def load_parent_state(self, state): |
| 41 | + old = MORPH_SHAPE['parent_shape'] |
| 42 | + if old is None: |
| 43 | + raise ValueError('No compatible morphology predecessor') |
| 44 | + expected = self.state_dict() |
| 45 | + old_blocks = old['residual_layers'] |
| 46 | + allowed_missing = {k for k in expected if k.startswith('residual.') and int(k.split('.')[1]) >= old_blocks} |
| 47 | + if set(state) != set(expected) - allowed_missing: |
| 48 | + raise ValueError('Parent keys differ from the frozen growth contract') |
| 49 | + if any(expected[k].shape != value.shape for k, value in state.items()): |
| 50 | + raise ValueError('Parent shape differs from the frozen growth contract') |
| 51 | + self.load_state_dict(state, strict=False) |
| 52 | + self.requires_grad_(False) |
| 53 | + for block in list(self.residual)[old_blocks:]: |
| 54 | + block.requires_grad_(True) |
| 55 | + self.embedding.eval() |
| 56 | + self.body.eval() |
| 57 | + self.head.eval() |
| 58 | +
|
| 59 | + def forward(self, tokens): |
| 60 | + value = self.embedding(tokens) |
| 61 | + if MORPH_SHAPE['architecture'] == 'gru': |
| 62 | + value, _ = self.body(value) |
| 63 | + else: |
| 64 | + # Parameter-free positions preserve the vocabulary and avoid a huge |
| 65 | + # learned positional table when the parent has a long context. |
| 66 | + positions = torch.arange(tokens.shape[1], device=tokens.device).float() |
| 67 | + scale = torch.arange(value.shape[-1], device=tokens.device).float() + 1 |
| 68 | + value = value + torch.sin(positions[:, None] / scale[None, :]).to(value.dtype) |
| 69 | + mask = torch.triu(torch.ones(tokens.shape[1], tokens.shape[1], device=tokens.device, dtype=torch.bool), 1) |
| 70 | + value = self.body(value, mask=mask) |
| 71 | + value = self.head(value) |
| 72 | + for block in self.residual: |
| 73 | + value = value + block(value) |
| 74 | + return value |
| 75 | +
|
| 76 | +def build(config): |
| 77 | + return MorphModel(config) |
| 78 | +""" |
| 79 | + |
| 80 | + |
| 81 | +def read_shape(source: Path) -> dict | None: |
| 82 | + if source.is_symlink() or source.stat().st_size > 12000: |
| 83 | + raise ValueError("Morphology source exceeds its frozen budget") |
| 84 | + for node in ast.parse(source.read_text()).body: |
| 85 | + if isinstance(node, ast.Assign) and any( |
| 86 | + isinstance(t, ast.Name) and t.id == "MORPH_SHAPE" for t in node.targets |
| 87 | + ): |
| 88 | + value = ast.literal_eval(node.value) |
| 89 | + if not isinstance(value, dict) or value.get("schema") != "synta-morphology-v1": |
| 90 | + raise ValueError("Invalid morphology shape contract") |
| 91 | + return value |
| 92 | + return None |
| 93 | + |
| 94 | + |
| 95 | +def propose( |
| 96 | + root: Path, |
| 97 | + branch: str, |
| 98 | + candidate_id: str, |
| 99 | + architecture: str, |
| 100 | + width: int, |
| 101 | + layers: int, |
| 102 | + heads: int, |
| 103 | + residual_layers: int, |
| 104 | + bottleneck: int, |
| 105 | + parent_candidate_id: str, |
| 106 | + hypothesis: str, |
| 107 | +) -> dict: |
| 108 | + goal = load_goal(root) |
| 109 | + if not goal: |
| 110 | + raise ValueError("Morphology needs the recorded operator goal") |
| 111 | + if architecture not in ("gru", "transformer") or any( |
| 112 | + type(v) is not int for v in (width, layers, heads, residual_layers, bottleneck) |
| 113 | + ): |
| 114 | + raise ValueError("Invalid morphology dimensions") |
| 115 | + if not ( |
| 116 | + 16 <= width <= 256 |
| 117 | + and 1 <= layers <= 4 |
| 118 | + and heads in (1, 2, 4, 8) |
| 119 | + and width % heads == 0 |
| 120 | + and 0 <= residual_layers <= 4 |
| 121 | + and 1 <= bottleneck <= 256 |
| 122 | + ): |
| 123 | + raise ValueError("Morphology dimensions exceed the bounded search space") |
| 124 | + # Conservative bound, before allocating a tensor on any worker. |
| 125 | + estimate = ( |
| 126 | + 257 * width * 2 |
| 127 | + + layers * width * width * 12 |
| 128 | + + residual_layers * (514 * bottleneck + 257 + bottleneck) |
| 129 | + ) |
| 130 | + if estimate > 2_000_000: |
| 131 | + raise ValueError("Morphology exceeds two million pilot parameters") |
| 132 | + parent_shape, parent_weights_hash = None, None |
| 133 | + if parent_candidate_id: |
| 134 | + parent = candidate_path(root, parent_candidate_id) |
| 135 | + manifest = verify_candidate(parent) |
| 136 | + parent_shape = read_shape(parent / "source/model.py") |
| 137 | + if manifest["goal"] != goal or parent_shape is None: |
| 138 | + raise ValueError("Warm growth requires a typed predecessor for the same goal") |
| 139 | + if ( |
| 140 | + any( |
| 141 | + parent_shape[key] != value |
| 142 | + for key, value in { |
| 143 | + "architecture": architecture, |
| 144 | + "width": width, |
| 145 | + "layers": layers, |
| 146 | + "heads": heads, |
| 147 | + "bottleneck": bottleneck, |
| 148 | + }.items() |
| 149 | + ) |
| 150 | + or residual_layers <= parent_shape["residual_layers"] |
| 151 | + ): |
| 152 | + raise ValueError( |
| 153 | + "Warm growth can append residual layers; other shape changes need an independent candidate" |
| 154 | + ) |
| 155 | + weights = parent / "trial/weights/weights.safetensors" |
| 156 | + if weights.is_symlink() or not weights.is_file() or weights.stat().st_size > 16 * 2**20: |
| 157 | + raise ValueError("Warm growth requires bounded trained predecessor weights") |
| 158 | + parent_weights_hash = file_hash(weights) |
| 159 | + parent_shape = {key: value for key, value in parent_shape.items() if key != "parent_shape"} |
| 160 | + shape = { |
| 161 | + "schema": "synta-morphology-v1", |
| 162 | + "goal_id": goal["id"], |
| 163 | + "architecture": architecture, |
| 164 | + "width": width, |
| 165 | + "layers": layers, |
| 166 | + "heads": heads, |
| 167 | + "residual_layers": residual_layers, |
| 168 | + "bottleneck": bottleneck, |
| 169 | + "parent_candidate_id": parent_candidate_id, |
| 170 | + "parent_weights_sha256": parent_weights_hash, |
| 171 | + "parent_shape": parent_shape, |
| 172 | + } |
| 173 | + result = create_candidate( |
| 174 | + root, branch, candidate_id, "MORPH_SHAPE = " + repr(shape) + "\n" + MODEL_SOURCE, hypothesis |
| 175 | + ) |
| 176 | + result.update( |
| 177 | + shape=shape, |
| 178 | + estimated_parameters=estimate, |
| 179 | + weights_changed=False, |
| 180 | + next_tools=["test_submodel", "propose_scratch_master"], |
| 181 | + scope="Isolated structural candidate; master activation requires strict measured goal improvement and all retained/fresh/public gates", |
| 182 | + ) |
| 183 | + ActivityLog(root, branch, "morphology").write("decisions", "morphology-proposed", result) |
| 184 | + return result |
| 185 | + |
| 186 | + |
| 187 | +def initialization(root: Path, candidate_id: str) -> tuple[Path | None, bool]: |
| 188 | + folder = candidate_path(root, candidate_id) |
| 189 | + verify_candidate(folder) |
| 190 | + shape = read_shape(folder / "source/model.py") |
| 191 | + if shape is None or not shape["parent_candidate_id"]: |
| 192 | + return None, False |
| 193 | + parent = candidate_path(root, shape["parent_candidate_id"]) |
| 194 | + verify_candidate(parent) |
| 195 | + weights = parent / "trial/weights/weights.safetensors" |
| 196 | + if weights.is_symlink() or file_hash(weights) != shape["parent_weights_sha256"]: |
| 197 | + raise ValueError("Morphology predecessor weights changed") |
| 198 | + return weights, True |
| 199 | + |
| 200 | + |
| 201 | +def status(root: Path) -> dict: |
| 202 | + values = [] |
| 203 | + for folder in sorted((root / "research/architecture-candidates").glob("*"))[-64:]: |
| 204 | + verify_candidate(folder) |
| 205 | + shape = read_shape(folder / "source/model.py") |
| 206 | + if shape is not None: |
| 207 | + report = folder / "trial/quality.json" |
| 208 | + values.append( |
| 209 | + { |
| 210 | + "candidate_id": folder.name, |
| 211 | + "shape": shape, |
| 212 | + "trial": json.loads(report.read_text()) if report.exists() else None, |
| 213 | + } |
| 214 | + ) |
| 215 | + return { |
| 216 | + "candidates": values, |
| 217 | + "scope": "Shape proposals and tested artifacts; not deployment claims", |
| 218 | + } |
0 commit comments