Files
exp-model-parameter-similarity/tests/test_lisa_effect_audit.py
T

67 lines
3.4 KiB
Python

"""Check answer-only metrics and isolate recovery from newly lost items."""
import json
from pathlib import Path
from tempfile import TemporaryDirectory
import unittest
import numpy as np
import torch
from torch.nn import functional as F
from crossqkv.lisa_effect_answer_audit import vocabulary_scores
from crossqkv.lisa_effect_audit import MODES, full_vocab_metrics, loss_delta, paired
from tests.test_lisa_tail import Tensor
class EffectAuditTests(unittest.TestCase):
def test_loss_interval_uses_paired_differences(self):
direct = np.array([0.5, 1.0, 4.0, 9.0])
result = loss_delta(direct - 0.25, direct)
self.assertEqual(result, {'mean_delta': -0.25,
'paired_prompt_bootstrap_95': [-0.25, -0.25]})
def test_full_vocab_compares_each_recipient_schedule_to_matched_direct(self):
mapped = ('native', 'linear', 'direct_chain', 'lisa_chain', 'lisa_answer_weighted',
'direct_23', 'lisa_23_ablation')
payload = {'candidate_sites': [19, 23], 'single_lisa_is_ablation_of_joint_checkpoint': True,
'arms': {}}
for name in mapped:
correct = name not in ('direct_chain', 'direct_23')
payload['arms'][name] = {
'n': 1, 'full_vocab_correct': int(correct), 'four_choice_correct': 1,
'full_vocab_nll': 0.25 if correct else 2.0,
'rows': [{'id': 'test-a', 'gold_id': 3, 'greedy_id': 3 if correct else 4,
'choice_id': 3, 'nll': 0.25 if correct else 2.0}],
}
choices = {name: np.array([True]) for name in MODES}
with TemporaryDirectory() as scratch:
path = Path(scratch) / 'answers.json'
path.write_text(json.dumps(payload))
measured = full_vocab_metrics(path, [('test-a', 0)], [19, 23], choices)
comparisons = measured['matched_direct_comparisons']
self.assertEqual(comparisons['lisa_chain']['baseline'], 'direct_chain')
self.assertEqual(comparisons['lisa_23_ablation']['baseline'], 'direct_23')
self.assertEqual(comparisons['lisa_chain']['rescued'], 1)
self.assertEqual(comparisons['lisa_chain']['answer_nll_delta']['mean_delta'], -1.75)
choices['lisa'] = np.array([False])
with self.assertRaisesRegex(ValueError, 'lisa_chain choices differ'):
full_vocab_metrics(path, [('test-a', 0)], [19, 23], choices)
def test_paired_rescue_excludes_natively_wrong_items(self):
native, direct, candidate = (np.array(x, dtype=bool) for x in
[(1, 1, 1, 0), (0, 0, 1, 1), (1, 0, 0, 1)])
result = paired(native, direct, candidate)
self.assertEqual(result['native_correct_lost_by_direct'], 2)
self.assertEqual(result['rescued'], 1)
self.assertEqual(result['newly_lost_among_native_and_direct_correct'], 1)
self.assertEqual(result['accuracy_delta_vs_direct'], 0)
def test_streamed_answer_scores_match_full_reference(self):
g = torch.Generator().manual_seed(9)
h, w = torch.randn(4, 6, generator=g), torch.randn(1033, 6, generator=g)
target = torch.tensor([2, 1032, 22, 1024])
actual, loss = vocabulary_scores(h, target, Tensor(w.numpy()))
logits = F.linear(h, w)
torch.testing.assert_close(actual, logits.argmax(-1))
torch.testing.assert_close(loss, F.cross_entropy(logits, target, reduction='none'))