@akshay_pachaar: How LLMs understand relative p...

@akshay_pachaar
37 views Nov 13, 2024
1
How LLMs understand relative positions of input words, clearly explained:
2
RoPE (Rotary Positional Embeddings) revolutionised the way positional information is encoded in LLMs and it's widely used by models like Llama-3.

Today, I'll clearly explain what they are & how positional embeddings evolved over time.

Let's go! 🚀
Media image
3
Why Positional embeddings❓

Let's first understand the concept of positional embeddings and why they are essential in the first place.

Without positional embeddings a transformer doesn't understand the relative arrangement of words in a sentence.
Media image
4
Absolute Positional embeddings

So, let's start with the simplest way to encode position of tokens (words) in an input.
Media image
5
Problem with absolute positional embeddings! ⚠️

Each positional embedding is independent of each other, they don't capture relative arrangements/positioning.

Check this out👇
Media image
6
Next up, let's talk about relative positional embeddings!

They definitely overcome the limitations of absolute positional embeddings, but they come with the drawback of increased parameters.

For a sequence of length N, we require 2N+1 positional embeddings.
Media image
7
Introducing Rotary Positional Embeddings (RoPE), the best of both worlds!

Instead of adding a token & its positional embedding, we rotate the token embedding by a fixed factor (theta) depending on its position in the sequence.

Imagine our token embeddings are 2-dimensional!
Media image
8
RoPE preserves relative position and relation.

For example, in the scenario below, the embedding vectors for the words 'love' and 'Tennis' will have the same cosine similarity as long as their relative positions stay the same, regardless of the sequence length.

Check this out👇
Media image
9
Mathematical representation.

This can be easily generalised to N dimensions by taking a pair at a time.

(I have also shared my detailed blog on self-attention at the end)

Check this out👇
Media image
10
If you interested in:

- Python 🐍
- ML/AI Engineering ⚙️

1. Find me → @akshay_pachaar ✔️
2. Subscribe to our Newsletter → @DailyDoseOfDS_ and get a FREE eBook, covering 150+ core DS/ML lessons!

Cheers!
Actions
Visual Editor Carousel Maker NEW
Update Thread
What You Can Do
  • Download as PDF
  • Save to Notion
  • Export as Markdown
  • Visual Editor
  • LinkedIn & Instagram Carousel Maker
Create Free Account

Includes 7-day Premium trial