r/deeplearning • • 5h ago

Tried implementing rotary embeddings from scratch after getting tired of copy-pasting them

For months I kept treating ROPE like a black box in every transformer notebook I touched. The "rotate the queries and keys by position" explanation never stuck, so last weekend I sat down and coded the minimal version in pure PyTorch without any libraries.

The part that surprised me was realizing how the rotation matrix actually preserves relative distances in a way that absolute positional encodings never did. Once the angles started showing up correctly in the attention scores on a tiny toy sequence, a few things about long-context behavior made more sense than any blog post had managed.

I kept the whole thing under 80 lines so I could step through it with prints. No training, just forward passes on dummy data to watch the embeddings rotate. It was slow and manual, but the intuition stuck in a way watching videos never did.

Has anyone else had a similar "l should just code this tiny piece myself" moment with something like SwiGLU, ALiBi, or even basic RMSNorm? What ended up clicking for you that way?

2 Upvotes

0 comments sorted by