"""Check answer-only metrics and isolate recovery from newly lost items.""" 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 paired from tests.test_lisa_tail import Tensor class EffectAuditTests(unittest.TestCase): 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'))