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

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

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

9
10
from .allclose_default import get_default_atol, get_default_rtol

11
12
13
14
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]
15
16
17
CUDA_DEVICES = [
    f"cuda:{i}" for i in range(1 if torch.cuda.device_count() == 1 else 2)
]
18

Woosuk Kwon's avatar
Woosuk Kwon committed
19

20
@pytest.mark.parametrize("activation", ["silu", "gelu", "gelu_tanh"])
21
22
23
24
@pytest.mark.parametrize("num_tokens", NUM_TOKENS)
@pytest.mark.parametrize("d", D)
@pytest.mark.parametrize("dtype", DTYPES)
@pytest.mark.parametrize("seed", SEEDS)
25
@pytest.mark.parametrize("device", CUDA_DEVICES)
Woosuk Kwon's avatar
Woosuk Kwon committed
26
@torch.inference_mode()
27
def test_act_and_mul(
28
    activation: str,
Woosuk Kwon's avatar
Woosuk Kwon committed
29
30
31
    num_tokens: int,
    d: int,
    dtype: torch.dtype,
32
    seed: int,
33
    device: str,
Woosuk Kwon's avatar
Woosuk Kwon committed
34
) -> None:
35
    torch.random.manual_seed(seed)
36
37
38
39
    if torch.cuda.is_available():
        torch.cuda.manual_seed(seed)
    torch.set_default_device(device)
    x = torch.randn(num_tokens, 2 * d, dtype=dtype)
40
41
42
43
44
45
    if activation == "silu":
        layer = SiluAndMul()
    elif activation == "gelu":
        layer = GeluAndMul(approximate="none")
    elif activation == "gelu_tanh":
        layer = GeluAndMul(approximate="tanh")
46
    out = layer(x)
47
    ref_out = layer.forward_native(x)
48
49
    # The SiLU and GELU implementations are equivalent to the native PyTorch
    # implementations, so we can do exact comparison.
50
    torch.testing.assert_close(out, ref_out, atol=0.0, rtol=0.0)
Woosuk Kwon's avatar
Woosuk Kwon committed
51
52


53
@pytest.mark.parametrize("activation", [FastGELU, NewGELU])
54
55
56
57
@pytest.mark.parametrize("num_tokens", NUM_TOKENS)
@pytest.mark.parametrize("d", D)
@pytest.mark.parametrize("dtype", DTYPES)
@pytest.mark.parametrize("seed", SEEDS)
58
@pytest.mark.parametrize("device", CUDA_DEVICES)
59
@torch.inference_mode()
60
61
def test_activation(
    activation: Type[torch.nn.Module],
62
63
64
    num_tokens: int,
    d: int,
    dtype: torch.dtype,
65
    seed: int,
66
    device: str,
67
) -> None:
68
    torch.random.manual_seed(seed)
69
70
71
72
    if torch.cuda.is_available():
        torch.cuda.manual_seed(seed)
    torch.set_default_device(device)
    x = torch.randn(num_tokens, d, dtype=dtype)
73
    layer = activation()
74
    out = layer(x)
75
    ref_out = layer.forward_native(x)
76
77
78
79
    torch.testing.assert_close(out,
                               ref_out,
                               atol=get_default_atol(out),
                               rtol=get_default_rtol(out))