File size: 1,454 Bytes
f95cbd4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
import pytest
import torch
from celestis_rl.training import TrainConfig, run_bandit, run_tiny_lm, load_tiny_weights


def test_small_bandit_reproducible_and_improves():
    cfg = TrainConfig(steps=30, batch_size=16, replay_capacity=64, learning_rate=0.1)
    a, b = run_bandit(cfg, vocabulary=8), run_bandit(cfg, vocabulary=8)
    assert a["history"] == b["history"]
    assert a["final_reward"] > a["initial_reward"]


def test_exact_gate_history_non_decreasing():
    r = run_bandit(TrainConfig(steps=25, batch_size=8, learning_rate=0.4), vocabulary=8, gate=True)
    x = [h["exact_expected_reward"] for h in r["history"]]
    assert np.all(np.diff(x) >= 0)


def test_tiny_train_and_safe_numeric_checkpoint(tmp_path):
    cfg = TrainConfig(steps=2, batch_size=4, learning_rate=0.002)
    r = run_tiny_lm(cfg, output=tmp_path)
    model = load_tiny_weights(tmp_path)
    assert torch.isfinite(model(torch.tensor([[2, 3, 3]]))).all()
    assert 0 <= r["final_greedy_accuracy"] <= 1


def test_dense_selected_training_agree_short_run():
    cfg = TrainConfig(steps=3, batch_size=4, learning_rate=0.002)
    a = run_tiny_lm(cfg, selected_backward=True)
    b = run_tiny_lm(cfg, selected_backward=False)
    assert a["history"] == b["history"]


@pytest.mark.parametrize("kw", [{"steps":0}, {"mode":"fake"}, {"beta":-1}, {"learning_rate":0}])
def test_invalid_config(kw):
    with pytest.raises(ValueError): TrainConfig(**kw).validate()