Skip to content

Add attention residuals - #873

Merged
gkielian merged 3 commits into
ReaLLMASIC:masterfrom
klei22:add-attention-residuals
Aug 2, 2026
Merged

Add attention residuals#873
gkielian merged 3 commits into
ReaLLMASIC:masterfrom
klei22:add-attention-residuals

Conversation

@klei22

@klei22 klei22 commented Aug 1, 2026

Copy link
Copy Markdown
Collaborator

This pull request introduces a new "Full Attention Residual" architecture as an alternative to the standard additive residuals in the Transformer model. It implements depth-wise attention over all previous sublayer outputs, provides configuration and CLI support, integrates the new residual routing into the model's forward pass, and adds tests and documentation.

Implementation of Full Attention Residuals:

  • Added FullAttentionResidual module in variations/attention_residual_variations.py, which mixes previous sublayer outputs using learned pseudo-queries and token-local depth-wise attention.
  • Updated variations/block_variations.py to add attention_residual_attn and attention_residual_mlp methods, enabling extraction of raw attention and MLP outputs without additive skips, as required by the new residual routing.

Model integration and configuration:

  • Modified model.py to integrate the full residual routing: added logic to select and run the new residual path, updated the forward pass to use it when configured, and ensured correct storage and mixing of sublayer outputs. [1] [2] [3] [4] [5]
  • Extended gpt_conf.py and train_args.py to add configuration options for attention_residual_variant and its epsilon parameter, with CLI support. [1] [2]

Testing and documentation:

  • Added tests/test_attention_residual.py with tests for the new module and model integration, ensuring correct behavior and gradients.
  • Created documentation/Attention_Residuals.md to describe the new architecture, its implementation, and usage instructions.

Experimentation support:

  • Added explorations/default_inf_attention_residual_comparison.yaml to enable systematic comparison between standard and full attention residuals across various hyperparameter sweeps.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a new “Full Attention Residual” routing option that replaces additive residual accumulation with token-local, depth-wise attention over the embedding plus all prior sublayer outputs, integrating it into the GPT forward pass with config/CLI support and accompanying tests/docs.

Changes:

  • Introduces FullAttentionResidual (depth-wise routing mixer) and a new model path gated by attention_residual_variant=full.
  • Extends Block to expose “no additive skip” attention/MLP transforms required by the new routing.
  • Adds configuration/CLI knobs plus tests, documentation, and an exploration YAML for comparisons.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
variations/block_variations.py Adds helper entrypoints to run attention/MLP transforms without additive skips (used by full residual routing).
variations/attention_residual_variations.py Implements the new depth-wise attention residual mixer.
model.py Wires the new residual routing into GPT init and forward pass.
gpt_conf.py Adds config fields for selecting residual routing variant and epsilon.
train_args.py Adds CLI flags for attention_residual_variant and attention_residual_eps.
tests/test_attention_residual.py Adds unit/integration tests covering forward+backward and initial equal-weight mixing behavior.
documentation/Attention_Residuals.md Documents the architecture, constraints, and how to enable it.
explorations/default_inf_attention_residual_comparison.yaml Adds an experiment spec to compare standard vs full attention residuals across sweeps.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread model.py
Comment on lines +147 to +153
self.attention_residual_variant = config.attention_residual_variant
if self.attention_residual_variant == "full":
self.attention_residual = FullAttentionResidual(
2 * config.n_layer + 1, config.n_embd, config.attention_residual_eps
)
elif self.attention_residual_variant != "standard":
raise ValueError(f"unknown attention_residual_variant: {self.attention_residual_variant}")
Comment on lines +479 to +499
def attention_residual_attn(self, x: torch.Tensor, iter_num: int) -> torch.Tensor:
"""Run only the attention transformation, without an additive skip."""
if self.use_parallel_mlp or self.use_edgellm_asic or self.use_post_ln_attn:
raise ValueError("Full Attention Residuals require a sequential block without post-attention norm")
out = self.attn(self.pre_ln_attn(x) if self.use_pre_ln_attn else x, iter_num)
if self.use_peri_ln_attn:
out = self.peri_ln_attn(out)
if self.attn_resid_scaler is not None:
out = self.attn_resid_scaler(out)
return out

def attention_residual_mlp(self, x: torch.Tensor, iter_num: int) -> torch.Tensor:
"""Run only the MLP transformation, without an additive skip."""
if self.use_parallel_mlp or self.use_edgellm_asic or self.use_post_ln_mlp:
raise ValueError("Full Attention Residuals require a sequential block without post-MLP norm")
out = self.mlp(self.pre_ln_mlp(x) if self.use_pre_ln_mlp else x, iter_num)
if self.use_peri_ln_mlp:
out = self.peri_ln_mlp(out)
if self.mlp_resid_scaler is not None:
out = self.mlp_resid_scaler(out)
return out
Comment on lines +23 to +30
def forward(self, sources: list[torch.Tensor], destination: int) -> torch.Tensor:
if not sources:
raise ValueError("attention residuals require at least one source")
values = torch.stack(sources, dim=0) # depth, batch, time, channels
keys = F.rms_norm(values, (values.size(-1),), eps=self.eps)
scores = torch.einsum("dbtc,c->dbt", keys, self.queries[destination])
weights = scores.softmax(dim=0)
return torch.einsum("dbt,dbtc->btc", weights, values)
muon_exclude_substrings:
- [" "]

# CAPPED HS NORM AND HSNORM VARIATONIS
@gkielian
gkielian merged commit 5433ebc into ReaLLMASIC:master Aug 2, 2026
0 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants