File size: 2,615 Bytes
05f5056
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import json
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))

from algo.config import ModelConfig, TrainingConfig
from algo.data import SimpleTokenizer, build_train_stream, synthetic_examples
from algo.eval import EvalScores, VerifierRepairLoop, strict_gate
from algo.rsi import RSIController
from algo.tools import build_envelope, execute_shell, execute_python, parse_envelope
try:
    import torch
    from algo.model import MiniTransformer
except ImportError as exc:
    raise RuntimeError("torch is required for smoke; install project dependencies") from exc

def main() -> None:
    cfg = ModelConfig()
    assert cfg.parameter_count_tied == 38_806_016
    assert cfg.parameter_count_untied == 47_194_624

    tokenizer = SimpleTokenizer()
    examples = synthetic_examples(count=2)
    stream = list(build_train_stream(tokenizer, examples, sequence_length=8, max_tokens=16))
    assert len(stream) == 2
    assert all(len(batch) == 8 for batch in stream)

    envelope = build_envelope("shell", {"ok": True, "output": "ok", "error": None, "exit_code": 0}, request_id="r1")
    assert parse_envelope(envelope)["request_id"] == "r1"
    assert execute_python("x = 2 + 3\nprint(x)").output.strip() == "5"
    assert not execute_shell("rm -rf /tmp/blocked").ok

    scores = EvalScores(math=0.72, coding=0.68, tool=0.86)
    assert strict_gate(scores)
    loop = VerifierRepairLoop(max_repairs=1)
    attempts: list[str] = []

    def verifier(answer: str) -> bool:
        attempts.append(answer)
        return len(attempts) == 2

    repaired, ok = loop.run("bad", verifier, repair=lambda answer, _feedback: answer + " repaired")
    assert ok
    assert attempts == ["bad", "bad repaired"]

    baseline = EvalScores(math=0.70, coding=0.65, tool=0.85)
    controller = RSIController(baseline)
    controller.apply(scores)
    assert controller.accept()
    controller.revert()
    assert not controller.accept()

    if torch is not None and MiniTransformer is not None:
        small = ModelConfig(num_layers=1, d_model=32, num_query_heads=4, num_kv_heads=2, ffn_dim=64, vocab_size=128, context_length=16)
        model = MiniTransformer(small)
        with torch.no_grad():
            logits = model(torch.randint(0, small.vocab_size, (1, 8)))
        assert tuple(logits.shape) == (1, 8, small.vocab_size)
        assert model.parameter_count == small.parameter_count_tied

    print(json.dumps({"status": "ok", "parameter_count_tied": cfg.parameter_count_tied}, sort_keys=True))


if __name__ == "__main__":
    main()