main
py 49 lines 1.44 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4 import pytest
5 import torch
6 import torch.nn.functional as F
7
8 from vllm.model_executor.layers import utils as layer_utils
9 from vllm.utils.flashinfer import is_flashinfer_cutedsl_bf16_gemm_supported
10
11
12 @pytest.fixture(scope="module", autouse=True)
13 def require_flashinfer_bf16_cutedsl() -> None:
14 if not torch.accelerator.is_available():
15 pytest.skip("CUDA is required")
16 if not is_flashinfer_cutedsl_bf16_gemm_supported():
17 pytest.skip("FlashInfer BF16 cute-dsl backend is unavailable")
18
19
20 @pytest.mark.parametrize("n,k", [(1024, 1024), (2048, 3072)])
21 @pytest.mark.parametrize(
22 "m,use_bias,pdl",
23 [
24 (1, False, False),
25 (8, True, False),
26 (16, False, True),
27 (32, True, True),
28 ],
29 )
30 def test_flashinfer_bf16_cutedsl_correctness(
31 m: int,
32 n: int,
33 k: int,
34 use_bias: bool,
35 pdl: bool,
36 ) -> None:
37 torch.manual_seed(0)
38 x = torch.randn(m, k, device="cuda", dtype=torch.bfloat16) * 0.1
39 weight = torch.randn(n, k, device="cuda", dtype=torch.bfloat16) * 0.1
40 bias = (
41 torch.randn(n, device="cuda", dtype=torch.bfloat16) * 0.1 if use_bias else None
42 )
43
44 actual = layer_utils.cuda_flashinfer_bf16_gemm_impl(
45 x, weight, bias, pdl, "flashinfer_cutedsl"
46 )
47 expected = F.linear(x, weight, bias)
48
49 torch.testing.assert_close(actual, expected, rtol=2e-2, atol=2e-1)