Repository navigation
Fold constant teacher entropy out of the MLX RCO KL step (1.09x) - #3514
Merged
Merged
Conversation
KL(t || s) = sum(t log t) - sum(t * s_logits) + logsumexp(s_logits). sum(t log t) depends only on the frozen teacher, so cache it once per batch instead of recomputing it on every optimizer step. The per-step form then runs two student reductions over the vocabulary rather than materializing the full-vocabulary student log-probabilities. Qwen3-0.6B RCO step: 1.09x at the paper geometry (4x4096 tokens), 1.09x at 1x4096, 1.04x at 16x128. The objective value matches the previous form to 6e-6 and the alpha gradient norm ratio is 1.00063.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
MLXTeacherKLnow uses the algebraic form of the KL and folds the constant teacher-entropy term out of the optimizer step.sum(t * log t)depends only on the frozen teacher, so it is computed once per batch while the hidden-state cache is built and reused for every step. The per-step chunk loss then runs two student reductions over the vocabulary instead of materializing the full-vocabulary student log-probabilities.Result
Qwen3-0.6B, 196 interpolated projections, 3 options (W2/W3/W4),
chunk_tokens=128, MLX 0.32.4, Apple M5 Ultra. End-to-end RCO step (mx.value_and_gradof the teacher-KL objective, best of 3):Numerics
The rewrite is an identity, not an approximation, so the only difference is floating-point reassociation.
0.937571->0.937567(absolute 4e-6, relative 4e-6).alpha(196x3): max abs 2.87e-4 against a max gradient magnitude of 0.122 (0.24%), mean abs 2.9e-5, norm ratio 1.000632.tests/test_rco_paper.py::test_teacher_kl_value_and_gradient_match_full_vocabulary_oracle, both dtypes, three chunk sizes) passes unchanged, as do the identical-model-is-zero and resident-vs-disk parity tests.The identity relies on
sum(softmax(t_logits)) == 1, which holds to f32 rounding;logsumexpis subtracted per row, so no large-magnitude term is accumulated across the vocabulary.Memory
Per chunk (
chunk_tokensx 151936), the old form materializedstudent_logp,teacher_logpandexp(teacher_logp); the new form materializesstudent_logitsand the teacher probabilities. That removes one chunk-sized f32 buffer (78 MB atchunk_tokens=128).The entropy cache is one f32 scalar per batch.
cleanup()now clears it, andtest_mlx_search_restores_original_linear_and_exact_budgetasserts it is released. No dense or dequantized weight is retained.Construction cost rises by one teacher pass over the corpus (the entropy pass), amortized over the whole search.
Tests
tests/test_rco_paper.py,tests/test_rco_mlx.py,tests/test_gsq_layer_bit_allocation.py: 62 passed, 1 skipped.ruff checkclean.Scope
MLX only. The Torch
LlamaTeacherKLinrco.pystill uses the log-softmax form; it is the fallback path on this hardware and is left untouched so this PR stays a single concern.