Add controlled two-layer LiSA reproduction on Qwen

This commit is contained in:
2026-11-24 00:00:42 +08:00
parent 115e440455
commit 1c8943a2e8
21 changed files with 4444 additions and 7 deletions
+16
View File
@@ -27,6 +27,22 @@ class LiSATrainingTests(unittest.TestCase):
torch.testing.assert_close(torch.autograd.grad(streamed, hidden)[0],
torch.autograd.grad(dense, hidden)[0])
def test_answer_weighted_ce_matches_dense(self):
torch.manual_seed(12)
h = torch.randn((2, 4, 3), dtype=torch.float64, requires_grad=True)
w = torch.randn((5, 3), dtype=h.dtype)
ids = torch.randint(0, 5, (2, 4))
lengths = torch.tensor([4, 3])
valid = torch.arange(1, 4) < lengths[:, None]
answer = torch.arange(2, 5) == lengths[:, None]
weights = torch.where(answer, 16., 1.)[valid]
ce = F.cross_entropy(F.linear(h[:, :-1][valid], w), ids[:, 1:][valid], reduction='none')
dense = (ce * weights).sum() / weights.sum()
streamed = language_model_loss(h, ids, lengths, lambda: iter([(0, w)]), answer_weight=16.)
torch.testing.assert_close(streamed, dense)
torch.testing.assert_close(torch.autograd.grad(streamed, h)[0],
torch.autograd.grad(dense, 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)