feat(attention): add relu2max residual routing - #888
Open
klei22 wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adds an alternative depth-routing normalization for the FullAttentionResidual mixer (“relu2max”), wiring it through configuration/CLI, documenting its behavior, and adding tests to validate its properties.
Changes:
- Add
weight_variant+relu2max_shiftoptions toFullAttentionResidual, with a newrelu2maxweight computation path. - Plumb new config/CLI knobs (
attention_residual_weight_variant,attention_residual_relu2max_shift) throughGPTConfig,train_args.py, andmodel.py. - Add unit tests and documentation for ReLU2Max residual routing behavior.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| variations/attention_residual_variations.py | Adds relu2max routing weights and related configuration/validation. |
| train_args.py | Exposes new CLI flags for selecting routing weight variant and shift. |
| tests/test_attention_residual.py | Adds coverage for relu2max uniform init/trainability and normalization properties. |
| model.py | Wires new config fields into FullAttentionResidual construction. |
| gpt_conf.py | Adds new config defaults for residual weight variant and shift. |
| documentation/Attention_Residuals.md | Documents ReLU2Max routing and how it differs from softmax depth routing. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+43
to
+51
| centered_scores = scores - scores.mean(dim=0, keepdim=True) | ||
| terms = torch.relu(centered_scores + self.relu2max_shift).square() | ||
| denominator = terms.sum(dim=0, keepdim=True) | ||
| normalized = terms / denominator.clamp_min(torch.finfo(terms.dtype).tiny) | ||
|
|
||
| # Retain a finite fallback for non-finite/extreme inputs and preserve | ||
| # the residual mixer's convex-combination invariant. | ||
| uniform = torch.full_like(terms, 1.0 / terms.size(0)) | ||
| return torch.where(denominator > 0, normalized, uniform) |
Comment on lines
149
to
155
| self.attention_residual = FullAttentionResidual( | ||
| 2 * config.n_layer + 1, config.n_embd, config.attention_residual_eps | ||
| 2 * config.n_layer + 1, | ||
| config.n_embd, | ||
| config.attention_residual_eps, | ||
| config.attention_residual_weight_variant, | ||
| config.attention_residual_relu2max_shift, | ||
| ) |
Comment on lines
+94
to
+95
| number of retained residual sources. If every squared-ReLU term is zero, the | ||
| implementation safely falls back to uniform weights. |
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.
No description provided.