|
9 | 9 | import torch
|
10 | 10 | from tests.test_utils import assert_expected, fixed_init_model
|
11 | 11 | from torch import nn
|
12 |
| -from torchtune.modules.moe import GroupedExperts |
| 12 | +from torchtune.modules.moe import GroupedExperts, LoRAGroupedExperts |
| 13 | +from torchtune.modules.peft import LoRALinear |
13 | 14 | from torchtune.training.seed import set_seed
|
14 | 15 |
|
| 16 | +RANK = 4 |
| 17 | +ALPHA = 1.0 |
| 18 | +SEQ_LEN = 32 |
| 19 | + |
15 | 20 |
|
16 | 21 | @pytest.fixture(autouse=True)
|
17 | 22 | def random():
|
@@ -57,3 +62,101 @@ def test_forward(self, experts, num_tokens_per_expert, dim):
|
57 | 62 |
|
58 | 63 | assert out.shape == (16, dim)
|
59 | 64 | assert_expected(out.mean().item(), 120.8260, atol=1e-3, rtol=1e-3)
|
| 65 | + |
| 66 | + |
| 67 | +class TestLoRAGroupedExperts: |
| 68 | + @pytest.fixture |
| 69 | + def dim(self) -> int: |
| 70 | + return 64 |
| 71 | + |
| 72 | + @pytest.fixture |
| 73 | + def hidden_dim(self) -> int: |
| 74 | + return 128 |
| 75 | + |
| 76 | + @pytest.fixture |
| 77 | + def num_experts(self) -> int: |
| 78 | + return 8 |
| 79 | + |
| 80 | + @pytest.fixture |
| 81 | + def experts_per_token(self) -> int: |
| 82 | + return 2 |
| 83 | + |
| 84 | + @pytest.fixture |
| 85 | + def num_tokens_per_expert(self, num_experts) -> int: |
| 86 | + return torch.tensor([1, 2, 1, 4, 3, 1, 2, 2], dtype=torch.int) |
| 87 | + |
| 88 | + @pytest.fixture |
| 89 | + def inputs(self, dim, num_experts, experts_per_token) -> torch.Tensor: |
| 90 | + inputs = torch.randn(num_experts * experts_per_token, SEQ_LEN, dim) |
| 91 | + return inputs |
| 92 | + |
| 93 | + @pytest.fixture |
| 94 | + def experts(self, dim, hidden_dim, num_experts) -> nn.Module: |
| 95 | + experts = GroupedExperts( |
| 96 | + dim=dim, |
| 97 | + hidden_dim=hidden_dim, |
| 98 | + num_experts=num_experts, |
| 99 | + ) |
| 100 | + fixed_init_model(experts, min_val=-0.1, max_val=0.1) |
| 101 | + return experts |
| 102 | + |
| 103 | + @pytest.fixture |
| 104 | + def lora_experts(self, dim, hidden_dim, num_experts) -> nn.Module: |
| 105 | + experts = LoRAGroupedExperts( |
| 106 | + dim=dim, |
| 107 | + hidden_dim=hidden_dim, |
| 108 | + num_experts=num_experts, |
| 109 | + rank=RANK, |
| 110 | + alpha=ALPHA, |
| 111 | + ) |
| 112 | + fixed_init_model(experts, min_val=-0.1, max_val=0.1) |
| 113 | + return experts |
| 114 | + |
| 115 | + @pytest.fixture |
| 116 | + def lora_linear(self, dim, hidden_dim): |
| 117 | + def create_lora_linear(dim=dim, hidden_dim=hidden_dim): |
| 118 | + lora_linear = LoRALinear( |
| 119 | + in_dim=dim, |
| 120 | + out_dim=hidden_dim, |
| 121 | + rank=RANK, |
| 122 | + alpha=ALPHA, |
| 123 | + ) |
| 124 | + fixed_init_model(lora_linear) |
| 125 | + return lora_linear |
| 126 | + |
| 127 | + return create_lora_linear |
| 128 | + |
| 129 | + def test_lora_tc_layer_forward(self, lora_linear, lora_experts, inputs): |
| 130 | + """Compare TC forward with LoRALinear as reference""" |
| 131 | + lora = lora_linear() |
| 132 | + actual = lora_experts._lora_tc_layer_forward( |
| 133 | + inputs[0], |
| 134 | + lora.weight.T, |
| 135 | + lora.lora_a.weight.T, |
| 136 | + lora.lora_b.weight.T, |
| 137 | + ) |
| 138 | + expected = lora(inputs[0]) |
| 139 | + assert_expected(actual, expected, rtol=1e-6, atol=1e-4) |
| 140 | + |
| 141 | + def test_forward_disabled( |
| 142 | + self, experts, lora_experts, inputs, num_tokens_per_expert |
| 143 | + ): |
| 144 | + """Test forward with lora layers disabled and comparing with GroupedExperts""" |
| 145 | + lora_experts.disabled = True |
| 146 | + actual = lora_experts(inputs, num_tokens_per_expert) |
| 147 | + expected = experts(inputs, num_tokens_per_expert) |
| 148 | + assert_expected(actual, expected, rtol=1e-6, atol=1e-4) |
| 149 | + |
| 150 | + def test_forward( |
| 151 | + self, |
| 152 | + lora_experts, |
| 153 | + inputs, |
| 154 | + num_experts, |
| 155 | + experts_per_token, |
| 156 | + dim, |
| 157 | + num_tokens_per_expert, |
| 158 | + ) -> None: |
| 159 | + expected = torch.tensor(0.441491) |
| 160 | + actual = lora_experts(inputs, num_tokens_per_expert) |
| 161 | + assert actual.shape == (num_experts * experts_per_token, SEQ_LEN, dim) |
| 162 | + torch.testing.assert_close(actual.mean(), expected, atol=1e-4, rtol=1e-6) |
0 commit comments