Add embedding_scale and final_logit_softcap (Gemma 4 prep)#492
Open
jlamypoirier wants to merge 1 commit intomainfrom
Open
Add embedding_scale and final_logit_softcap (Gemma 4 prep)#492jlamypoirier wants to merge 1 commit intomainfrom
jlamypoirier wants to merge 1 commit intomainfrom
Conversation
- `LanguageModelEmbeddingsConfig.embedding_scale`: multiplicative scale applied to word embeddings after lookup (Gemma 4 uses sqrt(hidden_size)). Zero overhead for the default value of 1.0 via a compile-time branch in the @torch.compile-decorated _forward. - `LanguageModelHeadConfig.final_logit_softcap`: applies tanh(logits / cap) * cap before the loss. Forward and backward are each wrapped in @torch.compile for op fusion. Gradient back-propagates through the Jacobian (1 - (softcapped / cap)^2) before the output linear backward. - New test_embedding.py: generic parametrized embedding layer test covering scale, dtype, full_precision_residual, position embeddings, and padding (3 base cases x 4 variants). - Adds final_logit_softcap case to test_lm_head.py. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.
Summary
embedding_scale(LanguageModelEmbeddingsConfig): multiplicative scale applied to word embeddings after lookup. Gemma 4 usessqrt(hidden_size). Zero overhead for the default value of 1.0 via a compile-time branch inside@torch.compile-decorated_forward. Necessary as a runtime op (not a weight init change) because tied embeddings share the weight with the LM head — baking the scale into weights would also scale logits.final_logit_softcap(LanguageModelHeadConfig): appliestanh(logits / cap) * capbefore the loss. Gemma 4 uses cap=30. Forward and backward are each@torch.compile-decorated for op fusion. Gradient propagates through the Jacobian(1 - (softcapped / cap)²)before the output-linear backward.Tests
tests/layers/test_embedding.py: generic parametrized embedding layer test — 3 base cases (default, with_padding, with_position_embeddings) × 4 variants (default, bfloat16, full_precision_residual, embedding_scale=2.0) = 12 cases.final_logit_softcap=2.0case totest_lm_head.py(4 cases: plain, split, masked, masked+split).Test plan
pytest -v tests/layers/test_embedding.py— 12 passedpytest -v tests/layers/test_lm_head.py— 56 passed🤖 Generated with Claude Code