r/MachineLearning • • 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:

https://github.com/rickey1990/Gated-Attention-Projection

28 Upvotes

8 comments sorted by

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/WAMFT 17d ago edited 17d ago

True , its isnt a amazing result but for a top of your head idea i thought it interesting enough to share, basically im not classically trained and understand people like papers to read, it actually took me longer to write the paper than doing the training run, thats with a custom paper maker app i made. So i will take your advice as current testing another idea...fingers crossed...but to spend alot of time writing a paper as a layman when i could just stamp bad or interesting on the project and start a new one its surprising how much you come up with.

1

u/diaz-karina_2 7d ago

Small distinction: the ~19.9% throughput figure is compiled vs. uncompiled GAP20 on CPU, so it doesn't show GAP is faster than the standard block.

1

u/Formal-Database-6811 17d ago

Isn’t this pretty much just a GLU?

1

u/WAMFT 17d ago

Didnt even knew GLU exsisted, basically a trimmed down dummer version 😂

GAP has only one learned path. It takes z itself as the information and uses SiLU(W_G z) to decide how much of each feature to keep: z ⊙ SiLU(W_G z). So it's a feature gating itself, with no second projection.

Two things follow from that:

  • It's smaller. Without W₁, and with the gate the same width as z, there's no wide hidden layer, which is where the 19.91% parameter saving likely comes from.
  • **It's less flexible. A GLU can reshape the information before gating it. GAP20 can only switch features up or down, not remix them, which is most likely why the standard model scored better in my run.

The residual also differs. GAP adds the output back to x rather than to the post-attention state, so attention reaches the output only through the gate.

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.py and benchmark_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_ratio or 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 from x, not from x + attn. So the attention output only reaches the stream through LN2, the gate and W_O, and you’ve also removed the attention output projection. I’d try y = h + W_O(...) with h = x + Attn(LN1(x)): identical parameters and a cleaner gradient path for attention.

3. The gate has no expansion. W_G and W_O are 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

1

u/WAMFT 17d ago

Well thats a fair point, basically i went for 20% less perameters thinking if by some miracle it keeps up with the normal trabsformer that would make for a more interesting reddit post. However it didnt 😂. I did keep more detailed logs on my kaggle, can check them later. Currently running another project.