Reuse streamed vocabulary blocks for teacher and student projections

This commit is contained in:
2026-11-25 16:33:19 +08:00
parent 1f3ee3cbff
commit 23386797c4
3 changed files with 21 additions and 5 deletions
+8 -1
View File
@@ -19,7 +19,14 @@ class CombinationTests(unittest.TestCase):
student = normal(3, 4).requires_grad_()
teacher = normal(3, 4)
weight = normal(13, 4)
loss = reverse_kl(student, teacher, lambda: iter(weight.split(5)))
traversals = 0
def chunks():
nonlocal traversals
traversals += 1
yield from weight.split(5)
loss = reverse_kl(student, teacher, chunks)
self.assertEqual(traversals, 2)
logp = (student @ weight.T).log_softmax(-1)
logq = (teacher @ weight.T).log_softmax(-1)
dense = (logp.exp() * (logp - logq)).sum(-1).mean()