File size: 10,786 Bytes
c8ba9c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""CPU correctness tests; no GPU allocation or production state is involved."""

import copy
import json
from pathlib import Path

import pytest
import torch

import posttrain
from ops.posttrain_data import encode_messages, renderer_sha256
from ops.posttrain_preflight import make_fixture
from warpstate import WarpStateConfig, WarpStateLM


class CharacterTokenizer:
    def eos_id(self):
        return 2

    def bos_id(self):
        return 1

    def vocab_size(self):
        return 256

    def encode(self, text, out_type=int):
        return [ord(char) for char in text]


def messages(answer='four'):
    return [{'role': 'system', 'content': 'Be concise.'},
            {'role': 'user', 'content': 'Two plus two?'},
            {'role': 'assistant', 'content': answer}]


def test_assistant_content_and_real_eos_only():
    rows = messages() + [{'role': 'tool', 'content': 'untrusted result'},
                         {'role': 'assistant', 'content': 'done'}]
    encoded = encode_messages(rows, CharacterTokenizer())
    targets = [value for value in encoded['labels'] if value != -100]
    assert targets == [ord(c) for c in 'four'] + [2] + [ord(c) for c in 'done'] + [2]
    assert encoded['input_ids'][0] == 1
    assert len(encoded['input_ids']) == len(encoded['labels'])
    with pytest.raises(ValueError, match='exceeds'):
        encode_messages(rows, CharacterTokenizer(), max_length=len(encoded['input_ids']) - 1)
    assert encode_messages(rows, CharacterTokenizer(), max_length=len(encoded['input_ids'])) == encoded


@pytest.mark.parametrize('bad', [[], [{'role': 'user', 'content': 'no answer'}],
                               [{'role': 'unknown', 'content': 'text'}],
                               [{'role': 'assistant', 'content': ''}],
                               [{'role': 'assistant', 'content': 'call', 'tool_calls': [{'name': 'x'}]}]])
def test_invalid_conversations_rejected(bad):
    with pytest.raises(ValueError):
        encode_messages(bad, CharacterTokenizer())


def test_collation_shifts_and_masks_independent_padding():
    a = {'input_ids': [1, 4, 5, 2], 'labels': [-100, -100, 5, 2]}
    x, y = posttrain.collate([a, None], 8, 0)
    assert x.tolist() == [[1, 4, 5, 0, 0, 0, 0, 0], [0] * 8]
    assert y.tolist() == [[-100, 5, 2, -100, -100, -100, -100, -100], [-100] * 8]


def test_sample_order_resume_and_tail_do_not_duplicate():
    cursor, groups = 0, []
    for _ in range(6):
        values, cursor = posttrain.update_indices(5, 17, cursor, 2, 2)
        groups.append(values)
    assert sorted(sum(groups[:3], [])) == list(range(5))
    assert sorted(sum(groups[3:], [])) == list(range(5))
    assert [len(v) for v in groups] == [2, 2, 1, 2, 2, 1]
    assert posttrain.update_indices(5, 17, 4, 2, 2)[0] == groups[2]
    assert posttrain.update_indices(5, 17, cursor, 2, 2) == ([], 10)


def test_ddp_token_weighting_matches_global_objective_with_empty_rank():
    # Simulate DDP's gradient average with unequal rank/microbatch target counts.
    parameter = torch.tensor(0.7, dtype=torch.float64, requires_grad=True)
    partitions = [[torch.tensor([1., 2., 4.]), torch.tensor([7.])],
                  [torch.tensor([]), torch.tensor([3., 8.])]]
    gradients = []
    for rank in partitions:
        parameter.grad = None
        for target in rank:
            target = target.double()
            mean = ((parameter - target) ** 2).sum() / max(1, len(target))
            posttrain.token_weighted_loss(mean, len(target), 6, 2).backward()
        gradients.append(parameter.grad.clone())
    parameter.grad = None
    target = torch.tensor([1., 2., 4., 7., 3., 8.], dtype=torch.float64)
    ((parameter - target) ** 2).mean().backward()
    torch.testing.assert_close(sum(gradients) / 2, parameter.grad, rtol=1e-14, atol=1e-14)


def assert_tree_equal(a, b):
    if isinstance(a, torch.Tensor):
        assert torch.equal(a, b)
    elif isinstance(a, dict):
        assert a.keys() == b.keys()
        for key in a:
            assert_tree_equal(a[key], b[key])
    elif isinstance(a, (list, tuple)):
        assert len(a) == len(b)
        for x, y in zip(a, b):
            assert_tree_equal(x, y)
    else:
        assert a == b


def test_real_bf16_training_exact_resume_and_frozen_base(tmp_path):
    args = make_fixture(tmp_path)
    original_hash = posttrain.digest(args.weights)
    original = torch.load(args.weights, weights_only=False, mmap=True)
    try:
        posttrain.train(args)
        uninterrupted = torch.load(args.work_dir / 'latest.pt', weights_only=False, mmap=True)
        args.work_dir = tmp_path / 'resumed'
        args.max_steps = 1
        posttrain.train(args)
        partial = torch.load(args.work_dir / 'latest.pt', weights_only=False, mmap=True)
        assert partial['step'] == 1 and partial['completed'] is False
        assert not (args.work_dir / 'completion.json').exists()
        args.max_steps = None
        posttrain.train(args)
        resumed = torch.load(args.work_dir / 'latest.pt', weights_only=False, mmap=True)
        assert resumed['step'] == 3 and resumed['sample_cursor'] == 5
        assert resumed['completed'] is True
        for key in ('model', 'optimizer', 'sample_cursor', 'tokens_seen', 'assistant_tokens_seen', 'val_loss'):
            assert_tree_equal(uninterrupted[key], resumed[key])
        assert any(not torch.equal(original['model'][k], v) for k, v in resumed['model'].items())
        assert all(v.dtype == torch.float32 for v in resumed['model'].values())
        assert resumed['origin']['step'] == 76294
        report = json.loads((args.work_dir / 'evaluation.json').read_text())
        assert report['step'] == 3 and report['evaluated_samples'] == 2
        assert report['objective'] == 'assistant-token-mean'
        completion = json.loads((args.work_dir / 'completion.json').read_text())
        assert completion['completed'] is True and completion['paused_for_evaluation'] is True
        assert completion['step'] == resumed['step']
        assert completion['val_loss'] == resumed['val_loss']
        assert torch.equal(uninterrupted['rng_by_rank'][0]['torch'], resumed['rng_by_rank'][0]['torch'])
        assert posttrain.digest(args.weights) == original_hash
        bad = copy.deepcopy(resumed['signature'])
        bad['world_size'] = 2
        with pytest.raises(ValueError, match='signature'):
            posttrain.validate_resume(resumed, bad, 2)
    finally:
        torch.use_deterministic_algorithms(False)


def test_ready_manifest_and_per_record_counts_are_enforced(tmp_path):
    import sentencepiece as spm
    args = make_fixture(tmp_path)
    sp = spm.SentencePieceProcessor(model_file=str(args.tokenizer))
    path = args.data_dir / 'manifest.json'
    manifest = json.loads(path.read_text())
    manifest['complete'] = False
    path.write_text(json.dumps(manifest))
    with pytest.raises(ValueError, match='not complete'):
        posttrain.load_data(args.data_dir, sp, posttrain.digest(args.tokenizer), 128)
    manifest['complete'] = True
    manifest['renderer_sha256'] = 'wrong'
    path.write_text(json.dumps(manifest))
    with pytest.raises(ValueError, match='renderer'):
        posttrain.load_data(args.data_dir, sp, posttrain.digest(args.tokenizer), 128)


def test_inputs_cannot_be_training_outputs(tmp_path):
    args = make_fixture(tmp_path)
    for output in (args.data_dir, args.weights.parent / 'run', tmp_path):
        with pytest.raises(ValueError, match='separate|inside'):
            posttrain.validate_paths(output, args.data_dir, args.weights, args.tokenizer, args.config)


def test_wrong_base_token_boundary_is_rejected(tmp_path):
    args = make_fixture(tmp_path)
    plan = json.loads(args.config.read_text())
    plan['base']['tokens_seen'] = 999
    args.config.write_text(json.dumps(plan))
    try:
        with pytest.raises(ValueError, match='tokens_seen differs'):
            posttrain.train(args)
    finally:
        torch.use_deterministic_algorithms(False)


@pytest.mark.parametrize('bad', [{'stage': 'dpo'}, {'training': {'min_lr': -1}},
                               {'training': {'warmup_ratio': 1}}, {'training': {'lr': 0}},
                               {'training': {'bogus': True}}, {'training': {'micro_batch': True}}])
def test_plan_rejects_unsupported_settings(tmp_path, bad):
    plan = dict(format='warpstate-posttrain-plan-v1', stage='sft') | bad
    path = tmp_path / 'plan.json'
    path.write_text(json.dumps(plan))
    with pytest.raises(ValueError):
        posttrain.load_plan(path)


def test_schedule_is_fixed_by_plan_not_pause_boundary():
    cfg = posttrain.DEFAULTS | dict(warmup_ratio=.1)
    assert posttrain.learning_rate(0, 100, cfg) == pytest.approx(cfg['lr'] / 10)
    assert posttrain.learning_rate(9, 100, cfg) == pytest.approx(cfg['lr'])
    assert posttrain.learning_rate(99, 100, cfg) == 0


def test_preflight_fixture_identity_and_command_are_cpu_only(tmp_path):
    from ops.posttrain_preflight import training_command, compare_tree
    fixture = make_fixture(tmp_path, device='cuda', compile_core=True)
    repeated = make_fixture(tmp_path, device='cuda', compile_core=True)
    assert repeated.weights == fixture.weights
    cfg = torch.load(fixture.weights, weights_only=False, mmap=True)['config']
    assert cfg['d_model'] == 32 and cfg['n_heads'] == 4
    command = training_command(fixture, 2, tmp_path / 'split', tmp_path / 'stop', 1)
    assert command[-2:] == ['--max-steps', '1']
    assert '--device' in command and command[command.index('--device') + 1] == 'cuda'
    with pytest.raises(ValueError, match='two ranks'):
        training_command(fixture, 1, tmp_path / 'split', tmp_path / 'stop')
    with pytest.raises(ValueError, match='settings/source differ'):
        make_fixture(tmp_path, device='cpu', compile_core=True)
    a = torch.tensor([1.], dtype=torch.float32)
    b = a + 1e-6
    report = compare_tree(a, b, tolerant=True)
    assert report['bitwise_equal'] is False and report['max_absolute_difference'] > 0
    with pytest.raises(ValueError, match='tolerance'):
        compare_tree(a, a + 1e-3, tolerant=True)
    with pytest.raises(ValueError, match='tolerance'):
        compare_tree(a, b)


def test_evaluation_full_holdout_exceeds_intermediate_subset():
    class ConstantLoss(torch.nn.Module):
        def forward(self, x, y, return_logits=False):
            return None, torch.tensor(2.)
    row = dict(input_ids=[1, 3, 4, 2], labels=[-100, -100, 4, 2])
    settings = posttrain.DEFAULTS | dict(eval_batches=1, micro_batch=1, sequence_length=8)
    model = ConstantLoss()
    assert posttrain.evaluate(model, [row] * 3, settings, torch.device('cpu'), 0) == (2., 2)
    assert posttrain.evaluate(model, [row] * 3, settings, torch.device('cpu'), 0, full=True) == (2., 6)
    assert model.training