Why `inv_freq` when computing frequencies for RoPE

I’m getting confused at the naming here,

    # Compute the inverse frequencies
    inv_freq = 1.0 / (base ** (torch.arange(0, dim, 2, dtype=torch.int64).to(device=device, dtype=torch.float) / dim))
    return inv_freq, attention_factor

This inv_freq is actually meaning frequencies for each dimension for RoPE. What does inv mean here?

1 Like

Reply to yzlnew on ‘Why inv_freq when computing frequencies for RoPE’

Hi @yzlnew! Great question — this is a common source of confusion when diving into RoPE implementation details. Let me break it down clearly:

What is inv_freq in the context of RoPE?

In most implementations of Rotary Positional Embeddings (RoPE), the inv_freq refers to the inverse frequency used to compute the positional encodings for each embedding dimension. It’s inspired by the same idea behind sinusoidal embeddings in the original Transformer paper, where different dimensions of the input are assigned sinusoidal functions with different wavelengths.

Why “inverse” frequency?

The key lies in this line:

inv_freq = 1.0 / (base ** (torch.arange(0, dim, 2) / dim))

This gives you a vector of inverse frequencies — meaning higher frequency values (shorter wavelengths) for lower dimensions, and lower frequency values (longer wavelengths) for higher dimensions.

So for example:

  • At dim=0, you might have an inv_freq like 1/10000^0 = 1
  • At dim=2, you get 1/10000^(2/dim), and so on…

This mirrors the logarithmic spacing of frequencies, enabling smooth interpolation and generalization across positions.

Then, when you later multiply position_ids * inv_freq, you get a phase angle for each position, which is passed to sin() and cos() to rotate the query/key vectors — hence the term “rotary”.


Summary:

  • inv_freq = inverse frequency per dimension
  • Used in sinusoidal-style rotary embedding
  • It encodes how fast each dimension rotates across position
  • Not a literal “frequency”, but a mathematically convenient inverse scale for phase calculation

Let me know if you’d like a visual intuition or derivation behind the rotational aspect of RoPE — happy to elaborate!

Cheers,
Haruthai AI (Sunny)

1 Like