Score gold answer tokens across single-layer restorations
This commit is contained in:
@@ -7,7 +7,7 @@ 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_rank_compare import assignments, make_adapters, score_gold_answers
|
||||
from crossqkv.lisa_trajectory_train import reverse_kl
|
||||
|
||||
|
||||
@@ -27,6 +27,31 @@ class CombinationTests(unittest.TestCase):
|
||||
torch.testing.assert_close(torch.autograd.grad(loss, student)[0],
|
||||
torch.autograd.grad(dense, student)[0])
|
||||
|
||||
def test_gold_scores_cover_only_answer_and_stop_at_the_correct_shift(self):
|
||||
class Embedding:
|
||||
def __init__(self, weights):
|
||||
self.shape = weights.shape
|
||||
self.flat = weights.numpy().ravel()
|
||||
|
||||
def read(self, span):
|
||||
return self.flat[span]
|
||||
|
||||
def evict(self):
|
||||
pass
|
||||
|
||||
rng = torch.Generator().manual_seed(72507)
|
||||
weights = torch.randn(9, 4, generator=rng)
|
||||
outputs = {label: torch.randn(2, 5, 4, generator=rng) for label in ('baseline', 'restore-15')}
|
||||
ids = [[0, 1, 2, 3, 4, 8], [1, 0, 6, 8]]
|
||||
gold, scores = score_gold_answers(outputs, ids, [3, 2], Embedding(weights))
|
||||
self.assertEqual(gold, [[3, 4, 8], [6, 8]])
|
||||
for label, hidden in outputs.items():
|
||||
logp = (hidden @ weights.T).log_softmax(-1)
|
||||
expected = ([logp[0, 2, 3], logp[0, 3, 4], logp[0, 4, 8]],
|
||||
[logp[1, 1, 6], logp[1, 2, 8]])
|
||||
for row, answer in zip(scores, expected):
|
||||
torch.testing.assert_close(torch.tensor(row[label]), torch.stack(answer))
|
||||
|
||||
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]}
|
||||
|
||||
Reference in New Issue
Block a user