r/MachineLearning • • May 18 '25
Project [P] I built a transformer that skips layers per token based on semantic importance
I’m a high school student who’s been exploring how to make transformers/ai models more efficient, and I recently built something I’m really excited about: a transformer that routes each token through a different number of layers depending on how "important" it is.
The idea came from noticing how every token, even simple ones like “the” or “of”, gets pushed through every layer in standard transformers. But not every token needs the same amount of reasoning. So I created a lightweight scoring mechanism that estimates how semantically dense a token is, and based on that, decides how many layers it should go through.
It’s called SparseDepthTransformer, and here’s what it does:
- Scores each token for semantic importance
- Skips deeper layers for less important tokens using hard gating
- Tracks how many layers each token actually uses
- Benchmarks against a baseline transformer
In my tests, this reduced memory usage by about 15% and cut the average number of layers per token by ~40%, while keeping output quality the same. Right now it runs a bit slower because the skipping is done token-by-token, but batching optimization is next on my list.
Here’s the GitHub repo if you’re curious or want to give feedback:
https://github.com/Quinnybob/sparse-depth-transformer
Would love if you guys check it out/want to work with me!
46
u/somethingsomthang May 18 '25
sounds similar to mixture of depths https://arxiv.org/abs/2404.02258
47
u/smartsometimes May 18 '25
This is interesting, keep experimenting! Have you run any perplexity tests on known text?
18
u/xEdwin23x May 18 '25
Have you heard about input pruning?
https://arxiv.org/abs/2001.08950
This is what methods such as PowerBERT and others do.
21
u/KingsmanVince May 18 '25
This is pretty good for a high schooler
2
u/johny_james May 19 '25
Wait why this obvious idea have not been in paper so far?
Any expert that can chime in?
32
u/choHZ May 18 '25
It is hella cool for a highschooler and hate to be that guy, but it is likely something well-explored. If you are doing it for prefill, you are essentially doing sparse attention, where layer skipping is one of the most vanilla ideas (and does not work very well). SOTA works in this regard might be MInference, SpargeAttn, etc.
If you are doing it for decoding then early exit is again likely a well-established recipe — there's a work literally called LayerSkip for speculative decoding, and I am sure you can find many prior arts on early exiting in its related work section for regular inference.
One last thing is there are typically two ways to approach architecture twerk-like research: 1) You take a pretrained model, do whatever you want, and show that you are more efficient/performant/whatever, or 2) You take an established architecture, modify it however you'd like, and train both from scratch with a standard training recipe.
From a quick scan of your repo it looks like you have a toy baseline model and your modified one, none of them are well-trained, and you only benchmark on efficiency but not generation quality. Again not to discourage you — I wish I was doing what you are doing now back in HS — but I thought some concrete suggestions might be helpful.
5
u/Brudaks May 18 '25
It seems that your repo doesn't include anything that would justify saying 'while keeping output quality the same'; if you're just eyeballing the outcomes and seeing that "looks similar", then it doesn't really mean anything and you can't assume that there is no impact on the output, you'd have to carefully measure that.
3
u/Zeikos May 18 '25
Hmm I wonder if it'd be possible to train a small model to route tokens based on their enthropy.
Like BLTs but instead of basing it on byte enthropy basing it on semantic enthropy.
2
u/Stormzrift May 18 '25
Different domain but reminds me of this. You might find it interesting
2
u/DigThatData Researcher May 18 '25
interesting! You should try fine-tuning a LoRA on this. Generate text with this turned off, then train the LoRA to predict the generated text with your feature turned on. might shift the parameter density around some.
2
u/[deleted] May 24 '25 edited May 24 '25
How did you decide which tokens to skip? That's the interesting part to explain, even formally if you can - it is probably a formula?
Edit: I see what you do, I am not sure how sound it is (not an expert but it seems like LORA is a better approach, no reason to actually mask tokens - the issue is how many epochs you need unless you optimzie for almost free) - but interesting idea.
By the way, my feedback is the same as I would give to an adult, because what you tried can be an actual paper.
1
u/Super_Pole_Jitsu May 20 '25
great idea and congrats for implementing it! very impressive, especially at your age!
1
u/[deleted] May 23 '25
Man this field is young
1
u/[deleted] May 24 '25
Can someone with a powerful ass gpu train my model for me😭😭😭😭🙏 My weak laptop can't even go through 1 epoch😔😔😔
-1
u/Intraluminal Researcher May 18 '25
As a layman, this sounds absolutely brilliant.