Skip to content

Fast Rotary Position Embedding value different to transformers? #837

Closed

Description

@fahadh4ilyas

I'm testing unsloth rope and here is my script:

import torch
from unsloth.kernels.rope_embedding import fast_rope_embedding
from unsloth.models.llama import LlamaRotaryEmbedding as UnslothLlamaRotaryEmbedding
from transformers.models.llama.modeling_llama import LlamaRotaryEmbedding , rotate_half, apply_rotary_pos_emb

unsloth_rotary = UnslothLlamaRotaryEmbedding(dim=128, max_position_embeddings=8192, device='cuda')

Q = torch.randn((8192, 32, 128), device='cuda')
K = torch.randn((8192, 32, 128), device='cuda')

cos, sin = unsloth_rotary.cos_cached, unsloth_rotary.sin_cached

unsloth_Q, unsloth_K = fast_rope_embedding(Q.unsqueeze(0).transpose(1, 2), K.unsqueeze(0).transpose(1, 2), cos, sin)

llama_rotary = LlamaRotaryEmbedding(128, 8192, device='cuda')

llama_cos, llama_sin = llama_rotary(Q.unsqueeze(0).transpose(1, 2), torch.arange(0, 8192, dtype=torch.long, device='cuda').unsqueeze(0))

llama_Q, llama_K = apply_rotary_pos_emb(Q.unsqueeze(0).transpose(1, 2), K.unsqueeze(0).transpose(1, 2), llama_cos, llama_sin)

print('Maximum difference Q:', (unsloth_Q - llama_Q).abs().max())
print('Maximum difference K:', (unsloth_K - llama_K).abs().max())

Here is one of the result:

Maximum difference Q: tensor(10.7633, device='cuda:0')
Maximum difference K: tensor(10.0592, device='cuda:0')

The maximum difference is quite big. Why is that? Will it hurt the accuracy?

Activity

  1. danielhanchen commented on Jul 31, 2024

    @danielhanchen
    Member

    Thanks for the investigation! The trick with Unsloth is we overwrite Q and K to converse VRAM, so the 2nd time you call transformers after Unsloth's function call, the Q and K matrices were already changed. The code snippet below will be correct:

    import torch
    from unsloth.kernels.rope_embedding import fast_rope_embedding
    from unsloth.models.llama import LlamaRotaryEmbedding as UnslothLlamaRotaryEmbedding
    from transformers.models.llama.modeling_llama import LlamaRotaryEmbedding , rotate_half, apply_rotary_pos_emb
    
    unsloth_rotary = UnslothLlamaRotaryEmbedding(dim=128, max_position_embeddings=8192, device='cuda')
    
    torch.manual_seed(3407)
    torch.cuda.manual_seed(3407)
    Q = torch.randn((8192, 32, 128), device='cuda')
    K = torch.randn((8192, 32, 128), device='cuda')
    
    cos, sin = unsloth_rotary.cos_cached, unsloth_rotary.sin_cached
    
    unsloth_Q, unsloth_K = fast_rope_embedding(Q.unsqueeze(0).transpose(1, 2), K.unsqueeze(0).transpose(1, 2), cos, sin)
    
    llama_rotary = LlamaRotaryEmbedding(128, 8192, device='cuda')
    
    torch.manual_seed(3407)
    torch.cuda.manual_seed(3407)
    Q = torch.randn((8192, 32, 128), device='cuda')
    K = torch.randn((8192, 32, 128), device='cuda')
    llama_cos, llama_sin = llama_rotary(Q.unsqueeze(0).transpose(1, 2), torch.arange(0, 8192, dtype=torch.long, device='cuda').unsqueeze(0))
    
    llama_Q, llama_K = apply_rotary_pos_emb(Q.unsqueeze(0).transpose(1, 2), K.unsqueeze(0).transpose(1, 2), llama_cos, llama_sin)
    
    print('Maximum difference Q:', (unsloth_Q - llama_Q).abs().max())
    print('Maximum difference K:', (unsloth_K - llama_K).abs().max())

    which will print out

    Maximum difference Q: tensor(0.0001, device='cuda:0')
    Maximum difference K: tensor(0.0001, device='cuda:0')
  2. fahadh4ilyas commented on Jul 31, 2024

    @fahadh4ilyas
    Author

    Thanks for the investigation! The trick with Unsloth is we overwrite Q and K to converse VRAM, so the 2nd time you call transformers after Unsloth's function call, the Q and K matrices were already changed. The code snippet below will be correct:

    import torch
    from unsloth.kernels.rope_embedding import fast_rope_embedding
    from unsloth.models.llama import LlamaRotaryEmbedding as UnslothLlamaRotaryEmbedding
    from transformers.models.llama.modeling_llama import LlamaRotaryEmbedding , rotate_half, apply_rotary_pos_emb
    
    unsloth_rotary = UnslothLlamaRotaryEmbedding(dim=128, max_position_embeddings=8192, device='cuda')
    
    torch.manual_seed(3407)
    torch.cuda.manual_seed(3407)
    Q = torch.randn((8192, 32, 128), device='cuda')
    K = torch.randn((8192, 32, 128), device='cuda')
    
    cos, sin = unsloth_rotary.cos_cached, unsloth_rotary.sin_cached
    
    unsloth_Q, unsloth_K = fast_rope_embedding(Q.unsqueeze(0).transpose(1, 2), K.unsqueeze(0).transpose(1, 2), cos, sin)
    
    llama_rotary = LlamaRotaryEmbedding(128, 8192, device='cuda')
    
    torch.manual_seed(3407)
    torch.cuda.manual_seed(3407)
    Q = torch.randn((8192, 32, 128), device='cuda')
    K = torch.randn((8192, 32, 128), device='cuda')
    llama_cos, llama_sin = llama_rotary(Q.unsqueeze(0).transpose(1, 2), torch.arange(0, 8192, dtype=torch.long, device='cuda').unsqueeze(0))
    
    llama_Q, llama_K = apply_rotary_pos_emb(Q.unsqueeze(0).transpose(1, 2), K.unsqueeze(0).transpose(1, 2), llama_cos, llama_sin)
    
    print('Maximum difference Q:', (unsloth_Q - llama_Q).abs().max())
    print('Maximum difference K:', (unsloth_K - llama_K).abs().max())

    which will print out

    Maximum difference Q: tensor(0.0001, device='cuda:0')
    Maximum difference K: tensor(0.0001, device='cuda:0')

    So, fast rope embedding is change the tensor inplace?

  3. danielhanchen commented on Aug 2, 2024

    @danielhanchen
    Member

    Yes currently its in place

  4. oobabooga commented on Jul 28, 2026

    @oobabooga
    Member

    @fahadh4ilyas The discrepancy came from the comparison script: at the time, fast_rope_embedding rotated Q and K in place, so the transformers reference ran on already-rotated tensors.

    Regenerating them gives a ~1e-4 difference, which is ordinary float32 error. Closing as answered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions