File size: 3,740 Bytes
5f02c05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
import pytest
import torch

from hades.data.shards import ShardWriter, write_manifest
from hades.train.data import MixtureLoader, effective_mix

GROUP_TOKEN = {"security": 1, "general": 2, "logs": 3}


def make_corpus(root, sizes=None, tiny_shard=False):
    sizes = sizes or {"security": 5000, "general": 5000, "logs": 5000}
    groups = {}
    for g, n in sizes.items():
        w = ShardWriter(root / g, "train", 900, 901, shard_tokens=10**9)
        w.add([GROUP_TOKEN[g]] * n, n_bytes=n * 4)
        groups[g] = {"train": {"tokens": w.tokens, "bytes": w.bytes, "shards": w.close()}}
    if tiny_shard:
        w = ShardWriter(root / "logs", "tiny", 900, 901)
        w.add([7] * 8)
        groups["logs"]["train"]["shards"] += w.close()
    write_manifest(root, {"tokenizer_sha256": "x", "groups": groups, "sources": {}})
    return root


def test_effective_mix_uncapped():
    mix = effective_mix({"security": 0.4, "general": 0.4, "logs": 0.2},
                        {"security": 200e6, "general": 500e6, "logs": 150e6}, 1e9, {"security": 2})
    assert mix["security"] == pytest.approx(0.4)
    assert sum(mix.values()) == pytest.approx(1.0)


def test_effective_mix_capped():
    mix = effective_mix({"security": 0.4, "general": 0.4, "logs": 0.2},
                        {"security": 150e6, "general": 500e6, "logs": 150e6}, 1e9, {"security": 2})
    assert mix["security"] == pytest.approx(0.3)
    assert mix["general"] == pytest.approx(0.7 * 2 / 3)
    assert mix["logs"] == pytest.approx(0.7 / 3)


def test_batches_deterministic_and_shifted(tmp_path):
    loader = MixtureLoader(make_corpus(tmp_path), "train", {"security": 0.4, "general": 0.4, "logs": 0.2}, 32, 4, seed=1)
    x1, y1 = loader.get_batch(5, 0)
    x2, y2 = loader.get_batch(5, 0)
    assert torch.equal(x1, x2) and torch.equal(y1, y2)
    assert x1.shape == (4, 32) and x1.dtype == torch.long
    x3, _ = loader.get_batch(6, 0)
    assert not torch.equal(x1, x3) or not torch.equal(loader.get_batch(5, 1)[0], x1)


def test_y_is_x_shifted(tmp_path):
    root = tmp_path
    w = ShardWriter(root / "logs", "train", 900, 901)
    w.add(list(range(1, 500)))
    write_manifest(root, {"groups": {"logs": {"train": {"tokens": w.tokens, "bytes": 1, "shards": w.close()}}}, "sources": {}})
    x, y = MixtureLoader(root, "train", {"logs": 1.0}, 16, 3, seed=0).get_batch(0, 0)
    assert torch.equal(x[:, 1:], y[:, :-1])


def test_short_shard_never_sampled(tmp_path):
    loader = MixtureLoader(make_corpus(tmp_path, tiny_shard=True), "train", {"logs": 1.0}, 256, 8, seed=0)
    for step in range(50):
        x, _ = loader.get_batch(step, 0)
        assert 7 not in x.tolist()[0]


def test_group_frequencies(tmp_path):
    weights = {"security": 0.4, "general": 0.4, "logs": 0.2}
    loader = MixtureLoader(make_corpus(tmp_path), "train", weights, 8, 1, seed=3)
    counts = {g: 0 for g in weights}
    inv = {v: k for k, v in GROUP_TOKEN.items()}
    for step in range(2000):
        x, _ = loader.get_batch(step, 0)
        vals = [v for v in x[0].tolist() if v in inv]
        counts[inv[vals[0]]] += 1
    for g, w in weights.items():
        assert abs(counts[g] / 2000 - w) < 0.03


def test_group_batch(tmp_path):
    loader = MixtureLoader(make_corpus(tmp_path), "train", {"security": 0.5, "general": 0.5}, 16, 2, seed=0)
    x, _ = loader.get_group_batch("logs", 0)
    assert set(x.flatten().tolist()) <= {3, 900, 901}
    assert torch.equal(x, loader.get_group_batch("logs", 0)[0])


def test_data_dir_is_relocatable(tmp_path):
    root = make_corpus(tmp_path / "a")
    moved = tmp_path / "b"
    root.rename(moved)
    x, _ = MixtureLoader(moved, "train", {"logs": 1.0}, 8, 1, seed=0).get_batch(0, 0)
    assert x.shape == (1, 8)