diff --git a/crossqkv/lisa_trajectory_train.py b/crossqkv/lisa_trajectory_train.py index afa8341..d808f38 100644 --- a/crossqkv/lisa_trajectory_train.py +++ b/crossqkv/lisa_trajectory_train.py @@ -19,8 +19,17 @@ from functional.run import write_json def reverse_kl(student, teacher, chunks): """Exact reverse KL and its first derivative for a streamed, frozen head.""" with torch.no_grad(): - logp = torch.cat([student @ w.T for w in chunks()], dim=-1).log_softmax(-1) - logq = torch.cat([teacher @ w.T for w in chunks()], dim=-1).log_softmax(-1) + # Both models use the same frozen output weights. Read each block once + # for the two projections, then revisit it only for the student gradient. + student_logits = [] + teacher_logits = [] + for w in chunks(): + student_logits.append(student @ w.T) + teacher_logits.append(teacher @ w.T) + logp = torch.cat(student_logits, dim=-1).log_softmax(-1) + del student_logits + logq = torch.cat(teacher_logits, dim=-1).log_softmax(-1) + del teacher_logits diff = logp - logq prob = logp.exp() kl = (prob * diff).sum(-1, keepdim=True) diff --git a/results/lisa-combinations/execution.sha256 b/results/lisa-combinations/execution.sha256 index 932d5e4..838ef1e 100644 --- a/results/lisa-combinations/execution.sha256 +++ b/results/lisa-combinations/execution.sha256 @@ -1,6 +1,6 @@ 024720e9eae7556e4d5ca2ca1d71dd544815c79ed2c51b113564d5a6fb4ad004 crossqkv/lisa_combination_screen.py 7c2156139d3447a9e69e903bc80821ea55b4e01df63a1b6137e4de589748efba crossqkv/lisa_rank_compare.py -f55e6cebec2e462959279122d134e81770c035d4e46bbf7620ba5a738dcba4fb crossqkv/lisa_trajectory_train.py +1aab837faee43bbdf1b4fe8f92ce28647a4f1377d962d538a57d32ab65215678 crossqkv/lisa_trajectory_train.py efbbc2ee4fe821a5aa002dc90b144a49251a141bec38ca733bc429f4d0bdce49 crossqkv/lisa_combination_eval.py 519e0a031efcfb69c9b86fca1cb649fe8bb93d799401c8023ff7b6549cded448 crossqkv/lisa_full_answer.py 6369a674600812d06fc6d667e4a67ae97b63b4be3ba02ad28520eb9a748d272b crossqkv/lisa_full_answer_train.py @@ -9,5 +9,5 @@ efbbc2ee4fe821a5aa002dc90b144a49251a141bec38ca733bc429f4d0bdce49 crossqkv/lisa_ d47b4c9cc34082e9b356e745821298a43981e4cd18ed55a3f59b4b6fe7cc46e3 crossqkv/lisa_tail.py 9f0b6a53f107950661e91c16f03b49440708b3936120e2f84ef1d66f185257b6 crossqkv/lisa_train.py 94437e0138c54ec34b1beaaa0e08a334ce0822f60af598a5a896459b778a3a1c functional/model.py -b01eb3aa2c19b6626c76e909f415b185479e0c8b273b2f0b87439991ef32c64c tests/test_lisa_combinations.py +ca5b1a2b64b9d48b5067d89eedc542250fc3ec1ebc20ad4f78affa85a82fae32 tests/test_lisa_combinations.py e1d70b10863dab1e13dfa9573c0d87fc6ca017039c3cf9b90f7628f3e7fe5664 crossqkv/lisa_effect_answer_audit.py diff --git a/tests/test_lisa_combinations.py b/tests/test_lisa_combinations.py index f45e633..78c9379 100644 --- a/tests/test_lisa_combinations.py +++ b/tests/test_lisa_combinations.py @@ -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()