r/MachineLearning • u/WAMFT • 18d ago
Research I merged attention and MLP processing into a single gated Transformer block. GAP [R]
Hi everyone!
I've been experimenting with an alternative Transformer architecture called GAP (Gated Attention Projection).
The main idea is to combine attention and MLP-style processing into a single gated block, instead of using the standard separate attention and feed-forward sublayers.
I tested it against a standard Transformer using 100 million scored training tokens per model from WikiText-103.
Some results:
GAP20 used 19.91% fewer parameters than the standard Transformer.
A smaller configuration can reduce parameters by 34.31%, although that version did not fair as well as the current model against the regular transformer, so i ended up giving 14% of my perameters savings to the GAP model for extra width, leaving the model at about 20% fewer perameters, which is why i called it in testing GAP20
The standard Transformer performed better on final language-model accuracy.
Compiling GAP20 with torch.compile improved training throughput by approximately 19.9% in one CPU benchmark compared with running GAP20 without compilation.
I've included the architecture code, configurable model sizes, speed benchmark and research paper.
This is an experimental architecture, not a claim that it outperforms Transformers overall. I'd be interested in feedback on the design, mathematics and possible improvements.
GitHub:
13
u/thexylophone 17d ago
If you want to put together a good scientific paper, it's good to separate components of your method and analyze them. Your headline result that it's 20% fewer parameters and 20% better training throughput is mostly to do with the model width, qk size per head, etc. changes that you've made rather than the changes you've made to the block described in the equations. As far as I can tell from those equations, the main changes are the attention module uses post layer norm instead of pre layer norm in the standard transformer and that the residual for the MLP is the input to the attention module. So what I'm saying is you should ablate all of those decisions if you want to generate an informative result. This is also ignoring the fact that you also seem to pay a quality penalty for your proposed block.
1
u/Formal-Database-6811 17d ago
Isn’t this pretty much just a GLU?
1
u/Aishor 16d ago
Thanks for sharing this and for being so upfront about the limitations; it’s rare to see a negative result reported this clearly. I read
models.pyandbenchmark_compile.py, and here are some thoughts on the design:1. A parameter-matched control is missing. With 20% fewer parameters, some loss is expected, so this comparison can’t separate the effect of the architecture from the effect of size. I’d add a standard Transformer cut down to ~270K, for example with a lower
mlp_ratioor a smaller width. If GAP20 beats that, there’s a signal; if it loses, the block isn’t adding anything.2. Attention doesn’t reach the residual stream directly. In
y = x + W_O(z * SiLU(W_G(z)))the residual starts fromx, not fromx + attn. So the attention output only reaches the stream through LN2, the gate andW_O, and you’ve also removed the attention output projection. I’d tryy = h + W_O(...)withh = x + Attn(LN1(x)): identical parameters and a cleaner gradient path for attention.3. The gate has no expansion.
W_GandW_Oare width×width, so the block is a SwiGLU without a wide hidden layer. In current LLMs, most of the capacity to memorize lives in that expansion. That may explain why neither model recalled facts in your synthetic tests. A variant with a 2× expansion, using the parameters you save, would be worth trying.4. Speed. The +19.9% compares compiled GAP20 with eager GAP20, so it measures
torch.compile. The interesting measurement would be GAP20 vs the standard model, both compiled and on GPU. Without the MLP, GAP20 should be genuinely faster, and that would be an argument in its favor if quality got close.5. Statistics. With a single seed and ~300K-parameter models, the perplexity gap (40.0 vs 45.6) is probably real, but any small improvement in future variants will fall within the noise. With 3 seeds per configuration, and if possible a 10M+ parameter model, the conclusions would carry much more weight.
6. Related work. Compare against Gated Attention (Qwen, 2025: a sigmoid gate on the attention output) and against Transformers with a reduced MLP or no MLP. Positioning GAP against them helps show what’s new about it.
In our case, training small alternative architectures, widening the MLP was one of the few things that clearly improved the loss. That suggests the “merge the MLP” part is the one that costs the most. Good luck with the next iteration.
1
u/[deleted] 18d ago
[removed] — view removed comment