test_activation.py 2.38 KB
Newer Older
1
import pytest
Woosuk Kwon's avatar
Woosuk Kwon committed
2
import torch
3

4
from vllm.model_executor.layers.activation import FastGELU, NewGELU, SiluAndMul
Woosuk Kwon's avatar
Woosuk Kwon committed
5

6
7
8
9
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]
10
11
12
CUDA_DEVICES = [
    f"cuda:{i}" for i in range(1 if torch.cuda.device_count() == 1 else 2)
]
13

Woosuk Kwon's avatar
Woosuk Kwon committed
14

15
16
17
18
@pytest.mark.parametrize("num_tokens", NUM_TOKENS)
@pytest.mark.parametrize("d", D)
@pytest.mark.parametrize("dtype", DTYPES)
@pytest.mark.parametrize("seed", SEEDS)
19
@pytest.mark.parametrize("device", CUDA_DEVICES)
Woosuk Kwon's avatar
Woosuk Kwon committed
20
@torch.inference_mode()
21
def test_silu_and_mul(
Woosuk Kwon's avatar
Woosuk Kwon committed
22
23
24
    num_tokens: int,
    d: int,
    dtype: torch.dtype,
25
    seed: int,
26
    device: str,
Woosuk Kwon's avatar
Woosuk Kwon committed
27
) -> None:
28
    torch.random.manual_seed(seed)
29
30
31
32
    if torch.cuda.is_available():
        torch.cuda.manual_seed(seed)
    torch.set_default_device(device)
    x = torch.randn(num_tokens, 2 * d, dtype=dtype)
33
34
35
    layer = SiluAndMul()
    out = layer(x)
    ref_out = layer._forward(x)
Woosuk Kwon's avatar
Woosuk Kwon committed
36
37
38
    assert torch.allclose(out, ref_out, atol=1e-5, rtol=1e-5)


39
40
41
42
@pytest.mark.parametrize("num_tokens", NUM_TOKENS)
@pytest.mark.parametrize("d", D)
@pytest.mark.parametrize("dtype", DTYPES)
@pytest.mark.parametrize("seed", SEEDS)
43
@pytest.mark.parametrize("device", CUDA_DEVICES)
44
@torch.inference_mode()
45
def test_gelu_new(
46
47
48
    num_tokens: int,
    d: int,
    dtype: torch.dtype,
49
    seed: int,
50
    device: str,
51
) -> None:
52
    torch.random.manual_seed(seed)
53
54
55
56
    if torch.cuda.is_available():
        torch.cuda.manual_seed(seed)
    torch.set_default_device(device)
    x = torch.randn(num_tokens, d, dtype=dtype)
57
58
59
    layer = NewGELU()
    out = layer(x)
    ref_out = layer._forward(x)
60
61
62
    assert torch.allclose(out, ref_out, atol=1e-5, rtol=1e-5)


63
64
65
66
@pytest.mark.parametrize("num_tokens", NUM_TOKENS)
@pytest.mark.parametrize("d", D)
@pytest.mark.parametrize("dtype", DTYPES)
@pytest.mark.parametrize("seed", SEEDS)
67
@pytest.mark.parametrize("device", CUDA_DEVICES)
68
def test_gelu_fast(
69
70
71
    num_tokens: int,
    d: int,
    dtype: torch.dtype,
72
    seed: int,
73
    device: str,
74
) -> None:
75
    torch.random.manual_seed(seed)
76
77
78
79
    if torch.cuda.is_available():
        torch.cuda.manual_seed(seed)
    torch.set_default_device(device)
    x = torch.randn(num_tokens, d, dtype=dtype)
80
81
82
    layer = FastGELU()
    out = layer(x)
    ref_out = layer._forward(x)
83
    assert torch.allclose(out, ref_out, atol=1e-5, rtol=1e-5)