test_activation.py 3.75 KB
Newer Older
1
2
from typing import Type

3
import pytest
Woosuk Kwon's avatar
Woosuk Kwon committed
4
import torch
5

6
from vllm.model_executor.layers.activation import (FastGELU, GeluAndMul,
7
8
                                                   NewGELU, QuickGELU,
                                                   SiluAndMul)
9
from vllm.utils import seed_everything
Woosuk Kwon's avatar
Woosuk Kwon committed
10

11
from .allclose_default import get_default_atol, get_default_rtol
12
from .utils import torch_version
13

14
15
16
17
DTYPES = [torch.half, torch.bfloat16, torch.float]
NUM_TOKENS = [7, 83, 2048]  # Arbitrary values for testing
D = [512, 4096, 5120, 13824]  # Arbitrary values for testing
SEEDS = [0]
18
19
20
CUDA_DEVICES = [
    f"cuda:{i}" for i in range(1 if torch.cuda.device_count() == 1 else 2)
]
21

Woosuk Kwon's avatar
Woosuk Kwon committed
22

23
@pytest.mark.parametrize("activation", ["silu", "gelu", "gelu_tanh"])
24
25
26
27
@pytest.mark.parametrize("num_tokens", NUM_TOKENS)
@pytest.mark.parametrize("d", D)
@pytest.mark.parametrize("dtype", DTYPES)
@pytest.mark.parametrize("seed", SEEDS)
28
@pytest.mark.parametrize("device", CUDA_DEVICES)
Woosuk Kwon's avatar
Woosuk Kwon committed
29
@torch.inference_mode()
30
def test_act_and_mul(
31
    activation: str,
Woosuk Kwon's avatar
Woosuk Kwon committed
32
33
34
    num_tokens: int,
    d: int,
    dtype: torch.dtype,
35
    seed: int,
36
    device: str,
Woosuk Kwon's avatar
Woosuk Kwon committed
37
) -> None:
38
    seed_everything(seed)
39
40
    torch.set_default_device(device)
    x = torch.randn(num_tokens, 2 * d, dtype=dtype)
41
42
    if activation == "silu":
        layer = SiluAndMul()
43
        fn = torch.ops._C.silu_and_mul
44
45
    elif activation == "gelu":
        layer = GeluAndMul(approximate="none")
46
        fn = torch.ops._C.gelu_and_mul
47
48
    elif activation == "gelu_tanh":
        layer = GeluAndMul(approximate="tanh")
49
        fn = torch.ops._C.gelu_tanh_and_mul
50
    out = layer(x)
51
    ref_out = layer.forward_native(x)
52
53
54
55
56
57
58
59
    
    if torch_version.startswith("2.3"):
        assert torch.allclose(out, ref_out, atol=0.0, rtol=0.0)
    elif torch_version.startswith("2.4"):
        from tests.kernels.utils import opcheck 
        # The SiLU and GELU implementations are equivalent to the native PyTorch
        # implementations, so we can do exact comparison.
        torch.testing.assert_close(out, ref_out, atol=0.0, rtol=0.0)
Woosuk Kwon's avatar
Woosuk Kwon committed
60

61
62
63
64
65
66
        d = x.shape[-1] // 2
        output_shape = (x.shape[:-1] + (d, ))
        out = torch.empty(output_shape, dtype=x.dtype, device=x.device)
        opcheck(fn, (out, x))
    else:
        print(f"PyTorch version {torch_version} is not specifically handled.")
Woosuk Kwon's avatar
Woosuk Kwon committed
67

68
69
70
71

@pytest.mark.parametrize("activation", [(FastGELU, torch.ops._C.gelu_fast),
                                        (NewGELU, torch.ops._C.gelu_new),
                                        (QuickGELU, torch.ops._C.gelu_quick)])
72
73
74
75
@pytest.mark.parametrize("num_tokens", NUM_TOKENS)
@pytest.mark.parametrize("d", D)
@pytest.mark.parametrize("dtype", DTYPES)
@pytest.mark.parametrize("seed", SEEDS)
76
@pytest.mark.parametrize("device", CUDA_DEVICES)
77
@torch.inference_mode()
78
79
def test_activation(
    activation: Type[torch.nn.Module],
80
81
82
    num_tokens: int,
    d: int,
    dtype: torch.dtype,
83
    seed: int,
84
    device: str,
85
) -> None:
86
    seed_everything(seed)
87
88
    torch.set_default_device(device)
    x = torch.randn(num_tokens, d, dtype=dtype)
89
90
    layer = activation[0]()
    fn = activation[1]
91
    out = layer(x)
92
    ref_out = layer.forward_native(x)
93

94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
    if torch_version.startswith("2.3"):
            assert torch.allclose(out,
                          ref_out,
                          atol=get_default_atol(out),
                          rtol=get_default_rtol(out))
    elif torch_version.startswith("2.4"):
        from tests.kernels.utils import opcheck
        torch.testing.assert_close(out,
                                ref_out,
                                atol=get_default_atol(out),
                                rtol=get_default_rtol(out))

        out = torch.empty_like(x)
        opcheck(fn, (out, x))
    else:
        print(f"PyTorch version {torch_version} is not specifically handled.")