|
1 | 1 | import copy
|
2 |
| -import unittest |
| 2 | +from dataclasses import dataclass |
3 | 3 |
|
| 4 | +import pytest |
4 | 5 | import torch
|
| 6 | +import torch.nn as nn |
| 7 | +import torch.test |
5 | 8 | import torch_xla
|
6 | 9 | from omegaconf import OmegaConf
|
7 | 10 | from transformers import AutoConfig
|
|
10 | 13 | from torchprime.torch_xla_models.llama import LlamaForCausalLM
|
11 | 14 |
|
12 | 15 |
|
13 |
| -class TestYourModule(unittest.TestCase): |
14 |
| - def setUp(self): |
15 |
| - super().setUp() |
16 |
| - torch.manual_seed(42) |
17 |
| - torch_xla.manual_seed(42) |
18 |
| - self.vocab_size = 128 |
19 |
| - config = AutoConfig.from_pretrained( |
20 |
| - "meta-llama/Meta-Llama-3-8B", |
21 |
| - num_hidden_layers=1, |
22 |
| - num_attention_heads=8, |
23 |
| - hidden_size=8, |
24 |
| - intermediate_size=16, |
25 |
| - vocab_size=self.vocab_size, |
26 |
| - ) |
27 |
| - config.flash_attention = False |
28 |
| - torchprime_config = OmegaConf.create( |
29 |
| - { |
30 |
| - "vocab_size": 128, |
31 |
| - "hidden_size": 8, |
32 |
| - "intermediate_size": 16, |
33 |
| - "num_hidden_layers": 1, |
34 |
| - "num_attention_heads": 8, |
35 |
| - "num_key_value_heads": 8, |
36 |
| - "hidden_act": "silu", |
37 |
| - "max_position_embeddings": 8192, |
38 |
| - "initializer_range": 0.02, |
39 |
| - "rms_norm_eps": 1.0e-05, |
40 |
| - "attention_dropout": False, |
41 |
| - "attention_bias": False, |
42 |
| - "flash_attention": False, |
43 |
| - "rope_theta": 500000.0, |
44 |
| - } |
45 |
| - ) |
46 |
| - # place model on CPU device first |
47 |
| - with torch.device("cpu"): |
48 |
| - self.hf_model = HfLlamaForCausalLM(config) |
49 |
| - self.model = LlamaForCausalLM(torchprime_config) |
50 |
| - self.model.load_state_dict(self.hf_model.state_dict()) |
| 16 | +@dataclass |
| 17 | +class LlamaFixture: |
| 18 | + vocab_size: int |
| 19 | + hf_model: HfLlamaForCausalLM |
| 20 | + model: LlamaForCausalLM |
51 | 21 |
|
52 |
| - def test_forward_our_model_against_hf_model(self): |
53 |
| - device = torch_xla.device() |
54 |
| - model_xla = copy.deepcopy(self.model).to(device) |
55 |
| - hf_model_xla = copy.deepcopy(self.hf_model).to(device) |
56 |
| - torch_xla.sync() |
57 |
| - input_sizes = [8, 128, 256] |
58 |
| - for input_size in input_sizes: |
59 |
| - input = torch.randint(128, ((2, input_size // 2))).to(device) |
60 |
| - hf_output = hf_model_xla( |
61 |
| - input, labels=input, attention_mask=torch.ones_like(input) |
62 |
| - ) |
63 |
| - llama_xla_logits, llama_xla_loss = model_xla( |
64 |
| - input, labels=input, attention_mask=torch.ones_like(input) |
65 |
| - ) |
66 |
| - torch_xla.sync() |
67 |
| - self.assertTrue( |
68 |
| - torch.allclose(hf_output.logits, llama_xla_logits, atol=1e-6), |
69 |
| - "logits are not equal", |
70 |
| - ) |
71 |
| - self.assertTrue( |
72 |
| - torch.allclose(hf_output.loss, llama_xla_loss, atol=1e-6), |
73 |
| - "loss is not equal", |
74 |
| - ) |
75 | 22 |
|
76 |
| - def test_forward_torch_xla_against_native(self): |
77 |
| - input_size = 8 |
78 |
| - device = torch.device("cpu") |
79 |
| - input = torch.randint(self.vocab_size, ((2, input_size // 2))) |
80 |
| - llama_native_logits, llama_native_loss = self.model( |
81 |
| - input, labels=input, attention_mask=torch.ones_like(input) |
| 23 | +def get_llama_3_8b() -> LlamaFixture: |
| 24 | + torch.manual_seed(42) |
| 25 | + torch_xla.manual_seed(42) |
| 26 | + vocab_size = 128 |
| 27 | + config = AutoConfig.from_pretrained( |
| 28 | + "meta-llama/Meta-Llama-3-8B", |
| 29 | + num_hidden_layers=1, |
| 30 | + num_attention_heads=8, |
| 31 | + hidden_size=64, |
| 32 | + intermediate_size=16, |
| 33 | + vocab_size=vocab_size, |
| 34 | + ) |
| 35 | + config.flash_attention = False |
| 36 | + torchprime_config = OmegaConf.create( |
| 37 | + { |
| 38 | + "vocab_size": 128, |
| 39 | + "hidden_size": 64, |
| 40 | + "intermediate_size": 16, |
| 41 | + "num_hidden_layers": 1, |
| 42 | + "num_attention_heads": 8, |
| 43 | + "num_key_value_heads": 8, |
| 44 | + "hidden_act": "silu", |
| 45 | + "max_position_embeddings": 8192, |
| 46 | + "initializer_range": 0.02, |
| 47 | + "rms_norm_eps": 1.0e-05, |
| 48 | + "attention_dropout": False, |
| 49 | + "attention_bias": False, |
| 50 | + "flash_attention": False, |
| 51 | + "rope_theta": 500000.0, |
| 52 | + } |
| 53 | + ) |
| 54 | + # Place model on CPU device first |
| 55 | + with torch.device("cpu"): |
| 56 | + hf_model = HfLlamaForCausalLM(config) |
| 57 | + model = LlamaForCausalLM(torchprime_config) |
| 58 | + model.load_state_dict(hf_model.state_dict()) |
| 59 | + return LlamaFixture(vocab_size, hf_model, model) |
| 60 | + |
| 61 | + |
| 62 | +def get_llama_3_1_405b() -> LlamaFixture: |
| 63 | + torch.manual_seed(42) |
| 64 | + torch_xla.manual_seed(42) |
| 65 | + vocab_size = 256 |
| 66 | + config = AutoConfig.from_pretrained( |
| 67 | + "meta-llama/Meta-Llama-3.1-405B", |
| 68 | + num_hidden_layers=2, |
| 69 | + num_attention_heads=8, |
| 70 | + hidden_size=64, |
| 71 | + intermediate_size=32, |
| 72 | + vocab_size=vocab_size, |
| 73 | + ) |
| 74 | + config.flash_attention = False |
| 75 | + torchprime_config = OmegaConf.create( |
| 76 | + { |
| 77 | + "vocab_size": 256, |
| 78 | + "hidden_size": 64, |
| 79 | + "intermediate_size": 32, |
| 80 | + "num_hidden_layers": 2, |
| 81 | + "num_attention_heads": 8, |
| 82 | + "num_key_value_heads": 8, |
| 83 | + "hidden_act": "silu", |
| 84 | + "max_position_embeddings": 131072, |
| 85 | + "initializer_range": 0.02, |
| 86 | + "rms_norm_eps": 1.0e-05, |
| 87 | + "attention_dropout": False, |
| 88 | + "attention_bias": False, |
| 89 | + "flash_attention": False, |
| 90 | + "rope_theta": 500000.0, |
| 91 | + "rope_scaling": { |
| 92 | + "factor": 8.0, |
| 93 | + "low_freq_factor": 1.0, |
| 94 | + "high_freq_factor": 4.0, |
| 95 | + "original_context_len": 8192, |
| 96 | + }, |
| 97 | + } |
| 98 | + ) |
| 99 | + # Place model on CPU device first |
| 100 | + with torch.device("cpu"): |
| 101 | + hf_model = HfLlamaForCausalLM(config) |
| 102 | + model = LlamaForCausalLM(torchprime_config) |
| 103 | + # Assert that the `inv_freq` values are the same |
| 104 | + assert isinstance(model.model.layers[0].self_attn, nn.Module) |
| 105 | + assert isinstance(hf_model.model.layers[0].self_attn, nn.Module) |
| 106 | + assert isinstance(model.model.layers[0].self_attn.rotary_emb, nn.Module) |
| 107 | + assert isinstance(hf_model.model.layers[0].self_attn.rotary_emb, nn.Module) |
| 108 | + torch.testing.assert_close( |
| 109 | + model.model.layers[0].self_attn.rotary_emb.inv_freq, |
| 110 | + hf_model.model.layers[0].self_attn.rotary_emb.inv_freq, |
82 | 111 | )
|
| 112 | + # In this simplified model architecture, hidden_size 64 / num_attention_heads 8 = 8 head dim, |
| 113 | + # and the inv_freq size is half of the head dim. |
| 114 | + assert model.model.layers[0].self_attn.rotary_emb.inv_freq.shape == (4,) |
| 115 | + model.load_state_dict(hf_model.state_dict()) |
| 116 | + return LlamaFixture(vocab_size, hf_model, model) |
83 | 117 |
|
84 |
| - device = torch_xla.device() |
85 |
| - input = input.to(device) |
86 |
| - model_xla = copy.deepcopy(self.model).to(device) |
87 |
| - torch_xla.sync() |
88 | 118 |
|
| 119 | +@pytest.mark.parametrize( |
| 120 | + "fixture", |
| 121 | + [get_llama_3_8b, get_llama_3_1_405b], |
| 122 | + ids=["Llama 3.0 8B", "Llama 3.1 405B"], |
| 123 | +) |
| 124 | +def test_forward_our_model_against_hf_model(fixture): |
| 125 | + fixture = fixture() |
| 126 | + device = torch_xla.device() |
| 127 | + model_xla = copy.deepcopy(fixture.model).to(device) |
| 128 | + hf_model_xla = copy.deepcopy(fixture.hf_model).to(device) |
| 129 | + torch_xla.sync() |
| 130 | + input_sizes = [8, 128, 256] |
| 131 | + for input_size in input_sizes: |
| 132 | + input = torch.randint(fixture.vocab_size, ((2, input_size // 2))).to(device) |
| 133 | + hf_output = hf_model_xla(input, labels=input, attention_mask=torch.ones_like(input)) |
89 | 134 | llama_xla_logits, llama_xla_loss = model_xla(
|
90 | 135 | input, labels=input, attention_mask=torch.ones_like(input)
|
91 | 136 | )
|
92 | 137 | torch_xla.sync()
|
93 |
| - self.assertTrue( |
94 |
| - torch.allclose(llama_native_logits, llama_xla_logits.to("cpu"), atol=1e-2), |
95 |
| - "CPU run and XLA run logits are not equal", |
| 138 | + torch.testing.assert_close( |
| 139 | + hf_output.logits, |
| 140 | + llama_xla_logits, |
| 141 | + atol=1e-6, |
| 142 | + rtol=1e-9, |
| 143 | + msg="logits are not equal", |
96 | 144 | )
|
97 |
| - self.assertTrue( |
98 |
| - torch.allclose(llama_native_loss, llama_xla_loss.to("cpu"), atol=1e-2), |
99 |
| - "CPU run and XLA run loss is not equal", |
| 145 | + torch.testing.assert_close( |
| 146 | + hf_output.loss, llama_xla_loss, atol=1e-6, rtol=1e-9, msg="loss is not equal" |
100 | 147 | )
|
101 | 148 |
|
102 | 149 |
|
103 |
| -if __name__ == "__main__": |
104 |
| - unittest.main() |
| 150 | +@pytest.mark.parametrize( |
| 151 | + "fixture", |
| 152 | + [get_llama_3_8b, get_llama_3_1_405b], |
| 153 | + ids=["Llama 3.0 8B", "Llama 3.1 405B"], |
| 154 | +) |
| 155 | +def test_forward_torch_xla_against_native(fixture): |
| 156 | + fixture = fixture() |
| 157 | + input_size = 8 |
| 158 | + device = torch.device("cpu") |
| 159 | + input = torch.randint(fixture.vocab_size, ((2, input_size // 2))) |
| 160 | + llama_native_logits, llama_native_loss = fixture.model( |
| 161 | + input, labels=input, attention_mask=torch.ones_like(input) |
| 162 | + ) |
| 163 | + |
| 164 | + device = torch_xla.device() |
| 165 | + input = input.to(device) |
| 166 | + model_xla = copy.deepcopy(fixture.model).to(device) |
| 167 | + torch_xla.sync() |
| 168 | + |
| 169 | + llama_xla_logits, llama_xla_loss = model_xla( |
| 170 | + input, labels=input, attention_mask=torch.ones_like(input) |
| 171 | + ) |
| 172 | + torch_xla.sync() |
| 173 | + torch.testing.assert_close( |
| 174 | + llama_native_logits, |
| 175 | + llama_xla_logits.to("cpu"), |
| 176 | + atol=1e-2, |
| 177 | + rtol=1e-6, |
| 178 | + msg="CPU run and XLA run logits are not equal", |
| 179 | + ) |
| 180 | + torch.testing.assert_close( |
| 181 | + llama_native_loss, |
| 182 | + llama_xla_loss.to("cpu"), |
| 183 | + atol=1e-2, |
| 184 | + rtol=1e-6, |
| 185 | + msg="CPU run and XLA run loss is not equal", |
| 186 | + ) |
0 commit comments