test_activation.py 3.37 KB
Newer Older
1
import random
2
3
from typing import Type

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

7
from tests.kernels.utils import opcheck
8
9
10
from vllm.model_executor.layers.activation import (FastGELU, FatreluAndMul,
                                                   GeluAndMul, NewGELU,
                                                   QuickGELU, SiluAndMul)
11
from vllm.platforms import current_platform
Woosuk Kwon's avatar
Woosuk Kwon committed
12

13
14
from .allclose_default import get_default_atol, get_default_rtol

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

Woosuk Kwon's avatar
Woosuk Kwon committed
23

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

62
63
64
    d = x.shape[-1] // 2
    output_shape = (x.shape[:-1] + (d, ))
    out = torch.empty(output_shape, dtype=x.dtype, device=x.device)
65
66
67
68
    if activation == "fatrelu":
        opcheck(fn, (out, x, threshold))
    else:
        opcheck(fn, (out, x))
Woosuk Kwon's avatar
Woosuk Kwon committed
69

70
71
72
73

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

    out = torch.empty_like(x)
    opcheck(fn, (out, x))