test_punica_ops_variation.py 8.37 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
"""
3
4
This script is mainly used to test whether trtion kernels can run normally
under different conditions, including various batches, numbers of LoRA , and
5
6
maximum ranks.
"""
7
8
from threading import Lock

9
10
11
import pytest
import torch

12
# Enable custom op register
13
14
15
16
17
import vllm.lora.ops.triton_ops  # noqa: F401
from vllm.lora.ops.torch_ops import (bgmv_expand, bgmv_expand_slice,
                                     bgmv_shrink, sgmv_expand,
                                     sgmv_expand_slice, sgmv_shrink)
from vllm.lora.ops.triton_ops.utils import _LORA_A_PTR_DICT, _LORA_B_PTR_DICT
18
from vllm.platforms import current_platform
19

20
21
from .utils import (assert_close, generate_data,
                    generate_data_for_expand_nslices,
22
                    generate_data_for_nslices)
23

24
HIDDEN_SIZES = [2049]
25
26

BATCHES = [1, 4, 16, 32]
27
NUM_LORA = [1, 8, 32, 128]
28
DTYPES = [torch.float16, torch.bfloat16]
29
MAX_RANKS = [1, 4, 8, 16, 32, 64, 128, 256]
30
31
SCALES = [0.5]
SEED = [0]
32
DEVICES = [f"cuda:{0}"]
33

34
35
_dict_lock = Lock()

36

37
38
39
40
41
@pytest.mark.parametrize("batches", BATCHES)
@pytest.mark.parametrize("num_loras", NUM_LORA)
@pytest.mark.parametrize("rank", MAX_RANKS)
@pytest.mark.parametrize("hidden_size", HIDDEN_SIZES)
@pytest.mark.parametrize("scaling", SCALES)
42
@pytest.mark.parametrize("nslices", [1, 2, 3])
43
44
45
@pytest.mark.parametrize("dtype", DTYPES)
@pytest.mark.parametrize("op_type", ["shrink", "expand"])
@pytest.mark.parametrize("seed", SEED)
46
@pytest.mark.parametrize("device", DEVICES)
47
48
49
50
51
52
def test_punica_sgmv(
    batches: int,
    num_loras: int,
    rank: int,
    hidden_size: int,
    scaling: float,
53
    nslices: int,
54
55
56
57
58
59
    dtype: torch.dtype,
    op_type: str,
    seed: int,
    device: str,
):
    torch.set_default_device(device)
60
    current_platform.seed_everything(seed)
61
62
63
64

    seq_length = 128
    (
        inputs_tensor,
65
        lora_weights_lst,
66
67
68
69
70
71
        our_out_tensor,
        ref_out_tensor,
        b_seq_start_loc,
        lora_indices_tensor,
        seq_len_tensor,
        indices,
72
    ) = generate_data_for_nslices(
73
74
75
76
77
        batches,
        hidden_size,
        num_loras,
        rank,
        seq_length,
78
        nslices,
79
80
81
82
83
        dtype,
        op_type,
        device,
    )
    max_seq_length = seq_len_tensor.max()
84
    token_nums = seq_len_tensor.sum().item()
85
86
87
88
89
    if isinstance(max_seq_length, tuple):
        max_seq_length = max_seq_length[0].item()
    else:
        max_seq_length = max_seq_length.item()
    if op_type == "shrink":
90
91
92
        # Preventing cache error pointer.
        with _dict_lock:
            _LORA_A_PTR_DICT.clear()
93
            torch.ops.vllm.sgmv_shrink(
94
95
96
97
98
99
100
101
102
103
104
105
                inputs_tensor,
                lora_weights_lst,
                our_out_tensor,
                b_seq_start_loc,
                seq_len_tensor,
                lora_indices_tensor,
                batches,
                max_seq_length,
                token_nums,
                scaling,
            )
        for index in range(nslices):
106
            sgmv_shrink(
107
108
                inputs_tensor,
                lora_weights_lst[index],
109
110
                ref_out_tensor[index],
                b_seq_start_loc,
111
                seq_len_tensor,
112
                lora_indices_tensor,
113
                batches,
114
115
                max_seq_length,
                token_nums,
116
117
                scaling,
            )
118

119
    else:
120
121
        with _dict_lock:
            _LORA_B_PTR_DICT.clear()
122
            torch.ops.vllm.sgmv_expand(
123
124
125
126
127
128
129
130
131
132
133
134
135
                inputs_tensor,
                lora_weights_lst,
                our_out_tensor,
                b_seq_start_loc,
                seq_len_tensor,
                lora_indices_tensor,
                batches,
                max_seq_length,
                token_nums,
                offset_start=0,
                add_inputs=True,
            )
        slice_offset = 0
136
137
138
139
140
141
142
        if nslices == 1:
            # Verify the torch's sgmv_expand op
            sgmv_expand(
                inputs_tensor[0],
                lora_weights_lst[0],
                ref_out_tensor,
                b_seq_start_loc,
143
                seq_len_tensor,
144
                lora_indices_tensor,
145
                batches,
146
147
148
                max_seq_length,
                token_nums,
                add_inputs=True,
149
            )
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
        else:
            for index in range(nslices):
                lora_weights = lora_weights_lst[index]
                sgmv_expand_slice(
                    inputs_tensor[index],
                    lora_weights,
                    ref_out_tensor,
                    b_seq_start_loc,
                    seq_len_tensor,
                    lora_indices_tensor,
                    batches,
                    max_seq_length,
                    token_nums,
                    slice_offset,
                    hidden_size,
                    add_inputs=True,
                )
                slice_offset += hidden_size
168

169
170
171
172
173
174
175
176
177
178
179
    assert_close(our_out_tensor, ref_out_tensor)


@pytest.mark.parametrize("batches", BATCHES)
@pytest.mark.parametrize("num_loras", NUM_LORA)
@pytest.mark.parametrize("rank", MAX_RANKS)
@pytest.mark.parametrize("hidden_size", HIDDEN_SIZES)
@pytest.mark.parametrize("scaling", SCALES)
@pytest.mark.parametrize("dtype", DTYPES)
@pytest.mark.parametrize("op_type", ["shrink", "expand"])
@pytest.mark.parametrize("seed", SEED)
180
@pytest.mark.parametrize("device", DEVICES)
181
182
183
184
185
186
187
188
189
190
191
192
def test_punica_bgmv(
    batches: int,
    num_loras: int,
    rank: int,
    hidden_size: int,
    scaling: float,
    dtype: torch.dtype,
    op_type: str,
    seed: int,
    device: str,
):
    torch.set_default_device(device)
193
    current_platform.seed_everything(seed)
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215

    seq_length = 1
    (
        inputs_tensor,
        lora_weights,
        our_out_tensor,
        ref_out_tensor,
        b_seq_start_loc,
        lora_indices_tensor,
        seq_len_tensor,
        indices,
    ) = generate_data(
        batches,
        hidden_size,
        num_loras,
        rank,
        seq_length,
        dtype,
        op_type,
        device,
    )
    if op_type == "shrink":
216
        torch.ops.vllm.bgmv_shrink(
217
218
219
220
221
222
223
            inputs_tensor,
            lora_weights,
            our_out_tensor,
            indices,
            scaling,
        )

224
225
226
227
228
229
230
231
232
233
        bgmv_shrink(
            inputs_tensor,
            lora_weights,
            ref_out_tensor,
            indices,
            scaling,
        )

    else:
        torch.ops.vllm.bgmv_expand(
234
235
236
237
238
239
            inputs_tensor,
            lora_weights,
            our_out_tensor,
            indices,
            add_inputs=True,
        )
240
241
242
243
244
245
246
247
        bgmv_expand(
            inputs_tensor,
            lora_weights,
            ref_out_tensor,
            indices,
            add_inputs=True,
        )

248
249
250
251
252
253
254
255
256
257
258
259
    if op_type == "shrink":
        ref_out_tensor = ref_out_tensor.to(torch.float32)
    assert_close(our_out_tensor, ref_out_tensor)


@pytest.mark.parametrize("batches", BATCHES)
@pytest.mark.parametrize("num_loras", NUM_LORA)
@pytest.mark.parametrize("rank", MAX_RANKS)
@pytest.mark.parametrize("hidden_size", HIDDEN_SIZES)
@pytest.mark.parametrize("nslices", [2, 3])
@pytest.mark.parametrize("dtype", DTYPES)
@pytest.mark.parametrize("seed", SEED)
260
@pytest.mark.parametrize("device", DEVICES)
261
def test_punica_bgmv_expand_nslices(
262
263
264
265
266
267
268
269
270
271
    batches: int,
    num_loras: int,
    rank: int,
    hidden_size: int,
    nslices: int,
    dtype: torch.dtype,
    seed: int,
    device: str,
):
    torch.set_default_device(device)
272
    current_platform.seed_everything(seed)
273

274
    seq_length = 1
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
    (
        inputs_tensor,
        lora_weights_lst,
        our_outputs,
        ref_outputs,
        b_seq_start_loc,
        lora_indices_tensor,
        seq_len_tensor,
        indices,
    ) = generate_data_for_expand_nslices(
        batches,
        hidden_size,
        num_loras,
        rank,
        seq_length,
        dtype,
        nslices,
        device,
    )
    slice_offset = 0
    for index in range(nslices):
        lora_weights = lora_weights_lst[index]
297
        torch.ops.vllm.bgmv_expand_slice(
298
299
300
301
302
303
304
305
            inputs_tensor,
            lora_weights,
            our_outputs,
            indices,
            slice_offset,
            slice_size=hidden_size,
            add_inputs=True,
        )
306
        bgmv_expand_slice(
307
308
            inputs_tensor,
            lora_weights,
309
310
311
312
313
            ref_outputs,
            indices,
            slice_offset,
            slice_size=hidden_size,
            add_inputs=True,
314
315
316
317
        )

        slice_offset += hidden_size
    assert_close(our_outputs, ref_outputs)