Screen deeper LiSA sharing with complete autoregressive answers
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
"""Protect final-answer scoring, split isolation and the screening threshold."""
|
||||
import unittest
|
||||
|
||||
from crossqkv.lisa_full_answer import SCHEDULES, choose, final_integer, fixed_problems, grade
|
||||
from crossqkv.lisa_full_answer_train import audit
|
||||
|
||||
|
||||
class FullAnswerTests(unittest.TestCase):
|
||||
def test_reads_the_terminal_answer_and_requires_eos(self):
|
||||
self.assertEqual(final_integer('<think>First try 17.</think>\n42'), 42)
|
||||
self.assertEqual(final_integer('Working: 17 and 25.\nFinal answer: 42.'), 42)
|
||||
self.assertIsNone(final_integer('17 or 42'))
|
||||
self.assertIsNone(final_integer('<think>17'))
|
||||
self.assertIsNone(final_integer('<think>\nFinal answer: 42'))
|
||||
|
||||
class TextTokenizer:
|
||||
def decode(self, tokens, skip_special_tokens):
|
||||
return ''.join(tokens)
|
||||
|
||||
tok = TextTokenizer()
|
||||
self.assertTrue(grade(tok, ['4', '2', '<EOS>'], 'eos', 42)['correct'])
|
||||
self.assertFalse(grade(tok, ['4', '2'], 'max_new_tokens', 42)['correct'])
|
||||
|
||||
def test_frozen_splits_are_disjoint_and_schedule_must_lose_answers(self):
|
||||
data = fixed_problems()
|
||||
questions = [row['question'] for rows in data.values() for row in rows]
|
||||
self.assertEqual(len(questions), len(set(questions)))
|
||||
self.assertTrue(all(row['answer'] >= 10 for rows in data.values() for row in rows))
|
||||
a = {name: {'correct': 6, 'native_correct_to_wrong_integer': []} for name in SCHEDULES}
|
||||
self.assertIsNone(choose({'lookup': a}))
|
||||
a['direct_4']['native_correct_to_wrong_integer'] = ['a', 'b']
|
||||
self.assertEqual(choose({'lookup': a})['recipients'], [11, 15, 19, 23])
|
||||
|
||||
def test_audit_distinguishes_wrong_numbers_and_missing_answers(self):
|
||||
def row(name, direct):
|
||||
return {'id': name, 'arms': {'native': {'answer': 12, 'correct': True},
|
||||
'direct': {'answer': direct, 'correct': False},
|
||||
'lisa': {'answer': 12, 'correct': True}}}
|
||||
|
||||
report = audit([row('wrong', 13), row('missing', None)])
|
||||
self.assertEqual(report['rescued'], 2)
|
||||
self.assertEqual(report['substantive_loss_ids'], ['wrong'])
|
||||
self.assertEqual(report['substantive_rescued_ids'], ['wrong'])
|
||||
@@ -7,6 +7,7 @@ from torch.nn import functional as F
|
||||
|
||||
from crossqkv.lisa_decode import AttentionSiteCache
|
||||
from crossqkv.lisa_decode_bench import ResidentSite, timed_site
|
||||
from crossqkv.lisa_generate import GenerationDecoder
|
||||
from crossqkv.lisa_tail import FrozenLinear, TorchTail, gated_delta, rotate
|
||||
from crossqkv.lisa_train import LearnableSharingAttention
|
||||
from functional.model import PREFIX, delta_rule, rope
|
||||
@@ -85,6 +86,17 @@ class TailTests(unittest.TestCase):
|
||||
lengths = torch.tensor([5, 5])
|
||||
native, _ = tail(h, None, lengths, 'native', ())
|
||||
torch.testing.assert_close(native, torch.tensor(expected), atol=2e-5, rtol=2e-5)
|
||||
padded = np.stack([ids[0], np.concatenate(([0, 0], ids[1, 2:]))])
|
||||
for mode, schedules, modules in (
|
||||
('direct', [(), (2,)], None), ('lisa', [(2,), (2,)], {2: adapters[2]})
|
||||
):
|
||||
decoder = GenerationDecoder(original, schedules, [0, 2], modules)
|
||||
actual = torch.cat((decoder.advance(torch.tensor(padded[:, :4])),
|
||||
decoder.advance(torch.tensor(padded[:, 4:]))), dim=1)
|
||||
for bi, pad in enumerate((0, 2)):
|
||||
x = h[bi:bi+1, pad:]
|
||||
ref, _ = tail(x, None, torch.tensor([5-pad]), mode, schedules[bi])
|
||||
torch.testing.assert_close(actual[bi:bi+1, pad:], ref, atol=2e-6, rtol=2e-5)
|
||||
donor = torch.tensor(rand(2, 2, 5, 5))
|
||||
run, _ = tail(h, donor, lengths, 'lisa', (0,2), keep_teacher=False)
|
||||
ref, _ = tail(h, donor, lengths, 'lisa', (0,2), keep_teacher=True)
|
||||
|
||||
@@ -43,6 +43,20 @@ class LiSATrainingTests(unittest.TestCase):
|
||||
torch.testing.assert_close(torch.autograd.grad(streamed, h)[0],
|
||||
torch.autograd.grad(dense, h)[0])
|
||||
|
||||
def test_completion_loss_covers_all_reply_tokens(self):
|
||||
h = torch.randn((2, 6, 3), generator=torch.Generator().manual_seed(4),
|
||||
requires_grad=True)
|
||||
w = torch.randn((8, 3), generator=torch.Generator().manual_seed(5))
|
||||
ids = torch.tensor([[1, 2, 1, 4, 2, 7], [1, 3, 5, 7, 0, 0]])
|
||||
lengths, starts = torch.tensor([6, 4]), torch.tensor([3, 2])
|
||||
valid = (torch.arange(1, 6) < lengths[:, None]) & (torch.arange(1, 6) >= starts[:, None])
|
||||
reference = F.cross_entropy(F.linear(h[:, :-1][valid], w), ids[:, 1:][valid])
|
||||
streamed = language_model_loss(h, ids, lengths, lambda: iter([(0, w)]),
|
||||
completion_starts=starts)
|
||||
torch.testing.assert_close(streamed, reference)
|
||||
torch.testing.assert_close(torch.autograd.grad(streamed, h)[0],
|
||||
torch.autograd.grad(reference, h)[0])
|
||||
|
||||
def test_causality_and_gradients_for_both_adapter_components(self):
|
||||
torch.manual_seed(9)
|
||||
a = LearnableSharingAttention(8, 2, 1, 8, 10000, .25, width=10)
|
||||
|
||||
Reference in New Issue
Block a user