|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +import pytest |
| 8 | +import torch |
| 9 | + |
| 10 | +from tests.test_utils import assert_expected |
| 11 | +from torch import tensor |
| 12 | + |
| 13 | +from torchtune.models.llama3_1._position_embeddings import Llama3ScaledRoPE |
| 14 | + |
| 15 | +from torchtune.utils.seed import set_seed |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture(autouse=True) |
| 19 | +def random(): |
| 20 | + set_seed(0) |
| 21 | + |
| 22 | + |
| 23 | +class TestLlama3ScaledRoPE: |
| 24 | + """ |
| 25 | + Class for testing our Scaled RoPE for LLama3.1 (RoPE) |
| 26 | + implementation. The expected tensors are computed from the |
| 27 | + reference implementation here: |
| 28 | + https://github.com/meta-llama/llama-models/blob/main/models/llama3_1/api/model.py#L272 |
| 29 | +
|
| 30 | + The expected values are computed using the following code: |
| 31 | + https://gist.github.com/joecummings/4f1331a9c1e5aa15bad1641acb74fe0e |
| 32 | + """ |
| 33 | + |
| 34 | + EXPECTED_FREQS_CIS_MEAN = tensor(0.1738) |
| 35 | + EXPECTED_FREQS_CIS_SUM = tensor(91141.7656) |
| 36 | + EXPECTED_FREQS_CIS_MAX = tensor(1.0) |
| 37 | + |
| 38 | + EXPECTED_X_OUT_MEAN = tensor(-2.4781e-06) |
| 39 | + EXPECTED_X_OUT_SUM = tensor(-83.1523) |
| 40 | + EXPECTED_X_OUT_MAX = tensor(5.4625) |
| 41 | + |
| 42 | + @pytest.fixture |
| 43 | + def input_params(self): |
| 44 | + bsz = 4 |
| 45 | + num_heads = 32 |
| 46 | + embed_dim = 4096 |
| 47 | + head_dim = embed_dim // num_heads |
| 48 | + seq_len = 2048 |
| 49 | + max_seq_len = 4096 |
| 50 | + return bsz, num_heads, head_dim, seq_len, max_seq_len |
| 51 | + |
| 52 | + @pytest.fixture |
| 53 | + def input(self, input_params) -> tensor: |
| 54 | + bsz, num_heads, head_dim, seq_len, _ = input_params |
| 55 | + return torch.randn(bsz, seq_len, num_heads, head_dim) |
| 56 | + |
| 57 | + @pytest.fixture |
| 58 | + def rope(self, input_params) -> Llama3ScaledRoPE: |
| 59 | + _, _, head_dim, _, max_seq_len = input_params |
| 60 | + return Llama3ScaledRoPE(dim=head_dim, max_seq_len=max_seq_len) |
| 61 | + |
| 62 | + def test_cache_equality(self, input, rope) -> None: |
| 63 | + # Have to explicitly call _rope_init() to initialize theta matrix |
| 64 | + rope._rope_init() |
| 65 | + cache = rope.cache |
| 66 | + |
| 67 | + assert_expected(cache.mean(), self.EXPECTED_FREQS_CIS_MEAN, atol=1e-4) |
| 68 | + assert_expected(cache.sum(), self.EXPECTED_FREQS_CIS_SUM, atol=1e-4) |
| 69 | + assert_expected(cache.max(), self.EXPECTED_FREQS_CIS_MAX) |
| 70 | + |
| 71 | + def test_forward(self, input, rope) -> None: |
| 72 | + x_out = rope(input) |
| 73 | + |
| 74 | + # check the numerics of the computed tensor |
| 75 | + assert_expected(x_out.mean(), self.EXPECTED_X_OUT_MEAN) |
| 76 | + assert_expected(x_out.sum(), self.EXPECTED_X_OUT_SUM) |
| 77 | + assert_expected(x_out.max(), self.EXPECTED_X_OUT_MAX) |
| 78 | + |
| 79 | + # check shapes |
| 80 | + assert_expected(x_out.shape, input.shape) |
| 81 | + |
| 82 | + def test_forward_with_curr_pos(self, input, rope) -> None: |
| 83 | + ( |
| 84 | + _, |
| 85 | + seq_len, |
| 86 | + _, |
| 87 | + _, |
| 88 | + ) = input.shape |
| 89 | + x_out = rope(input, input_pos=torch.arange(seq_len)) |
| 90 | + |
| 91 | + # these values should be exactly the same as test_forward |
| 92 | + # since in this case input_pos covers the entire input |
| 93 | + # sequence. This tests that input_pos works as expected i.e. |
| 94 | + # extracts the embeddings for the relevant positions |
| 95 | + assert_expected(x_out.mean(), self.EXPECTED_X_OUT_MEAN, atol=1e-4) |
| 96 | + assert_expected(x_out.sum(), self.EXPECTED_X_OUT_SUM) |
| 97 | + assert_expected(x_out.max(), self.EXPECTED_X_OUT_MAX) |
| 98 | + |
| 99 | + # check shapes |
| 100 | + assert_expected(x_out.shape, input.shape) |
| 101 | + |
| 102 | + def test_forward_with_2d_pos_ids(self, input, rope) -> None: |
| 103 | + """ |
| 104 | + Use input_pos to indicate positions of each token relative to its sequence |
| 105 | + when sample is packed. |
| 106 | + """ |
| 107 | + ( |
| 108 | + bsz, |
| 109 | + seq_len, |
| 110 | + _, |
| 111 | + _, |
| 112 | + ) = input.shape |
| 113 | + x_out = rope( |
| 114 | + input, input_pos=torch.arange(seq_len).unsqueeze(0).expand(bsz, seq_len) |
| 115 | + ) |
| 116 | + |
| 117 | + # these values should be exactly the same as test_forward |
| 118 | + # AND test_forward_with_current_pos. In this case input_pos |
| 119 | + # covers the entire batch dim and is defined for each sample separately. |
| 120 | + # This tests that input_pos works as expected i.e. |
| 121 | + # extracts the embeddings for the relevant positions for each sample |
| 122 | + assert_expected(x_out.mean(), self.EXPECTED_X_OUT_MEAN, atol=1e-4) |
| 123 | + assert_expected(x_out.sum(), self.EXPECTED_X_OUT_SUM) |
| 124 | + assert_expected(x_out.max(), self.EXPECTED_X_OUT_MAX) |
| 125 | + |
| 126 | + # check shapes |
| 127 | + assert_expected(x_out.shape, input.shape) |
| 128 | + |
| 129 | + def test_rope_init_meta_device(self, input_params): |
| 130 | + _, _, head_dim, _, max_seq_len = input_params |
| 131 | + rope_on_device = Llama3ScaledRoPE(dim=head_dim, max_seq_len=max_seq_len) |
| 132 | + with torch.device("meta"): |
| 133 | + meta_rope = Llama3ScaledRoPE(dim=head_dim, max_seq_len=max_seq_len) |
| 134 | + |
| 135 | + meta_rope._rope_init() |
| 136 | + for p1, p2 in zip(rope_on_device.buffers(), meta_rope.buffers()): |
| 137 | + torch.testing.assert_close(p1, p2) |
| 138 | + |
| 139 | + # Assert meta_rope cache is no longer on meta device |
| 140 | + assert meta_rope.cache.device != torch.device("meta") |
0 commit comments