Run matched rank allocation and trajectory distillation experiments for LiSA
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
"""Regression checks for rank allocation, donor controls and reverse KL."""
|
||||
import json
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
import torch
|
||||
|
||||
from crossqkv.lisa_combination_screen import SITES, paired_questions
|
||||
from crossqkv.lisa_rank_compare import assignments, make_adapters
|
||||
from crossqkv.lisa_trajectory_train import reverse_kl
|
||||
|
||||
|
||||
class CombinationTests(unittest.TestCase):
|
||||
def test_reverse_kl_gradient_matches_dense_unequal_vocab_chunks(self):
|
||||
rng = torch.Generator().manual_seed(72504)
|
||||
def normal(*shape):
|
||||
return torch.randn(*shape, dtype=torch.float64, generator=rng)
|
||||
student = normal(3, 4).requires_grad_()
|
||||
teacher = normal(3, 4)
|
||||
weight = normal(13, 4)
|
||||
loss = reverse_kl(student, teacher, lambda: iter(weight.split(5)))
|
||||
logp = (student @ weight.T).log_softmax(-1)
|
||||
logq = (teacher @ weight.T).log_softmax(-1)
|
||||
dense = (logp.exp() * (logp - logq)).sum(-1).mean()
|
||||
torch.testing.assert_close(loss, dense)
|
||||
torch.testing.assert_close(torch.autograd.grad(loss, student)[0],
|
||||
torch.autograd.grad(dense, student)[0])
|
||||
|
||||
def test_diagnostic_tie_rule_and_wrong_assignment_control(self):
|
||||
rows = paired_questions('diagnostic', 2, 72501)
|
||||
protocol = {'splits': {'diagnostic': rows}, 'rank_budget': [32, 40, 48]}
|
||||
with tempfile.TemporaryDirectory() as folder:
|
||||
directory = Path(folder)
|
||||
for label, correct in [('baseline', 0), ('restore-15', 1),
|
||||
('restore-19', 3), ('restore-23', 1)]:
|
||||
data = [{'id': x['id'], 'gold': x['answer'],
|
||||
'arms': {label: {'correct': i < correct}}} for i, x in enumerate(rows)]
|
||||
(directory/(label+'.json')).write_text(json.dumps(data))
|
||||
_, ranks = assignments(protocol, directory)
|
||||
self.assertEqual(ranks['allocated'], {19: 48, 15: 40, 23: 32})
|
||||
self.assertEqual(ranks['reversed'], {19: 32, 15: 40, 23: 48})
|
||||
|
||||
def test_local_control_is_insensitive_to_donor_with_equal_parameter_count(self):
|
||||
config = {'hidden_size': 8, 'num_attention_heads': 4, 'num_key_value_heads': 2,
|
||||
'rope_parameters': {'rope_theta': 10000, 'partial_rotary_factor': 0.25}}
|
||||
allocated = dict(zip(SITES, (32, 40, 48)))
|
||||
local = make_adapters(config, allocated, False)
|
||||
uniform = make_adapters(config, dict.fromkeys(SITES, 40), True)
|
||||
def count(adapters):
|
||||
return sum(p.numel() for a in adapters.values() for p in a.parameters())
|
||||
self.assertEqual(count(local), count(uniform))
|
||||
h = torch.randn(1, 3, 8)
|
||||
donor = torch.randn(1, 4, 3, 3)
|
||||
pos = torch.arange(3)[None, :]
|
||||
a = local[15]
|
||||
torch.testing.assert_close(a(h, donor, pos), a(h, donor * 5 + 2, pos))
|
||||
Reference in New Issue
Block a user