|
| 1 | +import math |
| 2 | + |
| 3 | +import pytest |
| 4 | +import torch |
| 5 | +from transformers import PretrainedConfig |
| 6 | +from transformers.modeling_rope_utils import ROPE_INIT_FUNCTIONS |
| 7 | + |
| 8 | +from torchprime.rope import rope |
| 9 | + |
| 10 | +LLAMA3_SCALING = rope.RopeScaling( |
| 11 | + factor=8, |
| 12 | + low_freq_factor=1, |
| 13 | + high_freq_factor=4, |
| 14 | + original_context_len=8192, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +@pytest.mark.parametrize( |
| 19 | + "hidden_size, num_attention_heads, theta", |
| 20 | + [(4096, 32, 500000.0), (16384, 128, 500000.0), (65536, 128, 500000.0)], |
| 21 | +) |
| 22 | +class TestRope: |
| 23 | + def test_default_rope(self, hidden_size, num_attention_heads, theta): |
| 24 | + head_dim = hidden_size // num_attention_heads |
| 25 | + ours = rope.default_rope_frequencies(head_dim=head_dim, theta=theta) |
| 26 | + |
| 27 | + hf_rope_fn = ROPE_INIT_FUNCTIONS["default"] |
| 28 | + hf, scale = hf_rope_fn( |
| 29 | + PretrainedConfig.from_dict( |
| 30 | + { |
| 31 | + "hidden_size": hidden_size, |
| 32 | + "num_attention_heads": num_attention_heads, |
| 33 | + "rope_theta": theta, |
| 34 | + } |
| 35 | + ) |
| 36 | + ) |
| 37 | + |
| 38 | + assert scale == 1 |
| 39 | + torch.testing.assert_close(ours, hf) |
| 40 | + |
| 41 | + def test_llama3_rope_against_hf(self, hidden_size, num_attention_heads, theta): |
| 42 | + head_dim = hidden_size // num_attention_heads |
| 43 | + ours = rope.llama3_rope_frequencies( |
| 44 | + head_dim=head_dim, |
| 45 | + theta=theta, |
| 46 | + scaling=LLAMA3_SCALING, |
| 47 | + ) |
| 48 | + |
| 49 | + hf_rope_fn = ROPE_INIT_FUNCTIONS["llama3"] |
| 50 | + hf, scale = hf_rope_fn( |
| 51 | + PretrainedConfig.from_dict( |
| 52 | + { |
| 53 | + "hidden_size": hidden_size, |
| 54 | + "num_attention_heads": num_attention_heads, |
| 55 | + "rope_theta": theta, |
| 56 | + "rope_scaling": { |
| 57 | + "factor": 8, |
| 58 | + "low_freq_factor": 1, |
| 59 | + "high_freq_factor": 4, |
| 60 | + "original_max_position_embeddings": 8192, |
| 61 | + }, |
| 62 | + } |
| 63 | + ), |
| 64 | + device="cpu", |
| 65 | + ) |
| 66 | + |
| 67 | + assert scale == 1 |
| 68 | + torch.testing.assert_close(ours, hf) |
| 69 | + |
| 70 | + def test_llama3_rope_against_reference(self, hidden_size, num_attention_heads, theta): |
| 71 | + head_dim = hidden_size // num_attention_heads |
| 72 | + ours = rope.llama3_rope_frequencies( |
| 73 | + head_dim=head_dim, |
| 74 | + theta=theta, |
| 75 | + scaling=LLAMA3_SCALING, |
| 76 | + ) |
| 77 | + reference = _llama3_reference_apply_scaling( |
| 78 | + rope.default_rope_frequencies(head_dim=head_dim, theta=theta) |
| 79 | + ) |
| 80 | + torch.testing.assert_close(ours, reference) |
| 81 | + |
| 82 | + |
| 83 | +def _llama3_reference_apply_scaling(freqs: torch.Tensor): |
| 84 | + """ |
| 85 | + Reference from https://github.com/karpathy/llm.c/blob/7ecd8906afe6ed7a2b2cdb731c042f26d525b820/train_llama3.py#L80 |
| 86 | + """ |
| 87 | + # Values obtained from grid search |
| 88 | + scale_factor = 8 |
| 89 | + low_freq_factor = 1 |
| 90 | + high_freq_factor = 4 |
| 91 | + old_context_len = 8192 # original llama3 length |
| 92 | + |
| 93 | + low_freq_wavelen = old_context_len / low_freq_factor |
| 94 | + high_freq_wavelen = old_context_len / high_freq_factor |
| 95 | + new_freqs = [] |
| 96 | + for freq in freqs: |
| 97 | + wavelen = 2 * math.pi / freq |
| 98 | + if wavelen < high_freq_wavelen: |
| 99 | + new_freqs.append(freq) |
| 100 | + elif wavelen > low_freq_wavelen: |
| 101 | + new_freqs.append(freq / scale_factor) |
| 102 | + else: |
| 103 | + assert low_freq_wavelen != high_freq_wavelen |
| 104 | + smooth = (old_context_len / wavelen - low_freq_factor) / ( |
| 105 | + high_freq_factor - low_freq_factor |
| 106 | + ) |
| 107 | + new_freqs.append((1 - smooth) * freq / scale_factor + smooth * freq) |
| 108 | + return torch.tensor(new_freqs, dtype=freqs.dtype, device=freqs.device) |
0 commit comments