File size: 10,337 Bytes
43eefa6
 
 
 
 
 
 
 
 
0eeed4c
43eefa6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0eeed4c
43eefa6
 
 
 
 
 
 
 
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
"""Author-source oracle parity. Requires existing torch; never installs packages."""
import ast
import copy
import hashlib
import json
import logging
import pathlib
import types
import unittest
from mp_opd_source_checks import assert_source_hash

import test_mp_opd_port as fixtures

ROOT = pathlib.Path(__file__).resolve().parents[1]
if fixtures.HAS_TORCH:
    import torch


def original_oracle_class():
    """Execute original numerical bodies, stripping only imports/decorators.

    The oracle does not import the port, adapter, or CTOPD RKL implementation.
    """
    source = ROOT / 'tests/fixtures/simct_upstream'
    rkl_tree = ast.parse((source / 'reverse_kl_div.py.txt').read_text())
    rkl = next(n for n in rkl_tree.body if isinstance(n, ast.FunctionDef))
    rkl.decorator_list = []
    scope = {'torch': torch}
    exec(compile(ast.Module(body=[rkl], type_ignores=[]), 'original_rkl', 'exec'), scope)
    cls_tree = ast.parse((source / 'span_ctkd.py.txt').read_text())
    cls = next(n for n in cls_tree.body if isinstance(n, ast.ClassDef))
    cls.decorator_list = []
    scope.update(build_loss_fn=lambda *_: scope['compute_reverse_kl_div'],
                 logger=logging.getLogger('simct-oracle'))
    exec(compile(ast.Module(body=[cls], type_ignores=[]), 'original_simct', 'exec'), scope)
    return scope['SpanCrossTokenizerKD']


@unittest.skipUnless(fixtures.HAS_TORCH, 'requires existing PyTorch; no bootstrap')
class SimCTTests(unittest.TestCase):
    parity_errors = []

    @classmethod
    def tearDownClass(cls):
        if cls.parity_errors:
            print('SIMCT_PARITY ' + json.dumps({
                'cases': len(cls.parity_errors),
                'max_loss_abs_error': max(x[0] for x in cls.parity_errors),
                'max_student_gradient_abs_error': max(x[1] for x in cls.parity_errors)}))

    def pair(self, *, threshold=0.0, cap=None):
        from kdflow.algorithms.simct import SimCT
        helper = fixtures.MPPortTests()
        mp, student, teacher = helper.fixture(cap=cap)
        args = mp.args
        args.kd.kd_algorithm = 'simct'
        args.kd.simct_gh_mask_threshold = args.kd.span_gh_mask_threshold = threshold
        actual = SimCT(mp.strategy, student, teacher, fixtures.Tokenizer(), fixtures.Tokenizer())
        ref_student, ref_teacher = copy.deepcopy(student), copy.deepcopy(teacher)

        class OriginalHFOutput:
            def __call__(self, ids, **kwargs):
                hidden = ref_student(ids, **kwargs)['hidden_states'][-1]
                logits = ref_student.model.lm_head(hidden, skip=False)
                if cap is not None:
                    logits = torch.tanh(logits / cap) * cap
                return {'logits': logits}

        reference = original_oracle_class()(mp.strategy, OriginalHFOutput(), ref_teacher,
                                            fixtures.Tokenizer(), fixtures.Tokenizer())
        reference._debug_count, reference._span_debug_count = 3, 1
        # Set iteration ordering is identical within this process. Assert explicitly.
        self.assertTrue(torch.equal(actual.upstream.student_overlap_token_ids,
                                    reference.student_overlap_token_ids))
        return helper, actual, reference, student, ref_student, teacher, ref_teacher

    def assert_pair(self, *, threshold=0.0, cap=None, copies=1, strict=False):
        helper, actual, reference, student, rs, teacher, rt = self.pair(threshold=threshold, cap=cap)
        batch = helper.batch(copies)
        if strict:
            batch['tea_input_ids'] = batch['stu_input_ids'].clone()
            batch['tea_loss_mask'] = batch['stu_loss_mask'].clone()
            batch['tea_attn_mask'] = batch['stu_attn_mask'].clone()
            batch['teacher_hiddens'] = torch.randn(int(batch['tea_loss_mask'].sum()), 4, requires_grad=True)
        a, b = actual.training_step(batch), reference.training_step(batch)
        torch.testing.assert_close(a['train/loss'], b['loss'], atol=1e-6, rtol=1e-5)
        torch.testing.assert_close(a['distill/alignment_ratio'], b['align_ratio'])
        torch.testing.assert_close(a['distill/simct_gh_mask_ratio'], b['gh_mask_ratio'])
        ga = torch.autograd.grad(a['train/loss'], tuple(student.parameters()))
        gb = torch.autograd.grad(b['loss'], tuple(rs.parameters()))
        for x, y in zip(ga, gb):
            torch.testing.assert_close(x, y, atol=1e-6, rtol=1e-5)
        self.parity_errors.append((float((a['train/loss'] - b['loss']).detach().abs()),
                                   max(float((x - y).abs().max()) for x, y in zip(ga, gb))))
        self.assertTrue(all(p.grad is None for p in teacher.parameters()))
        self.assertTrue(all(p.grad is None for p in rt.parameters()))
        # Assert teacher/hidden stop-gradient with autograd too, not only .grad buffers.
        grads = torch.autograd.grad(actual.training_step(batch)['train/loss'],
                                    (*teacher.parameters(), batch['teacher_hiddens']), allow_unused=True)
        self.assertTrue(all(g is None for g in grads))

    def test_mixed_spans_loss_and_all_student_gradients_match_original(self):
        self.assert_pair(copies=2)

    def test_strict_only_loss_and_gradients_match_original(self):
        self.assert_pair(strict=True)

    def test_softcap_matches_original_hf_student_output(self):
        self.assert_pair(cap=2.5)

    def test_optional_teacher_mass_mask_matches_original(self):
        for threshold in (0.5, 2.0, 100.0):
            with self.subTest(threshold=threshold):
                self.assert_pair(threshold=threshold)

    def test_virtual_logits_and_derivatives_match_independent_hand_calculation(self):
        _, algo, _, _, _, _, _ = self.pair()
        # First segment mismatches: teacher 'ab' vs student 'a','b'; next is EOS.
        segments = [(0, 1, 0, 2), (1, 2, 2, 3)]
        sl = torch.randn(3, 8, requires_grad=True)
        tl = torch.randn(2, 8)
        sv, tv = algo.upstream._build_virtual_vocab_logits(segments, sl, tl, [1, 2, 0], [4, 0])
        ids, tids = algo.upstream.student_overlap_token_ids, algo.upstream.teacher_overlap_token_ids
        manual_s = torch.stack([torch.cat([sl[0, ids], ((sl[0, 1] + sl[1, 2]) / 2)[None]]),
                                torch.cat([sl[2, ids], sl.new_tensor([-1e9])])])
        manual_t = torch.stack([torch.cat([tl[0, tids], tl[0, 4][None]]),
                                torch.cat([tl[1, tids], tl.new_tensor([-1e9])])])
        torch.testing.assert_close(sv, manual_s)
        torch.testing.assert_close(tv, manual_t)
        p, q = manual_s.log_softmax(-1), manual_t.log_softmax(-1)
        expected = (p.exp() * (p - q)).sum()
        result = algo.upstream.loss_fn(sv, tv, reduction='sum')
        torch.testing.assert_close(result, expected)
        torch.testing.assert_close(torch.autograd.grad(result, sl)[0],
                                   torch.autograd.grad(expected, sl)[0])

    def test_response_boundaries_and_microbatch_normalizer(self):
        helper, algo, _, student, _, _, _ = self.pair()
        full = helper.batch(copies=2)
        one = helper.batch()
        one['avg_micro_batch_token_num'] = full['avg_micro_batch_token_num']
        whole = algo.training_step(full)['train/loss']
        split = sum(algo.training_step(one)['train/loss'] for _ in range(2))
        torch.testing.assert_close(whole, split)
        for a, b in zip(torch.autograd.grad(whole, tuple(student.parameters())),
                        torch.autograd.grad(split, tuple(student.parameters()))):
            torch.testing.assert_close(a, b)

    def test_no_alignment_returns_connected_zero_and_bad_shapes_fail(self):
        helper, algo, _, student, _, _, _ = self.pair()
        batch = helper.batch()
        batch['tea_input_ids'][:] = 5
        loss = algo.training_step(batch)['train/loss']
        self.assertEqual(float(loss.detach()), 0.0)
        gradients = torch.autograd.grad(loss, tuple(student.parameters()), allow_unused=True)
        self.assertTrue(all(g is None or not bool(g.any()) for g in gradients))
        batch['teacher_hiddens'] = batch['teacher_hiddens'][:-1]
        with self.assertRaisesRegex(ValueError, 'hidden rows'):
            algo.training_step(batch)

    def test_mode_registration_arguments_and_no_legacy_alias(self):
        from kdflow.algorithms import ALGO_DICT
        from kdflow.arguments.distillation_args import DistillationArguments
        self.assertIn('simct', ALGO_DICT)
        self.assertNotIn('span_ctkd', ALGO_DICT)
        options = dict(kd_algorithm='simct', kd_ratio=1, kd_loss_fn='rkl')
        self.assertEqual(DistillationArguments(**options).simct_gh_mask_threshold, 0)
        for value in (-1, float('nan'), float('inf')):
            with self.assertRaises(ValueError):
                DistillationArguments(**options, simct_gh_mask_threshold=value)
        with self.assertRaises(ValueError):
            DistillationArguments(kd_algorithm='simct')

    def test_unsupported_topology_and_non_on_policy_fail(self):
        from kdflow.algorithms.simct import SimCT
        _, algo, _, student, _, teacher, _ = self.pair()
        algo.args.train.num_gpus_per_node = 2
        with self.assertRaisesRegex(ValueError, 'one student rank'):
            SimCT(algo.upstream.strategy, student, teacher, fixtures.Tokenizer(), fixtures.Tokenizer())
        algo.args.train.num_gpus_per_node = 1
        algo.args.scenario = 'off_policy_kd'
        with self.assertRaisesRegex(ValueError, 'on-policy'):
            SimCT(algo.upstream.strategy, student, teacher, fixtures.Tokenizer(), fixtures.Tokenizer())


class SimCTSourceTests(unittest.TestCase):
    def test_pinned_sources_and_frozen_existing_modes(self):
        manifest = json.loads((ROOT / 'docs/SIMCT_SOURCE.json').read_text())
        for record in manifest['files'] + manifest['unchanged_existing_files']:
            assert_source_hash(self, ROOT, record)
        raw = (ROOT / 'tests/fixtures/simct_upstream/span_ctkd.py.txt').read_bytes()
        port = (ROOT / 'kdflow/algorithms/_simct_upstream/span_ctkd.py').read_bytes()
        self.assertEqual(raw.replace(b'from kdflow.algorithms import register_algorithm',
                                     b'from . import register_algorithm'), port)


if __name__ == '__main__':
    unittest.main()