test_activation.py 1.92 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
10
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]

Woosuk Kwon's avatar
Woosuk Kwon committed
11

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


32
33
34
35
@pytest.mark.parametrize("num_tokens", NUM_TOKENS)
@pytest.mark.parametrize("d", D)
@pytest.mark.parametrize("dtype", DTYPES)
@pytest.mark.parametrize("seed", SEEDS)
36
@torch.inference_mode()
37
def test_gelu_new(
38
39
40
    num_tokens: int,
    d: int,
    dtype: torch.dtype,
41
    seed: int,
42
) -> None:
43
44
    torch.random.manual_seed(seed)
    torch.cuda.manual_seed(seed)
45
    x = torch.randn(num_tokens, d, dtype=dtype, device="cuda")
46
47
48
    layer = NewGELU()
    out = layer(x)
    ref_out = layer._forward(x)
49
50
51
    assert torch.allclose(out, ref_out, atol=1e-5, rtol=1e-5)


52
53
54
55
56
@pytest.mark.parametrize("num_tokens", NUM_TOKENS)
@pytest.mark.parametrize("d", D)
@pytest.mark.parametrize("dtype", DTYPES)
@pytest.mark.parametrize("seed", SEEDS)
def test_gelu_fast(
57
58
59
    num_tokens: int,
    d: int,
    dtype: torch.dtype,
60
    seed: int,
61
) -> None:
62
63
    torch.random.manual_seed(seed)
    torch.cuda.manual_seed(seed)
64
    x = torch.randn(num_tokens, d, dtype=dtype, device="cuda")
65
66
67
    layer = FastGELU()
    out = layer(x)
    ref_out = layer._forward(x)
68
    assert torch.allclose(out, ref_out, atol=1e-5, rtol=1e-5)