test_punica.py 6.62 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Based on code from https://github.com/punica-ai/punica

import pytest
import torch

import vllm.lora.punica as punica


def assert_close(a, b):
    rtol, atol = {
        torch.float16: (5e-3, 5e-3),
        torch.bfloat16: (3e-2, 2e-2),
        torch.float32: (None, None),
    }[a.dtype]
    torch.testing.assert_close(a, b, rtol=rtol, atol=atol)


def _lora_ref_impl(
    y_final: torch.Tensor,
    x: torch.Tensor,
    wa_T_all: torch.Tensor,
    wb_T_all: torch.Tensor,
    indicies: torch.LongTensor,
    layer_idx: int,
    scale: float,
):
    y_stage_1 = torch.empty(
        (x.size(0), wa_T_all.size(-2)),
        dtype=torch.float32,
        device=x.device,
    )
    bs = x.shape[0]
    s = torch.tensor(scale, dtype=torch.float32, device=x.device)
    for i, lora_idx in zip(range(bs), indicies.cpu().tolist()):
        xi = x[i].unsqueeze(0).to(torch.float32)
        wa = wa_T_all[lora_idx, layer_idx].transpose(-1, -2).to(torch.float32)
37
38
39
        if wb_T_all is not None:
            wb = wb_T_all[lora_idx, layer_idx].transpose(-1,
                                                         -2).to(torch.float32)
40
41
42

        tmp = xi @ wa
        y_stage_1[i] = tmp.squeeze(0)
43
44
        y_final[i] += ((tmp @ wb).squeeze(0) *
                       s if wb_T_all is not None else y_stage_1[i])
45
46
47
48
    return y_final, y_stage_1


H1 = H2 = [
49
50
51
    128,
    256,
    512,
52
    896,
53
54
    1024,
    1152,
55
    1216,
56
57
    1280,
    1536,
58
    1664,
59
    2048,
60
    2240,
61
    2304,
62
63
    2368,
    2432,
64
65
66
    2560,
    2752,
    3072,
67
    3328,
68
69
    3456,
    3584,
70
    3712,
71
    4096,
72
    4480,
73
    4608,
74
75
    4736,
    4864,
76
77
78
    5120,
    5504,
    5632,
79
    5888,
80
    6144,
81
    6400,
82
83
84
    6848,
    6912,
    7168,
85
    7424,
86
    8192,
87
    8960,
88
    9216,
89
    9472,
90
91
    10240,
    11008,
92
    11264,
93
94
    13824,
    14336,
95
96
    14784,
    14848,
97
    15360,
98
    18944,
99
    22016,
100
    22528,
101
102
    24576,
    27392,
103
    27648,
104
105
    29568,
    29696,
106
107
108
109
110
111
    32000,
    32256,
    32512,
    32768,
    33024,
    36864,
112
    43264,
113
    49152,
114
    49408,
115
116
    60544,
    60672,
117
118
119
120
121
122
    64000,
    64256,
    102400,
    102656,
    128000,
    128256,
123
]
124
125
H2 = [64] + H2
R = [1, 2, 4]
126
SEED = [0xabcdabcd987]
127
128
129
CUDA_DEVICES = [
    f"cuda:{i}" for i in range(1 if torch.cuda.device_count() == 1 else 2)
]
130
131


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
@pytest.mark.parametrize("dtype_str", ["float16", "bfloat16"])
@pytest.mark.parametrize("h1", H1)
@pytest.mark.parametrize("r", R)
@pytest.mark.parametrize("seed", SEED)
@torch.inference_mode()
def test_lora_a_extra_shapes(dtype_str, h1, r, seed):
    torch.manual_seed(seed)
    num_loras = 4
    num_layers = 1
    bs = 32
    dtype = getattr(torch, dtype_str)
    device = torch.device("cuda")

    wa_T_all = torch.randn(num_loras,
                           num_layers,
                           r,
                           h1,
                           dtype=dtype,
                           device=device)
    indices = torch.randint(num_loras, (bs, ), dtype=torch.long, device=device)

    for layer_idx in range(num_layers):
        x = torch.randn(bs, h1, dtype=dtype, device=device)
        y = torch.randn(bs, r, dtype=dtype, device=device)

        y_ref = y.clone()
        _lora_ref_impl(
            y_ref,
            x,
            wa_T_all,
            None,
            indices,
            layer_idx,
            1.0,
        )

        y_our = y.clone()
        punica.bgmv(y_our, x, wa_T_all, indices, layer_idx, 1.0)

        assert_close(y_ref, y_our)


174
175
176
177
@pytest.mark.parametrize("dtype_str", ["float16", "bfloat16"])
@pytest.mark.parametrize("h1", H1)
@pytest.mark.parametrize("h2", H2)
@pytest.mark.parametrize("seed", SEED)
178
@pytest.mark.parametrize("device", CUDA_DEVICES)
179
@torch.inference_mode()
180
def test_lora_correctness(dtype_str, h1, h2, seed, device):
181
182
183
184
185
186
187
    torch.manual_seed(seed)
    num_loras = 4
    num_layers = 1
    r = 8
    bs = 32
    scale = 0.123
    dtype = getattr(torch, dtype_str)
188
189
190
191
192
    torch.set_default_device(device)

    wa_T_all = torch.randn(num_loras, num_layers, r, h1, dtype=dtype)
    wb_T_all = torch.randn(num_loras, num_layers, h2, r, dtype=dtype)
    indices = torch.randint(num_loras, (bs, ), dtype=torch.long)
193
194

    for layer_idx in range(num_layers):
195
196
        x = torch.randn(bs, h1, dtype=dtype)
        y = torch.randn(bs, h2, dtype=dtype)
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211

        y_ref = y.clone()
        _lora_ref_impl(y_ref, x, wa_T_all, wb_T_all, indices, layer_idx, scale)

        y_our = y.clone()
        punica.add_lora(y_our, x, wa_T_all, wb_T_all, indices, layer_idx,
                        scale)

        assert_close(y_ref, y_our)


@pytest.mark.parametrize("dtype_str", ["float16", "bfloat16"])
@pytest.mark.parametrize("h1", H1)
@pytest.mark.parametrize("h2", H2)
@pytest.mark.parametrize("seed", SEED)
212
@pytest.mark.parametrize("device", CUDA_DEVICES)
213
@torch.inference_mode()
214
def test_lora_correctness_slice(dtype_str, h1, h2, seed, device):
215
216
217
218
219
220
221
222
223
    if h2 % 3 != 0 or h2 // 3 not in H1:
        pytest.skip("h2 must be divisible by 3 and in supported shapes")
    torch.manual_seed(seed)
    num_loras = 4
    num_layers = 1
    r = 8
    bs = 32
    scale = 0.123
    dtype = getattr(torch, dtype_str)
224
225
226
227
228
229
230
231
232
233
    torch.set_default_device(device)

    wa_T_all_0 = torch.randn(num_loras, num_layers, r, h1, dtype=dtype)
    wa_T_all_1 = torch.randn(num_loras, num_layers, r, h1, dtype=dtype)
    wa_T_all_2 = torch.randn(num_loras, num_layers, r, h1, dtype=dtype)
    wb_T_all_0 = torch.randn(num_loras, num_layers, h2 // 3, r, dtype=dtype)
    wb_T_all_1 = torch.randn(num_loras, num_layers, h2 // 3, r, dtype=dtype)
    wb_T_all_2 = torch.randn(num_loras, num_layers, h2 // 3, r, dtype=dtype)

    indices = torch.randint(num_loras, (bs, ), dtype=torch.long)
234
235

    for layer_idx in range(num_layers):
236
237
        x = torch.randn(bs, h1, dtype=dtype)
        y = torch.randn(bs, h2, dtype=dtype)
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
        s = h2 // 3

        y_ref = y.clone()
        _lora_ref_impl(y_ref[:, :s], x, wa_T_all_0, wb_T_all_0, indices,
                       layer_idx, scale)
        _lora_ref_impl(y_ref[:, s:s * 2], x, wa_T_all_1, wb_T_all_1, indices,
                       layer_idx, scale)
        _lora_ref_impl(y_ref[:, s * 2:], x, wa_T_all_2, wb_T_all_2, indices,
                       layer_idx, scale)

        y_our = y.clone()
        punica.add_lora_slice(y_our, x, wa_T_all_0, wb_T_all_0, indices,
                              layer_idx, scale, 0, s)
        punica.add_lora_slice(y_our, x, wa_T_all_1, wb_T_all_1, indices,
                              layer_idx, scale, s, s)
        punica.add_lora_slice(y_our, x, wa_T_all_2, wb_T_all_2, indices,
                              layer_idx, scale, s * 2, s)

        assert_close(y_ref[:, :s], y_our[:, :s])
        assert_close(y_ref[:, s:s * 2], y_our[:, s:s * 2])
        assert_close(y_ref[:, s * 2:], y_our[:, s * 2:])