File size: 11,678 Bytes
53a1159
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
"""Contracts affecting truth, end-to-end text, scoring and paired depth initialization."""

import copy
import json

import numpy as np
import pytest
import torch
from torch.nn import functional as F

from llm_memory_editability.depth_step import (
    BOS,
    EOS,
    EVALUATION_SPLITS,
    TRAIN_SPLITS,
    _validate_output,
    audit_world,
    build_world,
    construct,
    data_digest,
    evaluate,
    generate_rows,
    pack_rows,
    prompt_rows,
    run_name,
    truth_path_details,
)
from llm_memory_editability.grok_depth import EpochStream, SmallGPT
from llm_memory_editability.grok_loop_data import build_world as old_world


@pytest.fixture(scope="module")
def spec():
    return {
        "world_seed": 751011,
        "entities": 64,
        "relations": 4,
        "degree": 4,
        "phi": 4.0,
        "id_fraction": 0.75,
        "id_test_fraction": 0.2,
        "evaluation_size": 512,
        "width": 32,
        "heads": 4,
        "layers": 2,
        "repeats": 1,
        "dropout": 0.0,
        "initialization": 752011,
        "stream_seed": 753011,
        "batch_size": 128,
        "steps": 64000,
    }


@pytest.fixture(scope="module")
def world(spec):
    return build_world(spec)


def test_three_lengths_share_exact_graph_id_mask_and_full_reserved_pools(spec, world):
    report = audit_world(world)
    assert report["counts"] == {
        "atomic": 256,
        "train_2": 444,
        "familiar_2": 124,
        "strict_2": 64,
        "train_3": 768,
        "familiar_3": 329,
        "strict_3": 59,
        "train_4": 768,
        "familiar_4": 1025,
        "strict_4": 53,
    }
    for hops in (2, 3, 4):
        source = old_world(dict(spec, hops=hops))
        np.testing.assert_array_equal(world["atomic"], source["atomic"] + 1)
        np.testing.assert_array_equal(world[f"familiar_{hops}"], source["test_full_composite"] + 1)
        np.testing.assert_array_equal(world[f"strict_{hops}"], source["ood_composite"] + 1)
        source_ids = set(map(tuple, source["id_atomic"]))
        expected = [tuple(row) in source_ids for row in source["atomic"]]
        assert world["metadata"]["id_mask"] == expected
        assert (
            world["metadata"]["source_worlds"][str(hops)]["dataset_sha256"]
            == source["metadata"]["dataset_sha256"]
        )
    assert world["metadata"]["source_token_shift"] == 1
    assert world["metadata"]["source_worlds"]["2"]["train_composite_capped"]


def test_truth_edges_and_strict_facts_never_used_at_any_training_length(world):
    trained = set()
    lookup = {(int(h), int(r)): int(t) for h, r, t in world["atomic"]}
    for hops in (2, 3, 4):
        rows = world[f"train_{hops}"]
        nodes, edges = truth_path_details(world, rows)
        assert nodes.shape == (len(rows), hops + 1)
        assert edges.shape == (len(rows), hops)
        trained.update(edges.ravel())
        for i, row in enumerate(rows):
            head = int(row[0])
            for j, relation in enumerate(row[1:-1]):
                head = lookup[head, int(relation)]
                assert head == nodes[i, j + 1]
            assert head == row[-1]
    for hops in (2, 3, 4):
        strict = truth_path_details(world, world[f"strict_{hops}"])[1]
        assert not set(strict.ravel()) & trained


@pytest.mark.parametrize("change", ["truth", "id_mask", "overlap", "duplicate"])
def test_invalid_data_contracts_are_rejected(world, change):
    damaged = copy.deepcopy(world)
    if change == "truth":
        tail = damaged["familiar_3"][0, -1]
        damaged["familiar_3"][0, -1] = 3 + (tail - 3 + 1) % 64
    elif change == "id_mask":
        edge = truth_path_details(damaged, damaged["train_2"][:1])[1][0, 0]
        damaged["metadata"]["id_mask"][edge] = False
    elif change == "overlap":
        damaged["familiar_2"][0] = damaged["train_2"][0]
    else:
        damaged["train_4"][1] = damaged["train_4"][0]
    with pytest.raises(ValueError):
        audit_world(damaged)


def test_frozen_digest_enforced_and_architecture_does_not_change_world(spec, world):
    digest = data_digest(world)
    for layers, repeats in ((1, 4), (6, 1)):
        actual = build_world(dict(spec, layers=layers, repeats=repeats, frozen_data_sha256=digest))
        assert data_digest(actual) == digest
    with pytest.raises(ValueError, match="frozen"):
        build_world(dict(spec, frozen_data_sha256="invalid"))


def test_compact_prompt_has_separator_full_token_ce_and_no_intermediate_labels(world):
    separator = world["metadata"]["separator_token"]
    assert separator == 71
    assert world["metadata"]["vocab_size"] == 72
    for split in TRAIN_SPLITS:
        rows = world[split][:3]
        prompts = prompt_rows(rows, separator)
        assert np.all(prompts[:, 0] == BOS)
        assert np.all(prompts[:, -1] == separator)
        np.testing.assert_array_equal(prompts[:, 1:-1], rows[:, :-1])
        tokens, labels = pack_rows(rows, separator=separator)
        length = rows.shape[1] + 2
        expected = np.c_[
            rows[:, :-1], np.full(len(rows), separator), rows[:, -1], np.full(len(rows), EOS)
        ]
        np.testing.assert_array_equal(labels[:, :length], expected)
        assert np.all(labels[:, length:] == -100)
        assert np.all(tokens[:, length:] == 0)
        assert not (labels == BOS).any()
    assert len(pack_rows(world["train_4"][:1])[0][0]) == 8
    assert sum(int((pack_rows(world[key][:1])[1] >= 0).sum()) for key in TRAIN_SPLITS) * 32 == 832
    with pytest.raises(ValueError):
        pack_rows(world["train_4"][:1], sequence=7)


def test_paired_initialization_all_architectures_and_effective_depth(spec):
    torch.set_num_threads(1)
    architectures = [(layers, 1) for layers in (1, 2, 3, 4, 6)] + [(1, r) for r in (2, 3, 4, 6)]
    models = {
        (layers, r): construct(dict(spec, layers=layers, repeats=r), "cpu")
        for layers, r in architectures
    }
    reference = models[1, 1]
    for (layers, repeats), model in models.items():
        assert len(model.blocks) == layers and model.effective_depth == layers * repeats
        assert all(p.requires_grad for p in model.parameters())
        for name in ("token.weight", "position.weight", "ln_final.weight", "ln_final.bias"):
            torch.testing.assert_close(
                model.state_dict()[name], reference.state_dict()[name], rtol=0, atol=0
            )
    for depth in (2, 3, 4, 6):
        ordinary, loop = models[depth, 1], models[1, depth]
        for key, value in ordinary.blocks[0].state_dict().items():
            torch.testing.assert_close(value, loop.blocks[0].state_dict()[key], rtol=0, atol=0)
    assert not torch.equal(
        models[2, 1].blocks[0].mlp.up.weight, models[2, 1].blocks[1].mlp.up.weight
    )
    assert len({run_name(dict(spec, layers=layers, repeats=r)) for layers, r in architectures}) == 9


def test_standard_full_forward_causal_and_all_parameters_train(spec, world):
    torch.set_num_threads(1)
    model = construct(spec, "cpu").eval()
    ordinary = SmallGPT(model.config, dropout=0.0).eval()
    ordinary.load_state_dict(model.state_dict())
    tokens = torch.as_tensor(pack_rows(world["train_4"][:2])[0])
    logits = model(tokens)
    torch.testing.assert_close(logits, ordinary(tokens), rtol=0, atol=0)
    assert logits.shape == (2, 8, 72)
    changed = tokens.clone()
    changed[:, 5:] = 3
    torch.testing.assert_close(model(changed)[:, :5], logits[:, :5], rtol=0, atol=0)
    labels = torch.as_tensor(pack_rows(world["train_4"][:2])[1])
    F.cross_entropy(logits.flatten(0, 1), labels.flatten(), ignore_index=-100).backward()
    for param in (
        model.token.weight,
        model.position.weight,
        model.blocks[0].attention.qkv.weight,
        model.blocks[0].mlp.up.weight,
        model.blocks[0].mlp.down.weight,
    ):
        assert (
            param.grad is not None
            and torch.isfinite(param.grad).all()
            and param.grad.abs().sum() > 0
        )


class TruthOracle(torch.nn.Module):
    def __init__(self, world, wrong_eos=False):
        super().__init__()
        self.lookup = {(int(h), int(r)): int(t) for h, r, t in world["atomic"]}
        self.separator = world["metadata"]["separator_token"]
        self.vocab = world["metadata"]["vocab_size"]
        self.wrong_eos = wrong_eos

    def forward(self, tokens):
        logits = torch.full((*tokens.shape, self.vocab), -20.0, device=tokens.device)
        for i, row in enumerate(tokens.tolist()):
            if row[-1] == self.separator:
                current = row[1]
                for relation in row[2:-1]:
                    current = self.lookup.get((current, relation), 3)
                result = current
            else:
                result = 0 if self.wrong_eos else EOS
            logits[i, -1, result] = 20.0
        return logits


def test_free_tail_eos_coverage_and_autonomous_truth_scoring(world):
    oracle = TruthOracle(world)
    metrics, predictions = evaluate(oracle, world, "cpu")
    assert oracle.training
    for name in EVALUATION_SPLITS:
        assert metrics[name]["accuracy"] == metrics[name]["answer_accuracy"] == 1.0
        assert predictions[name + "_correct"].all()
        if name != "atomic":
            assert metrics[name]["atomic_correct_coverage"] == 1.0
            assert metrics[name]["conditional_accuracy"] == 1.0
            assert metrics[name]["autonomous_two_calls"] == 1.0
            assert metrics[name]["autonomous_path_accuracy"] == 1.0
    metrics, _ = evaluate(TruthOracle(world, wrong_eos=True), world, "cpu")
    assert metrics["atomic"]["answer_accuracy"] == 1.0
    assert metrics["atomic"]["accuracy"] == 0.0
    assert metrics["familiar_4"]["conditional_accuracy"] is None
    assert metrics["familiar_4"]["autonomous_two_calls"] == 0.0


def test_empty_pools_never_report_nan(world):
    empty = dict(world)
    empty["strict_4"] = np.empty((0, 6), dtype=np.int64)
    metrics, predictions = evaluate(TruthOracle(world), empty, "cpu")
    assert metrics["strict_4"]["n"] == 0
    assert metrics["strict_4"]["accuracy"] is None
    assert metrics["strict_4"]["atomic_correct_coverage"] is None
    assert predictions["strict_4_autonomous_generated"].shape == (0, 4, 2)
    json.dumps(metrics, allow_nan=False)
    task, prediction = generate_rows(TruthOracle(world), empty["strict_4"], "cpu")
    assert task["answer_nll"] is None and prediction["generated"].shape == (0, 2)


def test_epoch_stream_batches_and_saved_continuation_match_across_architectures(world, spec):
    for i, name in enumerate(TRAIN_SPLITS):
        first = EpochStream(len(world[name]), spec["stream_seed"] + i)
        second = EpochStream(len(world[name]), spec["stream_seed"] + i)
        np.testing.assert_array_equal(
            first.take(2048).reshape(64, 32), second.take(2048).reshape(64, 32)
        )
        restored = EpochStream(len(world[name]), 0)
        restored.load_state_dict(first.state_dict())
        np.testing.assert_array_equal(first.take(32), restored.take(32))


@pytest.mark.parametrize(
    "artifact",
    [
        "latest.pt",
        "complete.json",
        "world.npz",
        "spec.json",
        "predictions-000000.npz",
        "exposures.npz",
    ],
)
def test_attempt_artifacts_never_overwritten_but_scheduler_files_allowed(tmp_path, artifact):
    (tmp_path / "input-spec.json").write_text("{}")
    (tmp_path / "train-process.log").write_text("scheduler")
    _validate_output(tmp_path)
    (tmp_path / artifact).write_bytes(b"existing experiment")
    with pytest.raises(FileExistsError):
        _validate_output(tmp_path)
    assert (tmp_path / artifact).read_bytes() == b"existing experiment"